blob: 62167f0f2ab0f425a2bb8185d6de0fe5915e3bbf [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"
scroggo565901d2015-12-10 10:44:13 -080013#include "SkTemplates.h"
tfarina@chromium.org452eecb2014-01-06 12:43:53 +000014#include "Test.h"
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000015
reed6c225732014-06-09 19:52:07 -070016static SkColorType gColorTypes[] = {
17 kRGB_565_SkColorType,
18 kN32_SkColorType,
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());
reed6c225732014-06-09 19:52:07 -070035 for (size_t ctIndex = 0; ctIndex < SK_ARRAY_COUNT(gColorTypes); ++ctIndex) {
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000036 // A bitmap that should generate the above bytes:
37 SkBitmap bitmap;
38 {
reed84825042014-09-02 12:50:45 -070039 bitmap.allocPixels(SkImageInfo::Make(kWidth, kHeight, gColorTypes[ctIndex],
40 kOpaque_SkAlphaType));
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000041 bitmap.eraseColor(SK_ColorBLUE);
42 // Change rows [0,1] from blue to [red,green].
43 SkCanvas canvas(bitmap);
44 SkPaint paint;
45 paint.setColor(SK_ColorRED);
46 canvas.drawIRect(SkIRect::MakeLTRB(0, 0, kWidth, 1), paint);
47 paint.setColor(SK_ColorGREEN);
48 canvas.drawIRect(SkIRect::MakeLTRB(0, 1, kWidth, 2), paint);
49 }
50
51 // Transform the bitmap.
52 int bufferSize = bitmap.width() * bitmap.height() * 4;
scroggo565901d2015-12-10 10:44:13 -080053 SkAutoTMalloc<char> pixelBufferManager(bufferSize);
54 char* pixelBuffer = pixelBufferManager.get();
bungeman@google.com8c6a4f22013-04-23 18:06:23 +000055 SkMemoryWStream out(pixelBuffer, bufferSize);
56 REPORTER_ASSERT(reporter, enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality));
57
58 // Confirm we got the expected results.
59 REPORTER_ASSERT(reporter, bufferSize == sizeof(comparisonBuffer));
60 REPORTER_ASSERT(reporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0);
61 }
62}