blob: 514af108593ffa6113233fe9cf807e376d8169fe [file] [log] [blame]
msaretta5783ae2015-09-08 15:35:32 -07001/*
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 "SkBitmapRegionSampler.h"
msarett04965c62015-10-12 10:24:38 -07009#include "SkCodecPriv.h"
msaretta5783ae2015-09-08 15:35:32 -070010
11SkBitmapRegionSampler::SkBitmapRegionSampler(SkImageDecoder* decoder, int width,
12 int height)
13 : INHERITED(width, height)
14 , fDecoder(decoder)
15{}
16
17/*
18 * Three differences from the Android version:
19 * Returns a Skia bitmap instead of an Android bitmap.
20 * Android version attempts to reuse a recycled bitmap.
21 * Removed the options object and used parameters for color type and
22 * sample size.
23 */
24SkBitmap* SkBitmapRegionSampler::decodeRegion(int start_x, int start_y,
25 int width, int height,
26 int sampleSize,
27 SkColorType prefColorType) {
28 // Match Android's default settings
29 fDecoder->setDitherImage(true);
30 fDecoder->setPreferQualityOverSpeed(false);
31 fDecoder->setRequireUnpremultipliedColors(false);
32 fDecoder->setSampleSize(sampleSize);
33
34 // kAlpha8 is the legacy representation of kGray8 used by SkImageDecoder
35 if (kGray_8_SkColorType == prefColorType) {
36 prefColorType = kAlpha_8_SkColorType;
37 }
38
39 SkIRect region;
40 region.fLeft = start_x;
41 region.fTop = start_y;
42 region.fRight = start_x + width;
43 region.fBottom = start_y + height;
44
45 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap());
46 if (!fDecoder->decodeSubset(bitmap.get(), region, prefColorType)) {
msarett04965c62015-10-12 10:24:38 -070047 SkCodecPrintf("Error: decodeRegion failed.\n");
msaretta5783ae2015-09-08 15:35:32 -070048 return nullptr;
49 }
50 return bitmap.detach();
51}