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 | |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 8 | #include "SkAndroidCodec.h" |
msarett | 84a8065 | 2015-11-10 15:49:46 -0800 | [diff] [blame] | 9 | #include "SkBitmapRegionCodec.h" |
| 10 | #include "SkBitmapRegionDecoderPriv.h" |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 11 | #include "SkCodecPriv.h" |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 12 | |
| 13 | SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec) |
| 14 | : INHERITED(codec->getInfo().width(), codec->getInfo().height()) |
| 15 | , fCodec(codec) |
| 16 | {} |
| 17 | |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 18 | bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator, |
Leon Scroggins III | 0118e97 | 2018-03-13 11:14:33 -0400 | [diff] [blame] | 19 | const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType, |
| 20 | bool requireUnpremul, sk_sp<SkColorSpace> dstColorSpace) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 21 | |
| 22 | // Fix the input sampleSize if necessary. |
| 23 | if (sampleSize < 1) { |
| 24 | sampleSize = 1; |
| 25 | } |
| 26 | |
| 27 | // The size of the output bitmap is determined by the size of the |
| 28 | // requested subset, not by the size of the intersection of the subset |
| 29 | // and the image dimensions. |
| 30 | // If inputX is negative, we will need to place decoded pixels into the |
| 31 | // output bitmap starting at a left offset. Call this outX. |
| 32 | // If outX is non-zero, subsetX must be zero. |
| 33 | // If inputY is negative, we will need to place decoded pixels into the |
| 34 | // output bitmap starting at a top offset. Call this outY. |
| 35 | // If outY is non-zero, subsetY must be zero. |
| 36 | int outX; |
| 37 | int outY; |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 38 | SkIRect subset = desiredSubset; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 39 | SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY); |
| 40 | if (SubsetType::kOutside_SubsetType == type) { |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 41 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // Ask the codec for a scaled subset |
| 45 | if (!fCodec->getSupportedSubset(&subset)) { |
| 46 | SkCodecPrintf("Error: Could not get subset.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 47 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 48 | } |
| 49 | SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset); |
| 50 | |
| 51 | // Create the image info for the decode |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 52 | SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul); |
msarett | e26a8ad | 2016-09-01 17:47:46 -0700 | [diff] [blame] | 53 | SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(), |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 54 | dstColorType, dstAlphaType, dstColorSpace); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 55 | |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 56 | // Initialize the destination bitmap |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 57 | int scaledOutX = 0; |
| 58 | int scaledOutY = 0; |
| 59 | int scaledOutWidth = scaledSize.width(); |
| 60 | int scaledOutHeight = scaledSize.height(); |
| 61 | if (SubsetType::kPartiallyInside_SubsetType == type) { |
| 62 | scaledOutX = outX / sampleSize; |
| 63 | scaledOutY = outY / sampleSize; |
| 64 | // We need to be safe here because getSupportedSubset() may have modified the subset. |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 65 | const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width()); |
| 66 | const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height()); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 67 | const int scaledExtraX = extraX / sampleSize; |
| 68 | const int scaledExtraY = extraY / sampleSize; |
| 69 | scaledOutWidth += scaledOutX + scaledExtraX; |
| 70 | scaledOutHeight += scaledOutY + scaledExtraY; |
| 71 | } |
| 72 | SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 73 | if (kGray_8_SkColorType == dstColorType) { |
| 74 | // The legacy implementations of BitmapFactory and BitmapRegionDecoder |
| 75 | // used kAlpha8 for grayscale images (before kGray8 existed). While |
| 76 | // the codec recognizes kGray8, we need to decode into a kAlpha8 |
| 77 | // bitmap in order to avoid a behavior change. |
msarett | 577967c | 2016-06-03 08:23:40 -0700 | [diff] [blame] | 78 | outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType); |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 79 | } |
msarett | fcff08c | 2015-11-05 15:00:56 -0800 | [diff] [blame] | 80 | bitmap->setInfo(outInfo); |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 81 | if (!bitmap->tryAllocPixels(allocator)) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 82 | SkCodecPrintf("Error: Could not allocate pixels.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 83 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Zero the bitmap if the region is not completely within the image. |
| 87 | // TODO (msarett): Can we make this faster by implementing it to only |
| 88 | // zero parts of the image that we won't overwrite with |
| 89 | // pixels? |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 90 | SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() : |
| 91 | SkCodec::kNo_ZeroInitialized; |
| 92 | if (SubsetType::kPartiallyInside_SubsetType == type && |
| 93 | SkCodec::kNo_ZeroInitialized == zeroInit) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 94 | void* pixels = bitmap->getPixels(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 95 | size_t bytes = outInfo.computeByteSize(bitmap->rowBytes()); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 96 | memset(pixels, 0, bytes); |
| 97 | } |
| 98 | |
| 99 | // Decode into the destination bitmap |
| 100 | SkAndroidCodec::AndroidOptions options; |
| 101 | options.fSampleSize = sampleSize; |
| 102 | options.fSubset = ⊂ |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 103 | options.fZeroInitialized = zeroInit; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 104 | void* dst = bitmap->getAddr(scaledOutX, scaledOutY); |
msarett | fcff08c | 2015-11-05 15:00:56 -0800 | [diff] [blame] | 105 | |
msarett | 3d47732 | 2016-05-19 07:50:24 -0700 | [diff] [blame] | 106 | SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(), |
| 107 | &options); |
Leon Scroggins III | 6e45ce7 | 2018-10-16 15:29:11 -0400 | [diff] [blame] | 108 | switch (result) { |
| 109 | case SkCodec::kSuccess: |
| 110 | case SkCodec::kIncompleteInput: |
| 111 | case SkCodec::kErrorInInput: |
| 112 | return true; |
| 113 | default: |
| 114 | SkCodecPrintf("Error: Could not get pixels with message \"%s\".\n", |
| 115 | SkCodec::ResultToString(result)); |
| 116 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 117 | } |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 118 | } |