blob: c9c8d3b11c8be484efb6a81855b683763f935d4c [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) {
43 return false;
44 }
45
46 src->next_input_byte = (const JOCTET*) src->fBuffer;
47 src->bytes_in_buffer = bytes;
48 return true;
49}
50
51/*
52 * Skip a certain number of bytes in the stream
53 */
Matt Sarett912e6b82017-06-07 09:48:21 -040054static void sk_skip_buffered_input_data(j_decompress_ptr dinfo, long numBytes) {
Matt Sarett15f4d022017-06-06 17:58:33 +000055 skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
msarette16b04a2015-04-15 07:32:19 -070056 size_t bytes = (size_t) numBytes;
57
58 if (bytes > src->bytes_in_buffer) {
59 size_t bytesToSkip = bytes - src->bytes_in_buffer;
60 if (bytesToSkip != src->fStream->skip(bytesToSkip)) {
61 SkCodecPrintf("Failure to skip.\n");
62 dinfo->err->error_exit((j_common_ptr) dinfo);
63 return;
64 }
65
66 src->next_input_byte = (const JOCTET*) src->fBuffer;
67 src->bytes_in_buffer = 0;
68 } else {
69 src->next_input_byte += numBytes;
70 src->bytes_in_buffer -= numBytes;
71 }
72}
73
74/*
75 * We do not need to do anything to terminate our stream
76 */
77static void sk_term_source(j_decompress_ptr dinfo)
msarett19ae3152015-10-02 13:44:12 -070078{
79 // The current implementation of SkJpegCodec does not call
80 // jpeg_finish_decompress(), so this function is never called.
81 // If we want to modify this function to do something, we also
82 // need to modify SkJpegCodec to call jpeg_finish_decompress().
83}
msarette16b04a2015-04-15 07:32:19 -070084
Matt Sarett912e6b82017-06-07 09:48:21 -040085// Functions for memory backed sources //
86
87/*
88 * Initialize the mem backed source manager
89 */
90static void sk_init_mem_source(j_decompress_ptr dinfo) {
91 /* no work necessary here, all things are done in constructor */
92}
93
94static void sk_skip_mem_input_data (j_decompress_ptr cinfo, long num_bytes) {
95 jpeg_source_mgr* src = cinfo->src;
96 size_t bytes = static_cast<size_t>(num_bytes);
97 if(bytes > src->bytes_in_buffer) {
98 src->next_input_byte = nullptr;
99 src->bytes_in_buffer = 0;
100 } else {
101 src->next_input_byte += bytes;
102 src->bytes_in_buffer -= bytes;
103 }
104}
105
106static boolean sk_fill_mem_input_buffer (j_decompress_ptr cinfo) {
107 /* The whole JPEG data is expected to reside in the supplied memory,
108 * buffer, so any request for more data beyond the given buffer size
109 * is treated as an error.
110 */
111 return false;
112}
113
msarette16b04a2015-04-15 07:32:19 -0700114/*
115 * Constructor for the source manager that we provide to libjpeg
116 * We provide skia implementations of all of the stream processing functions required by libjpeg
117 */
Matt Sarett15f4d022017-06-06 17:58:33 +0000118skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream)
msarette16b04a2015-04-15 07:32:19 -0700119 : fStream(stream)
120{
Matt Sarett912e6b82017-06-07 09:48:21 -0400121 if (stream->hasLength() && stream->getMemoryBase()) {
122 init_source = sk_init_mem_source;
123 fill_input_buffer = sk_fill_mem_input_buffer;
124 skip_input_data = sk_skip_mem_input_data;
125 resync_to_restart = jpeg_resync_to_restart;
126 term_source = sk_term_source;
127 bytes_in_buffer = static_cast<size_t>(stream->getLength());
128 next_input_byte = static_cast<const JOCTET*>(stream->getMemoryBase());
129 } else {
130 init_source = sk_init_buffered_source;
131 fill_input_buffer = sk_fill_buffered_input_buffer;
132 skip_input_data = sk_skip_buffered_input_data;
133 resync_to_restart = jpeg_resync_to_restart;
134 term_source = sk_term_source;
135 }
msarette16b04a2015-04-15 07:32:19 -0700136}