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 | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 42 | case JCS_GRAYSCALE: |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 43 | return kGray_8_SkColorType; |
| 44 | default: |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 45 | return kN32_SkColorType; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | JpegDecoderMgr::JpegDecoderMgr(SkStream* stream) |
| 50 | : fSrcMgr(stream) |
| 51 | , fInit(false) |
| 52 | { |
| 53 | // Error manager must be set before any calls to libjeg in order to handle failures |
msarett | e9e3ee3 | 2015-07-01 12:36:18 -0700 | [diff] [blame] | 54 | fDInfo.err = turbo_jpeg_std_error(&fErrorMgr); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 55 | fErrorMgr.error_exit = skjpeg_err_exit; |
| 56 | } |
| 57 | |
| 58 | void JpegDecoderMgr::init() { |
| 59 | jpeg_create_decompress(&fDInfo); |
| 60 | fInit = true; |
| 61 | fDInfo.src = &fSrcMgr; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 62 | fDInfo.err->emit_message = &emit_message; |
| 63 | fDInfo.err->output_message = &output_message; |
| 64 | } |
| 65 | |
| 66 | JpegDecoderMgr::~JpegDecoderMgr() { |
| 67 | if (fInit) { |
| 68 | jpeg_destroy_decompress(&fDInfo); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | jmp_buf& JpegDecoderMgr::getJmpBuf() { |
| 73 | return fErrorMgr.fJmpBuf; |
| 74 | } |
| 75 | |
| 76 | jpeg_decompress_struct* JpegDecoderMgr::dinfo() { |
| 77 | return &fDInfo; |
| 78 | } |