kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 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 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "src/core/SkImagePriv.h" |
| 11 | #include "tests/Test.h" |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 12 | |
| 13 | static const int gWidth = 20; |
| 14 | static const int gHeight = 20; |
| 15 | |
| 16 | // Tests that SkNewImageFromBitmap obeys pixelref origin. |
| 17 | DEF_TEST(SkImageFromBitmap_extractSubset, reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 18 | sk_sp<SkImage> image; |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 19 | { |
| 20 | SkBitmap srcBitmap; |
| 21 | srcBitmap.allocN32Pixels(gWidth, gHeight); |
| 22 | srcBitmap.eraseColor(SK_ColorRED); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 23 | SkCanvas canvas(srcBitmap); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 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); |
Mike Reed | dc607e3 | 2020-12-23 11:50:36 -0500 | [diff] [blame] | 30 | image = dstBitmap.asImage(); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | SkBitmap tgt; |
| 34 | tgt.allocN32Pixels(gWidth, gHeight); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 35 | SkCanvas canvas(tgt); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 36 | canvas.clear(SK_ColorTRANSPARENT); |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 37 | canvas.drawImage(image, 0, 0); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 38 | |
| 39 | uint32_t pixel = 0; |
kkinnunen | a9d9a39 | 2015-03-06 07:16:00 -0800 | [diff] [blame] | 40 | SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 41 | tgt.readPixels(info, &pixel, 4, 0, 0); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 42 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 43 | tgt.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 45 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 46 | tgt.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT); |
| 48 | } |