OLD | NEW |
---|---|
(Empty) | |
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 "SkBitmapRegionCodec.h" | |
9 #include "SkAndroidCodec.h" | |
10 #include "SkCodecPriv.h" | |
11 #include "SkCodecTools.h" | |
12 | |
13 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec) | |
14 : INHERITED(codec->getInfo().width(), codec->getInfo().height()) | |
15 , fCodec(codec) | |
16 {} | |
17 | |
18 /* | |
19 * Three differences from the Android version: | |
20 * Returns a skia bitmap instead of an Android bitmap. | |
21 * Android version attempts to reuse a recycled bitmap. | |
22 * Removed the options object and used parameters for color type and sample size. | |
23 */ | |
24 // FIXME: Should this function should take in SkIRect? | |
25 SkBitmap* SkBitmapRegionCodec::decodeRegion(int inputX, int inputY, int inputWid th, int inputHeight, | |
26 int sampleSize, SkColorType dstColorType) { | |
27 | |
28 // Fix the input sampleSize if necessary. | |
29 if (sampleSize < 1) { | |
30 sampleSize = 1; | |
31 } | |
32 | |
33 // The size of the output bitmap is determined by the size of the | |
34 // requested region, not by the size of the intersection of the region | |
scroggo
2015/10/21 20:56:20
nit: region -> subset (although, to be fair, this
msarett
2015/10/21 22:00:00
Done.
| |
35 // and the image dimensions. | |
36 // If inputX is negative, we will need to place decoded pixels into the | |
37 // output bitmap starting at a left offset. Call this outX. | |
38 // If outX is non-zero, subsetX must be zero. | |
39 // If inputY is negative, we will need to place decoded pixels into the | |
40 // output bitmap starting at a top offset. Call this outY. | |
41 // If outY is non-zero, subsetY must be zero. | |
42 int outX; | |
43 int outY; | |
44 SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight); | |
45 SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset , &outX, &outY); | |
46 if (SubsetType::kInvalid_SubsetType == type) { | |
47 return NULL; | |
48 } | |
49 | |
50 // Ask the codec for a scaled subset | |
51 if (!fCodec->getSupportedSubset(&subset)) { | |
52 SkCodecPrintf("Error: Could not get subset.\n"); | |
53 return nullptr; | |
54 } | |
55 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset); | |
56 | |
57 // Create the image info for the decode | |
58 SkAlphaType dstAlphaType = fCodec->getInfo().alphaType(); | |
59 if (kUnpremul_SkAlphaType == dstAlphaType) { | |
60 dstAlphaType = kPremul_SkAlphaType; | |
61 } | |
62 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.he ight(), | |
63 dstColorType, dstAlphaType); | |
64 | |
65 // Construct a color table for the decode if necessary | |
66 SkAutoTUnref<SkColorTable> colorTable(nullptr); | |
67 SkPMColor* colorPtr = nullptr; | |
68 int* colorCountPtr = nullptr; | |
69 int maxColors = 256; | |
70 SkPMColor colors[maxColors]; | |
71 if (kIndex_8_SkColorType == dstColorType) { | |
72 // TODO (msarett): This performs a copy that is unnecessary since | |
73 // we have not yet initialized the color table. | |
74 // And then we need to use a const cast to get | |
75 // a pointer to the color table that we can | |
76 // modify during the decode. Is there a better | |
77 // way to do this? Can we allocate memory to | |
78 // decode into and then create the bitmap after | |
scroggo
2015/10/21 20:56:20
We certainly can. Using an SkBitmap early on is ju
msarett
2015/10/21 22:00:00
Acknowledged.
| |
79 // the decode? Can we create an SkColorTable | |
80 // without copying the colors? | |
81 colorTable.reset(new SkColorTable(colors, maxColors)); | |
82 colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); | |
83 colorCountPtr = &maxColors; | |
84 } | |
85 | |
86 // Initialize the destination bitmap | |
87 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap()); | |
88 int scaledOutX = 0; | |
89 int scaledOutY = 0; | |
90 int scaledOutWidth = scaledSize.width(); | |
91 int scaledOutHeight = scaledSize.height(); | |
92 if (SubsetType::kPartiallyInside_SubsetType == type) { | |
93 scaledOutX = outX / sampleSize; | |
94 scaledOutY = outY / sampleSize; | |
95 // We need to be safe here because getSupportedSubset() may have modifie d the subset. | |
96 const int extraX = SkTMax(0, inputWidth - outX - subset.width()); | |
97 const int extraY = SkTMax(0, inputHeight - outY - subset.height()); | |
98 const int scaledExtraX = extraX / sampleSize; | |
99 const int scaledExtraY = extraY / sampleSize; | |
100 scaledOutWidth += scaledOutX + scaledExtraX; | |
101 scaledOutHeight += scaledOutY + scaledExtraY; | |
102 } | |
103 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); | |
104 if (!bitmap->tryAllocPixels(outInfo, nullptr, colorTable.get())) { | |
105 SkCodecPrintf("Error: Could not allocate pixels.\n"); | |
106 return nullptr; | |
107 } | |
108 | |
109 // Zero the bitmap if the region is not completely within the image. | |
110 // TODO (msarett): Can we make this faster by implementing it to only | |
111 // zero parts of the image that we won't overwrite with | |
112 // pixels? | |
113 // TODO (msarett): This could be skipped if memory is zero initialized. | |
114 // This would matter if this code is moved to Android and | |
115 // uses Android bitmaps. | |
116 if (SubsetType::kPartiallyInside_SubsetType == type) { | |
117 bitmap->eraseColor(0); | |
scroggo
2015/10/21 20:56:20
Won't this not work for kIndex_8?
msarett
2015/10/21 22:00:00
Ah yes...
Will fix.
| |
118 } | |
119 | |
120 // Decode into the destination bitmap | |
121 SkAndroidCodec::AndroidOptions options; | |
122 options.fSampleSize = sampleSize; | |
123 options.fSubset = ⊂ | |
124 options.fColorPtr = colorPtr; | |
125 options.fColorCount = colorCountPtr; | |
126 void* dst = bitmap->getAddr(scaledOutX, scaledOutY); | |
127 size_t rowBytes = bitmap->rowBytes(); | |
128 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options); | |
129 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) { | |
130 SkCodecPrintf("Error: Could not get pixels.\n"); | |
131 return nullptr; | |
132 } | |
133 | |
134 return bitmap.detach(); | |
135 } | |
136 | |
137 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) { | |
138 // FIXME: Call virtual function when it lands. | |
139 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph aType(), | |
140 fCodec->getInfo().profileType()); | |
141 return conversion_possible(info, fCodec->getInfo()); | |
142 } | |
OLD | NEW |