test_image.h (2536B)
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_TEST_IMAGE_H_ 7 #define LIB_JXL_TEST_IMAGE_H_ 8 9 #include <jxl/codestream_header.h> 10 #include <jxl/types.h> 11 #include <stddef.h> 12 13 #include <cstdint> 14 #include <string> 15 #include <vector> 16 17 #include "lib/extras/packed_image.h" 18 #include "lib/jxl/base/span.h" 19 20 namespace jxl { 21 namespace test { 22 23 // Returns a test image with some autogenerated pixel content, using 16 bits per 24 // channel, big endian order, 1 to 4 channels 25 // The seed parameter allows to create images with different pixel content. 26 std::vector<uint8_t> GetSomeTestImage(size_t xsize, size_t ysize, 27 size_t num_channels, uint16_t seed); 28 29 class TestImage { 30 public: 31 TestImage(); 32 33 extras::PackedPixelFile& ppf() { return ppf_; } 34 35 TestImage& DecodeFromBytes(const std::vector<uint8_t>& bytes); 36 37 TestImage& ClearMetadata(); 38 39 TestImage& SetDimensions(size_t xsize, size_t ysize); 40 41 TestImage& SetChannels(size_t num_channels); 42 43 // Sets the same bit depth on color, alpha and all extra channels. 44 TestImage& SetAllBitDepths(uint32_t bits_per_sample, 45 uint32_t exponent_bits_per_sample = 0); 46 47 TestImage& SetDataType(JxlDataType data_type); 48 49 TestImage& SetEndianness(JxlEndianness endianness); 50 51 TestImage& SetRowAlignment(size_t align); 52 53 TestImage& SetColorEncoding(const std::string& description); 54 55 TestImage& CoalesceGIFAnimationWithAlpha(); 56 57 class Frame { 58 public: 59 Frame(TestImage* parent, bool is_preview, size_t index); 60 61 void ZeroFill(); 62 void RandomFill(uint16_t seed = 177); 63 64 void SetValue(size_t y, size_t x, size_t c, float val); 65 66 private: 67 extras::PackedPixelFile& ppf() const { return parent_->ppf(); } 68 69 extras::PackedFrame& frame() { 70 return is_preview_ ? *ppf().preview_frame : ppf().frames[index_]; 71 } 72 73 TestImage* parent_; 74 bool is_preview_; 75 size_t index_; 76 }; 77 78 Frame AddFrame(); 79 80 Frame AddPreview(size_t xsize, size_t ysize); 81 82 private: 83 extras::PackedPixelFile ppf_; 84 JxlPixelFormat format_ = {3, JXL_TYPE_UINT8, JXL_LITTLE_ENDIAN, 0}; 85 86 static void CropLayerInfo(size_t xsize, size_t ysize, JxlLayerInfo* info); 87 88 static void CropImage(size_t xsize, size_t ysize, extras::PackedImage* image); 89 90 static JxlDataType DefaultDataType(const JxlBasicInfo& info); 91 }; 92 93 } // namespace test 94 } // namespace jxl 95 96 #endif // LIB_JXL_TEST_IMAGE_H_