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 dstColorType, 21 bool requireUnpremul, sk_sp<SkColorSpace> dstColorSpace) { 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 SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul); 54 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(), 55 dstColorType, dstAlphaType, dstColorSpace); 56 57 // Initialize the destination bitmap 58 int scaledOutX = 0; 59 int scaledOutY = 0; 60 int scaledOutWidth = scaledSize.width(); 61 int scaledOutHeight = scaledSize.height(); 62 if (SubsetType::kPartiallyInside_SubsetType == type) { 63 scaledOutX = outX / sampleSize; 64 scaledOutY = outY / sampleSize; 65 // We need to be safe here because getSupportedSubset() may have modified the subset. 66 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width()); 67 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height()); 68 const int scaledExtraX = extraX / sampleSize; 69 const int scaledExtraY = extraY / sampleSize; 70 scaledOutWidth += scaledOutX + scaledExtraX; 71 scaledOutHeight += scaledOutY + scaledExtraY; 72 } 73 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); 74 if (kGray_8_SkColorType == dstColorType) { 75 // The legacy implementations of BitmapFactory and BitmapRegionDecoder 76 // used kAlpha8 for grayscale images (before kGray8 existed). While 77 // the codec recognizes kGray8, we need to decode into a kAlpha8 78 // bitmap in order to avoid a behavior change. 79 outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType); 80 } 81 bitmap->setInfo(outInfo); 82 if (!bitmap->tryAllocPixels(allocator)) { 83 SkCodecPrintf("Error: Could not allocate pixels.\n"); 84 return false; 85 } 86 87 // Zero the bitmap if the region is not completely within the image. 88 // TODO (msarett): Can we make this faster by implementing it to only 89 // zero parts of the image that we won't overwrite with 90 // pixels? 91 SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() : 92 SkCodec::kNo_ZeroInitialized; 93 if (SubsetType::kPartiallyInside_SubsetType == type && 94 SkCodec::kNo_ZeroInitialized == zeroInit) { 95 void* pixels = bitmap->getPixels(); 96 size_t bytes = outInfo.computeByteSize(bitmap->rowBytes()); 97 memset(pixels, 0, bytes); 98 } 99 100 // Decode into the destination bitmap 101 SkAndroidCodec::AndroidOptions options; 102 options.fSampleSize = sampleSize; 103 options.fSubset = ⊂ 104 options.fZeroInitialized = zeroInit; 105 void* dst = bitmap->getAddr(scaledOutX, scaledOutY); 106 107 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(), 108 &options); 109 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) { 110 SkCodecPrintf("Error: Could not get pixels.\n"); 111 return false; 112 } 113 114 return true; 115 } 116