File: pcx.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (316 lines) | stat: -rw-r--r-- 7,502 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Descent 3 
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.

--- HISTORICAL COMMENTS FOLLOW ---

 * $Logfile: /DescentIII/Main/bitmap/pcx.cpp $
 * $Revision: 8 $
 * $Date: 3/30/99 12:36a $
 * $Author: Jeff $
 *
 * Code to read PCX files
 *
 * $Log: /DescentIII/Main/bitmap/pcx.cpp $
 *
 * 8     3/30/99 12:36a Jeff
 * added support for 24bit pcx's
 *
 * 7     2/16/99 5:50p Jason
 * made pcx stuff only work with 8 bit files
 *
 * 6     10/20/98 1:41a Jeff
 * fixed a mem_malloc/free bug
 *
 * 5     10/08/98 4:23p Kevin
 * Changed code to comply with memory library usage. Always use mem_malloc
 * , mem_free and mem_strdup
 *
 * 4     4/23/98 6:38p Jason
 * made bitmaps use 1555 format
 *
 * 3     12/22/97 7:34p Samir
 * Removed instances of gr.h include.  Replaced with grdefs.h
 *
 * 2     10/15/97 5:20p Jason
 * did a HUGE overhaul of the bitmap system
 *
 * 4     3/03/97 6:20p Matt
 * Changed cfile functions to use D3 naming convention
 *
 * $NoKeywords: $
 */

#include "mem.h"
#include "bitmap.h"
#include "pserror.h"
#include "pstypes.h"
#include "grdefs.h"
#include <stdlib.h>

// load an 8bit pcx image
static int bm_pcx_8bit_alloc_file(CFILE *infile);
// load a 24bit pcx image
static int bm_pcx_24bit_alloc_file(CFILE *infile);

// Loads a pcx file and converts it to 16 bit.  Returns bitmap handle or -1 on error
int bm_pcx_alloc_file(CFILE *infile) {
  uint8_t temp[128];

  cf_ReadBytes(temp, 128, infile);
  cfseek(infile, 0, SEEK_SET);

  switch (temp[65]) {
  case 1:
    // one plane, sounds like an 8bit pcx
    return bm_pcx_8bit_alloc_file(infile);
    break;
  case 3:
    // three planes, sounds like a 24bit pcx
    return bm_pcx_24bit_alloc_file(infile);
    break;
  }

  return -1; // Must be 8 bit only
}

// load an 8bit pcx image
int bm_pcx_8bit_alloc_file(CFILE *infile) {
  int xmin, ymin, xmax, ymax;
  int width, height;
  int total, run = 0;
  int i, t;
  int src_bm;
  uint8_t pred[256], pgreen[256], pblue[256];
  uint8_t buf;
  uint8_t temp[128];

  cf_ReadBytes(temp, 4, infile);

  if (temp[3] != 8)
    return -1; // nope...bail

  xmin = cf_ReadShort(infile);
  ymin = cf_ReadShort(infile);
  xmax = cf_ReadShort(infile);
  ymax = cf_ReadShort(infile);

  cf_ReadBytes(temp, 116, infile);

  if (temp[65 - 12] != 1)
    return -1; // Must be 8 bit only

  width = 1 + xmax - xmin;
  height = 1 + ymax - ymin;
  total = width * height;

  uint8_t *rawdata = (uint8_t *)mem_malloc(width * height);
  if (!rawdata)
    return -1; // no memory!

  run = 0;
  while (run < total) {
    buf = cf_ReadByte(infile);
    if (buf >= 192) {
      uint8_t tb;
      tb = cf_ReadByte(infile);
      for (i = 0; i < (buf - 192); i++, run++)
        rawdata[run] = tb;
    } else {
      rawdata[run] = buf;
      run++;
    }
  }

  cf_ReadByte(infile); // ignore pad

  // Read palette

  for (i = 0; i < 256; i++) {
    pred[i] = cf_ReadByte(infile);
    pgreen[i] = cf_ReadByte(infile);
    pblue[i] = cf_ReadByte(infile);

    pred[i] >>= 3;
    pgreen[i] >>= 3;
    pblue[i] >>= 3;
  }

  src_bm = bm_AllocBitmap(width, height, 0);
  if (src_bm < 0)
    return -1; // probably out of memory

  uint16_t *data = bm_data(src_bm, 0);

  for (i = 0; i < height; i++) {
    for (t = 0; t < width; t++) {
      uint16_t pixel;
      uint8_t c = rawdata[i * width + t];

      int r = pred[c];
      int g = pgreen[c];
      int b = pblue[c];

      pixel = OPAQUE_FLAG | (r << 10) | (g << 5) | b;
      if (c == 0)
        pixel = NEW_TRANSPARENT_COLOR;

      data[i * width + t] = pixel;
    }
  }
  mem_free(rawdata);

  return src_bm; // success!
}

