msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 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 | |
Leon Scroggins III | 87caae6 | 2020-05-04 10:02:45 -0400 | [diff] [blame] | 8 | #include "client_utils/android/BitmapRegionDecoder.h" |
| 9 | #include "client_utils/android/BitmapRegionDecoderPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/codec/SkAndroidCodec.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/codec/SkCodecPriv.h" |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 12 | |
Leon Scroggins III | 87caae6 | 2020-05-04 10:02:45 -0400 | [diff] [blame] | 13 | namespace android { |
| 14 | namespace skia { |
| 15 | |
| 16 | std::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 | |
| 36 | BitmapRegionDecoder::BitmapRegionDecoder(std::unique_ptr<SkAndroidCodec> codec) |
Leon Scroggins III | 43182bc | 2020-05-21 11:14:31 -0400 | [diff] [blame] | 37 | : fCodec(std::move(codec)) |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 38 | {} |
| 39 | |
Leon Scroggins III | 43182bc | 2020-05-21 11:14:31 -0400 | [diff] [blame] | 40 | int BitmapRegionDecoder::width() const { |
| 41 | return fCodec->getInfo().width(); |
| 42 | } |
| 43 | |
| 44 | int BitmapRegionDecoder::height() const { |
| 45 | return fCodec->getInfo().height(); |
| 46 | } |
| 47 | |
Leon Scroggins III | 87caae6 | 2020-05-04 10:02:45 -0400 | [diff] [blame] | 48 | bool BitmapRegionDecoder::decodeRegion(SkBitmap* bitmap, BRDAllocator* allocator, |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 49 | const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType, |
| 50 | bool requireUnpremul, sk_sp<SkColorSpace> dstColorSpace) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 51 | |
| 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; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 68 | SkIRect subset = desiredSubset; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 69 | SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY); |
| 70 | if (SubsetType::kOutside_SubsetType == type) { |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 71 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Ask the codec for a scaled subset |
| 75 | if (!fCodec->getSupportedSubset(&subset)) { |
| 76 | SkCodecPrintf("Error: Could not get subset.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 77 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 78 | } |
| 79 | SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset); |
| 80 | |
| 81 | // Create the image info for the decode |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 82 | SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 83 | SkImageInfo decodeInfo = |
| 84 | SkImageInfo::Make(scaledSize, dstColorType, dstAlphaType, dstColorSpace); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 85 | |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 86 | // Initialize the destination bitmap |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 87 | 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 Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 95 | const int extraX = std::max(0, desiredSubset.width() - outX - subset.width()); |
| 96 | const int extraY = std::max(0, desiredSubset.height() - outY - subset.height()); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 97 | 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); |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 103 | 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. |
msarett | 577967c | 2016-06-03 08:23:40 -0700 | [diff] [blame] | 108 | outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType); |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 109 | } |
msarett | fcff08c | 2015-11-05 15:00:56 -0800 | [diff] [blame] | 110 | bitmap->setInfo(outInfo); |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 111 | if (!bitmap->tryAllocPixels(allocator)) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 112 | SkCodecPrintf("Error: Could not allocate pixels.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 113 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 114 | } |
| 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? |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 120 | SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() : |
| 121 | SkCodec::kNo_ZeroInitialized; |
| 122 | if (SubsetType::kPartiallyInside_SubsetType == type && |
| 123 | SkCodec::kNo_ZeroInitialized == zeroInit) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 124 | void* pixels = bitmap->getPixels(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 125 | size_t bytes = outInfo.computeByteSize(bitmap->rowBytes()); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 126 | memset(pixels, 0, bytes); |
| 127 | } |
| 128 | |
| 129 | // Decode into the destination bitmap |
| 130 | SkAndroidCodec::AndroidOptions options; |
| 131 | options.fSampleSize = sampleSize; |
| 132 | options.fSubset = ⊂ |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 133 | options.fZeroInitialized = zeroInit; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 134 | void* dst = bitmap->getAddr(scaledOutX, scaledOutY); |
msarett | fcff08c | 2015-11-05 15:00:56 -0800 | [diff] [blame] | 135 | |
msarett | 3d47732 | 2016-05-19 07:50:24 -0700 | [diff] [blame] | 136 | SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(), |
| 137 | &options); |
Leon Scroggins III | 6e45ce7 | 2018-10-16 15:29:11 -0400 | [diff] [blame] | 138 | 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; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 147 | } |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 148 | } |
Leon Scroggins III | 87caae6 | 2020-05-04 10:02:45 -0400 | [diff] [blame] | 149 | |
| 150 | } // namespace skia |
| 151 | } // namespace android |