OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkAndroidCodec.h" | 8 #include "SkAndroidCodec.h" |
9 #include "SkBitmapRegionCodec.h" | 9 #include "SkBitmapRegionCodec.h" |
10 #include "SkBitmapRegionDecoderPriv.h" | 10 #include "SkBitmapRegionDecoderPriv.h" |
11 #include "SkCodecPriv.h" | 11 #include "SkCodecPriv.h" |
12 #include "SkPixelRef.h" | 12 #include "SkPixelRef.h" |
13 | 13 |
14 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec) | 14 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec) |
15 : INHERITED(codec->getInfo().width(), codec->getInfo().height()) | 15 : INHERITED(codec->getInfo().width(), codec->getInfo().height()) |
16 , fCodec(codec) | 16 , fCodec(codec) |
17 {} | 17 {} |
18 | 18 |
19 bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocat or, | 19 bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocat or, |
20 const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType, | 20 const SkIRect& desiredSubset, int sampleSize, SkColorType prefColorType, |
21 bool requireUnpremul) { | 21 bool requireUnpremul) { |
22 | 22 |
23 // Fix the input sampleSize if necessary. | 23 // Fix the input sampleSize if necessary. |
24 if (sampleSize < 1) { | 24 if (sampleSize < 1) { |
25 sampleSize = 1; | 25 sampleSize = 1; |
26 } | 26 } |
27 | 27 |
28 // The size of the output bitmap is determined by the size of the | 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 | 29 // requested subset, not by the size of the intersection of the subset |
30 // and the image dimensions. | 30 // and the image dimensions. |
(...skipping 12 matching lines...) Expand all Loading... | |
43 } | 43 } |
44 | 44 |
45 // Ask the codec for a scaled subset | 45 // Ask the codec for a scaled subset |
46 if (!fCodec->getSupportedSubset(&subset)) { | 46 if (!fCodec->getSupportedSubset(&subset)) { |
47 SkCodecPrintf("Error: Could not get subset.\n"); | 47 SkCodecPrintf("Error: Could not get subset.\n"); |
48 return false; | 48 return false; |
49 } | 49 } |
50 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset); | 50 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset); |
51 | 51 |
52 // Create the image info for the decode | 52 // Create the image info for the decode |
53 SkAlphaType dstAlphaType = fCodec->getInfo().alphaType(); | 53 SkColorType dstColorType = SkAndroidCodec::FixRequestedColorType(prefColorTy pe, |
54 if (kOpaque_SkAlphaType != dstAlphaType) { | 54 fCodec->getInfo().colorType(), fCodec->getInfo().alphaType()); |
55 dstAlphaType = requireUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlpha Type; | 55 SkAlphaType dstAlphaType = SkAndroidCodec::FixRequestedAlphaType(requireUnpr emul, |
56 } | 56 fCodec->getInfo().alphaType()); |
57 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.he ight(), | 57 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.he ight(), |
58 dstColorType, dstAlphaType); | 58 dstColorType, dstAlphaType); |
59 | 59 |
60 // Construct a color table for the decode if necessary | 60 // Construct a color table for the decode if necessary |
61 SkAutoTUnref<SkColorTable> colorTable(nullptr); | 61 SkAutoTUnref<SkColorTable> colorTable(nullptr); |
62 SkPMColor* colorPtr = nullptr; | 62 SkPMColor* colorPtr = nullptr; |
63 int* colorCountPtr = nullptr; | 63 int* colorCountPtr = nullptr; |
64 int maxColors = 256; | 64 int maxColors = 256; |
65 SkPMColor colors[256]; | 65 SkPMColor colors[256]; |
66 if (kIndex_8_SkColorType == dstColorType) { | 66 if (kIndex_8_SkColorType == dstColorType) { |
(...skipping 20 matching lines...) Expand all Loading... | |
87 scaledOutY = outY / sampleSize; | 87 scaledOutY = outY / sampleSize; |
88 // We need to be safe here because getSupportedSubset() may have modifie d the subset. | 88 // We need to be safe here because getSupportedSubset() may have modifie d the subset. |
89 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width ()); | 89 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width ()); |
90 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.heig ht()); | 90 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.heig ht()); |
91 const int scaledExtraX = extraX / sampleSize; | 91 const int scaledExtraX = extraX / sampleSize; |
92 const int scaledExtraY = extraY / sampleSize; | 92 const int scaledExtraY = extraY / sampleSize; |
93 scaledOutWidth += scaledOutX + scaledExtraX; | 93 scaledOutWidth += scaledOutX + scaledExtraX; |
94 scaledOutHeight += scaledOutY + scaledExtraY; | 94 scaledOutHeight += scaledOutY + scaledExtraY; |
95 } | 95 } |
96 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); | 96 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); |
97 if (kGray_8_SkColorType == dstColorType) { | |
scroggo
2015/12/09 20:57:31
Comment explaining why?
msarett
2015/12/10 15:19:26
Done.
| |
98 outInfo = SkImageInfo::MakeA8(scaledOutWidth, scaledOutHeight); | |
99 } | |
97 bitmap->setInfo(outInfo); | 100 bitmap->setInfo(outInfo); |
98 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) { | 101 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) { |
99 SkCodecPrintf("Error: Could not allocate pixels.\n"); | 102 SkCodecPrintf("Error: Could not allocate pixels.\n"); |
100 return false; | 103 return false; |
101 } | 104 } |
102 | 105 |
103 // Zero the bitmap if the region is not completely within the image. | 106 // Zero the bitmap if the region is not completely within the image. |
104 // TODO (msarett): Can we make this faster by implementing it to only | 107 // TODO (msarett): Can we make this faster by implementing it to only |
105 // zero parts of the image that we won't overwrite with | 108 // zero parts of the image that we won't overwrite with |
106 // pixels? | 109 // pixels? |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 | 141 |
139 return true; | 142 return true; |
140 } | 143 } |
141 | 144 |
142 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) { | 145 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) { |
143 // FIXME: Call virtual function when it lands. | 146 // FIXME: Call virtual function when it lands. |
144 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph aType(), | 147 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph aType(), |
145 fCodec->getInfo().profileType()); | 148 fCodec->getInfo().profileType()); |
146 return conversion_possible(info, fCodec->getInfo()); | 149 return conversion_possible(info, fCodec->getInfo()); |
147 } | 150 } |
OLD | NEW |