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 "SkBitmapRegionCanvas.h" | 8 #include "SkBitmapRegionCanvas.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkCodecPriv.h" |
| 11 #include "SkCodecTools.h" |
10 | 12 |
11 SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder) | 13 SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder) |
12 : INHERITED(decoder->getInfo().width(), decoder->getInfo().height()) | 14 : INHERITED(decoder->getInfo().width(), decoder->getInfo().height()) |
13 , fDecoder(decoder) | 15 , fDecoder(decoder) |
14 {} | 16 {} |
15 | 17 |
16 /* | 18 /* |
17 * Chooses the correct image subset offsets and dimensions for the partial decod
e. | 19 * Chooses the correct image subset offsets and dimensions for the partial decod
e. |
| 20 * |
| 21 * @return true if the subset is completely contained within the image |
| 22 * false otherwise |
18 */ | 23 */ |
19 static inline void set_subset_region(int inputOffset, int inputDimension, | 24 static bool set_subset_region(int inputOffset, int inputDimension, |
20 int imageOriginalDimension, int* imageSubsetOffset, int* outOffset, | 25 int imageOriginalDimension, int* imageSubsetOffset, int* outOffset, |
21 int* imageSubsetDimension) { | 26 int* imageSubsetDimension) { |
22 | 27 |
23 // This must be at least zero, we can't start decoding the image at a negati
ve coordinate. | 28 // This must be at least zero, we can't start decoding the image at a negati
ve coordinate. |
24 *imageSubsetOffset = SkTMax(0, inputOffset); | 29 *imageSubsetOffset = SkTMax(0, inputOffset); |
25 | 30 |
26 // If inputOffset is less than zero, we decode to an offset location in the
output bitmap. | 31 // If inputOffset is less than zero, we decode to an offset location in the
output bitmap. |
27 *outOffset = *imageSubsetOffset - inputOffset; | 32 *outOffset = *imageSubsetOffset - inputOffset; |
28 | 33 |
29 // Use imageSusetOffset to make sure we don't decode pixels past the edge of
the image. | 34 // Use imageSusetOffset to make sure we don't decode pixels past the edge of
the image. |
30 // Use outOffset to make sure we don't decode pixels past the edge of the re
gion. | 35 // Use outOffset to make sure we don't decode pixels past the edge of the re
gion. |
31 *imageSubsetDimension = SkTMin(imageOriginalDimension - *imageSubsetOffset, | 36 *imageSubsetDimension = SkTMin(imageOriginalDimension - *imageSubsetOffset, |
32 inputDimension - *outOffset); | 37 inputDimension - *outOffset); |
| 38 |
| 39 return (*outOffset == 0) && (*imageSubsetDimension == inputDimension); |
33 } | 40 } |
34 | 41 |
35 /* | 42 /* |
36 * Returns a scaled dimension based on the original dimension and the sample siz
e. | |
37 * TODO: Share this implementation with SkScaledCodec. | |
38 */ | |
39 static int get_scaled_dimension(int srcDimension, int sampleSize) { | |
40 if (sampleSize > srcDimension) { | |
41 return 1; | |
42 } | |
43 return srcDimension / sampleSize; | |
44 } | |
45 | |
46 /* | |
47 * Three differences from the Android version: | 43 * Three differences from the Android version: |
48 * Returns a Skia bitmap instead of an Android bitmap. | 44 * Returns a Skia bitmap instead of an Android bitmap. |
49 * Android version attempts to reuse a recycled bitmap. | 45 * Android version attempts to reuse a recycled bitmap. |
50 * Removed the options object and used parameters for color type and | 46 * Removed the options object and used parameters for color type and |
51 * sample size. | 47 * sample size. |
52 */ | 48 */ |
53 SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, | 49 SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY, |
54 int inputWidth, int inputHeight, | 50 int inputWidth, int inputHeight, |
55 int sampleSize, | 51 int sampleSize, |
56 SkColorType dstColorType) { | 52 SkColorType dstColorType) { |
57 // Reject color types not supported by this method | 53 // Reject color types not supported by this method |
58 if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorT
ype) { | 54 if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorT
ype) { |
59 SkDebugf("Error: Color type not supported.\n"); | 55 SkCodecPrintf("Error: Color type not supported.\n"); |
60 return nullptr; | 56 return nullptr; |
61 } | 57 } |
62 | 58 |
63 // The client may not necessarily request a region that is fully within | 59 // The client may not necessarily request a region that is fully within |
64 // the image. We may need to do some calculation to determine what part | 60 // the image. We may need to do some calculation to determine what part |
65 // of the image to decode. | 61 // of the image to decode. |
66 | 62 |
67 // The left offset of the portion of the image we want, where zero | 63 // The left offset of the portion of the image we want, where zero |
68 // indicates the left edge of the image. | 64 // indicates the left edge of the image. |
69 int imageSubsetX; | 65 int imageSubsetX; |
70 | 66 |
71 // The size of the output bitmap is determined by the size of the | 67 // The size of the output bitmap is determined by the size of the |
72 // requested region, not by the size of the intersection of the region | 68 // requested region, not by the size of the intersection of the region |
73 // and the image dimensions. If inputX is negative, we will need to | 69 // and the image dimensions. If inputX is negative, we will need to |
74 // place decoded pixels into the output bitmap starting at a left offset. | 70 // place decoded pixels into the output bitmap starting at a left offset. |
75 // If this is non-zero, imageSubsetX must be zero. | 71 // If this is non-zero, imageSubsetX must be zero. |
76 int outX; | 72 int outX; |
77 | 73 |
78 // The width of the portion of the image that we will write to the output | 74 // The width of the portion of the image that we will write to the output |
79 // bitmap. If the region is not fully contained within the image, this | 75 // bitmap. If the region is not fully contained within the image, this |
80 // will not be the same as inputWidth. | 76 // will not be the same as inputWidth. |
81 int imageSubsetWidth; | 77 int imageSubsetWidth; |
82 set_subset_region(inputX, inputWidth, this->width(), &imageSubsetX, &outX, &
imageSubsetWidth); | 78 bool imageContainsEntireSubset = set_subset_region(inputX, inputWidth, this-
>width(), |
| 79 &imageSubsetX, &outX, &imageSubsetWidth); |
83 | 80 |
84 // The top offset of the portion of the image we want, where zero | 81 // The top offset of the portion of the image we want, where zero |
85 // indicates the top edge of the image. | 82 // indicates the top edge of the image. |
86 int imageSubsetY; | 83 int imageSubsetY; |
87 | 84 |
88 // The size of the output bitmap is determined by the size of the | 85 // The size of the output bitmap is determined by the size of the |
89 // requested region, not by the size of the intersection of the region | 86 // requested region, not by the size of the intersection of the region |
90 // and the image dimensions. If inputY is negative, we will need to | 87 // and the image dimensions. If inputY is negative, we will need to |
91 // place decoded pixels into the output bitmap starting at a top offset. | 88 // place decoded pixels into the output bitmap starting at a top offset. |
92 // If this is non-zero, imageSubsetY must be zero. | 89 // If this is non-zero, imageSubsetY must be zero. |
93 int outY; | 90 int outY; |
94 | 91 |
95 // The height of the portion of the image that we will write to the output | 92 // The height of the portion of the image that we will write to the output |
96 // bitmap. If the region is not fully contained within the image, this | 93 // bitmap. If the region is not fully contained within the image, this |
97 // will not be the same as inputHeight. | 94 // will not be the same as inputHeight. |
98 int imageSubsetHeight; | 95 int imageSubsetHeight; |
99 set_subset_region(inputY, inputHeight, this->height(), &imageSubsetY, &outY, | 96 imageContainsEntireSubset &= set_subset_region(inputY, inputHeight, this->he
ight(), |
100 &imageSubsetHeight); | 97 &imageSubsetY, &outY, &imageSubsetHeight); |
101 | 98 |
102 if (imageSubsetWidth <= 0 || imageSubsetHeight <= 0) { | 99 if (imageSubsetWidth <= 0 || imageSubsetHeight <= 0) { |
103 SkDebugf("Error: Region must intersect part of the image.\n"); | 100 SkCodecPrintf("Error: Region must intersect part of the image.\n"); |
104 return nullptr; | 101 return nullptr; |
105 } | 102 } |
106 | 103 |
107 // Create the image info for the decode | 104 // Create the image info for the decode |
108 SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType(); | 105 SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType(); |
109 if (kUnpremul_SkAlphaType == dstAlphaType) { | 106 if (kUnpremul_SkAlphaType == dstAlphaType) { |
110 dstAlphaType = kPremul_SkAlphaType; | 107 dstAlphaType = kPremul_SkAlphaType; |
111 } | 108 } |
112 SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), | 109 SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), |
113 dstColorType, dstAlphaType); | 110 dstColorType, dstAlphaType); |
114 | 111 |
115 // Start the scanline decoder | 112 // Start the scanline decoder |
116 SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo); | 113 SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo); |
117 if (SkCodec::kSuccess != r) { | 114 if (SkCodec::kSuccess != r) { |
118 SkDebugf("Error: Could not start scanline decoder.\n"); | 115 SkCodecPrintf("Error: Could not start scanline decoder.\n"); |
119 return nullptr; | 116 return nullptr; |
120 } | 117 } |
121 | 118 |
122 // Allocate a bitmap for the unscaled decode | 119 // Allocate a bitmap for the unscaled decode |
123 SkBitmap tmp; | 120 SkBitmap tmp; |
124 SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), imageSubsetHeight); | 121 SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), imageSubsetHeight); |
125 if (!tmp.tryAllocPixels(tmpInfo)) { | 122 if (!tmp.tryAllocPixels(tmpInfo)) { |
126 SkDebugf("Error: Could not allocate pixels.\n"); | 123 SkCodecPrintf("Error: Could not allocate pixels.\n"); |
127 return nullptr; | 124 return nullptr; |
128 } | 125 } |
129 | 126 |
130 // Skip the unneeded rows | 127 // Skip the unneeded rows |
131 if (!fDecoder->skipScanlines(imageSubsetY)) { | 128 if (!fDecoder->skipScanlines(imageSubsetY)) { |
132 SkDebugf("Error: Failed to skip scanlines.\n"); | 129 SkCodecPrintf("Error: Failed to skip scanlines.\n"); |
133 return nullptr; | 130 return nullptr; |
134 } | 131 } |
135 | 132 |
136 // Decode the necessary rows | 133 // Decode the necessary rows |
137 fDecoder->getScanlines(tmp.getAddr(0, 0), imageSubsetHeight, tmp.rowBytes())
; | 134 fDecoder->getScanlines(tmp.getAddr(0, 0), imageSubsetHeight, tmp.rowBytes())
; |
138 | 135 |
139 // Calculate the size of the output | 136 // Calculate the size of the output |
140 const int outWidth = get_scaled_dimension(inputWidth, sampleSize); | 137 const int outWidth = get_scaled_dimension(inputWidth, sampleSize); |
141 const int outHeight = get_scaled_dimension(inputHeight, sampleSize); | 138 const int outHeight = get_scaled_dimension(inputHeight, sampleSize); |
142 | 139 |
143 // Initialize the destination bitmap | 140 // Initialize the destination bitmap |
144 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap()); | 141 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap()); |
145 SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight); | 142 SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight); |
146 if (!bitmap->tryAllocPixels(dstInfo)) { | 143 if (!bitmap->tryAllocPixels(dstInfo)) { |
147 SkDebugf("Error: Could not allocate pixels.\n"); | 144 SkCodecPrintf("Error: Could not allocate pixels.\n"); |
148 return nullptr; | 145 return nullptr; |
149 } | 146 } |
150 | 147 |
151 // Zero the bitmap if the region is not completely within the image. | 148 // Zero the bitmap if the region is not completely within the image. |
152 // TODO (msarett): Can we make this faster by implementing it to only | 149 // TODO (msarett): Can we make this faster by implementing it to only |
153 // zero parts of the image that we won't overwrite with | 150 // zero parts of the image that we won't overwrite with |
154 // pixels? | 151 // pixels? |
155 // TODO (msarett): This could be skipped if memory is zero initialized. | 152 // TODO (msarett): This could be skipped if memory is zero initialized. |
156 // This would matter if this code is moved to Android and | 153 // This would matter if this code is moved to Android and |
157 // uses Android bitmaps. | 154 // uses Android bitmaps. |
158 if (0 != outX || 0 != outY || | 155 if (imageContainsEntireSubset) { |
159 inputX + inputWidth > this->width() || | |
160 inputY + inputHeight > this->height()) { | |
161 bitmap->eraseColor(0); | 156 bitmap->eraseColor(0); |
162 } | 157 } |
163 | 158 |
164 // Use a canvas to crop and scale to the destination bitmap | 159 // Use a canvas to crop and scale to the destination bitmap |
165 SkCanvas canvas(*bitmap); | 160 SkCanvas canvas(*bitmap); |
166 // TODO (msarett): Maybe we can take advantage of the fact that SkRect uses
floats? | 161 // TODO (msarett): Maybe we can take advantage of the fact that SkRect uses
floats? |
167 SkRect src = SkRect::MakeXYWH((SkScalar) imageSubsetX, (SkScalar) 0, | 162 SkRect src = SkRect::MakeXYWH((SkScalar) imageSubsetX, (SkScalar) 0, |
168 (SkScalar) imageSubsetWidth, (SkScalar) imageSubsetHeight); | 163 (SkScalar) imageSubsetWidth, (SkScalar) imageSubsetHeight); |
169 SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (ou
tY / sampleSize), | 164 SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (ou
tY / sampleSize), |
170 (SkScalar) get_scaled_dimension(imageSubsetWidth, sampleSize), | 165 (SkScalar) get_scaled_dimension(imageSubsetWidth, sampleSize), |
171 (SkScalar) get_scaled_dimension(imageSubsetHeight, sampleSize)); | 166 (SkScalar) get_scaled_dimension(imageSubsetHeight, sampleSize)); |
172 SkPaint paint; | 167 SkPaint paint; |
173 // Overwrite the dst with the src pixels | 168 // Overwrite the dst with the src pixels |
174 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 169 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
175 // TODO (msarett): Test multiple filter qualities. kNone is the default. | 170 // TODO (msarett): Test multiple filter qualities. kNone is the default. |
176 canvas.drawBitmapRect(tmp, src, dst, &paint); | 171 canvas.drawBitmapRect(tmp, src, dst, &paint); |
177 | 172 |
178 return bitmap.detach(); | 173 return bitmap.detach(); |
179 } | 174 } |
| 175 |
| 176 bool SkBitmapRegionCanvas::conversionSupported(SkColorType colorType) { |
| 177 // SkCanvas does not draw to these color types. |
| 178 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) { |
| 179 return false; |
| 180 } |
| 181 |
| 182 // FIXME: Call virtual function when it lands. |
| 183 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fDecoder->getInfo().al
phaType(), |
| 184 fDecoder->getInfo().profileType()); |
| 185 return conversion_possible(info, fDecoder->getInfo()); |
| 186 } |
OLD | NEW |