epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 The Android Open Source Project |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 10 | #include "SkJpegUtility.h" |
| 11 | |
| 12 | ///////////////////////////////////////////////////////////////////// |
| 13 | static 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.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 17 | src->fStream->rewind(); |
| 18 | } |
| 19 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 20 | static 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 | |
| 37 | static void sk_skip_input_data(j_decompress_ptr cinfo, long num_bytes) { |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 38 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 39 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 40 | if (num_bytes > (long)src->bytes_in_buffer) { |
reed@android.com | b8fd84b | 2010-01-21 18:04:44 +0000 | [diff] [blame] | 41 | 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.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 50 | } |
| 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 | |
| 59 | static 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 | |
| 74 | static void sk_term_source(j_decompress_ptr /*cinfo*/) {} |
| 75 | |
| 76 | |
caryclark@google.com | 2a2cc20 | 2012-06-06 12:04:36 +0000 | [diff] [blame] | 77 | #if 0 // UNUSED |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 78 | static 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 | |
| 84 | static boolean skmem_fill_input_buffer(j_decompress_ptr cinfo) { |
| 85 | SkDebugf("xxxxxxxxxxxxxx skmem_fill_input_buffer called\n"); |
| 86 | return FALSE; |
| 87 | } |
| 88 | |
| 89 | static 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 | |
| 96 | static 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 | |
| 101 | static void skmem_term_source(j_decompress_ptr /*cinfo*/) {} |
caryclark@google.com | 2a2cc20 | 2012-06-06 12:04:36 +0000 | [diff] [blame] | 102 | #endif |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 107 | skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder, |
| 108 | bool ownStream) : fStream(stream) { |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 109 | fDecoder = decoder; |
caryclark@google.com | 2a2cc20 | 2012-06-06 12:04:36 +0000 | [diff] [blame] | 110 | // const void* baseAddr = stream->getMemoryBase(); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 111 | fMemoryBase = NULL; |
| 112 | fUnrefStream = ownStream; |
| 113 | fMemoryBaseSize = 0; |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 114 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 115 | 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.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 120 | // SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBaseSize); |
| 121 | } |
| 122 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 123 | skjpeg_source_mgr::~skjpeg_source_mgr() { |
| 124 | if (fMemoryBase) { |
| 125 | sk_free(fMemoryBase); |
| 126 | } |
| 127 | if (fUnrefStream) { |
| 128 | fStream->unref(); |
| 129 | } |
| 130 | } |
| 131 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | |
| 134 | static 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 | |
| 141 | static 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 | |
| 156 | static 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 | |
| 169 | skjpeg_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 | |
| 176 | void 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 | } |