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 #include "wtf/Vector.h" 44 45 #include <gtest/gtest.h> 46 47 using namespace WebCore; 48 using namespace blink; 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 // Tests failure on a too big image. 83 TEST(JPEGImageDecoderTest, tooBig) 84 { 85 OwnPtr<JPEGImageDecoder> decoder = createDecoder(100); 86 EXPECT_FALSE(decoder->setSize(10000, 10000)); 87 EXPECT_TRUE(decoder->failed()); 88 } 89 90 // Tests that JPEG decoder can downsample image whose width and height are multiple of 8, 91 // to ensure we compute the correct decodedSize and pass correct parameters to libjpeg to 92 // output image with the expected size. 93 TEST(JPEGImageDecoderTest, downsampleImageSizeMultipleOf8) 94 { 95 const char* jpegFile = "/LayoutTests/fast/images/resources/lenna.jpg"; // 256x256 96 unsigned outputWidth, outputHeight; 97 98 // 1/8 downsample. 99 downsample(40 * 40 * 4, &outputWidth, &outputHeight, jpegFile); 100 EXPECT_EQ(32u, outputWidth); 101 EXPECT_EQ(32u, outputHeight); 102 103 // 2/8 downsample. 104 downsample(70 * 70 * 4, &outputWidth, &outputHeight, jpegFile); 105 EXPECT_EQ(64u, outputWidth); 106 EXPECT_EQ(64u, outputHeight); 107 108 // 3/8 downsample. 109 downsample(100 * 100 * 4, &outputWidth, &outputHeight, jpegFile); 110 EXPECT_EQ(96u, outputWidth); 111 EXPECT_EQ(96u, outputHeight); 112 113 // 4/8 downsample. 114 downsample(130 * 130 * 4, &outputWidth, &outputHeight, jpegFile); 115 EXPECT_EQ(128u, outputWidth); 116 EXPECT_EQ(128u, outputHeight); 117 118 // 5/8 downsample. 119 downsample(170 * 170 * 4, &outputWidth, &outputHeight, jpegFile); 120 EXPECT_EQ(160u, outputWidth); 121 EXPECT_EQ(160u, outputHeight); 122 123 // 6/8 downsample. 124 downsample(200 * 200 * 4, &outputWidth, &outputHeight, jpegFile); 125 EXPECT_EQ(192u, outputWidth); 126 EXPECT_EQ(192u, outputHeight); 127 128 // 7/8 downsample. 129 downsample(230 * 230 * 4, &outputWidth, &outputHeight, jpegFile); 130 EXPECT_EQ(224u, outputWidth); 131 EXPECT_EQ(224u, outputHeight); 132 } 133 134 // Tests that JPEG decoder can downsample image whose width and height are not multiple of 8. 135 // Ensures that we round decodedSize and scale_num using the same algorithm as that of libjpeg. 136 TEST(JPEGImageDecoderTest, downsampleImageSizeNotMultipleOf8) 137 { 138 const char* jpegFile = "/LayoutTests/fast/images/resources/icc-v2-gbr.jpg"; // 275x207 139 unsigned outputWidth, outputHeight; 140 141 // 1/8 downsample. 142 downsample(40 * 40 * 4, &outputWidth, &outputHeight, jpegFile); 143 EXPECT_EQ(35u, outputWidth); 144 EXPECT_EQ(26u, outputHeight); 145 146 // 2/8 downsample. 147 downsample(70 * 70 * 4, &outputWidth, &outputHeight, jpegFile); 148 EXPECT_EQ(69u, outputWidth); 149 EXPECT_EQ(52u, outputHeight); 150 151 // 3/8 downsample. 152 downsample(100 * 100 * 4, &outputWidth, &outputHeight, jpegFile); 153 EXPECT_EQ(104u, outputWidth); 154 EXPECT_EQ(78u, outputHeight); 155 156 // 4/8 downsample. 157 downsample(130 * 130 * 4, &outputWidth, &outputHeight, jpegFile); 158 EXPECT_EQ(138u, outputWidth); 159 EXPECT_EQ(104u, outputHeight); 160 161 // 5/8 downsample. 162 downsample(170 * 170 * 4, &outputWidth, &outputHeight, jpegFile); 163 EXPECT_EQ(172u, outputWidth); 164 EXPECT_EQ(130u, outputHeight); 165 166 // 6/8 downsample. 167 downsample(200 * 200 * 4, &outputWidth, &outputHeight, jpegFile); 168 EXPECT_EQ(207u, outputWidth); 169 EXPECT_EQ(156u, outputHeight); 170 171 // 7/8 downsample. 172 downsample(230 * 230 * 4, &outputWidth, &outputHeight, jpegFile); 173 EXPECT_EQ(241u, outputWidth); 174 EXPECT_EQ(182u, outputHeight); 175 } 176 177 // Tests that upsampling is not allowed. 178 TEST(JPEGImageDecoderTest, upsample) 179 { 180 const char* jpegFile = "/LayoutTests/fast/images/resources/lenna.jpg"; // 256x256 181 unsigned outputWidth, outputHeight; 182 downsample(1000 * 1000, &outputWidth, &outputHeight, jpegFile); 183 EXPECT_EQ(256u, outputWidth); 184 EXPECT_EQ(256u, outputHeight); 185 } 186