Backed out 3 changesets (bug 1790375) for causing wd failures on fetch_error.py....
[gecko.git] / third_party / jpeg-xl / lib / jpegli / test_utils.h
blob132cfd042ad039b8cde41bcf824278cf66452195
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.
6 #ifndef LIB_JPEGLI_TEST_UTILS_H_
7 #define LIB_JPEGLI_TEST_UTILS_H_
9 #include <stddef.h>
10 #include <stdint.h>
12 #include <algorithm>
13 #include <string>
14 #include <vector>
16 /* clang-format off */
17 #include <stdio.h>
18 #include <jpeglib.h>
19 #include <setjmp.h>
20 /* clang-format on */
22 #include "lib/jpegli/common.h"
23 #include "lib/jpegli/libjpeg_test_util.h"
24 #include "lib/jpegli/test_params.h"
26 namespace jpegli {
28 #define ERROR_HANDLER_SETUP(flavor) \
29 jpeg_error_mgr jerr; \
30 jmp_buf env; \
31 cinfo.err = flavor##_std_error(&jerr); \
32 if (setjmp(env)) { \
33 return false; \
34 } \
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); \
40 longjmp(*env, 1); \
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,
63 TestImage* output);
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);
71 class PNMParser {
72 public:
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);
80 private:
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();
90 const uint8_t* pos_;
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_