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
SkBitmapRegionCodec(SkAndroidCodec * codec)14 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
15 : INHERITED(codec->getInfo().width(), codec->getInfo().height())
16 , fCodec(codec)
17 {}
18
decodeRegion(SkBitmap * bitmap,SkBRDAllocator * allocator,const SkIRect & desiredSubset,int sampleSize,SkColorType prefColorType,bool requireUnpremul)19 bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
20 const SkIRect& desiredSubset, int sampleSize, SkColorType prefColorType,
21 bool requireUnpremul) {
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 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
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.
87 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width());
88 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height());
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);
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 }
102 bitmap->setInfo(outInfo);
103 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) {
104 SkCodecPrintf("Error: Could not allocate pixels.\n");
105 return false;
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?
112 SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
113 SkCodec::kNo_ZeroInitialized;
114 if (SubsetType::kPartiallyInside_SubsetType == type &&
115 SkCodec::kNo_ZeroInitialized == zeroInit) {
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;
127 options.fZeroInitialized = zeroInit;
128 void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
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();
137 bitmap->lockPixels();
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");
141 return false;
142 }
143
144 return true;
145 }
146
conversionSupported(SkColorType colorType)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 }
153