-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcx.cpp
executable file
·175 lines (144 loc) · 3.48 KB
/
pcx.cpp
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
/***
pcx.cpp
Support for PC Paintbrush .PCX image files
A practically obsolete file format nowadays, but they are occasionally encountered.
Copyright (c) 1997-2022 Chris M. Street.
***/
#include "libcodehappy.h"
/***
This is ported to C from BMP2PCX.EX and PCX.EX, an implementation I wrote in Euphoria in 1997. -- CMS
***/
/***
Save the image bmp as a .PCX named filename.
***/
int save_pcx(SBitmap* bmp, const char* filename) {
RamFile rf;
const unsigned char hdr1[] =
{ 10, 5, 1, 8, 0, 0, 0, 0 }; /* We only write Paintbrush 3.0+ format PCX files. */
const unsigned char hdr2[] = {
64, 1, 200, 0, 0, 0, 0, 216, 152, 56, 120, 116, 4, 112, 108, 4, 236,
172, 76, 248, 196, 128, 64, 36, 36, 36, 40, 20, 248,
188, 104, 212, 144, 156, 60, 36, 36, 116, 112, 8,
120, 116, 8, 124, 120, 8, 52, 48, 4, 240, 196, 136,
0, 1, 64, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
int e, f;
/*** image must be palettized first. ***/
if (bmp->type() != BITMAP_PALETTE)
return(1);
rf.open(filename, RAMFILE_DEFAULT);
rf.putmem((char *)hdr1, sizeof(hdr1));
rf.putc(bmp->width() & 0xff);
rf.putc((bmp->width() >> 8) & 0xff);
rf.putc(bmp->height() & 0xff);
rf.putc((bmp->height() >> 8) & 0xff);
rf.putmem((char *)hdr2, sizeof(hdr2));
// RLE the image
for (e = 0; e < bmp->height(); ++e) {
int t;
t = 0;
while (t < bmp->width()) {
int i;
u32 c;
i = 0;
c = (u32)bmp->get_pixel_palette(t, e);
while (t + i + 1 < bmp->width() &&
c == bmp->get_pixel_palette(t + i + 1, e) && i <= 62)
++i;
if (i > 0) {
rf.putc(i | 192);
rf.putc(c);
t += i;
} else {
if ((c & 192) > 0)
rf.putc(193);
rf.putc(c);
++t;
}
}
}
// write the image palette
rf.putc(12);
for (e = 0; e < bmp->palette()->ncolors; ++e) {
rf.putc(RGB_RED(bmp->palette()->clrs[e]));
rf.putc(RGB_GREEN(bmp->palette()->clrs[e]));
rf.putc(RGB_BLUE(bmp->palette()->clrs[e]));
}
for (; e < 256; ++e) {
rf.putc(0);
rf.putc(0);
rf.putc(0);
}
rf.close();
return(0);
}
/***
Load the named .PCX file into an image.
Currently only supports 256-color files.
***/
SBitmap* load_pcx(const char* filename) {
SBitmap* bmp;
RamFile rf;
u32 flen;
u32 xdim, ydim;
u32 x, y;
int c;
int p;
rf.open(filename, RAMFILE_READONLY);
flen = rf.length();
rf.seek(8);
xdim = rf.getc();
xdim += (rf.getc() << 8);
ydim = rf.getc();
ydim += (rf.getc() << 8);
/* create a palettized (8-bit) xdim by ydim bitmap */
bmp = new SBitmap(xdim, ydim, BITMAP_PALETTE);
/* read in the palette */
rf.seek_from_end(768);
for (p = 0; p < 256; ++p) {
u32 r, g, b;
r = rf.getc();
g = rf.getc();
b = rf.getc();
bmp->palette()->clrs[p] = RGB(r, g, b);
}
/* read the PCX image data */
rf.seek(128);
x = 0UL;
y = 0UL;
while (y < ydim) {
c = rf.getc();
if (c < 0) {
delete bmp;
return(NULL);
}
if (c > 192) {
int interim = c - 192;
c = rf.getc();
for (p = interim; p > 0; --p) {
if (x >= xdim) {
x = 0UL;
++y;
}
bmp->put_pixel_palette(x, y, c);
++x;
}
} else {
if (x >= xdim) {
x = 0UL;
++y;
}
bmp->put_pixel_palette(x, y, c);
++x;
}
}
rf.close();
return(bmp);
}
/*** end pcx.cpp ***/