blob: 19db0186d27fd392f9c58605b9d6d4f127de88f1 [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
12/////////////////////////////////////////////////////////////////////
13static void sk_init_source(j_decompress_ptr cinfo) {
14 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
15 src->next_input_byte = (const JOCTET*)src->fBuffer;
16 src->bytes_in_buffer = 0;
djsollen@google.com57f49692011-02-23 20:46:31 +000017 src->fStream->rewind();
18}
19
reed@android.com6f598152010-01-21 15:34:19 +000020static boolean sk_fill_input_buffer(j_decompress_ptr cinfo) {
21 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
22 if (src->fDecoder != NULL && src->fDecoder->shouldCancelDecode()) {
23 return FALSE;
24 }
25 size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize);
26 // note that JPEG is happy with less than the full read,
27 // as long as the result is non-zero
28 if (bytes == 0) {
29 return FALSE;
30 }
31
32 src->next_input_byte = (const JOCTET*)src->fBuffer;
33 src->bytes_in_buffer = bytes;
34 return TRUE;
35}
36
37static void sk_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
reed@android.com6f598152010-01-21 15:34:19 +000038 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
39
reed@android.com3f1f06a2010-03-03 21:04:12 +000040 if (num_bytes > (long)src->bytes_in_buffer) {
reed@android.comb8fd84b2010-01-21 18:04:44 +000041 long bytesToSkip = num_bytes - src->bytes_in_buffer;
42 while (bytesToSkip > 0) {
43 long bytes = (long)src->fStream->skip(bytesToSkip);
44 if (bytes <= 0 || bytes > bytesToSkip) {
45// SkDebugf("xxxxxxxxxxxxxx failure to skip request %d returned %d\n", bytesToSkip, bytes);
46 cinfo->err->error_exit((j_common_ptr)cinfo);
47 return;
48 }
49 bytesToSkip -= bytes;
reed@android.com6f598152010-01-21 15:34:19 +000050 }
51 src->next_input_byte = (const JOCTET*)src->fBuffer;
52 src->bytes_in_buffer = 0;
53 } else {
54 src->next_input_byte += num_bytes;
55 src->bytes_in_buffer -= num_bytes;
56 }
57}
58
59static boolean sk_resync_to_restart(j_decompress_ptr cinfo, int desired) {
60 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
61
62 // what is the desired param for???
63
64 if (!src->fStream->rewind()) {
65 SkDebugf("xxxxxxxxxxxxxx failure to rewind\n");
66 cinfo->err->error_exit((j_common_ptr)cinfo);
67 return FALSE;
68 }
69 src->next_input_byte = (const JOCTET*)src->fBuffer;
70 src->bytes_in_buffer = 0;
71 return TRUE;
72}
73
74static void sk_term_source(j_decompress_ptr /*cinfo*/) {}
75
76
caryclark@google.com2a2cc202012-06-06 12:04:36 +000077#if 0 // UNUSED
reed@android.com6f598152010-01-21 15:34:19 +000078static void skmem_init_source(j_decompress_ptr cinfo) {
79 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
80 src->next_input_byte = (const JOCTET*)src->fMemoryBase;
81 src->bytes_in_buffer = src->fMemoryBaseSize;
82}
83
84static boolean skmem_fill_input_buffer(j_decompress_ptr cinfo) {
85 SkDebugf("xxxxxxxxxxxxxx skmem_fill_input_buffer called\n");
86 return FALSE;
87}
88
89static void skmem_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
90 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
91// SkDebugf("xxxxxxxxxxxxxx skmem_skip_input_data called %d\n", num_bytes);
92 src->next_input_byte = (const JOCTET*)((const char*)src->next_input_byte + num_bytes);
93 src->bytes_in_buffer -= num_bytes;
94}
95
96static boolean skmem_resync_to_restart(j_decompress_ptr cinfo, int desired) {
97 SkDebugf("xxxxxxxxxxxxxx skmem_resync_to_restart called\n");
98 return TRUE;
99}
100
101static void skmem_term_source(j_decompress_ptr /*cinfo*/) {}
caryclark@google.com2a2cc202012-06-06 12:04:36 +0000102#endif
reed@android.com6f598152010-01-21 15:34:19 +0000103
104
105///////////////////////////////////////////////////////////////////////////////
106
djsollen@google.com57f49692011-02-23 20:46:31 +0000107skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder,
108 bool ownStream) : fStream(stream) {
reed@android.com6f598152010-01-21 15:34:19 +0000109 fDecoder = decoder;
caryclark@google.com2a2cc202012-06-06 12:04:36 +0000110 // const void* baseAddr = stream->getMemoryBase();
djsollen@google.com57f49692011-02-23 20:46:31 +0000111 fMemoryBase = NULL;
112 fUnrefStream = ownStream;
113 fMemoryBaseSize = 0;
reed@android.com6f598152010-01-21 15:34:19 +0000114
djsollen@google.com57f49692011-02-23 20:46:31 +0000115 init_source = sk_init_source;
116 fill_input_buffer = sk_fill_input_buffer;
117 skip_input_data = sk_skip_input_data;
118 resync_to_restart = sk_resync_to_restart;
119 term_source = sk_term_source;
reed@android.com6f598152010-01-21 15:34:19 +0000120// SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBaseSize);
121}
122
djsollen@google.com57f49692011-02-23 20:46:31 +0000123skjpeg_source_mgr::~skjpeg_source_mgr() {
124 if (fMemoryBase) {
125 sk_free(fMemoryBase);
126 }
127 if (fUnrefStream) {
128 fStream->unref();
129 }
130}
131
reed@android.com6f598152010-01-21 15:34:19 +0000132///////////////////////////////////////////////////////////////////////////////
133
134static void sk_init_destination(j_compress_ptr cinfo) {
135 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
136
137 dest->next_output_byte = dest->fBuffer;
138 dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize;
139}
140
141static boolean sk_empty_output_buffer(j_compress_ptr cinfo) {
142 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
143
144// if (!dest->fStream->write(dest->fBuffer, skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer))
145 if (!dest->fStream->write(dest->fBuffer,
146 skjpeg_destination_mgr::kBufferSize)) {
147 ERREXIT(cinfo, JERR_FILE_WRITE);
148 return false;
149 }
150
151 dest->next_output_byte = dest->fBuffer;
152 dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize;
153 return TRUE;
154}
155
156static void sk_term_destination (j_compress_ptr cinfo) {
157 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
158
159 size_t size = skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer;
160 if (size > 0) {
161 if (!dest->fStream->write(dest->fBuffer, size)) {
162 ERREXIT(cinfo, JERR_FILE_WRITE);
163 return;
164 }
165 }
166 dest->fStream->flush();
167}
168
169skjpeg_destination_mgr::skjpeg_destination_mgr(SkWStream* stream)
170 : fStream(stream) {
171 this->init_destination = sk_init_destination;
172 this->empty_output_buffer = sk_empty_output_buffer;
173 this->term_destination = sk_term_destination;
174}
175
176void skjpeg_error_exit(j_common_ptr cinfo) {
177 skjpeg_error_mgr* error = (skjpeg_error_mgr*)cinfo->err;
178
179 (*error->output_message) (cinfo);
180
181 /* Let the memory manager delete any temp files before we die */
182 jpeg_destroy(cinfo);
183
184 longjmp(error->fJmpBuf, -1);
185}