Imported from antiword-0.37.tar.gz.
[antiword.git] / png2eps.c
blob25d1755a74bab2970b5b4d1eca0a282977f3d172
1 /*
2 * png2eps.c
3 * Copyright (C) 2000-2002 A.J. van Os; Released under GPL
5 * Description:
6 * Functions to translate png images into eps
8 */
10 #include <stdio.h>
11 #include <ctype.h>
12 #include "antiword.h"
14 #if defined(DEBUG)
15 static int iPicCounter = 0;
16 #endif /* DEBUG */
20 * tSkipToData - skip until a IDAT chunk is found
22 * returns the length of the pixeldata or -1 in case of error
24 static size_t
25 tSkipToData(FILE *pFile, size_t tMaxBytes, size_t *ptSkipped)
27 ULONG ulName, ulTmp;
28 size_t tDataLength, tToSkip;
29 int iCounter;
31 fail(pFile == NULL);
32 fail(ptSkipped == NULL);
34 /* Examine chunks */
35 while (*ptSkipped + 8 < tMaxBytes) {
36 tDataLength = (size_t)ulNextLongBE(pFile);
37 DBG_DEC(tDataLength);
38 *ptSkipped += 4;
40 ulName = 0x00;
41 for (iCounter = 0; iCounter < 4; iCounter++) {
42 ulTmp = (ULONG)iNextByte(pFile);
43 if (!isalpha((int)ulTmp)) {
44 DBG_HEX(ulTmp);
45 return (size_t)-1;
47 ulName <<= 8;
48 ulName |= ulTmp;
50 DBG_HEX(ulName);
51 *ptSkipped += 4;
53 if (ulName == PNG_CN_IEND) {
54 break;
56 if (ulName == PNG_CN_IDAT) {
57 return tDataLength;
60 tToSkip = tDataLength + 4;
61 if (tToSkip >= tMaxBytes - *ptSkipped) {
62 DBG_DEC(tToSkip);
63 DBG_DEC(tMaxBytes - *ptSkipped);
64 return (size_t)-1;
66 (void)tSkipBytes(pFile, tToSkip);
67 *ptSkipped += tToSkip;
70 return (size_t)-1;
71 } /* end of iSkipToData */
74 * iFindFirstPixelData - find the first pixeldata if a PNG image
76 * returns the length of the pixeldata or -1 in case of error
78 static size_t
79 tFindFirstPixelData(FILE *pFile, size_t tMaxBytes, size_t *ptSkipped)
81 fail(pFile == NULL);
82 fail(tMaxBytes == 0);
83 fail(ptSkipped == NULL);
85 if (tMaxBytes < 8) {
86 DBG_DEC(tMaxBytes);
87 return (size_t)-1;
90 /* Skip over the PNG signature */
91 (void)tSkipBytes(pFile, 8);
92 *ptSkipped = 8;
94 return tSkipToData(pFile, tMaxBytes, ptSkipped);
95 } /* end of iFindFirstPixelData */
98 * tFindNextPixelData - find the next pixeldata if a PNG image
100 * returns the length of the pixeldata or -1 in case of error
102 static size_t
103 tFindNextPixelData(FILE *pFile, size_t tMaxBytes, size_t *ptSkipped)
105 fail(pFile == NULL);
106 fail(tMaxBytes == 0);
107 fail(ptSkipped == NULL);
109 if (tMaxBytes < 4) {
110 DBG_DEC(tMaxBytes);
111 return (size_t)-1;
114 /* Skip over the crc */
115 (void)tSkipBytes(pFile, 4);
116 *ptSkipped = 4;
118 return tSkipToData(pFile, tMaxBytes, ptSkipped);
119 } /* end of tFindNextPixelData */
121 #if defined(DEBUG)
123 * vCopy2File
125 static void
126 vCopy2File(FILE *pFile, ULONG ulFileOffset, size_t tPictureLen)
128 FILE *pOutFile;
129 size_t tIndex;
130 int iTmp;
131 char szFilename[30];
133 if (!bSetDataOffset(pFile, ulFileOffset)) {
134 return;
137 sprintf(szFilename, "/tmp/pic/pic%04d.png", ++iPicCounter);
138 pOutFile = fopen(szFilename, "wb");
139 if (pOutFile == NULL) {
140 return;
142 for (tIndex = 0; tIndex < tPictureLen; tIndex++) {
143 iTmp = iNextByte(pFile);
144 if (putc(iTmp, pOutFile) == EOF) {
145 break;
148 (void)fclose(pOutFile);
149 } /* end of vCopy2File */
150 #endif /* DEBUG */
153 * bTranslatePNG - translate a PNG image
155 * This function translates an image from png to eps
157 * return TRUE when sucessful, otherwise FALSE
159 BOOL
160 bTranslatePNG(diagram_type *pDiag, FILE *pFile,
161 ULONG ulFileOffset, size_t tPictureLen, const imagedata_type *pImg)
163 size_t tMaxBytes, tDataLength, tSkipped;
165 #if defined(DEBUG)
166 vCopy2File(pFile, ulFileOffset, tPictureLen);
167 #endif /* DEBUG */
169 /* Seek to start position of PNG data */
170 if (!bSetDataOffset(pFile, ulFileOffset)) {
171 return FALSE;
174 tMaxBytes = tPictureLen;
175 tDataLength = tFindFirstPixelData(pFile, tMaxBytes, &tSkipped);
176 if (tDataLength == (size_t)-1) {
177 return FALSE;
180 vImagePrologue(pDiag, pImg);
181 do {
182 tMaxBytes -= tSkipped;
183 vASCII85EncodeArray(pFile, pDiag->pOutFile, tDataLength);
184 tMaxBytes -= tDataLength;
185 tDataLength = tFindNextPixelData(pFile, tMaxBytes, &tSkipped);
186 } while (tDataLength != (size_t)-1);
187 vASCII85EncodeByte(pDiag->pOutFile, EOF);
188 vImageEpilogue(pDiag);
190 return TRUE;
191 } /* end of bTranslatePNG */