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