-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpng2eps.c
191 lines (164 loc) · 3.94 KB
/
png2eps.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
/*
* png2eps.c
* Copyright (C) 2000-2002 A.J. van Os; Released under GPL
*
* Description:
* Functions to translate png images into eps
*
*/
#include <stdio.h>
#include <ctype.h>
#include "antiword.h"
#if defined(DEBUG)
static int iPicCounter = 0;
#endif /* DEBUG */
/*
* tSkipToData - skip until a IDAT chunk is found
*
* returns the length of the pixeldata or -1 in case of error
*/
static size_t
tSkipToData(FILE *pFile, size_t tMaxBytes, size_t *ptSkipped)
{
ULONG ulName, ulTmp;
size_t tDataLength, tToSkip;
int iCounter;
fail(pFile == NULL);
fail(ptSkipped == NULL);
/* Examine chunks */
while (*ptSkipped + 8 < tMaxBytes) {
tDataLength = (size_t)ulNextLongBE(pFile);
DBG_DEC(tDataLength);
*ptSkipped += 4;
ulName = 0x00;
for (iCounter = 0; iCounter < 4; iCounter++) {
ulTmp = (ULONG)iNextByte(pFile);
if (!isalpha((int)ulTmp)) {
DBG_HEX(ulTmp);
return (size_t)-1;
}
ulName <<= 8;
ulName |= ulTmp;
}
DBG_HEX(ulName);
*ptSkipped += 4;
if (ulName == PNG_CN_IEND) {
break;
}
if (ulName == PNG_CN_IDAT) {
return tDataLength;
}
tToSkip = tDataLength + 4;
if (tToSkip >= tMaxBytes - *ptSkipped) {
DBG_DEC(tToSkip);
DBG_DEC(tMaxBytes - *ptSkipped);
return (size_t)-1;
}
(void)tSkipBytes(pFile, tToSkip);
*ptSkipped += tToSkip;
}
return (size_t)-1;
} /* end of iSkipToData */
/*
* iFindFirstPixelData - find the first pixeldata if a PNG image
*
* returns the length of the pixeldata or -1 in case of error
*/
static size_t
tFindFirstPixelData(FILE *pFile, size_t tMaxBytes, size_t *ptSkipped)
{
fail(pFile == NULL);
fail(tMaxBytes == 0);
fail(ptSkipped == NULL);
if (tMaxBytes < 8) {
DBG_DEC(tMaxBytes);
return (size_t)-1;
}
/* Skip over the PNG signature */
(void)tSkipBytes(pFile, 8);
*ptSkipped = 8;
return tSkipToData(pFile, tMaxBytes, ptSkipped);
} /* end of iFindFirstPixelData */
/*
* tFindNextPixelData - find the next pixeldata if a PNG image
*
* returns the length of the pixeldata or -1 in case of error
*/
static size_t
tFindNextPixelData(FILE *pFile, size_t tMaxBytes, size_t *ptSkipped)
{
fail(pFile == NULL);
fail(tMaxBytes == 0);
fail(ptSkipped == NULL);
if (tMaxBytes < 4) {
DBG_DEC(tMaxBytes);
return (size_t)-1;
}
/* Skip over the crc */
(void)tSkipBytes(pFile, 4);
*ptSkipped = 4;
return tSkipToData(pFile, tMaxBytes, ptSkipped);
} /* end of tFindNextPixelData */
#if defined(DEBUG)
/*
* vCopy2File
*/
static void
vCopy2File(FILE *pFile, ULONG ulFileOffset, size_t tPictureLen)
{
FILE *pOutFile;
size_t tIndex;
int iTmp;
char szFilename[30];
if (!bSetDataOffset(pFile, ulFileOffset)) {
return;
}
sprintf(szFilename, "/tmp/pic/pic%04d.png", ++iPicCounter);
pOutFile = fopen(szFilename, "wb");
if (pOutFile == NULL) {
return;
}
for (tIndex = 0; tIndex < tPictureLen; tIndex++) {
iTmp = iNextByte(pFile);
if (putc(iTmp, pOutFile) == EOF) {
break;
}
}
(void)fclose(pOutFile);
} /* end of vCopy2File */
#endif /* DEBUG */
/*
* bTranslatePNG - translate a PNG image
*
* This function translates an image from png to eps
*
* return TRUE when sucessful, otherwise FALSE
*/
BOOL
bTranslatePNG(diagram_type *pDiag, FILE *pFile,
ULONG ulFileOffset, size_t tPictureLen, const imagedata_type *pImg)
{
size_t tMaxBytes, tDataLength, tSkipped;
#if defined(DEBUG)
vCopy2File(pFile, ulFileOffset, tPictureLen);
#endif /* DEBUG */
/* Seek to start position of PNG data */
if (!bSetDataOffset(pFile, ulFileOffset)) {
return FALSE;
}
tMaxBytes = tPictureLen;
tDataLength = tFindFirstPixelData(pFile, tMaxBytes, &tSkipped);
if (tDataLength == (size_t)-1) {
return FALSE;
}
vImagePrologue(pDiag, pImg);
do {
tMaxBytes -= tSkipped;
vASCII85EncodeArray(pFile, pDiag->pOutFile, tDataLength);
tMaxBytes -= tDataLength;
tDataLength = tFindNextPixelData(pFile, tMaxBytes, &tSkipped);
} while (tDataLength != (size_t)-1);
vASCII85EncodeByte(pDiag->pOutFile, EOF);
vImageEpilogue(pDiag);
return TRUE;
} /* end of bTranslatePNG */