diff options
Diffstat (limited to 'third_party/jpeg-xl/lib/extras/packed_image.h')
-rw-r--r-- | third_party/jpeg-xl/lib/extras/packed_image.h | 55 |
1 files changed, 42 insertions, 13 deletions
diff --git a/third_party/jpeg-xl/lib/extras/packed_image.h b/third_party/jpeg-xl/lib/extras/packed_image.h index edd5f1be75..a66ddfbd70 100644 --- a/third_party/jpeg-xl/lib/extras/packed_image.h +++ b/third_party/jpeg-xl/lib/extras/packed_image.h @@ -37,11 +37,18 @@ namespace extras { // Class representing an interleaved image with a bunch of channels. class PackedImage { public: - PackedImage(size_t xsize, size_t ysize, const JxlPixelFormat& format) - : PackedImage(xsize, ysize, format, CalcStride(format, xsize)) {} + static StatusOr<PackedImage> Create(size_t xsize, size_t ysize, + const JxlPixelFormat& format) { + PackedImage image(xsize, ysize, format, CalcStride(format, xsize)); + if (!image.pixels()) { + // TODO(szabadka): use specialized OOM error code + return JXL_FAILURE("Failed to allocate memory for image"); + } + return image; + } PackedImage Copy() const { - PackedImage copy(xsize, ysize, format); + PackedImage copy(xsize, ysize, format, CalcStride(format, xsize)); memcpy(reinterpret_cast<uint8_t*>(copy.pixels()), reinterpret_cast<const uint8_t*>(pixels()), pixels_size); return copy; @@ -108,7 +115,7 @@ class PackedImage { } } - void SetPixelValue(size_t y, size_t x, size_t c, float val) { + void SetPixelValue(size_t y, size_t x, size_t c, float val) const { uint8_t* data = pixels(y, x, c); switch (format.data_type) { case JXL_TYPE_UINT8: @@ -169,17 +176,25 @@ class PackedImage { // as all other frames in the same image. class PackedFrame { public: - template <typename... Args> - explicit PackedFrame(Args&&... args) : color(std::forward<Args>(args)...) {} + explicit PackedFrame(PackedImage&& image) : color(std::move(image)) {} + + static StatusOr<PackedFrame> Create(size_t xsize, size_t ysize, + const JxlPixelFormat& format) { + JXL_ASSIGN_OR_RETURN(PackedImage image, + PackedImage::Create(xsize, ysize, format)); + PackedFrame frame(std::move(image)); + return frame; + } - PackedFrame Copy() const { - PackedFrame copy(color.xsize, color.ysize, color.format); + StatusOr<PackedFrame> Copy() const { + JXL_ASSIGN_OR_RETURN( + PackedFrame copy, + PackedFrame::Create(color.xsize, color.ysize, color.format)); copy.frame_info = frame_info; copy.name = name; copy.color = color.Copy(); - for (size_t i = 0; i < extra_channels.size(); ++i) { - PackedImage ec = extra_channels[i].Copy(); - copy.extra_channels.emplace_back(std::move(ec)); + for (const auto& ec : extra_channels) { + copy.extra_channels.emplace_back(ec.Copy()); } return copy; } @@ -244,12 +259,26 @@ class PackedPixelFile { std::vector<PackedExtraChannel> extra_channels_info; // Color information of the decoded pixels. - // If the icc is empty, the JxlColorEncoding should be used instead. - std::vector<uint8_t> icc; + // `primary_color_representation` indicates whether `color_encoding` or `icc` + // is the “authoritative” encoding of the colorspace, as opposed to a fallback + // encoding. For example, if `color_encoding` is the primary one, as would + // occur when decoding a jxl file with such a representation, then `enc/jxl` + // will use it and ignore the ICC profile, whereas `enc/png` will include the + // ICC profile for compatibility. + // If `icc` is the primary representation, `enc/jxl` will preserve it when + // compressing losslessly, but *may* encode it as a color_encoding when + // compressing lossily. + enum { + kColorEncodingIsPrimary, + kIccIsPrimary + } primary_color_representation = kColorEncodingIsPrimary; JxlColorEncoding color_encoding = {}; + std::vector<uint8_t> icc; // The icc profile of the original image. std::vector<uint8_t> orig_icc; + JxlBitDepth input_bitdepth = {JXL_BIT_DEPTH_FROM_PIXEL_FORMAT, 0, 0}; + std::unique_ptr<PackedFrame> preview_frame; std::vector<PackedFrame> frames; mutable std::vector<ChunkedPackedFrame> chunked_frames; |