2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkImageEncoder.h"
15 static SkColorType gColorTypes[] = {
20 DEF_TEST(ARGBImageEncoder, reporter) {
21 // Bytes we expect to get:
23 const int kHeight = 5;
24 const unsigned char comparisonBuffer[] = {
25 // kHeight rows, each with kWidth pixels, premultiplied ARGB for each pixel
26 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, // red
27 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, // green
28 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue
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
33 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder());
34 for (size_t ctIndex = 0; ctIndex < SK_ARRAY_COUNT(gColorTypes); ++ctIndex) {
35 // A bitmap that should generate the above bytes:
38 bitmap.allocPixels(SkImageInfo::Make(kWidth, kHeight, gColorTypes[ctIndex],
39 kOpaque_SkAlphaType));
40 bitmap.eraseColor(SK_ColorBLUE);
41 // Change rows [0,1] from blue to [red,green].
42 SkCanvas canvas(bitmap);
44 paint.setColor(SK_ColorRED);
45 canvas.drawIRect(SkIRect::MakeLTRB(0, 0, kWidth, 1), paint);
46 paint.setColor(SK_ColorGREEN);
47 canvas.drawIRect(SkIRect::MakeLTRB(0, 1, kWidth, 2), paint);
50 // Transform the bitmap.
51 int bufferSize = bitmap.width() * bitmap.height() * 4;
52 SkAutoMalloc pixelBufferManager(bufferSize);
53 char *pixelBuffer = static_cast<char *>(pixelBufferManager.get());
54 SkMemoryWStream out(pixelBuffer, bufferSize);
55 REPORTER_ASSERT(reporter, enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality));
57 // Confirm we got the expected results.
58 REPORTER_ASSERT(reporter, bufferSize == sizeof(comparisonBuffer));
59 REPORTER_ASSERT(reporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0);