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 "SkBitmapRegionDecoderPriv.h" | |
| 9 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkCodecPriv.h" | |
| 10 | 12 |
| 11 SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder) | 13 SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder) |
| 12 : INHERITED(decoder->getInfo().width(), decoder->getInfo().height()) | 14 : INHERITED(decoder->getInfo().width(), decoder->getInfo().height()) |
| 13 , fDecoder(decoder) | 15 , fDecoder(decoder) |
| 14 {} | 16 {} |
| 15 | 17 |
| 16 /* | 18 /* |
| 17 * Chooses the correct image subset offsets and dimensions for the partial decod e. | |
| 18 */ | |
| 19 static inline void set_subset_region(int inputOffset, int inputDimension, | |
| 20 int imageOriginalDimension, int* imageSubsetOffset, int* outOffset, | |
| 21 int* imageSubsetDimension) { | |
| 22 | |
| 23 // This must be at least zero, we can't start decoding the image at a negati ve coordinate. | |
| 24 *imageSubsetOffset = SkTMax(0, inputOffset); | |
| 25 | |
| 26 // If inputOffset is less than zero, we decode to an offset location in the output bitmap. | |
| 27 *outOffset = *imageSubsetOffset - inputOffset; | |
| 28 | |
| 29 // Use imageSusetOffset to make sure we don't decode pixels past the edge of the image. | |
| 30 // Use outOffset to make sure we don't decode pixels past the edge of the re gion. | |
| 31 *imageSubsetDimension = SkTMin(imageOriginalDimension - *imageSubsetOffset, | |
| 32 inputDimension - *outOffset); | |
| 33 } | |
| 34 | |
| 35 /* | |
| 36 * Returns a scaled dimension based on the original dimension and the sample siz e. | |
| 37 * TODO: Share this implementation with SkScaledCodec. | |
| 38 */ | |
| 39 static int get_scaled_dimension(int srcDimension, int sampleSize) { | |
| 40 if (sampleSize > srcDimension) { | |
| 41 return 1; | |
| 42 } | |
| 43 return srcDimension / sampleSize; | |
| 44 } | |
| 45 | |
| 46 /* | |
| 47 * Three differences from the Android version: | 19 * Three differences from the Android version: |
| 48 * Returns a Skia bitmap instead of an Android bitmap. | 20 * Returns a Skia bitmap instead of an Android bitmap. |
| 49 * Android version attempts to reuse a recycled bitmap. | 21 * Android version attempts to reuse a recycled bitmap. |
| 50 * Removed the options object and used parameters for color type and | 22 * Removed the options object and used parameters for color type and |
| 51 * sample size. | 23 * sample size. |
| 52 */ | 24 */ |
| 53 SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, | 25 SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
| 54 int inputWidth, int inputHeight, | 26 int inputWidth, int inputHeight, |
| 55 int sampleSize, | 27 int sampleSize, |
| 56 SkColorType dstColorType) { | 28 SkColorType dstColorType) { |
| 57 // Reject color types not supported by this method | 29 // Reject color types not supported by this method |
| 58 if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorT ype) { | 30 if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorT ype) { |
| 59 SkDebugf("Error: Color type not supported.\n"); | 31 SkCodecPrintf("Error: Color type not supported.\n"); |
| 60 return nullptr; | 32 return nullptr; |
| 61 } | 33 } |
| 62 | 34 |
| 35 // Fix the input sampleSize if necessary. | |
| 36 if (sampleSize < 1) { | |
|
scroggo
2015/10/02 18:27:03
Is this just in case? Or were you seeing bad input
msarett
2015/10/06 23:01:27
The Android code likes to call decodeRegion with s
scroggo
2015/10/07 17:49:39
Oh, weird, it looks like BitmapFactory.Options.inS
| |
| 37 sampleSize = 1; | |
| 38 } | |
| 39 | |
| 63 // 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 |
| 64 // 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 |
| 65 // of the image to decode. | 42 // of the image to decode. |
| 66 | 43 |
| 67 // 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 |
| 68 // indicates the left edge of the image. | 45 // indicates the left edge of the image. |
| 69 int imageSubsetX; | 46 int imageSubsetX; |
| 70 | 47 |
| 71 // 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 |
| 72 // 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 |
| 73 // and the image dimensions. If inputX is negative, we will need to | 50 // and the image dimensions. If inputX is negative, we will need to |
| 74 // place decoded pixels into the output bitmap starting at a left offset. | 51 // place decoded pixels into the output bitmap starting at a left offset. |
| 75 // If this is non-zero, imageSubsetX must be zero. | 52 // If this is non-zero, imageSubsetX must be zero. |
| 76 int outX; | 53 int outX; |
| 77 | 54 |
| 78 // The width of the portion of the image that we will write to the output | 55 // The width of the portion of the image that we will write to the output |
| 79 // bitmap. If the region is not fully contained within the image, this | 56 // bitmap. If the region is not fully contained within the image, this |
| 80 // will not be the same as inputWidth. | 57 // will not be the same as inputWidth. |
| 81 int imageSubsetWidth; | 58 int imageSubsetWidth; |
| 82 set_subset_region(inputX, inputWidth, this->width(), &imageSubsetX, &outX, & imageSubsetWidth); | 59 bool imageContainsEntireSubset = set_subset_region(inputX, inputWidth, this- >width(), |
| 60 &imageSubsetX, &outX, &imageSubsetWidth); | |
| 83 | 61 |
| 84 // The top offset of the portion of the image we want, where zero | 62 // The top offset of the portion of the image we want, where zero |
| 85 // indicates the top edge of the image. | 63 // indicates the top edge of the image. |
| 86 int imageSubsetY; | 64 int imageSubsetY; |
| 87 | 65 |
| 88 // The size of the output bitmap is determined by the size of the | 66 // The size of the output bitmap is determined by the size of the |
| 89 // requested region, not by the size of the intersection of the region | 67 // requested region, not by the size of the intersection of the region |
| 90 // and the image dimensions. If inputY is negative, we will need to | 68 // and the image dimensions. If inputY is negative, we will need to |
| 91 // place decoded pixels into the output bitmap starting at a top offset. | 69 // place decoded pixels into the output bitmap starting at a top offset. |
| 92 // If this is non-zero, imageSubsetY must be zero. | 70 // If this is non-zero, imageSubsetY must be zero. |
| 93 int outY; | 71 int outY; |
| 94 | 72 |
| 95 // The height of the portion of the image that we will write to the output | 73 // The height of the portion of the image that we will write to the output |
| 96 // bitmap. If the region is not fully contained within the image, this | 74 // bitmap. If the region is not fully contained within the image, this |
| 97 // will not be the same as inputHeight. | 75 // will not be the same as inputHeight. |
| 98 int imageSubsetHeight; | 76 int imageSubsetHeight; |
| 99 set_subset_region(inputY, inputHeight, this->height(), &imageSubsetY, &outY, | 77 imageContainsEntireSubset &= set_subset_region(inputY, inputHeight, this->he ight(), |
| 100 &imageSubsetHeight); | 78 &imageSubsetY, &outY, &imageSubsetHeight); |
| 101 | 79 |
| 102 if (imageSubsetWidth <= 0 || imageSubsetHeight <= 0) { | 80 if (imageSubsetWidth <= 0 || imageSubsetHeight <= 0) { |
| 103 SkDebugf("Error: Region must intersect part of the image.\n"); | 81 SkCodecPrintf("Error: Region must intersect part of the image.\n"); |
| 104 return nullptr; | 82 return nullptr; |
| 105 } | 83 } |
| 106 | 84 |
| 107 // Create the image info for the decode | 85 // Create the image info for the decode |
| 108 SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType(); | 86 SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType(); |
| 109 if (kUnpremul_SkAlphaType == dstAlphaType) { | 87 if (kUnpremul_SkAlphaType == dstAlphaType) { |
| 110 dstAlphaType = kPremul_SkAlphaType; | 88 dstAlphaType = kPremul_SkAlphaType; |
| 111 } | 89 } |
| 112 SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), | 90 SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), |
| 113 dstColorType, dstAlphaType); | 91 dstColorType, dstAlphaType); |
| 114 | 92 |
| 115 // Start the scanline decoder | 93 // Start the scanline decoder |
| 116 SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo); | 94 SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo); |
| 117 if (SkCodec::kSuccess != r) { | 95 if (SkCodec::kSuccess != r) { |
| 118 SkDebugf("Error: Could not start scanline decoder.\n"); | 96 SkCodecPrintf("Error: Could not start scanline decoder.\n"); |
| 119 return nullptr; | 97 return nullptr; |
| 120 } | 98 } |
| 121 | 99 |
| 122 // Allocate a bitmap for the unscaled decode | 100 // Allocate a bitmap for the unscaled decode |
| 123 SkBitmap tmp; | 101 SkBitmap tmp; |
| 124 SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), imageSubsetHeight); | 102 SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), imageSubsetHeight); |
| 125 if (!tmp.tryAllocPixels(tmpInfo)) { | 103 if (!tmp.tryAllocPixels(tmpInfo)) { |
| 126 SkDebugf("Error: Could not allocate pixels.\n"); | 104 SkCodecPrintf("Error: Could not allocate pixels.\n"); |
| 127 return nullptr; | 105 return nullptr; |
| 128 } | 106 } |
| 129 | 107 |
| 130 // Skip the unneeded rows | 108 // Skip the unneeded rows |
| 131 if (!fDecoder->skipScanlines(imageSubsetY)) { | 109 if (!fDecoder->skipScanlines(imageSubsetY)) { |
| 132 SkDebugf("Error: Failed to skip scanlines.\n"); | 110 SkCodecPrintf("Error: Failed to skip scanlines.\n"); |
| 133 return nullptr; | 111 return nullptr; |
| 134 } | 112 } |
| 135 | 113 |
| 136 // Decode the necessary rows | 114 // Decode the necessary rows |
| 137 fDecoder->getScanlines(tmp.getAddr(0, 0), imageSubsetHeight, tmp.rowBytes()) ; | 115 fDecoder->getScanlines(tmp.getAddr(0, 0), imageSubsetHeight, tmp.rowBytes()) ; |
| 138 | 116 |
| 139 // Calculate the size of the output | 117 // Calculate the size of the output |
| 140 const int outWidth = get_scaled_dimension(inputWidth, sampleSize); | 118 const int outWidth = get_scaled_dimension(inputWidth, sampleSize); |
| 141 const int outHeight = get_scaled_dimension(inputHeight, sampleSize); | 119 const int outHeight = get_scaled_dimension(inputHeight, sampleSize); |
| 142 | 120 |
| 143 // Initialize the destination bitmap | 121 // Initialize the destination bitmap |
| 144 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap()); | 122 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap()); |
| 145 SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight); | 123 SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight); |
| 146 if (!bitmap->tryAllocPixels(dstInfo)) { | 124 if (!bitmap->tryAllocPixels(dstInfo)) { |
| 147 SkDebugf("Error: Could not allocate pixels.\n"); | 125 SkCodecPrintf("Error: Could not allocate pixels.\n"); |
| 148 return nullptr; | 126 return nullptr; |
| 149 } | 127 } |
| 150 | 128 |
| 151 // 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. |
| 152 // 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 |
| 153 // zero parts of the image that we won't overwrite with | 131 // zero parts of the image that we won't overwrite with |
| 154 // pixels? | 132 // pixels? |
| 155 // TODO (msarett): This could be skipped if memory is zero initialized. | 133 // TODO (msarett): This could be skipped if memory is zero initialized. |
| 156 // This would matter if this code is moved to Android and | 134 // This would matter if this code is moved to Android and |
| 157 // uses Android bitmaps. | 135 // uses Android bitmaps. |
| 158 if (0 != outX || 0 != outY || | 136 if (!imageContainsEntireSubset) { |
| 159 inputX + inputWidth > this->width() || | |
| 160 inputY + inputHeight > this->height()) { | |
| 161 bitmap->eraseColor(0); | 137 bitmap->eraseColor(0); |
| 162 } | 138 } |
| 163 | 139 |
| 164 // Use a canvas to crop and scale to the destination bitmap | 140 // Use a canvas to crop and scale to the destination bitmap |
| 165 SkCanvas canvas(*bitmap); | 141 SkCanvas canvas(*bitmap); |
| 166 // 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? |
| 167 SkRect src = SkRect::MakeXYWH((SkScalar) imageSubsetX, (SkScalar) 0, | 143 SkRect src = SkRect::MakeXYWH((SkScalar) imageSubsetX, (SkScalar) 0, |
| 168 (SkScalar) imageSubsetWidth, (SkScalar) imageSubsetHeight); | 144 (SkScalar) imageSubsetWidth, (SkScalar) imageSubsetHeight); |
| 169 SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (ou tY / sampleSize), | 145 SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (ou tY / sampleSize), |
| 170 (SkScalar) get_scaled_dimension(imageSubsetWidth, sampleSize), | 146 (SkScalar) get_scaled_dimension(imageSubsetWidth, sampleSize), |
| 171 (SkScalar) get_scaled_dimension(imageSubsetHeight, sampleSize)); | 147 (SkScalar) get_scaled_dimension(imageSubsetHeight, sampleSize)); |
| 172 SkPaint paint; | 148 SkPaint paint; |
| 173 // Overwrite the dst with the src pixels | 149 // Overwrite the dst with the src pixels |
| 174 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 150 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 175 // TODO (msarett): Test multiple filter qualities. kNone is the default. | 151 // TODO (msarett): Test multiple filter qualities. kNone is the default. |
| 176 canvas.drawBitmapRect(tmp, src, dst, &paint); | 152 canvas.drawBitmapRect(tmp, src, dst, &paint); |
| 177 | 153 |
| 178 return bitmap.detach(); | 154 return bitmap.detach(); |
| 179 } | 155 } |
| 156 | |
| 157 bool SkBitmapRegionCanvas::conversionSupported(SkColorType colorType) { | |
| 158 // SkCanvas does not draw to these color types. | |
| 159 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) { | |
| 160 return false; | |
| 161 } | |
| 162 | |
| 163 // FIXME: Call virtual function when it lands. | |
| 164 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fDecoder->getInfo().al phaType(), | |
| 165 fDecoder->getInfo().profileType()); | |
| 166 return conversion_possible(info, fDecoder->getInfo()); | |
| 167 } | |
| OLD | NEW |