blob: 05ad58cd54fe6d1df7a7fd138f2aafdaf6ce9559 [file] [log] [blame]
bungeman@google.com8c6a4f22013-04-23 18:06:23 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8/** Tests for ARGBImageEncoder. */
9
10#include "Test.h"
11#include "SkBitmap.h"
12#include "SkCanvas.h"
13#include "SkImageEncoder.h"
14#include "SkStream.h"
15
16namespace skiatest {
17
18class BitmapTransformerTestClass : public Test {
19public:
20 static Test* Factory(void*) { return SkNEW(BitmapTransformerTestClass); }
21protected:
22 virtual void onGetName(SkString* name) SK_OVERRIDE { name->set("ARGBImageEncoder"); }
23 virtual void onRun(Reporter* reporter) SK_OVERRIDE;
24};
25
26static SkBitmap::Config configs[] = {
27 SkBitmap::kRGB_565_Config,
28 SkBitmap::kARGB_4444_Config,
29 SkBitmap::kARGB_8888_Config,
30};
31
32void BitmapTransformerTestClass::onRun(Reporter* reporter) {
33 // Bytes we expect to get:
34 const int kWidth = 3;
35 const int kHeight = 5;
36 const unsigned char comparisonBuffer[] = {
37 // kHeight rows, each with kWidth pixels, premultiplied ARGB for each pixel
38 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, // red
39 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, // green
40 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
41 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
42 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
43 };
44
45 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder());
46 for (size_t configIndex = 0; configIndex < SK_ARRAY_COUNT(configs); ++configIndex) {
47 // A bitmap that should generate the above bytes:
48 SkBitmap bitmap;
49 {
50 bitmap.setConfig(configs[configIndex], kWidth, kHeight);
51 REPORTER_ASSERT(reporter, bitmap.allocPixels());
52 bitmap.setIsOpaque(true);
53 bitmap.eraseColor(SK_ColorBLUE);
54 // Change rows [0,1] from blue to [red,green].
55 SkCanvas canvas(bitmap);
56 SkPaint paint;
57 paint.setColor(SK_ColorRED);
58 canvas.drawIRect(SkIRect::MakeLTRB(0, 0, kWidth, 1), paint);
59 paint.setColor(SK_ColorGREEN);
60 canvas.drawIRect(SkIRect::MakeLTRB(0, 1, kWidth, 2), paint);
61 }
62
63 // Transform the bitmap.
64 int bufferSize = bitmap.width() * bitmap.height() * 4;
65 SkAutoMalloc pixelBufferManager(bufferSize);
66 char *pixelBuffer = static_cast<char *>(pixelBufferManager.get());
67 SkMemoryWStream out(pixelBuffer, bufferSize);
68 REPORTER_ASSERT(reporter, enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality));
69
70 // Confirm we got the expected results.
71 REPORTER_ASSERT(reporter, bufferSize == sizeof(comparisonBuffer));
72 REPORTER_ASSERT(reporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0);
73 }
74}
75
76static TestRegistry gReg(BitmapTransformerTestClass::Factory);
77
78}