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) |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 37 | : INHERITED(codec->getInfo().width(), codec->getInfo().height()) |
Leon Scroggins III | 87caae6 | 2020-05-04 10:02:45 -0400 | [diff] [blame] | 38 | , fCodec(std::move(codec)) |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 39 | {} |
| 40 | |
Leon Scroggins III | 87caae6 | 2020-05-04 10:02:45 -0400 | [diff] [blame] | 41 | bool BitmapRegionDecoder::decodeRegion(SkBitmap* bitmap, BRDAllocator* allocator, |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 42 | const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType, |
| 43 | bool requireUnpremul, sk_sp<SkColorSpace> dstColorSpace) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 44 | |
| 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; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 61 | SkIRect subset = desiredSubset; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 62 | SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY); |
| 63 | if (SubsetType::kOutside_SubsetType == type) { |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 64 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Ask the codec for a scaled subset |
| 68 | if (!fCodec->getSupportedSubset(&subset)) { |
| 69 | SkCodecPrintf("Error: Could not get subset.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 70 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 71 | } |
| 72 | SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset); |
| 73 | |
| 74 | // Create the image info for the decode |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 75 | SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul); |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 76 | SkImageInfo decodeInfo = |
| 77 | SkImageInfo::Make(scaledSize, dstColorType, dstAlphaType, dstColorSpace); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 78 | |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 79 | // Initialize the destination bitmap |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 80 | 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 Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 88 | const int extraX = std::max(0, desiredSubset.width() - outX - subset.width()); |
| 89 | const int extraY = std::max(0, desiredSubset.height() - outY - subset.height()); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 90 | 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); |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 96 | 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. |
msarett | 577967c | 2016-06-03 08:23:40 -0700 | [diff] [blame] | 101 | outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType); |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 102 | } |
msarett | fcff08c | 2015-11-05 15:00:56 -0800 | [diff] [blame] | 103 | bitmap->setInfo(outInfo); |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 104 | if (!bitmap->tryAllocPixels(allocator)) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 105 | SkCodecPrintf("Error: Could not allocate pixels.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 106 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 107 | } |
| 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? |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 113 | SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() : |
| 114 | SkCodec::kNo_ZeroInitialized; |
| 115 | if (SubsetType::kPartiallyInside_SubsetType == type && |
| 116 | SkCodec::kNo_ZeroInitialized == zeroInit) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 117 | void* pixels = bitmap->getPixels(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 118 | size_t bytes = outInfo.computeByteSize(bitmap->rowBytes()); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 119 | memset(pixels, 0, bytes); |
| 120 | } |
| 121 | |
| 122 | // Decode into the destination bitmap |
| 123 | SkAndroidCodec::AndroidOptions options; |
| 124 | options.fSampleSize = sampleSize; |
| 125 | options.fSubset = ⊂ |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 126 | options.fZeroInitialized = zeroInit; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 127 | void* dst = bitmap->getAddr(scaledOutX, scaledOutY); |
msarett | fcff08c | 2015-11-05 15:00:56 -0800 | [diff] [blame] | 128 | |
msarett | 3d47732 | 2016-05-19 07:50:24 -0700 | [diff] [blame] | 129 | SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(), |
| 130 | &options); |
Leon Scroggins III | 6e45ce7 | 2018-10-16 15:29:11 -0400 | [diff] [blame] | 131 | 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; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 140 | } |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 141 | } |
Leon Scroggins III | 87caae6 | 2020-05-04 10:02:45 -0400 | [diff] [blame] | 142 | |
| 143 | } // namespace skia |
| 144 | } // namespace android |