blob: 4a05b44be1033ab974e65e38d84948a866d60c6f [file] [log] [blame]
kkinnunen73953e72015-02-23 22:12:12 -08001/*
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
robertphillips9a53fd72015-06-22 09:46:59 -07008#include "SkCanvas.h"
kkinnunen73953e72015-02-23 22:12:12 -08009#include "SkImagePriv.h"
10#include "Test.h"
11
12static const int gWidth = 20;
13static const int gHeight = 20;
14
15// Tests that SkNewImageFromBitmap obeys pixelref origin.
16DEF_TEST(SkImageFromBitmap_extractSubset, reporter) {
reed9ce9d672016-03-17 10:51:11 -070017 sk_sp<SkImage> image;
kkinnunen73953e72015-02-23 22:12:12 -080018 {
19 SkBitmap srcBitmap;
20 srcBitmap.allocN32Pixels(gWidth, gHeight);
21 srcBitmap.eraseColor(SK_ColorRED);
robertphillips9a53fd72015-06-22 09:46:59 -070022 SkCanvas canvas(srcBitmap);
kkinnunen73953e72015-02-23 22:12:12 -080023 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);
reed9ce9d672016-03-17 10:51:11 -070029 image = SkImage::MakeFromBitmap(dstBitmap);
kkinnunen73953e72015-02-23 22:12:12 -080030 }
31
32 SkBitmap tgt;
33 tgt.allocN32Pixels(gWidth, gHeight);
robertphillips9a53fd72015-06-22 09:46:59 -070034 SkCanvas canvas(tgt);
kkinnunen73953e72015-02-23 22:12:12 -080035 canvas.clear(SK_ColorTRANSPARENT);
halcanary96fcdcc2015-08-27 07:41:13 -070036 canvas.drawImage(image, 0, 0, nullptr);
kkinnunen73953e72015-02-23 22:12:12 -080037
38 uint32_t pixel = 0;
kkinnunena9d9a392015-03-06 07:16:00 -080039 SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType);
kkinnunen73953e72015-02-23 22:12:12 -080040 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}