scroggo@google.com | 9f686f3 | 2012-11-29 21:05:37 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkBitmapFactory.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkColor.h" |
| 13 | #include "SkData.h" |
| 14 | #include "SkImageEncoder.h" |
| 15 | #include "SkPaint.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkTemplates.h" |
| 18 | #include "Test.h" |
| 19 | |
| 20 | static SkBitmap* create_bitmap() { |
| 21 | SkBitmap* bm = SkNEW(SkBitmap); |
| 22 | const int W = 100, H = 100; |
| 23 | bm->setConfig(SkBitmap::kARGB_8888_Config, W, H); |
| 24 | bm->allocPixels(); |
| 25 | bm->eraseColor(SK_ColorBLACK); |
| 26 | SkCanvas canvas(*bm); |
| 27 | SkPaint paint; |
| 28 | paint.setColor(SK_ColorBLUE); |
| 29 | canvas.drawRectCoords(0, 0, SkIntToScalar(W/2), SkIntToScalar(H/2), paint); |
| 30 | return bm; |
| 31 | } |
| 32 | |
| 33 | static SkData* create_data_from_bitmap(const SkBitmap& bm) { |
| 34 | SkDynamicMemoryWStream stream; |
| 35 | if (SkImageEncoder::EncodeStream(&stream, bm, SkImageEncoder::kPNG_Type, 100)) { |
| 36 | return stream.copyToData(); |
| 37 | } |
| 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | static void assert_bounds_equal(skiatest::Reporter* reporter, const SkBitmap& bm1, |
| 42 | const SkBitmap& bm2) { |
| 43 | REPORTER_ASSERT(reporter, bm1.width() == bm2.width()); |
| 44 | REPORTER_ASSERT(reporter, bm1.height() == bm2.height()); |
| 45 | } |
| 46 | |
| 47 | static void TestBitmapFactory(skiatest::Reporter* reporter) { |
| 48 | SkAutoTDelete<SkBitmap> bitmap(create_bitmap()); |
| 49 | SkASSERT(bitmap.get() != NULL); |
| 50 | |
| 51 | SkAutoDataUnref encodedBitmap(create_data_from_bitmap(*bitmap.get())); |
| 52 | if (encodedBitmap.get() == NULL) { |
| 53 | // Encoding failed. |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | SkBitmap bitmapFromFactory; |
| 58 | bool success = SkBitmapFactory::DecodeBitmap(&bitmapFromFactory, encodedBitmap); |
| 59 | // This assumes that if the encoder worked, the decoder should also work, so the above call |
| 60 | // should not fail. |
| 61 | REPORTER_ASSERT(reporter, success); |
| 62 | assert_bounds_equal(reporter, *bitmap.get(), bitmapFromFactory); |
| 63 | REPORTER_ASSERT(reporter, bitmapFromFactory.pixelRef() != NULL); |
| 64 | |
| 65 | // When only requesting that the bounds be decoded, the bounds should be set properly while |
| 66 | // the pixels should be empty. |
| 67 | SkBitmap boundedBitmap; |
| 68 | success = SkBitmapFactory::DecodeBitmap(&boundedBitmap, encodedBitmap, |
| 69 | SkBitmapFactory::kDecodeBoundsOnly_Constraint); |
| 70 | REPORTER_ASSERT(reporter, success); |
| 71 | assert_bounds_equal(reporter, *bitmap.get(), boundedBitmap); |
| 72 | REPORTER_ASSERT(reporter, boundedBitmap.pixelRef() == NULL); |
| 73 | } |
| 74 | |
| 75 | #include "TestClassDef.h" |
| 76 | DEFINE_TESTCLASS("BitmapFactory", TestBitmapFactoryClass, TestBitmapFactory) |