bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 452eecb | 2014-01-06 12:43:53 +0000 | [diff] [blame^] | 8 | #include "SkImageEncoder.h" |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 9 | |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 12 | #include "SkStream.h" |
tfarina@chromium.org | 452eecb | 2014-01-06 12:43:53 +0000 | [diff] [blame^] | 13 | #include "Test.h" |
| 14 | #include "TestClassDef.h" |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 15 | |
| 16 | static SkBitmap::Config configs[] = { |
tfarina@chromium.org | 452eecb | 2014-01-06 12:43:53 +0000 | [diff] [blame^] | 17 | SkBitmap::kRGB_565_Config, |
| 18 | SkBitmap::kARGB_8888_Config, |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 19 | }; |
| 20 | |
tfarina@chromium.org | 452eecb | 2014-01-06 12:43:53 +0000 | [diff] [blame^] | 21 | DEF_TEST(ARGBImageEncoder, reporter) { |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 22 | // 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.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 41 | bitmap.setAlphaType(kOpaque_SkAlphaType); |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 42 | 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 | } |