modular_image.h (3463B)
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_MODULAR_MODULAR_IMAGE_H_ 7 #define LIB_JXL_MODULAR_MODULAR_IMAGE_H_ 8 9 #include <stddef.h> 10 #include <stdint.h> 11 #include <string.h> 12 13 #include <string> 14 #include <utility> 15 #include <vector> 16 17 #include "lib/jxl/base/compiler_specific.h" 18 #include "lib/jxl/base/data_parallel.h" 19 #include "lib/jxl/base/status.h" 20 #include "lib/jxl/image.h" 21 22 namespace jxl { 23 24 typedef int32_t pixel_type; // can use int16_t if it's only for 8-bit images. 25 // Need some wiggle room for YCoCg / Squeeze etc 26 27 typedef int64_t pixel_type_w; 28 29 namespace weighted { 30 struct Header; 31 } 32 33 class Channel { 34 public: 35 jxl::Plane<pixel_type> plane; 36 size_t w, h; 37 int hshift, vshift; // w ~= image.w >> hshift; h ~= image.h >> vshift 38 Channel(const Channel& other) = delete; 39 Channel& operator=(const Channel& other) = delete; 40 41 static StatusOr<Channel> Create(size_t iw, size_t ih, int hsh = 0, 42 int vsh = 0) { 43 JXL_ASSIGN_OR_RETURN(Plane<pixel_type> plane, 44 Plane<pixel_type>::Create(iw, ih)); 45 return Channel(std::move(plane), iw, ih, hsh, vsh); 46 } 47 48 // Move assignment 49 Channel& operator=(Channel&& other) noexcept { 50 w = other.w; 51 h = other.h; 52 hshift = other.hshift; 53 vshift = other.vshift; 54 plane = std::move(other.plane); 55 return *this; 56 } 57 58 // Move constructor 59 Channel(Channel&& other) noexcept = default; 60 61 Status shrink() { 62 if (plane.xsize() == w && plane.ysize() == h) return true; 63 JXL_ASSIGN_OR_RETURN(plane, Plane<pixel_type>::Create(w, h)); 64 return true; 65 } 66 Status shrink(int nw, int nh) { 67 w = nw; 68 h = nh; 69 return shrink(); 70 } 71 72 JXL_INLINE pixel_type* Row(const size_t y) { return plane.Row(y); } 73 JXL_INLINE const pixel_type* Row(const size_t y) const { 74 return plane.Row(y); 75 } 76 77 private: 78 Channel(jxl::Plane<pixel_type>&& p, size_t iw, size_t ih, int hsh, int vsh) 79 : plane(std::move(p)), w(iw), h(ih), hshift(hsh), vshift(vsh) {} 80 }; 81 82 class Transform; 83 84 class Image { 85 public: 86 // image data, transforms can dramatically change the number of channels and 87 // their semantics 88 std::vector<Channel> channel; 89 // transforms that have been applied (and that have to be undone) 90 std::vector<Transform> transform; 91 92 // image dimensions (channels may have different dimensions due to transforms) 93 size_t w, h; 94 int bitdepth; 95 size_t nb_meta_channels; // first few channels might contain palette(s) 96 bool error; // true if a fatal error occurred, false otherwise 97 98 Image(); 99 100 Image(const Image& other) = delete; 101 Image& operator=(const Image& other) = delete; 102 103 Image& operator=(Image&& other) noexcept; 104 Image(Image&& other) noexcept = default; 105 106 static StatusOr<Image> Create(size_t iw, size_t ih, int bitdepth, 107 int nb_chans); 108 109 bool empty() const { 110 for (const auto& ch : channel) { 111 if (ch.w && ch.h) return false; 112 } 113 return true; 114 } 115 116 static StatusOr<Image> Clone(const Image& that); 117 118 void undo_transforms(const weighted::Header& wp_header, 119 jxl::ThreadPool* pool = nullptr); 120 121 std::string DebugString() const; 122 123 private: 124 Image(size_t iw, size_t ih, int bitdepth); 125 }; 126 127 } // namespace jxl 128 129 #endif // LIB_JXL_MODULAR_MODULAR_IMAGE_H_