blob: f0ed4522ca6e562eac23e5134e8bc315c02d7c60 [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) {
msarettaa2a7de2015-07-01 13:11:08 -070042 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;
msarette16b04a2015-04-15 07:32:19 -070049 case JCS_GRAYSCALE:
msarettaa2a7de2015-07-01 13:11:08 -070050 fDInfo.out_color_space = JCS_GRAYSCALE;
msarette16b04a2015-04-15 07:32:19 -070051 return kGray_8_SkColorType;
52 default:
msarettaa2a7de2015-07-01 13:11:08 -070053#ifdef ANDROID_RGB
54 fDInfo.out_color_space = JCS_RGBA_8888;
55#else
56 fDInfo.out_color_space = JCS_RGB;
57#endif
msarette16b04a2015-04-15 07:32:19 -070058 return kN32_SkColorType;
59 }
60}
61
62JpegDecoderMgr::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
msarettaa2a7de2015-07-01 13:11:08 -070067 fDInfo.err = jpeg_std_error(&fErrorMgr);
msarette16b04a2015-04-15 07:32:19 -070068 fErrorMgr.error_exit = skjpeg_err_exit;
69}
70
71void JpegDecoderMgr::init() {
72 jpeg_create_decompress(&fDInfo);
73 fInit = true;
74 fDInfo.src = &fSrcMgr;
msarette16b04a2015-04-15 07:32:19 -070075 fDInfo.err->emit_message = &emit_message;
76 fDInfo.err->output_message = &output_message;
77}
78
79JpegDecoderMgr::~JpegDecoderMgr() {
80 if (fInit) {
81 jpeg_destroy_decompress(&fDInfo);
82 }
83}
84
85jmp_buf& JpegDecoderMgr::getJmpBuf() {
86 return fErrorMgr.fJmpBuf;
87}
88
89jpeg_decompress_struct* JpegDecoderMgr::dinfo() {
90 return &fDInfo;
91}