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