2015-10-22 14:29:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2020-05-04 14:02:45 +00:00
|
|
|
#include "client_utils/android/BitmapRegionDecoder.h"
|
|
|
|
#include "client_utils/android/BitmapRegionDecoderPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/codec/SkAndroidCodec.h"
|
|
|
|
#include "src/codec/SkCodecPriv.h"
|
2015-10-22 14:29:19 +00:00
|
|
|
|
2020-05-04 14:02:45 +00:00
|
|
|
namespace android {
|
|
|
|
namespace skia {
|
|
|
|
|
|
|
|
std::unique_ptr<BitmapRegionDecoder> BitmapRegionDecoder::Make(sk_sp<SkData> data) {
|
|
|
|
auto codec = SkAndroidCodec::MakeFromData(std::move(data));
|
|
|
|
if (nullptr == codec) {
|
|
|
|
SkCodecPrintf("Error: Failed to create codec.\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (codec->getEncodedFormat()) {
|
|
|
|
case SkEncodedImageFormat::kJPEG:
|
|
|
|
case SkEncodedImageFormat::kPNG:
|
|
|
|
case SkEncodedImageFormat::kWEBP:
|
|
|
|
case SkEncodedImageFormat::kHEIF:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::unique_ptr<BitmapRegionDecoder>(new BitmapRegionDecoder(std::move(codec)));
|
|
|
|
}
|
|
|
|
|
|
|
|
BitmapRegionDecoder::BitmapRegionDecoder(std::unique_ptr<SkAndroidCodec> codec)
|
2020-05-21 15:14:31 +00:00
|
|
|
: fCodec(std::move(codec))
|
2015-10-22 14:29:19 +00:00
|
|
|
{}
|
|
|
|
|
2020-05-21 15:14:31 +00:00
|
|
|
int BitmapRegionDecoder::width() const {
|
|
|
|
return fCodec->getInfo().width();
|
|
|
|
}
|
|
|
|
|
|
|
|
int BitmapRegionDecoder::height() const {
|
|
|
|
return fCodec->getInfo().height();
|
|
|
|
}
|
|
|
|
|
2020-05-04 14:02:45 +00:00
|
|
|
bool BitmapRegionDecoder::decodeRegion(SkBitmap* bitmap, BRDAllocator* allocator,
|
2018-03-13 15:14:33 +00:00
|
|
|
const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
|
|
|
|
bool requireUnpremul, sk_sp<SkColorSpace> dstColorSpace) {
|
2015-10-22 14:29:19 +00:00
|
|
|
|
|
|
|
// Fix the input sampleSize if necessary.
|
|
|
|
if (sampleSize < 1) {
|
|
|
|
sampleSize = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The size of the output bitmap is determined by the size of the
|
|
|
|
// requested subset, not by the size of the intersection of the subset
|
|
|
|
// and the image dimensions.
|
|
|
|
// If inputX is negative, we will need to place decoded pixels into the
|
|
|
|
// output bitmap starting at a left offset. Call this outX.
|
|
|
|
// If outX is non-zero, subsetX must be zero.
|
|
|
|
// If inputY is negative, we will need to place decoded pixels into the
|
|
|
|
// output bitmap starting at a top offset. Call this outY.
|
|
|
|
// If outY is non-zero, subsetY must be zero.
|
|
|
|
int outX;
|
|
|
|
int outY;
|
2015-10-27 19:50:25 +00:00
|
|
|
SkIRect subset = desiredSubset;
|
2015-10-22 14:29:19 +00:00
|
|
|
SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY);
|
|
|
|
if (SubsetType::kOutside_SubsetType == type) {
|
2015-10-27 19:50:25 +00:00
|
|
|
return false;
|
2015-10-22 14:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the codec for a scaled subset
|
|
|
|
if (!fCodec->getSupportedSubset(&subset)) {
|
|
|
|
SkCodecPrintf("Error: Could not get subset.\n");
|
2015-10-27 19:50:25 +00:00
|
|
|
return false;
|
2015-10-22 14:29:19 +00:00
|
|
|
}
|
|
|
|
SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
|
|
|
|
|
|
|
|
// Create the image info for the decode
|
2015-12-11 15:38:50 +00:00
|
|
|
SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul);
|
2019-09-30 19:12:27 +00:00
|
|
|
SkImageInfo decodeInfo =
|
|
|
|
SkImageInfo::Make(scaledSize, dstColorType, dstAlphaType, dstColorSpace);
|
2015-10-22 14:29:19 +00:00
|
|
|
|
|
|
|
// Initialize the destination bitmap
|
|
|
|
int scaledOutX = 0;
|
|
|
|
int scaledOutY = 0;
|
|
|
|
int scaledOutWidth = scaledSize.width();
|
|
|
|
int scaledOutHeight = scaledSize.height();
|
|
|
|
if (SubsetType::kPartiallyInside_SubsetType == type) {
|
|
|
|
scaledOutX = outX / sampleSize;
|
|
|
|
scaledOutY = outY / sampleSize;
|
|
|
|
// We need to be safe here because getSupportedSubset() may have modified the subset.
|
2020-02-07 15:36:46 +00:00
|
|
|
const int extraX = std::max(0, desiredSubset.width() - outX - subset.width());
|
|
|
|
const int extraY = std::max(0, desiredSubset.height() - outY - subset.height());
|
2015-10-22 14:29:19 +00:00
|
|
|
const int scaledExtraX = extraX / sampleSize;
|
|
|
|
const int scaledExtraY = extraY / sampleSize;
|
|
|
|
scaledOutWidth += scaledOutX + scaledExtraX;
|
|
|
|
scaledOutHeight += scaledOutY + scaledExtraY;
|
|
|
|
}
|
|
|
|
SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
|
2015-12-11 15:38:50 +00:00
|
|
|
if (kGray_8_SkColorType == dstColorType) {
|
|
|
|
// The legacy implementations of BitmapFactory and BitmapRegionDecoder
|
|
|
|
// used kAlpha8 for grayscale images (before kGray8 existed). While
|
|
|
|
// the codec recognizes kGray8, we need to decode into a kAlpha8
|
|
|
|
// bitmap in order to avoid a behavior change.
|
2016-06-03 15:23:40 +00:00
|
|
|
outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
|
2015-12-11 15:38:50 +00:00
|
|
|
}
|
2015-11-05 23:00:56 +00:00
|
|
|
bitmap->setInfo(outInfo);
|
2017-07-18 14:53:11 +00:00
|
|
|
if (!bitmap->tryAllocPixels(allocator)) {
|
2015-10-22 14:29:19 +00:00
|
|
|
SkCodecPrintf("Error: Could not allocate pixels.\n");
|
2015-10-27 19:50:25 +00:00
|
|
|
return false;
|
2015-10-22 14:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Zero the bitmap if the region is not completely within the image.
|
|
|
|
// TODO (msarett): Can we make this faster by implementing it to only
|
|
|
|
// zero parts of the image that we won't overwrite with
|
|
|
|
// pixels?
|
2015-11-11 21:30:43 +00:00
|
|
|
SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
|
|
|
|
SkCodec::kNo_ZeroInitialized;
|
|
|
|
if (SubsetType::kPartiallyInside_SubsetType == type &&
|
|
|
|
SkCodec::kNo_ZeroInitialized == zeroInit) {
|
2015-10-22 14:29:19 +00:00
|
|
|
void* pixels = bitmap->getPixels();
|
2017-10-03 18:47:21 +00:00
|
|
|
size_t bytes = outInfo.computeByteSize(bitmap->rowBytes());
|
2015-10-22 14:29:19 +00:00
|
|
|
memset(pixels, 0, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode into the destination bitmap
|
|
|
|
SkAndroidCodec::AndroidOptions options;
|
|
|
|
options.fSampleSize = sampleSize;
|
|
|
|
options.fSubset = ⊂
|
2015-11-11 21:30:43 +00:00
|
|
|
options.fZeroInitialized = zeroInit;
|
2015-10-22 14:29:19 +00:00
|
|
|
void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
|
2015-11-05 23:00:56 +00:00
|
|
|
|
2016-05-19 14:50:24 +00:00
|
|
|
SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(),
|
|
|
|
&options);
|
2018-10-16 19:29:11 +00:00
|
|
|
switch (result) {
|
|
|
|
case SkCodec::kSuccess:
|
|
|
|
case SkCodec::kIncompleteInput:
|
|
|
|
case SkCodec::kErrorInInput:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
SkCodecPrintf("Error: Could not get pixels with message \"%s\".\n",
|
|
|
|
SkCodec::ResultToString(result));
|
|
|
|
return false;
|
2015-10-22 14:29:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 14:02:45 +00:00
|
|
|
|
|
|
|
} // namespace skia
|
|
|
|
} // namespace android
|