Debian Codegraph @

similar/2d/pcx.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19 /*
20 *
21 * Routines to read/write pcx images.
22 *
23 */
24
25 #include <span>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "gr.h"
31 #include "u_mem.h"
32 #include "pcx.h"
33 #include "physfsx.h"
34 #include "palette.h"
35
36 #include "dxxsconf.h"
37 #include "dsx-ns.h"
38 #include "compiler-poison.h"
39 #include "compiler-range_for.h"
40 #include "d_range.h"
41 #include "partial_range.h"
42 #include "console.h"
43
44 #if DXX_USE_SDLIMAGE
45 #include "physfsrwops.h"
46 #include <SDL_image.h>
47 #include "d_uspan.h"
48 #endif
49
50 namespace dcx {
51
52 namespace {
53
54 #if !DXX_USE_OGL && DXX_USE_SCREENSHOT_FORMAT_LEGACY
55 [[nodiscard]]
56 static int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_File *fid);
57 [[nodiscard]]
58 static int pcx_encode_line(std::span<const uint8_t> in, PHYSFS_File *fp);
59
60 /* PCX Header data type */
61 struct PCXHeader
62 {
63 ubyte Manufacturer;
64 ubyte Version;
65 ubyte Encoding;
66 ubyte BitsPerPixel;
67 short Xmin;
68 short Ymin;
69 short Xmax;
70 short Ymax;
71 short Hdpi;
72 short Vdpi;
73 ubyte ColorMap[16][3];
74 ubyte Reserved;
75 ubyte Nplanes;
76 short BytesPerLine;
77 ubyte filler[60];
78 } __pack__;
79 #endif
80
81 #if DXX_USE_SDLIMAGE
82 static pcx_result pcx_read_bitmap(const char *const filename, grs_main_bitmap &bmp, palette_array_t &palette, RWops_ptr rw)
83 {
84 RAII_SDL_Surface surface(IMG_LoadPCX_RW(rw.get()));
85 if (!surface.surface)
86 {
87 con_printf(CON_NORMAL, "%s:%u: failed to create surface from \"%s\"", __FILE__, __LINE__, filename);
88 return pcx_result::ERROR_OPENING;
89 }
90 const auto &s = *surface.surface.get();
91 const auto fmt{s.format};
92 if (!fmt || fmt->BitsPerPixel != 8)
93 return pcx_result::ERROR_WRONG_VERSION;
94 const auto fpal = fmt->palette;
95 if (!fpal || fpal->ncolors != palette.size())
96 return pcx_result::ERROR_NO_PALETTE;
97 const unsigned xsize = s.w;
98 const unsigned ysize = s.h;
99 if (xsize > 3840)
100 return pcx_result::ERROR_MEMORY;
101 if (ysize > 2400)
102 return pcx_result::ERROR_MEMORY;
103 DXX_CHECK_MEM_IS_DEFINED(std::span(static_cast<const std::byte *>(s.pixels), xsize * ysize));
104 gr_init_bitmap_alloc(bmp, bm_mode::linear, 0, 0, xsize, ysize, xsize);
105 std::copy_n(reinterpret_cast<const uint8_t *>(s.pixels), xsize * ysize, &bmp.get_bitmap_data()[0]);
106 {
107 const auto a = [](const SDL_Color &c) {
108 return rgb_t{
109 static_cast<uint8_t>(c.r >> 2),
110 static_cast<uint8_t>(c.g >> 2),
111 static_cast<uint8_t>(c.b >> 2)
112 };
113 };
114 std::transform(fpal->colors, fpal->colors + palette.size(), palette.begin(), a);
115 }
116 return pcx_result::SUCCESS;
117 }
118 #endif
119
120 static pcx_result pcx_read_blank(grs_main_bitmap &bmp, palette_array_t &palette)
121 {
122 constexpr unsigned xsize = 640;
123 constexpr unsigned ysize = 480;
124 gr_init_bitmap_alloc(bmp, bm_mode::linear, 0, 0, xsize, ysize, xsize);
125 auto &bitmap_data = *reinterpret_cast<uint8_t (*)[ysize][xsize]>(bmp.get_bitmap_data());
126 constexpr uint8_t border = 1;
127 constexpr uint8_t body = 0;
128 std::fill(&bitmap_data[0][0], &bitmap_data[2][0], border);
129 std::fill(&bitmap_data[2][0], &bitmap_data[ysize - 2][0], body);
130 std::fill(&bitmap_data[ysize - 2][0], &bitmap_data[ysize][0], border);
131 for (auto &&y : xrange(2u, ysize - 2))
132 {
133 bitmap_data[y][0] = border;
134 bitmap_data[y][1] = border;
135 bitmap_data[y][xsize - 2] = border;
136 bitmap_data[y][xsize - 1] = border;
137 }
138 palette = {};
139 palette[border] = {63, 63, 63};
140 return pcx_result::SUCCESS;
141 }
142
143 #if !DXX_USE_SDLIMAGE
144 static pcx_result pcx_support_not_compiled(const char *const filename, grs_main_bitmap &bmp, palette_array_t &palette)
145 {
146 con_printf(CON_NORMAL, "%s:%u: PCX support disabled at compile time; cannot read file \"%s\"", __FILE__, __LINE__, filename);
147 return pcx_read_blank(bmp, palette);
148 }
149 #endif
150
151 }
152
153 }
154
155 #if DXX_BUILD_DESCENT == 1
156 namespace dsx {
157
158 namespace {
159
160 #if DXX_USE_SDLIMAGE
161 static auto load_physfs_blob(const char *const filename)
162 {
163 unique_span<uint8_t> result;
164 if (auto &&[file, physfserr]{PHYSFSX_openReadBuffered(filename)}; !file)
165 {
166 con_printf(CON_VERBOSE, "%s:%u: failed to open \"%s\": %s", __FILE__, __LINE__, filename, PHYSFS_getErrorByCode(physfserr));
167 }
168 else if (const std::size_t fsize = PHYSFS_fileLength(file); fsize > 0x100000u)
169 {
170 con_printf(CON_VERBOSE, "%s:%u: file too large \"%s\"", __FILE__, __LINE__, filename);
171 }
172 else if (PHYSFSX_readBytes(file, (result = {fsize}).get(), fsize) != fsize)
173 {
174 result.reset();
175 con_printf(CON_VERBOSE, "%s:%u: failed to read \"%s\"", __FILE__, __LINE__, filename);
176 }
177 return result;
178 }
179
180 static auto load_decoded_physfs_blob(const char *const filename)
181 {
182 unique_span<uint8_t> decoded_buffer;
183 auto encoded_blob{load_physfs_blob(filename)};
184 const auto b{encoded_blob.get()};
185 if (!b)
186 return decoded_buffer;
187 const std::size_t data_size = encoded_blob.size() - 1;
188 auto &&transform_predicate{[xor_value = b[data_size]](const uint8_t c) mutable {
189 return c ^ --xor_value;
190 }};
191 decoded_buffer = {data_size};
192 std::transform(std::make_reverse_iterator(std::next(b, data_size)), std::make_reverse_iterator(b), decoded_buffer.get(), transform_predicate);
193 return decoded_buffer;
194 }
195 #endif
196
197 }
198
199 pcx_result bald_guy_load(const char *const filename, grs_main_bitmap &bmp, palette_array_t &palette)
200 {
201 #if DXX_USE_SDLIMAGE
202 const auto &&bguy_data{load_decoded_physfs_blob(filename)};
203 const auto b{bguy_data.get()};
204 if (!b)
205 return pcx_result::ERROR_OPENING;
206
207 RWops_ptr rw(SDL_RWFromConstMem(b, bguy_data.size()));
208 return pcx_read_bitmap(filename, bmp, palette, std::move(rw));
209 #else
210 return pcx_support_not_compiled(filename, bmp, palette);
211 #endif
212 }
213
214 }
215 #endif
216
217 namespace dcx {
218
219 pcx_result pcx_read_bitmap(const char *const filename, grs_main_bitmap &bmp, palette_array_t &palette)
220 {
221 #if DXX_USE_SDLIMAGE
222 /* Try to enable buffering on the PHYSFS file. On Windows,
223 * unbuffered access to the file causes SDL_image to be slow enough
224 * for users to detect the latency and report it as an issue. On
225 * Linux, unbuffered access is still fast.
226 */
227 auto &&[rw, physfserr] = PHYSFSRWOPS_openReadBuffered(filename, 1024 * 1024);
228 if (!rw)
229 {
230 con_printf(CON_NORMAL, "%s:%u: failed to open \"%s\": %s", __FILE__, __LINE__, filename, PHYSFS_getErrorByCode(physfserr));
231 return pcx_result::ERROR_OPENING;
232 }
233 return pcx_read_bitmap(filename, bmp, palette, std::move(rw));
234 #else
235 return pcx_support_not_compiled(filename, bmp, palette);
236 #endif
237 }
238
239 pcx_result pcx_read_bitmap_or_default(const char *const filename, grs_main_bitmap &bmp, palette_array_t &palette)
240 {
241 const auto r = pcx_read_bitmap(filename, bmp, palette);
242 if (r != pcx_result::SUCCESS)
243 pcx_read_blank(bmp, palette);
244 return r;
245 }
246
247 #if !DXX_USE_OGL && DXX_USE_SCREENSHOT_FORMAT_LEGACY
248 unsigned pcx_write_bitmap(PHYSFS_File *const PCXfile, const grs_bitmap *const bmp, palette_array_t &palette)
249 {
250 static constexpr std::size_t PCXHEADER_SIZE{128};
251 ubyte data;
252 PCXHeader header{};
253
254 header.Manufacturer = 10;
255 header.Encoding = 1;
256 header.Nplanes = 1;
257 header.BitsPerPixel = 8;
258 header.Version = 5;
259 header.Xmax = bmp->bm_w-1;
260 header.Ymax = bmp->bm_h-1;
261 header.BytesPerLine = bmp->bm_w;
262
263 if (PHYSFSX_writeBytes(PCXfile, &header, PCXHEADER_SIZE) != PCXHEADER_SIZE)
264 {
265 return 1;
266 }
267
268 {
269 const uint_fast32_t bm_w = bmp->bm_w;
270 const uint_fast32_t bm_rowsize = bmp->bm_rowsize;
271 const auto bm_data = bmp->get_bitmap_data();
272 const auto e = &bm_data[bm_rowsize * bmp->bm_h];
273 for (auto i = &bm_data[0]; i != e; i += bm_rowsize)
274 {
275 if (!pcx_encode_line(std::span{i, bm_w}, PCXfile))
276 {
277 return 1;
278 }
279 }
280 }
281
282 // Mark an extended palette
283 data = 12;
284 if (PHYSFSX_writeBytes(PCXfile, &data, 1) != 1)
285 {
286 return 1;
287 }
288
289 const auto retval{PHYSFSX_writeBytes(PCXfile, palette.data(), palette.size())};
290 if (retval != palette.size())
291 return 1;
292 return 0;
293 }
294
295 namespace {
296
297 // returns number of bytes written into outBuff, 0 if failed
298 int pcx_encode_line(const std::span<const uint8_t> in, PHYSFS_File *fp)
299 {
300 int i;
301 int total;
302 ubyte runCount; // max single runlength is 63
303 total = 0;
304 auto last = in.front();
305 runCount = 1;
306
307 range_for (const auto ub, unchecked_partial_range(in.data(), 1u, in.size()))
308 {
309 if (ub == last) {
310 runCount++; // it encodes
311 if (runCount == 63) {
312 if (!(i=pcx_encode_byte(last, runCount, fp)))
313 return(0);
314 total += i;
315 runCount = 0;
316 }
317 } else { // this != last
318 if (runCount) {
319 if (!(i=pcx_encode_byte(last, runCount, fp)))
320 return(0);
321 total += i;
322 }
323 last = ub;
324 runCount = 1;
325 }
326 }
327
328 if (runCount) { // finish up
329 if (!(i=pcx_encode_byte(last, runCount, fp)))
330 return 0;
331 return total + i;
332 }
333 return total;
334 }
335
336 static inline int PHYSFSX_putc(PHYSFS_File *file, uint8_t ch)
337 {
338 if (PHYSFSX_writeBytes(file, &ch, 1) < 1)
339 return -1;
340 else
341 return ch;
342 }
343
344 // subroutine for writing an encoded byte pair
345 // returns count of bytes written, 0 if error
346 int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_File *fid)
347 {
348 if (cnt) {
349 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
350 if(EOF == PHYSFSX_putc(fid, static_cast<int>(byt)))
351 return 0; // disk write error (probably full)
352 return 1;
353 } else {
354 if(EOF == PHYSFSX_putc(fid, 0xC0 | cnt))
355 return 0; // disk write error
356 if(EOF == PHYSFSX_putc(fid, static_cast<int>(byt)))
357 return 0; // disk write error
358 return 2;
359 }
360 }
361 return 0;
362 }
363
364 }
365 #endif
366
367 //text for error messges
368 constexpr char pcx_error_messages[] = {
369 "No error.\0"
370 "Error opening file.\0"
371 "Could not read PCX header.\0"
372 "Unsupported PCX version.\0"
373 "Error reading data.\0"
374 "Could not find palette information.\0"
375 "Error writing data.\0"
376 };
377
378
379 //function to return pointer to error message
380 const char *pcx_errormsg(const pcx_result r)
381 {
382 const char *p = pcx_error_messages;
383
384 unsigned error_number = static_cast<unsigned>(r);
385 while (error_number--) {
386 if (p == std::end(pcx_error_messages))
387 return nullptr;
388 p += strlen(p)+1;
389 }
390
391 return p;
392 }
393
394 }