reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "SkJpegUtility.h" |
| 18 | |
| 19 | ///////////////////////////////////////////////////////////////////// |
| 20 | static void sk_init_source(j_decompress_ptr cinfo) { |
| 21 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 22 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 23 | src->bytes_in_buffer = 0; |
| 24 | } |
| 25 | |
| 26 | static boolean sk_fill_input_buffer(j_decompress_ptr cinfo) { |
| 27 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 28 | if (src->fDecoder != NULL && src->fDecoder->shouldCancelDecode()) { |
| 29 | return FALSE; |
| 30 | } |
| 31 | size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize); |
| 32 | // note that JPEG is happy with less than the full read, |
| 33 | // as long as the result is non-zero |
| 34 | if (bytes == 0) { |
| 35 | return FALSE; |
| 36 | } |
| 37 | |
| 38 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 39 | src->bytes_in_buffer = bytes; |
| 40 | return TRUE; |
| 41 | } |
| 42 | |
| 43 | 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] | 44 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 45 | |
reed@android.com | b8fd84b | 2010-01-21 18:04:44 +0000 | [diff] [blame^] | 46 | if (num_bytes > src->bytes_in_buffer) { |
| 47 | long bytesToSkip = num_bytes - src->bytes_in_buffer; |
| 48 | while (bytesToSkip > 0) { |
| 49 | long bytes = (long)src->fStream->skip(bytesToSkip); |
| 50 | if (bytes <= 0 || bytes > bytesToSkip) { |
| 51 | // SkDebugf("xxxxxxxxxxxxxx failure to skip request %d returned %d\n", bytesToSkip, bytes); |
| 52 | cinfo->err->error_exit((j_common_ptr)cinfo); |
| 53 | return; |
| 54 | } |
| 55 | bytesToSkip -= bytes; |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 56 | } |
| 57 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 58 | src->bytes_in_buffer = 0; |
| 59 | } else { |
| 60 | src->next_input_byte += num_bytes; |
| 61 | src->bytes_in_buffer -= num_bytes; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static boolean sk_resync_to_restart(j_decompress_ptr cinfo, int desired) { |
| 66 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 67 | |
| 68 | // what is the desired param for??? |
| 69 | |
| 70 | if (!src->fStream->rewind()) { |
| 71 | SkDebugf("xxxxxxxxxxxxxx failure to rewind\n"); |
| 72 | cinfo->err->error_exit((j_common_ptr)cinfo); |
| 73 | return FALSE; |
| 74 | } |
| 75 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 76 | src->bytes_in_buffer = 0; |
| 77 | return TRUE; |
| 78 | } |
| 79 | |
| 80 | static void sk_term_source(j_decompress_ptr /*cinfo*/) {} |
| 81 | |
| 82 | |
| 83 | static void skmem_init_source(j_decompress_ptr cinfo) { |
| 84 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 85 | src->next_input_byte = (const JOCTET*)src->fMemoryBase; |
| 86 | src->bytes_in_buffer = src->fMemoryBaseSize; |
| 87 | } |
| 88 | |
| 89 | static boolean skmem_fill_input_buffer(j_decompress_ptr cinfo) { |
| 90 | SkDebugf("xxxxxxxxxxxxxx skmem_fill_input_buffer called\n"); |
| 91 | return FALSE; |
| 92 | } |
| 93 | |
| 94 | static void skmem_skip_input_data(j_decompress_ptr cinfo, long num_bytes) { |
| 95 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 96 | // SkDebugf("xxxxxxxxxxxxxx skmem_skip_input_data called %d\n", num_bytes); |
| 97 | src->next_input_byte = (const JOCTET*)((const char*)src->next_input_byte + num_bytes); |
| 98 | src->bytes_in_buffer -= num_bytes; |
| 99 | } |
| 100 | |
| 101 | static boolean skmem_resync_to_restart(j_decompress_ptr cinfo, int desired) { |
| 102 | SkDebugf("xxxxxxxxxxxxxx skmem_resync_to_restart called\n"); |
| 103 | return TRUE; |
| 104 | } |
| 105 | |
| 106 | static void skmem_term_source(j_decompress_ptr /*cinfo*/) {} |
| 107 | |
| 108 | |
| 109 | /////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder) : fStream(stream) { |
| 112 | fDecoder = decoder; |
| 113 | const void* baseAddr = stream->getMemoryBase(); |
| 114 | if (baseAddr && false) { |
| 115 | fMemoryBase = baseAddr; |
| 116 | fMemoryBaseSize = stream->getLength(); |
| 117 | |
| 118 | init_source = skmem_init_source; |
| 119 | fill_input_buffer = skmem_fill_input_buffer; |
| 120 | skip_input_data = skmem_skip_input_data; |
| 121 | resync_to_restart = skmem_resync_to_restart; |
| 122 | term_source = skmem_term_source; |
| 123 | } else { |
| 124 | fMemoryBase = NULL; |
| 125 | fMemoryBaseSize = 0; |
| 126 | |
| 127 | init_source = sk_init_source; |
| 128 | fill_input_buffer = sk_fill_input_buffer; |
| 129 | skip_input_data = sk_skip_input_data; |
| 130 | resync_to_restart = sk_resync_to_restart; |
| 131 | term_source = sk_term_source; |
| 132 | } |
| 133 | // SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBaseSize); |
| 134 | } |
| 135 | |
| 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | |
| 138 | static void sk_init_destination(j_compress_ptr cinfo) { |
| 139 | skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest; |
| 140 | |
| 141 | dest->next_output_byte = dest->fBuffer; |
| 142 | dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize; |
| 143 | } |
| 144 | |
| 145 | static boolean sk_empty_output_buffer(j_compress_ptr cinfo) { |
| 146 | skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest; |
| 147 | |
| 148 | // if (!dest->fStream->write(dest->fBuffer, skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer)) |
| 149 | if (!dest->fStream->write(dest->fBuffer, |
| 150 | skjpeg_destination_mgr::kBufferSize)) { |
| 151 | ERREXIT(cinfo, JERR_FILE_WRITE); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | dest->next_output_byte = dest->fBuffer; |
| 156 | dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize; |
| 157 | return TRUE; |
| 158 | } |
| 159 | |
| 160 | static void sk_term_destination (j_compress_ptr cinfo) { |
| 161 | skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest; |
| 162 | |
| 163 | size_t size = skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer; |
| 164 | if (size > 0) { |
| 165 | if (!dest->fStream->write(dest->fBuffer, size)) { |
| 166 | ERREXIT(cinfo, JERR_FILE_WRITE); |
| 167 | return; |
| 168 | } |
| 169 | } |
| 170 | dest->fStream->flush(); |
| 171 | } |
| 172 | |
| 173 | skjpeg_destination_mgr::skjpeg_destination_mgr(SkWStream* stream) |
| 174 | : fStream(stream) { |
| 175 | this->init_destination = sk_init_destination; |
| 176 | this->empty_output_buffer = sk_empty_output_buffer; |
| 177 | this->term_destination = sk_term_destination; |
| 178 | } |
| 179 | |
| 180 | void skjpeg_error_exit(j_common_ptr cinfo) { |
| 181 | skjpeg_error_mgr* error = (skjpeg_error_mgr*)cinfo->err; |
| 182 | |
| 183 | (*error->output_message) (cinfo); |
| 184 | |
| 185 | /* Let the memory manager delete any temp files before we die */ |
| 186 | jpeg_destroy(cinfo); |
| 187 | |
| 188 | longjmp(error->fJmpBuf, -1); |
| 189 | } |