blob: b5a12297a0c41b4f3e74ab194b0c66de14ff6842 [file] [log] [blame]
msarette16b04a2015-04-15 07:32:19 -07001/*
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"
mtklein525e90a2015-06-18 09:58:57 -07009#include "SkJpegUtility_codec.h"
msarette16b04a2015-04-15 07:32:19 -070010
11/*
12 * Print information, warning, and error messages
13 */
14static 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 */
23static void emit_message(j_common_ptr info, int) {
24 print_message(info, "emit_message");
25}
26static void output_message(j_common_ptr info) {
27 print_message(info, "output_message");
28}
29
msarette16b04a2015-04-15 07:32:19 -070030bool JpegDecoderMgr::returnFalse(const char caller[]) {
31 print_message((j_common_ptr) &fDInfo, caller);
32 return false;
33}
34
35SkCodec::Result JpegDecoderMgr::returnFailure(const char caller[], SkCodec::Result result) {
36 print_message((j_common_ptr) &fDInfo, caller);
37 return result;
38}
39
40SkColorType JpegDecoderMgr::getColorType() {
41 switch (fDInfo.jpeg_color_space) {
msarette16b04a2015-04-15 07:32:19 -070042 case JCS_GRAYSCALE:
msarette16b04a2015-04-15 07:32:19 -070043 return kGray_8_SkColorType;
44 default:
msarette16b04a2015-04-15 07:32:19 -070045 return kN32_SkColorType;
46 }
47}
48
49JpegDecoderMgr::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
msarett1c8a5872015-07-07 08:50:01 -070054 fDInfo.err = turbo_jpeg_std_error(&fErrorMgr);
msarette16b04a2015-04-15 07:32:19 -070055 fErrorMgr.error_exit = skjpeg_err_exit;
56}
57
58void JpegDecoderMgr::init() {
59 jpeg_create_decompress(&fDInfo);
60 fInit = true;
61 fDInfo.src = &fSrcMgr;
msarette16b04a2015-04-15 07:32:19 -070062 fDInfo.err->emit_message = &emit_message;
63 fDInfo.err->output_message = &output_message;
64}
65
66JpegDecoderMgr::~JpegDecoderMgr() {
67 if (fInit) {
68 jpeg_destroy_decompress(&fDInfo);
69 }
70}
71
72jmp_buf& JpegDecoderMgr::getJmpBuf() {
73 return fErrorMgr.fJmpBuf;
74}
75
76jpeg_decompress_struct* JpegDecoderMgr::dinfo() {
77 return &fDInfo;
78}