-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcx.c
190 lines (157 loc) · 3.84 KB
/
pcx.c
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
/*
PCX
Mikolaj Felix 13/07/01
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pcx.h"
char error_message[128];
typedef struct tPCXHEAD
{
unsigned char Manufacter; // 0x0a
unsigned char Version;
unsigned char Encoding; // 0x01
unsigned char BitsPerPixel;
unsigned short XMin;
unsigned short YMin;
unsigned short XMax;
unsigned short YMax;
unsigned short HRes;
unsigned short VRes;
unsigned char Palette[48];
unsigned char Reserved;
unsigned char NumOfPlanes;
unsigned short BytesPerLine;
unsigned short PaletteType;
unsigned short HSize;
unsigned short VSize;
unsigned char Filler[54];
} PCXHEAD;
int load_pcx(PCX* pcx, char* filename, int load_palette)
{
FILE* p;
unsigned i;
int j;
unsigned char b;
if ((p = fopen(filename, "r+b")) == NULL) {
strcpy(error_message, "Unable to open file!");
return 0;
}
if (fgetc(p) != 0x0a) {
fclose(p);
strcpy(error_message, "Incorrect file format!");
return 0;
}
fseek(p, 2, SEEK_CUR);
if (fgetc(p) != 8) {
fclose(p);
strcpy(error_message, "Incorrect color depth. Must by 8-bit!");
return 0;
}
fread(&i, 2, 1, p);
pcx->width = i;
fread(&i, 2, 1, p);
pcx->height = i;
fread(&i, 2, 1, p);
pcx->width = i - pcx->width + 1;
fread(&i, 2, 1, p);
pcx->height = i - pcx->height + 1;
if (pcx->width > 320 || pcx->height > 200) {
fclose(p);
strcpy(error_message, "Image is too big!");
return 0;
}
if ((pcx->data = (unsigned char*)malloc(pcx->width * pcx->height)) == NULL) {
fclose(p);
strcpy(error_message, "Not enough memory!");
return 0;
}
fseek(p, 128, SEEK_SET);
i = 0;
while (i != (pcx->width * pcx->height)) {
b = fgetc(p);
if (b >= 192) {
j = b - 192;
b = fgetc(p);
while (j-- > 0)
pcx->data[i++] = b;
} else
pcx->data[i++] = b;
}
if (load_palette) {
fseek(p, -768L, SEEK_END);
for (i = 0; i < 768; i++)
pcx->palette[i] = fgetc(p) >> 2;
}
fclose(p);
return 1;
}
void unload_pcx(PCX* pcx)
{
free(pcx->data);
}
int save_pcx(char* FileName, unsigned char* Buffer, unsigned char* Palette,
unsigned int Width, unsigned int Height)
{
PCXHEAD Head;
FILE* FPtr;
unsigned char c, cc, n_found;
unsigned short i, j, FileSize, ImgSize = Width * Height;
memset(&Head, 0, sizeof(PCXHEAD));
Head.Manufacter = 0x0a;
Head.Version = 5;
Head.Encoding = 1;
Head.BitsPerPixel = 8;
Head.XMin = 0;
Head.YMin = 0;
Head.XMax = Width - 1;
Head.YMax = Height - 1;
Head.HRes = Width;
Head.VRes = Height;
Head.NumOfPlanes = 1;
Head.BytesPerLine = Width;
Head.PaletteType = 1;
Head.HSize = Width;
Head.VSize = Height;
FPtr = fopen(FileName, "w+b");
if (!FPtr)
return 0;
fwrite(&Head, sizeof(PCXHEAD), 1, FPtr);
i = 0;
while (i != ImgSize) {
c = Buffer[i];
if (c >= 192) {
cc = 192 | 1;
fputc(cc, FPtr);
fputc(c, FPtr);
i++;
continue;
}
n_found = 0;
for (j = 1; j < 63; j++) {
if ((i + j) == ImgSize)
break;
if (Buffer[i + j] == c)
n_found++;
else
break;
}
if (n_found) {
cc = 192 | n_found;
fputc(cc, FPtr);
fputc(c, FPtr);
i += n_found;
} else {
fputc(c, FPtr);
i++;
}
}
for (i = 0; i < 768; i++) {
fputc(Palette[i] << 2, FPtr);
}
FileSize = ftell(FPtr);
fclose(FPtr);
return FileSize;
}