blob: 3876d6f1cf46db99bb9bbb65d9df57642e081b3d [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"
Hal Canary2d404902018-09-06 11:20:23 -040011#include "SkMathPriv.h"
msarett3d9d7a72015-10-21 10:27:10 -070012#include "SkSampledCodec.h"
msaretta4970dc2016-01-11 07:23:23 -080013#include "SkSampler.h"
scroggo565901d2015-12-10 10:44:13 -080014#include "SkTemplates.h"
emmaleer8f4ba762015-08-14 07:44:46 -070015
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050016SkSampledCodec::SkSampledCodec(SkCodec* codec, ExifOrientationBehavior behavior)
17 : INHERITED(codec, behavior)
emmaleer8f4ba762015-08-14 07:44:46 -070018{}
19
scroggo501b7342015-11-03 07:55:11 -080020SkISize SkSampledCodec::accountForNativeScaling(int* sampleSizePtr, int* nativeSampleSize) const {
Leon Scroggins III712476e2018-10-03 15:47:00 -040021 SkISize preSampledSize = this->codec()->dimensions();
scroggo501b7342015-11-03 07:55:11 -080022 int sampleSize = *sampleSizePtr;
23 SkASSERT(sampleSize > 1);
24
25 if (nativeSampleSize) {
26 *nativeSampleSize = 1;
27 }
28
29 // Only JPEG supports native downsampling.
Hal Canarydb683012016-11-23 08:55:18 -070030 if (this->codec()->getEncodedFormat() == SkEncodedImageFormat::kJPEG) {
scroggo501b7342015-11-03 07:55:11 -080031 // See if libjpeg supports this scale directly
32 switch (sampleSize) {
33 case 2:
34 case 4:
35 case 8:
36 // This class does not need to do any sampling.
37 *sampleSizePtr = 1;
msarett90c4d5f2015-12-10 13:09:24 -080038 return this->codec()->getScaledDimensions(get_scale_from_sample_size(sampleSize));
scroggo501b7342015-11-03 07:55:11 -080039 default:
40 break;
41 }
42
43 // Check if sampleSize is a multiple of something libjpeg can support.
44 int remainder;
45 const int sampleSizes[] = { 8, 4, 2 };
46 for (int supportedSampleSize : sampleSizes) {
47 int actualSampleSize;
48 SkTDivMod(sampleSize, supportedSampleSize, &actualSampleSize, &remainder);
49 if (0 == remainder) {
50 float scale = get_scale_from_sample_size(supportedSampleSize);
51
msarett90c4d5f2015-12-10 13:09:24 -080052 // this->codec() will scale to this size.
53 preSampledSize = this->codec()->getScaledDimensions(scale);
scroggo501b7342015-11-03 07:55:11 -080054
55 // And then this class will sample it.
56 *sampleSizePtr = actualSampleSize;
57 if (nativeSampleSize) {
58 *nativeSampleSize = supportedSampleSize;
59 }
60 break;
61 }
62 }
63 }
64
65 return preSampledSize;
66}
67
msarett3d9d7a72015-10-21 10:27:10 -070068SkISize SkSampledCodec::onGetSampledDimensions(int sampleSize) const {
scroggo501b7342015-11-03 07:55:11 -080069 const SkISize size = this->accountForNativeScaling(&sampleSize);
70 return SkISize::Make(get_scaled_dimension(size.width(), sampleSize),
71 get_scaled_dimension(size.height(), sampleSize));
msaretta83593b2015-08-18 08:03:58 -070072}
emmaleer8f4ba762015-08-14 07:44:46 -070073
msarett3d9d7a72015-10-21 10:27:10 -070074SkCodec::Result SkSampledCodec::onGetAndroidPixels(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -080075 size_t rowBytes, const AndroidOptions& options) {
msarett3d9d7a72015-10-21 10:27:10 -070076 // Create an Options struct for the codec.
77 SkCodec::Options codecOptions;
78 codecOptions.fZeroInitialized = options.fZeroInitialized;
emmaleer8f4ba762015-08-14 07:44:46 -070079
msarett3d9d7a72015-10-21 10:27:10 -070080 SkIRect* subset = options.fSubset;
Leon Scroggins III712476e2018-10-03 15:47:00 -040081 if (!subset || subset->size() == this->codec()->dimensions()) {
msarett90c4d5f2015-12-10 13:09:24 -080082 if (this->codec()->dimensionsSupported(info.dimensions())) {
Leon Scroggins571b30f2017-07-11 17:35:31 +000083 return this->codec()->getPixels(info, pixels, rowBytes, &codecOptions);
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(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000114 scaledInfo, pixels, rowBytes, &codecOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700115 if (SkCodec::kSuccess == startResult) {
Leon Scroggins III6e45ce72018-10-16 15:29:11 -0400116 int rowsDecoded = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700117 const SkCodec::Result incResult = this->codec()->incrementalDecode(&rowsDecoded);
118 if (incResult == SkCodec::kSuccess) {
119 return SkCodec::kSuccess;
120 }
Leon Scroggins III6e45ce72018-10-16 15:29:11 -0400121 SkASSERT(incResult == SkCodec::kIncompleteInput || incResult == SkCodec::kErrorInInput);
scroggo8e6c7ad2016-09-16 08:20:38 -0700122
123 // FIXME: Can zero initialized be read from SkCodec::fOptions?
124 this->codec()->fillIncompleteImage(scaledInfo, pixels, rowBytes,
125 options.fZeroInitialized, scaledSubsetHeight, rowsDecoded);
Leon Scroggins III6e45ce72018-10-16 15:29:11 -0400126 return incResult;
scroggo8e6c7ad2016-09-16 08:20:38 -0700127 } else if (startResult != SkCodec::kUnimplemented) {
128 return startResult;
129 }
130 // Otherwise fall down to use the old scanline decoder.
131 // codecOptions.fSubset will be reset below, so it will not continue to
132 // point to the object that is no longer on the stack.
133 }
134
msarett3d9d7a72015-10-21 10:27:10 -0700135 // Start the scanline decode.
136 SkIRect scanlineSubset = SkIRect::MakeXYWH(scaledSubsetX, 0, scaledSubsetWidth,
137 scaledSize.height());
138 codecOptions.fSubset = &scanlineSubset;
scroggo8e6c7ad2016-09-16 08:20:38 -0700139
140 SkCodec::Result result = this->codec()->startScanlineDecode(scaledInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000141 &codecOptions);
msarett3d9d7a72015-10-21 10:27:10 -0700142 if (SkCodec::kSuccess != result) {
143 return result;
144 }
emmaleer8f4ba762015-08-14 07:44:46 -0700145
msarett3d9d7a72015-10-21 10:27:10 -0700146 // At this point, we are only concerned with subsetting. Either no scale was
msarett90c4d5f2015-12-10 13:09:24 -0800147 // requested, or the this->codec() is handling the scale.
scroggo8e6c7ad2016-09-16 08:20:38 -0700148 // Note that subsetting is only supported for kTopDown, so this code will not be
149 // reached for other orders.
150 SkASSERT(this->codec()->getScanlineOrder() == SkCodec::kTopDown_SkScanlineOrder);
151 if (!this->codec()->skipScanlines(scaledSubsetY)) {
152 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized,
153 scaledSubsetHeight, 0);
154 return SkCodec::kIncompleteInput;
scroggo6fb23912016-06-02 14:16:43 -0700155 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700156
157 int decodedLines = this->codec()->getScanlines(pixels, scaledSubsetHeight, rowBytes);
158 if (decodedLines != scaledSubsetHeight) {
159 return SkCodec::kIncompleteInput;
160 }
161 return SkCodec::kSuccess;
emmaleer8f4ba762015-08-14 07:44:46 -0700162}
163
emmaleer8f4ba762015-08-14 07:44:46 -0700164
msarett3d9d7a72015-10-21 10:27:10 -0700165SkCodec::Result SkSampledCodec::sampledDecode(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -0800166 size_t rowBytes, const AndroidOptions& options) {
scroggo501b7342015-11-03 07:55:11 -0800167 // We should only call this function when sampling.
168 SkASSERT(options.fSampleSize > 1);
169
msarett3d9d7a72015-10-21 10:27:10 -0700170 // Create options struct for the codec.
171 SkCodec::Options sampledOptions;
172 sampledOptions.fZeroInitialized = options.fZeroInitialized;
emmaleer8f4ba762015-08-14 07:44:46 -0700173
scroggo501b7342015-11-03 07:55:11 -0800174 // FIXME: This was already called by onGetAndroidPixels. Can we reduce that?
175 int sampleSize = options.fSampleSize;
176 int nativeSampleSize;
177 SkISize nativeSize = this->accountForNativeScaling(&sampleSize, &nativeSampleSize);
178
msarett3d9d7a72015-10-21 10:27:10 -0700179 // Check if there is a subset.
180 SkIRect subset;
181 int subsetY = 0;
scroggo501b7342015-11-03 07:55:11 -0800182 int subsetWidth = nativeSize.width();
183 int subsetHeight = nativeSize.height();
emmaleer8f4ba762015-08-14 07:44:46 -0700184 if (options.fSubset) {
msarett3d9d7a72015-10-21 10:27:10 -0700185 // We will need to know about subsetting in the y-dimension in order to use the
186 // scanline decoder.
msarett90c4d5f2015-12-10 13:09:24 -0800187 // Update the subset to account for scaling done by this->codec().
scroggo4f2a88c2016-10-17 14:32:41 -0700188 const SkIRect* subsetPtr = options.fSubset;
scroggo501b7342015-11-03 07:55:11 -0800189
190 // Do the divide ourselves, instead of calling get_scaled_dimension. If
191 // X and Y are 0, they should remain 0, rather than being upgraded to 1
192 // due to being smaller than the sampleSize.
193 const int subsetX = subsetPtr->x() / nativeSampleSize;
194 subsetY = subsetPtr->y() / nativeSampleSize;
195
196 subsetWidth = get_scaled_dimension(subsetPtr->width(), nativeSampleSize);
197 subsetHeight = get_scaled_dimension(subsetPtr->height(), nativeSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700198
199 // The scanline decoder only needs to be aware of subsetting in the x-dimension.
scroggo501b7342015-11-03 07:55:11 -0800200 subset.setXYWH(subsetX, 0, subsetWidth, nativeSize.height());
msarett3d9d7a72015-10-21 10:27:10 -0700201 sampledOptions.fSubset = ⊂
emmaleer8f4ba762015-08-14 07:44:46 -0700202 }
203
scroggo8e6c7ad2016-09-16 08:20:38 -0700204 // Since we guarantee that output dimensions are always at least one (even if the sampleSize
205 // is greater than a given dimension), the input sampleSize is not always the sampleSize that
206 // we use in practice.
207 const int sampleX = subsetWidth / info.width();
208 const int sampleY = subsetHeight / info.height();
209
210 const int samplingOffsetY = get_start_coord(sampleY);
211 const int startY = samplingOffsetY + subsetY;
scroggo4f2a88c2016-10-17 14:32:41 -0700212 const int dstHeight = info.height();
scroggo8e6c7ad2016-09-16 08:20:38 -0700213
214 const SkImageInfo nativeInfo = info.makeWH(nativeSize.width(), nativeSize.height());
215
216 {
217 // Although startScanlineDecode expects the bottom and top to match the
218 // SkImageInfo, startIncrementalDecode uses them to determine which rows to
219 // decode.
scroggo4f2a88c2016-10-17 14:32:41 -0700220 SkCodec::Options incrementalOptions = sampledOptions;
scroggo8e6c7ad2016-09-16 08:20:38 -0700221 SkIRect incrementalSubset;
scroggo8e6c7ad2016-09-16 08:20:38 -0700222 if (sampledOptions.fSubset) {
scroggo4f2a88c2016-10-17 14:32:41 -0700223 incrementalSubset.fTop = subsetY;
224 incrementalSubset.fBottom = subsetY + subsetHeight;
scroggo8e6c7ad2016-09-16 08:20:38 -0700225 incrementalSubset.fLeft = sampledOptions.fSubset->fLeft;
226 incrementalSubset.fRight = sampledOptions.fSubset->fRight;
scroggo4f2a88c2016-10-17 14:32:41 -0700227 incrementalOptions.fSubset = &incrementalSubset;
scroggo8e6c7ad2016-09-16 08:20:38 -0700228 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700229 const SkCodec::Result startResult = this->codec()->startIncrementalDecode(nativeInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000230 pixels, rowBytes, &incrementalOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700231 if (SkCodec::kSuccess == startResult) {
232 SkSampler* sampler = this->codec()->getSampler(true);
233 if (!sampler) {
234 return SkCodec::kUnimplemented;
235 }
236
237 if (sampler->setSampleX(sampleX) != info.width()) {
238 return SkCodec::kInvalidScale;
239 }
240 if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) {
241 return SkCodec::kInvalidScale;
242 }
243
244 sampler->setSampleY(sampleY);
245
Leon Scroggins III6e45ce72018-10-16 15:29:11 -0400246 int rowsDecoded = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700247 const SkCodec::Result incResult = this->codec()->incrementalDecode(&rowsDecoded);
248 if (incResult == SkCodec::kSuccess) {
249 return SkCodec::kSuccess;
250 }
Leon Scroggins III674a1842017-07-06 12:26:09 -0400251 SkASSERT(incResult == SkCodec::kIncompleteInput || incResult == SkCodec::kErrorInInput);
msarettc6c81e12016-09-16 14:52:07 -0700252
scroggoff9f7bb2016-10-10 11:35:01 -0700253 SkASSERT(rowsDecoded <= info.height());
scroggo8e6c7ad2016-09-16 08:20:38 -0700254 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized,
scroggoff9f7bb2016-10-10 11:35:01 -0700255 info.height(), rowsDecoded);
Leon Scroggins III674a1842017-07-06 12:26:09 -0400256 return incResult;
Leon Scroggins III32526b52018-10-31 15:00:16 -0400257 } else if (startResult == SkCodec::kIncompleteInput
258 || startResult == SkCodec::kErrorInInput) {
259 return SkCodec::kInvalidInput;
scroggo8e6c7ad2016-09-16 08:20:38 -0700260 } else if (startResult != SkCodec::kUnimplemented) {
261 return startResult;
262 } // kUnimplemented means use the old method.
263 }
264
msarett3d9d7a72015-10-21 10:27:10 -0700265 // Start the scanline decode.
scroggo8e6c7ad2016-09-16 08:20:38 -0700266 SkCodec::Result result = this->codec()->startScanlineDecode(nativeInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000267 &sampledOptions);
Leon Scroggins III32526b52018-10-31 15:00:16 -0400268 if (SkCodec::kIncompleteInput == result || SkCodec::kErrorInInput == result) {
269 return SkCodec::kInvalidInput;
270 } else if (SkCodec::kSuccess != result) {
emmaleer8f4ba762015-08-14 07:44:46 -0700271 return result;
272 }
emmaleer8f4ba762015-08-14 07:44:46 -0700273
msarett90c4d5f2015-12-10 13:09:24 -0800274 SkSampler* sampler = this->codec()->getSampler(true);
scroggoe7fc14b2015-10-02 13:14:46 -0700275 if (!sampler) {
msarett3d9d7a72015-10-21 10:27:10 -0700276 return SkCodec::kUnimplemented;
scroggoe7fc14b2015-10-02 13:14:46 -0700277 }
278
msarett3d9d7a72015-10-21 10:27:10 -0700279 if (sampler->setSampleX(sampleX) != info.width()) {
280 return SkCodec::kInvalidScale;
281 }
282 if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) {
283 return SkCodec::kInvalidScale;
scroggoe7fc14b2015-10-02 13:14:46 -0700284 }
285
msarett90c4d5f2015-12-10 13:09:24 -0800286 switch(this->codec()->getScanlineOrder()) {
scroggo46c57472015-09-30 08:57:13 -0700287 case SkCodec::kTopDown_SkScanlineOrder: {
msarett90c4d5f2015-12-10 13:09:24 -0800288 if (!this->codec()->skipScanlines(startY)) {
289 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized,
msarett3d9d7a72015-10-21 10:27:10 -0700290 dstHeight, 0);
291 return SkCodec::kIncompleteInput;
msarett5406d6f2015-08-31 06:55:13 -0700292 }
msarett3d9d7a72015-10-21 10:27:10 -0700293 void* pixelPtr = pixels;
msarett5406d6f2015-08-31 06:55:13 -0700294 for (int y = 0; y < dstHeight; y++) {
msarett90c4d5f2015-12-10 13:09:24 -0800295 if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) {
296 this->codec()->fillIncompleteImage(info, pixels, rowBytes,
297 options.fZeroInitialized, dstHeight, y + 1);
msarett3d9d7a72015-10-21 10:27:10 -0700298 return SkCodec::kIncompleteInput;
msarett5406d6f2015-08-31 06:55:13 -0700299 }
msarett691ad762015-11-05 11:19:29 -0800300 if (y < dstHeight - 1) {
msarett90c4d5f2015-12-10 13:09:24 -0800301 if (!this->codec()->skipScanlines(sampleY - 1)) {
302 this->codec()->fillIncompleteImage(info, pixels, rowBytes,
msarett691ad762015-11-05 11:19:29 -0800303 options.fZeroInitialized, dstHeight, y + 1);
304 return SkCodec::kIncompleteInput;
305 }
msarett5406d6f2015-08-31 06:55:13 -0700306 }
msarett3d9d7a72015-10-21 10:27:10 -0700307 pixelPtr = SkTAddOffset<void>(pixelPtr, rowBytes);
msarett5406d6f2015-08-31 06:55:13 -0700308 }
msarett3d9d7a72015-10-21 10:27:10 -0700309 return SkCodec::kSuccess;
emmaleer8f4ba762015-08-14 07:44:46 -0700310 }
msarett887e18e2015-11-17 08:46:02 -0800311 case SkCodec::kBottomUp_SkScanlineOrder: {
msarett5af4e0b2015-11-17 11:18:03 -0800312 // Note that these modes do not support subsetting.
msarett887e18e2015-11-17 08:46:02 -0800313 SkASSERT(0 == subsetY && nativeSize.height() == subsetHeight);
314 int y;
315 for (y = 0; y < nativeSize.height(); y++) {
msarett90c4d5f2015-12-10 13:09:24 -0800316 int srcY = this->codec()->nextScanline();
msarett887e18e2015-11-17 08:46:02 -0800317 if (is_coord_necessary(srcY, sampleY, dstHeight)) {
318 void* pixelPtr = SkTAddOffset<void>(pixels,
319 rowBytes * get_dst_coord(srcY, sampleY));
msarett90c4d5f2015-12-10 13:09:24 -0800320 if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) {
msarett887e18e2015-11-17 08:46:02 -0800321 break;
322 }
323 } else {
msarett90c4d5f2015-12-10 13:09:24 -0800324 if (!this->codec()->skipScanlines(1)) {
msarett887e18e2015-11-17 08:46:02 -0800325 break;
326 }
327 }
328 }
329
330 if (nativeSize.height() == y) {
331 return SkCodec::kSuccess;
332 }
333
msarett90c4d5f2015-12-10 13:09:24 -0800334 // We handle filling uninitialized memory here instead of using this->codec().
335 // this->codec() does not know that we are sampling.
msarett887e18e2015-11-17 08:46:02 -0800336 const SkImageInfo fillInfo = info.makeWH(info.width(), 1);
337 for (; y < nativeSize.height(); y++) {
msarett90c4d5f2015-12-10 13:09:24 -0800338 int srcY = this->codec()->outputScanline(y);
msarett887e18e2015-11-17 08:46:02 -0800339 if (!is_coord_necessary(srcY, sampleY, dstHeight)) {
340 continue;
341 }
342
343 void* rowPtr = SkTAddOffset<void>(pixels, rowBytes * get_dst_coord(srcY, sampleY));
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400344 SkSampler::Fill(fillInfo, rowPtr, rowBytes, options.fZeroInitialized);
msarett887e18e2015-11-17 08:46:02 -0800345 }
346 return SkCodec::kIncompleteInput;
347 }
msarett5406d6f2015-08-31 06:55:13 -0700348 default:
349 SkASSERT(false);
msarett3d9d7a72015-10-21 10:27:10 -0700350 return SkCodec::kUnimplemented;
emmaleer8f4ba762015-08-14 07:44:46 -0700351 }
emmaleer8f4ba762015-08-14 07:44:46 -0700352}