blob: b2284c81595af1f7a48bb332e60d2b5649f49233 [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
msarettc1d03122016-03-25 08:58:55 -07008#include "SkJpegUtility.h"
9
msarette16b04a2015-04-15 07:32:19 -070010#include "SkCodecPriv.h"
msarette16b04a2015-04-15 07:32:19 -070011
12/*
cjbao96cc3612017-05-22 23:40:36 +080013 * Initialize the buffered source manager
msarette16b04a2015-04-15 07:32:19 -070014 */
cjbao96cc3612017-05-22 23:40:36 +080015static void sk_init_buffered_source(j_decompress_ptr dinfo) {
16 skjpeg_buffered_source_mgr* src = (skjpeg_buffered_source_mgr*) dinfo->src;
msarette16b04a2015-04-15 07:32:19 -070017 src->next_input_byte = (const JOCTET*) src->fBuffer;
18 src->bytes_in_buffer = 0;
19}
20
21/*
22 * Fill the input buffer from the stream
23 */
cjbao96cc3612017-05-22 23:40:36 +080024static boolean sk_fill_buffered_input_buffer(j_decompress_ptr dinfo) {
25 skjpeg_buffered_source_mgr* src = (skjpeg_buffered_source_mgr*) dinfo->src;
26 size_t bytes = src->fStream->read(src->fBuffer, skjpeg_buffered_source_mgr::kBufferSize);
mtklein525e90a2015-06-18 09:58:57 -070027
msarette16b04a2015-04-15 07:32:19 -070028 // libjpeg is still happy with a less than full read, as long as the result is non-zero
29 if (bytes == 0) {
30 return false;
31 }
32
33 src->next_input_byte = (const JOCTET*) src->fBuffer;
34 src->bytes_in_buffer = bytes;
35 return true;
36}
37
38/*
39 * Skip a certain number of bytes in the stream
40 */
cjbao96cc3612017-05-22 23:40:36 +080041static void sk_skip_buffered_input_data(j_decompress_ptr dinfo, long numBytes) {
42 skjpeg_buffered_source_mgr* src = (skjpeg_buffered_source_mgr*) dinfo->src;
msarette16b04a2015-04-15 07:32:19 -070043 size_t bytes = (size_t) numBytes;
44
45 if (bytes > src->bytes_in_buffer) {
46 size_t bytesToSkip = bytes - src->bytes_in_buffer;
47 if (bytesToSkip != src->fStream->skip(bytesToSkip)) {
48 SkCodecPrintf("Failure to skip.\n");
49 dinfo->err->error_exit((j_common_ptr) dinfo);
50 return;
51 }
52
53 src->next_input_byte = (const JOCTET*) src->fBuffer;
54 src->bytes_in_buffer = 0;
55 } else {
56 src->next_input_byte += numBytes;
57 src->bytes_in_buffer -= numBytes;
58 }
59}
60
61/*
62 * We do not need to do anything to terminate our stream
63 */
64static void sk_term_source(j_decompress_ptr dinfo)
msarett19ae3152015-10-02 13:44:12 -070065{
66 // The current implementation of SkJpegCodec does not call
67 // jpeg_finish_decompress(), so this function is never called.
68 // If we want to modify this function to do something, we also
69 // need to modify SkJpegCodec to call jpeg_finish_decompress().
70}
msarette16b04a2015-04-15 07:32:19 -070071
72/*
73 * Constructor for the source manager that we provide to libjpeg
74 * We provide skia implementations of all of the stream processing functions required by libjpeg
75 */
cjbao96cc3612017-05-22 23:40:36 +080076skjpeg_buffered_source_mgr::skjpeg_buffered_source_mgr(SkStream* stream)
msarette16b04a2015-04-15 07:32:19 -070077 : fStream(stream)
78{
cjbao96cc3612017-05-22 23:40:36 +080079 init_source = sk_init_buffered_source;
80 fill_input_buffer = sk_fill_buffered_input_buffer;
81 skip_input_data = sk_skip_buffered_input_data;
msarettfbccb592015-09-01 06:43:41 -070082 resync_to_restart = jpeg_resync_to_restart;
msarette16b04a2015-04-15 07:32:19 -070083 term_source = sk_term_source;
84}
85
86/*
87 * Call longjmp to continue execution on an error
88 */
89void skjpeg_err_exit(j_common_ptr dinfo) {
90 // Simply return to Skia client code
91 // JpegDecoderMgr will take care of freeing memory
92 skjpeg_error_mgr* error = (skjpeg_error_mgr*) dinfo->err;
93 (*error->output_message) (dinfo);
94 longjmp(error->fJmpBuf, 1);
95}
cjbao96cc3612017-05-22 23:40:36 +080096
97/* memory backed source manager */
98/*
99 * Initialize the mem backed source manager
100 */
101static void sk_init_mem_source(j_decompress_ptr dinfo) {
102 /* no work necessary here, all things are done in constructor */
103}
104
105static void sk_skip_mem_input_data (j_decompress_ptr cinfo, long num_bytes) {
106 jpeg_source_mgr* src = cinfo->src;
107 size_t bytes = static_cast<size_t>(num_bytes);
108 if(bytes > src->bytes_in_buffer) {
109 src->next_input_byte = nullptr;
110 src->bytes_in_buffer = 0;
111 } else {
112 src->next_input_byte += bytes;
113 src->bytes_in_buffer -= bytes;
114 }
115}
116
117static boolean sk_fill_mem_input_buffer (j_decompress_ptr cinfo) {
118 /* The whole JPEG data is expected to reside in the supplied memory,
119 * buffer, so any request for more data beyond the given buffer size
120 * is treated as an error.
121 */
122 return false;
123}
124
125skjpeg_mem_source_mgr::skjpeg_mem_source_mgr(SkStream* stream)
126{
127 SkASSERT(stream->hasLength());
128 SkASSERT(stream->getMemoryBase());
129
130 init_source = sk_init_mem_source;
131 fill_input_buffer = sk_fill_mem_input_buffer;
132 skip_input_data = sk_skip_mem_input_data;
133 resync_to_restart = jpeg_resync_to_restart;
134 term_source = sk_term_source;
135 bytes_in_buffer = static_cast<size_t>(stream->getLength());
136 next_input_byte = static_cast<const JOCTET*>(stream->getMemoryBase());
137}
138