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