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 8 #include "SkAndroidCodec.h" 9 #include "SkBitmapRegionCodec.h" 10 #include "SkBitmapRegionDecoderPriv.h" 11 #include "SkCodecPriv.h" 12 #include "SkPixelRef.h" 13 14 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec) 15 : INHERITED(codec->getInfo().width(), codec->getInfo().height()) 16 , fCodec(codec) 17 {} 18 19 bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator, 20 const SkIRect& desiredSubset, int sampleSize, SkColorType prefColorType, 21 bool requireUnpremul, sk_sp<SkColorSpace> prefColorSpace) { 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; 39 SkIRect subset = desiredSubset; 40 SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY); 41 if (SubsetType::kOutside_SubsetType == type) { 42 return false; 43 } 44 45 // Ask the codec for a scaled subset 46 if (!fCodec->getSupportedSubset(&subset)) { 47 SkCodecPrintf("Error: Could not get subset.\n"); 48 return false; 49 } 50 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset); 51 52 // Create the image info for the decode 53 SkColorType dstColorType = fCodec->computeOutputColorType(prefColorType); 54 SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul); 55 sk_sp<SkColorSpace> dstColorSpace = fCodec->computeOutputColorSpace(dstColorType, 56 prefColorSpace); 57 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(), 58 dstColorType, dstAlphaType, dstColorSpace); 59 60 // Initialize the destination bitmap 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. 69 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width()); 70 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height()); 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); 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. 82 outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType); 83 } 84 bitmap->setInfo(outInfo); 85 if (!bitmap->tryAllocPixels(allocator)) { 86 SkCodecPrintf("Error: Could not allocate pixels.\n"); 87 return false; 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? 94 SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() : 95 SkCodec::kNo_ZeroInitialized; 96 if (SubsetType::kPartiallyInside_SubsetType == type && 97 SkCodec::kNo_ZeroInitialized == zeroInit) { 98 void* pixels = bitmap->getPixels(); 99 size_t bytes = outInfo.getSafeSize(bitmap->rowBytes()); 100 memset(pixels, 0, bytes); 101 } 102 103 // Decode into the destination bitmap 104 SkAndroidCodec::AndroidOptions options; 105 options.fSampleSize = sampleSize; 106 options.fSubset = ⊂ 107 options.fZeroInitialized = zeroInit; 108 void* dst = bitmap->getAddr(scaledOutX, scaledOutY); 109 110 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(), 111 &options); 112 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) { 113 SkCodecPrintf("Error: Could not get pixels.\n"); 114 return false; 115 } 116 117 return true; 118 } 119 120 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) { 121 SkImageInfo dstInfo = fCodec->getInfo().makeColorType(colorType); 122 return conversion_possible(dstInfo, fCodec->getInfo()); 123 } 124