| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCodec.h" | 8 #include "SkCodec.h" |
| 9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
| 10 #include "SkMath.h" | 10 #include "SkMath.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 // If the native codec does not support the requested scale, scale by sa
mpling. | 94 // If the native codec does not support the requested scale, scale by sa
mpling. |
| 95 return this->sampledDecode(info, pixels, rowBytes, options); | 95 return this->sampledDecode(info, pixels, rowBytes, options); |
| 96 } | 96 } |
| 97 | 97 |
| 98 // Calculate the scaled subset bounds. | 98 // Calculate the scaled subset bounds. |
| 99 int scaledSubsetX = subset->x() / sampleSize; | 99 int scaledSubsetX = subset->x() / sampleSize; |
| 100 int scaledSubsetY = subset->y() / sampleSize; | 100 int scaledSubsetY = subset->y() / sampleSize; |
| 101 int scaledSubsetWidth = info.width(); | 101 int scaledSubsetWidth = info.width(); |
| 102 int scaledSubsetHeight = info.height(); | 102 int scaledSubsetHeight = info.height(); |
| 103 | 103 |
| 104 const SkImageInfo scaledInfo = info.makeWH(scaledSize.width(), scaledSize.he
ight()); |
| 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, scaledSubse
tY, |
| 111 scaledSubsetWidth, scaledS
ubsetHeight); |
| 112 codecOptions.fSubset = &incrementalSubset; |
| 113 const SkCodec::Result startResult = this->codec()->startIncrementalDecod
e( |
| 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 |
| 104 // Start the scanline decode. | 136 // Start the scanline decode. |
| 105 SkIRect scanlineSubset = SkIRect::MakeXYWH(scaledSubsetX, 0, scaledSubsetWid
th, | 137 SkIRect scanlineSubset = SkIRect::MakeXYWH(scaledSubsetX, 0, scaledSubsetWid
th, |
| 106 scaledSize.height()); | 138 scaledSize.height()); |
| 107 codecOptions.fSubset = &scanlineSubset; | 139 codecOptions.fSubset = &scanlineSubset; |
| 108 SkCodec::Result result = this->codec()->startScanlineDecode(info.makeWH(scal
edSize.width(), | 140 |
| 109 scaledSize.height()), &codecOptions, options.fColorPtr, options.fCol
orCount); | 141 SkCodec::Result result = this->codec()->startScanlineDecode(scaledInfo, |
| 142 &codecOptions, options.fColorPtr, options.fColorCount); |
| 110 if (SkCodec::kSuccess != result) { | 143 if (SkCodec::kSuccess != result) { |
| 111 return result; | 144 return result; |
| 112 } | 145 } |
| 113 | 146 |
| 114 // At this point, we are only concerned with subsetting. Either no scale wa
s | 147 // At this point, we are only concerned with subsetting. Either no scale wa
s |
| 115 // requested, or the this->codec() is handling the scale. | 148 // requested, or the this->codec() is handling the scale. |
| 116 switch (this->codec()->getScanlineOrder()) { | 149 // Note that subsetting is only supported for kTopDown, so this code will no
t be |
| 117 case SkCodec::kTopDown_SkScanlineOrder: | 150 // reached for other orders. |
| 118 case SkCodec::kNone_SkScanlineOrder: { | 151 SkASSERT(this->codec()->getScanlineOrder() == SkCodec::kTopDown_SkScanlineOr
der); |
| 119 if (!this->codec()->skipScanlines(scaledSubsetY)) { | 152 if (!this->codec()->skipScanlines(scaledSubsetY)) { |
| 120 this->codec()->fillIncompleteImage(info, pixels, rowBytes, optio
ns.fZeroInitialized, | 153 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZero
Initialized, |
| 121 scaledSubsetHeight, 0); | 154 scaledSubsetHeight, 0); |
| 122 return SkCodec::kIncompleteInput; | 155 return SkCodec::kIncompleteInput; |
| 123 } | 156 } |
| 124 | 157 |
| 125 int decodedLines = this->codec()->getScanlines(pixels, scaledSubsetH
eight, rowBytes); | 158 int decodedLines = this->codec()->getScanlines(pixels, scaledSubsetHeight, r
owBytes); |
| 126 if (decodedLines != scaledSubsetHeight) { | 159 if (decodedLines != scaledSubsetHeight) { |
| 127 return SkCodec::kIncompleteInput; | 160 return SkCodec::kIncompleteInput; |
| 128 } | |
| 129 return SkCodec::kSuccess; | |
| 130 } | |
| 131 default: | |
| 132 SkASSERT(false); | |
| 133 return SkCodec::kUnimplemented; | |
| 134 } | 161 } |
| 162 return SkCodec::kSuccess; |
| 135 } | 163 } |
| 136 | 164 |
| 137 | 165 |
| 138 SkCodec::Result SkSampledCodec::sampledDecode(const SkImageInfo& info, void* pix
els, | 166 SkCodec::Result SkSampledCodec::sampledDecode(const SkImageInfo& info, void* pix
els, |
| 139 size_t rowBytes, const AndroidOptions& options) { | 167 size_t rowBytes, const AndroidOptions& options) { |
| 140 // We should only call this function when sampling. | 168 // We should only call this function when sampling. |
| 141 SkASSERT(options.fSampleSize > 1); | 169 SkASSERT(options.fSampleSize > 1); |
| 142 | 170 |
| 143 // Create options struct for the codec. | 171 // Create options struct for the codec. |
| 144 SkCodec::Options sampledOptions; | 172 SkCodec::Options sampledOptions; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 167 subsetY = subsetPtr->y() / nativeSampleSize; | 195 subsetY = subsetPtr->y() / nativeSampleSize; |
| 168 | 196 |
| 169 subsetWidth = get_scaled_dimension(subsetPtr->width(), nativeSampleSize)
; | 197 subsetWidth = get_scaled_dimension(subsetPtr->width(), nativeSampleSize)
; |
| 170 subsetHeight = get_scaled_dimension(subsetPtr->height(), nativeSampleSiz
e); | 198 subsetHeight = get_scaled_dimension(subsetPtr->height(), nativeSampleSiz
e); |
| 171 | 199 |
| 172 // The scanline decoder only needs to be aware of subsetting in the x-di
mension. | 200 // The scanline decoder only needs to be aware of subsetting in the x-di
mension. |
| 173 subset.setXYWH(subsetX, 0, subsetWidth, nativeSize.height()); | 201 subset.setXYWH(subsetX, 0, subsetWidth, nativeSize.height()); |
| 174 sampledOptions.fSubset = ⊂ | 202 sampledOptions.fSubset = ⊂ |
| 175 } | 203 } |
| 176 | 204 |
| 205 // Since we guarantee that output dimensions are always at least one (even i
f the sampleSize |
| 206 // is greater than a given dimension), the input sampleSize is not always th
e 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.he
ight()); |
| 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 sub
set, |
| 223 // meaning that we can start with the first row actually needed by the o
utput, |
| 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()->startIncrementalDecod
e(nativeInfo, |
| 238 pixels, rowBytes, &incrementalOptions, options.fColorPtr, option
s.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); |
| 260 const int lastRowInOutput = (rowsDecoded - startY) / sampleY; |
| 261 // FIXME: Should this be info or nativeInfo? Does it make a differen
ce? |
| 262 this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.f
ZeroInitialized, |
| 263 info.height(), lastRowInOutput); |
| 264 return SkCodec::kIncompleteInput; |
| 265 } else if (startResult != SkCodec::kUnimplemented) { |
| 266 return startResult; |
| 267 } // kUnimplemented means use the old method. |
| 268 } |
| 269 |
| 177 // Start the scanline decode. | 270 // Start the scanline decode. |
| 178 SkCodec::Result result = this->codec()->startScanlineDecode( | 271 SkCodec::Result result = this->codec()->startScanlineDecode(nativeInfo, |
| 179 info.makeWH(nativeSize.width(), nativeSize.height()), &sampledOption
s, | 272 &sampledOptions, options.fColorPtr, options.fColorCount); |
| 180 options.fColorPtr, options.fColorCount); | |
| 181 if (SkCodec::kSuccess != result) { | 273 if (SkCodec::kSuccess != result) { |
| 182 return result; | 274 return result; |
| 183 } | 275 } |
| 184 | 276 |
| 185 SkSampler* sampler = this->codec()->getSampler(true); | 277 SkSampler* sampler = this->codec()->getSampler(true); |
| 186 if (!sampler) { | 278 if (!sampler) { |
| 187 return SkCodec::kUnimplemented; | 279 return SkCodec::kUnimplemented; |
| 188 } | 280 } |
| 189 | 281 |
| 190 // Since we guarantee that output dimensions are always at least one (even i
f the sampleSize | |
| 191 // is greater than a given dimension), the input sampleSize is not always th
e 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()) { | 282 if (sampler->setSampleX(sampleX) != info.width()) { |
| 196 return SkCodec::kInvalidScale; | 283 return SkCodec::kInvalidScale; |
| 197 } | 284 } |
| 198 if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) { | 285 if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) { |
| 199 return SkCodec::kInvalidScale; | 286 return SkCodec::kInvalidScale; |
| 200 } | 287 } |
| 201 | 288 |
| 202 const int samplingOffsetY = get_start_coord(sampleY); | |
| 203 const int startY = samplingOffsetY + subsetY; | |
| 204 int dstHeight = info.height(); | |
| 205 switch(this->codec()->getScanlineOrder()) { | 289 switch(this->codec()->getScanlineOrder()) { |
| 206 case SkCodec::kTopDown_SkScanlineOrder: { | 290 case SkCodec::kTopDown_SkScanlineOrder: { |
| 207 if (!this->codec()->skipScanlines(startY)) { | 291 if (!this->codec()->skipScanlines(startY)) { |
| 208 this->codec()->fillIncompleteImage(info, pixels, rowBytes, optio
ns.fZeroInitialized, | 292 this->codec()->fillIncompleteImage(info, pixels, rowBytes, optio
ns.fZeroInitialized, |
| 209 dstHeight, 0); | 293 dstHeight, 0); |
| 210 return SkCodec::kIncompleteInput; | 294 return SkCodec::kIncompleteInput; |
| 211 } | 295 } |
| 212 void* pixelPtr = pixels; | 296 void* pixelPtr = pixels; |
| 213 for (int y = 0; y < dstHeight; y++) { | 297 for (int y = 0; y < dstHeight; y++) { |
| 214 if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) { | 298 if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 int srcY = this->codec()->outputScanline(y); | 343 int srcY = this->codec()->outputScanline(y); |
| 260 if (!is_coord_necessary(srcY, sampleY, dstHeight)) { | 344 if (!is_coord_necessary(srcY, sampleY, dstHeight)) { |
| 261 continue; | 345 continue; |
| 262 } | 346 } |
| 263 | 347 |
| 264 void* rowPtr = SkTAddOffset<void>(pixels, rowBytes * get_dst_coo
rd(srcY, sampleY)); | 348 void* rowPtr = SkTAddOffset<void>(pixels, rowBytes * get_dst_coo
rd(srcY, sampleY)); |
| 265 SkSampler::Fill(fillInfo, rowPtr, rowBytes, fillValue, options.f
ZeroInitialized); | 349 SkSampler::Fill(fillInfo, rowPtr, rowBytes, fillValue, options.f
ZeroInitialized); |
| 266 } | 350 } |
| 267 return SkCodec::kIncompleteInput; | 351 return SkCodec::kIncompleteInput; |
| 268 } | 352 } |
| 269 case SkCodec::kNone_SkScanlineOrder: { | |
| 270 const int linesNeeded = subsetHeight - samplingOffsetY; | |
| 271 SkAutoTMalloc<uint8_t> storage(linesNeeded * rowBytes); | |
| 272 uint8_t* storagePtr = storage.get(); | |
| 273 | |
| 274 if (!this->codec()->skipScanlines(startY)) { | |
| 275 this->codec()->fillIncompleteImage(info, pixels, rowBytes, optio
ns.fZeroInitialized, | |
| 276 dstHeight, 0); | |
| 277 return SkCodec::kIncompleteInput; | |
| 278 } | |
| 279 int scanlines = this->codec()->getScanlines(storagePtr, linesNeeded,
rowBytes); | |
| 280 | |
| 281 for (int y = 0; y < dstHeight; y++) { | |
| 282 memcpy(pixels, storagePtr, info.minRowBytes()); | |
| 283 storagePtr += sampleY * rowBytes; | |
| 284 pixels = SkTAddOffset<void>(pixels, rowBytes); | |
| 285 } | |
| 286 | |
| 287 if (scanlines < dstHeight) { | |
| 288 // this->codec() has already handled filling uninitialized memor
y. | |
| 289 return SkCodec::kIncompleteInput; | |
| 290 } | |
| 291 return SkCodec::kSuccess; | |
| 292 } | |
| 293 default: | 353 default: |
| 294 SkASSERT(false); | 354 SkASSERT(false); |
| 295 return SkCodec::kUnimplemented; | 355 return SkCodec::kUnimplemented; |
| 296 } | 356 } |
| 297 } | 357 } |
| OLD | NEW |