msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
msarett | c1d0312 | 2016-03-25 08:58:55 -0700 | [diff] [blame] | 8 | #include "SkJpegUtility.h" |
| 9 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 10 | #include "SkCodecPriv.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 11 | |
| 12 | /* |
Matt Sarett | 912e6b8 | 2017-06-07 09:48:21 -0400 | [diff] [blame] | 13 | * Call longjmp to continue execution on an error |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 14 | */ |
Matt Sarett | 912e6b8 | 2017-06-07 09:48:21 -0400 | [diff] [blame] | 15 | void skjpeg_err_exit(j_common_ptr dinfo) { |
| 16 | // Simply return to Skia client code |
| 17 | // JpegDecoderMgr will take care of freeing memory |
| 18 | skjpeg_error_mgr* error = (skjpeg_error_mgr*) dinfo->err; |
| 19 | (*error->output_message) (dinfo); |
| 20 | longjmp(error->fJmpBuf, 1); |
| 21 | } |
| 22 | |
| 23 | // Functions for buffered sources // |
| 24 | |
| 25 | /* |
| 26 | * Initialize the buffered source manager |
| 27 | */ |
| 28 | static void sk_init_buffered_source(j_decompress_ptr dinfo) { |
Matt Sarett | 15f4d02 | 2017-06-06 17:58:33 +0000 | [diff] [blame] | 29 | skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 30 | src->next_input_byte = (const JOCTET*) src->fBuffer; |
| 31 | src->bytes_in_buffer = 0; |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | * Fill the input buffer from the stream |
| 36 | */ |
Matt Sarett | 912e6b8 | 2017-06-07 09:48:21 -0400 | [diff] [blame] | 37 | static boolean sk_fill_buffered_input_buffer(j_decompress_ptr dinfo) { |
Matt Sarett | 15f4d02 | 2017-06-06 17:58:33 +0000 | [diff] [blame] | 38 | skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src; |
| 39 | size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize); |
mtklein | 525e90a | 2015-06-18 09:58:57 -0700 | [diff] [blame] | 40 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 41 | // libjpeg is still happy with a less than full read, as long as the result is non-zero |
| 42 | if (bytes == 0) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | src->next_input_byte = (const JOCTET*) src->fBuffer; |
| 47 | src->bytes_in_buffer = bytes; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Skip a certain number of bytes in the stream |
| 53 | */ |
Matt Sarett | 912e6b8 | 2017-06-07 09:48:21 -0400 | [diff] [blame] | 54 | static void sk_skip_buffered_input_data(j_decompress_ptr dinfo, long numBytes) { |
Matt Sarett | 15f4d02 | 2017-06-06 17:58:33 +0000 | [diff] [blame] | 55 | skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 56 | size_t bytes = (size_t) numBytes; |
| 57 | |
| 58 | if (bytes > src->bytes_in_buffer) { |
| 59 | size_t bytesToSkip = bytes - src->bytes_in_buffer; |
| 60 | if (bytesToSkip != src->fStream->skip(bytesToSkip)) { |
| 61 | SkCodecPrintf("Failure to skip.\n"); |
| 62 | dinfo->err->error_exit((j_common_ptr) dinfo); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | src->next_input_byte = (const JOCTET*) src->fBuffer; |
| 67 | src->bytes_in_buffer = 0; |
| 68 | } else { |
| 69 | src->next_input_byte += numBytes; |
| 70 | src->bytes_in_buffer -= numBytes; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * We do not need to do anything to terminate our stream |
| 76 | */ |
| 77 | static void sk_term_source(j_decompress_ptr dinfo) |
msarett | 19ae315 | 2015-10-02 13:44:12 -0700 | [diff] [blame] | 78 | { |
| 79 | // The current implementation of SkJpegCodec does not call |
| 80 | // jpeg_finish_decompress(), so this function is never called. |
| 81 | // If we want to modify this function to do something, we also |
| 82 | // need to modify SkJpegCodec to call jpeg_finish_decompress(). |
| 83 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 84 | |
Matt Sarett | 912e6b8 | 2017-06-07 09:48:21 -0400 | [diff] [blame] | 85 | // Functions for memory backed sources // |
| 86 | |
| 87 | /* |
| 88 | * Initialize the mem backed source manager |
| 89 | */ |
| 90 | static void sk_init_mem_source(j_decompress_ptr dinfo) { |
| 91 | /* no work necessary here, all things are done in constructor */ |
| 92 | } |
| 93 | |
| 94 | static void sk_skip_mem_input_data (j_decompress_ptr cinfo, long num_bytes) { |
| 95 | jpeg_source_mgr* src = cinfo->src; |
| 96 | size_t bytes = static_cast<size_t>(num_bytes); |
| 97 | if(bytes > src->bytes_in_buffer) { |
| 98 | src->next_input_byte = nullptr; |
| 99 | src->bytes_in_buffer = 0; |
| 100 | } else { |
| 101 | src->next_input_byte += bytes; |
| 102 | src->bytes_in_buffer -= bytes; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static boolean sk_fill_mem_input_buffer (j_decompress_ptr cinfo) { |
| 107 | /* The whole JPEG data is expected to reside in the supplied memory, |
| 108 | * buffer, so any request for more data beyond the given buffer size |
| 109 | * is treated as an error. |
| 110 | */ |
| 111 | return false; |
| 112 | } |
| 113 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 114 | /* |
| 115 | * Constructor for the source manager that we provide to libjpeg |
| 116 | * We provide skia implementations of all of the stream processing functions required by libjpeg |
| 117 | */ |
Matt Sarett | 15f4d02 | 2017-06-06 17:58:33 +0000 | [diff] [blame] | 118 | skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream) |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 119 | : fStream(stream) |
| 120 | { |
Matt Sarett | 912e6b8 | 2017-06-07 09:48:21 -0400 | [diff] [blame] | 121 | if (stream->hasLength() && stream->getMemoryBase()) { |
| 122 | init_source = sk_init_mem_source; |
| 123 | fill_input_buffer = sk_fill_mem_input_buffer; |
| 124 | skip_input_data = sk_skip_mem_input_data; |
| 125 | resync_to_restart = jpeg_resync_to_restart; |
| 126 | term_source = sk_term_source; |
| 127 | bytes_in_buffer = static_cast<size_t>(stream->getLength()); |
| 128 | next_input_byte = static_cast<const JOCTET*>(stream->getMemoryBase()); |
| 129 | } else { |
| 130 | init_source = sk_init_buffered_source; |
| 131 | fill_input_buffer = sk_fill_buffered_input_buffer; |
| 132 | skip_input_data = sk_skip_buffered_input_data; |
| 133 | resync_to_restart = jpeg_resync_to_restart; |
| 134 | term_source = sk_term_source; |
| 135 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 136 | } |