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 | |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 8 | #include "SkCanvas.h" |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 9 | #include "SkImagePriv.h" |
| 10 | #include "Test.h" |
| 11 | |
| 12 | static const int gWidth = 20; |
| 13 | static const int gHeight = 20; |
| 14 | |
| 15 | // Tests that SkNewImageFromBitmap obeys pixelref origin. |
| 16 | DEF_TEST(SkImageFromBitmap_extractSubset, reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 17 | sk_sp<SkImage> image; |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 18 | { |
| 19 | SkBitmap srcBitmap; |
| 20 | srcBitmap.allocN32Pixels(gWidth, gHeight); |
| 21 | srcBitmap.eraseColor(SK_ColorRED); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 22 | SkCanvas canvas(srcBitmap); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 23 | SkIRect r = SkIRect::MakeXYWH(5, 5, gWidth - 5, gWidth - 5); |
| 24 | SkPaint p; |
| 25 | p.setColor(SK_ColorGREEN); |
| 26 | canvas.drawIRect(r, p); |
| 27 | SkBitmap dstBitmap; |
| 28 | srcBitmap.extractSubset(&dstBitmap, r); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 29 | image = SkImage::MakeFromBitmap(dstBitmap); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | SkBitmap tgt; |
| 33 | tgt.allocN32Pixels(gWidth, gHeight); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 34 | SkCanvas canvas(tgt); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 35 | canvas.clear(SK_ColorTRANSPARENT); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | canvas.drawImage(image, 0, 0, nullptr); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 37 | |
| 38 | uint32_t pixel = 0; |
kkinnunen | a9d9a39 | 2015-03-06 07:16:00 -0800 | [diff] [blame] | 39 | SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 40 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 41 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 42 | canvas.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6); |
| 43 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 44 | |
| 45 | canvas.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5); |
| 46 | REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT); |
| 47 | } |