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 | |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame^] | 12 | // Uncomment to enable the code path used by the Android framework with their |
| 13 | // custom image decoders. |
| 14 | //#if defined(SK_BUILD_FOR_ANDROID) && defined(SK_DEBUG) |
| 15 | // #define SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 16 | //#endif |
| 17 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 18 | ///////////////////////////////////////////////////////////////////// |
| 19 | static void sk_init_source(j_decompress_ptr cinfo) { |
| 20 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 21 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 22 | src->bytes_in_buffer = 0; |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame^] | 23 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 24 | src->current_offset = 0; |
| 25 | #endif |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 26 | src->fStream->rewind(); |
| 27 | } |
| 28 | |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame^] | 29 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 30 | static boolean sk_seek_input_data(j_decompress_ptr cinfo, long byte_offset) { |
| 31 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 32 | |
| 33 | if (byte_offset > src->current_offset) { |
| 34 | (void)src->fStream->skip(byte_offset - src->current_offset); |
| 35 | } else { |
| 36 | src->fStream->rewind(); |
| 37 | (void)src->fStream->skip(byte_offset); |
| 38 | } |
| 39 | |
| 40 | src->current_offset = byte_offset; |
| 41 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 42 | src->bytes_in_buffer = 0; |
| 43 | return true; |
| 44 | } |
| 45 | #endif |
| 46 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 47 | static boolean sk_fill_input_buffer(j_decompress_ptr cinfo) { |
| 48 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 49 | if (src->fDecoder != NULL && src->fDecoder->shouldCancelDecode()) { |
| 50 | return FALSE; |
| 51 | } |
| 52 | size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize); |
| 53 | // note that JPEG is happy with less than the full read, |
| 54 | // as long as the result is non-zero |
| 55 | if (bytes == 0) { |
| 56 | return FALSE; |
| 57 | } |
| 58 | |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame^] | 59 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 60 | src->current_offset += bytes; |
| 61 | #endif |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 62 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 63 | src->bytes_in_buffer = bytes; |
| 64 | return TRUE; |
| 65 | } |
| 66 | |
| 67 | 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] | 68 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 69 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 70 | if (num_bytes > (long)src->bytes_in_buffer) { |
reed@android.com | b8fd84b | 2010-01-21 18:04:44 +0000 | [diff] [blame] | 71 | long bytesToSkip = num_bytes - src->bytes_in_buffer; |
| 72 | while (bytesToSkip > 0) { |
| 73 | long bytes = (long)src->fStream->skip(bytesToSkip); |
| 74 | if (bytes <= 0 || bytes > bytesToSkip) { |
| 75 | // SkDebugf("xxxxxxxxxxxxxx failure to skip request %d returned %d\n", bytesToSkip, bytes); |
| 76 | cinfo->err->error_exit((j_common_ptr)cinfo); |
| 77 | return; |
| 78 | } |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame^] | 79 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 80 | src->current_offset += bytes; |
| 81 | #endif |
reed@android.com | b8fd84b | 2010-01-21 18:04:44 +0000 | [diff] [blame] | 82 | bytesToSkip -= bytes; |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 83 | } |
| 84 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 85 | src->bytes_in_buffer = 0; |
| 86 | } else { |
| 87 | src->next_input_byte += num_bytes; |
| 88 | src->bytes_in_buffer -= num_bytes; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static boolean sk_resync_to_restart(j_decompress_ptr cinfo, int desired) { |
| 93 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 94 | |
| 95 | // what is the desired param for??? |
| 96 | |
| 97 | if (!src->fStream->rewind()) { |
| 98 | SkDebugf("xxxxxxxxxxxxxx failure to rewind\n"); |
| 99 | cinfo->err->error_exit((j_common_ptr)cinfo); |
| 100 | return FALSE; |
| 101 | } |
| 102 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 103 | src->bytes_in_buffer = 0; |
| 104 | return TRUE; |
| 105 | } |
| 106 | |
| 107 | static void sk_term_source(j_decompress_ptr /*cinfo*/) {} |
| 108 | |
| 109 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 112 | skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder, |
| 113 | bool ownStream) : fStream(stream) { |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 114 | fDecoder = decoder; |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 115 | fMemoryBase = NULL; |
| 116 | fUnrefStream = ownStream; |
| 117 | fMemoryBaseSize = 0; |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 118 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 119 | init_source = sk_init_source; |
| 120 | fill_input_buffer = sk_fill_input_buffer; |
| 121 | skip_input_data = sk_skip_input_data; |
| 122 | resync_to_restart = sk_resync_to_restart; |
| 123 | term_source = sk_term_source; |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame^] | 124 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 125 | seek_input_data = sk_seek_input_data; |
| 126 | #endif |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 127 | // SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBaseSize); |
| 128 | } |
| 129 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 130 | skjpeg_source_mgr::~skjpeg_source_mgr() { |
| 131 | if (fMemoryBase) { |
| 132 | sk_free(fMemoryBase); |
| 133 | } |
| 134 | if (fUnrefStream) { |
| 135 | fStream->unref(); |
| 136 | } |
| 137 | } |
| 138 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | static void sk_init_destination(j_compress_ptr cinfo) { |
| 142 | skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest; |
| 143 | |
| 144 | dest->next_output_byte = dest->fBuffer; |
| 145 | dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize; |
| 146 | } |
| 147 | |
| 148 | static boolean sk_empty_output_buffer(j_compress_ptr cinfo) { |
| 149 | skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest; |
| 150 | |
| 151 | // if (!dest->fStream->write(dest->fBuffer, skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer)) |
| 152 | if (!dest->fStream->write(dest->fBuffer, |
| 153 | skjpeg_destination_mgr::kBufferSize)) { |
| 154 | ERREXIT(cinfo, JERR_FILE_WRITE); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | dest->next_output_byte = dest->fBuffer; |
| 159 | dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize; |
| 160 | return TRUE; |
| 161 | } |
| 162 | |
| 163 | static void sk_term_destination (j_compress_ptr cinfo) { |
| 164 | skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest; |
| 165 | |
| 166 | size_t size = skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer; |
| 167 | if (size > 0) { |
| 168 | if (!dest->fStream->write(dest->fBuffer, size)) { |
| 169 | ERREXIT(cinfo, JERR_FILE_WRITE); |
| 170 | return; |
| 171 | } |
| 172 | } |
| 173 | dest->fStream->flush(); |
| 174 | } |
| 175 | |
| 176 | skjpeg_destination_mgr::skjpeg_destination_mgr(SkWStream* stream) |
| 177 | : fStream(stream) { |
| 178 | this->init_destination = sk_init_destination; |
| 179 | this->empty_output_buffer = sk_empty_output_buffer; |
| 180 | this->term_destination = sk_term_destination; |
| 181 | } |
| 182 | |
| 183 | void skjpeg_error_exit(j_common_ptr cinfo) { |
| 184 | skjpeg_error_mgr* error = (skjpeg_error_mgr*)cinfo->err; |
| 185 | |
| 186 | (*error->output_message) (cinfo); |
| 187 | |
| 188 | /* Let the memory manager delete any temp files before we die */ |
| 189 | jpeg_destroy(cinfo); |
| 190 | |
| 191 | longjmp(error->fJmpBuf, -1); |
| 192 | } |