test_utils.h (4586B)
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_JPEGLI_TEST_UTILS_H_ 7 #define LIB_JPEGLI_TEST_UTILS_H_ 8 9 #include <stddef.h> 10 #include <stdint.h> 11 12 #include <algorithm> 13 #include <string> 14 #include <vector> 15 16 /* clang-format off */ 17 #include <stdio.h> 18 #include <jpeglib.h> 19 #include <setjmp.h> 20 /* clang-format on */ 21 22 #include "lib/jpegli/common.h" 23 #include "lib/jpegli/libjpeg_test_util.h" 24 #include "lib/jpegli/test_params.h" 25 26 namespace jpegli { 27 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); \ 41 }; 42 43 std::string IOMethodName(JpegliDataType data_type, JpegliEndianness endianness); 44 45 std::string ColorSpaceName(J_COLOR_SPACE colorspace); 46 47 std::ostream& operator<<(std::ostream& os, const TestImage& input); 48 49 std::ostream& operator<<(std::ostream& os, const CompressParams& jparams); 50 51 int NumTestScanScripts(); 52 53 void VerifyHeader(const CompressParams& jparams, j_decompress_ptr cinfo); 54 void VerifyScanHeader(const CompressParams& jparams, j_decompress_ptr cinfo); 55 56 void SetDecompressParams(const DecompressParams& dparams, 57 j_decompress_ptr cinfo); 58 59 void SetScanDecompressParams(const DecompressParams& dparams, 60 j_decompress_ptr cinfo, int scan_number); 61 62 void CopyCoefficients(j_decompress_ptr cinfo, jvirt_barray_ptr* coef_arrays, 63 TestImage* output); 64 65 void UnmapColors(uint8_t* row, size_t xsize, int components, 66 JSAMPARRAY colormap, size_t num_colors); 67 68 std::string GetTestDataPath(const std::string& filename); 69 std::vector<uint8_t> ReadTestData(const std::string& filename); 70 71 class PNMParser { 72 public: 73 explicit PNMParser(const uint8_t* data, const size_t len) 74 : pos_(data), end_(data + len) {} 75 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); 79 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 == ' '; 84 } 85 86 bool ParseUnsigned(size_t* number); 87 88 bool SkipWhitespace(); 89 90 const uint8_t* pos_; 91 const uint8_t* const end_; 92 }; 93 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); 97 98 void SetNumChannels(J_COLOR_SPACE colorspace, size_t* channels); 99 100 void ConvertToGrayscale(TestImage* img); 101 102 void GeneratePixels(TestImage* img); 103 104 void GenerateRawData(const CompressParams& jparams, TestImage* img); 105 106 void GenerateCoeffs(const CompressParams& jparams, TestImage* img); 107 108 void EncodeWithJpegli(const TestImage& input, const CompressParams& jparams, 109 j_compress_ptr cinfo); 110 111 bool EncodeWithJpegli(const TestImage& input, const CompressParams& jparams, 112 std::vector<uint8_t>* compressed); 113 114 double DistanceRms(const TestImage& input, const TestImage& output, 115 size_t start_line, size_t num_lines, 116 double* max_diff = nullptr); 117 118 double DistanceRms(const TestImage& input, const TestImage& output, 119 double* max_diff = nullptr); 120 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); 124 125 void VerifyOutputImage(const TestImage& input, const TestImage& output, 126 double max_rms, double max_diff = 255.0); 127 128 } // namespace jpegli 129 130 #endif // LIB_JPEGLI_TEST_UTILS_H_