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/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "tests/Test.h"
19 #include <cstdint>
20 
21 static const int gWidth = 20;
22 static const int gHeight = 20;
23 
24 // Tests that SkNewImageFromBitmap obeys pixelref origin.
DEF_TEST(SkImageFromBitmap_extractSubset,reporter)25 DEF_TEST(SkImageFromBitmap_extractSubset, reporter) {
26     sk_sp<SkImage> image;
27     {
28         SkBitmap srcBitmap;
29         srcBitmap.allocN32Pixels(gWidth, gHeight);
30         srcBitmap.eraseColor(SK_ColorRED);
31         SkCanvas canvas(srcBitmap);
32         SkIRect r = SkIRect::MakeXYWH(5, 5, gWidth - 5, gWidth - 5);
33         SkPaint p;
34         p.setColor(SK_ColorGREEN);
35         canvas.drawIRect(r, p);
36         SkBitmap dstBitmap;
37         srcBitmap.extractSubset(&dstBitmap, r);
38         image = dstBitmap.asImage();
39     }
40 
41     SkBitmap tgt;
42     tgt.allocN32Pixels(gWidth, gHeight);
43     SkCanvas canvas(tgt);
44     canvas.clear(SK_ColorTRANSPARENT);
45     canvas.drawImage(image, 0, 0);
46 
47     uint32_t pixel = 0;
48     SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType);
49     tgt.readPixels(info, &pixel, 4, 0, 0);
50     REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
51     tgt.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6);
52     REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
53 
54     tgt.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5);
55     REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT);
56 }
57