3 * Copyright (C) 2000-2002 A.J. van Os; Released under GPL
6 * Functions to translate png images into eps
15 static int iPicCounter
= 0;
20 * tSkipToData - skip until a IDAT chunk is found
22 * returns the length of the pixeldata or -1 in case of error
25 tSkipToData(FILE *pFile
, size_t tMaxBytes
, size_t *ptSkipped
)
28 size_t tDataLength
, tToSkip
;
32 fail(ptSkipped
== NULL
);
35 while (*ptSkipped
+ 8 < tMaxBytes
) {
36 tDataLength
= (size_t)ulNextLongBE(pFile
);
41 for (iCounter
= 0; iCounter
< 4; iCounter
++) {
42 ulTmp
= (ULONG
)iNextByte(pFile
);
43 if (!isalpha((int)ulTmp
)) {
53 if (ulName
== PNG_CN_IEND
) {
56 if (ulName
== PNG_CN_IDAT
) {
60 tToSkip
= tDataLength
+ 4;
61 if (tToSkip
>= tMaxBytes
- *ptSkipped
) {
63 DBG_DEC(tMaxBytes
- *ptSkipped
);
66 (void)tSkipBytes(pFile
, tToSkip
);
67 *ptSkipped
+= tToSkip
;
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
79 tFindFirstPixelData(FILE *pFile
, size_t tMaxBytes
, size_t *ptSkipped
)
83 fail(ptSkipped
== NULL
);
90 /* Skip over the PNG signature */
91 (void)tSkipBytes(pFile
, 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
103 tFindNextPixelData(FILE *pFile
, size_t tMaxBytes
, size_t *ptSkipped
)
106 fail(tMaxBytes
== 0);
107 fail(ptSkipped
== NULL
);
114 /* Skip over the crc */
115 (void)tSkipBytes(pFile
, 4);
118 return tSkipToData(pFile
, tMaxBytes
, ptSkipped
);
119 } /* end of tFindNextPixelData */
126 vCopy2File(FILE *pFile
, ULONG ulFileOffset
, size_t tPictureLen
)
133 if (!bSetDataOffset(pFile
, ulFileOffset
)) {
137 sprintf(szFilename
, "/tmp/pic/pic%04d.png", ++iPicCounter
);
138 pOutFile
= fopen(szFilename
, "wb");
139 if (pOutFile
== NULL
) {
142 for (tIndex
= 0; tIndex
< tPictureLen
; tIndex
++) {
143 iTmp
= iNextByte(pFile
);
144 if (putc(iTmp
, pOutFile
) == EOF
) {
148 (void)fclose(pOutFile
);
149 } /* end of vCopy2File */
153 * bTranslatePNG - translate a PNG image
155 * This function translates an image from png to eps
157 * return TRUE when sucessful, otherwise FALSE
160 bTranslatePNG(diagram_type
*pDiag
, FILE *pFile
,
161 ULONG ulFileOffset
, size_t tPictureLen
, const imagedata_type
*pImg
)
163 size_t tMaxBytes
, tDataLength
, tSkipped
;
166 vCopy2File(pFile
, ulFileOffset
, tPictureLen
);
169 /* Seek to start position of PNG data */
170 if (!bSetDataOffset(pFile
, ulFileOffset
)) {
174 tMaxBytes
= tPictureLen
;
175 tDataLength
= tFindFirstPixelData(pFile
, tMaxBytes
, &tSkipped
);
176 if (tDataLength
== (size_t)-1) {
180 vImagePrologue(pDiag
, pImg
);
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
);
191 } /* end of bTranslatePNG */