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
|
/******************************************************************************
Plush Version 1.2
read_pcx.c
PCX Texture Reader
Copyright (c) 1996-2000, Justin Frankel
******************************************************************************/
#include "plush.h"
static pl_uInt _plHiBit(pl_uInt16);
static pl_uInt _plOptimizeImage(pl_uChar *, pl_uChar *, pl_uInt32);
static pl_sInt _plReadPCX(char *filename, pl_uInt16 *width, pl_uInt16 *height,
pl_uChar **pal, pl_uChar **data);
static void _plRescaleImage(pl_uChar *in, pl_uChar *out, pl_uInt inx,
pl_uInt iny, pl_uInt outx, pl_uInt outy);
pl_Texture *plReadPCXTex(char *fn, pl_Bool rescale, pl_Bool optimize) {
pl_uChar *data, *pal;
pl_uInt16 x, y;
pl_Texture *t;
if (_plReadPCX(fn,&x,&y,&pal,&data) < 0) return 0;
t = (pl_Texture *) malloc(sizeof(pl_Texture));
if (!t) return 0;
t->Width = _plHiBit(x);
t->Height = _plHiBit(y);
if (rescale && (1 << t->Width != x || 1 << t->Height != y)) {
pl_uChar nx, ny, *newdata;
nx = t->Width;
if ((1 << t->Width) != x) nx++;
ny = t->Height;
if ((1 << t->Height) != y) ny++;
newdata = (pl_uChar *) malloc((1<<nx)*(1<<ny));
if (!newdata) {
free(t);
free(data);
free(pal);
return 0;
}
_plRescaleImage(data,newdata,x,y,1<<nx,1<<ny);
free(data);
data = newdata;
t->Width = nx;
t->Height = ny;
x = 1<<nx; y = 1<<ny;
}
t->iWidth = x;
t->iHeight = y;
t->uScale = (pl_Float) (1<<t->Width);
t->vScale = (pl_Float) (1<<t->Height);
if (optimize) t->NumColors = _plOptimizeImage(pal, data,x*y);
else t->NumColors = 256;
t->Data = data;
t->PaletteData = pal;
return t;
}
static pl_uInt _plHiBit(pl_uInt16 x) {
pl_uInt i = 16, mask = 1<<15;
while (mask) {
if (x & mask) return i;
mask >>= 1; i--;
}
return 0;
}
static pl_uInt _plOptimizeImage(pl_uChar *pal, pl_uChar *data, pl_uInt32 len) {
pl_uChar colors[256], *dd = data;
pl_uChar remap[256];
pl_sInt32 lastused, firstunused;
pl_uInt32 x;
memset(colors,0,256);
for (x = 0; x < len; x ++) colors[(pl_uInt) *dd++] = 1;
lastused = -1;
for (x = 0; x < 256; x ++) remap[x] = (pl_uChar)x;
lastused = 255;
firstunused = 0;
for (;;) {
while (firstunused < 256 && colors[firstunused]) firstunused++;
if (firstunused > 255) break;
while (lastused >= 0 && !colors[lastused]) lastused--;
if (lastused < 0) break;
if (lastused <= firstunused) break;
pal[firstunused*3] = pal[lastused*3];
pal[firstunused*3+1] = pal[lastused*3+1];
pal[firstunused*3+2] = pal[lastused*3+2];
colors[lastused] = 0;
colors[firstunused] = 1;
remap[lastused] = (pl_uChar) firstunused;
}
x = len;
while (x--) *data++ = remap[(pl_uInt) *data];
return (lastused+1);
}
static pl_sInt _plReadPCX(char *filename, pl_uInt16 *width, pl_uInt16 *height,
pl_uChar **pal, pl_uChar **data) {
pl_uInt16 sx, sy, ex, ey;
FILE *fp = fopen(filename,"rb");
pl_uChar *data2;
if (!fp) return -1;
fgetc(fp);
if (fgetc(fp) != 5) { fclose(fp); return -2; }
if (fgetc(fp) != 1) { fclose(fp); return -2; }
if (fgetc(fp) != 8) { fclose(fp); return -3; }
sx = fgetc(fp); sx |= fgetc(fp)<<8;
sy = fgetc(fp); sy |= fgetc(fp)<<8;
ex = fgetc(fp); ex |= fgetc(fp)<<8;
ey = fgetc(fp); ey |= fgetc(fp)<<8;
*width = ex - sx + 1;
*height = ey - sy + 1;
fseek(fp,128,SEEK_SET);
if (feof(fp)) { fclose(fp); return -4; }
*data = (pl_uChar *) malloc((*width) * (*height));
if (!*data) { fclose(fp); return -128; }
sx = *height;
data2 = *data;
do {
int xpos = 0;
do {
char c = fgetc(fp);
if ((c & 192) == 192) {
char oc = fgetc(fp);
c &= ~192;
do {
*(data2++) = oc;
xpos++;
} while (--c && xpos < *width);
} else {
*(data2++) = c;
xpos++;
}
} while (xpos < *width);
} while (--sx);
if (feof(fp)) { fclose(fp); free(*data); return -5; }
fseek(fp,-769,SEEK_END);
if (fgetc(fp) != 12) { fclose(fp); free(*data); return -6; }
*pal = (pl_uChar *) malloc(768);
if (!*pal) { fclose(fp); free(*data); return -7; }
fread(*pal,3,256,fp);
fclose(fp);
return 0;
}
static void _plRescaleImage(pl_uChar *in, pl_uChar *out, pl_uInt inx,
pl_uInt iny, pl_uInt outx, pl_uInt outy) {
pl_uInt x;
pl_uInt32 X, dX, dY, Y;
dX = (inx<<16) / outx;
dY = (iny<<16) / outy;
Y = 0;
do {
pl_uChar *ptr = in + inx*(Y>>16);
X = 0;
Y += dY;
x = outx;
do {
*out++ = ptr[X>>16];
X += dX;
} while (--x);
} while (--outy);
}
|