1// Copyright (c) 2011 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/basictypes.h"
8#include "testing/gtest/include/gtest/gtest.h"
9#include "ui/gfx/codec/jpeg_codec.h"
10
11namespace {
12
13// A JPEG image used by TopSitesMigrationTest, whose size is 1x1.
14// This image causes an invalid-read error to libjpeg-turbo 1.0.1.
15const uint8 kTopSitesMigrationTestImage[] =
16    "\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01\x01\x00\x00\x01"
17    "\x00\x01\x00\x00\xff\xdb\x00\x43\x00\x03\x02\x02\x03\x02\x02\x03"
18    "\x03\x03\x03\x04\x03\x03\x04\x05\x08\x05\x05\x04\x04\x05\x0a\x07"
19    "\x07\x06\x08\x0c\x0a\x0c\x0c\x0b\x0a\x0b\x0b\x0d\x0e\x12\x10\x0d"
20    "\x0e\x11\x0e\x0b\x0b\x10\x16\x10\x11\x13\x14\x15\x15\x15\x0c\x0f"
21    "\x17\x18\x16\x14\x18\x12\x14\x15\x14\xff\xdb\x00\x43\x01\x03\x04"
22    "\x04\x05\x04\x05\x09\x05\x05\x09\x14\x0d\x0b\x0d\x14\x14\x14\x14"
23    "\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14"
24    "\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14"
25    "\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\x14\xff\xc0"
26    "\x00\x11\x08\x00\x01\x00\x01\x03\x01\x22\x00\x02\x11\x01\x03\x11"
27    "\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00"
28    "\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"
29    "\x0a\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05"
30    "\x05\x04\x04\x00\x00\x01\x7d\x01\x02\x03\x00\x04\x11\x05\x12\x21"
31    "\x31\x41\x06\x13\x51\x61\x07\x22\x71\x14\x32\x81\x91\xa1\x08\x23"
32    "\x42\xb1\xc1\x15\x52\xd1\xf0\x24\x33\x62\x72\x82\x09\x0a\x16\x17"
33    "\x18\x19\x1a\x25\x26\x27\x28\x29\x2a\x34\x35\x36\x37\x38\x39\x3a"
34    "\x43\x44\x45\x46\x47\x48\x49\x4a\x53\x54\x55\x56\x57\x58\x59\x5a"
35    "\x63\x64\x65\x66\x67\x68\x69\x6a\x73\x74\x75\x76\x77\x78\x79\x7a"
36    "\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99"
37    "\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7"
38    "\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5"
39    "\xd6\xd7\xd8\xd9\xda\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf1"
40    "\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xc4\x00\x1f\x01\x00\x03"
41    "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x01"
42    "\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\xff\xc4\x00\xb5\x11\x00"
43    "\x02\x01\x02\x04\x04\x03\x04\x07\x05\x04\x04\x00\x01\x02\x77\x00"
44    "\x01\x02\x03\x11\x04\x05\x21\x31\x06\x12\x41\x51\x07\x61\x71\x13"
45    "\x22\x32\x81\x08\x14\x42\x91\xa1\xb1\xc1\x09\x23\x33\x52\xf0\x15"
46    "\x62\x72\xd1\x0a\x16\x24\x34\xe1\x25\xf1\x17\x18\x19\x1a\x26\x27"
47    "\x28\x29\x2a\x35\x36\x37\x38\x39\x3a\x43\x44\x45\x46\x47\x48\x49"
48    "\x4a\x53\x54\x55\x56\x57\x58\x59\x5a\x63\x64\x65\x66\x67\x68\x69"
49    "\x6a\x73\x74\x75\x76\x77\x78\x79\x7a\x82\x83\x84\x85\x86\x87\x88"
50    "\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6"
51    "\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4"
52    "\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe2"
53    "\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9"
54    "\xfa\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00\x3f\x00\xf9"
55    "\xd2\x8a\x28\xaf\xc3\x0f\xf5\x4c\xff\xd9";
56
57}  // namespace
58
59namespace gfx {
60
61// out of 100, this indicates how compressed it will be, this should be changed
62// with jpeg equality threshold
63// static int jpeg_quality = 75;  // FIXME(brettw)
64static int jpeg_quality = 100;
65
66// The threshold of average color differences where we consider two images
67// equal. This number was picked to be a little above the observed difference
68// using the above quality.
69static double jpeg_equality_threshold = 1.0;
70
71// Computes the average difference between each value in a and b. A and b
72// should be the same size. Used to see if two images are approximately equal
73// in the presence of compression.
74static double AveragePixelDelta(const std::vector<unsigned char>& a,
75                                const std::vector<unsigned char>& b) {
76  // if the sizes are different, say the average difference is the maximum
77  if (a.size() != b.size())
78    return 255.0;
79  if (a.empty())
80    return 0;  // prevent divide by 0 below
81
82  double acc = 0.0;
83  for (size_t i = 0; i < a.size(); i++)
84    acc += fabs(static_cast<double>(a[i]) - static_cast<double>(b[i]));
85
86  return acc / static_cast<double>(a.size());
87}
88
89static void MakeRGBImage(int w, int h, std::vector<unsigned char>* dat) {
90  dat->resize(w * h * 3);
91  for (int y = 0; y < h; y++) {
92    for (int x = 0; x < w; x++) {
93      unsigned char* org_px = &(*dat)[(y * w + x) * 3];
94      org_px[0] = x * 3;      // r
95      org_px[1] = x * 3 + 1;  // g
96      org_px[2] = x * 3 + 2;  // b
97    }
98  }
99}
100
101TEST(JPEGCodec, EncodeDecodeRGB) {
102  int w = 20, h = 20;
103
104  // create an image with known values
105  std::vector<unsigned char> original;
106  MakeRGBImage(w, h, &original);
107
108  // encode, making sure it was compressed some
109  std::vector<unsigned char> encoded;
110  EXPECT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGB, w, h,
111                                w * 3, jpeg_quality, &encoded));
112  EXPECT_GT(original.size(), encoded.size());
113
114  // decode, it should have the same size as the original
115  std::vector<unsigned char> decoded;
116  int outw, outh;
117  EXPECT_TRUE(JPEGCodec::Decode(&encoded[0], encoded.size(),
118                                JPEGCodec::FORMAT_RGB, &decoded,
119                                &outw, &outh));
120  ASSERT_EQ(w, outw);
121  ASSERT_EQ(h, outh);
122  ASSERT_EQ(original.size(), decoded.size());
123
124  // Images must be approximately equal (compression will have introduced some
125  // minor artifacts).
126  ASSERT_GE(jpeg_equality_threshold, AveragePixelDelta(original, decoded));
127}
128
129TEST(JPEGCodec, EncodeDecodeRGBA) {
130  int w = 20, h = 20;
131
132  // create an image with known values, a must be opaque because it will be
133  // lost during compression
134  std::vector<unsigned char> original;
135  original.resize(w * h * 4);
136  for (int y = 0; y < h; y++) {
137    for (int x = 0; x < w; x++) {
138      unsigned char* org_px = &original[(y * w + x) * 4];
139      org_px[0] = x * 3;      // r
140      org_px[1] = x * 3 + 1;  // g
141      org_px[2] = x * 3 + 2;  // b
142      org_px[3] = 0xFF;       // a (opaque)
143    }
144  }
145
146  // encode, making sure it was compressed some
147  std::vector<unsigned char> encoded;
148  EXPECT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGBA, w, h,
149                                w * 4, jpeg_quality, &encoded));
150  EXPECT_GT(original.size(), encoded.size());
151
152  // decode, it should have the same size as the original
153  std::vector<unsigned char> decoded;
154  int outw, outh;
155  EXPECT_TRUE(JPEGCodec::Decode(&encoded[0], encoded.size(),
156                                JPEGCodec::FORMAT_RGBA, &decoded,
157                                &outw, &outh));
158  ASSERT_EQ(w, outw);
159  ASSERT_EQ(h, outh);
160  ASSERT_EQ(original.size(), decoded.size());
161
162  // Images must be approximately equal (compression will have introduced some
163  // minor artifacts).
164  ASSERT_GE(jpeg_equality_threshold, AveragePixelDelta(original, decoded));
165}
166
167// Test that corrupted data decompression causes failures.
168TEST(JPEGCodec, DecodeCorrupted) {
169  int w = 20, h = 20;
170
171  // some random data (an uncompressed image)
172  std::vector<unsigned char> original;
173  MakeRGBImage(w, h, &original);
174
175  // it should fail when given non-JPEG compressed data
176  std::vector<unsigned char> output;
177  int outw, outh;
178  ASSERT_FALSE(JPEGCodec::Decode(&original[0], original.size(),
179                                 JPEGCodec::FORMAT_RGB, &output,
180                                 &outw, &outh));
181
182  // make some compressed data
183  std::vector<unsigned char> compressed;
184  ASSERT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGB, w, h,
185                                w * 3, jpeg_quality, &compressed));
186
187  // try decompressing a truncated version
188  ASSERT_FALSE(JPEGCodec::Decode(&compressed[0], compressed.size() / 2,
189                                 JPEGCodec::FORMAT_RGB, &output,
190                                 &outw, &outh));
191
192  // corrupt it and try decompressing that
193  for (int i = 10; i < 30; i++)
194    compressed[i] = i;
195  ASSERT_FALSE(JPEGCodec::Decode(&compressed[0], compressed.size(),
196                                 JPEGCodec::FORMAT_RGB, &output,
197                                 &outw, &outh));
198}
199
200// Test that we can decode JPEG images without invalid-read errors on valgrind.
201// This test decodes a 1x1 JPEG image and writes the decoded RGB (or RGBA) pixel
202// to the output buffer without OOB reads.
203TEST(JPEGCodec, InvalidRead) {
204  std::vector<unsigned char> output;
205  int outw, outh;
206  JPEGCodec::Decode(kTopSitesMigrationTestImage,
207                    arraysize(kTopSitesMigrationTestImage),
208                    JPEGCodec::FORMAT_RGB, &output,
209                    &outw, &outh);
210
211  JPEGCodec::Decode(kTopSitesMigrationTestImage,
212                    arraysize(kTopSitesMigrationTestImage),
213                    JPEGCodec::FORMAT_RGBA, &output,
214                    &outw, &outh);
215}
216
217}  // namespace gfx
218