test_utils.h (4521B)
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 <cstddef> 10 #include <cstdint> 11 #include <string> 12 #include <vector> 13 14 #include "lib/jpegli/test_params.h" 15 #include "lib/jpegli/types.h" 16 #include "lib/jxl/base/include_jpeglib.h" // NOLINT 17 #include "lib/jxl/base/status.h" 18 19 namespace jpegli { 20 21 #define ERROR_HANDLER_SETUP(flavor) \ 22 jpeg_error_mgr jerr; \ 23 jmp_buf env; \ 24 cinfo.err = flavor##_std_error(&jerr); \ 25 if (setjmp(env)) { \ 26 return false; \ 27 } \ 28 cinfo.client_data = reinterpret_cast<void*>(&env); \ 29 cinfo.err->error_exit = [](j_common_ptr cinfo) { \ 30 (*cinfo->err->output_message)(cinfo); \ 31 jmp_buf* env = reinterpret_cast<jmp_buf*>(cinfo->client_data); \ 32 flavor##_destroy(cinfo); \ 33 longjmp(*env, 1); \ 34 }; 35 36 std::string IOMethodName(JpegliDataType data_type, JpegliEndianness endianness); 37 38 std::string ColorSpaceName(J_COLOR_SPACE colorspace); 39 40 std::ostream& operator<<(std::ostream& os, const TestImage& input); 41 42 std::ostream& operator<<(std::ostream& os, const CompressParams& jparams); 43 44 int NumTestScanScripts(); 45 46 void VerifyHeader(const CompressParams& jparams, j_decompress_ptr cinfo); 47 void VerifyScanHeader(const CompressParams& jparams, j_decompress_ptr cinfo); 48 49 void SetDecompressParams(const DecompressParams& dparams, 50 j_decompress_ptr cinfo); 51 52 void SetScanDecompressParams(const DecompressParams& dparams, 53 j_decompress_ptr cinfo, int scan_number); 54 55 void CopyCoefficients(j_decompress_ptr cinfo, jvirt_barray_ptr* coef_arrays, 56 TestImage* output); 57 58 void UnmapColors(uint8_t* row, size_t xsize, int components, 59 JSAMPARRAY colormap, size_t num_colors); 60 61 std::string GetTestDataPath(const std::string& filename); 62 jxl::StatusOr<std::vector<uint8_t>> ReadTestData(const std::string& filename); 63 64 class PNMParser { 65 public: 66 explicit PNMParser(const uint8_t* data, const size_t len) 67 : pos_(data), end_(data + len) {} 68 69 // Sets "pos" to the first non-header byte/pixel on success. 70 bool ParseHeader(const uint8_t** pos, size_t* xsize, size_t* ysize, 71 size_t* num_channels, size_t* bitdepth); 72 73 private: 74 static bool IsLineBreak(const uint8_t c) { return c == '\r' || c == '\n'; } 75 static bool IsWhitespace(const uint8_t c) { 76 return IsLineBreak(c) || c == '\t' || c == ' '; 77 } 78 79 bool ParseUnsigned(size_t* number); 80 81 bool SkipWhitespace(); 82 83 const uint8_t* pos_; 84 const uint8_t* const end_; 85 }; 86 87 bool ReadPNM(const std::vector<uint8_t>& data, size_t* xsize, size_t* ysize, 88 size_t* num_channels, size_t* bitdepth, 89 std::vector<uint8_t>* pixels); 90 91 jxl::Status SetNumChannels(J_COLOR_SPACE colorspace, size_t* channels); 92 93 void ConvertToGrayscale(TestImage* img); 94 95 void GeneratePixels(TestImage* img); 96 97 void GenerateRawData(const CompressParams& jparams, TestImage* img); 98 99 void GenerateCoeffs(const CompressParams& jparams, TestImage* img); 100 101 void EncodeWithJpegli(const TestImage& input, const CompressParams& jparams, 102 j_compress_ptr cinfo); 103 104 bool EncodeWithJpegli(const TestImage& input, const CompressParams& jparams, 105 std::vector<uint8_t>* compressed); 106 107 double DistanceRms(const TestImage& input, const TestImage& output, 108 size_t start_line, size_t num_lines, 109 double* max_diff = nullptr); 110 111 double DistanceRms(const TestImage& input, const TestImage& output, 112 double* max_diff = nullptr); 113 114 void VerifyOutputImage(const TestImage& input, const TestImage& output, 115 size_t start_line, size_t num_lines, double max_rms, 116 double max_diff = 255.0); 117 118 void VerifyOutputImage(const TestImage& input, const TestImage& output, 119 double max_rms, double max_diff = 255.0); 120 121 } // namespace jpegli 122 123 #endif // LIB_JPEGLI_TEST_UTILS_H_