summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/test_image.h
blob: ae620470522ffb451d1a5a3d98e2fc610eef0907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#ifndef LIB_JXL_TEST_IMAGE_H_
#define LIB_JXL_TEST_IMAGE_H_

#include <jxl/codestream_header.h>
#include <jxl/types.h>

#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>

#include "lib/extras/packed_image.h"

namespace jxl {
namespace test {

// Returns a test image with some autogenerated pixel content, using 16 bits per
// channel, big endian order, 1 to 4 channels
// The seed parameter allows to create images with different pixel content.
std::vector<uint8_t> GetSomeTestImage(size_t xsize, size_t ysize,
                                      size_t num_channels, uint16_t seed);

class TestImage {
 public:
  TestImage();

  extras::PackedPixelFile& ppf() { return ppf_; }

  TestImage& DecodeFromBytes(const std::vector<uint8_t>& bytes);

  TestImage& ClearMetadata();

  TestImage& SetDimensions(size_t xsize, size_t ysize);

  TestImage& SetChannels(size_t num_channels);

  // Sets the same bit depth on color, alpha and all extra channels.
  TestImage& SetAllBitDepths(uint32_t bits_per_sample,
                             uint32_t exponent_bits_per_sample = 0);

  TestImage& SetDataType(JxlDataType data_type);

  TestImage& SetEndianness(JxlEndianness endianness);

  TestImage& SetRowAlignment(size_t align);

  TestImage& SetColorEncoding(const std::string& description);

  TestImage& CoalesceGIFAnimationWithAlpha();

  class Frame {
   public:
    Frame(TestImage* parent, bool is_preview, size_t index);

    void ZeroFill();
    void RandomFill(uint16_t seed = 177);

    void SetValue(size_t y, size_t x, size_t c, float val);

   private:
    extras::PackedPixelFile& ppf() const { return parent_->ppf(); }

    extras::PackedFrame& frame() {
      return is_preview_ ? *ppf().preview_frame : ppf().frames[index_];
    }

    TestImage* parent_;
    bool is_preview_;
    size_t index_;
  };

  Frame AddFrame();

  Frame AddPreview(size_t xsize, size_t ysize);

 private:
  extras::PackedPixelFile ppf_;
  JxlPixelFormat format_ = {3, JXL_TYPE_UINT8, JXL_LITTLE_ENDIAN, 0};

  static void CropLayerInfo(size_t xsize, size_t ysize, JxlLayerInfo* info);

  static void CropImage(size_t xsize, size_t ysize, extras::PackedImage* image);

  static JxlDataType DefaultDataType(const JxlBasicInfo& info);
};

}  // namespace test
}  // namespace jxl

#endif  // LIB_JXL_TEST_IMAGE_H_