OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <math.h> | |
6 | |
7 #include "base/gfx/jpeg_codec.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 // out of 100, this indicates how compressed it will be, this should be changed | |
11 // with jpeg equality threshold | |
12 // static int jpeg_quality = 75; // FIXME(brettw) | |
13 static int jpeg_quality = 100; | |
14 | |
15 // The threshold of average color differences where we consider two images | |
16 // equal. This number was picked to be a little above the observed difference | |
17 // using the above quality. | |
18 static double jpeg_equality_threshold = 1.0; | |
19 | |
20 // Computes the average difference between each value in a and b. A and b | |
21 // should be the same size. Used to see if two images are approximately equal | |
22 // in the presence of compression. | |
23 static double AveragePixelDelta(const std::vector<unsigned char>& a, | |
24 const std::vector<unsigned char>& b) { | |
25 // if the sizes are different, say the average difference is the maximum | |
26 if (a.size() != b.size()) | |
27 return 255.0; | |
28 if (a.size() == 0) | |
29 return 0; // prevent divide by 0 below | |
30 | |
31 double acc = 0.0; | |
32 for (size_t i = 0; i < a.size(); i++) | |
33 acc += fabs(static_cast<double>(a[i]) - static_cast<double>(b[i])); | |
34 | |
35 return acc / static_cast<double>(a.size()); | |
36 } | |
37 | |
38 static void MakeRGBImage(int w, int h, std::vector<unsigned char>* dat) { | |
39 dat->resize(w * h * 3); | |
40 for (int y = 0; y < h; y++) { | |
41 for (int x = 0; x < w; x++) { | |
42 unsigned char* org_px = &(*dat)[(y * w + x) * 3]; | |
43 org_px[0] = x * 3; // r | |
44 org_px[1] = x * 3 + 1; // g | |
45 org_px[2] = x * 3 + 2; // b | |
46 } | |
47 } | |
48 } | |
49 | |
50 TEST(JPEGCodec, EncodeDecodeRGB) { | |
51 int w = 20, h = 20; | |
52 | |
53 // create an image with known values | |
54 std::vector<unsigned char> original; | |
55 MakeRGBImage(w, h, &original); | |
56 | |
57 // encode, making sure it was compressed some | |
58 std::vector<unsigned char> encoded; | |
59 EXPECT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGB, w, h, | |
60 w * 3, jpeg_quality, &encoded)); | |
61 EXPECT_GT(original.size(), encoded.size()); | |
62 | |
63 // decode, it should have the same size as the original | |
64 std::vector<unsigned char> decoded; | |
65 int outw, outh; | |
66 EXPECT_TRUE(JPEGCodec::Decode(&encoded[0], encoded.size(), | |
67 JPEGCodec::FORMAT_RGB, &decoded, | |
68 &outw, &outh)); | |
69 ASSERT_EQ(w, outw); | |
70 ASSERT_EQ(h, outh); | |
71 ASSERT_EQ(original.size(), decoded.size()); | |
72 | |
73 // Images must be approximately equal (compression will have introduced some | |
74 // minor artifacts). | |
75 ASSERT_GE(jpeg_equality_threshold, AveragePixelDelta(original, decoded)); | |
76 } | |
77 | |
78 TEST(JPEGCodec, EncodeDecodeRGBA) { | |
79 int w = 20, h = 20; | |
80 | |
81 // create an image with known values, a must be opaque because it will be | |
82 // lost during compression | |
83 std::vector<unsigned char> original; | |
84 original.resize(w * h * 4); | |
85 for (int y = 0; y < h; y++) { | |
86 for (int x = 0; x < w; x++) { | |
87 unsigned char* org_px = &original[(y * w + x) * 4]; | |
88 org_px[0] = x * 3; // r | |
89 org_px[1] = x * 3 + 1; // g | |
90 org_px[2] = x * 3 + 2; // b | |
91 org_px[3] = 0xFF; // a (opaque) | |
92 } | |
93 } | |
94 | |
95 // encode, making sure it was compressed some | |
96 std::vector<unsigned char> encoded; | |
97 EXPECT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGBA, w, h, | |
98 w * 4, jpeg_quality, &encoded)); | |
99 EXPECT_GT(original.size(), encoded.size()); | |
100 | |
101 // decode, it should have the same size as the original | |
102 std::vector<unsigned char> decoded; | |
103 int outw, outh; | |
104 EXPECT_TRUE(JPEGCodec::Decode(&encoded[0], encoded.size(), | |
105 JPEGCodec::FORMAT_RGBA, &decoded, | |
106 &outw, &outh)); | |
107 ASSERT_EQ(w, outw); | |
108 ASSERT_EQ(h, outh); | |
109 ASSERT_EQ(original.size(), decoded.size()); | |
110 | |
111 // Images must be approximately equal (compression will have introduced some | |
112 // minor artifacts). | |
113 ASSERT_GE(jpeg_equality_threshold, AveragePixelDelta(original, decoded)); | |
114 } | |
115 | |
116 // Test that corrupted data decompression causes failures. | |
117 TEST(JPEGCodec, DecodeCorrupted) { | |
118 int w = 20, h = 20; | |
119 | |
120 // some random data (an uncompressed image) | |
121 std::vector<unsigned char> original; | |
122 MakeRGBImage(w, h, &original); | |
123 | |
124 // it should fail when given non-JPEG compressed data | |
125 std::vector<unsigned char> output; | |
126 int outw, outh; | |
127 ASSERT_FALSE(JPEGCodec::Decode(&original[0], original.size(), | |
128 JPEGCodec::FORMAT_RGB, &output, | |
129 &outw, &outh)); | |
130 | |
131 // make some compressed data | |
132 std::vector<unsigned char> compressed; | |
133 ASSERT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGB, w, h, | |
134 w * 3, jpeg_quality, &compressed)); | |
135 | |
136 // try decompressing a truncated version | |
137 ASSERT_FALSE(JPEGCodec::Decode(&compressed[0], compressed.size() / 2, | |
138 JPEGCodec::FORMAT_RGB, &output, | |
139 &outw, &outh)); | |
140 | |
141 // corrupt it and try decompressing that | |
142 for (int i = 10; i < 30; i++) | |
143 compressed[i] = i; | |
144 ASSERT_FALSE(JPEGCodec::Decode(&compressed[0], compressed.size(), | |
145 JPEGCodec::FORMAT_RGB, &output, | |
146 &outw, &outh)); | |
147 } | |
OLD | NEW |