/
writebgf.c
324 lines (275 loc) · 8.42 KB
/
writebgf.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
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
317
318
319
320
321
322
323
// Meridian 59, Copyright 1994-2012 Andrew Kirmse and Chris Kirmse.
// All rights reserved.
//
// This software is distributed under a license that is described in
// the LICENSE file that accompanies it.
//
// Meridian is a registered trademark.
/*
* writebgf.c: Routines for writing out a bgf file.
*/
#include "makebgf.h"
#define VERSION 9
static BYTE magic[] = {0x42, 0x47, 0x46, 0x11};
typedef struct
{
HANDLE fh; // Handle of file
HANDLE mapfh; // Handle of mapping object
int length; // Length of file
char *mem; // Start of file
char *ptr; // Current position in file
} file_node;
static int MappedFileRead(file_node *f, void *buf, int num);
static int MappedFileWrite(file_node *f, void *buf, int num);
static Bool MappedFileOpenWrite(char *filename, file_node *f, int length);
static Bool MappedFileGoto(file_node *f, int pos);
static void MappedFileClose(file_node *f);
static int EstimateBGFFileSize(Bitmaps *b);
static BOOL WriteBitmap(file_node *f, PDIB pdib, Options *options);
/**************************************************************************/
/*
* WriteBGFFile: Write Bitmaps and Options structures out to given filename.
* Return FALSE on error.
*/
BOOL WriteBGFFile(Bitmaps *b, Options *opts, char *filename)
{
int i, j, temp, max_indices, len;
Group *g;
file_node f;
len = EstimateBGFFileSize(b);
if (!MappedFileOpenWrite(filename, &f, len))
return FALSE;
// Write magic number
for (i=0; i < 4; i++)
if (MappedFileWrite(&f, &magic[i], 1) < 0) return FALSE;
// Write version
temp = VERSION;
if (MappedFileWrite(&f, &temp, 4) < 0) return FALSE;
// Write bitmap name
if (MappedFileWrite(&f, &b->name, MAX_BITMAPNAME) < 0) return FALSE;
// Write # of bitmaps
if (MappedFileWrite(&f, &b->num_bitmaps, 4) < 0) return FALSE;
// Write # of index groups
if (MappedFileWrite(&f, &b->num_groups, 4) < 0) return FALSE;
// Find most indices in a group
max_indices = 0;
for (i=0; i < b->num_groups; i++)
if (b->groups[i].num_indices > max_indices)
max_indices = b->groups[i].num_indices;
if (MappedFileWrite(&f, &max_indices, 4) < 0) return FALSE;
// Write shrink factor
if (MappedFileWrite(&f, &opts->shrink, 4) < 0) return FALSE;
// Write out bitmaps
for (i=0; i < b->num_bitmaps; i++)
{
PDIB pdib = b->bitmaps[i];
// Write out bitmap size (swap width and height if rotated)
if (opts->rotate)
{
temp = DibHeight(pdib);
if (MappedFileWrite(&f, &temp, 4) < 0) return FALSE;
temp = DibWidth(pdib);
if (MappedFileWrite(&f, &temp, 4) < 0) return FALSE;
}
else
{
temp = DibWidth(pdib);
if (MappedFileWrite(&f, &temp, 4) < 0) return FALSE;
temp = DibHeight(pdib);
if (MappedFileWrite(&f, &temp, 4) < 0) return FALSE;
}
// Write out x and y offsets
temp = b->offsets[i].x;
if (MappedFileWrite(&f, &temp, 4) < 0) return FALSE;
temp = b->offsets[i].y;
if (MappedFileWrite(&f, &temp, 4) < 0) return FALSE;
// Write out hotspots
if (MappedFileWrite(&f, &b->hotspots[i].num_hotspots, 1) < 0) return FALSE;
for (j=0; j < b->hotspots[i].num_hotspots; j++)
{
if (MappedFileWrite(&f, &b->hotspots[i].numbers[j], 1) < 0) return FALSE;
if (MappedFileWrite(&f, &b->hotspots[i].positions[j].x, 4) < 0) return FALSE;
if (MappedFileWrite(&f, &b->hotspots[i].positions[j].y, 4) < 0) return FALSE;
}
// Write out the bytes of the bitmap
if (!WriteBitmap(&f, pdib, opts)) return FALSE;
}
// Write out indices
for (i=0; i < b->num_groups; i++)
{
g = &b->groups[i];
if (MappedFileWrite(&f, &g->num_indices, 4) < 0) return FALSE;
for (j=0; j < g->num_indices; j++)
if (MappedFileWrite(&f, &g->indices[j], 4) < 0) return FALSE;
}
UnmapViewOfFile(f.mem);
CloseHandle(f.mapfh);
SetFilePointer(f.fh, (f.ptr - f.mem), NULL, FILE_BEGIN);
SetEndOfFile(f.fh);
CloseHandle(f.fh);
return TRUE;
}
/**************************************************************************/
/*
* WriteBitmap: Write bytes of given PDIB out to given file.
* Return FALSE on error.
*/
BOOL WriteBitmap(file_node *f, PDIB pdib, Options *options)
{
int width, height, row, col, len, temp;
BYTE *bits;
BYTE *buf, *bufptr, byte;
width = DibWidth(pdib);
height = DibHeight(pdib);
buf = (BYTE *) malloc(width * height);
bufptr = buf;
if (options->rotate)
{
for (col=0; col < width; col++)
{
bits = (BYTE *) DibPtr(pdib) + col;
for (row = 0; row < height; row++)
{
*bufptr++ = *bits;
bits += DibWidthBytes(pdib);
}
}
}
else
{
for (row=0; row < height; row++)
{
// Watch out--rows are 4-bytes aligned
bits = (BYTE *) DibPtr(pdib) + row * DibWidthBytes(pdib);
memcpy(bufptr, bits, width);
bufptr += width;
}
}
// Leave space for # of bytes
if (options->compress)
{
len = WrapCompress((char *) buf, f->ptr + 5, width * height);
if (len > 0)
{
byte = 1;
// Save compressed length
MappedFileWrite(f, &byte, 1);
MappedFileWrite(f, &len, 4);
f->ptr += len;
}
}
else len = -1;
if (len < 0)
{
byte = 0;
MappedFileWrite(f, &byte, 1);
temp = 0;
MappedFileWrite(f, &temp, 4);
// Buffer is incompressible; just write out bits themselves
MappedFileWrite(f, buf, width * height);
}
free(buf);
return True;
}
/***************************************************************************/
/*
* EstimateBGFFileSize: Return an upper bound on the size of the BGF file
* that will result from saving the given bitmaps.
*/
int EstimateBGFFileSize(Bitmaps *b)
{
Group *g;
int len = 0, i;
len += 56; // header stuff
// Space for hotspots and bitmaps
for (i=0; i < b->num_bitmaps; i++)
{
PDIB pdib = b->bitmaps[i];
len += 17 + 9 * b->hotspots[i].num_hotspots;
len += DibHeight(pdib) * DibWidth(pdib);
}
// Write out indices
for (i=0; i < b->num_groups; i++)
{
g = &b->groups[i];
len += 4 + 4 * g->num_indices;
}
len += 100; // just to be safe
return len;
}
/***************************************************************************/
/*
* MappedFileOpenWrite: Open filename as a memory-mapped file for writing,
* and map a view of the entire file. Returns True on success, and fills in f.
*/
Bool MappedFileOpenWrite(char *filename, file_node *f, int length)
{
f->fh = CreateFile(filename,GENERIC_READ | GENERIC_WRITE,0,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,NULL);
if (f->fh == INVALID_HANDLE_VALUE)
{
return False;
}
f->length = length;
f->mapfh = CreateFileMapping(f->fh,NULL,PAGE_READWRITE,0,f->length,NULL);
if (f->mapfh == NULL)
{
CloseHandle(f->fh);
return False;
}
f->mem = (char *) MapViewOfFile(f->mapfh,FILE_MAP_WRITE,0,0,0);
if (f->mem == NULL)
{
CloseHandle(f->mapfh);
CloseHandle(f->fh);
return False;
}
f->ptr = f->mem;
return True;
}
/***************************************************************************/
/*
* MappedFileRead: Copy num bytes from the memory-mapped file to buf.
* Increments the file pointer.
* Returns num on success, or -1 on failure.
*/
int MappedFileRead(file_node *f, void *buf, int num)
{
if ((f->ptr - f->mem) + num > f->length || f->ptr < f->mem || num <= 0)
return -1;
memcpy(buf, f->ptr, num);
f->ptr += num;
return num;
}
/***************************************************************************/
/*
* MappedFileGoto: Set file pointer of f to given position.
* Return True iff position is legal.
*/
Bool MappedFileGoto(file_node *f, int pos)
{
if (pos < 0 || pos > f->length)
return False;
f->ptr = f->mem + pos;
return True;
}
/***************************************************************************/
void MappedFileClose(file_node *f)
{
UnmapViewOfFile(f->mem);
CloseHandle(f->mapfh);
CloseHandle(f->fh);
}
/***************************************************************************/
/*
* MappedFileWrite: Write num bytes from buf to f, and increment the file pointer.
* Return num on success, -1 on failure.
*/
int MappedFileWrite(file_node *f, void *buf, int num)
{
if ((f->ptr - f->mem) + num > f->length || num <= 0)
return -1;
memcpy(f->ptr, buf, num);
f->ptr += num;
return num;
}