Chromium Code Reviews| Index: tools/SkBitmapRegionCanvas.cpp |
| diff --git a/tools/SkBitmapRegionCanvas.cpp b/tools/SkBitmapRegionCanvas.cpp |
| index 086ac19864b3a15e96dae61d26a835eca43cb269..0ed95cbe1528de7d3edd4356ffd0bbece7804de9 100644 |
| --- a/tools/SkBitmapRegionCanvas.cpp |
| +++ b/tools/SkBitmapRegionCanvas.cpp |
| @@ -6,7 +6,9 @@ |
| */ |
| #include "SkBitmapRegionCanvas.h" |
| +#include "SkBitmapRegionDecoderPriv.h" |
| #include "SkCanvas.h" |
| +#include "SkCodecPriv.h" |
| SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder) |
| : INHERITED(decoder->getInfo().width(), decoder->getInfo().height()) |
| @@ -14,36 +16,6 @@ SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder) |
| {} |
| /* |
| - * Chooses the correct image subset offsets and dimensions for the partial decode. |
| - */ |
| -static inline void set_subset_region(int inputOffset, int inputDimension, |
| - int imageOriginalDimension, int* imageSubsetOffset, int* outOffset, |
| - int* imageSubsetDimension) { |
| - |
| - // This must be at least zero, we can't start decoding the image at a negative coordinate. |
| - *imageSubsetOffset = SkTMax(0, inputOffset); |
| - |
| - // If inputOffset is less than zero, we decode to an offset location in the output bitmap. |
| - *outOffset = *imageSubsetOffset - inputOffset; |
| - |
| - // Use imageSusetOffset to make sure we don't decode pixels past the edge of the image. |
| - // Use outOffset to make sure we don't decode pixels past the edge of the region. |
| - *imageSubsetDimension = SkTMin(imageOriginalDimension - *imageSubsetOffset, |
| - inputDimension - *outOffset); |
| -} |
| - |
| -/* |
| - * Returns a scaled dimension based on the original dimension and the sample size. |
| - * TODO: Share this implementation with SkScaledCodec. |
| - */ |
| -static int get_scaled_dimension(int srcDimension, int sampleSize) { |
| - if (sampleSize > srcDimension) { |
| - return 1; |
| - } |
| - return srcDimension / sampleSize; |
| -} |
| - |
| -/* |
| * Three differences from the Android version: |
| * Returns a Skia bitmap instead of an Android bitmap. |
| * Android version attempts to reuse a recycled bitmap. |
| @@ -56,10 +28,15 @@ SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
| SkColorType dstColorType) { |
| // Reject color types not supported by this method |
| if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorType) { |
| - SkDebugf("Error: Color type not supported.\n"); |
| + SkCodecPrintf("Error: Color type not supported.\n"); |
| return nullptr; |
| } |
| + // Fix the input sampleSize if necessary. |
| + 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
|
| + sampleSize = 1; |
| + } |
| + |
| // The client may not necessarily request a region that is fully within |
| // the image. We may need to do some calculation to determine what part |
| // of the image to decode. |
| @@ -79,7 +56,8 @@ SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
| // bitmap. If the region is not fully contained within the image, this |
| // will not be the same as inputWidth. |
| int imageSubsetWidth; |
| - set_subset_region(inputX, inputWidth, this->width(), &imageSubsetX, &outX, &imageSubsetWidth); |
| + bool imageContainsEntireSubset = set_subset_region(inputX, inputWidth, this->width(), |
| + &imageSubsetX, &outX, &imageSubsetWidth); |
| // The top offset of the portion of the image we want, where zero |
| // indicates the top edge of the image. |
| @@ -96,11 +74,11 @@ SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
| // bitmap. If the region is not fully contained within the image, this |
| // will not be the same as inputHeight. |
| int imageSubsetHeight; |
| - set_subset_region(inputY, inputHeight, this->height(), &imageSubsetY, &outY, |
| - &imageSubsetHeight); |
| + imageContainsEntireSubset &= set_subset_region(inputY, inputHeight, this->height(), |
| + &imageSubsetY, &outY, &imageSubsetHeight); |
| if (imageSubsetWidth <= 0 || imageSubsetHeight <= 0) { |
| - SkDebugf("Error: Region must intersect part of the image.\n"); |
| + SkCodecPrintf("Error: Region must intersect part of the image.\n"); |
| return nullptr; |
| } |
| @@ -111,11 +89,11 @@ SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
| } |
| SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), |
| dstColorType, dstAlphaType); |
| - |
| + |
| // Start the scanline decoder |
| SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo); |
| if (SkCodec::kSuccess != r) { |
| - SkDebugf("Error: Could not start scanline decoder.\n"); |
| + SkCodecPrintf("Error: Could not start scanline decoder.\n"); |
| return nullptr; |
| } |
| @@ -123,13 +101,13 @@ SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
| SkBitmap tmp; |
| SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), imageSubsetHeight); |
| if (!tmp.tryAllocPixels(tmpInfo)) { |
| - SkDebugf("Error: Could not allocate pixels.\n"); |
| + SkCodecPrintf("Error: Could not allocate pixels.\n"); |
| return nullptr; |
| } |
| // Skip the unneeded rows |
| if (!fDecoder->skipScanlines(imageSubsetY)) { |
| - SkDebugf("Error: Failed to skip scanlines.\n"); |
| + SkCodecPrintf("Error: Failed to skip scanlines.\n"); |
| return nullptr; |
| } |
| @@ -144,7 +122,7 @@ SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
| SkAutoTDelete<SkBitmap> bitmap(new SkBitmap()); |
| SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight); |
| if (!bitmap->tryAllocPixels(dstInfo)) { |
| - SkDebugf("Error: Could not allocate pixels.\n"); |
| + SkCodecPrintf("Error: Could not allocate pixels.\n"); |
| return nullptr; |
| } |
| @@ -155,9 +133,7 @@ SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
| // TODO (msarett): This could be skipped if memory is zero initialized. |
| // This would matter if this code is moved to Android and |
| // uses Android bitmaps. |
| - if (0 != outX || 0 != outY || |
| - inputX + inputWidth > this->width() || |
| - inputY + inputHeight > this->height()) { |
| + if (!imageContainsEntireSubset) { |
| bitmap->eraseColor(0); |
| } |
| @@ -177,3 +153,15 @@ SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
| return bitmap.detach(); |
| } |
| + |
| +bool SkBitmapRegionCanvas::conversionSupported(SkColorType colorType) { |
| + // SkCanvas does not draw to these color types. |
| + if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) { |
| + return false; |
| + } |
| + |
| + // FIXME: Call virtual function when it lands. |
| + SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fDecoder->getInfo().alphaType(), |
| + fDecoder->getInfo().profileType()); |
| + return conversion_possible(info, fDecoder->getInfo()); |
| +} |