blob: 7bfb6bf5e6ab7d645e4a1f4eae2b003e08754368 [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/*
Matt Sarett912e6b82017-06-07 09:48:21 -040013 * Call longjmp to continue execution on an error
msarette16b04a2015-04-15 07:32:19 -070014 */
Matt Sarett912e6b82017-06-07 09:48:21 -040015void 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 longjmp(error->fJmpBuf, 1);
21}
22
23// Functions for buffered sources //
24
25/*
26 * Initialize the buffered source manager
27 */
28static void sk_init_buffered_source(j_decompress_ptr dinfo) {
Matt Sarett15f4d022017-06-06 17:58:33 +000029 skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
msarette16b04a2015-04-15 07:32:19 -070030 src->next_input_byte = (const JOCTET*) src->fBuffer;
31 src->bytes_in_buffer = 0;
32}
33
34/*
35 * Fill the input buffer from the stream
36 */
Matt Sarett912e6b82017-06-07 09:48:21 -040037static boolean sk_fill_buffered_input_buffer(j_decompress_ptr dinfo) {
Matt Sarett15f4d022017-06-06 17:58:33 +000038 skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
39 size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize);
mtklein525e90a2015-06-18 09:58:57 -070040
msarette16b04a2015-04-15 07:32:19 -070041 // libjpeg is still happy with a less than full read, as long as the result is non-zero
42 if (bytes == 0) {
nagarajan.n67e34292017-07-19 19:02:25 +053043 // Let libjpeg know that the buffer needs to be refilled
44 src->next_input_byte = nullptr;
45 src->bytes_in_buffer = 0;
msarette16b04a2015-04-15 07:32:19 -070046 return false;
47 }
48
49 src->next_input_byte = (const JOCTET*) src->fBuffer;
50 src->bytes_in_buffer = bytes;
51 return true;
52}
53
54/*
55 * Skip a certain number of bytes in the stream
56 */
Matt Sarett912e6b82017-06-07 09:48:21 -040057static void sk_skip_buffered_input_data(j_decompress_ptr dinfo, long numBytes) {
Matt Sarett15f4d022017-06-06 17:58:33 +000058 skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
msarette16b04a2015-04-15 07:32:19 -070059 size_t bytes = (size_t) numBytes;
60
61 if (bytes > src->bytes_in_buffer) {
62 size_t bytesToSkip = bytes - src->bytes_in_buffer;
63 if (bytesToSkip != src->fStream->skip(bytesToSkip)) {
64 SkCodecPrintf("Failure to skip.\n");
65 dinfo->err->error_exit((j_common_ptr) dinfo);
66 return;
67 }
68
69 src->next_input_byte = (const JOCTET*) src->fBuffer;
70 src->bytes_in_buffer = 0;
71 } else {
72 src->next_input_byte += numBytes;
73 src->bytes_in_buffer -= numBytes;
74 }
75}
76
77/*
78 * We do not need to do anything to terminate our stream
79 */
80static void sk_term_source(j_decompress_ptr dinfo)
msarett19ae3152015-10-02 13:44:12 -070081{
82 // The current implementation of SkJpegCodec does not call
83 // jpeg_finish_decompress(), so this function is never called.
84 // If we want to modify this function to do something, we also
85 // need to modify SkJpegCodec to call jpeg_finish_decompress().
86}
msarette16b04a2015-04-15 07:32:19 -070087
Matt Sarett912e6b82017-06-07 09:48:21 -040088// Functions for memory backed sources //
89
90/*
91 * Initialize the mem backed source manager
92 */
93static void sk_init_mem_source(j_decompress_ptr dinfo) {
94 /* no work necessary here, all things are done in constructor */
95}
96
97static void sk_skip_mem_input_data (j_decompress_ptr cinfo, long num_bytes) {
98 jpeg_source_mgr* src = cinfo->src;
99 size_t bytes = static_cast<size_t>(num_bytes);
100 if(bytes > src->bytes_in_buffer) {
101 src->next_input_byte = nullptr;
102 src->bytes_in_buffer = 0;
103 } else {
104 src->next_input_byte += bytes;
105 src->bytes_in_buffer -= bytes;
106 }
107}
108
109static boolean sk_fill_mem_input_buffer (j_decompress_ptr cinfo) {
110 /* The whole JPEG data is expected to reside in the supplied memory,
111 * buffer, so any request for more data beyond the given buffer size
112 * is treated as an error.
113 */
114 return false;
115}
116
msarette16b04a2015-04-15 07:32:19 -0700117/*
118 * Constructor for the source manager that we provide to libjpeg
119 * We provide skia implementations of all of the stream processing functions required by libjpeg
120 */
Matt Sarett15f4d022017-06-06 17:58:33 +0000121skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream)
msarette16b04a2015-04-15 07:32:19 -0700122 : fStream(stream)
123{
Matt Sarett912e6b82017-06-07 09:48:21 -0400124 if (stream->hasLength() && stream->getMemoryBase()) {
125 init_source = sk_init_mem_source;
126 fill_input_buffer = sk_fill_mem_input_buffer;
127 skip_input_data = sk_skip_mem_input_data;
128 resync_to_restart = jpeg_resync_to_restart;
129 term_source = sk_term_source;
130 bytes_in_buffer = static_cast<size_t>(stream->getLength());
131 next_input_byte = static_cast<const JOCTET*>(stream->getMemoryBase());
132 } else {
133 init_source = sk_init_buffered_source;
134 fill_input_buffer = sk_fill_buffered_input_buffer;
135 skip_input_data = sk_skip_buffered_input_data;
136 resync_to_restart = jpeg_resync_to_restart;
137 term_source = sk_term_source;
138 }
msarette16b04a2015-04-15 07:32:19 -0700139}