blob: bffcaa4f7873001d451f42eb628d7cf22277b8a5 [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,
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000028 SkBitmap::kARGB_8888_Config,
29};
30
31void BitmapTransformerTestClass::onRun(Reporter* reporter) {
32 // Bytes we expect to get:
33 const int kWidth = 3;
34 const int kHeight = 5;
35 const unsigned char comparisonBuffer[] = {
36 // kHeight rows, each with kWidth pixels, premultiplied ARGB for each pixel
37 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, // red
38 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, // green
39 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
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 };
43
44 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder());
45 for (size_t configIndex = 0; configIndex < SK_ARRAY_COUNT(configs); ++configIndex) {
46 // A bitmap that should generate the above bytes:
47 SkBitmap bitmap;
48 {
49 bitmap.setConfig(configs[configIndex], kWidth, kHeight);
50 REPORTER_ASSERT(reporter, bitmap.allocPixels());
reed@google.com383a6972013-10-21 14:00:07 +000051 bitmap.setAlphaType(kOpaque_SkAlphaType);
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000052 bitmap.eraseColor(SK_ColorBLUE);
53 // Change rows [0,1] from blue to [red,green].
54 SkCanvas canvas(bitmap);
55 SkPaint paint;
56 paint.setColor(SK_ColorRED);
57 canvas.drawIRect(SkIRect::MakeLTRB(0, 0, kWidth, 1), paint);
58 paint.setColor(SK_ColorGREEN);
59 canvas.drawIRect(SkIRect::MakeLTRB(0, 1, kWidth, 2), paint);
60 }
61
62 // Transform the bitmap.
63 int bufferSize = bitmap.width() * bitmap.height() * 4;
64 SkAutoMalloc pixelBufferManager(bufferSize);
65 char *pixelBuffer = static_cast<char *>(pixelBufferManager.get());
66 SkMemoryWStream out(pixelBuffer, bufferSize);
67 REPORTER_ASSERT(reporter, enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality));
68
69 // Confirm we got the expected results.
70 REPORTER_ASSERT(reporter, bufferSize == sizeof(comparisonBuffer));
71 REPORTER_ASSERT(reporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0);
72 }
73}
74
75static TestRegistry gReg(BitmapTransformerTestClass::Factory);
76
77}