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, |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 21 | bool requireUnpremul) { |
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); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 55 | SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(), |
| 56 | dstColorType, dstAlphaType); |
| 57 | |
| 58 | // Construct a color table for the decode if necessary |
| 59 | SkAutoTUnref<SkColorTable> colorTable(nullptr); |
| 60 | SkPMColor* colorPtr = nullptr; |
| 61 | int* colorCountPtr = nullptr; |
| 62 | int maxColors = 256; |
| 63 | SkPMColor colors[256]; |
| 64 | if (kIndex_8_SkColorType == dstColorType) { |
| 65 | // TODO (msarett): This performs a copy that is unnecessary since |
| 66 | // we have not yet initialized the color table. |
| 67 | // And then we need to use a const cast to get |
| 68 | // a pointer to the color table that we can |
| 69 | // modify during the decode. We could alternatively |
| 70 | // perform the decode before creating the bitmap and |
| 71 | // the color table. We still would need to copy the |
| 72 | // colors into the color table after the decode. |
| 73 | colorTable.reset(new SkColorTable(colors, maxColors)); |
| 74 | colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); |
| 75 | colorCountPtr = &maxColors; |
| 76 | } |
| 77 | |
| 78 | // Initialize the destination bitmap |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 79 | int scaledOutX = 0; |
| 80 | int scaledOutY = 0; |
| 81 | int scaledOutWidth = scaledSize.width(); |
| 82 | int scaledOutHeight = scaledSize.height(); |
| 83 | if (SubsetType::kPartiallyInside_SubsetType == type) { |
| 84 | scaledOutX = outX / sampleSize; |
| 85 | scaledOutY = outY / sampleSize; |
| 86 | // We need to be safe here because getSupportedSubset() may have modified the subset. |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 87 | const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width()); |
| 88 | const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height()); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 89 | const int scaledExtraX = extraX / sampleSize; |
| 90 | const int scaledExtraY = extraY / sampleSize; |
| 91 | scaledOutWidth += scaledOutX + scaledExtraX; |
| 92 | scaledOutHeight += scaledOutY + scaledExtraY; |
| 93 | } |
| 94 | SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 95 | if (kGray_8_SkColorType == dstColorType) { |
| 96 | // The legacy implementations of BitmapFactory and BitmapRegionDecoder |
| 97 | // used kAlpha8 for grayscale images (before kGray8 existed). While |
| 98 | // the codec recognizes kGray8, we need to decode into a kAlpha8 |
| 99 | // bitmap in order to avoid a behavior change. |
| 100 | outInfo = SkImageInfo::MakeA8(scaledOutWidth, scaledOutHeight); |
| 101 | } |
msarett | fcff08c | 2015-11-05 15:00:56 -0800 | [diff] [blame] | 102 | bitmap->setInfo(outInfo); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 103 | if (!bitmap->tryAllocPixels(allocator, colorTable.get())) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 104 | SkCodecPrintf("Error: Could not allocate pixels.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 105 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Zero the bitmap if the region is not completely within the image. |
| 109 | // TODO (msarett): Can we make this faster by implementing it to only |
| 110 | // zero parts of the image that we won't overwrite with |
| 111 | // pixels? |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 112 | SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() : |
| 113 | SkCodec::kNo_ZeroInitialized; |
| 114 | if (SubsetType::kPartiallyInside_SubsetType == type && |
| 115 | SkCodec::kNo_ZeroInitialized == zeroInit) { |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 116 | void* pixels = bitmap->getPixels(); |
| 117 | size_t bytes = outInfo.getSafeSize(bitmap->rowBytes()); |
| 118 | memset(pixels, 0, bytes); |
| 119 | } |
| 120 | |
| 121 | // Decode into the destination bitmap |
| 122 | SkAndroidCodec::AndroidOptions options; |
| 123 | options.fSampleSize = sampleSize; |
| 124 | options.fSubset = ⊂ |
| 125 | options.fColorPtr = colorPtr; |
| 126 | options.fColorCount = colorCountPtr; |
msarett | cb8d719 | 2015-11-11 13:30:43 -0800 | [diff] [blame] | 127 | options.fZeroInitialized = zeroInit; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 128 | void* dst = bitmap->getAddr(scaledOutX, scaledOutY); |
msarett | fcff08c | 2015-11-05 15:00:56 -0800 | [diff] [blame] | 129 | |
| 130 | // FIXME: skbug.com/4538 |
| 131 | // It is important that we use the rowBytes on the pixelRef. They may not be |
| 132 | // set properly on the bitmap. |
| 133 | SkPixelRef* pr = SkRef(bitmap->pixelRef()); |
| 134 | size_t rowBytes = pr->rowBytes(); |
| 135 | bitmap->setInfo(outInfo, rowBytes); |
| 136 | bitmap->setPixelRef(pr)->unref(); |
msarett | 7fdda60 | 2015-11-13 05:56:27 -0800 | [diff] [blame] | 137 | bitmap->lockPixels(); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 138 | SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options); |
| 139 | if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) { |
| 140 | SkCodecPrintf("Error: Could not get pixels.\n"); |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 141 | return false; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 142 | } |
| 143 | |
msarett | 35e5d1b | 2015-10-27 12:50:25 -0700 | [diff] [blame] | 144 | return true; |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) { |
| 148 | // FIXME: Call virtual function when it lands. |
| 149 | SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alphaType(), |
| 150 | fCodec->getInfo().profileType()); |
| 151 | return conversion_possible(info, fCodec->getInfo()); |
| 152 | } |