OLD | NEW |
(Empty) | |
| 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 "SkBitmapRegionCodec.h" |
| 9 #include "SkBitmapRegionDecoderPriv.h" |
| 10 #include "SkCanvas.h" |
| 11 #include "SkCodec.h" |
| 12 #include "SkCodecPriv.h" |
| 13 |
| 14 SkBitmapRegionCodec::SkBitmapRegionCodec(SkCodec* codec) |
| 15 : INHERITED(codec->getInfo().width(), codec->getInfo().height()) |
| 16 , fCodec(codec) |
| 17 {} |
| 18 |
| 19 /* |
| 20 * Three differences from the Android version: |
| 21 * Returns a skia bitmap instead of an Android bitmap. |
| 22 * Android version attempts to reuse a recycled bitmap. |
| 23 * Removed the options object and used parameters for color type and sample
size. |
| 24 */ |
| 25 SkBitmap* SkBitmapRegionCodec::decodeRegion(int inputX, int inputY, int inputWid
th, int inputHeight, |
| 26 int sampleSize, SkColorType dstColorType) { |
| 27 |
| 28 // Fix the input sampleSize if necessary. |
| 29 if (sampleSize < 1) { |
| 30 sampleSize = 1; |
| 31 } |
| 32 |
| 33 // The client may not necessarily request a region that is fully within |
| 34 // the image. We may need to do some calculation to determine what part |
| 35 // of the image to decode. |
| 36 |
| 37 // The left offset of the portion of the image we want, where zero |
| 38 // indicates the left edge of the image. |
| 39 int imageSubsetX; |
| 40 |
| 41 // The size of the output bitmap is determined by the size of the |
| 42 // requested region, not by the size of the intersection of the region |
| 43 // and the image dimensions. If inputX is negative, we will need to |
| 44 // place decoded pixels into the output bitmap starting at a left offset. |
| 45 // If this is non-zero, imageSubsetX must be zero. |
| 46 int outX; |
| 47 |
| 48 // The width of the portion of the image that we will write to the output |
| 49 // bitmap. If the region is not fully contained within the image, this |
| 50 // will not be the same as inputWidth. |
| 51 int imageSubsetWidth; |
| 52 bool imageContainsEntireSubset = set_subset_region(inputX, inputWidth, this-
>width(), |
| 53 &imageSubsetX, &outX, &imageSubsetWidth); |
| 54 |
| 55 // The top offset of the portion of the image we want, where zero |
| 56 // indicates the top edge of the image. |
| 57 int imageSubsetY; |
| 58 |
| 59 // The size of the output bitmap is determined by the size of the |
| 60 // requested region, not by the size of the intersection of the region |
| 61 // and the image dimensions. If inputY is negative, we will need to |
| 62 // place decoded pixels into the output bitmap starting at a top offset. |
| 63 // If this is non-zero, imageSubsetY must be zero. |
| 64 int outY; |
| 65 |
| 66 // The height of the portion of the image that we will write to the output |
| 67 // bitmap. If the region is not fully contained within the image, this |
| 68 // will not be the same as inputHeight. |
| 69 int imageSubsetHeight; |
| 70 imageContainsEntireSubset &= set_subset_region(inputY, inputHeight, this->he
ight(), |
| 71 &imageSubsetY, &outY, &imageSubsetHeight); |
| 72 |
| 73 if (imageSubsetWidth <= 0 || imageSubsetHeight <= 0) { |
| 74 SkCodecPrintf("Error: Region must intersect part of the image.\n"); |
| 75 return nullptr; |
| 76 } |
| 77 |
| 78 // Ask the codec for a scaled subset |
| 79 SkIRect subset = SkIRect::MakeXYWH(imageSubsetX, imageSubsetY, imageSubsetWi
dth, |
| 80 imageSubsetHeight); |
| 81 SkCodec::Options options; |
| 82 options.fSubset = ⊂ |
| 83 if (!fCodec->getScaledSubsetDimensions(get_scale(sampleSize), &options)) { |
| 84 SkCodecPrintf("Error: Could not scale.\n"); |
| 85 return nullptr; |
| 86 } |
| 87 |
| 88 // Create the image info the decode |
| 89 SkAlphaType dstAlphaType = fCodec->getInfo().alphaType(); |
| 90 if (kUnpremul_SkAlphaType == dstAlphaType) { |
| 91 dstAlphaType = kPremul_SkAlphaType; |
| 92 } |
| 93 SkImageInfo decodeInfo = SkImageInfo::Make(options.fScaledSubset.width(), |
| 94 options.fScaledSubset.height(), dstColorType, dstAlphaType); |
| 95 |
| 96 // Construct a color table for the decode if necessary |
| 97 SkAutoTUnref<SkColorTable> colorTable(nullptr); |
| 98 SkPMColor* colorPtr = nullptr; |
| 99 int* colorCountPtr = nullptr; |
| 100 int maxColors = 256; |
| 101 SkPMColor colors[maxColors]; |
| 102 if (kIndex_8_SkColorType == dstColorType) { |
| 103 // TODO (msarett): This performs a copy that is completely unnecessary. |
| 104 // And then below we need to use a const cast.... |
| 105 // Finish this comment. |
| 106 colorTable.reset(new SkColorTable(colors, maxColors)); |
| 107 colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 108 colorCountPtr = &maxColors; |
| 109 } |
| 110 |
| 111 // Initialize the destination bitmap |
| 112 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap()); |
| 113 int scaledOutX = 0; |
| 114 int scaledOutY = 0; |
| 115 int scaledOutWidth = options.fScaledSubset.width(); |
| 116 int scaledOutHeight = options.fScaledSubset.height(); |
| 117 if (!imageContainsEntireSubset) { |
| 118 scaledOutX = outX / sampleSize; |
| 119 scaledOutY = outY / sampleSize; |
| 120 int scaledExtraX = (inputWidth - outX - imageSubsetWidth) / sampleSize; |
| 121 int scaledExtraY = (inputHeight - outY - imageSubsetHeight) / sampleSize
; |
| 122 scaledOutWidth += scaledOutX + scaledExtraX; |
| 123 scaledOutHeight += scaledOutY + scaledExtraY; |
| 124 } |
| 125 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); |
| 126 if (!bitmap->tryAllocPixels(outInfo, nullptr, colorTable.get())) { |
| 127 SkCodecPrintf("Error: Could not allocate pixels.\n"); |
| 128 return nullptr; |
| 129 } |
| 130 |
| 131 // Zero the bitmap if the region is not completely within the image. |
| 132 // TODO (msarett): Can we make this faster by implementing it to only |
| 133 // zero parts of the image that we won't overwrite with |
| 134 // pixels? |
| 135 // TODO (msarett): This could be skipped if memory is zero initialized. |
| 136 // This would matter if this code is moved to Android and |
| 137 // uses Android bitmaps. |
| 138 if (!imageContainsEntireSubset && SkCodec::kNo_ZeroInitialized == options.fZ
eroInitialized) { |
| 139 bitmap->eraseColor(0); |
| 140 } |
| 141 |
| 142 // Decode into the destination bitmap |
| 143 void* dst = bitmap->getAddr(scaledOutX, scaledOutY); |
| 144 size_t rowBytes = bitmap->rowBytes(); |
| 145 SkCodec::Result result = fCodec->getPixels(decodeInfo, dst, rowBytes, &optio
ns, colorPtr, |
| 146 colorCountPtr); |
| 147 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) { |
| 148 SkCodecPrintf("Error: Could not get pixels.\n"); |
| 149 return nullptr; |
| 150 } |
| 151 |
| 152 return bitmap.detach(); |
| 153 } |
| 154 |
| 155 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) { |
| 156 // FIXME: Call virtual function when it lands. |
| 157 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph
aType(), |
| 158 fCodec->getInfo().profileType()); |
| 159 return conversion_possible(info, fCodec->getInfo()); |
| 160 } |
OLD | NEW |