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