This source file includes following definitions.
- readFile
- createDecoder
- downsample
- TEST
- TEST
- TEST
- TEST
#include "config.h"
#include "platform/image-decoders/jpeg/JPEGImageDecoder.h"
#include "platform/SharedBuffer.h"
#include "public/platform/Platform.h"
#include "public/platform/WebData.h"
#include "public/platform/WebSize.h"
#include "public/platform/WebUnitTestSupport.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/StringHasher.h"
#include "wtf/Vector.h"
#include <gtest/gtest.h>
using namespace WebCore;
using namespace blink;
namespace {
PassRefPtr<SharedBuffer> readFile(const char* fileName)
{
String filePath = Platform::current()->unitTestSupport()->webKitRootDir();
filePath.append(fileName);
return Platform::current()->unitTestSupport()->readFromFile(filePath);
}
PassOwnPtr<JPEGImageDecoder> createDecoder(size_t maxDecodedBytes)
{
return adoptPtr(new JPEGImageDecoder(ImageSource::AlphaNotPremultiplied, ImageSource::GammaAndColorProfileApplied, maxDecodedBytes));
}
}
void downsample(size_t maxDecodedBytes, unsigned* outputWidth, unsigned* outputHeight, const char* imageFilePath)
{
RefPtr<SharedBuffer> data = readFile(imageFilePath);
ASSERT_TRUE(data.get());
OwnPtr<JPEGImageDecoder> decoder = createDecoder(maxDecodedBytes);
decoder->setData(data.get(), true);
ImageFrame* frame = decoder->frameBufferAtIndex(0);
ASSERT_TRUE(frame);
*outputWidth = frame->getSkBitmap().width();
*outputHeight = frame->getSkBitmap().height();
EXPECT_EQ(IntSize(*outputWidth, *outputHeight), decoder->decodedSize());
}
TEST(JPEGImageDecoderTest, tooBig)
{
OwnPtr<JPEGImageDecoder> decoder = createDecoder(100);
EXPECT_FALSE(decoder->setSize(10000, 10000));
EXPECT_TRUE(decoder->failed());
}
TEST(JPEGImageDecoderTest, downsampleImageSizeMultipleOf8)
{
const char* jpegFile = "/LayoutTests/fast/images/resources/lenna.jpg";
unsigned outputWidth, outputHeight;
downsample(40 * 40 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(32u, outputWidth);
EXPECT_EQ(32u, outputHeight);
downsample(70 * 70 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(64u, outputWidth);
EXPECT_EQ(64u, outputHeight);
downsample(100 * 100 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(96u, outputWidth);
EXPECT_EQ(96u, outputHeight);
downsample(130 * 130 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(128u, outputWidth);
EXPECT_EQ(128u, outputHeight);
downsample(170 * 170 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(160u, outputWidth);
EXPECT_EQ(160u, outputHeight);
downsample(200 * 200 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(192u, outputWidth);
EXPECT_EQ(192u, outputHeight);
downsample(230 * 230 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(224u, outputWidth);
EXPECT_EQ(224u, outputHeight);
}
TEST(JPEGImageDecoderTest, downsampleImageSizeNotMultipleOf8)
{
const char* jpegFile = "/LayoutTests/fast/images/resources/icc-v2-gbr.jpg";
unsigned outputWidth, outputHeight;
downsample(40 * 40 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(35u, outputWidth);
EXPECT_EQ(26u, outputHeight);
downsample(70 * 70 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(69u, outputWidth);
EXPECT_EQ(52u, outputHeight);
downsample(100 * 100 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(104u, outputWidth);
EXPECT_EQ(78u, outputHeight);
downsample(130 * 130 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(138u, outputWidth);
EXPECT_EQ(104u, outputHeight);
downsample(170 * 170 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(172u, outputWidth);
EXPECT_EQ(130u, outputHeight);
downsample(200 * 200 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(207u, outputWidth);
EXPECT_EQ(156u, outputHeight);
downsample(230 * 230 * 4, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(241u, outputWidth);
EXPECT_EQ(182u, outputHeight);
}
TEST(JPEGImageDecoderTest, upsample)
{
const char* jpegFile = "/LayoutTests/fast/images/resources/lenna.jpg";
unsigned outputWidth, outputHeight;
downsample(1000 * 1000, &outputWidth, &outputHeight, jpegFile);
EXPECT_EQ(256u, outputWidth);
EXPECT_EQ(256u, outputHeight);
}