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 "SkCodec.h" | 8 #include "SkCodec.h" |
9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
10 #include "SkMath.h" | 10 #include "SkMath.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 // If the native codec does not support the requested scale, scale by sa mpling. | 94 // If the native codec does not support the requested scale, scale by sa mpling. |
95 return this->sampledDecode(info, pixels, rowBytes, options); | 95 return this->sampledDecode(info, pixels, rowBytes, options); |
96 } | 96 } |
97 | 97 |
98 // Calculate the scaled subset bounds. | 98 // Calculate the scaled subset bounds. |
99 int scaledSubsetX = subset->x() / sampleSize; | 99 int scaledSubsetX = subset->x() / sampleSize; |
100 int scaledSubsetY = subset->y() / sampleSize; | 100 int scaledSubsetY = subset->y() / sampleSize; |
101 int scaledSubsetWidth = info.width(); | 101 int scaledSubsetWidth = info.width(); |
102 int scaledSubsetHeight = info.height(); | 102 int scaledSubsetHeight = info.height(); |
103 | 103 |
104 const SkImageInfo scaledInfo = info.makeWH(scaledSize.width(), scaledSize.he ight()); | |
105 | |
106 { | |
107 // Although startScanlineDecode expects the bottom and top to match the | |
108 // SkImageInfo, startIncrementalDecode uses them to determine when to st art | |
109 // and end calling the callback. | |
110 SkIRect incrementalSubset = SkIRect::MakeXYWH(scaledSubsetX, scaledSubse tY, | |
msarett
2016/05/20 15:04:41
Here's a case where the new API makes more sense :
scroggo_chromium
2016/05/20 16:52:00
Agreed. I felt good about the new API when some of
| |
111 scaledSubsetWidth, scaledS ubsetHeight); | |
112 codecOptions.fSubset = &incrementalSubset; | |
113 const SkCodec::Result startResult = this->codec()->startIncrementalDecod e( | |
114 scaledInfo, &codecOptions, options.fColorPtr, options.fColorCoun t); | |
115 if (SkCodec::kSuccess == startResult) { | |
116 int rowsDecoded; | |
117 const SkCodec::Result incrementalResult = this->codec()->incremental Decode( | |
118 [pixels, scaledSubsetY, rowBytes](int rowNum) -> void* { | |
119 // No sampling, so this calculation is simple. | |
120 return SkTAddOffset<void>(pixels, (rowNum - scaledSubsetY) * rowBytes); | |
121 }, &rowsDecoded); | |
122 if (incrementalResult == SkCodec::kSuccess) { | |
123 return SkCodec::kSuccess; | |
124 } | |
125 SkASSERT(SkCodec::kIncompleteInput == incrementalResult); | |
126 | |
127 // FIXME: Can zero initialized be read from SkCodec::fOptions? | |
128 this->codec()->fillIncompleteImage(scaledInfo, pixels, rowBytes, opt ions.fZeroInitialized, | |
msarett
2016/05/20 15:04:41
nit: 100 chars
scroggo_chromium
2016/05/20 16:51:59
Done.
| |
129 scaledSubsetHeight, rowsDecoded); | |
130 return SkCodec::kIncompleteInput; | |
131 } else if (startResult != SkCodec::kUnimplemented) { | |
132 return startResult; | |
133 } | |
134 // Otherwise fall down to use the old scanline decoder. | |
135 // codecOptions.fSubset will be reset below, so it will not continue to | |
136 // point to the object that is no longer on the stack. | |
137 } | |
138 | |
104 // Start the scanline decode. | 139 // Start the scanline decode. |
105 SkIRect scanlineSubset = SkIRect::MakeXYWH(scaledSubsetX, 0, scaledSubsetWid th, | 140 SkIRect scanlineSubset = SkIRect::MakeXYWH(scaledSubsetX, 0, scaledSubsetWid th, |
106 scaledSize.height()); | 141 scaledSize.height()); |
107 codecOptions.fSubset = &scanlineSubset; | 142 codecOptions.fSubset = &scanlineSubset; |
108 SkCodec::Result result = this->codec()->startScanlineDecode(info.makeWH(scal edSize.width(), | 143 |
109 scaledSize.height()), &codecOptions, options.fColorPtr, options.fCol orCount); | 144 SkCodec::Result result = this->codec()->startScanlineDecode(scaledInfo, |
145 &codecOptions, options.fColorPtr, options.fColorCount); | |
110 if (SkCodec::kSuccess != result) { | 146 if (SkCodec::kSuccess != result) { |
111 return result; | 147 return result; |
112 } | 148 } |
113 | 149 |
114 // At this point, we are only concerned with subsetting. Either no scale wa s | 150 // At this point, we are only concerned with subsetting. Either no scale wa s |
115 // requested, or the this->codec() is handling the scale. | 151 // requested, or the this->codec() is handling the scale. |
116 switch (this->codec()->getScanlineOrder()) { | 152 // Note that subsetting is only supported for kTopDown, so this code will no t be |
117 case SkCodec::kTopDown_SkScanlineOrder: | 153 // reached for other orders. |
118 case SkCodec::kNone_SkScanlineOrder: { | 154 SkASSERT(this->codec()->getScanlineOrder() == SkCodec::kTopDown_SkScanlineOr der); |
119 if (!this->codec()->skipScanlines(scaledSubsetY)) { | 155 if (!this->codec()->skipScanlines(scaledSubsetY)) { |
120 this->codec()->fillIncompleteImage(info, pixels, rowBytes, optio ns.fZeroInitialized, | 156 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZero Initialized, |
121 scaledSubsetHeight, 0); | 157 scaledSubsetHeight, 0); |
122 return SkCodec::kIncompleteInput; | 158 return SkCodec::kIncompleteInput; |
123 } | 159 } |
124 | 160 |
125 int decodedLines = this->codec()->getScanlines(pixels, scaledSubsetH eight, rowBytes); | 161 int decodedLines = this->codec()->getScanlines(pixels, scaledSubsetHeight, r owBytes); |
126 if (decodedLines != scaledSubsetHeight) { | 162 if (decodedLines != scaledSubsetHeight) { |
127 return SkCodec::kIncompleteInput; | 163 return SkCodec::kIncompleteInput; |
128 } | |
129 return SkCodec::kSuccess; | |
130 } | |
131 default: | |
132 SkASSERT(false); | |
133 return SkCodec::kUnimplemented; | |
134 } | 164 } |
165 return SkCodec::kSuccess; | |
135 } | 166 } |
136 | 167 |
137 | 168 |
138 SkCodec::Result SkSampledCodec::sampledDecode(const SkImageInfo& info, void* pix els, | 169 SkCodec::Result SkSampledCodec::sampledDecode(const SkImageInfo& info, void* pix els, |
139 size_t rowBytes, const AndroidOptions& options) { | 170 size_t rowBytes, const AndroidOptions& options) { |
140 // We should only call this function when sampling. | 171 // We should only call this function when sampling. |
141 SkASSERT(options.fSampleSize > 1); | 172 SkASSERT(options.fSampleSize > 1); |
142 | 173 |
143 // Create options struct for the codec. | 174 // Create options struct for the codec. |
144 SkCodec::Options sampledOptions; | 175 SkCodec::Options sampledOptions; |
(...skipping 22 matching lines...) Expand all Loading... | |
167 subsetY = subsetPtr->y() / nativeSampleSize; | 198 subsetY = subsetPtr->y() / nativeSampleSize; |
168 | 199 |
169 subsetWidth = get_scaled_dimension(subsetPtr->width(), nativeSampleSize) ; | 200 subsetWidth = get_scaled_dimension(subsetPtr->width(), nativeSampleSize) ; |
170 subsetHeight = get_scaled_dimension(subsetPtr->height(), nativeSampleSiz e); | 201 subsetHeight = get_scaled_dimension(subsetPtr->height(), nativeSampleSiz e); |
171 | 202 |
172 // The scanline decoder only needs to be aware of subsetting in the x-di mension. | 203 // The scanline decoder only needs to be aware of subsetting in the x-di mension. |
173 subset.setXYWH(subsetX, 0, subsetWidth, nativeSize.height()); | 204 subset.setXYWH(subsetX, 0, subsetWidth, nativeSize.height()); |
174 sampledOptions.fSubset = ⊂ | 205 sampledOptions.fSubset = ⊂ |
175 } | 206 } |
176 | 207 |
208 // Since we guarantee that output dimensions are always at least one (even i f the sampleSize | |
209 // is greater than a given dimension), the input sampleSize is not always th e sampleSize that | |
210 // we use in practice. | |
211 const int sampleX = subsetWidth / info.width(); | |
212 const int sampleY = subsetHeight / info.height(); | |
213 | |
214 const int samplingOffsetY = get_start_coord(sampleY); | |
215 const int startY = samplingOffsetY + subsetY; | |
216 int dstHeight = info.height(); | |
217 | |
218 const SkImageInfo nativeInfo = info.makeWH(nativeSize.width(), nativeSize.he ight()); | |
219 | |
220 { | |
221 // Although startScanlineDecode expects the bottom and top to match the | |
222 // SkImageInfo, startIncrementalDecode uses them to determine when to st art | |
223 // and end calling the callback. | |
224 SkIRect incrementalSubset; | |
225 incrementalSubset.fTop = startY; | |
226 incrementalSubset.fBottom = startY + (dstHeight - 1) * sampleY + 1; | |
227 if (sampledOptions.fSubset) { | |
228 incrementalSubset.fLeft = sampledOptions.fSubset->fLeft; | |
229 incrementalSubset.fRight = sampledOptions.fSubset->fRight; | |
230 } else { | |
231 incrementalSubset.fLeft = 0; | |
232 incrementalSubset.fRight = nativeSize.width(); | |
233 } | |
234 SkCodec::Options incrementalOptions = sampledOptions; | |
235 incrementalOptions.fSubset = &incrementalSubset; | |
236 const SkCodec::Result startResult = this->codec()->startIncrementalDecod e(nativeInfo, | |
237 &incrementalOptions, options.fColorPtr, options.fColorCount); | |
238 if (SkCodec::kSuccess == startResult) { | |
239 SkSampler* sampler = this->codec()->getSampler(true); | |
240 if (!sampler) { | |
241 return SkCodec::kUnimplemented; | |
242 } | |
243 | |
244 if (sampler->setSampleX(sampleX) != info.width()) { | |
245 return SkCodec::kInvalidScale; | |
246 } | |
247 int rowsDecoded; | |
248 const SkCodec::Result incrementalResult = this->codec()->incremental Decode( | |
249 [pixels, rowBytes, startY, dstHeight, sampleY](int rowNum) - > void* { | |
250 SkASSERT(rowNum >= startY && rowNum <= startY + (dstHeig ht - 1) * sampleY); | |
251 if ((rowNum - startY) % sampleY != 0) { | |
252 return nullptr; | |
253 } | |
254 const int dstRow = (rowNum - startY) / sampleY; | |
255 return SkTAddOffset<void>(pixels, dstRow * rowBytes); | |
256 }, &rowsDecoded); | |
257 if (incrementalResult == SkCodec::kSuccess) { | |
258 return SkCodec::kSuccess; | |
259 } | |
260 SkASSERT(incrementalResult == SkCodec::kIncompleteInput); | |
261 // This uses the same formula as the callback uses to compute dstRow , but | |
262 // it counts on the divide to truncate. | |
263 const int lastRowInOutput = (rowsDecoded - startY) / sampleY; | |
264 // FIXME: Should this be info or nativeInfo? Does it make a differen ce? | |
msarett
2016/05/20 15:04:41
I think it's irrelevant, though the fill() code ha
scroggo_chromium
2016/05/20 16:51:59
That was my interpretation as well.
| |
265 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.f ZeroInitialized, | |
266 info.height(), lastRowInOutput); | |
267 return SkCodec::kIncompleteInput; | |
268 } else if (startResult != SkCodec::kUnimplemented) { | |
269 return startResult; | |
270 } // kUnimplemented means use the old method. | |
271 } | |
272 | |
177 // Start the scanline decode. | 273 // Start the scanline decode. |
178 SkCodec::Result result = this->codec()->startScanlineDecode( | 274 SkCodec::Result result = this->codec()->startScanlineDecode(nativeInfo, |
179 info.makeWH(nativeSize.width(), nativeSize.height()), &sampledOption s, | 275 &sampledOptions, options.fColorPtr, options.fColorCount); |
180 options.fColorPtr, options.fColorCount); | |
181 if (SkCodec::kSuccess != result) { | 276 if (SkCodec::kSuccess != result) { |
182 return result; | 277 return result; |
183 } | 278 } |
184 | 279 |
185 SkSampler* sampler = this->codec()->getSampler(true); | 280 SkSampler* sampler = this->codec()->getSampler(true); |
186 if (!sampler) { | 281 if (!sampler) { |
187 return SkCodec::kUnimplemented; | 282 return SkCodec::kUnimplemented; |
188 } | 283 } |
189 | 284 |
190 // Since we guarantee that output dimensions are always at least one (even i f the sampleSize | |
191 // is greater than a given dimension), the input sampleSize is not always th e sampleSize that | |
192 // we use in practice. | |
193 const int sampleX = subsetWidth / info.width(); | |
194 const int sampleY = subsetHeight / info.height(); | |
195 if (sampler->setSampleX(sampleX) != info.width()) { | 285 if (sampler->setSampleX(sampleX) != info.width()) { |
196 return SkCodec::kInvalidScale; | 286 return SkCodec::kInvalidScale; |
197 } | 287 } |
198 if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) { | 288 if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) { |
199 return SkCodec::kInvalidScale; | 289 return SkCodec::kInvalidScale; |
200 } | 290 } |
201 | 291 |
202 const int samplingOffsetY = get_start_coord(sampleY); | |
203 const int startY = samplingOffsetY + subsetY; | |
204 int dstHeight = info.height(); | |
205 switch(this->codec()->getScanlineOrder()) { | 292 switch(this->codec()->getScanlineOrder()) { |
206 case SkCodec::kTopDown_SkScanlineOrder: { | 293 case SkCodec::kTopDown_SkScanlineOrder: { |
207 if (!this->codec()->skipScanlines(startY)) { | 294 if (!this->codec()->skipScanlines(startY)) { |
208 this->codec()->fillIncompleteImage(info, pixels, rowBytes, optio ns.fZeroInitialized, | 295 this->codec()->fillIncompleteImage(info, pixels, rowBytes, optio ns.fZeroInitialized, |
209 dstHeight, 0); | 296 dstHeight, 0); |
210 return SkCodec::kIncompleteInput; | 297 return SkCodec::kIncompleteInput; |
211 } | 298 } |
212 void* pixelPtr = pixels; | 299 void* pixelPtr = pixels; |
213 for (int y = 0; y < dstHeight; y++) { | 300 for (int y = 0; y < dstHeight; y++) { |
214 if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) { | 301 if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 int srcY = this->codec()->outputScanline(y); | 346 int srcY = this->codec()->outputScanline(y); |
260 if (!is_coord_necessary(srcY, sampleY, dstHeight)) { | 347 if (!is_coord_necessary(srcY, sampleY, dstHeight)) { |
261 continue; | 348 continue; |
262 } | 349 } |
263 | 350 |
264 void* rowPtr = SkTAddOffset<void>(pixels, rowBytes * get_dst_coo rd(srcY, sampleY)); | 351 void* rowPtr = SkTAddOffset<void>(pixels, rowBytes * get_dst_coo rd(srcY, sampleY)); |
265 SkSampler::Fill(fillInfo, rowPtr, rowBytes, fillValue, options.f ZeroInitialized); | 352 SkSampler::Fill(fillInfo, rowPtr, rowBytes, fillValue, options.f ZeroInitialized); |
266 } | 353 } |
267 return SkCodec::kIncompleteInput; | 354 return SkCodec::kIncompleteInput; |
268 } | 355 } |
269 case SkCodec::kNone_SkScanlineOrder: { | |
270 const int linesNeeded = subsetHeight - samplingOffsetY; | |
271 SkAutoTMalloc<uint8_t> storage(linesNeeded * rowBytes); | |
272 uint8_t* storagePtr = storage.get(); | |
273 | |
274 if (!this->codec()->skipScanlines(startY)) { | |
275 this->codec()->fillIncompleteImage(info, pixels, rowBytes, optio ns.fZeroInitialized, | |
276 dstHeight, 0); | |
277 return SkCodec::kIncompleteInput; | |
278 } | |
279 int scanlines = this->codec()->getScanlines(storagePtr, linesNeeded, rowBytes); | |
280 | |
281 for (int y = 0; y < dstHeight; y++) { | |
282 memcpy(pixels, storagePtr, info.minRowBytes()); | |
283 storagePtr += sampleY * rowBytes; | |
284 pixels = SkTAddOffset<void>(pixels, rowBytes); | |
285 } | |
286 | |
287 if (scanlines < dstHeight) { | |
288 // this->codec() has already handled filling uninitialized memor y. | |
289 return SkCodec::kIncompleteInput; | |
290 } | |
291 return SkCodec::kSuccess; | |
292 } | |
293 default: | 356 default: |
294 SkASSERT(false); | 357 SkASSERT(false); |
295 return SkCodec::kUnimplemented; | 358 return SkCodec::kUnimplemented; |
296 } | 359 } |
297 } | 360 } |
OLD | NEW |