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; |
scroggo@google.com | d4c3565 | 2013-08-01 15:03:42 +0000 | [diff] [blame^] | 32 | size_t bo = (size_t) byte_offset; |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame] | 33 | |
scroggo@google.com | d4c3565 | 2013-08-01 15:03:42 +0000 | [diff] [blame^] | 34 | if (bo > src->current_offset) { |
| 35 | (void)src->fStream->skip(bo - src->current_offset); |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame] | 36 | } else { |
| 37 | src->fStream->rewind(); |
scroggo@google.com | d4c3565 | 2013-08-01 15:03:42 +0000 | [diff] [blame^] | 38 | (void)src->fStream->skip(bo); |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame] | 39 | } |
| 40 | |
scroggo@google.com | d4c3565 | 2013-08-01 15:03:42 +0000 | [diff] [blame^] | 41 | src->current_offset = bo; |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame] | 42 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 43 | src->bytes_in_buffer = 0; |
| 44 | return true; |
| 45 | } |
| 46 | #endif |
| 47 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 48 | static boolean sk_fill_input_buffer(j_decompress_ptr cinfo) { |
| 49 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 50 | if (src->fDecoder != NULL && src->fDecoder->shouldCancelDecode()) { |
| 51 | 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 | |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame] | 60 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 61 | src->current_offset += bytes; |
| 62 | #endif |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 63 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 64 | src->bytes_in_buffer = bytes; |
| 65 | return TRUE; |
| 66 | } |
| 67 | |
| 68 | 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] | 69 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 70 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 71 | if (num_bytes > (long)src->bytes_in_buffer) { |
reed@android.com | b8fd84b | 2010-01-21 18:04:44 +0000 | [diff] [blame] | 72 | long bytesToSkip = num_bytes - src->bytes_in_buffer; |
| 73 | while (bytesToSkip > 0) { |
| 74 | long bytes = (long)src->fStream->skip(bytesToSkip); |
| 75 | 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 | } |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame] | 80 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 81 | src->current_offset += bytes; |
| 82 | #endif |
reed@android.com | b8fd84b | 2010-01-21 18:04:44 +0000 | [diff] [blame] | 83 | bytesToSkip -= bytes; |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 84 | } |
| 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 | |
| 93 | static boolean sk_resync_to_restart(j_decompress_ptr cinfo, int desired) { |
| 94 | skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 95 | |
| 96 | // what is the desired param for??? |
| 97 | |
| 98 | if (!src->fStream->rewind()) { |
| 99 | SkDebugf("xxxxxxxxxxxxxx failure to rewind\n"); |
| 100 | cinfo->err->error_exit((j_common_ptr)cinfo); |
| 101 | return FALSE; |
| 102 | } |
| 103 | src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 104 | src->bytes_in_buffer = 0; |
| 105 | return TRUE; |
| 106 | } |
| 107 | |
| 108 | static void sk_term_source(j_decompress_ptr /*cinfo*/) {} |
| 109 | |
| 110 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 111 | /////////////////////////////////////////////////////////////////////////////// |
| 112 | |
scroggo@google.com | d4c3565 | 2013-08-01 15:03:42 +0000 | [diff] [blame^] | 113 | skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder) |
| 114 | : fStream(SkRef(stream)) |
| 115 | , fDecoder(decoder) { |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 116 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 117 | init_source = sk_init_source; |
| 118 | fill_input_buffer = sk_fill_input_buffer; |
| 119 | skip_input_data = sk_skip_input_data; |
| 120 | resync_to_restart = sk_resync_to_restart; |
| 121 | term_source = sk_term_source; |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame] | 122 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 123 | seek_input_data = sk_seek_input_data; |
| 124 | #endif |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 125 | // SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBaseSize); |
| 126 | } |
| 127 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 128 | skjpeg_source_mgr::~skjpeg_source_mgr() { |
scroggo@google.com | d4c3565 | 2013-08-01 15:03:42 +0000 | [diff] [blame^] | 129 | SkSafeUnref(fStream); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 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 | } |