1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
6 #ifndef LIB_JPEGLI_TEST_UTILS_H_
7 #define LIB_JPEGLI_TEST_UTILS_H_
16 /* clang-format off */
22 #include "lib/jpegli/common.h"
23 #include "lib/jpegli/libjpeg_test_util.h"
24 #include "lib/jpegli/test_params.h"
28 #define ERROR_HANDLER_SETUP(flavor) \
29 jpeg_error_mgr jerr; \
31 cinfo.err = flavor##_std_error(&jerr); \
35 cinfo.client_data = reinterpret_cast<void*>(&env); \
36 cinfo.err->error_exit = [](j_common_ptr cinfo) { \
37 (*cinfo->err->output_message)(cinfo); \
38 jmp_buf* env = reinterpret_cast<jmp_buf*>(cinfo->client_data); \
39 flavor##_destroy(cinfo); \
43 std::string
IOMethodName(JpegliDataType data_type
, JpegliEndianness endianness
);
45 std::string
ColorSpaceName(J_COLOR_SPACE colorspace
);
47 std::ostream
& operator<<(std::ostream
& os
, const TestImage
& input
);
49 std::ostream
& operator<<(std::ostream
& os
, const CompressParams
& jparams
);
51 int NumTestScanScripts();
53 void VerifyHeader(const CompressParams
& jparams
, j_decompress_ptr cinfo
);
54 void VerifyScanHeader(const CompressParams
& jparams
, j_decompress_ptr cinfo
);
56 void SetDecompressParams(const DecompressParams
& dparams
,
57 j_decompress_ptr cinfo
);
59 void SetScanDecompressParams(const DecompressParams
& dparams
,
60 j_decompress_ptr cinfo
, int scan_number
);
62 void CopyCoefficients(j_decompress_ptr cinfo
, jvirt_barray_ptr
* coef_arrays
,
65 void UnmapColors(uint8_t* row
, size_t xsize
, int components
,
66 JSAMPARRAY colormap
, size_t num_colors
);
68 std::string
GetTestDataPath(const std::string
& filename
);
69 std::vector
<uint8_t> ReadTestData(const std::string
& filename
);
73 explicit PNMParser(const uint8_t* data
, const size_t len
)
74 : pos_(data
), end_(data
+ len
) {}
76 // Sets "pos" to the first non-header byte/pixel on success.
77 bool ParseHeader(const uint8_t** pos
, size_t* xsize
, size_t* ysize
,
78 size_t* num_channels
, size_t* bitdepth
);
81 static bool IsLineBreak(const uint8_t c
) { return c
== '\r' || c
== '\n'; }
82 static bool IsWhitespace(const uint8_t c
) {
83 return IsLineBreak(c
) || c
== '\t' || c
== ' ';
86 bool ParseUnsigned(size_t* number
);
88 bool SkipWhitespace();
91 const uint8_t* const end_
;
94 bool ReadPNM(const std::vector
<uint8_t>& data
, size_t* xsize
, size_t* ysize
,
95 size_t* num_channels
, size_t* bitdepth
,
96 std::vector
<uint8_t>* pixels
);
98 void SetNumChannels(J_COLOR_SPACE colorspace
, size_t* channels
);
100 void ConvertToGrayscale(TestImage
* img
);
102 void GeneratePixels(TestImage
* img
);
104 void GenerateRawData(const CompressParams
& jparams
, TestImage
* img
);
106 void GenerateCoeffs(const CompressParams
& jparams
, TestImage
* img
);
108 void EncodeWithJpegli(const TestImage
& input
, const CompressParams
& jparams
,
109 j_compress_ptr cinfo
);
111 bool EncodeWithJpegli(const TestImage
& input
, const CompressParams
& jparams
,
112 std::vector
<uint8_t>* compressed
);
114 double DistanceRms(const TestImage
& input
, const TestImage
& output
,
115 size_t start_line
, size_t num_lines
,
116 double* max_diff
= nullptr);
118 double DistanceRms(const TestImage
& input
, const TestImage
& output
,
119 double* max_diff
= nullptr);
121 void VerifyOutputImage(const TestImage
& input
, const TestImage
& output
,
122 size_t start_line
, size_t num_lines
, double max_rms
,
123 double max_diff
= 255.0);
125 void VerifyOutputImage(const TestImage
& input
, const TestImage
& output
,
126 double max_rms
, double max_diff
= 255.0);
128 } // namespace jpegli
130 #endif // LIB_JPEGLI_TEST_UTILS_H_