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 "src/codec/SkJpegUtility.h"
9
10 #include "src/codec/SkCodecPriv.h"
11
12 /*
13 * Call longjmp to continue execution on an error
14 */
skjpeg_err_exit(j_common_ptr dinfo)15 void skjpeg_err_exit(j_common_ptr dinfo) {
16 // Simply return to Skia client code
17 // JpegDecoderMgr will take care of freeing memory
18 skjpeg_error_mgr* error = (skjpeg_error_mgr*) dinfo->err;
19 (*error->output_message) (dinfo);
20 if (error->fJmpBufStack.empty()) {
21 SK_ABORT("JPEG error with no jmp_buf set.");
22 }
23 #if defined(_JBLEN) && (defined(_JMP_BUF_DEFINED) || defined(_JBTYPE))
24 longjmp((_JBTYPE*)*error->fJmpBufStack.back(), 1);
25 #else
26 longjmp(*error->fJmpBufStack.back(), 1);
27 #endif
28 }
29
30 // Functions for buffered sources //
31
32 /*
33 * Initialize the buffered source manager
34 */
sk_init_buffered_source(j_decompress_ptr dinfo)35 static void sk_init_buffered_source(j_decompress_ptr dinfo) {
36 skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
37 src->next_input_byte = (const JOCTET*) src->fBuffer;
38 src->bytes_in_buffer = 0;
39 }
40
41 /*
42 * Fill the input buffer from the stream
43 */
sk_fill_buffered_input_buffer(j_decompress_ptr dinfo)44 static boolean sk_fill_buffered_input_buffer(j_decompress_ptr dinfo) {
45 skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
46 size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize);
47
48 // libjpeg is still happy with a less than full read, as long as the result is non-zero
49 if (bytes == 0) {
50 // Let libjpeg know that the buffer needs to be refilled
51 src->next_input_byte = nullptr;
52 src->bytes_in_buffer = 0;
53 return false;
54 }
55
56 src->next_input_byte = (const JOCTET*) src->fBuffer;
57 src->bytes_in_buffer = bytes;
58 return true;
59 }
60
61 /*
62 * Skip a certain number of bytes in the stream
63 */
sk_skip_buffered_input_data(j_decompress_ptr dinfo, long numBytes)64 static void sk_skip_buffered_input_data(j_decompress_ptr dinfo, long numBytes) {
65 skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
66 size_t bytes = (size_t) numBytes;
67
68 if (bytes > src->bytes_in_buffer) {
69 size_t bytesToSkip = bytes - src->bytes_in_buffer;
70 if (bytesToSkip != src->fStream->skip(bytesToSkip)) {
71 SkCodecPrintf("Failure to skip.\n");
72 dinfo->err->error_exit((j_common_ptr) dinfo);
73 return;
74 }
75
76 src->next_input_byte = (const JOCTET*) src->fBuffer;
77 src->bytes_in_buffer = 0;
78 } else {
79 src->next_input_byte += numBytes;
80 src->bytes_in_buffer -= numBytes;
81 }
82 }
83
84 /*
85 * We do not need to do anything to terminate our stream
86 */
sk_term_source(j_decompress_ptr dinfo)87 static void sk_term_source(j_decompress_ptr dinfo)
88 {
89 // The current implementation of SkJpegCodec does not call
90 // jpeg_finish_decompress(), so this function is never called.
91 // If we want to modify this function to do something, we also
92 // need to modify SkJpegCodec to call jpeg_finish_decompress().
93 }
94
95 // Functions for memory backed sources //
96
97 /*
98 * Initialize the mem backed source manager
99 */
sk_init_mem_source(j_decompress_ptr dinfo)100 static void sk_init_mem_source(j_decompress_ptr dinfo) {
101 /* no work necessary here, all things are done in constructor */
102 }
103
sk_skip_mem_input_data(j_decompress_ptr cinfo, long num_bytes)104 static void sk_skip_mem_input_data (j_decompress_ptr cinfo, long num_bytes) {
105 jpeg_source_mgr* src = cinfo->src;
106 size_t bytes = static_cast<size_t>(num_bytes);
107 if(bytes > src->bytes_in_buffer) {
108 src->next_input_byte = nullptr;
109 src->bytes_in_buffer = 0;
110 } else {
111 src->next_input_byte += bytes;
112 src->bytes_in_buffer -= bytes;
113 }
114 }
115
sk_fill_mem_input_buffer(j_decompress_ptr cinfo)116 static boolean sk_fill_mem_input_buffer (j_decompress_ptr cinfo) {
117 /* The whole JPEG data is expected to reside in the supplied memory,
118 * buffer, so any request for more data beyond the given buffer size
119 * is treated as an error.
120 */
121 return false;
122 }
123
124 /*
125 * Constructor for the source manager that we provide to libjpeg
126 * We provide skia implementations of all of the stream processing functions required by libjpeg
127 */
skjpeg_source_mgr(SkStream* stream)128 skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream)
129 : fStream(stream)
130 {
131 if (stream->hasLength() && stream->getMemoryBase()) {
132 init_source = sk_init_mem_source;
133 fill_input_buffer = sk_fill_mem_input_buffer;
134 skip_input_data = sk_skip_mem_input_data;
135 resync_to_restart = jpeg_resync_to_restart;
136 term_source = sk_term_source;
137 bytes_in_buffer = static_cast<size_t>(stream->getLength());
138 next_input_byte = static_cast<const JOCTET*>(stream->getMemoryBase());
139 } else {
140 init_source = sk_init_buffered_source;
141 fill_input_buffer = sk_fill_buffered_input_buffer;
142 skip_input_data = sk_skip_buffered_input_data;
143 resync_to_restart = jpeg_resync_to_restart;
144 term_source = sk_term_source;
145 }
146 }
147