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 | |
| 77 | static 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 | |
| 83 | static boolean skmem_fill_input_buffer(j_decompress_ptr cinfo) { |
| 84 | SkDebugf("xxxxxxxxxxxxxx skmem_fill_input_buffer called\n"); |
| 85 | return FALSE; |
| 86 | } |
| 87 | |
| 88 | static 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 | |
| 95 | static 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 | |
| 100 | static void skmem_term_source(j_decompress_ptr /*cinfo*/) {} |
| 101 | |
| 102 | |
| 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 105 | skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder, |
| 106 | bool ownStream) : fStream(stream) { |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 107 | fDecoder = decoder; |
| 108 | const void* baseAddr = stream->getMemoryBase(); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 109 | fMemoryBase = NULL; |
| 110 | fUnrefStream = ownStream; |
| 111 | fMemoryBaseSize = 0; |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 112 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 113 | 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.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 118 | // SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBaseSize); |
| 119 | } |
| 120 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 121 | skjpeg_source_mgr::~skjpeg_source_mgr() { |
| 122 | if (fMemoryBase) { |
| 123 | sk_free(fMemoryBase); |
| 124 | } |
| 125 | if (fUnrefStream) { |
| 126 | fStream->unref(); |
| 127 | } |
| 128 | } |
| 129 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 130 | /////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | static 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 | |
| 139 | static 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 | |
| 154 | static 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 | |
| 167 | skjpeg_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 | |
| 174 | void 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 | } |