blob: 89a0472380b444118e422e28ee2e9aaea8c5d891 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com6f598152010-01-21 15:34:19 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 The Android Open Source Project
reed@android.com6f598152010-01-21 15:34:19 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com6f598152010-01-21 15:34:19 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com6f598152010-01-21 15:34:19 +000010#include "SkJpegUtility.h"
11
djsollen@google.com11399402013-03-20 17:45:27 +000012// Uncomment to enable the code path used by the Android framework with their
13// custom image decoders.
14//#if defined(SK_BUILD_FOR_ANDROID) && defined(SK_DEBUG)
15// #define SK_BUILD_FOR_ANDROID_FRAMEWORK
16//#endif
17
reed@android.com6f598152010-01-21 15:34:19 +000018/////////////////////////////////////////////////////////////////////
19static void sk_init_source(j_decompress_ptr cinfo) {
20 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
21 src->next_input_byte = (const JOCTET*)src->fBuffer;
22 src->bytes_in_buffer = 0;
djsollen@google.com11399402013-03-20 17:45:27 +000023#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
24 src->current_offset = 0;
25#endif
djsollen@google.com57f49692011-02-23 20:46:31 +000026 src->fStream->rewind();
27}
28
djsollen@google.com11399402013-03-20 17:45:27 +000029#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
30static boolean sk_seek_input_data(j_decompress_ptr cinfo, long byte_offset) {
31 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
32
33 if (byte_offset > src->current_offset) {
34 (void)src->fStream->skip(byte_offset - src->current_offset);
35 } else {
36 src->fStream->rewind();
37 (void)src->fStream->skip(byte_offset);
38 }
39
40 src->current_offset = byte_offset;
41 src->next_input_byte = (const JOCTET*)src->fBuffer;
42 src->bytes_in_buffer = 0;
43 return true;
44}
45#endif
46
reed@android.com6f598152010-01-21 15:34:19 +000047static boolean sk_fill_input_buffer(j_decompress_ptr cinfo) {
48 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
49 if (src->fDecoder != NULL && src->fDecoder->shouldCancelDecode()) {
50 return FALSE;
51 }
52 size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize);
53 // note that JPEG is happy with less than the full read,
54 // as long as the result is non-zero
55 if (bytes == 0) {
56 return FALSE;
57 }
58
djsollen@google.com11399402013-03-20 17:45:27 +000059#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
60 src->current_offset += bytes;
61#endif
reed@android.com6f598152010-01-21 15:34:19 +000062 src->next_input_byte = (const JOCTET*)src->fBuffer;
63 src->bytes_in_buffer = bytes;
64 return TRUE;
65}
66
67static void sk_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
reed@android.com6f598152010-01-21 15:34:19 +000068 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
69
reed@android.com3f1f06a2010-03-03 21:04:12 +000070 if (num_bytes > (long)src->bytes_in_buffer) {
reed@android.comb8fd84b2010-01-21 18:04:44 +000071 long bytesToSkip = num_bytes - src->bytes_in_buffer;
72 while (bytesToSkip > 0) {
73 long bytes = (long)src->fStream->skip(bytesToSkip);
74 if (bytes <= 0 || bytes > bytesToSkip) {
75// SkDebugf("xxxxxxxxxxxxxx failure to skip request %d returned %d\n", bytesToSkip, bytes);
76 cinfo->err->error_exit((j_common_ptr)cinfo);
77 return;
78 }
djsollen@google.com11399402013-03-20 17:45:27 +000079#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
80 src->current_offset += bytes;
81#endif
reed@android.comb8fd84b2010-01-21 18:04:44 +000082 bytesToSkip -= bytes;
reed@android.com6f598152010-01-21 15:34:19 +000083 }
84 src->next_input_byte = (const JOCTET*)src->fBuffer;
85 src->bytes_in_buffer = 0;
86 } else {
87 src->next_input_byte += num_bytes;
88 src->bytes_in_buffer -= num_bytes;
89 }
90}
91
92static boolean sk_resync_to_restart(j_decompress_ptr cinfo, int desired) {
93 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
94
95 // what is the desired param for???
96
97 if (!src->fStream->rewind()) {
98 SkDebugf("xxxxxxxxxxxxxx failure to rewind\n");
99 cinfo->err->error_exit((j_common_ptr)cinfo);
100 return FALSE;
101 }
102 src->next_input_byte = (const JOCTET*)src->fBuffer;
103 src->bytes_in_buffer = 0;
104 return TRUE;
105}
106
107static void sk_term_source(j_decompress_ptr /*cinfo*/) {}
108
109
reed@android.com6f598152010-01-21 15:34:19 +0000110///////////////////////////////////////////////////////////////////////////////
111
djsollen@google.com57f49692011-02-23 20:46:31 +0000112skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder,
113 bool ownStream) : fStream(stream) {
reed@android.com6f598152010-01-21 15:34:19 +0000114 fDecoder = decoder;
djsollen@google.com57f49692011-02-23 20:46:31 +0000115 fMemoryBase = NULL;
116 fUnrefStream = ownStream;
117 fMemoryBaseSize = 0;
reed@android.com6f598152010-01-21 15:34:19 +0000118
djsollen@google.com57f49692011-02-23 20:46:31 +0000119 init_source = sk_init_source;
120 fill_input_buffer = sk_fill_input_buffer;
121 skip_input_data = sk_skip_input_data;
122 resync_to_restart = sk_resync_to_restart;
123 term_source = sk_term_source;
djsollen@google.com11399402013-03-20 17:45:27 +0000124#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
125 seek_input_data = sk_seek_input_data;
126#endif
reed@android.com6f598152010-01-21 15:34:19 +0000127// SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBaseSize);
128}
129
djsollen@google.com57f49692011-02-23 20:46:31 +0000130skjpeg_source_mgr::~skjpeg_source_mgr() {
131 if (fMemoryBase) {
132 sk_free(fMemoryBase);
133 }
134 if (fUnrefStream) {
135 fStream->unref();
136 }
137}
138
reed@android.com6f598152010-01-21 15:34:19 +0000139///////////////////////////////////////////////////////////////////////////////
140
141static void sk_init_destination(j_compress_ptr cinfo) {
142 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
143
144 dest->next_output_byte = dest->fBuffer;
145 dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize;
146}
147
148static boolean sk_empty_output_buffer(j_compress_ptr cinfo) {
149 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
150
151// if (!dest->fStream->write(dest->fBuffer, skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer))
152 if (!dest->fStream->write(dest->fBuffer,
153 skjpeg_destination_mgr::kBufferSize)) {
154 ERREXIT(cinfo, JERR_FILE_WRITE);
155 return false;
156 }
157
158 dest->next_output_byte = dest->fBuffer;
159 dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize;
160 return TRUE;
161}
162
163static void sk_term_destination (j_compress_ptr cinfo) {
164 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
165
166 size_t size = skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer;
167 if (size > 0) {
168 if (!dest->fStream->write(dest->fBuffer, size)) {
169 ERREXIT(cinfo, JERR_FILE_WRITE);
170 return;
171 }
172 }
173 dest->fStream->flush();
174}
175
176skjpeg_destination_mgr::skjpeg_destination_mgr(SkWStream* stream)
177 : fStream(stream) {
178 this->init_destination = sk_init_destination;
179 this->empty_output_buffer = sk_empty_output_buffer;
180 this->term_destination = sk_term_destination;
181}
182
183void skjpeg_error_exit(j_common_ptr cinfo) {
184 skjpeg_error_mgr* error = (skjpeg_error_mgr*)cinfo->err;
185
186 (*error->output_message) (cinfo);
187
188 /* Let the memory manager delete any temp files before we die */
189 jpeg_destroy(cinfo);
190
191 longjmp(error->fJmpBuf, -1);
192}