Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_IMAGE_TEST_UTILS_H_
20 #define PAGESPEED_KERNEL_IMAGE_TEST_UTILS_H_
21 
22 #include <cstddef>
23 
28 
29 namespace net_instaweb {
30 class MessageHandler;
31 }
32 
33 namespace pagespeed {
34 
35 namespace image_compression {
36 
37 class ScanlineReaderInterface;
38 
40 
41  const char kTestRootDir[] = "/pagespeed/kernel/image/testdata/";
42 
44 const char kGifTestDir[] = "gif/";
45 const char kJpegTestDir[] = "jpeg/";
46 const char kPngSuiteGifTestDir[] = "pngsuite/gif/";
47 const char kPngSuiteTestDir[] = "pngsuite/";
48 const char kPngTestDir[] = "png/";
49 const char kWebpTestDir[] = "webp/";
50 const char kResizedTestDir[] = "resized/";
51 
53 const char kMessagePatternAnimatedGif[] =
54  "*Unable to optimize image with * frames.";
55 const char kMessagePatternFailedToOpen[] = "*Failed to open*";
56 const char kMessagePatternFailedToRead[] = "*Failed to read*";
57 const char kMessagePatternLibJpegFailure[] = "*libjpeg failed to*";
58 const char kMessagePatternLibpngError[] = "*libpng error:*";
59 const char kMessagePatternLibpngFailure[] = "*libpng failed to*";
60 const char kMessagePatternLibpngWarning[] = "*libpng warning:*";
61 const char kMessagePatternPixelFormat[] = "*Pixel format:*";
62 const char kMessagePatternStats[] = "*Stats:*";
63 const char kMessagePatternUnexpectedEOF[] = "*Unexpected EOF*";
64 const char kMessagePatternWritingToWebp[] = "*Writing to webp:*";
65 
67  public:
68  ImageCompressionInfo(const char* a_filename,
69  size_t a_original_size,
70  size_t a_compressed_size_best,
71  size_t a_compressed_size_default,
72  int a_width,
73  int a_height,
74  int a_original_bit_depth,
75  int a_original_color_type,
76  int a_compressed_bit_depth,
77  int a_compressed_color_type) :
78  filename(a_filename),
79  original_size(a_original_size),
80  compressed_size_best(a_compressed_size_best),
81  compressed_size_default(a_compressed_size_default),
82  width(a_width),
83  height(a_height),
84  original_bit_depth(a_original_bit_depth),
85  original_color_type(a_original_color_type),
86  compressed_bit_depth(a_compressed_bit_depth),
87  compressed_color_type(a_compressed_color_type) {
88  }
89 
90  public:
91  const char* filename;
92  size_t original_size;
93  size_t compressed_size_best;
94  size_t compressed_size_default;
95  int width;
96  int height;
97  int original_bit_depth;
98  int original_color_type;
99  int compressed_bit_depth;
100  int compressed_color_type;
101 };
102 
104  public:
105  GoldImageCompressionInfo(const char* a_filename,
106  size_t a_original_size,
107  size_t a_compressed_size_best,
108  size_t a_compressed_size_default,
109  int a_width,
110  int a_height,
111  int a_original_bit_depth,
112  int a_original_color_type,
113  int a_compressed_bit_depth,
114  int a_compressed_color_type,
115  bool a_transparency) :
116  ImageCompressionInfo(a_filename,
117  a_original_size,
118  a_compressed_size_best,
119  a_compressed_size_default,
120  a_width,
121  a_height,
122  a_original_bit_depth,
123  a_original_color_type,
124  a_compressed_bit_depth,
125  a_compressed_color_type),
126  transparency(a_transparency) {
127  }
128 
129  public:
130  bool transparency;
131 };
132 
133 const GoldImageCompressionInfo kValidGifImages[] = {
134  GoldImageCompressionInfo("basi0g01", 153, 166, 166, 32, 32, 8, 3, 1, 3,
135  false),
136  GoldImageCompressionInfo("basi0g02", 185, 112, 112, 32, 32, 8, 3, 2, 3,
137  false),
138  GoldImageCompressionInfo("basi0g04", 344, 144, 186, 32, 32, 8, 3, 4, 3,
139  false),
140  GoldImageCompressionInfo("basi0g08", 1736, 116, 714, 32, 32, 8, 3, 8, 0,
141  false),
142  GoldImageCompressionInfo("basi3p01", 138, 96, 96, 32, 32, 8, 3, 1, 3,
143  false),
144  GoldImageCompressionInfo("basi3p02", 186, 115, 115, 32, 32, 8, 3, 2, 3,
145  false),
146  GoldImageCompressionInfo("basi3p04", 344, 185, 185, 32, 32, 8, 3, 4, 3,
147  false),
148  GoldImageCompressionInfo("basi3p08", 1737, 1270, 1270, 32, 32, 8, 3, 8, 3,
149  false),
150  GoldImageCompressionInfo("basn0g01", 153, 166, 166, 32, 32, 8, 3, 1, 3,
151  false),
152  GoldImageCompressionInfo("basn0g02", 185, 112, 112, 32, 32, 8, 3, 2, 3,
153  false),
154  GoldImageCompressionInfo("basn0g04", 344, 144, 186, 32, 32, 8, 3, 4, 3,
155  false),
156  GoldImageCompressionInfo("basn0g08", 1736, 116, 714, 32, 32, 8, 3, 8, 0,
157  false),
158  GoldImageCompressionInfo("basn3p01", 138, 96, 96, 32, 32, 8, 3, 1, 3,
159  false),
160  GoldImageCompressionInfo("basn3p02", 186, 115, 115, 32, 32, 8, 3, 2, 3,
161  false),
162  GoldImageCompressionInfo("basn3p04", 344, 185, 185, 32, 32, 8, 3, 4, 3,
163  false),
164  GoldImageCompressionInfo("basn3p08", 1737, 1270, 1270, 32, 32, 8, 3, 8, 3,
165  false),
166 
169  GoldImageCompressionInfo("tr-basi4a08", 467, 239, 316, 32, 32, 8, 3, 8, 3,
170  true),
171  GoldImageCompressionInfo("tr-basn4a08", 467, 239, 316, 32, 32, 8, 3, 8, 3,
172  true),
173 };
174 const size_t kValidGifImageCount = arraysize(kValidGifImages);
175 
176 bool ReadFile(const GoogleString& file_name,
177  GoogleString* content);
178 
179 bool ReadTestFile(const GoogleString& path,
180  const char* name,
181  const char* extension,
182  GoogleString* content);
183 
184 bool ReadTestFileWithExt(const GoogleString& path,
185  const char* name_with_extension,
186  GoogleString* content);
187 
188 void DecodeAndCompareImages(
189  pagespeed::image_compression::ImageFormat image_format1,
190  const void* image_buffer1,
191  size_t buffer_length1,
192  pagespeed::image_compression::ImageFormat image_format2,
193  const void* image_buffer2,
194  size_t buffer_length2,
195  bool ignore_transparent_rgb,
196  MessageHandler* message_handler);
197 
198 void DecodeAndCompareImagesByPSNR(
199  pagespeed::image_compression::ImageFormat image_format1,
200  const void* image_buffer1,
201  size_t buffer_length1,
202  pagespeed::image_compression::ImageFormat image_format2,
203  const void* image_buffer2,
204  size_t buffer_length2,
205  double min_psnr,
206  bool ignore_transparent_rgb,
207  bool expand_colors,
208  MessageHandler* message_handler);
209 
211 void CompareImageReaders(ScanlineReaderInterface* reader1,
212  ScanlineReaderInterface* reader2);
213 
221 void CompareImageRegions(const uint8_t* image1, PixelFormat format1,
222  int bytes_per_row1, int col1, int row1,
223  const uint8_t* image2, PixelFormat format2,
224  int bytes_per_row2, int col2, int row2,
225  int num_cols, int num_rows, MessageHandler* handler);
226 
230 void ComparePixelsByPSNR(const uint8_t* image1, const uint8_t* image2,
231  PixelFormat format, int num_rows, int num_cols,
232  double min_psnr, bool ignore_transparent_rgb,
233  MessageHandler* handler);
234 
242 void CompareImageRegionsByPSNR(const uint8_t* image1, PixelFormat format1,
243  int bytes_per_row1, int col1, int row1,
244  const uint8_t* image2, PixelFormat format2,
245  int bytes_per_row2, int col2, int row2,
246  int num_cols, int num_rows, double min_psnr,
247  bool ignore_transparent_rgb,
248  bool expand_colors, MessageHandler* handler);
249 
258 void SynthesizeImage(int width, int height, int bytes_per_line,
259  int num_channels, const uint8_t* seed_value,
260  const int* delta_x, const int* delta_y, uint8_t* image);
261 
262 
265 inline GoogleString PixelRgbaChannelsToString(const uint8_t* const channels) {
266  return StringPrintf("%02hhx%02hhx%02hhx%02hhx",
267  channels[RGBA_RED],
268  channels[RGBA_GREEN],
269  channels[RGBA_BLUE],
270  channels[RGBA_ALPHA]);
271 }
272 
274 inline uint32_t PackAsRgba(uint8_t alpha,
275  uint8_t red,
276  uint8_t green,
277  uint8_t blue) {
278  return PackHiToLo(red, green, blue, alpha);
279 }
280 
283 inline uint32_t RgbaToPackedRgba(const PixelRgbaChannels rgba) {
284  return PackAsRgba(rgba[RGBA_ALPHA],
285  rgba[RGBA_RED],
286  rgba[RGBA_GREEN],
287  rgba[RGBA_BLUE]);
288 }
289 
291 bool CompareAnimatedImages(const GoogleString& expected_image_filename,
292  const GoogleString& actual_image_content,
293  MessageHandler* message_handler);
294 
295 }
296 
297 }
298 
299 #endif
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: message_handler.h:39