blob: ec6151e80fcddc68f10027819d7a084491265bec [file] [log] [blame]
msarett26ad17b2015-10-22 07:29:19 -07001/*
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
Leon Scroggins III87caae62020-05-04 10:02:45 -04008#include "client_utils/android/BitmapRegionDecoder.h"
9#include "client_utils/android/BitmapRegionDecoderPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/codec/SkAndroidCodec.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/codec/SkCodecPriv.h"
msarett26ad17b2015-10-22 07:29:19 -070012
Leon Scroggins III87caae62020-05-04 10:02:45 -040013namespace android {
14namespace skia {
15
16std::unique_ptr<BitmapRegionDecoder> BitmapRegionDecoder::Make(sk_sp<SkData> data) {
17 auto codec = SkAndroidCodec::MakeFromData(std::move(data));
18 if (nullptr == codec) {
19 SkCodecPrintf("Error: Failed to create codec.\n");
20 return nullptr;
21 }
22
23 switch (codec->getEncodedFormat()) {
24 case SkEncodedImageFormat::kJPEG:
25 case SkEncodedImageFormat::kPNG:
26 case SkEncodedImageFormat::kWEBP:
27 case SkEncodedImageFormat::kHEIF:
28 break;
29 default:
30 return nullptr;
31 }
32
33 return std::unique_ptr<BitmapRegionDecoder>(new BitmapRegionDecoder(std::move(codec)));
34}
35
36BitmapRegionDecoder::BitmapRegionDecoder(std::unique_ptr<SkAndroidCodec> codec)
msarett26ad17b2015-10-22 07:29:19 -070037 : INHERITED(codec->getInfo().width(), codec->getInfo().height())
Leon Scroggins III87caae62020-05-04 10:02:45 -040038 , fCodec(std::move(codec))
msarett26ad17b2015-10-22 07:29:19 -070039{}
40
Leon Scroggins III87caae62020-05-04 10:02:45 -040041bool BitmapRegionDecoder::decodeRegion(SkBitmap* bitmap, BRDAllocator* allocator,
Leon Scroggins III0118e972018-03-13 11:14:33 -040042 const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
43 bool requireUnpremul, sk_sp<SkColorSpace> dstColorSpace) {
msarett26ad17b2015-10-22 07:29:19 -070044
45 // Fix the input sampleSize if necessary.
46 if (sampleSize < 1) {
47 sampleSize = 1;
48 }
49
50 // The size of the output bitmap is determined by the size of the
51 // requested subset, not by the size of the intersection of the subset
52 // and the image dimensions.
53 // If inputX is negative, we will need to place decoded pixels into the
54 // output bitmap starting at a left offset. Call this outX.
55 // If outX is non-zero, subsetX must be zero.
56 // If inputY is negative, we will need to place decoded pixels into the
57 // output bitmap starting at a top offset. Call this outY.
58 // If outY is non-zero, subsetY must be zero.
59 int outX;
60 int outY;
msarett35e5d1b2015-10-27 12:50:25 -070061 SkIRect subset = desiredSubset;
msarett26ad17b2015-10-22 07:29:19 -070062 SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY);
63 if (SubsetType::kOutside_SubsetType == type) {
msarett35e5d1b2015-10-27 12:50:25 -070064 return false;
msarett26ad17b2015-10-22 07:29:19 -070065 }
66
67 // Ask the codec for a scaled subset
68 if (!fCodec->getSupportedSubset(&subset)) {
69 SkCodecPrintf("Error: Could not get subset.\n");
msarett35e5d1b2015-10-27 12:50:25 -070070 return false;
msarett26ad17b2015-10-22 07:29:19 -070071 }
72 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
73
74 // Create the image info for the decode
msarett9a0e3462015-12-11 07:38:50 -080075 SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040076 SkImageInfo decodeInfo =
77 SkImageInfo::Make(scaledSize, dstColorType, dstAlphaType, dstColorSpace);
msarett26ad17b2015-10-22 07:29:19 -070078
msarett26ad17b2015-10-22 07:29:19 -070079 // Initialize the destination bitmap
msarett26ad17b2015-10-22 07:29:19 -070080 int scaledOutX = 0;
81 int scaledOutY = 0;
82 int scaledOutWidth = scaledSize.width();
83 int scaledOutHeight = scaledSize.height();
84 if (SubsetType::kPartiallyInside_SubsetType == type) {
85 scaledOutX = outX / sampleSize;
86 scaledOutY = outY / sampleSize;
87 // We need to be safe here because getSupportedSubset() may have modified the subset.
Brian Osman788b9162020-02-07 10:36:46 -050088 const int extraX = std::max(0, desiredSubset.width() - outX - subset.width());
89 const int extraY = std::max(0, desiredSubset.height() - outY - subset.height());
msarett26ad17b2015-10-22 07:29:19 -070090 const int scaledExtraX = extraX / sampleSize;
91 const int scaledExtraY = extraY / sampleSize;
92 scaledOutWidth += scaledOutX + scaledExtraX;
93 scaledOutHeight += scaledOutY + scaledExtraY;
94 }
95 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
msarett9a0e3462015-12-11 07:38:50 -080096 if (kGray_8_SkColorType == dstColorType) {
97 // The legacy implementations of BitmapFactory and BitmapRegionDecoder
98 // used kAlpha8 for grayscale images (before kGray8 existed). While
99 // the codec recognizes kGray8, we need to decode into a kAlpha8
100 // bitmap in order to avoid a behavior change.
msarett577967c2016-06-03 08:23:40 -0700101 outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
msarett9a0e3462015-12-11 07:38:50 -0800102 }
msarettfcff08c2015-11-05 15:00:56 -0800103 bitmap->setInfo(outInfo);
Mike Reed086a4272017-07-18 10:53:11 -0400104 if (!bitmap->tryAllocPixels(allocator)) {
msarett26ad17b2015-10-22 07:29:19 -0700105 SkCodecPrintf("Error: Could not allocate pixels.\n");
msarett35e5d1b2015-10-27 12:50:25 -0700106 return false;
msarett26ad17b2015-10-22 07:29:19 -0700107 }
108
109 // Zero the bitmap if the region is not completely within the image.
110 // TODO (msarett): Can we make this faster by implementing it to only
111 // zero parts of the image that we won't overwrite with
112 // pixels?
msarettcb8d7192015-11-11 13:30:43 -0800113 SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
114 SkCodec::kNo_ZeroInitialized;
115 if (SubsetType::kPartiallyInside_SubsetType == type &&
116 SkCodec::kNo_ZeroInitialized == zeroInit) {
msarett26ad17b2015-10-22 07:29:19 -0700117 void* pixels = bitmap->getPixels();
Mike Reedf0ffb892017-10-03 14:47:21 -0400118 size_t bytes = outInfo.computeByteSize(bitmap->rowBytes());
msarett26ad17b2015-10-22 07:29:19 -0700119 memset(pixels, 0, bytes);
120 }
121
122 // Decode into the destination bitmap
123 SkAndroidCodec::AndroidOptions options;
124 options.fSampleSize = sampleSize;
125 options.fSubset = &subset;
msarettcb8d7192015-11-11 13:30:43 -0800126 options.fZeroInitialized = zeroInit;
msarett26ad17b2015-10-22 07:29:19 -0700127 void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
msarettfcff08c2015-11-05 15:00:56 -0800128
msarett3d477322016-05-19 07:50:24 -0700129 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(),
130 &options);
Leon Scroggins III6e45ce72018-10-16 15:29:11 -0400131 switch (result) {
132 case SkCodec::kSuccess:
133 case SkCodec::kIncompleteInput:
134 case SkCodec::kErrorInInput:
135 return true;
136 default:
137 SkCodecPrintf("Error: Could not get pixels with message \"%s\".\n",
138 SkCodec::ResultToString(result));
139 return false;
msarett26ad17b2015-10-22 07:29:19 -0700140 }
msarett26ad17b2015-10-22 07:29:19 -0700141}
Leon Scroggins III87caae62020-05-04 10:02:45 -0400142
143} // namespace skia
144} // namespace android