aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-11-29 21:05:37 +0000
committerGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-11-29 21:05:37 +0000
commit9f686f3639ff87e6d28b4ffcc42feebeca90f8d8 (patch)
treea68c3806894afa297b0fee3e63367b407ba3202d /tests
parent8a98e3bd18f1a8914cbfe1461e1ff47f51286556 (diff)
Create a factory to decode an SkBitmap from an SkData.
Add a test and a GM for the factory, and a PNG file for it to decode. The PNG file is copyright-free, obtained from http://openclipart.org/detail/29213/paper-plane-by-ddoo In cmykjpeg, do not attempt to decode in the constructor, since it currently crashes on Mac (if you provide the correct resource path). Even when we fix this crash there is no need to do it in the constructor, since we create all of the gms in order to get their names (to determine whether to run them). Review URL: https://codereview.appspot.com/6847122 git-svn-id: http://skia.googlecode.com/svn/trunk@6618 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tests')
-rw-r--r--tests/BitmapFactoryTest.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/BitmapFactoryTest.cpp b/tests/BitmapFactoryTest.cpp
new file mode 100644
index 0000000000..b24fd258ca
--- /dev/null
+++ b/tests/BitmapFactoryTest.cpp
@@ -0,0 +1,76 @@
+
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkBitmap.h"
+#include "SkBitmapFactory.h"
+#include "SkCanvas.h"
+#include "SkColor.h"
+#include "SkData.h"
+#include "SkImageEncoder.h"
+#include "SkPaint.h"
+#include "SkStream.h"
+#include "SkTemplates.h"
+#include "Test.h"
+
+static SkBitmap* create_bitmap() {
+ SkBitmap* bm = SkNEW(SkBitmap);
+ const int W = 100, H = 100;
+ bm->setConfig(SkBitmap::kARGB_8888_Config, W, H);
+ bm->allocPixels();
+ bm->eraseColor(SK_ColorBLACK);
+ SkCanvas canvas(*bm);
+ SkPaint paint;
+ paint.setColor(SK_ColorBLUE);
+ canvas.drawRectCoords(0, 0, SkIntToScalar(W/2), SkIntToScalar(H/2), paint);
+ return bm;
+}
+
+static SkData* create_data_from_bitmap(const SkBitmap& bm) {
+ SkDynamicMemoryWStream stream;
+ if (SkImageEncoder::EncodeStream(&stream, bm, SkImageEncoder::kPNG_Type, 100)) {
+ return stream.copyToData();
+ }
+ return NULL;
+}
+
+static void assert_bounds_equal(skiatest::Reporter* reporter, const SkBitmap& bm1,
+ const SkBitmap& bm2) {
+ REPORTER_ASSERT(reporter, bm1.width() == bm2.width());
+ REPORTER_ASSERT(reporter, bm1.height() == bm2.height());
+}
+
+static void TestBitmapFactory(skiatest::Reporter* reporter) {
+ SkAutoTDelete<SkBitmap> bitmap(create_bitmap());
+ SkASSERT(bitmap.get() != NULL);
+
+ SkAutoDataUnref encodedBitmap(create_data_from_bitmap(*bitmap.get()));
+ if (encodedBitmap.get() == NULL) {
+ // Encoding failed.
+ return;
+ }
+
+ SkBitmap bitmapFromFactory;
+ bool success = SkBitmapFactory::DecodeBitmap(&bitmapFromFactory, encodedBitmap);
+ // This assumes that if the encoder worked, the decoder should also work, so the above call
+ // should not fail.
+ REPORTER_ASSERT(reporter, success);
+ assert_bounds_equal(reporter, *bitmap.get(), bitmapFromFactory);
+ REPORTER_ASSERT(reporter, bitmapFromFactory.pixelRef() != NULL);
+
+ // When only requesting that the bounds be decoded, the bounds should be set properly while
+ // the pixels should be empty.
+ SkBitmap boundedBitmap;
+ success = SkBitmapFactory::DecodeBitmap(&boundedBitmap, encodedBitmap,
+ SkBitmapFactory::kDecodeBoundsOnly_Constraint);
+ REPORTER_ASSERT(reporter, success);
+ assert_bounds_equal(reporter, *bitmap.get(), boundedBitmap);
+ REPORTER_ASSERT(reporter, boundedBitmap.pixelRef() == NULL);
+}
+
+#include "TestClassDef.h"
+DEFINE_TESTCLASS("BitmapFactory", TestBitmapFactoryClass, TestBitmapFactory)