emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 8 | #include "SkCodec.h" |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 9 | #include "SkCodecPriv.h" |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 10 | #include "SkMath.h" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 11 | #include "SkSampledCodec.h" |
msarett | a4970dc | 2016-01-11 07:23:23 -0800 | [diff] [blame] | 12 | #include "SkSampler.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 13 | #include "SkTemplates.h" |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 14 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 15 | SkSampledCodec::SkSampledCodec(SkCodec* codec) |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 16 | : INHERITED(codec) |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 17 | {} |
| 18 | |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 19 | SkISize SkSampledCodec::accountForNativeScaling(int* sampleSizePtr, int* nativeSampleSize) const { |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 20 | SkISize preSampledSize = this->codec()->getInfo().dimensions(); |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 21 | int sampleSize = *sampleSizePtr; |
| 22 | SkASSERT(sampleSize > 1); |
| 23 | |
| 24 | if (nativeSampleSize) { |
| 25 | *nativeSampleSize = 1; |
| 26 | } |
| 27 | |
| 28 | // Only JPEG supports native downsampling. |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 29 | if (this->codec()->getEncodedFormat() == SkEncodedImageFormat::kJPEG) { |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 30 | // 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; |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 37 | return this->codec()->getScaledDimensions(get_scale_from_sample_size(sampleSize)); |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 38 | 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 | |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 51 | // this->codec() will scale to this size. |
| 52 | preSampledSize = this->codec()->getScaledDimensions(scale); |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 53 | |
| 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 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 67 | SkISize SkSampledCodec::onGetSampledDimensions(int sampleSize) const { |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 68 | const SkISize size = this->accountForNativeScaling(&sampleSize); |
| 69 | return SkISize::Make(get_scaled_dimension(size.width(), sampleSize), |
| 70 | get_scaled_dimension(size.height(), sampleSize)); |
msarett | a83593b | 2015-08-18 08:03:58 -0700 | [diff] [blame] | 71 | } |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 72 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 73 | SkCodec::Result SkSampledCodec::onGetAndroidPixels(const SkImageInfo& info, void* pixels, |
scroggo | e95a068 | 2015-11-04 04:31:12 -0800 | [diff] [blame] | 74 | size_t rowBytes, const AndroidOptions& options) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 75 | // Create an Options struct for the codec. |
| 76 | SkCodec::Options codecOptions; |
| 77 | codecOptions.fZeroInitialized = options.fZeroInitialized; |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame^] | 78 | codecOptions.fPremulBehavior = SkTransferFunctionBehavior::kIgnore; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 79 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 80 | SkIRect* subset = options.fSubset; |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 81 | if (!subset || subset->size() == this->codec()->getInfo().dimensions()) { |
| 82 | if (this->codec()->dimensionsSupported(info.dimensions())) { |
| 83 | return this->codec()->getPixels(info, pixels, rowBytes, &codecOptions, |
| 84 | options.fColorPtr, options.fColorCount); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 85 | } |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 86 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 87 | // If the native codec does not support the requested scale, scale by sampling. |
| 88 | return this->sampledDecode(info, pixels, rowBytes, options); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 89 | } |
| 90 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 91 | // We are performing a subset decode. |
| 92 | int sampleSize = options.fSampleSize; |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 93 | SkISize scaledSize = this->getSampledDimensions(sampleSize); |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 94 | if (!this->codec()->dimensionsSupported(scaledSize)) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 95 | // If the native codec does not support the requested scale, scale by sampling. |
| 96 | return this->sampledDecode(info, pixels, rowBytes, options); |
| 97 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 98 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 99 | // Calculate the scaled subset bounds. |
| 100 | int scaledSubsetX = subset->x() / sampleSize; |
| 101 | int scaledSubsetY = subset->y() / sampleSize; |
| 102 | int scaledSubsetWidth = info.width(); |
| 103 | int scaledSubsetHeight = info.height(); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 104 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 105 | const SkImageInfo scaledInfo = info.makeWH(scaledSize.width(), scaledSize.height()); |
| 106 | |
| 107 | { |
| 108 | // Although startScanlineDecode expects the bottom and top to match the |
| 109 | // SkImageInfo, startIncrementalDecode uses them to determine which rows to |
| 110 | // decode. |
| 111 | SkIRect incrementalSubset = SkIRect::MakeXYWH(scaledSubsetX, scaledSubsetY, |
| 112 | scaledSubsetWidth, scaledSubsetHeight); |
| 113 | codecOptions.fSubset = &incrementalSubset; |
| 114 | const SkCodec::Result startResult = this->codec()->startIncrementalDecode( |
| 115 | scaledInfo, pixels, rowBytes, &codecOptions, |
| 116 | options.fColorPtr, options.fColorCount); |
| 117 | if (SkCodec::kSuccess == startResult) { |
| 118 | int rowsDecoded; |
| 119 | const SkCodec::Result incResult = this->codec()->incrementalDecode(&rowsDecoded); |
| 120 | if (incResult == SkCodec::kSuccess) { |
| 121 | return SkCodec::kSuccess; |
| 122 | } |
| 123 | SkASSERT(SkCodec::kIncompleteInput == incResult); |
| 124 | |
| 125 | // FIXME: Can zero initialized be read from SkCodec::fOptions? |
| 126 | this->codec()->fillIncompleteImage(scaledInfo, pixels, rowBytes, |
| 127 | options.fZeroInitialized, scaledSubsetHeight, rowsDecoded); |
| 128 | return SkCodec::kIncompleteInput; |
| 129 | } else if (startResult != SkCodec::kUnimplemented) { |
| 130 | return startResult; |
| 131 | } |
| 132 | // Otherwise fall down to use the old scanline decoder. |
| 133 | // codecOptions.fSubset will be reset below, so it will not continue to |
| 134 | // point to the object that is no longer on the stack. |
| 135 | } |
| 136 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 137 | // Start the scanline decode. |
| 138 | SkIRect scanlineSubset = SkIRect::MakeXYWH(scaledSubsetX, 0, scaledSubsetWidth, |
| 139 | scaledSize.height()); |
| 140 | codecOptions.fSubset = &scanlineSubset; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 141 | |
| 142 | SkCodec::Result result = this->codec()->startScanlineDecode(scaledInfo, |
| 143 | &codecOptions, options.fColorPtr, options.fColorCount); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 144 | if (SkCodec::kSuccess != result) { |
| 145 | return result; |
| 146 | } |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 147 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 148 | // At this point, we are only concerned with subsetting. Either no scale was |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 149 | // requested, or the this->codec() is handling the scale. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 150 | // Note that subsetting is only supported for kTopDown, so this code will not be |
| 151 | // reached for other orders. |
| 152 | SkASSERT(this->codec()->getScanlineOrder() == SkCodec::kTopDown_SkScanlineOrder); |
| 153 | if (!this->codec()->skipScanlines(scaledSubsetY)) { |
| 154 | this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized, |
| 155 | scaledSubsetHeight, 0); |
| 156 | return SkCodec::kIncompleteInput; |
scroggo | 6fb2391 | 2016-06-02 14:16:43 -0700 | [diff] [blame] | 157 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 158 | |
| 159 | int decodedLines = this->codec()->getScanlines(pixels, scaledSubsetHeight, rowBytes); |
| 160 | if (decodedLines != scaledSubsetHeight) { |
| 161 | return SkCodec::kIncompleteInput; |
| 162 | } |
| 163 | return SkCodec::kSuccess; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 164 | } |
| 165 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 166 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 167 | SkCodec::Result SkSampledCodec::sampledDecode(const SkImageInfo& info, void* pixels, |
scroggo | e95a068 | 2015-11-04 04:31:12 -0800 | [diff] [blame] | 168 | size_t rowBytes, const AndroidOptions& options) { |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 169 | // We should only call this function when sampling. |
| 170 | SkASSERT(options.fSampleSize > 1); |
| 171 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 172 | // Create options struct for the codec. |
| 173 | SkCodec::Options sampledOptions; |
| 174 | sampledOptions.fZeroInitialized = options.fZeroInitialized; |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame^] | 175 | sampledOptions.fPremulBehavior = SkTransferFunctionBehavior::kIgnore; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 176 | |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 177 | // FIXME: This was already called by onGetAndroidPixels. Can we reduce that? |
| 178 | int sampleSize = options.fSampleSize; |
| 179 | int nativeSampleSize; |
| 180 | SkISize nativeSize = this->accountForNativeScaling(&sampleSize, &nativeSampleSize); |
| 181 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 182 | // Check if there is a subset. |
| 183 | SkIRect subset; |
| 184 | int subsetY = 0; |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 185 | int subsetWidth = nativeSize.width(); |
| 186 | int subsetHeight = nativeSize.height(); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 187 | if (options.fSubset) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 188 | // We will need to know about subsetting in the y-dimension in order to use the |
| 189 | // scanline decoder. |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 190 | // Update the subset to account for scaling done by this->codec(). |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 191 | const SkIRect* subsetPtr = options.fSubset; |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 192 | |
| 193 | // Do the divide ourselves, instead of calling get_scaled_dimension. If |
| 194 | // X and Y are 0, they should remain 0, rather than being upgraded to 1 |
| 195 | // due to being smaller than the sampleSize. |
| 196 | const int subsetX = subsetPtr->x() / nativeSampleSize; |
| 197 | subsetY = subsetPtr->y() / nativeSampleSize; |
| 198 | |
| 199 | subsetWidth = get_scaled_dimension(subsetPtr->width(), nativeSampleSize); |
| 200 | subsetHeight = get_scaled_dimension(subsetPtr->height(), nativeSampleSize); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 201 | |
| 202 | // The scanline decoder only needs to be aware of subsetting in the x-dimension. |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 203 | subset.setXYWH(subsetX, 0, subsetWidth, nativeSize.height()); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 204 | sampledOptions.fSubset = ⊂ |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 205 | } |
| 206 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 207 | // Since we guarantee that output dimensions are always at least one (even if the sampleSize |
| 208 | // is greater than a given dimension), the input sampleSize is not always the sampleSize that |
| 209 | // we use in practice. |
| 210 | const int sampleX = subsetWidth / info.width(); |
| 211 | const int sampleY = subsetHeight / info.height(); |
| 212 | |
| 213 | const int samplingOffsetY = get_start_coord(sampleY); |
| 214 | const int startY = samplingOffsetY + subsetY; |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 215 | const int dstHeight = info.height(); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 216 | |
| 217 | const SkImageInfo nativeInfo = info.makeWH(nativeSize.width(), nativeSize.height()); |
| 218 | |
| 219 | { |
| 220 | // Although startScanlineDecode expects the bottom and top to match the |
| 221 | // SkImageInfo, startIncrementalDecode uses them to determine which rows to |
| 222 | // decode. |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 223 | SkCodec::Options incrementalOptions = sampledOptions; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 224 | SkIRect incrementalSubset; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 225 | if (sampledOptions.fSubset) { |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 226 | incrementalSubset.fTop = subsetY; |
| 227 | incrementalSubset.fBottom = subsetY + subsetHeight; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 228 | incrementalSubset.fLeft = sampledOptions.fSubset->fLeft; |
| 229 | incrementalSubset.fRight = sampledOptions.fSubset->fRight; |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 230 | incrementalOptions.fSubset = &incrementalSubset; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 231 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 232 | const SkCodec::Result startResult = this->codec()->startIncrementalDecode(nativeInfo, |
| 233 | pixels, rowBytes, &incrementalOptions, options.fColorPtr, options.fColorCount); |
| 234 | if (SkCodec::kSuccess == startResult) { |
| 235 | SkSampler* sampler = this->codec()->getSampler(true); |
| 236 | if (!sampler) { |
| 237 | return SkCodec::kUnimplemented; |
| 238 | } |
| 239 | |
| 240 | if (sampler->setSampleX(sampleX) != info.width()) { |
| 241 | return SkCodec::kInvalidScale; |
| 242 | } |
| 243 | if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) { |
| 244 | return SkCodec::kInvalidScale; |
| 245 | } |
| 246 | |
| 247 | sampler->setSampleY(sampleY); |
| 248 | |
| 249 | int rowsDecoded; |
| 250 | const SkCodec::Result incResult = this->codec()->incrementalDecode(&rowsDecoded); |
| 251 | if (incResult == SkCodec::kSuccess) { |
| 252 | return SkCodec::kSuccess; |
| 253 | } |
| 254 | SkASSERT(incResult == SkCodec::kIncompleteInput); |
msarett | c6c81e1 | 2016-09-16 14:52:07 -0700 | [diff] [blame] | 255 | |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 256 | SkASSERT(rowsDecoded <= info.height()); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 257 | this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized, |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 258 | info.height(), rowsDecoded); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 259 | return SkCodec::kIncompleteInput; |
| 260 | } else if (startResult != SkCodec::kUnimplemented) { |
| 261 | return startResult; |
| 262 | } // kUnimplemented means use the old method. |
| 263 | } |
| 264 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 265 | // Start the scanline decode. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 266 | SkCodec::Result result = this->codec()->startScanlineDecode(nativeInfo, |
| 267 | &sampledOptions, options.fColorPtr, options.fColorCount); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 268 | if (SkCodec::kSuccess != result) { |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 269 | return result; |
| 270 | } |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 271 | |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 272 | SkSampler* sampler = this->codec()->getSampler(true); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 273 | if (!sampler) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 274 | return SkCodec::kUnimplemented; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 275 | } |
| 276 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 277 | if (sampler->setSampleX(sampleX) != info.width()) { |
| 278 | return SkCodec::kInvalidScale; |
| 279 | } |
| 280 | if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) { |
| 281 | return SkCodec::kInvalidScale; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 282 | } |
| 283 | |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 284 | switch(this->codec()->getScanlineOrder()) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 285 | case SkCodec::kTopDown_SkScanlineOrder: { |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 286 | if (!this->codec()->skipScanlines(startY)) { |
| 287 | this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized, |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 288 | dstHeight, 0); |
| 289 | return SkCodec::kIncompleteInput; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 290 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 291 | void* pixelPtr = pixels; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 292 | for (int y = 0; y < dstHeight; y++) { |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 293 | if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) { |
| 294 | this->codec()->fillIncompleteImage(info, pixels, rowBytes, |
| 295 | options.fZeroInitialized, dstHeight, y + 1); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 296 | return SkCodec::kIncompleteInput; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 297 | } |
msarett | 691ad76 | 2015-11-05 11:19:29 -0800 | [diff] [blame] | 298 | if (y < dstHeight - 1) { |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 299 | if (!this->codec()->skipScanlines(sampleY - 1)) { |
| 300 | this->codec()->fillIncompleteImage(info, pixels, rowBytes, |
msarett | 691ad76 | 2015-11-05 11:19:29 -0800 | [diff] [blame] | 301 | options.fZeroInitialized, dstHeight, y + 1); |
| 302 | return SkCodec::kIncompleteInput; |
| 303 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 304 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 305 | pixelPtr = SkTAddOffset<void>(pixelPtr, rowBytes); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 306 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 307 | return SkCodec::kSuccess; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 308 | } |
msarett | 887e18e | 2015-11-17 08:46:02 -0800 | [diff] [blame] | 309 | case SkCodec::kBottomUp_SkScanlineOrder: { |
msarett | 5af4e0b | 2015-11-17 11:18:03 -0800 | [diff] [blame] | 310 | // Note that these modes do not support subsetting. |
msarett | 887e18e | 2015-11-17 08:46:02 -0800 | [diff] [blame] | 311 | SkASSERT(0 == subsetY && nativeSize.height() == subsetHeight); |
| 312 | int y; |
| 313 | for (y = 0; y < nativeSize.height(); y++) { |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 314 | int srcY = this->codec()->nextScanline(); |
msarett | 887e18e | 2015-11-17 08:46:02 -0800 | [diff] [blame] | 315 | if (is_coord_necessary(srcY, sampleY, dstHeight)) { |
| 316 | void* pixelPtr = SkTAddOffset<void>(pixels, |
| 317 | rowBytes * get_dst_coord(srcY, sampleY)); |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 318 | if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) { |
msarett | 887e18e | 2015-11-17 08:46:02 -0800 | [diff] [blame] | 319 | break; |
| 320 | } |
| 321 | } else { |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 322 | if (!this->codec()->skipScanlines(1)) { |
msarett | 887e18e | 2015-11-17 08:46:02 -0800 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (nativeSize.height() == y) { |
| 329 | return SkCodec::kSuccess; |
| 330 | } |
| 331 | |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 332 | // We handle filling uninitialized memory here instead of using this->codec(). |
| 333 | // this->codec() does not know that we are sampling. |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 334 | const uint64_t fillValue = this->codec()->getFillValue(info); |
msarett | 887e18e | 2015-11-17 08:46:02 -0800 | [diff] [blame] | 335 | const SkImageInfo fillInfo = info.makeWH(info.width(), 1); |
| 336 | for (; y < nativeSize.height(); y++) { |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 337 | int srcY = this->codec()->outputScanline(y); |
msarett | 887e18e | 2015-11-17 08:46:02 -0800 | [diff] [blame] | 338 | if (!is_coord_necessary(srcY, sampleY, dstHeight)) { |
| 339 | continue; |
| 340 | } |
| 341 | |
| 342 | void* rowPtr = SkTAddOffset<void>(pixels, rowBytes * get_dst_coord(srcY, sampleY)); |
| 343 | SkSampler::Fill(fillInfo, rowPtr, rowBytes, fillValue, options.fZeroInitialized); |
| 344 | } |
| 345 | return SkCodec::kIncompleteInput; |
| 346 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 347 | default: |
| 348 | SkASSERT(false); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 349 | return SkCodec::kUnimplemented; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 350 | } |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 351 | } |