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" |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 14 | |
| 15 | static SkBitmap::Config configs[] = { |
tfarina@chromium.org | 452eecb | 2014-01-06 12:43:53 +0000 | [diff] [blame] | 16 | SkBitmap::kRGB_565_Config, |
| 17 | SkBitmap::kARGB_8888_Config, |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 18 | }; |
| 19 | |
tfarina@chromium.org | 452eecb | 2014-01-06 12:43:53 +0000 | [diff] [blame] | 20 | DEF_TEST(ARGBImageEncoder, reporter) { |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 21 | // Bytes we expect to get: |
| 22 | const int kWidth = 3; |
| 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 |
| 31 | }; |
| 32 | |
| 33 | SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); |
| 34 | for (size_t configIndex = 0; configIndex < SK_ARRAY_COUNT(configs); ++configIndex) { |
| 35 | // A bitmap that should generate the above bytes: |
| 36 | SkBitmap bitmap; |
| 37 | { |
| 38 | bitmap.setConfig(configs[configIndex], kWidth, kHeight); |
| 39 | REPORTER_ASSERT(reporter, bitmap.allocPixels()); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 40 | bitmap.setAlphaType(kOpaque_SkAlphaType); |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 41 | 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; |
| 53 | SkAutoMalloc pixelBufferManager(bufferSize); |
| 54 | char *pixelBuffer = static_cast<char *>(pixelBufferManager.get()); |
| 55 | 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 | } |