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 "SkBitmapRegionCanvas.h" |
msarett | 84a8065 | 2015-11-10 15:49:46 -0800 | [diff] [blame] | 9 | #include "SkBitmapRegionDecoderPriv.h" |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 10 | #include "SkCanvas.h" |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 11 | #include "SkCodecPriv.h" |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 12 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 13 | SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder) |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 14 | : INHERITED(decoder->getInfo().width(), decoder->getInfo().height()) |
| 15 | , fDecoder(decoder) |
| 16 | {} |
| 17 | |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 18 | bool SkBitmapRegionCanvas::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator, |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 19 | const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType, |
| 20 | bool requireUnpremul) { |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 21 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 22 | // Reject color types not supported by this method |
| 23 | if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorType) { |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 24 | SkCodecPrintf("Error: Color type not supported.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 25 | return false; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 26 | } |
| 27 | |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 28 | // Reject requests for unpremultiplied alpha |
| 29 | if (requireUnpremul) { |
| 30 | SkCodecPrintf("Error: Alpha type not supported.\n"); |
| 31 | return false; |
| 32 | } |
| 33 | SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType(); |
| 34 | if (kUnpremul_SkAlphaType == dstAlphaType) { |
| 35 | dstAlphaType = kPremul_SkAlphaType; |
| 36 | } |
| 37 | |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 38 | // Fix the input sampleSize if necessary. |
| 39 | if (sampleSize < 1) { |
| 40 | sampleSize = 1; |
| 41 | } |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 42 | |
| 43 | // The size of the output bitmap is determined by the size of the |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 44 | // requested subset, not by the size of the intersection of the subset |
| 45 | // and the image dimensions. |
| 46 | // If inputX is negative, we will need to place decoded pixels into the |
| 47 | // output bitmap starting at a left offset. Call this outX. |
| 48 | // If outX is non-zero, subsetX must be zero. |
| 49 | // If inputY is negative, we will need to place decoded pixels into the |
| 50 | // output bitmap starting at a top offset. Call this outY. |
| 51 | // If outY is non-zero, subsetY must be zero. |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 52 | int outX; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 53 | int outY; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 54 | SkIRect subset = desiredSubset; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 55 | SubsetType type = adjust_subset_rect(fDecoder->getInfo().dimensions(), &subset, &outX, &outY); |
| 56 | if (SubsetType::kOutside_SubsetType == type) { |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 57 | return false; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Create the image info for the decode |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 61 | SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), |
| 62 | dstColorType, dstAlphaType); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 63 | |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 64 | // Start the scanline decoder |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 65 | SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 66 | if (SkCodec::kSuccess != r) { |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 67 | SkCodecPrintf("Error: Could not start scanline decoder.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 68 | return false; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Allocate a bitmap for the unscaled decode |
| 72 | SkBitmap tmp; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 73 | SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), subset.height()); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 74 | if (!tmp.tryAllocPixels(tmpInfo)) { |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 75 | SkCodecPrintf("Error: Could not allocate pixels.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 76 | return false; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Skip the unneeded rows |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 80 | if (!fDecoder->skipScanlines(subset.y())) { |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 81 | SkCodecPrintf("Error: Failed to skip scanlines.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 82 | return false; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Decode the necessary rows |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 86 | fDecoder->getScanlines(tmp.getAddr(0, 0), subset.height(), tmp.rowBytes()); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 87 | |
| 88 | // Calculate the size of the output |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 89 | const int outWidth = get_scaled_dimension(desiredSubset.width(), sampleSize); |
| 90 | const int outHeight = get_scaled_dimension(desiredSubset.height(), sampleSize); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 91 | |
| 92 | // Initialize the destination bitmap |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 93 | SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 94 | bitmap->setInfo(dstInfo, dstInfo.minRowBytes()); |
| 95 | if (!bitmap->tryAllocPixels(allocator, nullptr)) { |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 96 | SkCodecPrintf("Error: Could not allocate pixels.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 97 | return false; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Zero the bitmap if the region is not completely within the image. |
| 101 | // TODO (msarett): Can we make this faster by implementing it to only |
| 102 | // zero parts of the image that we won't overwrite with |
| 103 | // pixels? |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 104 | if (SubsetType::kPartiallyInside_SubsetType == type) { |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 105 | SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() : |
| 106 | SkCodec::kNo_ZeroInitialized; |
| 107 | if (SkCodec::kNo_ZeroInitialized == zeroInit) { |
| 108 | bitmap->eraseColor(0); |
| 109 | } |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Use a canvas to crop and scale to the destination bitmap |
| 113 | SkCanvas canvas(*bitmap); |
| 114 | // TODO (msarett): Maybe we can take advantage of the fact that SkRect uses floats? |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 115 | SkRect src = SkRect::MakeXYWH((SkScalar) subset.x(), (SkScalar) 0, |
| 116 | (SkScalar) subset.width(), (SkScalar) subset.height()); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 117 | SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (outY / sampleSize), |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 118 | (SkScalar) get_scaled_dimension(subset.width(), sampleSize), |
| 119 | (SkScalar) get_scaled_dimension(subset.height(), sampleSize)); |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 120 | SkPaint paint; |
| 121 | // Overwrite the dst with the src pixels |
| 122 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 123 | // TODO (msarett): Test multiple filter qualities. kNone is the default. |
| 124 | canvas.drawBitmapRect(tmp, src, dst, &paint); |
| 125 | |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 126 | return true; |
msarett | a5783ae | 2015-09-08 15:35:32 -0700 | [diff] [blame] | 127 | } |
msarett | 04965c6 | 2015-10-12 10:24:38 -0700 | [diff] [blame] | 128 | |
| 129 | bool SkBitmapRegionCanvas::conversionSupported(SkColorType colorType) { |
| 130 | // SkCanvas does not draw to these color types. |
| 131 | if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | // FIXME: Call virtual function when it lands. |
| 136 | SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fDecoder->getInfo().alphaType(), |
| 137 | fDecoder->getInfo().profileType()); |
| 138 | return conversion_possible(info, fDecoder->getInfo()); |
| 139 | } |