1/*
2 * LegacyClonk
3 *
4 * Copyright (c) 1998-2000, Matthes Bender RedWolf Design
5 * Copyright (c) 2017-2019, The LegacyClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16
17// JPEG decoding using libjpeg
18
19#include <Standard.h>
20#include <StdJpeg.h>
21
22#include <stdexcept>
23
24// Some distributions ship jpeglib.h with extern "C", others don't - gah.
25extern "C"
26{
27#include <jpeglib.h>
28}
29
30struct StdJpeg::Impl
31{
32 jpeg_decompress_struct cinfo;
33 jpeg_error_mgr error_mgr;
34 jpeg_source_mgr source_mgr;
35
36 static constexpr JOCTET end_of_input[] = { 0xff, JPEG_EOI };
37
38 void *rowBuffer;
39
40 Impl(const void *const fileContents, const std::size_t fileSize)
41 {
42 try
43 {
44 Init(fileContents, fileSize);
45 }
46 catch (const std::runtime_error &)
47 {
48 Clear();
49 throw;
50 }
51 }
52
53 void Init(const void *const fileContents, const std::size_t fileSize)
54 {
55 // Error handling: Throw exception on critical error; ignore non-critical messages
56 cinfo.err = jpeg_std_error(err: &error_mgr);
57 error_mgr.error_exit = [](const j_common_ptr cinfo)
58 {
59 char buffer[JMSG_LENGTH_MAX];
60 cinfo->err->format_message(cinfo, buffer);
61 throw std::runtime_error(buffer);
62 };
63 error_mgr.output_message = [](j_common_ptr) {};
64 jpeg_create_decompress(&cinfo);
65
66 // no fancy function calling
67 cinfo.src = &source_mgr;
68 source_mgr.next_input_byte = static_cast<const JOCTET *>(fileContents);
69 source_mgr.bytes_in_buffer = fileSize;
70 source_mgr.init_source = [](j_decompress_ptr) {};
71 source_mgr.fill_input_buffer = [](const j_decompress_ptr cinfo)
72 {
73 // The doc says to give fake end-of-inputs if there is no more data
74 cinfo->src->next_input_byte = end_of_input;
75 cinfo->src->bytes_in_buffer = sizeof(end_of_input);
76 return static_cast<boolean>(true);
77 };
78 source_mgr.skip_input_data = [](const j_decompress_ptr cinfo, const long num_bytes)
79 {
80 cinfo->src->next_input_byte += num_bytes;
81 cinfo->src->bytes_in_buffer -= num_bytes;
82 if (cinfo->src->bytes_in_buffer <= 0)
83 {
84 cinfo->src->next_input_byte = end_of_input;
85 cinfo->src->bytes_in_buffer = sizeof(end_of_input);
86 }
87 };
88 source_mgr.resync_to_restart = jpeg_resync_to_restart;
89 source_mgr.term_source = [](j_decompress_ptr) {};
90
91 // a missing image is an error
92 jpeg_read_header(cinfo: &cinfo, TRUE);
93
94 // Let libjpeg convert for us
95 cinfo.out_color_space = JCS_RGB;
96 jpeg_start_decompress(cinfo: &cinfo);
97
98 // Make a one-row-high sample array that will go away at jpeg_destroy_decompress
99 const JDIMENSION samplesPerRow = cinfo.output_width * cinfo.output_components;
100 rowBuffer = (*cinfo.mem->alloc_sarray)
101 (reinterpret_cast<j_common_ptr>(&cinfo), JPOOL_IMAGE, samplesPerRow, 1);
102 }
103
104 ~Impl()
105 {
106 Clear();
107 }
108
109 void Clear()
110 {
111 jpeg_destroy_decompress(cinfo: &cinfo);
112 }
113
114 void Finish()
115 {
116 jpeg_finish_decompress(cinfo: &cinfo);
117 }
118
119 const void *DecodeRow()
120 {
121 if (cinfo.output_scanline >= cinfo.output_height) return nullptr;
122
123 // read an 1-row-array of scanlines
124 jpeg_read_scanlines(cinfo: &cinfo, scanlines: static_cast<JSAMPARRAY>(rowBuffer), max_lines: 1);
125
126 return static_cast<JSAMPARRAY>(rowBuffer)[0];
127 }
128};
129
130constexpr JOCTET StdJpeg::Impl::end_of_input[];
131
132StdJpeg::StdJpeg(const void *const fileContents, const std::size_t fileSize)
133 : impl(new Impl(fileContents, fileSize)) {}
134
135StdJpeg::~StdJpeg() = default;
136
137const void *StdJpeg::DecodeRow() { return impl->DecodeRow(); }
138void StdJpeg::Finish() { impl->Finish(); }
139
140std::uint32_t StdJpeg::Width() const { return impl->cinfo.output_width; }
141std::uint32_t StdJpeg::Height() const { return impl->cinfo.output_height; }
142