// load a 24bit pcx image
int bm_pcx_24bit_alloc_file(CFILE *infile) {
  int xmin, ymin, xmax, ymax;
  int width, height;
  int total, run = 0;
  int i, t;
  int src_bm;
  uint8_t buf;
  uint8_t temp[128];

  cf_ReadBytes(temp, 4, infile);

  if (temp[1] < 5) {
    // need at least version 5.0f
    mprintf(0, "PCXLoad: PCX Not version 5.0 or greater\n");
    return -1;
  }

  if (temp[3] != 8) {
    // need 8 bits per pixel
    mprintf(0, "PCXLoad: PCX Not 8 bpp\n");
    return -1; // nope...bail
  }

  xmin = cf_ReadShort(infile);
  ymin = cf_ReadShort(infile);
  xmax = cf_ReadShort(infile);
  ymax = cf_ReadShort(infile);

  cf_ReadBytes(temp, 116, infile);

#define PCXHEADER_OFFSET 12

  if (temp[65 - PCXHEADER_OFFSET] != 3) {
    // Must have 3 planes
    mprintf(0, "PCXLoad: PCX Not 3 Planes for 24bit encoding\n");
    return -1;
  }

  width = 1 + xmax - xmin;
  height = 1 + ymax - ymin;

  // Determine BytesPerLine
  cfseek(infile, 66, SEEK_SET);
  int BytesPerLine = cf_ReadShort(infile);
  cfseek(infile, 128, SEEK_SET);

  // scanline length
  total = 3 * BytesPerLine;

  uint8_t *rawdata = (uint8_t *)mem_malloc(total * height);
  if (!rawdata) {
    mprintf(0, "PCXLoad: Out of memory\n");
    return -1; // no memory!
  }

  // Load in the data
  // the data is divided into scanlines
  // the first scanline is line 0's red
  // the second scanline is line 0's green
  // the third scanline is line 0's blue
  // the fourth scanline is line 1's red
  // etc.
  int data_index = 0;
  for (int line = 0; line < height; line++) {
    // Read red scanline
    run = 0;
    while (run < BytesPerLine) {
      buf = cf_ReadByte(infile);
      if (buf >= 192) {
        uint8_t tb;
        tb = cf_ReadByte(infile);
        for (i = 0; i < (buf - 192); i++, run++, data_index++)
          rawdata[data_index] = tb;
      } else {
        rawdata[data_index] = buf;
        run++;
        data_index++;
      }
    }

    // Read green scanline
    run = 0;
    while (run < BytesPerLine) {
      buf = cf_ReadByte(infile);
      if (buf >= 192) {
        uint8_t tb;
        tb = cf_ReadByte(infile);
        for (i = 0; i < (buf - 192); i++, run++, data_index++)
          rawdata[data_index] = tb;
      } else {
        rawdata[data_index] = buf;
        run++;
        data_index++;
      }
    }

    // Read blue scanline
    run = 0;
    while (run < BytesPerLine) {
      buf = cf_ReadByte(infile);
      if (buf >= 192) {
        uint8_t tb;
        tb = cf_ReadByte(infile);
        for (i = 0; i < (buf - 192); i++, run++, data_index++)
          rawdata[data_index] = tb;
      } else {
        rawdata[data_index] = buf;
        run++;
        data_index++;
      }
    }
  }

  src_bm = bm_AllocBitmap(width, height, 0);
  if (src_bm < 0)
    return -1; // probably out of memory

  uint16_t *data = bm_data(src_bm, 0);

  for (i = 0; i < height; i++) {
    for (t = 0; t < width; t++) {
      int r, g, b;
      uint16_t pixel;

      r = rawdata[(i * total) + (0 * BytesPerLine) + t];
      g = rawdata[(i * total) + (1 * BytesPerLine) + t];
      b = rawdata[(i * total) + (2 * BytesPerLine) + t];

      pixel = OPAQUE_FLAG | GR_RGB16(r, g, b);

      data[i * width + t] = pixel;
    }
  }
  mem_free(rawdata);

  return src_bm; // success!
}