| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <math.h> | 5 #include <math.h> |
| 6 #include <stddef.h> | 6 #include <stddef.h> |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 TEST(JPEGCodec, EncodeDecodeRGBA) { | 104 TEST(JPEGCodec, EncodeDecodeRGBA) { |
| 105 int w = 20, h = 20; | 105 int w = 20, h = 20; |
| 106 | 106 |
| 107 // create an image with known values, a must be opaque because it will be | 107 // create an image with known values, a must be opaque because it will be |
| 108 // lost during compression | 108 // lost during compression |
| 109 std::vector<unsigned char> original; | 109 std::vector<unsigned char> original; |
| 110 MakeRGBAImage(w, h, &original); | 110 MakeRGBAImage(w, h, &original); |
| 111 | 111 |
| 112 // encode, making sure it was compressed some | 112 // encode, making sure it was compressed some |
| 113 std::vector<unsigned char> encoded; | 113 std::vector<unsigned char> encoded; |
| 114 EXPECT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGBA, w, h, | 114 SkImageInfo info = |
| 115 w * 4, jpeg_quality, &encoded)); | 115 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 116 SkPixmap src(info, &original[0], w * 4); |
| 117 EXPECT_TRUE(JPEGCodec::Encode(src, jpeg_quality, &encoded)); |
| 116 EXPECT_GT(original.size(), encoded.size()); | 118 EXPECT_GT(original.size(), encoded.size()); |
| 117 | 119 |
| 118 // decode, it should have the same size as the original | 120 // decode, it should have the same size as the original |
| 119 std::vector<unsigned char> decoded; | 121 std::vector<unsigned char> decoded; |
| 120 int outw, outh; | 122 int outw, outh; |
| 121 EXPECT_TRUE(JPEGCodec::Decode(&encoded[0], encoded.size(), | 123 EXPECT_TRUE(JPEGCodec::Decode(&encoded[0], encoded.size(), |
| 122 JPEGCodec::FORMAT_RGBA, &decoded, | 124 JPEGCodec::FORMAT_RGBA, &decoded, |
| 123 &outw, &outh)); | 125 &outw, &outh)); |
| 124 ASSERT_EQ(w, outw); | 126 ASSERT_EQ(w, outw); |
| 125 ASSERT_EQ(h, outh); | 127 ASSERT_EQ(h, outh); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 140 | 142 |
| 141 // it should fail when given non-JPEG compressed data | 143 // it should fail when given non-JPEG compressed data |
| 142 std::vector<unsigned char> output; | 144 std::vector<unsigned char> output; |
| 143 int outw, outh; | 145 int outw, outh; |
| 144 ASSERT_FALSE(JPEGCodec::Decode(&original[0], original.size(), | 146 ASSERT_FALSE(JPEGCodec::Decode(&original[0], original.size(), |
| 145 JPEGCodec::FORMAT_RGBA, &output, &outw, | 147 JPEGCodec::FORMAT_RGBA, &output, &outw, |
| 146 &outh)); | 148 &outh)); |
| 147 | 149 |
| 148 // make some compressed data | 150 // make some compressed data |
| 149 std::vector<unsigned char> compressed; | 151 std::vector<unsigned char> compressed; |
| 150 ASSERT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGBA, w, h, | 152 SkImageInfo info = |
| 151 w * 3, jpeg_quality, &compressed)); | 153 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 154 SkPixmap src(info, &original[0], w * 4); |
| 155 ASSERT_TRUE(JPEGCodec::Encode(src, jpeg_quality, &compressed)); |
| 152 | 156 |
| 153 // try decompressing a truncated version | 157 // try decompressing a truncated version |
| 154 ASSERT_FALSE(JPEGCodec::Decode(&compressed[0], compressed.size() / 2, | 158 ASSERT_FALSE(JPEGCodec::Decode(&compressed[0], compressed.size() / 2, |
| 155 JPEGCodec::FORMAT_RGBA, &output, &outw, | 159 JPEGCodec::FORMAT_RGBA, &output, &outw, |
| 156 &outh)); | 160 &outh)); |
| 157 | 161 |
| 158 // corrupt it and try decompressing that | 162 // corrupt it and try decompressing that |
| 159 for (int i = 10; i < 30; i++) | 163 for (int i = 10; i < 30; i++) |
| 160 compressed[i] = i; | 164 compressed[i] = i; |
| 161 ASSERT_FALSE(JPEGCodec::Decode(&compressed[0], compressed.size(), | 165 ASSERT_FALSE(JPEGCodec::Decode(&compressed[0], compressed.size(), |
| 162 JPEGCodec::FORMAT_RGBA, &output, &outw, | 166 JPEGCodec::FORMAT_RGBA, &output, &outw, |
| 163 &outh)); | 167 &outh)); |
| 164 } | 168 } |
| 165 | 169 |
| 166 // Test that we can decode JPEG images without invalid-read errors on valgrind. | 170 // Test that we can decode JPEG images without invalid-read errors on valgrind. |
| 167 // This test decodes a 1x1 JPEG image and writes the decoded RGB (or RGBA) pixel | 171 // This test decodes a 1x1 JPEG image and writes the decoded RGB (or RGBA) pixel |
| 168 // to the output buffer without OOB reads. | 172 // to the output buffer without OOB reads. |
| 169 TEST(JPEGCodec, InvalidRead) { | 173 TEST(JPEGCodec, InvalidRead) { |
| 170 std::vector<unsigned char> output; | 174 std::vector<unsigned char> output; |
| 171 int outw, outh; | 175 int outw, outh; |
| 172 JPEGCodec::Decode(kTopSitesMigrationTestImage, | 176 JPEGCodec::Decode(kTopSitesMigrationTestImage, |
| 173 arraysize(kTopSitesMigrationTestImage), | 177 arraysize(kTopSitesMigrationTestImage), |
| 174 JPEGCodec::FORMAT_RGBA, &output, | 178 JPEGCodec::FORMAT_RGBA, &output, |
| 175 &outw, &outh); | 179 &outw, &outh); |
| 176 } | 180 } |
| 177 | 181 |
| 178 } // namespace gfx | 182 } // namespace gfx |
| OLD | NEW |