Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapRegionCanvas.h" | 8 #include "SkBitmapRegionCanvas.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkCodecPriv.h" | 10 #include "SkCodecPriv.h" |
| 11 #include "SkCodecTools.h" | 11 #include "SkCodecTools.h" |
| 12 | 12 |
| 13 SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder) | 13 SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder) |
| 14 : INHERITED(decoder->getInfo().width(), decoder->getInfo().height()) | 14 : INHERITED(decoder->getInfo().width(), decoder->getInfo().height()) |
| 15 , fDecoder(decoder) | 15 , fDecoder(decoder) |
| 16 {} | 16 {} |
| 17 | 17 |
| 18 /* | 18 /* |
| 19 * Chooses the correct image subset offsets and dimensions for the partial decod e. | |
|
msarett
2015/10/12 18:42:36
Moved to SkCodecTools.h
The only change is that t
| |
| 20 * | |
| 21 * @return true if the subset is completely contained within the image | |
| 22 * false otherwise | |
| 23 */ | |
| 24 static bool set_subset_region(int inputOffset, int inputDimension, | |
| 25 int imageOriginalDimension, int* imageSubsetOffset, int* outOffset, | |
| 26 int* imageSubsetDimension) { | |
| 27 | |
| 28 // This must be at least zero, we can't start decoding the image at a negati ve coordinate. | |
| 29 *imageSubsetOffset = SkTMax(0, inputOffset); | |
| 30 | |
| 31 // If inputOffset is less than zero, we decode to an offset location in the output bitmap. | |
| 32 *outOffset = *imageSubsetOffset - inputOffset; | |
| 33 | |
| 34 // Use imageSusetOffset to make sure we don't decode pixels past the edge of the image. | |
| 35 // Use outOffset to make sure we don't decode pixels past the edge of the re gion. | |
| 36 *imageSubsetDimension = SkTMin(imageOriginalDimension - *imageSubsetOffset, | |
| 37 inputDimension - *outOffset); | |
| 38 | |
| 39 return (*outOffset == 0) && (*imageSubsetDimension == inputDimension); | |
| 40 } | |
| 41 | |
| 42 /* | |
| 43 * Three differences from the Android version: | 19 * Three differences from the Android version: |
| 44 * Returns a Skia bitmap instead of an Android bitmap. | 20 * Returns a Skia bitmap instead of an Android bitmap. |
| 45 * Android version attempts to reuse a recycled bitmap. | 21 * Android version attempts to reuse a recycled bitmap. |
| 46 * Removed the options object and used parameters for color type and | 22 * Removed the options object and used parameters for color type and |
| 47 * sample size. | 23 * sample size. |
| 48 */ | 24 */ |
| 49 SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, | 25 SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
| 50 int inputWidth, int inputHeight, | 26 int inputWidth, int inputHeight, |
| 51 int sampleSize, | 27 int sampleSize, |
| 52 SkColorType dstColorType) { | 28 SkColorType dstColorType) { |
| 53 // Reject color types not supported by this method | 29 // Reject color types not supported by this method |
| 54 if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorT ype) { | 30 if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorT ype) { |
| 55 SkCodecPrintf("Error: Color type not supported.\n"); | 31 SkCodecPrintf("Error: Color type not supported.\n"); |
| 56 return nullptr; | 32 return nullptr; |
| 57 } | 33 } |
| 58 | 34 |
| 35 // Fix the input sampleSize if necessary. | |
|
msarett
2015/10/12 18:42:36
Android does not set a default sampleSize. We sho
| |
| 36 if (sampleSize < 1) { | |
| 37 sampleSize = 1; | |
| 38 } | |
| 39 | |
| 59 // The client may not necessarily request a region that is fully within | 40 // The client may not necessarily request a region that is fully within |
| 60 // the image. We may need to do some calculation to determine what part | 41 // the image. We may need to do some calculation to determine what part |
| 61 // of the image to decode. | 42 // of the image to decode. |
| 62 | 43 |
| 63 // The left offset of the portion of the image we want, where zero | 44 // The left offset of the portion of the image we want, where zero |
| 64 // indicates the left edge of the image. | 45 // indicates the left edge of the image. |
| 65 int imageSubsetX; | 46 int imageSubsetX; |
| 66 | 47 |
| 67 // The size of the output bitmap is determined by the size of the | 48 // The size of the output bitmap is determined by the size of the |
| 68 // requested region, not by the size of the intersection of the region | 49 // requested region, not by the size of the intersection of the region |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 return nullptr; | 82 return nullptr; |
| 102 } | 83 } |
| 103 | 84 |
| 104 // Create the image info for the decode | 85 // Create the image info for the decode |
| 105 SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType(); | 86 SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType(); |
| 106 if (kUnpremul_SkAlphaType == dstAlphaType) { | 87 if (kUnpremul_SkAlphaType == dstAlphaType) { |
| 107 dstAlphaType = kPremul_SkAlphaType; | 88 dstAlphaType = kPremul_SkAlphaType; |
| 108 } | 89 } |
| 109 SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), | 90 SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), |
| 110 dstColorType, dstAlphaType); | 91 dstColorType, dstAlphaType); |
| 111 | 92 |
| 112 // Start the scanline decoder | 93 // Start the scanline decoder |
| 113 SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo); | 94 SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo); |
| 114 if (SkCodec::kSuccess != r) { | 95 if (SkCodec::kSuccess != r) { |
| 115 SkCodecPrintf("Error: Could not start scanline decoder.\n"); | 96 SkCodecPrintf("Error: Could not start scanline decoder.\n"); |
| 116 return nullptr; | 97 return nullptr; |
| 117 } | 98 } |
| 118 | 99 |
| 119 // Allocate a bitmap for the unscaled decode | 100 // Allocate a bitmap for the unscaled decode |
| 120 SkBitmap tmp; | 101 SkBitmap tmp; |
| 121 SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), imageSubsetHeight); | 102 SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), imageSubsetHeight); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 145 return nullptr; | 126 return nullptr; |
| 146 } | 127 } |
| 147 | 128 |
| 148 // Zero the bitmap if the region is not completely within the image. | 129 // Zero the bitmap if the region is not completely within the image. |
| 149 // TODO (msarett): Can we make this faster by implementing it to only | 130 // TODO (msarett): Can we make this faster by implementing it to only |
| 150 // zero parts of the image that we won't overwrite with | 131 // zero parts of the image that we won't overwrite with |
| 151 // pixels? | 132 // pixels? |
| 152 // TODO (msarett): This could be skipped if memory is zero initialized. | 133 // TODO (msarett): This could be skipped if memory is zero initialized. |
| 153 // This would matter if this code is moved to Android and | 134 // This would matter if this code is moved to Android and |
| 154 // uses Android bitmaps. | 135 // uses Android bitmaps. |
| 155 if (imageContainsEntireSubset) { | 136 if (!imageContainsEntireSubset) { |
|
msarett
2015/10/12 18:42:36
Oops.
| |
| 156 bitmap->eraseColor(0); | 137 bitmap->eraseColor(0); |
| 157 } | 138 } |
| 158 | 139 |
| 159 // Use a canvas to crop and scale to the destination bitmap | 140 // Use a canvas to crop and scale to the destination bitmap |
| 160 SkCanvas canvas(*bitmap); | 141 SkCanvas canvas(*bitmap); |
| 161 // TODO (msarett): Maybe we can take advantage of the fact that SkRect uses floats? | 142 // TODO (msarett): Maybe we can take advantage of the fact that SkRect uses floats? |
| 162 SkRect src = SkRect::MakeXYWH((SkScalar) imageSubsetX, (SkScalar) 0, | 143 SkRect src = SkRect::MakeXYWH((SkScalar) imageSubsetX, (SkScalar) 0, |
| 163 (SkScalar) imageSubsetWidth, (SkScalar) imageSubsetHeight); | 144 (SkScalar) imageSubsetWidth, (SkScalar) imageSubsetHeight); |
| 164 SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (ou tY / sampleSize), | 145 SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (ou tY / sampleSize), |
| 165 (SkScalar) get_scaled_dimension(imageSubsetWidth, sampleSize), | 146 (SkScalar) get_scaled_dimension(imageSubsetWidth, sampleSize), |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 177 // SkCanvas does not draw to these color types. | 158 // SkCanvas does not draw to these color types. |
| 178 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) { | 159 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) { |
| 179 return false; | 160 return false; |
| 180 } | 161 } |
| 181 | 162 |
| 182 // FIXME: Call virtual function when it lands. | 163 // FIXME: Call virtual function when it lands. |
| 183 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fDecoder->getInfo().al phaType(), | 164 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fDecoder->getInfo().al phaType(), |
| 184 fDecoder->getInfo().profileType()); | 165 fDecoder->getInfo().profileType()); |
| 185 return conversion_possible(info, fDecoder->getInfo()); | 166 return conversion_possible(info, fDecoder->getInfo()); |
| 186 } | 167 } |
| OLD | NEW |