-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput-dr2d.c
685 lines (545 loc) · 18 KB
/
output-dr2d.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/* output-dr2d.c: output in DR2D format
Copyright (C) 2002 Andrew Elia
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* Def: HAVE_CONFIG_H */
#include <stdlib.h>
#include <string.h>
#include "spline.h"
#include "color.h"
#include "output-dr2d.h"
/* Globals: Values are set by output_dr2d_writer() */
float XFactor;
float YFactor;
float LineThickness;
#define FIXOFFS 10
#define LF_ACTIVE 0x01
#define LF_DISPLAYED 0x02
#define FT_NONE 0
#define FT_COLOR 1
#define JT_NONE 0
#define JT_MITER 1
#define JT_BEVEL 2
#define JT_ROUND 3
#define INDICATOR 0xFFFFFFFF
#define IND_SPLINE 0x00000001
#define IND_MOVETO 0x00000002
struct Chunk {
unsigned char ID[4];
unsigned int Size;
unsigned char * Data;
};
static struct Chunk * BuildDRHD(int, int, int, int);
static struct Chunk * BuildPPRF(char *, int, char *, float);
static struct Chunk * BuildCMAP(spline_list_array_type);
static struct Chunk * BuildLAYR(void);
static struct Chunk * BuildDASH(void);
static struct Chunk * BuildBBOX(spline_list_type, int);
static struct Chunk * BuildATTR(at_color_type, int, struct Chunk *);
static int GetCMAPEntry(at_color_type, struct Chunk *);
static int CountSplines(spline_list_type);
static int SizeFloat(float, char *);
static void ShortAsBytes(int, unsigned char *);
static void IntAsBytes(int, unsigned char *);
static void FloatAsIEEEBytes(float, unsigned char *);
/* static void ieee2flt(long *, float *); */
static void flt2ieee(float *, unsigned char *);
static void FreeChunk(struct Chunk *);
static void FreeChunks(struct Chunk **, int);
static int TotalSizeChunks(struct Chunk **, int);
static int SizeChunk(struct Chunk *);
static void PushPolyPoint(unsigned char *, int *, float, float);
static void PushPolyIndicator(unsigned char *, int *, unsigned int);
static struct Chunk ** GeneratexPLY(struct Chunk *, spline_list_array_type, int);
static struct Chunk * BuildCMAP(spline_list_array_type shape) {
unsigned this_list;
unsigned this_list_length;
int ListSize, MaxListSize;
int WalkCol, FoundCol;
unsigned char Red, Green, Blue;
unsigned char * CMAP;
unsigned char * IndexCol;
struct Chunk * CMAPChunk;
MaxListSize = SPLINE_LIST_ARRAY_LENGTH(shape);
if ((CMAPChunk = (struct Chunk *) malloc(sizeof(struct Chunk))) == NULL) {
fprintf(stderr, "Insufficient memory to allocate CMAP chunk\n");
return NULL;
}
if ((CMAP = (unsigned char *) malloc(MaxListSize * 3)) == NULL) {
fprintf(stderr, "Insufficient memory to allocate colour map (size %d)\n", MaxListSize);
free(CMAPChunk);
return NULL;
}
ListSize = 0;
this_list_length = SPLINE_LIST_ARRAY_LENGTH(shape);
for (this_list = 0; this_list < this_list_length; this_list++) {
spline_list_type list = SPLINE_LIST_ARRAY_ELT(shape, this_list);
color_type curr_color = curr_color = (list.clockwise && shape.background_color != NULL)? *(shape.background_color) : list.color;
Red = curr_color.r;
Green = curr_color.g;
Blue = curr_color.b;
FoundCol = 0;
for (WalkCol = 0; WalkCol < ListSize; WalkCol++) {
IndexCol = CMAP + (WalkCol * 3);
if ((*IndexCol == Red) && (*(IndexCol + 1) == Green) && (*(IndexCol + 2) == Blue)) {
FoundCol = 1;
break;
}
}
if (FoundCol == 0) {
IndexCol = CMAP + (ListSize * 3);
*IndexCol = Red;
*(IndexCol + 1) = Green;
*(IndexCol + 2) = Blue;
++ListSize;
}
}
strncpy(CMAPChunk->ID, "CMAP", 4);
CMAPChunk->Size = ListSize * 3;
CMAPChunk->Data = CMAP;
return CMAPChunk;
}
static int GetCMAPEntry(at_color_type colour, struct Chunk * CMAPChunk) {
int WalkCol, ListSize;
unsigned char Red, Green, Blue;
unsigned char * IndexCol;
unsigned char * CMAPTable;
ListSize = (CMAPChunk->Size) / 3;
CMAPTable = CMAPChunk->Data;
Red = colour.r;
Green = colour.g;
Blue = colour.b;
for (WalkCol = 0; WalkCol < ListSize; WalkCol++) {
IndexCol = CMAPTable + (WalkCol * 3);
if ((*IndexCol == Red) && (*(IndexCol + 1) == Green) && (*(IndexCol + 2) == Blue)) {
return WalkCol;
}
}
return -1;
}
static struct Chunk * BuildBBOX(spline_list_type list, int height) {
unsigned this_spline;
unsigned this_spline_length;
float x1, y1, x2, y2;
float ex, ey;
struct Chunk * BBOXChunk;
unsigned char * BBOXData;
spline_type s;
if ((BBOXChunk = (struct Chunk *) malloc(sizeof(struct Chunk))) == NULL) {
fprintf(stderr, "Insufficient memory to allocate BBOX chunk\n");
return NULL;
}
if ((BBOXData = (unsigned char *) malloc(16)) == NULL) {
fprintf(stderr, "Insufficient memory to allocate BBOX data\n");
free(BBOXChunk);
return NULL;
}
s = SPLINE_LIST_ELT(list, 0);
x1 = START_POINT(s).x;
y1 = START_POINT(s).y;
x2 = START_POINT(s).x;
y2 = START_POINT(s).y;
this_spline_length = SPLINE_LIST_LENGTH(list);
for (this_spline = 0; this_spline < this_spline_length; this_spline++) {
s = SPLINE_LIST_ELT(list, this_spline);
ex = END_POINT(s).x;
ey = height - END_POINT(s).y;
if (x1 > ex) {
x1 = ex;
}
if (y1 > ey) {
y1 = ey;
}
if (x2 < ex) {
x2 = ex;
}
if (y2 < ey) {
y2 = ey;
}
}
FloatAsIEEEBytes(x1 * XFactor, BBOXData);
FloatAsIEEEBytes(y1 * YFactor, BBOXData + 4);
FloatAsIEEEBytes(x2 * XFactor, BBOXData + 8);
FloatAsIEEEBytes(y2 * YFactor, BBOXData + 12);
strncpy(BBOXChunk->ID, "BBOX", 4);
BBOXChunk->Size = 16;
BBOXChunk->Data = BBOXData;
return BBOXChunk;
}
static struct Chunk * BuildATTR(at_color_type colour, int StrokeOrFill, struct Chunk * CMAPChunk) {
struct Chunk * ATTRChunk;
unsigned char * ATTRData;
int ColourIndex;
if ((ATTRChunk = (struct Chunk *) malloc(sizeof(struct Chunk))) == NULL) {
fprintf(stderr, "Insufficient memory to allocate ATTR chunk\n");
return NULL;
}
if ((ATTRData = (unsigned char *) malloc(14)) == NULL) {
fprintf(stderr, "Insufficient memory to allocate ATTR data\n");
free(ATTRChunk);
return NULL;
}
ColourIndex = GetCMAPEntry(colour, CMAPChunk);
ATTRData[0] = (StrokeOrFill) ? FT_NONE : FT_COLOR;
ATTRData[1] = JT_ROUND;
ATTRData[2] = 1;
ATTRData[3] = 0;
ShortAsBytes(ColourIndex, ATTRData + 4);
ShortAsBytes(ColourIndex, ATTRData + 6);
ShortAsBytes(0, ATTRData + 8);
FloatAsIEEEBytes(LineThickness, ATTRData + 10);
strncpy(ATTRChunk->ID, "ATTR", 4);
ATTRChunk->Size = 14;
ATTRChunk->Data = ATTRData;
return ATTRChunk;
}
static struct Chunk * BuildDRHD(int x1, int y1, int x2, int y2) {
struct Chunk * DRHDChunk;
unsigned char * DRHDData;
if ((DRHDChunk = (struct Chunk *) malloc(sizeof(struct Chunk))) == NULL) {
fprintf(stderr, "Insufficient memory to allocate DRHD chunk\n");
return NULL;
}
if ((DRHDData = (unsigned char *) malloc(16)) == NULL) {
fprintf(stderr, "Insufficient memory to allocate DRHD data\n");
free(DRHDChunk);
return NULL;
}
FloatAsIEEEBytes(x1 * XFactor, DRHDData);
FloatAsIEEEBytes(y1 * YFactor, DRHDData + 4);
FloatAsIEEEBytes(x2 * XFactor, DRHDData + 8);
FloatAsIEEEBytes(y2 * YFactor, DRHDData + 12);
strncpy(DRHDChunk->ID, "DRHD", 4);
DRHDChunk->Size = 16;
DRHDChunk->Data = DRHDData;
return DRHDChunk;
}
static struct Chunk * BuildPPRF(char * Units, int Portrait, char * PageType, float GridSize) {
struct Chunk * PPRFChunk;
char * PPRFData;
char * PPRFPos;
int ChunkSize;
if ((PPRFChunk = (struct Chunk *) malloc(sizeof(struct Chunk))) == NULL) {
fprintf(stderr, "Insufficient memory to allocate PPRF chunk\n");
return NULL;
}
ChunkSize = strlen("Units=") + strlen(Units) + 1;
ChunkSize += strlen("Portrait=") + (Portrait ? 4 : 5) + 1;
ChunkSize += strlen("PageType=") + strlen(PageType) + 1;
ChunkSize += strlen("GridSize=") + SizeFloat(GridSize, "%f") + 1;
if ((PPRFData = (char *) malloc(ChunkSize)) == NULL) {
fprintf(stderr, "Insufficient memory to allocate PPRF data\n");
free(PPRFChunk);
return NULL;
}
PPRFPos = PPRFData;
sprintf(PPRFPos, "Units=%s", Units);
PPRFPos += strlen(PPRFPos) + 1;
sprintf(PPRFPos, "Portrait=%s", (Portrait ? "True" : "False"));
PPRFPos += strlen(PPRFPos) + 1;
sprintf(PPRFPos, "PageType=%s", PageType);
PPRFPos += strlen(PPRFPos) + 1;
sprintf(PPRFPos, "GridSize=%f", GridSize);
strncpy(PPRFChunk->ID, "PPRF", 4);
PPRFChunk->Size = ChunkSize;
PPRFChunk->Data = (unsigned char *) PPRFData;
return PPRFChunk;
}
static struct Chunk * BuildLAYR() {
struct Chunk * LAYRChunk;
unsigned char * LAYRData;
if ((LAYRChunk = (struct Chunk *) malloc(sizeof(struct Chunk))) == NULL) {
fprintf(stderr, "Insufficient memory to allocate LAYR chunk\n");
return NULL;
}
if ((LAYRData = (unsigned char *) malloc(20)) == NULL) {
fprintf(stderr, "Insufficient memory to allocate LAYR data\n");
free(LAYRChunk);
return NULL;
}
ShortAsBytes(0, LAYRData);
memset(LAYRData + 2, (char) NULL, 16);
strcpy(LAYRData + 2, "Default layer");
*(LAYRData + 18) = LF_ACTIVE | LF_DISPLAYED;
*(LAYRData + 19) = 0;
strncpy(LAYRChunk->ID, "LAYR", 4);
LAYRChunk->Size = 20;
LAYRChunk->Data = LAYRData;
return LAYRChunk;
}
static struct Chunk * BuildDASH(void) {
struct Chunk * DASHChunk;
unsigned char * DASHData;
if ((DASHChunk = (struct Chunk *) malloc(sizeof(struct Chunk))) == NULL) {
fprintf(stderr, "Insufficient memory to allocate DASH chunk\n");
return NULL;
}
if ((DASHData = (unsigned char *) malloc(4)) == NULL) {
fprintf(stderr, "Insufficient memory to allocate DASH data\n");
free(DASHChunk);
return NULL;
}
ShortAsBytes(1, DASHData);
ShortAsBytes(0, DASHData + 2);
strncpy(DASHChunk->ID, "DASH", 4);
DASHChunk->Size = 4;
DASHChunk->Data = DASHData;
return DASHChunk;
}
static struct Chunk ** GeneratexPLY(struct Chunk * CMAP, spline_list_array_type shape, int height) {
unsigned this_list;
unsigned this_list_length;
unsigned this_spline;
unsigned this_spline_length;
spline_type s;
struct Chunk ** ChunkList;
struct Chunk * PolyChunk;
int ListPoint, PolySize, PolyPoint, NumPoints;
int StrokeOrFill;
unsigned char * PolyData;
this_list_length = SPLINE_LIST_ARRAY_LENGTH(shape);
/* We store three chunks for every spline (one for BBOX, one for ATTR, and one for xPLY) */
if ((ChunkList = (struct Chunk **) malloc(sizeof(struct Chunk) * (this_list_length * 3))) == NULL) {
fprintf(stderr, "Insufficient memory to allocate chunk list\n");
return NULL;
}
ListPoint = 0;
for (this_list = 0; this_list < this_list_length; this_list++) {
spline_list_type list = SPLINE_LIST_ARRAY_ELT(shape, this_list);
spline_type first = SPLINE_LIST_ELT(list, 0);
color_type curr_color = curr_color = (list.clockwise && shape.background_color != NULL)? *(shape.background_color) : list.color;
StrokeOrFill = (shape.centerline || list.open);
this_spline_length = SPLINE_LIST_LENGTH(list);
ChunkList[ListPoint++] = BuildBBOX(list, height);
ChunkList[ListPoint++] = BuildATTR(curr_color, StrokeOrFill, CMAP);
if ((PolyChunk = (struct Chunk *) malloc(sizeof(struct Chunk))) == NULL) {
fprintf(stderr, "Insufficient memory to allocate xPLY chunk\n");
FreeChunks(ChunkList, ListPoint);
return NULL;
}
NumPoints = CountSplines(list);
/* Store an extra 2 bytes for length header */
PolySize = (NumPoints << 3) + 2;
if ((PolyData = (unsigned char *) malloc(PolySize)) == NULL) {
fprintf(stderr, "Insufficient memory to allocate xPLY data\n");
free(PolyChunk);
free(PolyData);
FreeChunks(ChunkList, ListPoint);
return NULL;
}
ChunkList[ListPoint++] = PolyChunk;
strncpy(PolyChunk->ID, (StrokeOrFill) ? "OPLY" : "CPLY", 4);
PolyChunk->Size = PolySize;
PolyChunk->Data = PolyData;
ShortAsBytes(NumPoints, PolyData);
PolyPoint = 2;
if (SPLINE_DEGREE(first) == LINEARTYPE) {
PushPolyPoint(PolyData, &PolyPoint, START_POINT(first).x, height - START_POINT(first).y);
}
for (this_spline = 0; this_spline < this_spline_length; this_spline++) {
s = SPLINE_LIST_ELT(list, this_spline);
if (SPLINE_DEGREE(s) == LINEARTYPE) {
PushPolyPoint(PolyData, &PolyPoint, END_POINT(s).x, height - END_POINT(s).y);
} else {
PushPolyIndicator(PolyData, &PolyPoint, IND_SPLINE);
PushPolyPoint(PolyData, &PolyPoint, START_POINT(s).x, height - START_POINT(s).y);
PushPolyPoint(PolyData, &PolyPoint, CONTROL1(s).x, height - CONTROL1(s).y);
PushPolyPoint(PolyData, &PolyPoint, CONTROL2(s).x, height - CONTROL2(s).y);
PushPolyPoint(PolyData, &PolyPoint, END_POINT(s).x, height - END_POINT(s).y);
}
}
}
return ChunkList;
}
static int CountSplines(spline_list_type list) {
unsigned this_spline;
unsigned this_spline_length;
int Total;
Total = 0;
if (SPLINE_DEGREE(SPLINE_LIST_ELT(list, 0)) == LINEARTYPE) {
++Total;
}
this_spline_length = SPLINE_LIST_LENGTH(list);
for (this_spline = 0; this_spline < this_spline_length; this_spline++) {
if (SPLINE_DEGREE(SPLINE_LIST_ELT(list, this_spline)) == LINEARTYPE) {
++Total;
} else {
Total += 5;
}
}
return Total;
}
static void PushPolyPoint(unsigned char * PolyData, int * PolyPoint, float x, float y) {
int PolyLocal;
PolyLocal = *PolyPoint;
FloatAsIEEEBytes(x * XFactor, PolyData + PolyLocal);
PolyLocal += 4;
FloatAsIEEEBytes(y * YFactor, PolyData + PolyLocal);
*PolyPoint = PolyLocal + 4;
}
static void PushPolyIndicator(unsigned char * PolyData, int * PolyPoint, unsigned int flags) {
int PolyLocal;
PolyLocal = *PolyPoint;
IntAsBytes(INDICATOR, PolyData + PolyLocal);
PolyLocal += 4;
IntAsBytes(flags, PolyData + PolyLocal);
*PolyPoint = PolyLocal + 4;
}
static void WriteChunk(FILE * file, struct Chunk * Chunk) {
unsigned char SizeBytes[4];
int Size;
Size = Chunk->Size;
IntAsBytes(Size, SizeBytes);
fwrite(Chunk->ID, 4, 1, file);
fwrite(SizeBytes, 4, 1, file);
fwrite(Chunk->Data, Size, 1, file);
if (Size & 0x01) {
fprintf(file, "%c", (char) NULL);
}
}
static void WriteChunks(FILE * file, struct Chunk ** ChunkList, int NumChunks) {
int WalkChunks;
for (WalkChunks = 0; WalkChunks < NumChunks; WalkChunks++) {
WriteChunk(file, ChunkList[WalkChunks]);
}
}
static int TotalSizeChunks(struct Chunk ** ChunkList, int NumChunks) {
int WalkChunks;
int Size;
int Total;
Total = 0;
for (WalkChunks = 0; WalkChunks < NumChunks; WalkChunks++) {
/* 4 bytes for ID and 4 bytes for length */
Size = ChunkList[WalkChunks]->Size;
Size += Size & 0x01;
Total += (Size) + 8;
}
return Total;
}
static int SizeChunk(struct Chunk * ThisChunk) {
int Size;
Size = ThisChunk->Size;
Size += Size & 0x01;
return Size;
}
static void FreeChunk(struct Chunk * ThisChunk) {
free(ThisChunk->Data);
free(ThisChunk);
}
static void FreeChunks(struct Chunk ** ChunkList, int NumChunks) {
int WalkChunks;
for (WalkChunks = 0; WalkChunks < NumChunks; WalkChunks++) {
FreeChunk(ChunkList[WalkChunks]);
}
}
int output_dr2d_writer(FILE * file, at_string name, int llx, int lly, int urx, int ury,
at_output_opts_type * opts,
spline_list_array_type shape, at_msg_func msg_func, at_address msg_data)
{
int width = urx - llx;
int height = ury - lly;
int NumSplines, FORMSize;
int Portrait;
struct Chunk * DRHDChunk;
struct Chunk * PPRFChunk;
struct Chunk * LAYRChunk;
struct Chunk * DASHChunk;
struct Chunk * CMAPChunk;
struct Chunk ** ChunkList;
unsigned char SizeBytes[4];
Portrait = width < height;
if (Portrait) {
XFactor = ((float)11.6930 / (float) width) * (1 << FIXOFFS);
YFactor = XFactor;
} else {
YFactor = ((float)8.2681 / (float) height) * (1 << FIXOFFS);
XFactor = YFactor;
}
LineThickness = (float)1.0 / opts->dpi;
DRHDChunk = BuildDRHD(llx, lly, urx, ury);
PPRFChunk = BuildPPRF("Inch", Portrait, "A4", 1.0);
LAYRChunk = BuildLAYR();
DASHChunk = BuildDASH();
CMAPChunk = BuildCMAP(shape);
ChunkList = GeneratexPLY(CMAPChunk, shape, height);
NumSplines = SPLINE_LIST_ARRAY_LENGTH(shape) * 3;
FORMSize = 4 + (SizeChunk(DRHDChunk) + 8) + (SizeChunk(PPRFChunk) + 8) + (SizeChunk(LAYRChunk) + 8) + (SizeChunk(DASHChunk) + 8) + (SizeChunk(CMAPChunk) + 8) + TotalSizeChunks(ChunkList, NumSplines);
IntAsBytes(FORMSize, SizeBytes);
fprintf(file, "FORM");
fwrite(SizeBytes, 4, 1, file);
fprintf(file, "DR2D");
WriteChunk(file, DRHDChunk);
FreeChunk(DRHDChunk);
WriteChunk(file, PPRFChunk);
FreeChunk(PPRFChunk);
WriteChunk(file, LAYRChunk);
FreeChunk(LAYRChunk);
WriteChunk(file, DASHChunk);
FreeChunk(DASHChunk);
WriteChunk(file, CMAPChunk);
FreeChunk(CMAPChunk);
WriteChunks(file, ChunkList, NumSplines);
FreeChunks(ChunkList, NumSplines);
return 0;
}
static int SizeFloat(float f, char * Format) {
char FloatString[100];
return (sprintf(FloatString, Format, f));
}
static void IntAsBytes(int value, unsigned char * bytes) {
*bytes = (value >> 24) & 0xFF;
*(bytes + 1) = (value >> 16) & 0xFF;
*(bytes + 2) = (value >> 8) & 0xFF;
*(bytes + 3) = value & 0xFF;
}
static void ShortAsBytes(int value, unsigned char * bytes) {
*(bytes + 0) = (value >> 8) & 0xFF;
*(bytes + 1) = value & 0xFF;
}
static void FloatAsIEEEBytes(float value, unsigned char * bytes) {
flt2ieee(&value, bytes);
}
static void flt2ieee(float * flt, unsigned char * bytes) {
long RealMant, RealMask, RealExp;
long MoveExp;
RealMant = (long) *flt;
*bytes = 0;
*(bytes + 1) = 0;
*(bytes + 2) = 0;
*(bytes + 3) = 0;
if (RealMant) {
if (RealMant < 0) {
*bytes |= 0x80;
RealMant = -RealMant;
}
for (RealMask = 0x40000000, RealExp = 31; RealMask; RealMask >>= 1, RealExp--) {
if (RealMant & RealMask) {
break;
}
}
if (RealExp > 24) {
RealMant >>= RealExp - 24;
} else {
RealMant <<= 24 - RealExp;
}
RealExp -= FIXOFFS;
RealExp += 126;
MoveExp = RealExp << 23;
*bytes |= (MoveExp >> 24) & 0x7F;
*(bytes + 1) |= ((MoveExp >> 16) & 0x80) | ((RealMant >> 16) & 0x7F);
*(bytes + 2) |= (RealMant >> 8) & 0xFF;
*(bytes + 3) |= RealMant & 0xFF;
}
}