xref: /aosp_15_r20/external/skia/tests/SkImageTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
20 #include <cstdint>
21 #include <map>
22 
23 static const int gWidth = 20;
24 static const int gHeight = 20;
25 
26 // Tests that SkNewImageFromBitmap obeys pixelref origin.
DEF_TEST(SkImageFromBitmap_extractSubset,reporter)27 DEF_TEST(SkImageFromBitmap_extractSubset, reporter) {
28     sk_sp<SkImage> image;
29     {
30         SkBitmap srcBitmap;
31         srcBitmap.allocN32Pixels(gWidth, gHeight);
32         srcBitmap.eraseColor(SK_ColorRED);
33         SkCanvas canvas(srcBitmap);
34         SkIRect r = SkIRect::MakeXYWH(5, 5, gWidth - 5, gWidth - 5);
35         SkPaint p;
36         p.setColor(SK_ColorGREEN);
37         canvas.drawIRect(r, p);
38         SkBitmap dstBitmap;
39         srcBitmap.extractSubset(&dstBitmap, r);
40         image = dstBitmap.asImage();
41     }
42 
43     SkBitmap tgt;
44     tgt.allocN32Pixels(gWidth, gHeight);
45     SkCanvas canvas(tgt);
46     canvas.clear(SK_ColorTRANSPARENT);
47     canvas.drawImage(image, 0, 0);
48 
49     uint32_t pixel = 0;
50     SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType);
51     tgt.readPixels(info, &pixel, 4, 0, 0);
52     REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
53     tgt.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6);
54     REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
55 
56     tgt.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5);
57     REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT);
58 }
59 
60 // This makes sure we can use RequiredProperties as a key in a map, which some
61 // clients depend on.
DEF_TEST(SkImageRequiredPropertiesCanBeMapKey,r)62 DEF_TEST(SkImageRequiredPropertiesCanBeMapKey, r) {
63     std::map<SkImage::RequiredProperties, int> test;
64     SkImage::RequiredProperties rp;
65     test.emplace(rp, 7);
66 }
67