blob: cca26000d684bacaaa8bc52d1896daf66da4b0c0 [file] [log] [blame]
emmaleer8f4ba762015-08-14 07:44:46 -07001/*
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
msarett3d9d7a72015-10-21 10:27:10 -07008#include "SkCodec.h"
emmaleer8f4ba762015-08-14 07:44:46 -07009#include "SkCodecPriv.h"
scroggo501b7342015-11-03 07:55:11 -080010#include "SkMath.h"
msarett3d9d7a72015-10-21 10:27:10 -070011#include "SkSampledCodec.h"
msaretta4970dc2016-01-11 07:23:23 -080012#include "SkSampler.h"
scroggo565901d2015-12-10 10:44:13 -080013#include "SkTemplates.h"
emmaleer8f4ba762015-08-14 07:44:46 -070014
msarett3d9d7a72015-10-21 10:27:10 -070015SkSampledCodec::SkSampledCodec(SkCodec* codec)
msarett90c4d5f2015-12-10 13:09:24 -080016 : INHERITED(codec)
emmaleer8f4ba762015-08-14 07:44:46 -070017{}
18
scroggo501b7342015-11-03 07:55:11 -080019SkISize SkSampledCodec::accountForNativeScaling(int* sampleSizePtr, int* nativeSampleSize) const {
msarett90c4d5f2015-12-10 13:09:24 -080020 SkISize preSampledSize = this->codec()->getInfo().dimensions();
scroggo501b7342015-11-03 07:55:11 -080021 int sampleSize = *sampleSizePtr;
22 SkASSERT(sampleSize > 1);
23
24 if (nativeSampleSize) {
25 *nativeSampleSize = 1;
26 }
27
28 // Only JPEG supports native downsampling.
msarett90c4d5f2015-12-10 13:09:24 -080029 if (this->codec()->getEncodedFormat() == kJPEG_SkEncodedFormat) {
scroggo501b7342015-11-03 07:55:11 -080030 // See if libjpeg supports this scale directly
31 switch (sampleSize) {
32 case 2:
33 case 4:
34 case 8:
35 // This class does not need to do any sampling.
36 *sampleSizePtr = 1;
msarett90c4d5f2015-12-10 13:09:24 -080037 return this->codec()->getScaledDimensions(get_scale_from_sample_size(sampleSize));
scroggo501b7342015-11-03 07:55:11 -080038 default:
39 break;
40 }
41
42 // Check if sampleSize is a multiple of something libjpeg can support.
43 int remainder;
44 const int sampleSizes[] = { 8, 4, 2 };
45 for (int supportedSampleSize : sampleSizes) {
46 int actualSampleSize;
47 SkTDivMod(sampleSize, supportedSampleSize, &actualSampleSize, &remainder);
48 if (0 == remainder) {
49 float scale = get_scale_from_sample_size(supportedSampleSize);
50
msarett90c4d5f2015-12-10 13:09:24 -080051 // this->codec() will scale to this size.
52 preSampledSize = this->codec()->getScaledDimensions(scale);
scroggo501b7342015-11-03 07:55:11 -080053
54 // And then this class will sample it.
55 *sampleSizePtr = actualSampleSize;
56 if (nativeSampleSize) {
57 *nativeSampleSize = supportedSampleSize;
58 }
59 break;
60 }
61 }
62 }
63
64 return preSampledSize;
65}
66
msarett3d9d7a72015-10-21 10:27:10 -070067SkISize SkSampledCodec::onGetSampledDimensions(int sampleSize) const {
scroggo501b7342015-11-03 07:55:11 -080068 const SkISize size = this->accountForNativeScaling(&sampleSize);
69 return SkISize::Make(get_scaled_dimension(size.width(), sampleSize),
70 get_scaled_dimension(size.height(), sampleSize));
msaretta83593b2015-08-18 08:03:58 -070071}
emmaleer8f4ba762015-08-14 07:44:46 -070072
msarett3d9d7a72015-10-21 10:27:10 -070073SkCodec::Result SkSampledCodec::onGetAndroidPixels(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -080074 size_t rowBytes, const AndroidOptions& options) {
msarett3d9d7a72015-10-21 10:27:10 -070075 // Create an Options struct for the codec.
76 SkCodec::Options codecOptions;
77 codecOptions.fZeroInitialized = options.fZeroInitialized;
emmaleer8f4ba762015-08-14 07:44:46 -070078
msarett3d9d7a72015-10-21 10:27:10 -070079 SkIRect* subset = options.fSubset;
msarett90c4d5f2015-12-10 13:09:24 -080080 if (!subset || subset->size() == this->codec()->getInfo().dimensions()) {
81 if (this->codec()->dimensionsSupported(info.dimensions())) {
82 return this->codec()->getPixels(info, pixels, rowBytes, &codecOptions,
83 options.fColorPtr, options.fColorCount);
emmaleer8f4ba762015-08-14 07:44:46 -070084 }
emmaleer8f4ba762015-08-14 07:44:46 -070085
msarett3d9d7a72015-10-21 10:27:10 -070086 // If the native codec does not support the requested scale, scale by sampling.
87 return this->sampledDecode(info, pixels, rowBytes, options);
scroggoe7fc14b2015-10-02 13:14:46 -070088 }
89
msarett3d9d7a72015-10-21 10:27:10 -070090 // We are performing a subset decode.
91 int sampleSize = options.fSampleSize;
scroggo501b7342015-11-03 07:55:11 -080092 SkISize scaledSize = this->getSampledDimensions(sampleSize);
msarett90c4d5f2015-12-10 13:09:24 -080093 if (!this->codec()->dimensionsSupported(scaledSize)) {
msarett3d9d7a72015-10-21 10:27:10 -070094 // If the native codec does not support the requested scale, scale by sampling.
95 return this->sampledDecode(info, pixels, rowBytes, options);
96 }
scroggoe7fc14b2015-10-02 13:14:46 -070097
msarett3d9d7a72015-10-21 10:27:10 -070098 // Calculate the scaled subset bounds.
99 int scaledSubsetX = subset->x() / sampleSize;
100 int scaledSubsetY = subset->y() / sampleSize;
101 int scaledSubsetWidth = info.width();
102 int scaledSubsetHeight = info.height();
emmaleer8f4ba762015-08-14 07:44:46 -0700103
scroggo8e6c7ad2016-09-16 08:20:38 -0700104 const SkImageInfo scaledInfo = info.makeWH(scaledSize.width(), scaledSize.height());
105
106 {
107 // Although startScanlineDecode expects the bottom and top to match the
108 // SkImageInfo, startIncrementalDecode uses them to determine which rows to
109 // decode.
110 SkIRect incrementalSubset = SkIRect::MakeXYWH(scaledSubsetX, scaledSubsetY,
111 scaledSubsetWidth, scaledSubsetHeight);
112 codecOptions.fSubset = &incrementalSubset;
113 const SkCodec::Result startResult = this->codec()->startIncrementalDecode(
114 scaledInfo, pixels, rowBytes, &codecOptions,
115 options.fColorPtr, options.fColorCount);
116 if (SkCodec::kSuccess == startResult) {
117 int rowsDecoded;
118 const SkCodec::Result incResult = this->codec()->incrementalDecode(&rowsDecoded);
119 if (incResult == SkCodec::kSuccess) {
120 return SkCodec::kSuccess;
121 }
122 SkASSERT(SkCodec::kIncompleteInput == incResult);
123
124 // FIXME: Can zero initialized be read from SkCodec::fOptions?
125 this->codec()->fillIncompleteImage(scaledInfo, pixels, rowBytes,
126 options.fZeroInitialized, scaledSubsetHeight, rowsDecoded);
127 return SkCodec::kIncompleteInput;
128 } else if (startResult != SkCodec::kUnimplemented) {
129 return startResult;
130 }
131 // Otherwise fall down to use the old scanline decoder.
132 // codecOptions.fSubset will be reset below, so it will not continue to
133 // point to the object that is no longer on the stack.
134 }
135
msarett3d9d7a72015-10-21 10:27:10 -0700136 // Start the scanline decode.
137 SkIRect scanlineSubset = SkIRect::MakeXYWH(scaledSubsetX, 0, scaledSubsetWidth,
138 scaledSize.height());
139 codecOptions.fSubset = &scanlineSubset;
scroggo8e6c7ad2016-09-16 08:20:38 -0700140
141 SkCodec::Result result = this->codec()->startScanlineDecode(scaledInfo,
142 &codecOptions, options.fColorPtr, options.fColorCount);
msarett3d9d7a72015-10-21 10:27:10 -0700143 if (SkCodec::kSuccess != result) {
144 return result;
145 }
emmaleer8f4ba762015-08-14 07:44:46 -0700146
msarett3d9d7a72015-10-21 10:27:10 -0700147 // At this point, we are only concerned with subsetting. Either no scale was
msarett90c4d5f2015-12-10 13:09:24 -0800148 // requested, or the this->codec() is handling the scale.
scroggo8e6c7ad2016-09-16 08:20:38 -0700149 // Note that subsetting is only supported for kTopDown, so this code will not be
150 // reached for other orders.
151 SkASSERT(this->codec()->getScanlineOrder() == SkCodec::kTopDown_SkScanlineOrder);
152 if (!this->codec()->skipScanlines(scaledSubsetY)) {
153 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized,
154 scaledSubsetHeight, 0);
155 return SkCodec::kIncompleteInput;
scroggo6fb23912016-06-02 14:16:43 -0700156 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700157
158 int decodedLines = this->codec()->getScanlines(pixels, scaledSubsetHeight, rowBytes);
159 if (decodedLines != scaledSubsetHeight) {
160 return SkCodec::kIncompleteInput;
161 }
162 return SkCodec::kSuccess;
emmaleer8f4ba762015-08-14 07:44:46 -0700163}
164
emmaleer8f4ba762015-08-14 07:44:46 -0700165
msarett3d9d7a72015-10-21 10:27:10 -0700166SkCodec::Result SkSampledCodec::sampledDecode(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -0800167 size_t rowBytes, const AndroidOptions& options) {
scroggo501b7342015-11-03 07:55:11 -0800168 // We should only call this function when sampling.
169 SkASSERT(options.fSampleSize > 1);
170
msarett3d9d7a72015-10-21 10:27:10 -0700171 // Create options struct for the codec.
172 SkCodec::Options sampledOptions;
173 sampledOptions.fZeroInitialized = options.fZeroInitialized;
emmaleer8f4ba762015-08-14 07:44:46 -0700174
scroggo501b7342015-11-03 07:55:11 -0800175 // FIXME: This was already called by onGetAndroidPixels. Can we reduce that?
176 int sampleSize = options.fSampleSize;
177 int nativeSampleSize;
178 SkISize nativeSize = this->accountForNativeScaling(&sampleSize, &nativeSampleSize);
179
msarett3d9d7a72015-10-21 10:27:10 -0700180 // Check if there is a subset.
181 SkIRect subset;
182 int subsetY = 0;
scroggo501b7342015-11-03 07:55:11 -0800183 int subsetWidth = nativeSize.width();
184 int subsetHeight = nativeSize.height();
emmaleer8f4ba762015-08-14 07:44:46 -0700185 if (options.fSubset) {
msarett3d9d7a72015-10-21 10:27:10 -0700186 // We will need to know about subsetting in the y-dimension in order to use the
187 // scanline decoder.
msarett90c4d5f2015-12-10 13:09:24 -0800188 // Update the subset to account for scaling done by this->codec().
msarett3d9d7a72015-10-21 10:27:10 -0700189 SkIRect* subsetPtr = options.fSubset;
scroggo501b7342015-11-03 07:55:11 -0800190
191 // Do the divide ourselves, instead of calling get_scaled_dimension. If
192 // X and Y are 0, they should remain 0, rather than being upgraded to 1
193 // due to being smaller than the sampleSize.
194 const int subsetX = subsetPtr->x() / nativeSampleSize;
195 subsetY = subsetPtr->y() / nativeSampleSize;
196
197 subsetWidth = get_scaled_dimension(subsetPtr->width(), nativeSampleSize);
198 subsetHeight = get_scaled_dimension(subsetPtr->height(), nativeSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700199
200 // The scanline decoder only needs to be aware of subsetting in the x-dimension.
scroggo501b7342015-11-03 07:55:11 -0800201 subset.setXYWH(subsetX, 0, subsetWidth, nativeSize.height());
msarett3d9d7a72015-10-21 10:27:10 -0700202 sampledOptions.fSubset = ⊂
emmaleer8f4ba762015-08-14 07:44:46 -0700203 }
204
scroggo8e6c7ad2016-09-16 08:20:38 -0700205 // Since we guarantee that output dimensions are always at least one (even if the sampleSize
206 // is greater than a given dimension), the input sampleSize is not always the sampleSize that
207 // we use in practice.
208 const int sampleX = subsetWidth / info.width();
209 const int sampleY = subsetHeight / info.height();
210
211 const int samplingOffsetY = get_start_coord(sampleY);
212 const int startY = samplingOffsetY + subsetY;
213 int dstHeight = info.height();
214
215 const SkImageInfo nativeInfo = info.makeWH(nativeSize.width(), nativeSize.height());
216
217 {
218 // Although startScanlineDecode expects the bottom and top to match the
219 // SkImageInfo, startIncrementalDecode uses them to determine which rows to
220 // decode.
221 // Note: We *could* use "subsetY" and "subsetHeight" (calculated above) for
222 // incrementalSubset, but this code gives us a tighter bounds on the subset,
223 // meaning that we can start with the first row actually needed by the output,
224 // and stop when we've decoded the last row needed by the output.
225 SkIRect incrementalSubset;
226 incrementalSubset.fTop = startY;
227 incrementalSubset.fBottom = startY + (dstHeight - 1) * sampleY + 1;
228 if (sampledOptions.fSubset) {
229 incrementalSubset.fLeft = sampledOptions.fSubset->fLeft;
230 incrementalSubset.fRight = sampledOptions.fSubset->fRight;
231 } else {
232 incrementalSubset.fLeft = 0;
233 incrementalSubset.fRight = nativeSize.width();
234 }
235 SkCodec::Options incrementalOptions = sampledOptions;
236 incrementalOptions.fSubset = &incrementalSubset;
237 const SkCodec::Result startResult = this->codec()->startIncrementalDecode(nativeInfo,
238 pixels, rowBytes, &incrementalOptions, options.fColorPtr, options.fColorCount);
239 if (SkCodec::kSuccess == startResult) {
240 SkSampler* sampler = this->codec()->getSampler(true);
241 if (!sampler) {
242 return SkCodec::kUnimplemented;
243 }
244
245 if (sampler->setSampleX(sampleX) != info.width()) {
246 return SkCodec::kInvalidScale;
247 }
248 if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) {
249 return SkCodec::kInvalidScale;
250 }
251
252 sampler->setSampleY(sampleY);
253
254 int rowsDecoded;
255 const SkCodec::Result incResult = this->codec()->incrementalDecode(&rowsDecoded);
256 if (incResult == SkCodec::kSuccess) {
257 return SkCodec::kSuccess;
258 }
259 SkASSERT(incResult == SkCodec::kIncompleteInput);
msarettc6c81e12016-09-16 14:52:07 -0700260
261 // Count the rows that we decoded, and also did not skip.
262 const int trueRowsDecoded = (rowsDecoded + sampleY - 1) / sampleY;
scroggo8e6c7ad2016-09-16 08:20:38 -0700263 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized,
msarettc6c81e12016-09-16 14:52:07 -0700264 info.height(), trueRowsDecoded);
scroggo8e6c7ad2016-09-16 08:20:38 -0700265 return SkCodec::kIncompleteInput;
266 } else if (startResult != SkCodec::kUnimplemented) {
267 return startResult;
268 } // kUnimplemented means use the old method.
269 }
270
msarett3d9d7a72015-10-21 10:27:10 -0700271 // Start the scanline decode.
scroggo8e6c7ad2016-09-16 08:20:38 -0700272 SkCodec::Result result = this->codec()->startScanlineDecode(nativeInfo,
273 &sampledOptions, options.fColorPtr, options.fColorCount);
msarett3d9d7a72015-10-21 10:27:10 -0700274 if (SkCodec::kSuccess != result) {
emmaleer8f4ba762015-08-14 07:44:46 -0700275 return result;
276 }
emmaleer8f4ba762015-08-14 07:44:46 -0700277
msarett90c4d5f2015-12-10 13:09:24 -0800278 SkSampler* sampler = this->codec()->getSampler(true);
scroggoe7fc14b2015-10-02 13:14:46 -0700279 if (!sampler) {
msarett3d9d7a72015-10-21 10:27:10 -0700280 return SkCodec::kUnimplemented;
scroggoe7fc14b2015-10-02 13:14:46 -0700281 }
282
msarett3d9d7a72015-10-21 10:27:10 -0700283 if (sampler->setSampleX(sampleX) != info.width()) {
284 return SkCodec::kInvalidScale;
285 }
286 if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) {
287 return SkCodec::kInvalidScale;
scroggoe7fc14b2015-10-02 13:14:46 -0700288 }
289
msarett90c4d5f2015-12-10 13:09:24 -0800290 switch(this->codec()->getScanlineOrder()) {
scroggo46c57472015-09-30 08:57:13 -0700291 case SkCodec::kTopDown_SkScanlineOrder: {
msarett90c4d5f2015-12-10 13:09:24 -0800292 if (!this->codec()->skipScanlines(startY)) {
293 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized,
msarett3d9d7a72015-10-21 10:27:10 -0700294 dstHeight, 0);
295 return SkCodec::kIncompleteInput;
msarett5406d6f2015-08-31 06:55:13 -0700296 }
msarett3d9d7a72015-10-21 10:27:10 -0700297 void* pixelPtr = pixels;
msarett5406d6f2015-08-31 06:55:13 -0700298 for (int y = 0; y < dstHeight; y++) {
msarett90c4d5f2015-12-10 13:09:24 -0800299 if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) {
300 this->codec()->fillIncompleteImage(info, pixels, rowBytes,
301 options.fZeroInitialized, dstHeight, y + 1);
msarett3d9d7a72015-10-21 10:27:10 -0700302 return SkCodec::kIncompleteInput;
msarett5406d6f2015-08-31 06:55:13 -0700303 }
msarett691ad762015-11-05 11:19:29 -0800304 if (y < dstHeight - 1) {
msarett90c4d5f2015-12-10 13:09:24 -0800305 if (!this->codec()->skipScanlines(sampleY - 1)) {
306 this->codec()->fillIncompleteImage(info, pixels, rowBytes,
msarett691ad762015-11-05 11:19:29 -0800307 options.fZeroInitialized, dstHeight, y + 1);
308 return SkCodec::kIncompleteInput;
309 }
msarett5406d6f2015-08-31 06:55:13 -0700310 }
msarett3d9d7a72015-10-21 10:27:10 -0700311 pixelPtr = SkTAddOffset<void>(pixelPtr, rowBytes);
msarett5406d6f2015-08-31 06:55:13 -0700312 }
msarett3d9d7a72015-10-21 10:27:10 -0700313 return SkCodec::kSuccess;
emmaleer8f4ba762015-08-14 07:44:46 -0700314 }
msarett5af4e0b2015-11-17 11:18:03 -0800315 case SkCodec::kOutOfOrder_SkScanlineOrder:
msarett887e18e2015-11-17 08:46:02 -0800316 case SkCodec::kBottomUp_SkScanlineOrder: {
msarett5af4e0b2015-11-17 11:18:03 -0800317 // Note that these modes do not support subsetting.
msarett887e18e2015-11-17 08:46:02 -0800318 SkASSERT(0 == subsetY && nativeSize.height() == subsetHeight);
319 int y;
320 for (y = 0; y < nativeSize.height(); y++) {
msarett90c4d5f2015-12-10 13:09:24 -0800321 int srcY = this->codec()->nextScanline();
msarett887e18e2015-11-17 08:46:02 -0800322 if (is_coord_necessary(srcY, sampleY, dstHeight)) {
323 void* pixelPtr = SkTAddOffset<void>(pixels,
324 rowBytes * get_dst_coord(srcY, sampleY));
msarett90c4d5f2015-12-10 13:09:24 -0800325 if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) {
msarett887e18e2015-11-17 08:46:02 -0800326 break;
327 }
328 } else {
msarett90c4d5f2015-12-10 13:09:24 -0800329 if (!this->codec()->skipScanlines(1)) {
msarett887e18e2015-11-17 08:46:02 -0800330 break;
331 }
332 }
333 }
334
335 if (nativeSize.height() == y) {
336 return SkCodec::kSuccess;
337 }
338
msarett90c4d5f2015-12-10 13:09:24 -0800339 // We handle filling uninitialized memory here instead of using this->codec().
340 // this->codec() does not know that we are sampling.
msarettf7eb6fc2016-09-13 09:04:11 -0700341 const uint64_t fillValue = this->codec()->getFillValue(info);
msarett887e18e2015-11-17 08:46:02 -0800342 const SkImageInfo fillInfo = info.makeWH(info.width(), 1);
343 for (; y < nativeSize.height(); y++) {
msarett90c4d5f2015-12-10 13:09:24 -0800344 int srcY = this->codec()->outputScanline(y);
msarett887e18e2015-11-17 08:46:02 -0800345 if (!is_coord_necessary(srcY, sampleY, dstHeight)) {
346 continue;
347 }
348
349 void* rowPtr = SkTAddOffset<void>(pixels, rowBytes * get_dst_coord(srcY, sampleY));
350 SkSampler::Fill(fillInfo, rowPtr, rowBytes, fillValue, options.fZeroInitialized);
351 }
352 return SkCodec::kIncompleteInput;
353 }
msarett5406d6f2015-08-31 06:55:13 -0700354 default:
355 SkASSERT(false);
msarett3d9d7a72015-10-21 10:27:10 -0700356 return SkCodec::kUnimplemented;
emmaleer8f4ba762015-08-14 07:44:46 -0700357 }
emmaleer8f4ba762015-08-14 07:44:46 -0700358}