| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2010 The Android Open Source Project | 3 * Copyright 2010 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #include "SkJpegUtility.h" | 10 #include "SkJpegUtility.h" |
| 11 | 11 |
| 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 |
| 12 ///////////////////////////////////////////////////////////////////// | 18 ///////////////////////////////////////////////////////////////////// |
| 13 static void sk_init_source(j_decompress_ptr cinfo) { | 19 static void sk_init_source(j_decompress_ptr cinfo) { |
| 14 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; | 20 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 15 src->next_input_byte = (const JOCTET*)src->fBuffer; | 21 src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 16 src->bytes_in_buffer = 0; | 22 src->bytes_in_buffer = 0; |
| 23 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 24 src->current_offset = 0; |
| 25 #endif |
| 17 src->fStream->rewind(); | 26 src->fStream->rewind(); |
| 18 } | 27 } |
| 19 | 28 |
| 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 |
| 20 static boolean sk_fill_input_buffer(j_decompress_ptr cinfo) { | 47 static boolean sk_fill_input_buffer(j_decompress_ptr cinfo) { |
| 21 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; | 48 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 22 if (src->fDecoder != NULL && src->fDecoder->shouldCancelDecode()) { | 49 if (src->fDecoder != NULL && src->fDecoder->shouldCancelDecode()) { |
| 23 return FALSE; | 50 return FALSE; |
| 24 } | 51 } |
| 25 size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSi
ze); | 52 size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSi
ze); |
| 26 // note that JPEG is happy with less than the full read, | 53 // note that JPEG is happy with less than the full read, |
| 27 // as long as the result is non-zero | 54 // as long as the result is non-zero |
| 28 if (bytes == 0) { | 55 if (bytes == 0) { |
| 29 return FALSE; | 56 return FALSE; |
| 30 } | 57 } |
| 31 | 58 |
| 59 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 60 src->current_offset += bytes; |
| 61 #endif |
| 32 src->next_input_byte = (const JOCTET*)src->fBuffer; | 62 src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 33 src->bytes_in_buffer = bytes; | 63 src->bytes_in_buffer = bytes; |
| 34 return TRUE; | 64 return TRUE; |
| 35 } | 65 } |
| 36 | 66 |
| 37 static void sk_skip_input_data(j_decompress_ptr cinfo, long num_bytes) { | 67 static void sk_skip_input_data(j_decompress_ptr cinfo, long num_bytes) { |
| 38 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; | 68 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 39 | 69 |
| 40 if (num_bytes > (long)src->bytes_in_buffer) { | 70 if (num_bytes > (long)src->bytes_in_buffer) { |
| 41 long bytesToSkip = num_bytes - src->bytes_in_buffer; | 71 long bytesToSkip = num_bytes - src->bytes_in_buffer; |
| 42 while (bytesToSkip > 0) { | 72 while (bytesToSkip > 0) { |
| 43 long bytes = (long)src->fStream->skip(bytesToSkip); | 73 long bytes = (long)src->fStream->skip(bytesToSkip); |
| 44 if (bytes <= 0 || bytes > bytesToSkip) { | 74 if (bytes <= 0 || bytes > bytesToSkip) { |
| 45 // SkDebugf("xxxxxxxxxxxxxx failure to skip request %d returned %d\
n", bytesToSkip, bytes); | 75 // SkDebugf("xxxxxxxxxxxxxx failure to skip request %d returned %d\
n", bytesToSkip, bytes); |
| 46 cinfo->err->error_exit((j_common_ptr)cinfo); | 76 cinfo->err->error_exit((j_common_ptr)cinfo); |
| 47 return; | 77 return; |
| 48 } | 78 } |
| 79 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 80 src->current_offset += bytes; |
| 81 #endif |
| 49 bytesToSkip -= bytes; | 82 bytesToSkip -= bytes; |
| 50 } | 83 } |
| 51 src->next_input_byte = (const JOCTET*)src->fBuffer; | 84 src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 52 src->bytes_in_buffer = 0; | 85 src->bytes_in_buffer = 0; |
| 53 } else { | 86 } else { |
| 54 src->next_input_byte += num_bytes; | 87 src->next_input_byte += num_bytes; |
| 55 src->bytes_in_buffer -= num_bytes; | 88 src->bytes_in_buffer -= num_bytes; |
| 56 } | 89 } |
| 57 } | 90 } |
| 58 | 91 |
| 59 static boolean sk_resync_to_restart(j_decompress_ptr cinfo, int desired) { | 92 static boolean sk_resync_to_restart(j_decompress_ptr cinfo, int desired) { |
| 60 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; | 93 skjpeg_source_mgr* src = (skjpeg_source_mgr*)cinfo->src; |
| 61 | 94 |
| 62 // what is the desired param for??? | 95 // what is the desired param for??? |
| 63 | 96 |
| 64 if (!src->fStream->rewind()) { | 97 if (!src->fStream->rewind()) { |
| 65 SkDebugf("xxxxxxxxxxxxxx failure to rewind\n"); | 98 SkDebugf("xxxxxxxxxxxxxx failure to rewind\n"); |
| 66 cinfo->err->error_exit((j_common_ptr)cinfo); | 99 cinfo->err->error_exit((j_common_ptr)cinfo); |
| 67 return FALSE; | 100 return FALSE; |
| 68 } | 101 } |
| 69 src->next_input_byte = (const JOCTET*)src->fBuffer; | 102 src->next_input_byte = (const JOCTET*)src->fBuffer; |
| 70 src->bytes_in_buffer = 0; | 103 src->bytes_in_buffer = 0; |
| 71 return TRUE; | 104 return TRUE; |
| 72 } | 105 } |
| 73 | 106 |
| 74 static void sk_term_source(j_decompress_ptr /*cinfo*/) {} | 107 static void sk_term_source(j_decompress_ptr /*cinfo*/) {} |
| 75 | 108 |
| 76 | 109 |
| 77 #if 0 // UNUSED | |
| 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 + n
um_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*/) {} | |
| 102 #endif | |
| 103 | |
| 104 | |
| 105 /////////////////////////////////////////////////////////////////////////////// | 110 /////////////////////////////////////////////////////////////////////////////// |
| 106 | 111 |
| 107 skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder, | 112 skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream, SkImageDecoder* decoder, |
| 108 bool ownStream) : fStream(stream) { | 113 bool ownStream) : fStream(stream) { |
| 109 fDecoder = decoder; | 114 fDecoder = decoder; |
| 110 // const void* baseAddr = stream->getMemoryBase(); | |
| 111 fMemoryBase = NULL; | 115 fMemoryBase = NULL; |
| 112 fUnrefStream = ownStream; | 116 fUnrefStream = ownStream; |
| 113 fMemoryBaseSize = 0; | 117 fMemoryBaseSize = 0; |
| 114 | 118 |
| 115 init_source = sk_init_source; | 119 init_source = sk_init_source; |
| 116 fill_input_buffer = sk_fill_input_buffer; | 120 fill_input_buffer = sk_fill_input_buffer; |
| 117 skip_input_data = sk_skip_input_data; | 121 skip_input_data = sk_skip_input_data; |
| 118 resync_to_restart = sk_resync_to_restart; | 122 resync_to_restart = sk_resync_to_restart; |
| 119 term_source = sk_term_source; | 123 term_source = sk_term_source; |
| 124 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 125 seek_input_data = sk_seek_input_data; |
| 126 #endif |
| 120 // SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBa
seSize); | 127 // SkDebugf("**************** use memorybase %p %d\n", fMemoryBase, fMemoryBa
seSize); |
| 121 } | 128 } |
| 122 | 129 |
| 123 skjpeg_source_mgr::~skjpeg_source_mgr() { | 130 skjpeg_source_mgr::~skjpeg_source_mgr() { |
| 124 if (fMemoryBase) { | 131 if (fMemoryBase) { |
| 125 sk_free(fMemoryBase); | 132 sk_free(fMemoryBase); |
| 126 } | 133 } |
| 127 if (fUnrefStream) { | 134 if (fUnrefStream) { |
| 128 fStream->unref(); | 135 fStream->unref(); |
| 129 } | 136 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 void skjpeg_error_exit(j_common_ptr cinfo) { | 183 void skjpeg_error_exit(j_common_ptr cinfo) { |
| 177 skjpeg_error_mgr* error = (skjpeg_error_mgr*)cinfo->err; | 184 skjpeg_error_mgr* error = (skjpeg_error_mgr*)cinfo->err; |
| 178 | 185 |
| 179 (*error->output_message) (cinfo); | 186 (*error->output_message) (cinfo); |
| 180 | 187 |
| 181 /* Let the memory manager delete any temp files before we die */ | 188 /* Let the memory manager delete any temp files before we die */ |
| 182 jpeg_destroy(cinfo); | 189 jpeg_destroy(cinfo); |
| 183 | 190 |
| 184 longjmp(error->fJmpBuf, -1); | 191 longjmp(error->fJmpBuf, -1); |
| 185 } | 192 } |
| OLD | NEW |