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 "SkJpegCodec.h" | 9 #include "SkJpegCodec.h" |
10 #include "SkJpegDecoderMgr.h" | 10 #include "SkJpegDecoderMgr.h" |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 {} | 148 {} |
149 | 149 |
150 /* | 150 /* |
151 * Return the row bytes of a particular image type and width | 151 * Return the row bytes of a particular image type and width |
152 */ | 152 */ |
153 static int get_row_bytes(const j_decompress_ptr dinfo) { | 153 static int get_row_bytes(const j_decompress_ptr dinfo) { |
154 int colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : dinfo->out_col or_components; | 154 int colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : dinfo->out_col or_components; |
155 return dinfo->output_width * colorBytes; | 155 return dinfo->output_width * colorBytes; |
156 | 156 |
157 } | 157 } |
158 | |
159 void calc_output_dimensions(jpeg_decompress_struct* dinfo, unsigned int num, uns igned int denom) { | |
160 dinfo->num_components = 0; | |
msarett
2015/10/01 19:34:34
This line belongs in onGetScaledDimensions(), I th
scroggo
2015/10/01 21:16:57
This function is only ever called on a fake dinfo
msarett
2015/10/01 22:53:07
Comment works for me.
| |
161 dinfo->scale_num = num; | |
162 dinfo->scale_denom = denom; | |
163 jpeg_calc_output_dimensions(dinfo); | |
164 } | |
165 | |
158 /* | 166 /* |
159 * Return a valid set of output dimensions for this decoder, given an input scal e | 167 * Return a valid set of output dimensions for this decoder, given an input scal e |
160 */ | 168 */ |
161 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { | 169 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { |
162 // libjpeg-turbo supports scaling by 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1, so we will | 170 // libjpeg-turbo supports scaling by 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1, so we will |
163 // support these as well | 171 // support these as well |
164 long num; | 172 unsigned int num; |
165 long denom = 8; | 173 unsigned int denom = 8; |
166 if (desiredScale > 0.875f) { | 174 if (desiredScale > 0.875f) { |
167 num = 8; | 175 num = 8; |
168 } else if (desiredScale > 0.75f) { | 176 } else if (desiredScale > 0.75f) { |
169 num = 7; | 177 num = 7; |
170 } else if (desiredScale > 0.625f) { | 178 } else if (desiredScale > 0.625f) { |
171 num = 6; | 179 num = 6; |
172 } else if (desiredScale > 0.5f) { | 180 } else if (desiredScale > 0.5f) { |
173 num = 5; | 181 num = 5; |
174 } else if (desiredScale > 0.375f) { | 182 } else if (desiredScale > 0.375f) { |
175 num = 4; | 183 num = 4; |
176 } else if (desiredScale > 0.25f) { | 184 } else if (desiredScale > 0.25f) { |
177 num = 3; | 185 num = 3; |
178 } else if (desiredScale > 0.125f) { | 186 } else if (desiredScale > 0.125f) { |
179 num = 2; | 187 num = 2; |
180 } else { | 188 } else { |
181 num = 1; | 189 num = 1; |
182 } | 190 } |
183 | 191 |
184 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions | 192 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions |
185 jpeg_decompress_struct dinfo; | 193 jpeg_decompress_struct dinfo; |
186 sk_bzero(&dinfo, sizeof(dinfo)); | 194 sk_bzero(&dinfo, sizeof(dinfo)); |
187 dinfo.image_width = this->getInfo().width(); | 195 dinfo.image_width = this->getInfo().width(); |
188 dinfo.image_height = this->getInfo().height(); | 196 dinfo.image_height = this->getInfo().height(); |
189 dinfo.global_state = fReadyState; | 197 dinfo.global_state = fReadyState; |
190 dinfo.num_components = 0; | 198 calc_output_dimensions(&dinfo, num, denom); |
191 dinfo.scale_num = num; | |
192 dinfo.scale_denom = denom; | |
193 jpeg_calc_output_dimensions(&dinfo); | |
194 | 199 |
195 // Return the calculated output dimensions for the given scale | 200 // Return the calculated output dimensions for the given scale |
196 return SkISize::Make(dinfo.output_width, dinfo.output_height); | 201 return SkISize::Make(dinfo.output_width, dinfo.output_height); |
197 } | 202 } |
198 | 203 |
199 bool SkJpegCodec::onRewind() { | 204 bool SkJpegCodec::onRewind() { |
200 JpegDecoderMgr* decoderMgr = nullptr; | 205 JpegDecoderMgr* decoderMgr = nullptr; |
201 if (!ReadHeader(this->stream(), nullptr, &decoderMgr)) { | 206 if (!ReadHeader(this->stream(), nullptr, &decoderMgr)) { |
202 return fDecoderMgr->returnFalse("could not rewind"); | 207 return fDecoderMgr->returnFalse("could not rewind"); |
203 } | 208 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 return true; | 270 return true; |
266 default: | 271 default: |
267 return false; | 272 return false; |
268 } | 273 } |
269 } | 274 } |
270 | 275 |
271 /* | 276 /* |
272 * Checks if we can natively scale to the requested dimensions and natively scal es the | 277 * Checks if we can natively scale to the requested dimensions and natively scal es the |
273 * dimensions if possible | 278 * dimensions if possible |
274 */ | 279 */ |
275 bool SkJpegCodec::nativelyScaleToDimensions(uint32_t dstWidth, uint32_t dstHeigh t) { | 280 bool SkJpegCodec::onDimensionsSupported(const SkISize& size) { |
281 if (setjmp(fDecoderMgr->getJmpBuf())) { | |
282 return fDecoderMgr->returnFalse("onDimensionsSupported/setjmp"); | |
283 } | |
284 | |
285 const int dstWidth = size.width(); | |
286 const int dstHeight = size.height(); | |
287 | |
288 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions | |
scroggo
2015/10/01 15:33:14
The old code crashed for me when I attempted to ca
msarett
2015/10/01 19:34:34
That's surprising. Hopefully it's because of the
scroggo
2015/10/01 21:16:57
I agree that it's worth figuring out why the other
msarett
2015/10/01 22:53:07
I'll file a bug on myself.
| |
289 jpeg_decompress_struct dinfo; | |
290 sk_bzero(&dinfo, sizeof(dinfo)); | |
291 dinfo.image_width = this->getInfo().width(); | |
292 dinfo.image_height = this->getInfo().height(); | |
293 dinfo.global_state = fReadyState; | |
294 | |
276 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1 | 295 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1 |
277 fDecoderMgr->dinfo()->scale_denom = 8; | 296 unsigned int num = 8; |
278 fDecoderMgr->dinfo()->scale_num = 8; | 297 const unsigned int denom = 8; |
279 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 298 calc_output_dimensions(&dinfo, num, denom); |
280 while (fDecoderMgr->dinfo()->output_width != dstWidth || | 299 while (dinfo.output_width != dstWidth || dinfo.output_height != dstHeight) { |
281 fDecoderMgr->dinfo()->output_height != dstHeight) { | |
282 | 300 |
283 // Return a failure if we have tried all of the possible scales | 301 // Return a failure if we have tried all of the possible scales |
284 if (1 == fDecoderMgr->dinfo()->scale_num || | 302 if (1 == num || dstWidth > dinfo.output_width || dstHeight > dinfo.outpu t_height) { |
285 dstWidth > fDecoderMgr->dinfo()->output_width || | |
286 dstHeight > fDecoderMgr->dinfo()->output_height) { | |
287 // reset native scale settings on failure because this may be suppor ted by the swizzler | |
288 this->fDecoderMgr->dinfo()->scale_num = 8; | |
289 jpeg_calc_output_dimensions(this->fDecoderMgr->dinfo()); | |
290 return false; | 303 return false; |
291 } | 304 } |
292 | 305 |
293 // Try the next scale | 306 // Try the next scale |
294 fDecoderMgr->dinfo()->scale_num -= 1; | 307 num -= 1; |
295 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 308 calc_output_dimensions(&dinfo, num, denom); |
296 } | 309 } |
310 | |
311 fDecoderMgr->dinfo()->scale_num = num; | |
312 fDecoderMgr->dinfo()->scale_denom = denom; | |
297 return true; | 313 return true; |
298 } | 314 } |
299 | 315 |
300 /* | 316 /* |
301 * Performs the jpeg decode | 317 * Performs the jpeg decode |
302 */ | 318 */ |
303 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, | 319 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, |
304 void* dst, size_t dstRowBytes, | 320 void* dst, size_t dstRowBytes, |
305 const Options& options, SkPMColor*, int *) { | 321 const Options& options, SkPMColor*, int *) { |
306 if (options.fSubset) { | 322 if (options.fSubset) { |
307 // Subsets are not supported. | 323 // Subsets are not supported. |
308 return kUnimplemented; | 324 return kUnimplemented; |
309 } | 325 } |
310 | 326 |
311 // Get a pointer to the decompress info since we will use it quite frequentl y | 327 // Get a pointer to the decompress info since we will use it quite frequentl y |
312 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); | 328 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
313 | 329 |
314 // Set the jump location for libjpeg errors | 330 // Set the jump location for libjpeg errors |
315 if (setjmp(fDecoderMgr->getJmpBuf())) { | 331 if (setjmp(fDecoderMgr->getJmpBuf())) { |
316 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 332 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
317 } | 333 } |
318 | 334 |
319 // Check if we can decode to the requested destination and set the output co lor space | 335 // Check if we can decode to the requested destination and set the output co lor space |
320 if (!this->setOutputColorSpace(dstInfo)) { | 336 if (!this->setOutputColorSpace(dstInfo)) { |
321 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion); | 337 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion); |
322 } | 338 } |
323 | 339 |
324 // Perform the necessary scaling | |
325 if (!this->nativelyScaleToDimensions(dstInfo.width(), dstInfo.height())) { | |
326 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInv alidScale); | |
327 } | |
328 | |
329 // Now, given valid output dimensions, we can start the decompress | 340 // Now, given valid output dimensions, we can start the decompress |
330 if (!jpeg_start_decompress(dinfo)) { | 341 if (!jpeg_start_decompress(dinfo)) { |
331 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); | 342 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
332 } | 343 } |
333 | 344 |
334 // The recommended output buffer height should always be 1 in high quality m odes. | 345 // The recommended output buffer height should always be 1 in high quality m odes. |
335 // If it's not, we want to know because it means our strategy is not optimal . | 346 // If it's not, we want to know because it means our strategy is not optimal . |
336 SkASSERT(1 == dinfo->rec_outbuf_height); | 347 SkASSERT(1 == dinfo->rec_outbuf_height); |
337 | 348 |
338 // Perform the decode a single row at a time | 349 // Perform the decode a single row at a time |
(...skipping 30 matching lines...) Expand all Loading... | |
369 } | 380 } |
370 | 381 |
371 // Move to the next row | 382 // Move to the next row |
372 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); | 383 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); |
373 } | 384 } |
374 jpeg_finish_decompress(dinfo); | 385 jpeg_finish_decompress(dinfo); |
375 | 386 |
376 return kSuccess; | 387 return kSuccess; |
377 } | 388 } |
378 | 389 |
379 SkCodec::Result SkJpegCodec::initializeSwizzler(const SkImageInfo& info, const O ptions& options) { | 390 SkSampler* SkJpegCodec::getSampler() { |
391 if (fSwizzler) { | |
392 SkASSERT(fSrcRow && static_cast<uint8_t*>(fStorage.get()) == fSrcRow); | |
393 return fSwizzler; | |
394 } | |
395 | |
396 const SkImageInfo& info = this->dstInfo(); | |
380 SkSwizzler::SrcConfig srcConfig; | 397 SkSwizzler::SrcConfig srcConfig; |
381 switch (info.colorType()) { | 398 switch (info.colorType()) { |
382 case kGray_8_SkColorType: | 399 case kGray_8_SkColorType: |
383 srcConfig = SkSwizzler::kGray; | 400 srcConfig = SkSwizzler::kGray; |
384 break; | 401 break; |
385 case kRGBA_8888_SkColorType: | 402 case kRGBA_8888_SkColorType: |
386 srcConfig = SkSwizzler::kRGBX; | 403 srcConfig = SkSwizzler::kRGBX; |
387 break; | 404 break; |
388 case kBGRA_8888_SkColorType: | 405 case kBGRA_8888_SkColorType: |
389 srcConfig = SkSwizzler::kBGRX; | 406 srcConfig = SkSwizzler::kBGRX; |
390 break; | 407 break; |
391 case kRGB_565_SkColorType: | 408 case kRGB_565_SkColorType: |
392 srcConfig = SkSwizzler::kRGB_565; | 409 srcConfig = SkSwizzler::kRGB_565; |
393 break; | 410 break; |
394 default: | 411 default: |
395 // This function should only be called if the colorType is supported by jpeg | 412 // This function should only be called if the colorType is supported by jpeg |
396 SkASSERT(false); | 413 SkASSERT(false); |
397 } | 414 } |
398 | 415 |
399 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, info, options .fZeroInitialized, | 416 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, info, |
400 this->getInfo())); | 417 this->options().fZeroInitialized) ); |
401 if (!fSwizzler) { | 418 if (!fSwizzler) { |
402 return SkCodec::kUnimplemented; | 419 return nullptr; |
403 } | 420 } |
404 | 421 |
405 return kSuccess; | 422 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); |
423 fSrcRow = static_cast<uint8_t*>(fStorage.get()); | |
424 return fSwizzler; | |
406 } | 425 } |
407 | 426 |
408 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, | 427 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
409 const Options& options, SkPMColor ctable[], int* ctableCount) { | 428 const Options& options, SkPMColor ctable[], int* ctableCount) { |
410 // Set the jump location for libjpeg errors | 429 // Set the jump location for libjpeg errors |
411 if (setjmp(fDecoderMgr->getJmpBuf())) { | 430 if (setjmp(fDecoderMgr->getJmpBuf())) { |
412 SkCodecPrintf("setjmp: Error from libjpeg\n"); | 431 SkCodecPrintf("setjmp: Error from libjpeg\n"); |
413 return kInvalidInput; | 432 return kInvalidInput; |
414 } | 433 } |
415 | 434 |
416 // Check if we can decode to the requested destination and set the output co lor space | 435 // Check if we can decode to the requested destination and set the output co lor space |
417 if (!this->setOutputColorSpace(dstInfo)) { | 436 if (!this->setOutputColorSpace(dstInfo)) { |
418 return kInvalidConversion; | 437 return kInvalidConversion; |
419 } | 438 } |
420 | 439 |
421 // Perform the necessary scaling | 440 // Remove objects used for sampling. |
422 if (!this->nativelyScaleToDimensions(dstInfo.width(), dstInfo.height())) { | 441 fSwizzler.reset(nullptr); |
423 // full native scaling to dstInfo dimensions not supported | 442 fSrcRow = nullptr; |
424 | 443 fStorage.free(); |
msarett
2015/10/01 19:34:34
I'm a little confused here...
Why can we do this/
scroggo
2015/10/01 21:16:57
Because onGetScanlines changes its behavior based
msarett
2015/10/01 22:53:07
Thanks!
| |
425 if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(), dstI nfo)) { | |
426 return kInvalidScale; | |
427 } | |
428 // create swizzler for sampling | |
429 Result result = this->initializeSwizzler(dstInfo, options); | |
430 if (kSuccess != result) { | |
431 SkCodecPrintf("failed to initialize the swizzler.\n"); | |
432 return result; | |
433 } | |
434 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); | |
435 fSrcRow = static_cast<uint8_t*>(fStorage.get()); | |
436 } else { | |
437 fSrcRow = nullptr; | |
438 fSwizzler.reset(nullptr); | |
439 } | |
440 | 444 |
441 // Now, given valid output dimensions, we can start the decompress | 445 // Now, given valid output dimensions, we can start the decompress |
442 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { | 446 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
443 SkCodecPrintf("start decompress failed\n"); | 447 SkCodecPrintf("start decompress failed\n"); |
444 return kInvalidInput; | 448 return kInvalidInput; |
445 } | 449 } |
446 | 450 |
447 return kSuccess; | 451 return kSuccess; |
448 } | 452 } |
449 | 453 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
517 // Set the jump location for libjpeg errors | 521 // Set the jump location for libjpeg errors |
518 if (setjmp(fDecoderMgr->getJmpBuf())) { | 522 if (setjmp(fDecoderMgr->getJmpBuf())) { |
519 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 523 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
520 } | 524 } |
521 | 525 |
522 jpeg_skip_scanlines(fDecoderMgr->dinfo(), count); | 526 jpeg_skip_scanlines(fDecoderMgr->dinfo(), count); |
523 | 527 |
524 return kSuccess; | 528 return kSuccess; |
525 } | 529 } |
526 | 530 |
OLD | NEW |