blob: e5cf168dd49205cc72c64c055e5ce9d2799a7f7d [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
Mike Reedac9f0c92020-12-23 10:11:33 -05008#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
10#include "src/core/SkImagePriv.h"
11#include "tests/Test.h"
kkinnunen73953e72015-02-23 22:12:12 -080012
13static const int gWidth = 20;
14static const int gHeight = 20;
15
16// Tests that SkNewImageFromBitmap obeys pixelref origin.
17DEF_TEST(SkImageFromBitmap_extractSubset, reporter) {
reed9ce9d672016-03-17 10:51:11 -070018 sk_sp<SkImage> image;
kkinnunen73953e72015-02-23 22:12:12 -080019 {
20 SkBitmap srcBitmap;
21 srcBitmap.allocN32Pixels(gWidth, gHeight);
22 srcBitmap.eraseColor(SK_ColorRED);
robertphillips9a53fd72015-06-22 09:46:59 -070023 SkCanvas canvas(srcBitmap);
kkinnunen73953e72015-02-23 22:12:12 -080024 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 Reeddc607e32020-12-23 11:50:36 -050030 image = dstBitmap.asImage();
kkinnunen73953e72015-02-23 22:12:12 -080031 }
32
33 SkBitmap tgt;
34 tgt.allocN32Pixels(gWidth, gHeight);
robertphillips9a53fd72015-06-22 09:46:59 -070035 SkCanvas canvas(tgt);
kkinnunen73953e72015-02-23 22:12:12 -080036 canvas.clear(SK_ColorTRANSPARENT);
Mike Reed039f1362021-01-27 21:21:08 -050037 canvas.drawImage(image, 0, 0);
kkinnunen73953e72015-02-23 22:12:12 -080038
39 uint32_t pixel = 0;
kkinnunena9d9a392015-03-06 07:16:00 -080040 SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType);
Mike Reedf1942192017-07-21 14:24:29 -040041 tgt.readPixels(info, &pixel, 4, 0, 0);
kkinnunen73953e72015-02-23 22:12:12 -080042 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
Mike Reedf1942192017-07-21 14:24:29 -040043 tgt.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6);
kkinnunen73953e72015-02-23 22:12:12 -080044 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
45
Mike Reedf1942192017-07-21 14:24:29 -040046 tgt.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5);
kkinnunen73953e72015-02-23 22:12:12 -080047 REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT);
48}