blob: 8727a0bd8abddfa4c25e5f3e8eadeeef296a0bce [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)
Leon Scroggins III43182bc2020-05-21 11:14:31 -040037 : fCodec(std::move(codec))
msarett26ad17b2015-10-22 07:29:19 -070038{}
39
Leon Scroggins III43182bc2020-05-21 11:14:31 -040040int BitmapRegionDecoder::width() const {
41 return fCodec->getInfo().width();
42}
43
44int BitmapRegionDecoder::height() const {
45 return fCodec->getInfo().height();
46}
47
Leon Scroggins III87caae62020-05-04 10:02:45 -040048bool BitmapRegionDecoder::decodeRegion(SkBitmap* bitmap, BRDAllocator* allocator,
Leon Scroggins III0118e972018-03-13 11:14:33 -040049 const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
50 bool requireUnpremul, sk_sp<SkColorSpace> dstColorSpace) {
msarett26ad17b2015-10-22 07:29:19 -070051
52 // Fix the input sampleSize if necessary.
53 if (sampleSize < 1) {
54 sampleSize = 1;
55 }
56
57 // The size of the output bitmap is determined by the size of the
58 // requested subset, not by the size of the intersection of the subset
59 // and the image dimensions.
60 // If inputX is negative, we will need to place decoded pixels into the
61 // output bitmap starting at a left offset. Call this outX.
62 // If outX is non-zero, subsetX must be zero.
63 // If inputY is negative, we will need to place decoded pixels into the
64 // output bitmap starting at a top offset. Call this outY.
65 // If outY is non-zero, subsetY must be zero.
66 int outX;
67 int outY;
msarett35e5d1b2015-10-27 12:50:25 -070068 SkIRect subset = desiredSubset;
msarett26ad17b2015-10-22 07:29:19 -070069 SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY);
70 if (SubsetType::kOutside_SubsetType == type) {
msarett35e5d1b2015-10-27 12:50:25 -070071 return false;
msarett26ad17b2015-10-22 07:29:19 -070072 }
73
74 // Ask the codec for a scaled subset
75 if (!fCodec->getSupportedSubset(&subset)) {
76 SkCodecPrintf("Error: Could not get subset.\n");
msarett35e5d1b2015-10-27 12:50:25 -070077 return false;
msarett26ad17b2015-10-22 07:29:19 -070078 }
79 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
80
81 // Create the image info for the decode
msarett9a0e3462015-12-11 07:38:50 -080082 SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040083 SkImageInfo decodeInfo =
84 SkImageInfo::Make(scaledSize, dstColorType, dstAlphaType, dstColorSpace);
msarett26ad17b2015-10-22 07:29:19 -070085
msarett26ad17b2015-10-22 07:29:19 -070086 // Initialize the destination bitmap
msarett26ad17b2015-10-22 07:29:19 -070087 int scaledOutX = 0;
88 int scaledOutY = 0;
89 int scaledOutWidth = scaledSize.width();
90 int scaledOutHeight = scaledSize.height();
91 if (SubsetType::kPartiallyInside_SubsetType == type) {
92 scaledOutX = outX / sampleSize;
93 scaledOutY = outY / sampleSize;
94 // We need to be safe here because getSupportedSubset() may have modified the subset.
Brian Osman788b9162020-02-07 10:36:46 -050095 const int extraX = std::max(0, desiredSubset.width() - outX - subset.width());
96 const int extraY = std::max(0, desiredSubset.height() - outY - subset.height());
msarett26ad17b2015-10-22 07:29:19 -070097 const int scaledExtraX = extraX / sampleSize;
98 const int scaledExtraY = extraY / sampleSize;
99 scaledOutWidth += scaledOutX + scaledExtraX;
100 scaledOutHeight += scaledOutY + scaledExtraY;
101 }
102 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
msarett9a0e3462015-12-11 07:38:50 -0800103 if (kGray_8_SkColorType == dstColorType) {
104 // The legacy implementations of BitmapFactory and BitmapRegionDecoder
105 // used kAlpha8 for grayscale images (before kGray8 existed). While
106 // the codec recognizes kGray8, we need to decode into a kAlpha8
107 // bitmap in order to avoid a behavior change.
msarett577967c2016-06-03 08:23:40 -0700108 outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
msarett9a0e3462015-12-11 07:38:50 -0800109 }
msarettfcff08c2015-11-05 15:00:56 -0800110 bitmap->setInfo(outInfo);
Mike Reed086a4272017-07-18 10:53:11 -0400111 if (!bitmap->tryAllocPixels(allocator)) {
msarett26ad17b2015-10-22 07:29:19 -0700112 SkCodecPrintf("Error: Could not allocate pixels.\n");
msarett35e5d1b2015-10-27 12:50:25 -0700113 return false;
msarett26ad17b2015-10-22 07:29:19 -0700114 }
115
116 // Zero the bitmap if the region is not completely within the image.
117 // TODO (msarett): Can we make this faster by implementing it to only
118 // zero parts of the image that we won't overwrite with
119 // pixels?
msarettcb8d7192015-11-11 13:30:43 -0800120 SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
121 SkCodec::kNo_ZeroInitialized;
122 if (SubsetType::kPartiallyInside_SubsetType == type &&
123 SkCodec::kNo_ZeroInitialized == zeroInit) {
msarett26ad17b2015-10-22 07:29:19 -0700124 void* pixels = bitmap->getPixels();
Mike Reedf0ffb892017-10-03 14:47:21 -0400125 size_t bytes = outInfo.computeByteSize(bitmap->rowBytes());
msarett26ad17b2015-10-22 07:29:19 -0700126 memset(pixels, 0, bytes);
127 }
128
129 // Decode into the destination bitmap
130 SkAndroidCodec::AndroidOptions options;
131 options.fSampleSize = sampleSize;
132 options.fSubset = &subset;
msarettcb8d7192015-11-11 13:30:43 -0800133 options.fZeroInitialized = zeroInit;
msarett26ad17b2015-10-22 07:29:19 -0700134 void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
msarettfcff08c2015-11-05 15:00:56 -0800135
msarett3d477322016-05-19 07:50:24 -0700136 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(),
137 &options);
Leon Scroggins III6e45ce72018-10-16 15:29:11 -0400138 switch (result) {
139 case SkCodec::kSuccess:
140 case SkCodec::kIncompleteInput:
141 case SkCodec::kErrorInInput:
142 return true;
143 default:
144 SkCodecPrintf("Error: Could not get pixels with message \"%s\".\n",
145 SkCodec::ResultToString(result));
146 return false;
msarett26ad17b2015-10-22 07:29:19 -0700147 }
msarett26ad17b2015-10-22 07:29:19 -0700148}
Leon Scroggins III87caae62020-05-04 10:02:45 -0400149
150} // namespace skia
151} // namespace android