blob: f1a32cae1007fa101f53353d8474cd9e709de079 [file] [log] [blame]
reed@android.com6f598152010-01-21 15:34:19 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 The Android Open Source Project
reed@android.com6f598152010-01-21 15:34:19 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com6f598152010-01-21 15:34:19 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
scroggo27631bd2015-06-15 09:10:03 -07009#include "SkJpegUtility.h"
reed@android.com6f598152010-01-21 15:34:19 +000010
11/////////////////////////////////////////////////////////////////////
12static void sk_init_source(j_decompress_ptr cinfo) {
13 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
14 src->next_input_byte = (const JOCTET*)src->fBuffer;
15 src->bytes_in_buffer = 0;
msarettc2d0bc52015-10-15 13:18:05 -070016#ifdef SK_JPEG_INDEX_SUPPORTED
djsollen@google.com11399402013-03-20 17:45:27 +000017 src->current_offset = 0;
18#endif
scroggo@google.com4d213ab2013-08-28 13:08:54 +000019 if (!src->fStream->rewind()) {
20 SkDebugf("xxxxxxxxxxxxxx failure to rewind\n");
21 cinfo->err->error_exit((j_common_ptr)cinfo);
22 }
djsollen@google.com57f49692011-02-23 20:46:31 +000023}
24
msarettc2d0bc52015-10-15 13:18:05 -070025#ifdef SK_JPEG_INDEX_SUPPORTED
djsollen@google.com11399402013-03-20 17:45:27 +000026static boolean sk_seek_input_data(j_decompress_ptr cinfo, long byte_offset) {
27 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
scroggo@google.comd4c35652013-08-01 15:03:42 +000028 size_t bo = (size_t) byte_offset;
djsollen@google.com11399402013-03-20 17:45:27 +000029
scroggo@google.comd4c35652013-08-01 15:03:42 +000030 if (bo > src->current_offset) {
31 (void)src->fStream->skip(bo - src->current_offset);
djsollen@google.com11399402013-03-20 17:45:27 +000032 } else {
scroggo@google.com4d213ab2013-08-28 13:08:54 +000033 if (!src->fStream->rewind()) {
34 SkDebugf("xxxxxxxxxxxxxx failure to rewind\n");
35 cinfo->err->error_exit((j_common_ptr)cinfo);
36 return false;
37 }
scroggo@google.comd4c35652013-08-01 15:03:42 +000038 (void)src->fStream->skip(bo);
djsollen@google.com11399402013-03-20 17:45:27 +000039 }
40
scroggo@google.comd4c35652013-08-01 15:03:42 +000041 src->current_offset = bo;
djsollen@google.com11399402013-03-20 17:45:27 +000042 src->next_input_byte = (const JOCTET*)src->fBuffer;
43 src->bytes_in_buffer = 0;
44 return true;
45}
46#endif
47
reed@android.com6f598152010-01-21 15:34:19 +000048static boolean sk_fill_input_buffer(j_decompress_ptr cinfo) {
49 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
halcanary96fcdcc2015-08-27 07:41:13 -070050 if (src->fDecoder != nullptr && src->fDecoder->shouldCancelDecode()) {
reed@android.com6f598152010-01-21 15:34:19 +000051 return FALSE;
52 }
53 size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize);
54 // note that JPEG is happy with less than the full read,
55 // as long as the result is non-zero
56 if (bytes == 0) {
57 return FALSE;
58 }
59
msarettc2d0bc52015-10-15 13:18:05 -070060#ifdef SK_JPEG_INDEX_SUPPORTED
djsollen@google.com11399402013-03-20 17:45:27 +000061 src->current_offset += bytes;
62#endif
reed@android.com6f598152010-01-21 15:34:19 +000063 src->next_input_byte = (const JOCTET*)src->fBuffer;
64 src->bytes_in_buffer = bytes;
65 return TRUE;
66}
67
68static void sk_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
reed@android.com6f598152010-01-21 15:34:19 +000069 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src;
70
reed@android.com3f1f06a2010-03-03 21:04:12 +000071 if (num_bytes > (long)src->bytes_in_buffer) {
robertphillips@google.com8b169312013-10-15 17:47:36 +000072 size_t bytesToSkip = num_bytes - src->bytes_in_buffer;
reed@android.comb8fd84b2010-01-21 18:04:44 +000073 while (bytesToSkip > 0) {
robertphillips@google.com8b169312013-10-15 17:47:36 +000074 size_t bytes = src->fStream->skip(bytesToSkip);
reed@android.comb8fd84b2010-01-21 18:04:44 +000075 if (bytes <= 0 || bytes > bytesToSkip) {
76// SkDebugf("xxxxxxxxxxxxxx failure to skip request %d returned %d\n", bytesToSkip, bytes);
77 cinfo->err->error_exit((j_common_ptr)cinfo);
78 return;
79 }
msarettc2d0bc52015-10-15 13:18:05 -070080#ifdef SK_JPEG_INDEX_SUPPORTED
djsollen@google.com11399402013-03-20 17:45:27 +000081 src->current_offset += bytes;
82#endif
reed@android.comb8fd84b2010-01-21 18:04:44 +000083 bytesToSkip -= bytes;
reed@android.com6f598152010-01-21 15:34:19 +000084 }
85 src->next_input_byte = (const JOCTET*)src->fBuffer;
86 src->bytes_in_buffer = 0;
87 } else {
88 src->next_input_byte += num_bytes;
89 src->bytes_in_buffer -= num_bytes;
90 }
91}
92
reed@android.com6f598152010-01-21 15:34:19 +000093static void sk_term_source(j_decompress_ptr /*cinfo*/) {}
94
95
reed@android.com6f598152010-01-21 15:34:19 +000096///////////////////////////////////////////////////////////////////////////////
97
scroggo@google.comd4c35652013-08-01 15:03:42 +000098skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder)
scroggoa1193e42015-01-21 12:09:53 -080099 : fStream(stream)
scroggo@google.comd4c35652013-08-01 15:03:42 +0000100 , fDecoder(decoder) {
reed@android.com6f598152010-01-21 15:34:19 +0000101
djsollen@google.com57f49692011-02-23 20:46:31 +0000102 init_source = sk_init_source;
103 fill_input_buffer = sk_fill_input_buffer;
104 skip_input_data = sk_skip_input_data;
scroggo@google.coma9348992013-09-25 21:17:41 +0000105 resync_to_restart = jpeg_resync_to_restart;
djsollen@google.com57f49692011-02-23 20:46:31 +0000106 term_source = sk_term_source;
msarettc2d0bc52015-10-15 13:18:05 -0700107#ifdef SK_JPEG_INDEX_SUPPORTED
djsollen@google.com11399402013-03-20 17:45:27 +0000108 seek_input_data = sk_seek_input_data;
109#endif
reed@android.com6f598152010-01-21 15:34:19 +0000110// SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBaseSize);
111}
112
reed@android.com6f598152010-01-21 15:34:19 +0000113///////////////////////////////////////////////////////////////////////////////
114
115static void sk_init_destination(j_compress_ptr cinfo) {
116 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
117
118 dest->next_output_byte = dest->fBuffer;
119 dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize;
120}
121
122static boolean sk_empty_output_buffer(j_compress_ptr cinfo) {
123 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
124
125// if (!dest->fStream->write(dest->fBuffer, skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer))
126 if (!dest->fStream->write(dest->fBuffer,
127 skjpeg_destination_mgr::kBufferSize)) {
128 ERREXIT(cinfo, JERR_FILE_WRITE);
129 return false;
130 }
131
132 dest->next_output_byte = dest->fBuffer;
133 dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize;
134 return TRUE;
135}
136
137static void sk_term_destination (j_compress_ptr cinfo) {
138 skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
139
140 size_t size = skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer;
141 if (size > 0) {
142 if (!dest->fStream->write(dest->fBuffer, size)) {
143 ERREXIT(cinfo, JERR_FILE_WRITE);
144 return;
145 }
146 }
147 dest->fStream->flush();
148}
149
150skjpeg_destination_mgr::skjpeg_destination_mgr(SkWStream* stream)
151 : fStream(stream) {
152 this->init_destination = sk_init_destination;
153 this->empty_output_buffer = sk_empty_output_buffer;
154 this->term_destination = sk_term_destination;
155}
156
157void skjpeg_error_exit(j_common_ptr cinfo) {
158 skjpeg_error_mgr* error = (skjpeg_error_mgr*)cinfo->err;
159
160 (*error->output_message) (cinfo);
161
162 /* Let the memory manager delete any temp files before we die */
163 jpeg_destroy(cinfo);
164
165 longjmp(error->fJmpBuf, -1);
166}