blob: 17d0a00da8a3f7616c1ff8ee3b759f860dd8eab4 [file] [log] [blame]
epoger@google.com31114c62012-12-12 17:22:23 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9/**
10 * Tests for SkBitmapTransformer.h and SkBitmapTransformer.cpp
11 */
12
13#include "Test.h"
14#include "SkBitmap.h"
15#include "SkBitmapTransformer.h"
16
17namespace skiatest {
18 class BitmapTransformerTestClass : public Test {
19 public:
20 static Test* Factory(void*) {return SkNEW(BitmapTransformerTestClass); }
21 protected:
22 virtual void onGetName(SkString* name) { name->set("BitmapTransformer"); }
23 virtual void onRun(Reporter* reporter) {
24 this->fReporter = reporter;
25 RunTest();
26 }
27 private:
28 void RunTest() {
29 SkBitmap bitmap;
30 SkBitmap::Config supportedConfig = SkBitmap::kARGB_8888_Config;
31 SkBitmap::Config unsupportedConfig = SkBitmap::kARGB_4444_Config;
32 SkBitmapTransformer::PixelFormat supportedPixelFormat =
33 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat;
34 const int kWidth = 55;
35 const int kHeight = 333;
36
37 // Transformations that we know are unsupported:
38 {
39 bitmap.setConfig(unsupportedConfig, kWidth, kHeight);
40 SkBitmapTransformer transformer = SkBitmapTransformer(bitmap, supportedPixelFormat);
41 REPORTER_ASSERT(fReporter, !transformer.isValid());
42 }
43
44 // Valid transformations:
45 {
46 // Bytes we expect to get:
47 const int kWidth = 3;
48 const int kHeight = 5;
49 const char comparisonBuffer[] = {
50 // kHeight rows, each with kWidth pixels, premultiplied ARGB for each pixel
51 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, // red
52 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, // green
53 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
54 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
55 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
56 };
57
58 // A bitmap that should generate the above bytes:
59 bitmap.setConfig(supportedConfig, kWidth, kHeight);
60 REPORTER_ASSERT(fReporter, bitmap.allocPixels());
61 bitmap.setIsOpaque(true);
62 bitmap.eraseColor(SK_ColorBLUE);
63 bitmap.lockPixels();
64 // Change rows [0,1] from blue to [red,green].
65 SkColor oldColor = SK_ColorBLUE;
66 SkColor newColors[] = {SK_ColorRED, SK_ColorGREEN};
67 for (int y = 0; y <= 1; y++) {
68 for (int x = 0; x < kWidth; x++) {
69 REPORTER_ASSERT(fReporter, bitmap.getColor(x, y) == oldColor);
70 SkPMColor* pixel = static_cast<SkPMColor *>(bitmap.getAddr(x, y));
71 *pixel = SkPreMultiplyColor(newColors[y]);
72 REPORTER_ASSERT(fReporter, bitmap.getColor(x, y) == newColors[y]);
73 }
74 }
75 bitmap.unlockPixels();
76
77 // Transform the bitmap and confirm we got the expected results.
78 SkBitmapTransformer transformer = SkBitmapTransformer(bitmap, supportedPixelFormat);
79 REPORTER_ASSERT(fReporter, transformer.isValid());
80 REPORTER_ASSERT(fReporter, transformer.bytesNeededPerRow() == kWidth * 4);
81 REPORTER_ASSERT(fReporter, transformer.bytesNeededTotal() == kWidth * kHeight * 4);
82 int bufferSize = transformer.bytesNeededTotal();
83 SkAutoMalloc pixelBufferManager(bufferSize);
84 char *pixelBuffer = static_cast<char *>(pixelBufferManager.get());
85 REPORTER_ASSERT(fReporter,
86 transformer.copyBitmapToPixelBuffer(pixelBuffer, bufferSize));
87 REPORTER_ASSERT(fReporter, bufferSize == sizeof(comparisonBuffer));
88 REPORTER_ASSERT(fReporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0);
89 }
90
91 }
92
93 Reporter* fReporter;
94 };
95
96 static TestRegistry gReg(BitmapTransformerTestClass::Factory);
97}