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