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