blob: 75a562a00454f45f8f0904d2f8a055fc1f65b006 [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 "SkCodecPriv.h"
mtklein525e90a2015-06-18 09:58:57 -07009#include "SkJpegUtility_codec.h"
msarette16b04a2015-04-15 07:32:19 -070010
11/*
12 * Initialize the source manager
13 */
14static void sk_init_source(j_decompress_ptr dinfo) {
15 skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
16 src->next_input_byte = (const JOCTET*) src->fBuffer;
17 src->bytes_in_buffer = 0;
18}
19
20/*
21 * Fill the input buffer from the stream
22 */
23static boolean sk_fill_input_buffer(j_decompress_ptr dinfo) {
24 skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
25 size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize);
mtklein525e90a2015-06-18 09:58:57 -070026
msarette16b04a2015-04-15 07:32:19 -070027 // libjpeg is still happy with a less than full read, as long as the result is non-zero
28 if (bytes == 0) {
29 return false;
30 }
31
32 src->next_input_byte = (const JOCTET*) src->fBuffer;
33 src->bytes_in_buffer = bytes;
34 return true;
35}
36
37/*
38 * Skip a certain number of bytes in the stream
39 */
40static void sk_skip_input_data(j_decompress_ptr dinfo, long numBytes) {
41 skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
42 size_t bytes = (size_t) numBytes;
43
44 if (bytes > src->bytes_in_buffer) {
45 size_t bytesToSkip = bytes - src->bytes_in_buffer;
46 if (bytesToSkip != src->fStream->skip(bytesToSkip)) {
47 SkCodecPrintf("Failure to skip.\n");
48 dinfo->err->error_exit((j_common_ptr) dinfo);
49 return;
50 }
51
52 src->next_input_byte = (const JOCTET*) src->fBuffer;
53 src->bytes_in_buffer = 0;
54 } else {
55 src->next_input_byte += numBytes;
56 src->bytes_in_buffer -= numBytes;
57 }
58}
59
60/*
61 * We do not need to do anything to terminate our stream
62 */
63static void sk_term_source(j_decompress_ptr dinfo)
64{}
65
66/*
67 * Constructor for the source manager that we provide to libjpeg
68 * We provide skia implementations of all of the stream processing functions required by libjpeg
69 */
70skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream)
71 : fStream(stream)
72{
73 init_source = sk_init_source;
74 fill_input_buffer = sk_fill_input_buffer;
75 skip_input_data = sk_skip_input_data;
msarettaa2a7de2015-07-01 13:11:08 -070076 resync_to_restart = jpeg_resync_to_restart;
msarette16b04a2015-04-15 07:32:19 -070077 term_source = sk_term_source;
78}
79
80/*
81 * Call longjmp to continue execution on an error
82 */
83void skjpeg_err_exit(j_common_ptr dinfo) {
84 // Simply return to Skia client code
85 // JpegDecoderMgr will take care of freeing memory
86 skjpeg_error_mgr* error = (skjpeg_error_mgr*) dinfo->err;
87 (*error->output_message) (dinfo);
88 longjmp(error->fJmpBuf, 1);
89}