blob: 8157cc779f61048cff81b7954d7eff5d83be8c17 [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
tfarina@chromium.org452eecb2014-01-06 12:43:53 +00008#include "SkImageEncoder.h"
bungeman@google.com8c6a4f22013-04-23 18:06:23 +00009
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000010#include "SkBitmap.h"
11#include "SkCanvas.h"
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000012#include "SkStream.h"
tfarina@chromium.org452eecb2014-01-06 12:43:53 +000013#include "Test.h"
14#include "TestClassDef.h"
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000015
16static SkBitmap::Config configs[] = {
tfarina@chromium.org452eecb2014-01-06 12:43:53 +000017 SkBitmap::kRGB_565_Config,
18 SkBitmap::kARGB_8888_Config,
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000019};
20
tfarina@chromium.org452eecb2014-01-06 12:43:53 +000021DEF_TEST(ARGBImageEncoder, reporter) {
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000022 // Bytes we expect to get:
23 const int kWidth = 3;
24 const int kHeight = 5;
25 const unsigned char comparisonBuffer[] = {
26 // kHeight rows, each with kWidth pixels, premultiplied ARGB for each pixel
27 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, // red
28 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, // green
29 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
30 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
31 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
32 };
33
34 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder());
35 for (size_t configIndex = 0; configIndex < SK_ARRAY_COUNT(configs); ++configIndex) {
36 // A bitmap that should generate the above bytes:
37 SkBitmap bitmap;
38 {
39 bitmap.setConfig(configs[configIndex], kWidth, kHeight);
40 REPORTER_ASSERT(reporter, bitmap.allocPixels());
reed@google.com383a6972013-10-21 14:00:07 +000041 bitmap.setAlphaType(kOpaque_SkAlphaType);
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000042 bitmap.eraseColor(SK_ColorBLUE);
43 // Change rows [0,1] from blue to [red,green].
44 SkCanvas canvas(bitmap);
45 SkPaint paint;
46 paint.setColor(SK_ColorRED);
47 canvas.drawIRect(SkIRect::MakeLTRB(0, 0, kWidth, 1), paint);
48 paint.setColor(SK_ColorGREEN);
49 canvas.drawIRect(SkIRect::MakeLTRB(0, 1, kWidth, 2), paint);
50 }
51
52 // Transform the bitmap.
53 int bufferSize = bitmap.width() * bitmap.height() * 4;
54 SkAutoMalloc pixelBufferManager(bufferSize);
55 char *pixelBuffer = static_cast<char *>(pixelBufferManager.get());
56 SkMemoryWStream out(pixelBuffer, bufferSize);
57 REPORTER_ASSERT(reporter, enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality));
58
59 // Confirm we got the expected results.
60 REPORTER_ASSERT(reporter, bufferSize == sizeof(comparisonBuffer));
61 REPORTER_ASSERT(reporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0);
62 }
63}