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