msarett | e16b04a | 2015-04-15 07:32:19 -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 | |
| 8 | #include "SkJpegDecoderMgr.h" |
mtklein | 525e90a | 2015-06-18 09:58:57 -0700 | [diff] [blame] | 9 | #include "SkJpegUtility_codec.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 10 | |
| 11 | /* |
| 12 | * Print information, warning, and error messages |
| 13 | */ |
| 14 | static void print_message(const j_common_ptr info, const char caller[]) { |
| 15 | char buffer[JMSG_LENGTH_MAX]; |
| 16 | info->err->format_message(info, buffer); |
| 17 | SkCodecPrintf("libjpeg error %d <%s> from %s\n", info->err->msg_code, buffer, caller); |
| 18 | } |
| 19 | |
| 20 | /* |
| 21 | * Reporting functions for libjpeg |
| 22 | */ |
| 23 | static void emit_message(j_common_ptr info, int) { |
| 24 | print_message(info, "emit_message"); |
| 25 | } |
| 26 | static void output_message(j_common_ptr info) { |
| 27 | print_message(info, "output_message"); |
| 28 | } |
| 29 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 30 | bool JpegDecoderMgr::returnFalse(const char caller[]) { |
| 31 | print_message((j_common_ptr) &fDInfo, caller); |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | SkCodec::Result JpegDecoderMgr::returnFailure(const char caller[], SkCodec::Result result) { |
| 36 | print_message((j_common_ptr) &fDInfo, caller); |
| 37 | return result; |
| 38 | } |
| 39 | |
| 40 | SkColorType JpegDecoderMgr::getColorType() { |
| 41 | switch (fDInfo.jpeg_color_space) { |
msarett | aa2a7de | 2015-07-01 13:11:08 -0700 | [diff] [blame^] | 42 | case JCS_CMYK: |
| 43 | case JCS_YCCK: |
| 44 | // libjpeg cannot convert from CMYK or YCCK to RGB. |
| 45 | // Here, we ask libjpeg to give us CMYK samples back and |
| 46 | // we will later manually convert them to RGB. |
| 47 | fDInfo.out_color_space = JCS_CMYK; |
| 48 | return kN32_SkColorType; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 49 | case JCS_GRAYSCALE: |
msarett | aa2a7de | 2015-07-01 13:11:08 -0700 | [diff] [blame^] | 50 | fDInfo.out_color_space = JCS_GRAYSCALE; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 51 | return kGray_8_SkColorType; |
| 52 | default: |
msarett | aa2a7de | 2015-07-01 13:11:08 -0700 | [diff] [blame^] | 53 | #ifdef ANDROID_RGB |
| 54 | fDInfo.out_color_space = JCS_RGBA_8888; |
| 55 | #else |
| 56 | fDInfo.out_color_space = JCS_RGB; |
| 57 | #endif |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 58 | return kN32_SkColorType; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | JpegDecoderMgr::JpegDecoderMgr(SkStream* stream) |
| 63 | : fSrcMgr(stream) |
| 64 | , fInit(false) |
| 65 | { |
| 66 | // Error manager must be set before any calls to libjeg in order to handle failures |
msarett | aa2a7de | 2015-07-01 13:11:08 -0700 | [diff] [blame^] | 67 | fDInfo.err = jpeg_std_error(&fErrorMgr); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 68 | fErrorMgr.error_exit = skjpeg_err_exit; |
| 69 | } |
| 70 | |
| 71 | void JpegDecoderMgr::init() { |
| 72 | jpeg_create_decompress(&fDInfo); |
| 73 | fInit = true; |
| 74 | fDInfo.src = &fSrcMgr; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 75 | fDInfo.err->emit_message = &emit_message; |
| 76 | fDInfo.err->output_message = &output_message; |
| 77 | } |
| 78 | |
| 79 | JpegDecoderMgr::~JpegDecoderMgr() { |
| 80 | if (fInit) { |
| 81 | jpeg_destroy_decompress(&fDInfo); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | jmp_buf& JpegDecoderMgr::getJmpBuf() { |
| 86 | return fErrorMgr.fJmpBuf; |
| 87 | } |
| 88 | |
| 89 | jpeg_decompress_struct* JpegDecoderMgr::dinfo() { |
| 90 | return &fDInfo; |
| 91 | } |