| 1 | // |
| 2 | // SPDX-License-Identifier: BSD-3-Clause |
| 3 | // Copyright (c) Contributors to the OpenEXR Project. |
| 4 | // |
| 5 | |
| 6 | |
| 7 | #ifndef INCLUDED_IMF_RGBA_FILE_H |
| 8 | #define INCLUDED_IMF_RGBA_FILE_H |
| 9 | |
| 10 | |
| 11 | //----------------------------------------------------------------------------- |
| 12 | // |
| 13 | // Simplified RGBA image I/O |
| 14 | // |
| 15 | // class RgbaOutputFile |
| 16 | // class RgbaInputFile |
| 17 | // |
| 18 | //----------------------------------------------------------------------------- |
| 19 | |
| 20 | #include "ImfExport.h" |
| 21 | #include "ImfNamespace.h" |
| 22 | |
| 23 | #include "ImfHeader.h" |
| 24 | #include "ImfFrameBuffer.h" |
| 25 | #include "ImfRgba.h" |
| 26 | |
| 27 | #include <ImathVec.h> |
| 28 | #include <ImathBox.h> |
| 29 | #include <half.h> |
| 30 | #include "ImfThreading.h" |
| 31 | #include <string> |
| 32 | |
| 33 | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER |
| 34 | |
| 35 | //------------------------------------------------------- |
| 36 | // Utility to compute the origin-based pointer address |
| 37 | // |
| 38 | // With large offsets for the data window, the naive code |
| 39 | // can wrap around, especially on 32-bit machines. |
| 40 | // This can be used to avoid that |
| 41 | //------------------------------------------------------- |
| 42 | |
| 43 | inline const Rgba * |
| 44 | ComputeBasePointer ( |
| 45 | const Rgba* ptr, |
| 46 | const IMATH_NAMESPACE::V2i& origin, |
| 47 | int64_t w, |
| 48 | size_t xStride = 1, |
| 49 | size_t yStride = 0) |
| 50 | { |
| 51 | if (yStride == 0) |
| 52 | yStride = w; |
| 53 | int64_t offx = static_cast<int64_t> (origin.x); |
| 54 | offx *= xStride; |
| 55 | int64_t offy = static_cast<int64_t> (origin.y); |
| 56 | offy *= yStride; |
| 57 | return ptr - offx - offy; |
| 58 | } |
| 59 | |
| 60 | inline const Rgba * |
| 61 | ComputeBasePointer (const Rgba* ptr, const IMATH_NAMESPACE::Box2i& dataWindow) |
| 62 | { |
| 63 | return ComputeBasePointer (ptr, origin: dataWindow.min, |
| 64 | w: static_cast<int64_t> (dataWindow.max.x) - |
| 65 | static_cast<int64_t> (dataWindow.min.x) + 1); |
| 66 | } |
| 67 | |
| 68 | inline Rgba* |
| 69 | ComputeBasePointer ( |
| 70 | Rgba* ptr, |
| 71 | const IMATH_NAMESPACE::V2i& origin, |
| 72 | int64_t w, |
| 73 | size_t xStride = 1, |
| 74 | size_t yStride = 0) |
| 75 | { |
| 76 | if (yStride == 0) |
| 77 | yStride = w; |
| 78 | int64_t offx = static_cast<int64_t> (origin.x); |
| 79 | offx *= xStride; |
| 80 | int64_t offy = static_cast<int64_t> (origin.y); |
| 81 | offy *= yStride; |
| 82 | return ptr - offx - offy; |
| 83 | } |
| 84 | |
| 85 | inline Rgba* |
| 86 | ComputeBasePointer (Rgba* ptr, const IMATH_NAMESPACE::Box2i& dataWindow) |
| 87 | { |
| 88 | return ComputeBasePointer ( |
| 89 | ptr, |
| 90 | origin: dataWindow.min, |
| 91 | w: static_cast<int64_t> (dataWindow.max.x) - |
| 92 | static_cast<int64_t> (dataWindow.min.x) + 1); |
| 93 | } |
| 94 | |
| 95 | // |
| 96 | // RGBA output file. |
| 97 | // |
| 98 | |
| 99 | class IMF_EXPORT_TYPE RgbaOutputFile |
| 100 | { |
| 101 | public: |
| 102 | |
| 103 | //--------------------------------------------------- |
| 104 | // Constructor -- header is constructed by the caller |
| 105 | //--------------------------------------------------- |
| 106 | |
| 107 | IMF_EXPORT |
| 108 | (const char name[], |
| 109 | const Header &, |
| 110 | RgbaChannels rgbaChannels = WRITE_RGBA, |
| 111 | int numThreads = globalThreadCount()); |
| 112 | |
| 113 | |
| 114 | //---------------------------------------------------- |
| 115 | // Constructor -- header is constructed by the caller, |
| 116 | // file is opened by the caller, destructor will not |
| 117 | // automatically close the file. |
| 118 | //---------------------------------------------------- |
| 119 | |
| 120 | IMF_EXPORT |
| 121 | (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os, |
| 122 | const Header &, |
| 123 | RgbaChannels rgbaChannels = WRITE_RGBA, |
| 124 | int numThreads = globalThreadCount()); |
| 125 | |
| 126 | |
| 127 | //---------------------------------------------------------------- |
| 128 | // Constructor -- header data are explicitly specified as function |
| 129 | // call arguments (empty dataWindow means "same as displayWindow") |
| 130 | //---------------------------------------------------------------- |
| 131 | |
| 132 | IMF_EXPORT |
| 133 | RgbaOutputFile (const char name[], |
| 134 | const IMATH_NAMESPACE::Box2i &displayWindow, |
| 135 | const IMATH_NAMESPACE::Box2i &dataWindow = IMATH_NAMESPACE::Box2i(), |
| 136 | RgbaChannels rgbaChannels = WRITE_RGBA, |
| 137 | float pixelAspectRatio = 1, |
| 138 | const IMATH_NAMESPACE::V2f screenWindowCenter = IMATH_NAMESPACE::V2f (0, 0), |
| 139 | float screenWindowWidth = 1, |
| 140 | LineOrder lineOrder = INCREASING_Y, |
| 141 | Compression compression = PIZ_COMPRESSION, |
| 142 | int numThreads = globalThreadCount()); |
| 143 | |
| 144 | |
| 145 | //----------------------------------------------- |
| 146 | // Constructor -- like the previous one, but both |
| 147 | // the display window and the data window are |
| 148 | // Box2i (V2i (0, 0), V2i (width - 1, height -1)) |
| 149 | //----------------------------------------------- |
| 150 | |
| 151 | IMF_EXPORT |
| 152 | RgbaOutputFile (const char name[], |
| 153 | int width, |
| 154 | int height, |
| 155 | RgbaChannels rgbaChannels = WRITE_RGBA, |
| 156 | float pixelAspectRatio = 1, |
| 157 | const IMATH_NAMESPACE::V2f screenWindowCenter = IMATH_NAMESPACE::V2f (0, 0), |
| 158 | float screenWindowWidth = 1, |
| 159 | LineOrder lineOrder = INCREASING_Y, |
| 160 | Compression compression = PIZ_COMPRESSION, |
| 161 | int numThreads = globalThreadCount()); |
| 162 | |
| 163 | |
| 164 | //----------- |
| 165 | // Destructor |
| 166 | //----------- |
| 167 | |
| 168 | IMF_EXPORT |
| 169 | virtual ~RgbaOutputFile (); |
| 170 | |
| 171 | |
| 172 | //------------------------------------------------ |
| 173 | // Define a frame buffer as the pixel data source: |
| 174 | // Pixel (x, y) is at address |
| 175 | // |
| 176 | // base + x * xStride + y * yStride |
| 177 | // |
| 178 | //------------------------------------------------ |
| 179 | |
| 180 | IMF_EXPORT |
| 181 | void setFrameBuffer (const Rgba *base, |
| 182 | size_t xStride, |
| 183 | size_t yStride); |
| 184 | |
| 185 | |
| 186 | //--------------------------------------------- |
| 187 | // Write pixel data (see class Imf::OutputFile) |
| 188 | //--------------------------------------------- |
| 189 | |
| 190 | IMF_EXPORT |
| 191 | void writePixels (int numScanLines = 1); |
| 192 | IMF_EXPORT |
| 193 | int currentScanLine () const; |
| 194 | |
| 195 | |
| 196 | //-------------------------- |
| 197 | // Access to the file header |
| 198 | //-------------------------- |
| 199 | |
| 200 | IMF_EXPORT |
| 201 | const Header & () const; |
| 202 | IMF_EXPORT |
| 203 | const FrameBuffer & frameBuffer () const; |
| 204 | IMF_EXPORT |
| 205 | const IMATH_NAMESPACE::Box2i & displayWindow () const; |
| 206 | IMF_EXPORT |
| 207 | const IMATH_NAMESPACE::Box2i & dataWindow () const; |
| 208 | IMF_EXPORT |
| 209 | float pixelAspectRatio () const; |
| 210 | IMF_EXPORT |
| 211 | const IMATH_NAMESPACE::V2f screenWindowCenter () const; |
| 212 | IMF_EXPORT |
| 213 | float screenWindowWidth () const; |
| 214 | IMF_EXPORT |
| 215 | LineOrder lineOrder () const; |
| 216 | IMF_EXPORT |
| 217 | Compression compression () const; |
| 218 | IMF_EXPORT |
| 219 | RgbaChannels channels () const; |
| 220 | |
| 221 | |
| 222 | // -------------------------------------------------------------------- |
| 223 | // Update the preview image (see Imf::OutputFile::updatePreviewImage()) |
| 224 | // -------------------------------------------------------------------- |
| 225 | |
| 226 | IMF_EXPORT |
| 227 | void updatePreviewImage (const PreviewRgba[]); |
| 228 | |
| 229 | |
| 230 | //----------------------------------------------------------------------- |
| 231 | // Rounding control for luminance/chroma images: |
| 232 | // |
| 233 | // If the output file contains luminance and chroma channels (WRITE_YC |
| 234 | // or WRITE_YCA), then the the significands of the luminance and |
| 235 | // chroma values are rounded to roundY and roundC bits respectively (see |
| 236 | // function half::round()). Rounding improves compression with minimal |
| 237 | // image degradation, usually much less than the degradation caused by |
| 238 | // chroma subsampling. By default, roundY is 7, and roundC is 5. |
| 239 | // |
| 240 | // If the output file contains RGB channels or a luminance channel, |
| 241 | // without chroma, then no rounding is performed. |
| 242 | //----------------------------------------------------------------------- |
| 243 | |
| 244 | IMF_EXPORT |
| 245 | void setYCRounding (unsigned int roundY, |
| 246 | unsigned int roundC); |
| 247 | |
| 248 | |
| 249 | //---------------------------------------------------- |
| 250 | // Break a scan line -- for testing and debugging only |
| 251 | // (see Imf::OutputFile::updatePreviewImage() |
| 252 | // |
| 253 | // Warning: Calling this function usually results in a |
| 254 | // broken image file. The file or parts of it may not |
| 255 | // be readable, or the file may contain bad data. |
| 256 | // |
| 257 | //---------------------------------------------------- |
| 258 | |
| 259 | IMF_EXPORT |
| 260 | void breakScanLine (int y, |
| 261 | int offset, |
| 262 | int length, |
| 263 | char c); |
| 264 | private: |
| 265 | |
| 266 | RgbaOutputFile (const RgbaOutputFile &) = delete; |
| 267 | RgbaOutputFile & operator = (const RgbaOutputFile &) = delete; |
| 268 | RgbaOutputFile (RgbaOutputFile &&) = delete; |
| 269 | RgbaOutputFile & operator = (RgbaOutputFile &&) = delete; |
| 270 | |
| 271 | class IMF_HIDDEN ToYca; |
| 272 | |
| 273 | OutputFile * _outputFile; |
| 274 | ToYca * _toYca; |
| 275 | }; |
| 276 | |
| 277 | |
| 278 | // |
| 279 | // RGBA input file |
| 280 | // |
| 281 | |
| 282 | class IMF_EXPORT_TYPE RgbaInputFile |
| 283 | { |
| 284 | public: |
| 285 | |
| 286 | //------------------------------------------------------- |
| 287 | // Constructor -- opens the file with the specified name, |
| 288 | // destructor will automatically close the file. |
| 289 | //------------------------------------------------------- |
| 290 | |
| 291 | IMF_EXPORT |
| 292 | RgbaInputFile (const char name[], int numThreads = globalThreadCount()); |
| 293 | |
| 294 | |
| 295 | //----------------------------------------------------------- |
| 296 | // Constructor -- attaches the new RgbaInputFile object to a |
| 297 | // file that has already been opened by the caller. |
| 298 | // Destroying the RgbaInputFile object will not automatically |
| 299 | // close the file. |
| 300 | //----------------------------------------------------------- |
| 301 | |
| 302 | IMF_EXPORT |
| 303 | RgbaInputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, int numThreads = globalThreadCount()); |
| 304 | |
| 305 | |
| 306 | //-------------------------------------------------------------- |
| 307 | // Constructors -- the same as the previous two, but the names |
| 308 | // of the red, green, blue, alpha, luminance and chroma channels |
| 309 | // are expected to be layerName.R, layerName.G, etc. |
| 310 | //-------------------------------------------------------------- |
| 311 | |
| 312 | IMF_EXPORT |
| 313 | RgbaInputFile (const char name[], |
| 314 | const std::string &layerName, |
| 315 | int numThreads = globalThreadCount()); |
| 316 | |
| 317 | IMF_EXPORT |
| 318 | RgbaInputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, |
| 319 | const std::string &layerName, |
| 320 | int numThreads = globalThreadCount()); |
| 321 | |
| 322 | |
| 323 | //----------- |
| 324 | // Destructor |
| 325 | //----------- |
| 326 | |
| 327 | IMF_EXPORT |
| 328 | virtual ~RgbaInputFile (); |
| 329 | |
| 330 | |
| 331 | //----------------------------------------------------- |
| 332 | // Define a frame buffer as the pixel data destination: |
| 333 | // Pixel (x, y) is at address |
| 334 | // |
| 335 | // base + x * xStride + y * yStride |
| 336 | // |
| 337 | //----------------------------------------------------- |
| 338 | |
| 339 | IMF_EXPORT |
| 340 | void setFrameBuffer (Rgba *base, |
| 341 | size_t xStride, |
| 342 | size_t yStride); |
| 343 | |
| 344 | |
| 345 | //---------------------------------------------------------------- |
| 346 | // Switch to a different layer -- subsequent calls to readPixels() |
| 347 | // will read channels layerName.R, layerName.G, etc. |
| 348 | // After each call to setLayerName(), setFrameBuffer() must be |
| 349 | // called at least once before the next call to readPixels(). |
| 350 | //---------------------------------------------------------------- |
| 351 | |
| 352 | IMF_EXPORT |
| 353 | void setLayerName (const std::string &layerName); |
| 354 | |
| 355 | |
| 356 | //------------------------------------------- |
| 357 | // Read pixel data (see class Imf::InputFile) |
| 358 | //------------------------------------------- |
| 359 | |
| 360 | IMF_EXPORT |
| 361 | void readPixels (int scanLine1, int scanLine2); |
| 362 | IMF_EXPORT |
| 363 | void readPixels (int scanLine); |
| 364 | |
| 365 | |
| 366 | //-------------------------- |
| 367 | // Access to the file header |
| 368 | //-------------------------- |
| 369 | |
| 370 | IMF_EXPORT |
| 371 | const Header & () const; |
| 372 | IMF_EXPORT |
| 373 | const FrameBuffer & frameBuffer () const; |
| 374 | IMF_EXPORT |
| 375 | const IMATH_NAMESPACE::Box2i & displayWindow () const; |
| 376 | IMF_EXPORT |
| 377 | const IMATH_NAMESPACE::Box2i & dataWindow () const; |
| 378 | IMF_EXPORT |
| 379 | float pixelAspectRatio () const; |
| 380 | IMF_EXPORT |
| 381 | const IMATH_NAMESPACE::V2f screenWindowCenter () const; |
| 382 | IMF_EXPORT |
| 383 | float screenWindowWidth () const; |
| 384 | IMF_EXPORT |
| 385 | LineOrder lineOrder () const; |
| 386 | IMF_EXPORT |
| 387 | Compression compression () const; |
| 388 | IMF_EXPORT |
| 389 | RgbaChannels channels () const; |
| 390 | IMF_EXPORT |
| 391 | const char * fileName () const; |
| 392 | IMF_EXPORT |
| 393 | bool isComplete () const; |
| 394 | |
| 395 | |
| 396 | //---------------------------------- |
| 397 | // Access to the file format version |
| 398 | //---------------------------------- |
| 399 | |
| 400 | IMF_EXPORT |
| 401 | int version () const; |
| 402 | |
| 403 | private: |
| 404 | |
| 405 | RgbaInputFile (const RgbaInputFile &) = delete; |
| 406 | RgbaInputFile & operator = (const RgbaInputFile &) = delete; |
| 407 | RgbaInputFile (RgbaInputFile &&) = delete; |
| 408 | RgbaInputFile & operator = (RgbaInputFile &&) = delete; |
| 409 | |
| 410 | class IMF_HIDDEN FromYca; |
| 411 | |
| 412 | InputFile * _inputFile; |
| 413 | FromYca * _fromYca; |
| 414 | std::string _channelNamePrefix; |
| 415 | }; |
| 416 | |
| 417 | |
| 418 | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | #endif |
| 425 | |