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 /* |
| 160 * Calculate output dimensions based on the provided factors. |
| 161 * |
| 162 * Not to be used on the actual jpeg_decompress_struct used for decoding, since
it will |
| 163 * incorrectly modify num_components. |
| 164 */ |
| 165 void calc_output_dimensions(jpeg_decompress_struct* dinfo, unsigned int num, uns
igned int denom) { |
| 166 dinfo->num_components = 0; |
| 167 dinfo->scale_num = num; |
| 168 dinfo->scale_denom = denom; |
| 169 jpeg_calc_output_dimensions(dinfo); |
| 170 } |
| 171 |
158 /* | 172 /* |
159 * Return a valid set of output dimensions for this decoder, given an input scal
e | 173 * Return a valid set of output dimensions for this decoder, given an input scal
e |
160 */ | 174 */ |
161 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const { | 175 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 | 176 // 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 | 177 // support these as well |
164 long num; | 178 unsigned int num; |
165 long denom = 8; | 179 unsigned int denom = 8; |
166 if (desiredScale > 0.875f) { | 180 if (desiredScale > 0.875f) { |
167 num = 8; | 181 num = 8; |
168 } else if (desiredScale > 0.75f) { | 182 } else if (desiredScale > 0.75f) { |
169 num = 7; | 183 num = 7; |
170 } else if (desiredScale > 0.625f) { | 184 } else if (desiredScale > 0.625f) { |
171 num = 6; | 185 num = 6; |
172 } else if (desiredScale > 0.5f) { | 186 } else if (desiredScale > 0.5f) { |
173 num = 5; | 187 num = 5; |
174 } else if (desiredScale > 0.375f) { | 188 } else if (desiredScale > 0.375f) { |
175 num = 4; | 189 num = 4; |
176 } else if (desiredScale > 0.25f) { | 190 } else if (desiredScale > 0.25f) { |
177 num = 3; | 191 num = 3; |
178 } else if (desiredScale > 0.125f) { | 192 } else if (desiredScale > 0.125f) { |
179 num = 2; | 193 num = 2; |
180 } else { | 194 } else { |
181 num = 1; | 195 num = 1; |
182 } | 196 } |
183 | 197 |
184 // Set up a fake decompress struct in order to use libjpeg to calculate outp
ut dimensions | 198 // Set up a fake decompress struct in order to use libjpeg to calculate outp
ut dimensions |
185 jpeg_decompress_struct dinfo; | 199 jpeg_decompress_struct dinfo; |
186 sk_bzero(&dinfo, sizeof(dinfo)); | 200 sk_bzero(&dinfo, sizeof(dinfo)); |
187 dinfo.image_width = this->getInfo().width(); | 201 dinfo.image_width = this->getInfo().width(); |
188 dinfo.image_height = this->getInfo().height(); | 202 dinfo.image_height = this->getInfo().height(); |
189 dinfo.global_state = fReadyState; | 203 dinfo.global_state = fReadyState; |
190 dinfo.num_components = 0; | 204 calc_output_dimensions(&dinfo, num, denom); |
191 dinfo.scale_num = num; | |
192 dinfo.scale_denom = denom; | |
193 jpeg_calc_output_dimensions(&dinfo); | |
194 | 205 |
195 // Return the calculated output dimensions for the given scale | 206 // Return the calculated output dimensions for the given scale |
196 return SkISize::Make(dinfo.output_width, dinfo.output_height); | 207 return SkISize::Make(dinfo.output_width, dinfo.output_height); |
197 } | 208 } |
198 | 209 |
199 bool SkJpegCodec::onRewind() { | 210 bool SkJpegCodec::onRewind() { |
200 JpegDecoderMgr* decoderMgr = nullptr; | 211 JpegDecoderMgr* decoderMgr = nullptr; |
201 if (!ReadHeader(this->stream(), nullptr, &decoderMgr)) { | 212 if (!ReadHeader(this->stream(), nullptr, &decoderMgr)) { |
202 return fDecoderMgr->returnFalse("could not rewind"); | 213 return fDecoderMgr->returnFalse("could not rewind"); |
203 } | 214 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 return true; | 276 return true; |
266 default: | 277 default: |
267 return false; | 278 return false; |
268 } | 279 } |
269 } | 280 } |
270 | 281 |
271 /* | 282 /* |
272 * Checks if we can natively scale to the requested dimensions and natively scal
es the | 283 * Checks if we can natively scale to the requested dimensions and natively scal
es the |
273 * dimensions if possible | 284 * dimensions if possible |
274 */ | 285 */ |
275 bool SkJpegCodec::nativelyScaleToDimensions(uint32_t dstWidth, uint32_t dstHeigh
t) { | 286 bool SkJpegCodec::onDimensionsSupported(const SkISize& size) { |
| 287 if (setjmp(fDecoderMgr->getJmpBuf())) { |
| 288 return fDecoderMgr->returnFalse("onDimensionsSupported/setjmp"); |
| 289 } |
| 290 |
| 291 const unsigned int dstWidth = size.width(); |
| 292 const unsigned int dstHeight = size.height(); |
| 293 |
| 294 // Set up a fake decompress struct in order to use libjpeg to calculate outp
ut dimensions |
| 295 // FIXME: Why is this necessary? |
| 296 jpeg_decompress_struct dinfo; |
| 297 sk_bzero(&dinfo, sizeof(dinfo)); |
| 298 dinfo.image_width = this->getInfo().width(); |
| 299 dinfo.image_height = this->getInfo().height(); |
| 300 dinfo.global_state = fReadyState; |
| 301 |
276 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1 | 302 // 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; | 303 unsigned int num = 8; |
278 fDecoderMgr->dinfo()->scale_num = 8; | 304 const unsigned int denom = 8; |
279 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 305 calc_output_dimensions(&dinfo, num, denom); |
280 while (fDecoderMgr->dinfo()->output_width != dstWidth || | 306 while (dinfo.output_width != dstWidth || dinfo.output_height != dstHeight) { |
281 fDecoderMgr->dinfo()->output_height != dstHeight) { | |
282 | 307 |
283 // Return a failure if we have tried all of the possible scales | 308 // Return a failure if we have tried all of the possible scales |
284 if (1 == fDecoderMgr->dinfo()->scale_num || | 309 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; | 310 return false; |
291 } | 311 } |
292 | 312 |
293 // Try the next scale | 313 // Try the next scale |
294 fDecoderMgr->dinfo()->scale_num -= 1; | 314 num -= 1; |
295 jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 315 calc_output_dimensions(&dinfo, num, denom); |
296 } | 316 } |
| 317 |
| 318 fDecoderMgr->dinfo()->scale_num = num; |
| 319 fDecoderMgr->dinfo()->scale_denom = denom; |
297 return true; | 320 return true; |
298 } | 321 } |
299 | 322 |
300 /* | 323 /* |
301 * Performs the jpeg decode | 324 * Performs the jpeg decode |
302 */ | 325 */ |
303 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, | 326 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, |
304 void* dst, size_t dstRowBytes, | 327 void* dst, size_t dstRowBytes, |
305 const Options& options, SkPMColor*, int
*) { | 328 const Options& options, SkPMColor*, int
*) { |
306 if (options.fSubset) { | 329 if (options.fSubset) { |
307 // Subsets are not supported. | 330 // Subsets are not supported. |
308 return kUnimplemented; | 331 return kUnimplemented; |
309 } | 332 } |
310 | 333 |
311 // Get a pointer to the decompress info since we will use it quite frequentl
y | 334 // Get a pointer to the decompress info since we will use it quite frequentl
y |
312 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); | 335 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); |
313 | 336 |
314 // Set the jump location for libjpeg errors | 337 // Set the jump location for libjpeg errors |
315 if (setjmp(fDecoderMgr->getJmpBuf())) { | 338 if (setjmp(fDecoderMgr->getJmpBuf())) { |
316 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 339 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
317 } | 340 } |
318 | 341 |
319 // Check if we can decode to the requested destination and set the output co
lor space | 342 // Check if we can decode to the requested destination and set the output co
lor space |
320 if (!this->setOutputColorSpace(dstInfo)) { | 343 if (!this->setOutputColorSpace(dstInfo)) { |
321 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers
ion); | 344 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers
ion); |
322 } | 345 } |
323 | 346 |
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 | 347 // Now, given valid output dimensions, we can start the decompress |
330 if (!jpeg_start_decompress(dinfo)) { | 348 if (!jpeg_start_decompress(dinfo)) { |
331 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); | 349 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); |
332 } | 350 } |
333 | 351 |
334 // The recommended output buffer height should always be 1 in high quality m
odes. | 352 // 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
. | 353 // If it's not, we want to know because it means our strategy is not optimal
. |
336 SkASSERT(1 == dinfo->rec_outbuf_height); | 354 SkASSERT(1 == dinfo->rec_outbuf_height); |
337 | 355 |
338 // Perform the decode a single row at a time | 356 // Perform the decode a single row at a time |
(...skipping 30 matching lines...) Expand all Loading... |
369 } | 387 } |
370 | 388 |
371 // Move to the next row | 389 // Move to the next row |
372 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); | 390 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes); |
373 } | 391 } |
374 jpeg_finish_decompress(dinfo); | 392 jpeg_finish_decompress(dinfo); |
375 | 393 |
376 return kSuccess; | 394 return kSuccess; |
377 } | 395 } |
378 | 396 |
379 SkCodec::Result SkJpegCodec::initializeSwizzler(const SkImageInfo& info, const O
ptions& options) { | 397 SkSampler* SkJpegCodec::getSampler() { |
| 398 if (fSwizzler) { |
| 399 SkASSERT(fSrcRow && static_cast<uint8_t*>(fStorage.get()) == fSrcRow); |
| 400 return fSwizzler; |
| 401 } |
| 402 |
| 403 const SkImageInfo& info = this->dstInfo(); |
380 SkSwizzler::SrcConfig srcConfig; | 404 SkSwizzler::SrcConfig srcConfig; |
381 switch (info.colorType()) { | 405 switch (info.colorType()) { |
382 case kGray_8_SkColorType: | 406 case kGray_8_SkColorType: |
383 srcConfig = SkSwizzler::kGray; | 407 srcConfig = SkSwizzler::kGray; |
384 break; | 408 break; |
385 case kRGBA_8888_SkColorType: | 409 case kRGBA_8888_SkColorType: |
386 srcConfig = SkSwizzler::kRGBX; | 410 srcConfig = SkSwizzler::kRGBX; |
387 break; | 411 break; |
388 case kBGRA_8888_SkColorType: | 412 case kBGRA_8888_SkColorType: |
389 srcConfig = SkSwizzler::kBGRX; | 413 srcConfig = SkSwizzler::kBGRX; |
390 break; | 414 break; |
391 case kRGB_565_SkColorType: | 415 case kRGB_565_SkColorType: |
392 srcConfig = SkSwizzler::kRGB_565; | 416 srcConfig = SkSwizzler::kRGB_565; |
393 break; | 417 break; |
394 default: | 418 default: |
395 // This function should only be called if the colorType is supported
by jpeg | 419 // This function should only be called if the colorType is supported
by jpeg |
396 SkASSERT(false); | 420 SkASSERT(false); |
397 } | 421 } |
398 | 422 |
399 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, info, options
.fZeroInitialized, | 423 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, info, |
400 this->getInfo())); | 424 this->options().fZeroInitialized)
); |
401 if (!fSwizzler) { | 425 if (!fSwizzler) { |
402 return SkCodec::kUnimplemented; | 426 return nullptr; |
403 } | 427 } |
404 | 428 |
405 return kSuccess; | 429 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); |
| 430 fSrcRow = static_cast<uint8_t*>(fStorage.get()); |
| 431 return fSwizzler; |
406 } | 432 } |
407 | 433 |
408 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, | 434 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
409 const Options& options, SkPMColor ctable[], int* ctableCount) { | 435 const Options& options, SkPMColor ctable[], int* ctableCount) { |
410 // Set the jump location for libjpeg errors | 436 // Set the jump location for libjpeg errors |
411 if (setjmp(fDecoderMgr->getJmpBuf())) { | 437 if (setjmp(fDecoderMgr->getJmpBuf())) { |
412 SkCodecPrintf("setjmp: Error from libjpeg\n"); | 438 SkCodecPrintf("setjmp: Error from libjpeg\n"); |
413 return kInvalidInput; | 439 return kInvalidInput; |
414 } | 440 } |
415 | 441 |
416 // Check if we can decode to the requested destination and set the output co
lor space | 442 // Check if we can decode to the requested destination and set the output co
lor space |
417 if (!this->setOutputColorSpace(dstInfo)) { | 443 if (!this->setOutputColorSpace(dstInfo)) { |
418 return kInvalidConversion; | 444 return kInvalidConversion; |
419 } | 445 } |
420 | 446 |
421 // Perform the necessary scaling | 447 // Remove objects used for sampling. |
422 if (!this->nativelyScaleToDimensions(dstInfo.width(), dstInfo.height())) { | 448 fSwizzler.reset(nullptr); |
423 // full native scaling to dstInfo dimensions not supported | 449 fSrcRow = nullptr; |
424 | 450 fStorage.free(); |
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 | 451 |
441 // Now, given valid output dimensions, we can start the decompress | 452 // Now, given valid output dimensions, we can start the decompress |
442 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { | 453 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { |
443 SkCodecPrintf("start decompress failed\n"); | 454 SkCodecPrintf("start decompress failed\n"); |
444 return kInvalidInput; | 455 return kInvalidInput; |
445 } | 456 } |
446 | 457 |
447 return kSuccess; | 458 return kSuccess; |
448 } | 459 } |
449 | 460 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 // Set the jump location for libjpeg errors | 528 // Set the jump location for libjpeg errors |
518 if (setjmp(fDecoderMgr->getJmpBuf())) { | 529 if (setjmp(fDecoderMgr->getJmpBuf())) { |
519 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | 530 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); |
520 } | 531 } |
521 | 532 |
522 jpeg_skip_scanlines(fDecoderMgr->dinfo(), count); | 533 jpeg_skip_scanlines(fDecoderMgr->dinfo(), count); |
523 | 534 |
524 return kSuccess; | 535 return kSuccess; |
525 } | 536 } |
526 | 537 |
OLD | NEW |