1 /* 2 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 33 #include "platform/image-decoders/jpeg/JPEGImageDecoder.h" 34 35 #include "platform/SharedBuffer.h" 36 #include "public/platform/Platform.h" 37 #include "public/platform/WebData.h" 38 #include "public/platform/WebSize.h" 39 #include "public/platform/WebUnitTestSupport.h" 40 #include "wtf/OwnPtr.h" 41 #include "wtf/PassOwnPtr.h" 42 #include "wtf/StringHasher.h" 43 44 #include <gtest/gtest.h> 45 46 using namespace blink; 47 48 static const size_t LargeEnoughSize = 1000 * 1000; 49 50 namespace { 51 52 PassRefPtr<SharedBuffer> readFile(const char* fileName) 53 { 54 String filePath = Platform::current()->unitTestSupport()->webKitRootDir(); 55 filePath.append(fileName); 56 57 return Platform::current()->unitTestSupport()->readFromFile(filePath); 58 } 59 60 PassOwnPtr<JPEGImageDecoder> createDecoder(size_t maxDecodedBytes) 61 { 62 return adoptPtr(new JPEGImageDecoder(ImageSource::AlphaNotPremultiplied, ImageSource::GammaAndColorProfileApplied, maxDecodedBytes)); 63 } 64 65 } // namespace 66 67 void downsample(size_t maxDecodedBytes, unsigned* outputWidth, unsigned* outputHeight, const char* imageFilePath) 68 { 69 RefPtr<SharedBuffer> data = readFile(imageFilePath); 70 ASSERT_TRUE(data.get()); 71 72 OwnPtr<JPEGImageDecoder> decoder = createDecoder(maxDecodedBytes); 73 decoder->setData(data.get(), true); 74 75 ImageFrame* frame = decoder->frameBufferAtIndex(0); 76 ASSERT_TRUE(frame); 77 *outputWidth = frame->getSkBitmap().width(); 78 *outputHeight = frame->getSkBitmap().height(); 79 EXPECT_EQ(IntSize(*outputWidth, *outputHeight), decoder->decodedSize()); 80 } 81 82 void readYUV(size_t maxDecodedBytes, unsigned* outputYWidth, unsigned* outputYHeight, unsigned* outputUVWidth, unsigned* outputUVHeight, const char* imageFilePath) 83 { 84 RefPtr<SharedBuffer> data = readFile(imageFilePath); 85 ASSERT_TRUE(data.get()); 86 87 OwnPtr<JPEGImageDecoder> decoder = createDecoder(maxDecodedBytes); 88 decoder->setData(data.get(), true); 89 90 OwnPtr<ImagePlanes> imagePlanes = adoptPtr(new ImagePlanes()); 91 decoder->setImagePlanes(imagePlanes.release()); 92 bool sizeIsAvailable = decoder->isSizeAvailable(); 93 ASSERT_TRUE(sizeIsAvailable); 94 95 IntSize size = decoder->decodedSize(); 96 IntSize ySize = decoder->decodedYUVSize(0, ImageDecoder::ActualSize); 97 IntSize uSize = decoder->decodedYUVSize(1, ImageDecoder::ActualSize); 98 IntSize vSize = decoder->decodedYUVSize(2, ImageDecoder::ActualSize); 99 100 ASSERT_TRUE(size.width() == ySize.width()); 101 ASSERT_TRUE(size.height() == ySize.height()); 102 ASSERT_TRUE(uSize.width() == vSize.width()); 103 ASSERT_TRUE(uSize.height() == vSize.height()); 104 105 *outputYWidth = ySize.width(); 106 *outputYHeight = ySize.height(); 107 *outputUVWidth = uSize.width(); 108 *outputUVHeight = uSize.height(); 109 } 110 111 // Tests failure on a too big image. 112 TEST(JPEGImageDecoderTest, tooBig) 113 { 114 OwnPtr<JPEGImageDecoder> decoder = createDecoder(100); 115 EXPECT_FALSE(decoder->setSize(10000, 10000)); 116 EXPECT_TRUE(decoder->failed()); 117 } 118 119 // Tests that JPEG decoder can downsample image whose width and height are multiple of 8, 120 // to ensure we compute the correct decodedSize and pass correct parameters to libjpeg to 121 // output image with the expected size. 122 TEST(JPEGImageDecoderTest, downsampleImageSizeMultipleOf8) 123 { 124 const char* jpegFile = "/LayoutTests/fast/images/resources/lenna.jpg"; // 256x256 125 unsigned outputWidth, outputHeight; 126 127 // 1/8 downsample. 128 downsample(40 * 40 * 4, &outputWidth, &outputHeight, jpegFile); 129 EXPECT_EQ(32u, outputWidth); 130 EXPECT_EQ(32u, outputHeight); 131 132 // 2/8 downsample. 133 downsample(70 * 70 * 4, &outputWidth, &outputHeight, jpegFile); 134 EXPECT_EQ(64u, outputWidth); 135 EXPECT_EQ(64u, outputHeight); 136 137 // 3/8 downsample. 138 downsample(100 * 100 * 4, &outputWidth, &outputHeight, jpegFile); 139 EXPECT_EQ(96u, outputWidth); 140 EXPECT_EQ(96u, outputHeight); 141 142 // 4/8 downsample. 143 downsample(130 * 130 * 4, &outputWidth, &outputHeight, jpegFile); 144 EXPECT_EQ(128u, outputWidth); 145 EXPECT_EQ(128u, outputHeight); 146 147 // 5/8 downsample. 148 downsample(170 * 170 * 4, &outputWidth, &outputHeight, jpegFile); 149 EXPECT_EQ(160u, outputWidth); 150 EXPECT_EQ(160u, outputHeight); 151 152 // 6/8 downsample. 153 downsample(200 * 200 * 4, &outputWidth, &outputHeight, jpegFile); 154 EXPECT_EQ(192u, outputWidth); 155 EXPECT_EQ(192u, outputHeight); 156 157 // 7/8 downsample. 158 downsample(230 * 230 * 4, &outputWidth, &outputHeight, jpegFile); 159 EXPECT_EQ(224u, outputWidth); 160 EXPECT_EQ(224u, outputHeight); 161 } 162 163 // Tests that JPEG decoder can downsample image whose width and height are not multiple of 8. 164 // Ensures that we round decodedSize and scale_num using the same algorithm as that of libjpeg. 165 TEST(JPEGImageDecoderTest, downsampleImageSizeNotMultipleOf8) 166 { 167 const char* jpegFile = "/LayoutTests/fast/images/resources/icc-v2-gbr.jpg"; // 275x207 168 unsigned outputWidth, outputHeight; 169 170 // 1/8 downsample. 171 downsample(40 * 40 * 4, &outputWidth, &outputHeight, jpegFile); 172 EXPECT_EQ(35u, outputWidth); 173 EXPECT_EQ(26u, outputHeight); 174 175 // 2/8 downsample. 176 downsample(70 * 70 * 4, &outputWidth, &outputHeight, jpegFile); 177 EXPECT_EQ(69u, outputWidth); 178 EXPECT_EQ(52u, outputHeight); 179 180 // 3/8 downsample. 181 downsample(100 * 100 * 4, &outputWidth, &outputHeight, jpegFile); 182 EXPECT_EQ(104u, outputWidth); 183 EXPECT_EQ(78u, outputHeight); 184 185 // 4/8 downsample. 186 downsample(130 * 130 * 4, &outputWidth, &outputHeight, jpegFile); 187 EXPECT_EQ(138u, outputWidth); 188 EXPECT_EQ(104u, outputHeight); 189 190 // 5/8 downsample. 191 downsample(170 * 170 * 4, &outputWidth, &outputHeight, jpegFile); 192 EXPECT_EQ(172u, outputWidth); 193 EXPECT_EQ(130u, outputHeight); 194 195 // 6/8 downsample. 196 downsample(200 * 200 * 4, &outputWidth, &outputHeight, jpegFile); 197 EXPECT_EQ(207u, outputWidth); 198 EXPECT_EQ(156u, outputHeight); 199 200 // 7/8 downsample. 201 downsample(230 * 230 * 4, &outputWidth, &outputHeight, jpegFile); 202 EXPECT_EQ(241u, outputWidth); 203 EXPECT_EQ(182u, outputHeight); 204 } 205 206 // Tests that upsampling is not allowed. 207 TEST(JPEGImageDecoderTest, upsample) 208 { 209 const char* jpegFile = "/LayoutTests/fast/images/resources/lenna.jpg"; // 256x256 210 unsigned outputWidth, outputHeight; 211 downsample(LargeEnoughSize, &outputWidth, &outputHeight, jpegFile); 212 EXPECT_EQ(256u, outputWidth); 213 EXPECT_EQ(256u, outputHeight); 214 } 215 216 TEST(JPEGImageDecoderTest, yuv) 217 { 218 const char* jpegFile = "/LayoutTests/fast/images/resources/lenna.jpg"; // 256x256, YUV 4:2:0 219 unsigned outputYWidth, outputYHeight, outputUVWidth, outputUVHeight; 220 readYUV(LargeEnoughSize, &outputYWidth, &outputYHeight, &outputUVWidth, &outputUVHeight, jpegFile); 221 EXPECT_EQ(256u, outputYWidth); 222 EXPECT_EQ(256u, outputYHeight); 223 EXPECT_EQ(128u, outputUVWidth); 224 EXPECT_EQ(128u, outputUVHeight); 225 } 226