| 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 | |
| 8 | #include "src/codec/SkJpegUtility.h" |
| 9 | |
| 10 | #include "include/core/SkStream.h" |
| 11 | #include "include/core/SkTypes.h" |
| 12 | #include "include/private/base/SkTArray.h" |
| 13 | #include "src/codec/SkCodecPriv.h" |
| 14 | #include "src/codec/SkJpegPriv.h" |
| 15 | |
| 16 | #include <csetjmp> |
| 17 | #include <cstddef> |
| 18 | |
| 19 | extern "C" { |
| 20 | #include "jmorecfg.h" |
| 21 | #include "jpeglib.h" |
| 22 | } |
| 23 | |
| 24 | /* |
| 25 | * Call longjmp to continue execution on an error |
| 26 | */ |
| 27 | void skjpeg_err_exit(j_common_ptr dinfo) { |
| 28 | // Simply return to Skia client code |
| 29 | // JpegDecoderMgr will take care of freeing memory |
| 30 | skjpeg_error_mgr* error = (skjpeg_error_mgr*) dinfo->err; |
| 31 | (*error->output_message) (dinfo); |
| 32 | if (error->fJmpBufStack.empty()) { |
| 33 | SK_ABORT("JPEG error with no jmp_buf set." ); |
| 34 | } |
| 35 | longjmp(env: *error->fJmpBufStack.back(), val: 1); |
| 36 | } |
| 37 | |
| 38 | // Functions for buffered sources // |
| 39 | |
| 40 | /* |
| 41 | * Initialize the buffered source manager |
| 42 | */ |
| 43 | static void sk_init_buffered_source(j_decompress_ptr dinfo) { |
| 44 | skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src; |
| 45 | src->next_input_byte = (const JOCTET*) src->fBuffer; |
| 46 | src->bytes_in_buffer = 0; |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Fill the input buffer from the stream |
| 51 | */ |
| 52 | static boolean sk_fill_buffered_input_buffer(j_decompress_ptr dinfo) { |
| 53 | skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src; |
| 54 | size_t bytes = src->fStream->read(buffer: src->fBuffer, size: skjpeg_source_mgr::kBufferSize); |
| 55 | |
| 56 | // libjpeg is still happy with a less than full read, as long as the result is non-zero |
| 57 | if (bytes == 0) { |
| 58 | // Let libjpeg know that the buffer needs to be refilled |
| 59 | src->next_input_byte = nullptr; |
| 60 | src->bytes_in_buffer = 0; |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | src->next_input_byte = (const JOCTET*) src->fBuffer; |
| 65 | src->bytes_in_buffer = bytes; |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Skip a certain number of bytes in the stream |
| 71 | */ |
| 72 | static void sk_skip_buffered_input_data(j_decompress_ptr dinfo, long numBytes) { |
| 73 | skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src; |
| 74 | size_t bytes = (size_t) numBytes; |
| 75 | |
| 76 | if (bytes > src->bytes_in_buffer) { |
| 77 | size_t bytesToSkip = bytes - src->bytes_in_buffer; |
| 78 | if (bytesToSkip != src->fStream->skip(size: bytesToSkip)) { |
| 79 | SkCodecPrintf("Failure to skip.\n" ); |
| 80 | dinfo->err->error_exit((j_common_ptr) dinfo); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | src->next_input_byte = (const JOCTET*) src->fBuffer; |
| 85 | src->bytes_in_buffer = 0; |
| 86 | } else { |
| 87 | src->next_input_byte += numBytes; |
| 88 | src->bytes_in_buffer -= numBytes; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * We do not need to do anything to terminate our stream |
| 94 | */ |
| 95 | static void sk_term_source(j_decompress_ptr dinfo) |
| 96 | { |
| 97 | // The current implementation of SkJpegCodec does not call |
| 98 | // jpeg_finish_decompress(), so this function is never called. |
| 99 | // If we want to modify this function to do something, we also |
| 100 | // need to modify SkJpegCodec to call jpeg_finish_decompress(). |
| 101 | } |
| 102 | |
| 103 | // Functions for memory backed sources // |
| 104 | |
| 105 | /* |
| 106 | * Initialize the mem backed source manager |
| 107 | */ |
| 108 | static void sk_init_mem_source(j_decompress_ptr dinfo) { |
| 109 | /* no work necessary here, all things are done in constructor */ |
| 110 | } |
| 111 | |
| 112 | static void sk_skip_mem_input_data (j_decompress_ptr cinfo, long num_bytes) { |
| 113 | jpeg_source_mgr* src = cinfo->src; |
| 114 | size_t bytes = static_cast<size_t>(num_bytes); |
| 115 | if(bytes > src->bytes_in_buffer) { |
| 116 | src->next_input_byte = nullptr; |
| 117 | src->bytes_in_buffer = 0; |
| 118 | } else { |
| 119 | src->next_input_byte += bytes; |
| 120 | src->bytes_in_buffer -= bytes; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static boolean sk_fill_mem_input_buffer (j_decompress_ptr cinfo) { |
| 125 | /* The whole JPEG data is expected to reside in the supplied memory, |
| 126 | * buffer, so any request for more data beyond the given buffer size |
| 127 | * is treated as an error. |
| 128 | */ |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Constructor for the source manager that we provide to libjpeg |
| 134 | * We provide skia implementations of all of the stream processing functions required by libjpeg |
| 135 | */ |
| 136 | skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream) |
| 137 | : fStream(stream) |
| 138 | { |
| 139 | if (stream->hasLength() && stream->getMemoryBase()) { |
| 140 | init_source = sk_init_mem_source; |
| 141 | fill_input_buffer = sk_fill_mem_input_buffer; |
| 142 | skip_input_data = sk_skip_mem_input_data; |
| 143 | resync_to_restart = jpeg_resync_to_restart; |
| 144 | term_source = sk_term_source; |
| 145 | bytes_in_buffer = static_cast<size_t>(stream->getLength()); |
| 146 | next_input_byte = static_cast<const JOCTET*>(stream->getMemoryBase()); |
| 147 | } else { |
| 148 | init_source = sk_init_buffered_source; |
| 149 | fill_input_buffer = sk_fill_buffered_input_buffer; |
| 150 | skip_input_data = sk_skip_buffered_input_data; |
| 151 | resync_to_restart = jpeg_resync_to_restart; |
| 152 | term_source = sk_term_source; |
| 153 | } |
| 154 | } |
| 155 | |