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