blob: 8517e0dac2ec494315e67eacb8b532efe41655bb [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"
msarettc1d03122016-03-25 08:58:55 -07009
10#include "SkJpegUtility.h"
msarette16b04a2015-04-15 07:32:19 -070011
12/*
13 * Print information, warning, and error messages
14 */
15static void print_message(const j_common_ptr info, const char caller[]) {
16 char buffer[JMSG_LENGTH_MAX];
17 info->err->format_message(info, buffer);
18 SkCodecPrintf("libjpeg error %d <%s> from %s\n", info->err->msg_code, buffer, caller);
19}
20
21/*
msarett6dfe9ac2015-11-10 11:22:12 -080022 * Reporting function for error and warning messages.
msarette16b04a2015-04-15 07:32:19 -070023 */
msarette16b04a2015-04-15 07:32:19 -070024static void output_message(j_common_ptr info) {
25 print_message(info, "output_message");
26}
27
msarette16b04a2015-04-15 07:32:19 -070028bool JpegDecoderMgr::returnFalse(const char caller[]) {
29 print_message((j_common_ptr) &fDInfo, caller);
30 return false;
31}
32
33SkCodec::Result JpegDecoderMgr::returnFailure(const char caller[], SkCodec::Result result) {
34 print_message((j_common_ptr) &fDInfo, caller);
35 return result;
36}
37
msarettc30c4182016-04-20 11:53:35 -070038SkEncodedInfo::Color JpegDecoderMgr::getEncodedColor() {
msarette16b04a2015-04-15 07:32:19 -070039 switch (fDInfo.jpeg_color_space) {
msarette16b04a2015-04-15 07:32:19 -070040 case JCS_GRAYSCALE:
msarettc30c4182016-04-20 11:53:35 -070041 return SkEncodedInfo::kGray_Color;
42 case JCS_YCbCr:
43 return SkEncodedInfo::kYUV_Color;
44 case JCS_RGB:
45 return SkEncodedInfo::kRGB_Color;
46 case JCS_YCCK:
47 return SkEncodedInfo::kYCCK_Color;
48 case JCS_CMYK:
49 return SkEncodedInfo::kInvertedCMYK_Color;
msarette16b04a2015-04-15 07:32:19 -070050 default:
msarettc30c4182016-04-20 11:53:35 -070051 return SkEncodedInfo::kUnknown_Color;
msarette16b04a2015-04-15 07:32:19 -070052 }
53}
54
55JpegDecoderMgr::JpegDecoderMgr(SkStream* stream)
56 : fSrcMgr(stream)
57 , fInit(false)
58{
59 // Error manager must be set before any calls to libjeg in order to handle failures
msarettfbccb592015-09-01 06:43:41 -070060 fDInfo.err = jpeg_std_error(&fErrorMgr);
msarette16b04a2015-04-15 07:32:19 -070061 fErrorMgr.error_exit = skjpeg_err_exit;
62}
63
64void JpegDecoderMgr::init() {
65 jpeg_create_decompress(&fDInfo);
66 fInit = true;
67 fDInfo.src = &fSrcMgr;
msarette16b04a2015-04-15 07:32:19 -070068 fDInfo.err->output_message = &output_message;
69}
70
71JpegDecoderMgr::~JpegDecoderMgr() {
72 if (fInit) {
73 jpeg_destroy_decompress(&fDInfo);
74 }
75}
76
77jmp_buf& JpegDecoderMgr::getJmpBuf() {
78 return fErrorMgr.fJmpBuf;
79}
80
81jpeg_decompress_struct* JpegDecoderMgr::dinfo() {
82 return &fDInfo;
83}