blob: cc44f3c42117cd1b285ee5651180f47bd2b602df [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/*
msarett6dfe9ac2015-11-10 11:22:12 -080021 * Reporting function for error and warning messages.
msarette16b04a2015-04-15 07:32:19 -070022 */
msarette16b04a2015-04-15 07:32:19 -070023static void output_message(j_common_ptr info) {
24 print_message(info, "output_message");
25}
26
msarette16b04a2015-04-15 07:32:19 -070027bool JpegDecoderMgr::returnFalse(const char caller[]) {
28 print_message((j_common_ptr) &fDInfo, caller);
29 return false;
30}
31
32SkCodec::Result JpegDecoderMgr::returnFailure(const char caller[], SkCodec::Result result) {
33 print_message((j_common_ptr) &fDInfo, caller);
34 return result;
35}
36
37SkColorType JpegDecoderMgr::getColorType() {
38 switch (fDInfo.jpeg_color_space) {
msarette16b04a2015-04-15 07:32:19 -070039 case JCS_GRAYSCALE:
msarette16b04a2015-04-15 07:32:19 -070040 return kGray_8_SkColorType;
41 default:
msarette16b04a2015-04-15 07:32:19 -070042 return kN32_SkColorType;
43 }
44}
45
46JpegDecoderMgr::JpegDecoderMgr(SkStream* stream)
47 : fSrcMgr(stream)
48 , fInit(false)
49{
50 // Error manager must be set before any calls to libjeg in order to handle failures
msarettfbccb592015-09-01 06:43:41 -070051 fDInfo.err = jpeg_std_error(&fErrorMgr);
msarette16b04a2015-04-15 07:32:19 -070052 fErrorMgr.error_exit = skjpeg_err_exit;
53}
54
55void JpegDecoderMgr::init() {
56 jpeg_create_decompress(&fDInfo);
57 fInit = true;
58 fDInfo.src = &fSrcMgr;
msarette16b04a2015-04-15 07:32:19 -070059 fDInfo.err->output_message = &output_message;
60}
61
62JpegDecoderMgr::~JpegDecoderMgr() {
63 if (fInit) {
64 jpeg_destroy_decompress(&fDInfo);
65 }
66}
67
68jmp_buf& JpegDecoderMgr::getJmpBuf() {
69 return fErrorMgr.fJmpBuf;
70}
71
72jpeg_decompress_struct* JpegDecoderMgr::dinfo() {
73 return &fDInfo;
74}