blob: 49c939c1f887c500f8795173a031dc2c996b36eb [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
msarett3d9d7a72015-10-21 10:27:10 -0700104 // Start the scanline decode.
105 SkIRect scanlineSubset = SkIRect::MakeXYWH(scaledSubsetX, 0, scaledSubsetWidth,
106 scaledSize.height());
107 codecOptions.fSubset = &scanlineSubset;
msarett90c4d5f2015-12-10 13:09:24 -0800108 SkCodec::Result result = this->codec()->startScanlineDecode(info.makeWH(scaledSize.width(),
msarett3d9d7a72015-10-21 10:27:10 -0700109 scaledSize.height()), &codecOptions, options.fColorPtr, options.fColorCount);
110 if (SkCodec::kSuccess != result) {
111 return result;
112 }
emmaleer8f4ba762015-08-14 07:44:46 -0700113
msarett3d9d7a72015-10-21 10:27:10 -0700114 // At this point, we are only concerned with subsetting. Either no scale was
msarett90c4d5f2015-12-10 13:09:24 -0800115 // requested, or the this->codec() is handling the scale.
116 switch (this->codec()->getScanlineOrder()) {
msarett3d9d7a72015-10-21 10:27:10 -0700117 case SkCodec::kTopDown_SkScanlineOrder:
118 case SkCodec::kNone_SkScanlineOrder: {
msarett90c4d5f2015-12-10 13:09:24 -0800119 if (!this->codec()->skipScanlines(scaledSubsetY)) {
120 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized,
msarett3d9d7a72015-10-21 10:27:10 -0700121 scaledSubsetHeight, 0);
122 return SkCodec::kIncompleteInput;
emmaleer8f4ba762015-08-14 07:44:46 -0700123 }
emmaleer8f4ba762015-08-14 07:44:46 -0700124
msarett90c4d5f2015-12-10 13:09:24 -0800125 int decodedLines = this->codec()->getScanlines(pixels, scaledSubsetHeight, rowBytes);
msarett3d9d7a72015-10-21 10:27:10 -0700126 if (decodedLines != scaledSubsetHeight) {
127 return SkCodec::kIncompleteInput;
128 }
129 return SkCodec::kSuccess;
130 }
131 default:
132 SkASSERT(false);
133 return SkCodec::kUnimplemented;
emmaleer8f4ba762015-08-14 07:44:46 -0700134 }
135}
136
emmaleer8f4ba762015-08-14 07:44:46 -0700137
msarett3d9d7a72015-10-21 10:27:10 -0700138SkCodec::Result SkSampledCodec::sampledDecode(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -0800139 size_t rowBytes, const AndroidOptions& options) {
scroggo501b7342015-11-03 07:55:11 -0800140 // We should only call this function when sampling.
141 SkASSERT(options.fSampleSize > 1);
142
msarett3d9d7a72015-10-21 10:27:10 -0700143 // Create options struct for the codec.
144 SkCodec::Options sampledOptions;
145 sampledOptions.fZeroInitialized = options.fZeroInitialized;
emmaleer8f4ba762015-08-14 07:44:46 -0700146
scroggo501b7342015-11-03 07:55:11 -0800147 // FIXME: This was already called by onGetAndroidPixels. Can we reduce that?
148 int sampleSize = options.fSampleSize;
149 int nativeSampleSize;
150 SkISize nativeSize = this->accountForNativeScaling(&sampleSize, &nativeSampleSize);
151
msarett3d9d7a72015-10-21 10:27:10 -0700152 // Check if there is a subset.
153 SkIRect subset;
154 int subsetY = 0;
scroggo501b7342015-11-03 07:55:11 -0800155 int subsetWidth = nativeSize.width();
156 int subsetHeight = nativeSize.height();
emmaleer8f4ba762015-08-14 07:44:46 -0700157 if (options.fSubset) {
msarett3d9d7a72015-10-21 10:27:10 -0700158 // We will need to know about subsetting in the y-dimension in order to use the
159 // scanline decoder.
msarett90c4d5f2015-12-10 13:09:24 -0800160 // Update the subset to account for scaling done by this->codec().
msarett3d9d7a72015-10-21 10:27:10 -0700161 SkIRect* subsetPtr = options.fSubset;
scroggo501b7342015-11-03 07:55:11 -0800162
163 // Do the divide ourselves, instead of calling get_scaled_dimension. If
164 // X and Y are 0, they should remain 0, rather than being upgraded to 1
165 // due to being smaller than the sampleSize.
166 const int subsetX = subsetPtr->x() / nativeSampleSize;
167 subsetY = subsetPtr->y() / nativeSampleSize;
168
169 subsetWidth = get_scaled_dimension(subsetPtr->width(), nativeSampleSize);
170 subsetHeight = get_scaled_dimension(subsetPtr->height(), nativeSampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700171
172 // The scanline decoder only needs to be aware of subsetting in the x-dimension.
scroggo501b7342015-11-03 07:55:11 -0800173 subset.setXYWH(subsetX, 0, subsetWidth, nativeSize.height());
msarett3d9d7a72015-10-21 10:27:10 -0700174 sampledOptions.fSubset = ⊂
emmaleer8f4ba762015-08-14 07:44:46 -0700175 }
176
msarett3d9d7a72015-10-21 10:27:10 -0700177 // Start the scanline decode.
msarett90c4d5f2015-12-10 13:09:24 -0800178 SkCodec::Result result = this->codec()->startScanlineDecode(
scroggo501b7342015-11-03 07:55:11 -0800179 info.makeWH(nativeSize.width(), nativeSize.height()), &sampledOptions,
msarett3d9d7a72015-10-21 10:27:10 -0700180 options.fColorPtr, options.fColorCount);
181 if (SkCodec::kSuccess != result) {
emmaleer8f4ba762015-08-14 07:44:46 -0700182 return result;
183 }
emmaleer8f4ba762015-08-14 07:44:46 -0700184
msarett90c4d5f2015-12-10 13:09:24 -0800185 SkSampler* sampler = this->codec()->getSampler(true);
scroggoe7fc14b2015-10-02 13:14:46 -0700186 if (!sampler) {
msarett3d9d7a72015-10-21 10:27:10 -0700187 return SkCodec::kUnimplemented;
scroggoe7fc14b2015-10-02 13:14:46 -0700188 }
189
msarett3d9d7a72015-10-21 10:27:10 -0700190 // Since we guarantee that output dimensions are always at least one (even if the sampleSize
191 // is greater than a given dimension), the input sampleSize is not always the 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()) {
196 return SkCodec::kInvalidScale;
197 }
198 if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) {
199 return SkCodec::kInvalidScale;
scroggoe7fc14b2015-10-02 13:14:46 -0700200 }
201
msarett3d9d7a72015-10-21 10:27:10 -0700202 const int samplingOffsetY = get_start_coord(sampleY);
203 const int startY = samplingOffsetY + subsetY;
204 int dstHeight = info.height();
msarett90c4d5f2015-12-10 13:09:24 -0800205 switch(this->codec()->getScanlineOrder()) {
scroggo46c57472015-09-30 08:57:13 -0700206 case SkCodec::kTopDown_SkScanlineOrder: {
msarett90c4d5f2015-12-10 13:09:24 -0800207 if (!this->codec()->skipScanlines(startY)) {
208 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized,
msarett3d9d7a72015-10-21 10:27:10 -0700209 dstHeight, 0);
210 return SkCodec::kIncompleteInput;
msarett5406d6f2015-08-31 06:55:13 -0700211 }
msarett3d9d7a72015-10-21 10:27:10 -0700212 void* pixelPtr = pixels;
msarett5406d6f2015-08-31 06:55:13 -0700213 for (int y = 0; y < dstHeight; y++) {
msarett90c4d5f2015-12-10 13:09:24 -0800214 if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) {
215 this->codec()->fillIncompleteImage(info, pixels, rowBytes,
216 options.fZeroInitialized, dstHeight, y + 1);
msarett3d9d7a72015-10-21 10:27:10 -0700217 return SkCodec::kIncompleteInput;
msarett5406d6f2015-08-31 06:55:13 -0700218 }
msarett691ad762015-11-05 11:19:29 -0800219 if (y < dstHeight - 1) {
msarett90c4d5f2015-12-10 13:09:24 -0800220 if (!this->codec()->skipScanlines(sampleY - 1)) {
221 this->codec()->fillIncompleteImage(info, pixels, rowBytes,
msarett691ad762015-11-05 11:19:29 -0800222 options.fZeroInitialized, dstHeight, y + 1);
223 return SkCodec::kIncompleteInput;
224 }
msarett5406d6f2015-08-31 06:55:13 -0700225 }
msarett3d9d7a72015-10-21 10:27:10 -0700226 pixelPtr = SkTAddOffset<void>(pixelPtr, rowBytes);
msarett5406d6f2015-08-31 06:55:13 -0700227 }
msarett3d9d7a72015-10-21 10:27:10 -0700228 return SkCodec::kSuccess;
emmaleer8f4ba762015-08-14 07:44:46 -0700229 }
msarett5af4e0b2015-11-17 11:18:03 -0800230 case SkCodec::kOutOfOrder_SkScanlineOrder:
msarett887e18e2015-11-17 08:46:02 -0800231 case SkCodec::kBottomUp_SkScanlineOrder: {
msarett5af4e0b2015-11-17 11:18:03 -0800232 // Note that these modes do not support subsetting.
msarett887e18e2015-11-17 08:46:02 -0800233 SkASSERT(0 == subsetY && nativeSize.height() == subsetHeight);
234 int y;
235 for (y = 0; y < nativeSize.height(); y++) {
msarett90c4d5f2015-12-10 13:09:24 -0800236 int srcY = this->codec()->nextScanline();
msarett887e18e2015-11-17 08:46:02 -0800237 if (is_coord_necessary(srcY, sampleY, dstHeight)) {
238 void* pixelPtr = SkTAddOffset<void>(pixels,
239 rowBytes * get_dst_coord(srcY, sampleY));
msarett90c4d5f2015-12-10 13:09:24 -0800240 if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) {
msarett887e18e2015-11-17 08:46:02 -0800241 break;
242 }
243 } else {
msarett90c4d5f2015-12-10 13:09:24 -0800244 if (!this->codec()->skipScanlines(1)) {
msarett887e18e2015-11-17 08:46:02 -0800245 break;
246 }
247 }
248 }
249
250 if (nativeSize.height() == y) {
251 return SkCodec::kSuccess;
252 }
253
msarett90c4d5f2015-12-10 13:09:24 -0800254 // We handle filling uninitialized memory here instead of using this->codec().
255 // this->codec() does not know that we are sampling.
scroggoc5560be2016-02-03 09:42:42 -0800256 const uint32_t fillValue = this->codec()->getFillValue(info.colorType());
msarett887e18e2015-11-17 08:46:02 -0800257 const SkImageInfo fillInfo = info.makeWH(info.width(), 1);
258 for (; y < nativeSize.height(); y++) {
msarett90c4d5f2015-12-10 13:09:24 -0800259 int srcY = this->codec()->outputScanline(y);
msarett887e18e2015-11-17 08:46:02 -0800260 if (!is_coord_necessary(srcY, sampleY, dstHeight)) {
261 continue;
262 }
263
264 void* rowPtr = SkTAddOffset<void>(pixels, rowBytes * get_dst_coord(srcY, sampleY));
265 SkSampler::Fill(fillInfo, rowPtr, rowBytes, fillValue, options.fZeroInitialized);
266 }
267 return SkCodec::kIncompleteInput;
268 }
scroggo46c57472015-09-30 08:57:13 -0700269 case SkCodec::kNone_SkScanlineOrder: {
msarett3d9d7a72015-10-21 10:27:10 -0700270 const int linesNeeded = subsetHeight - samplingOffsetY;
scroggo565901d2015-12-10 10:44:13 -0800271 SkAutoTMalloc<uint8_t> storage(linesNeeded * rowBytes);
272 uint8_t* storagePtr = storage.get();
msarett3d9d7a72015-10-21 10:27:10 -0700273
msarett90c4d5f2015-12-10 13:09:24 -0800274 if (!this->codec()->skipScanlines(startY)) {
275 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized,
msarett3d9d7a72015-10-21 10:27:10 -0700276 dstHeight, 0);
277 return SkCodec::kIncompleteInput;
278 }
msarett90c4d5f2015-12-10 13:09:24 -0800279 int scanlines = this->codec()->getScanlines(storagePtr, linesNeeded, rowBytes);
msarett3d9d7a72015-10-21 10:27:10 -0700280
281 for (int y = 0; y < dstHeight; y++) {
282 memcpy(pixels, storagePtr, info.minRowBytes());
msarett5406d6f2015-08-31 06:55:13 -0700283 storagePtr += sampleY * rowBytes;
msarett3d9d7a72015-10-21 10:27:10 -0700284 pixels = SkTAddOffset<void>(pixels, rowBytes);
emmaleer8f4ba762015-08-14 07:44:46 -0700285 }
msarett3d9d7a72015-10-21 10:27:10 -0700286
287 if (scanlines < dstHeight) {
msarett90c4d5f2015-12-10 13:09:24 -0800288 // this->codec() has already handled filling uninitialized memory.
msarett3d9d7a72015-10-21 10:27:10 -0700289 return SkCodec::kIncompleteInput;
msarette6dd0042015-10-09 11:07:34 -0700290 }
msarett3d9d7a72015-10-21 10:27:10 -0700291 return SkCodec::kSuccess;
emmaleer8f4ba762015-08-14 07:44:46 -0700292 }
msarett5406d6f2015-08-31 06:55:13 -0700293 default:
294 SkASSERT(false);
msarett3d9d7a72015-10-21 10:27:10 -0700295 return SkCodec::kUnimplemented;
emmaleer8f4ba762015-08-14 07:44:46 -0700296 }
emmaleer8f4ba762015-08-14 07:44:46 -0700297}