blob: e28c51229f73669fe96cce5245bc030d17e0978d [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
77static void skmem_init_source(j_decompress_ptr cinfo) {
78 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
79 src->next_input_byte = (const JOCTET*)src->fMemoryBase;
80 src->bytes_in_buffer = src->fMemoryBaseSize;
81}
82
83static boolean skmem_fill_input_buffer(j_decompress_ptr cinfo) {
84 SkDebugf("xxxxxxxxxxxxxx skmem_fill_input_buffer called\n");
85 return FALSE;
86}
87
88static void skmem_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
89 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
90// SkDebugf("xxxxxxxxxxxxxx skmem_skip_input_data called %d\n", num_bytes);
91 src->next_input_byte = (const JOCTET*)((const char*)src->next_input_byte + num_bytes);
92 src->bytes_in_buffer -= num_bytes;
93}
94
95static boolean skmem_resync_to_restart(j_decompress_ptr cinfo, int desired) {
96 SkDebugf("xxxxxxxxxxxxxx skmem_resync_to_restart called\n");
97 return TRUE;
98}
99
100static void skmem_term_source(j_decompress_ptr /*cinfo*/) {}
101
102
103///////////////////////////////////////////////////////////////////////////////
104
djsollen@google.com57f49692011-02-23 20:46:31 +0000105skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder,
106 bool ownStream) : fStream(stream) {
reed@android.com6f598152010-01-21 15:34:19 +0000107 fDecoder = decoder;
108 const void* baseAddr = stream->getMemoryBase();
djsollen@google.com57f49692011-02-23 20:46:31 +0000109 fMemoryBase = NULL;
110 fUnrefStream = ownStream;
111 fMemoryBaseSize = 0;
reed@android.com6f598152010-01-21 15:34:19 +0000112
djsollen@google.com57f49692011-02-23 20:46:31 +0000113 init_source = sk_init_source;
114 fill_input_buffer = sk_fill_input_buffer;
115 skip_input_data = sk_skip_input_data;
116 resync_to_restart = sk_resync_to_restart;
117 term_source = sk_term_source;
reed@android.com6f598152010-01-21 15:34:19 +0000118// SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBaseSize);
119}
120
djsollen@google.com57f49692011-02-23 20:46:31 +0000121skjpeg_source_mgr::~skjpeg_source_mgr() {
122 if (fMemoryBase) {
123 sk_free(fMemoryBase);
124 }
125 if (fUnrefStream) {
126 fStream->unref();
127 }
128}
129
reed@android.com6f598152010-01-21 15:34:19 +0000130///////////////////////////////////////////////////////////////////////////////
131
132static void sk_init_destination(j_compress_ptr cinfo) {
133 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
134
135 dest->next_output_byte = dest->fBuffer;
136 dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize;
137}
138
139static boolean sk_empty_output_buffer(j_compress_ptr cinfo) {
140 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
141
142// if (!dest->fStream->write(dest->fBuffer, skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer))
143 if (!dest->fStream->write(dest->fBuffer,
144 skjpeg_destination_mgr::kBufferSize)) {
145 ERREXIT(cinfo, JERR_FILE_WRITE);
146 return false;
147 }
148
149 dest->next_output_byte = dest->fBuffer;
150 dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize;
151 return TRUE;
152}
153
154static void sk_term_destination (j_compress_ptr cinfo) {
155 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
156
157 size_t size = skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer;
158 if (size > 0) {
159 if (!dest->fStream->write(dest->fBuffer, size)) {
160 ERREXIT(cinfo, JERR_FILE_WRITE);
161 return;
162 }
163 }
164 dest->fStream->flush();
165}
166
167skjpeg_destination_mgr::skjpeg_destination_mgr(SkWStream* stream)
168 : fStream(stream) {
169 this->init_destination = sk_init_destination;
170 this->empty_output_buffer = sk_empty_output_buffer;
171 this->term_destination = sk_term_destination;
172}
173
174void skjpeg_error_exit(j_common_ptr cinfo) {
175 skjpeg_error_mgr* error = (skjpeg_error_mgr*)cinfo->err;
176
177 (*error->output_message) (cinfo);
178
179 /* Let the memory manager delete any temp files before we die */
180 jpeg_destroy(cinfo);
181
182 longjmp(error->fJmpBuf, -1);
183}