1 /*
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 "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "src/core/SkImagePriv.h"
11 #include "tests/Test.h"
12
13 static const int gWidth = 20;
14 static const int gHeight = 20;
15
16 // Tests that SkNewImageFromBitmap obeys pixelref origin.
DEF_TEST(SkImageFromBitmap_extractSubset,reporter)17 DEF_TEST(SkImageFromBitmap_extractSubset, reporter) {
18 sk_sp<SkImage> image;
19 {
20 SkBitmap srcBitmap;
21 srcBitmap.allocN32Pixels(gWidth, gHeight);
22 srcBitmap.eraseColor(SK_ColorRED);
23 SkCanvas canvas(srcBitmap);
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 = dstBitmap.asImage();
31 }
32
33 SkBitmap tgt;
34 tgt.allocN32Pixels(gWidth, gHeight);
35 SkCanvas canvas(tgt);
36 canvas.clear(SK_ColorTRANSPARENT);
37 canvas.drawImage(image, 0, 0);
38
39 uint32_t pixel = 0;
40 SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType);
41 tgt.readPixels(info, &pixel, 4, 0, 0);
42 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
43 tgt.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6);
44 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
45
46 tgt.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5);
47 REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT);
48 }
49