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" |
msarett | c1d0312 | 2016-03-25 08:58:55 -0700 | [diff] [blame] | 9 | |
| 10 | #include "SkJpegUtility.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * Print information, warning, and error messages |
| 14 | */ |
| 15 | static 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 | /* |
msarett | 6dfe9ac | 2015-11-10 11:22:12 -0800 | [diff] [blame] | 22 | * Reporting function for error and warning messages. |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 23 | */ |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 24 | static void output_message(j_common_ptr info) { |
| 25 | print_message(info, "output_message"); |
| 26 | } |
| 27 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 28 | bool JpegDecoderMgr::returnFalse(const char caller[]) { |
| 29 | print_message((j_common_ptr) &fDInfo, caller); |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | SkCodec::Result JpegDecoderMgr::returnFailure(const char caller[], SkCodec::Result result) { |
| 34 | print_message((j_common_ptr) &fDInfo, caller); |
| 35 | return result; |
| 36 | } |
| 37 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 38 | bool JpegDecoderMgr::getEncodedColor(SkEncodedInfo::Color* outColor) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 39 | switch (fDInfo.jpeg_color_space) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 40 | case JCS_GRAYSCALE: |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 41 | *outColor = SkEncodedInfo::kGray_Color; |
| 42 | return true; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 43 | case JCS_YCbCr: |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 44 | *outColor = SkEncodedInfo::kYUV_Color; |
| 45 | return true; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 46 | case JCS_RGB: |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 47 | *outColor = SkEncodedInfo::kRGB_Color; |
| 48 | return true; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 49 | case JCS_YCCK: |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 50 | *outColor = SkEncodedInfo::kYCCK_Color; |
| 51 | return true; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 52 | case JCS_CMYK: |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 53 | *outColor = SkEncodedInfo::kInvertedCMYK_Color; |
| 54 | return true; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 55 | default: |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 56 | return false; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | JpegDecoderMgr::JpegDecoderMgr(SkStream* stream) |
| 61 | : fSrcMgr(stream) |
| 62 | , fInit(false) |
| 63 | { |
| 64 | // Error manager must be set before any calls to libjeg in order to handle failures |
msarett | fbccb59 | 2015-09-01 06:43:41 -0700 | [diff] [blame] | 65 | fDInfo.err = jpeg_std_error(&fErrorMgr); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 66 | fErrorMgr.error_exit = skjpeg_err_exit; |
| 67 | } |
| 68 | |
| 69 | void JpegDecoderMgr::init() { |
| 70 | jpeg_create_decompress(&fDInfo); |
| 71 | fInit = true; |
| 72 | fDInfo.src = &fSrcMgr; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 73 | fDInfo.err->output_message = &output_message; |
| 74 | } |
| 75 | |
| 76 | JpegDecoderMgr::~JpegDecoderMgr() { |
| 77 | if (fInit) { |
| 78 | jpeg_destroy_decompress(&fDInfo); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | jmp_buf& JpegDecoderMgr::getJmpBuf() { |
| 83 | return fErrorMgr.fJmpBuf; |
| 84 | } |
| 85 | |
| 86 | jpeg_decompress_struct* JpegDecoderMgr::dinfo() { |
| 87 | return &fDInfo; |
| 88 | } |