| 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 "client_utils/android/BitmapRegionDecoder.h" |
| 9 |
#include "client_utils/android/BitmapRegionDecoderPriv.h" |
| 10 |
#include "include/codec/SkAndroidCodec.h" |
| 11 |
#include "include/codec/SkEncodedImageFormat.h" |
| 12 |
#include "src/codec/SkCodecPriv.h" |
| 13 |
|
| 14 |
namespace android { |
| 15 |
namespace skia { |
| 16 |
|
| 17 |
std::unique_ptr<BitmapRegionDecoder> BitmapRegionDecoder::Make(sk_sp<SkData> data) { |
| 18 |
auto codec = SkAndroidCodec::MakeFromData(std::move(data)); |
| 19 |
if (nullptr == codec) { |
| 20 |
SkCodecPrintf("Error: Failed to create codec.\n"); |
| 21 |
return nullptr; |
| 22 |
} |
| 23 |
|
| 24 |
switch (codec->getEncodedFormat()) { |
| 25 |
case SkEncodedImageFormat::kJPEG: |
| 26 |
case SkEncodedImageFormat::kPNG: |
| 27 |
case SkEncodedImageFormat::kWEBP: |
| 28 |
case SkEncodedImageFormat::kHEIF: |
| 29 |
case SkEncodedImageFormat::kAVIF: |
| 30 |
break; |
| 31 |
default: |
| 32 |
return nullptr; |
| 33 |
} |
| 34 |
|
| 35 |
return std::unique_ptr<BitmapRegionDecoder>(new BitmapRegionDecoder(std::move(codec))); |
| 36 |
} |
| 37 |
|
| 38 |
BitmapRegionDecoder::BitmapRegionDecoder(std::unique_ptr<SkAndroidCodec> codec) |
| 39 |
: fCodec(std::move(codec)) |
| 40 |
{} |
| 41 |
|
| 42 |
int BitmapRegionDecoder::width() const { |
| 43 |
return fCodec->getInfo().width(); |
| 44 |
} |
| 45 |
|
| 46 |
int BitmapRegionDecoder::height() const { |
| 47 |
return fCodec->getInfo().height(); |
| 48 |
} |
| 49 |
|
| 50 |
bool BitmapRegionDecoder::decodeRegion(SkBitmap* bitmap, BRDAllocator* allocator, |
| 51 |
const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType, |
| 52 |
bool requireUnpremul, sk_sp<SkColorSpace> dstColorSpace) { |
| 53 |
|
| 54 |
// Fix the input sampleSize if necessary. |
| 55 |
if (sampleSize < 1) { |
| 56 |
sampleSize = 1; |
| 57 |
} |
| 58 |
|
| 59 |
// The size of the output bitmap is determined by the size of the |
| 60 |
// requested subset, not by the size of the intersection of the subset |
| 61 |
// and the image dimensions. |
| 62 |
// If inputX is negative, we will need to place decoded pixels into the |
| 63 |
// output bitmap starting at a left offset. Call this outX. |
| 64 |
// If outX is non-zero, subsetX must be zero. |
| 65 |
// If inputY is negative, we will need to place decoded pixels into the |
| 66 |
// output bitmap starting at a top offset. Call this outY. |
| 67 |
// If outY is non-zero, subsetY must be zero. |
| 68 |
int outX; |
| 69 |
int outY; |
| 70 |
SkIRect subset = desiredSubset; |
| 71 |
SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY); |
| 72 |
if (SubsetType::kOutside_SubsetType == type) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
// Ask the codec for a scaled subset |
| 77 |
if (!fCodec->getSupportedSubset(&subset)) { |
| 78 |
SkCodecPrintf("Error: Could not get subset.\n"); |
| 79 |
return false; |
| 80 |
} |
| 81 |
SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset); |
| 82 |
|
| 83 |
// Create the image info for the decode |
| 84 |
SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul); |
| 85 |
SkImageInfo decodeInfo = |
| 86 |
SkImageInfo::Make(scaledSize, dstColorType, dstAlphaType, std::move(dstColorSpace)); |
| 87 |
|
| 88 |
// Initialize the destination bitmap |
| 89 |
int scaledOutX = 0; |
| 90 |
int scaledOutY = 0; |
| 91 |
int scaledOutWidth = scaledSize.width(); |
| 92 |
int scaledOutHeight = scaledSize.height(); |
| 93 |
if (SubsetType::kPartiallyInside_SubsetType == type) { |
| 94 |
scaledOutX = outX / sampleSize; |
| 95 |
scaledOutY = outY / sampleSize; |
| 96 |
// We need to be safe here because getSupportedSubset() may have modified the subset. |
| 97 |
const int extraX = std::max(0, desiredSubset.width() - outX - subset.width()); |
| 98 |
const int extraY = std::max(0, desiredSubset.height() - outY - subset.height()); |
| 99 |
const int scaledExtraX = extraX / sampleSize; |
| 100 |
const int scaledExtraY = extraY / sampleSize; |
| 101 |
scaledOutWidth += scaledOutX + scaledExtraX; |
| 102 |
scaledOutHeight += scaledOutY + scaledExtraY; |
| 103 |
} |
| 104 |
SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); |
| 105 |
if (kGray_8_SkColorType == dstColorType) { |
| 106 |
// The legacy implementations of BitmapFactory and BitmapRegionDecoder |
| 107 |
// used kAlpha8 for grayscale images (before kGray8 existed). While |
| 108 |
// the codec recognizes kGray8, we need to decode into a kAlpha8 |
| 109 |
// bitmap in order to avoid a behavior change. |
| 110 |
outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType); |
| 111 |
} |
| 112 |
bitmap->setInfo(outInfo); |
| 113 |
if (!bitmap->tryAllocPixels(allocator)) { |
| 114 |
SkCodecPrintf("Error: Could not allocate pixels.\n"); |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
// Zero the bitmap if the region is not completely within the image. |
| 119 |
// TODO (msarett): Can we make this faster by implementing it to only |
| 120 |
// zero parts of the image that we won't overwrite with |
| 121 |
// pixels? |
| 122 |
SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() : |
| 123 |
SkCodec::kNo_ZeroInitialized; |
| 124 |
if (SubsetType::kPartiallyInside_SubsetType == type && |
| 125 |
SkCodec::kNo_ZeroInitialized == zeroInit) { |
| 126 |
void* pixels = bitmap->getPixels(); |
| 127 |
size_t bytes = outInfo.computeByteSize(bitmap->rowBytes()); |
| 128 |
memset(pixels, 0, bytes); |
| 129 |
} |
| 130 |
|
| 131 |
// Decode into the destination bitmap |
| 132 |
SkAndroidCodec::AndroidOptions options; |
| 133 |
options.fSampleSize = sampleSize; |
| 134 |
options.fSubset = ⊂ |
| 135 |
options.fZeroInitialized = zeroInit; |
| 136 |
void* dst = bitmap->getAddr(scaledOutX, scaledOutY); |
| 137 |
|
| 138 |
SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(), |
| 139 |
&options); |
| 140 |
switch (result) { |
| 141 |
case SkCodec::kSuccess: |
| 142 |
case SkCodec::kIncompleteInput: |
| 143 |
case SkCodec::kErrorInInput: |
| 144 |
return true; |
| 145 |
default: |
| 146 |
SkCodecPrintf("Error: Could not get pixels with message \"%s\".\n", |
| 147 |
SkCodec::ResultToString(result)); |
| 148 |
return false; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
} // namespace skia |
| 153 |
} // namespace android |