| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/bitmap/pcx.cpp $ |
| 21 | * $Revision: 8 $ |
| 22 | * $Date: 3/30/99 12:36a $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * Code to read PCX files |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/bitmap/pcx.cpp $ |
| 28 | * |
| 29 | * 8 3/30/99 12:36a Jeff |
| 30 | * added support for 24bit pcx's |
| 31 | * |
| 32 | * 7 2/16/99 5:50p Jason |
| 33 | * made pcx stuff only work with 8 bit files |
| 34 | * |
| 35 | * 6 10/20/98 1:41a Jeff |
| 36 | * fixed a mem_malloc/free bug |
| 37 | * |
| 38 | * 5 10/08/98 4:23p Kevin |
| 39 | * Changed code to comply with memory library usage. Always use mem_malloc |
| 40 | * , mem_free and mem_strdup |
| 41 | * |
| 42 | * 4 4/23/98 6:38p Jason |
| 43 | * made bitmaps use 1555 format |
| 44 | * |
| 45 | * 3 12/22/97 7:34p Samir |
| 46 | * Removed instances of gr.h include. Replaced with grdefs.h |
| 47 | * |
| 48 | * 2 10/15/97 5:20p Jason |
| 49 | * did a HUGE overhaul of the bitmap system |
| 50 | * |
| 51 | * 4 3/03/97 6:20p Matt |
| 52 | * Changed cfile functions to use D3 naming convention |
| 53 | * |
| 54 | * $NoKeywords: $ |
| 55 | */ |
| 56 | |
| 57 | #include "mem.h"cxx . . $ `<file>/mem/mem.h`/ |
| 58 | #include "bitmap.h"cxx . . $ `<file>/lib/bitmap.h`/ |
| 59 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 60 | #include "pstypes.h"cxx . . $ `<file>/lib/pstypes.h`/ |
| 61 | #include "grdefs.h"cxx . . $ `<file>/lib/grdefs.h`/ |
| 62 | #include <stdlib.h> |
| 63 | |
| 64 | // load an 8bit pcx image |
| 65 | static int bm_pcx_8bit_alloc_filecxx . . $ bm_pcx_8bit_alloc_file(413716a422df0036).(CFILEcxx . . $ CFILE# *infile); |
| 66 | // load a 24bit pcx image |
| 67 | static int bm_pcx_24bit_alloc_filecxx . . $ bm_pcx_24bit_alloc_file(413716a422df0036).(CFILEcxx . . $ CFILE# *infile); |
| 68 | |
| 69 | // Loads a pcx file and converts it to 16 bit. Returns bitmap handle or -1 on error |
| 70 | int bm_pcx_alloc_filecxx . . $ bm_pcx_alloc_file(413716a422df0036).(CFILEcxx . . $ CFILE# *infile) { |
| 71 | uint8_tcxx . . $ uint8_t# temp[128]; |
| 72 | |
| 73 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).(temp, 128, infile); |
| 74 | cfseekcxx . . $ cfseek(f9992d249f032805).(infile, 0, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 75 | |
| 76 | switch (temp[65]) { |
| 77 | case 1: |
| 78 | // one plane, sounds like an 8bit pcx |
| 79 | return bm_pcx_8bit_alloc_filecxx . . $ bm_pcx_8bit_alloc_file(413716a422df0036).(infile); |
| 80 | break; |
| 81 | case 3: |
| 82 | // three planes, sounds like a 24bit pcx |
| 83 | return bm_pcx_24bit_alloc_filecxx . . $ bm_pcx_24bit_alloc_file(413716a422df0036).(infile); |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | return -1; // Must be 8 bit only |
| 88 | } |
| 89 | |
| 90 | // load an 8bit pcx image |
| 91 | int bm_pcx_8bit_alloc_filecxx . . $ bm_pcx_8bit_alloc_file(413716a422df0036).(CFILEcxx . . $ CFILE# *infile) { |
| 92 | int xmin, ymin, xmax, ymax; |
| 93 | int width, height; |
| 94 | int total, run = 0; |
| 95 | int i, t; |
| 96 | int src_bm; |
| 97 | uint8_tcxx . . $ uint8_t# pred[256], pgreen[256], pblue[256]; |
| 98 | uint8_tcxx . . $ uint8_t# buf; |
| 99 | uint8_tcxx . . $ uint8_t# temp[128]; |
| 100 | |
| 101 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).(temp, 4, infile); |
| 102 | |
| 103 | if (temp[3] != 8) |
| 104 | return -1; // nope...bail |
| 105 | |
| 106 | xmin = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 107 | ymin = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 108 | xmax = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 109 | ymax = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 110 | |
| 111 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).(temp, 116, infile); |
| 112 | |
| 113 | if (temp[65 - 12] != 1) |
| 114 | return -1; // Must be 8 bit only |
| 115 | |
| 116 | width = 1 + xmax - xmin; |
| 117 | height = 1 + ymax - ymin; |
| 118 | total = width * height; |
| 119 | |
| 120 | uint8_tcxx . . $ uint8_t# *rawdata = (uint8_tcxx . . $ uint8_t# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(width * height); |
| 121 | if (!rawdata) |
| 122 | return -1; // no memory! |
| 123 | |
| 124 | run = 0; |
| 125 | while (run < total) { |
| 126 | buf = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 127 | if (buf >= 192) { |
| 128 | uint8_tcxx . . $ uint8_t# tb; |
| 129 | tb = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 130 | for (i = 0; i < (buf - 192); i++, run++) |
| 131 | rawdata[run] = tb; |
| 132 | } else { |
| 133 | rawdata[run] = buf; |
| 134 | run++; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); // ignore pad |
| 139 | |
| 140 | // Read palette |
| 141 | |
| 142 | for (i = 0; i < 256; i++) { |
| 143 | pred[i] = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 144 | pgreen[i] = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 145 | pblue[i] = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 146 | |
| 147 | pred[i] >>= 3; |
| 148 | pgreen[i] >>= 3; |
| 149 | pblue[i] >>= 3; |
| 150 | } |
| 151 | |
| 152 | src_bm = bm_AllocBitmapcxx . . $ bm_AllocBitmap(2326284602b0bb2d).(width, height, 0); |
| 153 | if (src_bm < 0) |
| 154 | return -1; // probably out of memory |
| 155 | |
| 156 | uint16_tcxx . . $ uint16_t# *data = bm_datacxx . . $ bm_data(501e1bc9f482c6bd).(src_bm, 0); |
| 157 | |
| 158 | for (i = 0; i < height; i++) { |
| 159 | for (t = 0; t < width; t++) { |
| 160 | uint16_tcxx . . $ uint16_t# pixel; |
| 161 | uint8_tcxx . . $ uint8_t# c = rawdata[i * width + t]; |
| 162 | |
| 163 | int r = pred[c]; |
| 164 | int g = pgreen[c]; |
| 165 | int b = pblue[c]; |
| 166 | |
| 167 | pixel = OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`! | (r << 10) | (g << 5) | b; |
| 168 | if (c == 0) |
| 169 | pixel = NEW_TRANSPARENT_COLORcxx . . $ `lib/grdefs.h:71:9`!; |
| 170 | |
| 171 | data[i * width + t] = pixel; |
| 172 | } |
| 173 | } |
| 174 | mem_freecxx . . $ `mem/mem.h:80:9`!(rawdata); |
| 175 | |
| 176 | return src_bm; // success! |
| 177 | } |
| 178 | |
| 179 | // load a 24bit pcx image |
| 180 | int bm_pcx_24bit_alloc_filecxx . . $ bm_pcx_24bit_alloc_file(413716a422df0036).(CFILEcxx . . $ CFILE# *infile) { |
| 181 | int xmin, ymin, xmax, ymax; |
| 182 | int width, height; |
| 183 | int total, run = 0; |
| 184 | int i, t; |
| 185 | int src_bm; |
| 186 | uint8_tcxx . . $ uint8_t# buf; |
| 187 | uint8_tcxx . . $ uint8_t# temp[128]; |
| 188 | |
| 189 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).(temp, 4, infile); |
| 190 | |
| 191 | if (temp[1] < 5) { |
| 192 | // need at least version 5.0f |
| 193 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PCXLoad: PCX Not version 5.0 or greater\n"); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | if (temp[3] != 8) { |
| 198 | // need 8 bits per pixel |
| 199 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PCXLoad: PCX Not 8 bpp\n"); |
| 200 | return -1; // nope...bail |
| 201 | } |
| 202 | |
| 203 | xmin = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 204 | ymin = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 205 | xmax = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 206 | ymax = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 207 | |
| 208 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).(temp, 116, infile); |
| 209 | |
| 210 | #define PCXHEADER_OFFSETcxx . . $ `bitmap/pcx.cpp:210:9`! 12 |
| 211 | |
| 212 | if (temp[65 - PCXHEADER_OFFSETcxx . . $ `bitmap/pcx.cpp:210:9`!] != 3) { |
| 213 | // Must have 3 planes |
| 214 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PCXLoad: PCX Not 3 Planes for 24bit encoding\n"); |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | width = 1 + xmax - xmin; |
| 219 | height = 1 + ymax - ymin; |
| 220 | |
| 221 | // Determine BytesPerLine |
| 222 | cfseekcxx . . $ cfseek(f9992d249f032805).(infile, 66, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 223 | int BytesPerLine = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 224 | cfseekcxx . . $ cfseek(f9992d249f032805).(infile, 128, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 225 | |
| 226 | // scanline length |
| 227 | total = 3 * BytesPerLine; |
| 228 | |
| 229 | uint8_tcxx . . $ uint8_t# *rawdata = (uint8_tcxx . . $ uint8_t# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(total * height); |
| 230 | if (!rawdata) { |
| 231 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "PCXLoad: Out of memory\n"); |
| 232 | return -1; // no memory! |
| 233 | } |
| 234 | |
| 235 | // Load in the data |
| 236 | // the data is divided into scanlines |
| 237 | // the first scanline is line 0's red |
| 238 | // the second scanline is line 0's green |
| 239 | // the third scanline is line 0's blue |
| 240 | // the fourth scanline is line 1's red |
| 241 | // etc. |
| 242 | int data_index = 0; |
| 243 | for (int line = 0; line < height; line++) { |
| 244 | // Read red scanline |
| 245 | run = 0; |
| 246 | while (run < BytesPerLine) { |
| 247 | buf = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 248 | if (buf >= 192) { |
| 249 | uint8_tcxx . . $ uint8_t# tb; |
| 250 | tb = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 251 | for (i = 0; i < (buf - 192); i++, run++, data_index++) |
| 252 | rawdata[data_index] = tb; |
| 253 | } else { |
| 254 | rawdata[data_index] = buf; |
| 255 | run++; |
| 256 | data_index++; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // Read green scanline |
| 261 | run = 0; |
| 262 | while (run < BytesPerLine) { |
| 263 | buf = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 264 | if (buf >= 192) { |
| 265 | uint8_tcxx . . $ uint8_t# tb; |
| 266 | tb = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 267 | for (i = 0; i < (buf - 192); i++, run++, data_index++) |
| 268 | rawdata[data_index] = tb; |
| 269 | } else { |
| 270 | rawdata[data_index] = buf; |
| 271 | run++; |
| 272 | data_index++; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Read blue scanline |
| 277 | run = 0; |
| 278 | while (run < BytesPerLine) { |
| 279 | buf = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 280 | if (buf >= 192) { |
| 281 | uint8_tcxx . . $ uint8_t# tb; |
| 282 | tb = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 283 | for (i = 0; i < (buf - 192); i++, run++, data_index++) |
| 284 | rawdata[data_index] = tb; |
| 285 | } else { |
| 286 | rawdata[data_index] = buf; |
| 287 | run++; |
| 288 | data_index++; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | src_bm = bm_AllocBitmapcxx . . $ bm_AllocBitmap(2326284602b0bb2d).(width, height, 0); |
| 294 | if (src_bm < 0) |
| 295 | return -1; // probably out of memory |
| 296 | |
| 297 | uint16_tcxx . . $ uint16_t# *data = bm_datacxx . . $ bm_data(501e1bc9f482c6bd).(src_bm, 0); |
| 298 | |
| 299 | for (i = 0; i < height; i++) { |
| 300 | for (t = 0; t < width; t++) { |
| 301 | int r, g, b; |
| 302 | uint16_tcxx . . $ uint16_t# pixel; |
| 303 | |
| 304 | r = rawdata[(i * total) + (0 * BytesPerLine) + t]; |
| 305 | g = rawdata[(i * total) + (1 * BytesPerLine) + t]; |
| 306 | b = rawdata[(i * total) + (2 * BytesPerLine) + t]; |
| 307 | |
| 308 | pixel = OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`! | GR_RGB16cxx . . $ GR_RGB16(6461fafb79bad5b9).(r, g, b); |
| 309 | |
| 310 | data[i * width + t] = pixel; |
| 311 | } |
| 312 | } |
| 313 | mem_freecxx . . $ `mem/mem.h:80:9`!(rawdata); |
| 314 | |
| 315 | return src_bm; // success! |
| 316 | } |