Flutter Engine
The Flutter Engine
SkJpegDecoderMgr.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 */
8
13
14#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
16#endif
17
18#include <cstddef>
19#include <utility>
20
21class SkStream;
22
23/*
24 * Print information, warning, and error messages
25 */
26static void print_message(const j_common_ptr info, const char caller[]) {
27 char buffer[JMSG_LENGTH_MAX];
28 info->err->format_message(info, buffer);
29 SkCodecPrintf("libjpeg error %d <%s> from %s\n", info->err->msg_code, buffer, caller);
30}
31
32/*
33 * Reporting function for error and warning messages.
34 */
35static void output_message(j_common_ptr info) {
36 print_message(info, "output_message");
37}
38
39static void progress_monitor(j_common_ptr info) {
40 int scan = ((j_decompress_ptr)info)->input_scan_number;
41 // Progressive images with a very large number of scans can cause the
42 // decoder to hang. Here we use the progress monitor to abort on
43 // a very large number of scans. 100 is arbitrary, but much larger
44 // than the number of scans we might expect in a normal image.
45 if (scan >= 100) {
47 }
48}
49
50////////////////////////////////////////////////////////////////////////////////////////////////////
51// JpegDecoderMgr
52
53bool JpegDecoderMgr::returnFalse(const char caller[]) {
54 print_message((j_common_ptr) &fDInfo, caller);
55 return false;
56}
57
59 print_message((j_common_ptr) &fDInfo, caller);
60 return result;
61}
62
64 switch (fDInfo.jpeg_color_space) {
65 case JCS_GRAYSCALE:
67 return true;
68 case JCS_YCbCr:
69 *outColor = SkEncodedInfo::kYUV_Color;
70 return true;
71 case JCS_RGB:
72#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
73 SkAndroidFrameworkUtils::SafetyNetLog("118372692");
74#endif
75 *outColor = SkEncodedInfo::kRGB_Color;
76 return true;
77 case JCS_YCCK:
79 return true;
80 case JCS_CMYK:
82 return true;
83 default:
84 return false;
85 }
86}
87
89 return fSrcMgr.fSourceMgr.get();
90}
91
93 : fSrcMgr(SkJpegSourceMgr::Make(stream)), fInit(false) {
94 // An error manager must be set before any calls to libjpeg, in order to handle failures.
95 fDInfo.err = jpeg_std_error(&fErrorMgr);
96 fErrorMgr.error_exit = skjpeg_err_exit;
97}
98
100 jpeg_create_decompress(&fDInfo);
101 fInit = true;
102 fDInfo.src = &fSrcMgr;
103 fDInfo.err->output_message = &output_message;
104 fDInfo.progress = &fProgressMgr;
105 fProgressMgr.progress_monitor = &progress_monitor;
106}
107
109 if (fInit) {
110 jpeg_destroy_decompress(&fDInfo);
111 }
112}
113
114////////////////////////////////////////////////////////////////////////////////////////////////////
115// JpegDecoderMgr::SourceMgr
116
117// static
118void JpegDecoderMgr::SourceMgr::InitSource(j_decompress_ptr dinfo) {
119 JpegDecoderMgr::SourceMgr* src = (JpegDecoderMgr::SourceMgr*)dinfo->src;
120 src->fSourceMgr->initSource(src->next_input_byte, src->bytes_in_buffer);
121}
122
123// static
124void JpegDecoderMgr::SourceMgr::SkipInputData(j_decompress_ptr dinfo, long num_bytes_long) {
125 JpegDecoderMgr::SourceMgr* src = (JpegDecoderMgr::SourceMgr*)dinfo->src;
126 size_t num_bytes = static_cast<size_t>(num_bytes_long);
127 if (!src->fSourceMgr->skipInputBytes(num_bytes, src->next_input_byte, src->bytes_in_buffer)) {
128 SkCodecPrintf("Failure to skip.\n");
129 src->next_input_byte = nullptr;
130 src->bytes_in_buffer = 0;
131 dinfo->err->error_exit((j_common_ptr)dinfo);
132 }
133}
134
135// static
136boolean JpegDecoderMgr::SourceMgr::FillInputBuffer(j_decompress_ptr dinfo) {
137 JpegDecoderMgr::SourceMgr* src = (JpegDecoderMgr::SourceMgr*)dinfo->src;
138 if (!src->fSourceMgr->fillInputBuffer(src->next_input_byte, src->bytes_in_buffer)) {
139 SkCodecPrintf("Failure to fill input buffer.\n");
140 src->next_input_byte = nullptr;
141 src->bytes_in_buffer = 0;
142 return false;
143 }
144 return true;
145}
146
147// static
148void JpegDecoderMgr::SourceMgr::TermSource(j_decompress_ptr dinfo) {}
149
150JpegDecoderMgr::SourceMgr::SourceMgr(std::unique_ptr<SkJpegSourceMgr> sourceMgr)
151 : fSourceMgr(std::move(sourceMgr)) {
152 init_source = JpegDecoderMgr::SourceMgr::InitSource;
153 fill_input_buffer = JpegDecoderMgr::SourceMgr::FillInputBuffer;
154 skip_input_data = JpegDecoderMgr::SourceMgr::SkipInputData;
155 resync_to_restart = jpeg_resync_to_restart;
156 term_source = JpegDecoderMgr::SourceMgr::TermSource;
157}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
#define SkCodecPrintf(...)
Definition: SkCodecPriv.h:23
static void output_message(j_common_ptr info)
static void progress_monitor(j_common_ptr info)
static void print_message(const j_common_ptr info, const char caller[])
void skjpeg_err_exit(j_common_ptr dinfo)
SkJpegSourceMgr * getSourceMgr()
JpegDecoderMgr(SkStream *stream)
SkCodec::Result returnFailure(const char caller[], SkCodec::Result result)
bool returnFalse(const char caller[])
bool getEncodedColor(SkEncodedInfo::Color *outColor)
Result
Definition: SkCodec.h:76
virtual bool move(long)
Definition: SkStream.h:131
if(end==-1)
GAsyncResult * result
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
Definition: ref_ptr.h:256