blob: 3ba63b425891bcb237b8c0eb37375907ac1c75f9 [file] [log] [blame]
kkinnunen73953e72015-02-23 22:12:12 -08001/*
2 * Copyright 2015 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
8#include "SkBitmapDevice.h"
9#include "SkImagePriv.h"
10#include "Test.h"
11
12static const int gWidth = 20;
13static const int gHeight = 20;
14
15// Tests that SkNewImageFromBitmap obeys pixelref origin.
16DEF_TEST(SkImageFromBitmap_extractSubset, reporter) {
17 SkAutoTUnref<SkImage> image;
18 {
19 SkBitmap srcBitmap;
20 srcBitmap.allocN32Pixels(gWidth, gHeight);
21 srcBitmap.eraseColor(SK_ColorRED);
22 SkBitmapDevice dev(srcBitmap);
23 SkCanvas canvas(&dev);
24 SkIRect r = SkIRect::MakeXYWH(5, 5, gWidth - 5, gWidth - 5);
25 SkPaint p;
26 p.setColor(SK_ColorGREEN);
27 canvas.drawIRect(r, p);
28 SkBitmap dstBitmap;
29 srcBitmap.extractSubset(&dstBitmap, r);
30 image.reset(SkNewImageFromBitmap(dstBitmap, true, NULL));
31 }
32
33 SkBitmap tgt;
34 tgt.allocN32Pixels(gWidth, gHeight);
35 SkBitmapDevice dev(tgt);
36 SkCanvas canvas(&dev);
37 canvas.clear(SK_ColorTRANSPARENT);
38 canvas.drawImage(image, 0, 0, NULL);
39
40 uint32_t pixel = 0;
kkinnunena9d9a392015-03-06 07:16:00 -080041 SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType);
kkinnunen73953e72015-02-23 22:12:12 -080042 canvas.readPixels(info, &pixel, 4, 0, 0);
43 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
44 canvas.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6);
45 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
46
47 canvas.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5);
48 REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT);
49}