codec_in_out.h (3064B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #ifndef LIB_JXL_CODEC_IN_OUT_H_ 7 #define LIB_JXL_CODEC_IN_OUT_H_ 8 9 // Holds inputs/outputs for decoding/encoding images. 10 11 #include <stddef.h> 12 #include <stdint.h> 13 14 #include <type_traits> 15 #include <utility> 16 #include <vector> 17 18 #include "lib/jxl/base/data_parallel.h" 19 #include "lib/jxl/base/status.h" 20 #include "lib/jxl/frame_header.h" 21 #include "lib/jxl/headers.h" 22 #include "lib/jxl/image.h" 23 #include "lib/jxl/image_bundle.h" 24 #include "lib/jxl/luminance.h" 25 26 namespace jxl { 27 28 // Optional text/EXIF metadata. 29 struct Blobs { 30 std::vector<uint8_t> exif; 31 std::vector<uint8_t> iptc; 32 std::vector<uint8_t> jumbf; 33 std::vector<uint8_t> xmp; 34 }; 35 36 // Holds a preview, a main image or one or more frames, plus the inputs/outputs 37 // to/from decoding/encoding. 38 class CodecInOut { 39 public: 40 CodecInOut() : preview_frame(&metadata.m) { 41 frames.reserve(1); 42 frames.emplace_back(&metadata.m); 43 } 44 45 // Move-only. 46 CodecInOut(CodecInOut&&) = default; 47 CodecInOut& operator=(CodecInOut&&) = default; 48 49 size_t LastStillFrame() const { 50 JXL_DASSERT(!frames.empty()); 51 size_t last = 0; 52 for (size_t i = 0; i < frames.size(); i++) { 53 last = i; 54 if (frames[i].duration > 0) break; 55 } 56 return last; 57 } 58 59 ImageBundle& Main() { return frames[LastStillFrame()]; } 60 const ImageBundle& Main() const { return frames[LastStillFrame()]; } 61 62 // If c_current.IsGray(), all planes must be identical. 63 void SetFromImage(Image3F&& color, const ColorEncoding& c_current) { 64 Main().SetFromImage(std::move(color), c_current); 65 SetIntensityTarget(&this->metadata.m); 66 SetSize(Main().xsize(), Main().ysize()); 67 } 68 69 void SetSize(size_t xsize, size_t ysize) { 70 JXL_CHECK(metadata.size.Set(xsize, ysize)); 71 } 72 73 void CheckMetadata() const { 74 JXL_CHECK(metadata.m.bit_depth.bits_per_sample != 0); 75 JXL_CHECK(!metadata.m.color_encoding.ICC().empty()); 76 77 if (preview_frame.xsize() != 0) preview_frame.VerifyMetadata(); 78 JXL_CHECK(preview_frame.metadata() == &metadata.m); 79 80 for (const ImageBundle& ib : frames) { 81 ib.VerifyMetadata(); 82 JXL_CHECK(ib.metadata() == &metadata.m); 83 } 84 } 85 86 size_t xsize() const { return metadata.size.xsize(); } 87 size_t ysize() const { return metadata.size.ysize(); } 88 void ShrinkTo(size_t xsize, size_t ysize) { 89 // preview is unaffected. 90 for (ImageBundle& ib : frames) { 91 ib.ShrinkTo(xsize, ysize); 92 } 93 SetSize(xsize, ysize); 94 } 95 96 // -- DECODER OUTPUT, ENCODER INPUT: 97 98 // Metadata stored into / retrieved from bitstreams. 99 100 Blobs blobs; 101 102 CodecMetadata metadata; // applies to preview and all frames 103 104 // If metadata.have_preview: 105 ImageBundle preview_frame; 106 107 std::vector<ImageBundle> frames; // size=1 if !metadata.have_animation 108 109 // If the image should be written to a JPEG, use this quality for encoding. 110 size_t jpeg_quality; 111 }; 112 113 } // namespace jxl 114 115 #endif // LIB_JXL_CODEC_IN_OUT_H_