examples/PIPS/antiword/src/png2eps.c

00001 /*
00002  * png2eps.c
00003  * Copyright (C) 2000-2002 A.J. van Os; Released under GPL
00004  *
00005  * Description:
00006  * Functions to translate png images into eps
00007  *
00008  */
00009 
00010 #include <stdio.h>
00011 #include <ctype.h>
00012 #include "antiword.h"
00013 
00014 #if defined(DEBUG)
00015 static int      iPicCounter = 0;
00016 #endif /* DEBUG */
00017 
00018 
00019 /*
00020  * tSkipToData - skip until a IDAT chunk is found
00021  *
00022  * returns the length of the pixeldata or -1 in case of error
00023  */
00024 static size_t
00025 tSkipToData(FILE *pFile, size_t tMaxBytes, size_t *ptSkipped)
00026 {
00027         ULONG   ulName, ulTmp;
00028         size_t  tDataLength, tToSkip;
00029         int     iCounter;
00030 
00031         fail(pFile == NULL);
00032         fail(ptSkipped == NULL);
00033 
00034         /* Examine chunks */
00035         while (*ptSkipped + 8 < tMaxBytes) {
00036                 tDataLength = (size_t)ulNextLongBE(pFile);
00037                 DBG_DEC(tDataLength);
00038                 *ptSkipped += 4;
00039 
00040                 ulName = 0x00;
00041                 for (iCounter = 0; iCounter < 4; iCounter++) {
00042                         ulTmp = (ULONG)iNextByte(pFile);
00043                         if (!isalpha((int)ulTmp)) {
00044                                 DBG_HEX(ulTmp);
00045                                 return (size_t)-1;
00046                         }
00047                         ulName <<= 8;
00048                         ulName |= ulTmp;
00049                 }
00050                 DBG_HEX(ulName);
00051                 *ptSkipped += 4;
00052 
00053                 if (ulName == PNG_CN_IEND) {
00054                         break;
00055                 }
00056                 if (ulName == PNG_CN_IDAT) {
00057                         return tDataLength;
00058                 }
00059 
00060                 tToSkip = tDataLength + 4;
00061                 if (tToSkip >= tMaxBytes - *ptSkipped) {
00062                         DBG_DEC(tToSkip);
00063                         DBG_DEC(tMaxBytes - *ptSkipped);
00064                         return (size_t)-1;
00065                 }
00066                 (void)tSkipBytes(pFile, tToSkip);
00067                 *ptSkipped += tToSkip;
00068         }
00069 
00070         return (size_t)-1;
00071 } /* end of iSkipToData */
00072 
00073 /*
00074  * iFindFirstPixelData - find the first pixeldata if a PNG image
00075  *
00076  * returns the length of the pixeldata or -1 in case of error
00077  */
00078 static size_t
00079 tFindFirstPixelData(FILE *pFile, size_t tMaxBytes, size_t *ptSkipped)
00080 {
00081         fail(pFile == NULL);
00082         fail(tMaxBytes == 0);
00083         fail(ptSkipped == NULL);
00084 
00085         if (tMaxBytes < 8) {
00086                 DBG_DEC(tMaxBytes);
00087                 return (size_t)-1;
00088         }
00089 
00090         /* Skip over the PNG signature */
00091         (void)tSkipBytes(pFile, 8);
00092         *ptSkipped = 8;
00093 
00094         return tSkipToData(pFile, tMaxBytes, ptSkipped);
00095 } /* end of iFindFirstPixelData */
00096 
00097 /*
00098  * tFindNextPixelData - find the next pixeldata if a PNG image
00099  *
00100  * returns the length of the pixeldata or -1 in case of error
00101  */
00102 static size_t
00103 tFindNextPixelData(FILE *pFile, size_t tMaxBytes, size_t *ptSkipped)
00104 {
00105         fail(pFile == NULL);
00106         fail(tMaxBytes == 0);
00107         fail(ptSkipped == NULL);
00108 
00109         if (tMaxBytes < 4) {
00110                 DBG_DEC(tMaxBytes);
00111                 return (size_t)-1;
00112         }
00113 
00114         /* Skip over the crc */
00115         (void)tSkipBytes(pFile, 4);
00116         *ptSkipped = 4;
00117 
00118         return tSkipToData(pFile, tMaxBytes, ptSkipped);
00119 } /* end of tFindNextPixelData */
00120 
00121 #if defined(DEBUG)
00122 /*
00123  * vCopy2File
00124  */
00125 static void
00126 vCopy2File(FILE *pFile, ULONG ulFileOffset, size_t tPictureLen)
00127 {
00128         FILE    *pOutFile;
00129         size_t  tIndex;
00130         int     iTmp;
00131         char    szFilename[30];
00132 
00133         if (!bSetDataOffset(pFile, ulFileOffset)) {
00134                 return;
00135         }
00136 
00137         sprintf(szFilename, "/tmp/pic/pic%04d.png", ++iPicCounter);
00138         pOutFile = fopen(szFilename, "wb");
00139         if (pOutFile == NULL) {
00140                 return;
00141         }
00142         for (tIndex = 0; tIndex < tPictureLen; tIndex++) {
00143                 iTmp = iNextByte(pFile);
00144                 if (putc(iTmp, pOutFile) == EOF) {
00145                         break;
00146                 }
00147         }
00148         (void)fclose(pOutFile);
00149 } /* end of vCopy2File */
00150 #endif /* DEBUG */
00151 
00152 /*
00153  * bTranslatePNG - translate a PNG image
00154  *
00155  * This function translates an image from png to eps
00156  *
00157  * return TRUE when sucessful, otherwise FALSE
00158  */
00159 BOOL
00160 bTranslatePNG(diagram_type *pDiag, FILE *pFile,
00161         ULONG ulFileOffset, size_t tPictureLen, const imagedata_type *pImg)
00162 {
00163         size_t  tMaxBytes, tDataLength, tSkipped;
00164 
00165 #if defined(DEBUG)
00166         vCopy2File(pFile, ulFileOffset, tPictureLen);
00167 #endif /* DEBUG */
00168 
00169         /* Seek to start position of PNG data */
00170         if (!bSetDataOffset(pFile, ulFileOffset)) {
00171                 return FALSE;
00172         }
00173 
00174         tMaxBytes = tPictureLen;
00175         tDataLength = tFindFirstPixelData(pFile, tMaxBytes, &tSkipped);
00176         if (tDataLength == (size_t)-1) {
00177                 return FALSE;
00178         }
00179 
00180         vImagePrologue(pDiag, pImg);
00181         do {
00182                 tMaxBytes -= tSkipped;
00183                 vASCII85EncodeArray(pFile, pDiag->pOutFile, tDataLength);
00184                 tMaxBytes -= tDataLength;
00185                 tDataLength = tFindNextPixelData(pFile, tMaxBytes, &tSkipped);
00186         } while (tDataLength != (size_t)-1);
00187         vASCII85EncodeByte(pDiag->pOutFile, EOF);
00188         vImageEpilogue(pDiag);
00189 
00190         return TRUE;
00191 } /* end of bTranslatePNG */

Generated by  doxygen 1.6.2