home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / png2eps.c < prev    next >
C/C++ Source or Header  |  2000-11-10  |  4KB  |  196 lines

  1. /*
  2.  * png2eps.c
  3.  * Copyright (C) 2000 A.J. van Os; Released under GPL
  4.  *
  5.  * Description:
  6.  * Functions to translate png images into eps
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include "antiword.h"
  13.  
  14. #if defined(DEBUG)
  15. static int    iPicCounter = 0;
  16. #endif /* DEBUG */
  17.  
  18.  
  19. /*
  20.  * iSkipToData - skip until a IDAT chunk is found
  21.  *
  22.  * returns the length of the pixeldata or -1 in case of error
  23.  */
  24. static int
  25. iSkipToData(FILE *pFile, int iMaxBytes, int *piSkipped)
  26. {
  27.     unsigned long    ulName, ulTmp;
  28.     int    iDataLength, iCounter, iToSkip;
  29.  
  30.     fail(pFile == NULL);
  31.     fail(iMaxBytes < 0);
  32.     fail(piSkipped == NULL);
  33.     fail(*piSkipped < 0);
  34.  
  35.     /* Examine chunks */
  36.     while (*piSkipped + 8 < iMaxBytes) {
  37.         iDataLength = (int)ulNextLongBE(pFile);
  38.         if (iDataLength < 0) {
  39.             DBG_DEC(iDataLength);
  40.             return -1;
  41.         }
  42.         DBG_DEC(iDataLength);
  43.         *piSkipped += 4;
  44.  
  45.         ulName = 0x00;
  46.         for (iCounter = 0; iCounter < 4; iCounter++) {
  47.             ulTmp = (unsigned long)iNextByte(pFile);
  48.             if (!isalpha((int)ulTmp)) {
  49.                 DBG_HEX(ulTmp);
  50.                 return -1;
  51.             }
  52.             ulName <<= 8;
  53.             ulName |= ulTmp;
  54.         }
  55.         DBG_HEX(ulName);
  56.         *piSkipped += 4;
  57.  
  58.         if (ulName == PNG_CN_IEND) {
  59.             break;
  60.         }
  61.         if (ulName == PNG_CN_IDAT) {
  62.             return iDataLength;
  63.         }
  64.  
  65.         iToSkip = iDataLength + 4;
  66.         if (iToSkip >= iMaxBytes - *piSkipped) {
  67.             DBG_DEC(iToSkip);
  68.             DBG_DEC(iMaxBytes - *piSkipped);
  69.             return -1;
  70.         }
  71.         (void)iSkipBytes(pFile, (size_t)iToSkip);
  72.         *piSkipped += iToSkip;
  73.     }
  74.  
  75.     return -1;
  76. } /* end of iSkipToData */
  77.  
  78. /*
  79.  * iFindFirstPixelData - find the first pixeldata if a PNG image
  80.  *
  81.  * returns the length of the pixeldata or -1 in case of error
  82.  */
  83. static int
  84. iFindFirstPixelData(FILE *pFile, int iMaxBytes, int *piSkipped)
  85. {
  86.     fail(pFile == NULL);
  87.     fail(iMaxBytes <= 0);
  88.     fail(piSkipped == NULL);
  89.  
  90.     if (iMaxBytes < 8) {
  91.         DBG_DEC(iMaxBytes);
  92.         return -1;
  93.     }
  94.  
  95.     /* Skip over the PNG signature */
  96.     (void)iSkipBytes(pFile, 8);
  97.     *piSkipped = 8;
  98.  
  99.     return iSkipToData(pFile, iMaxBytes, piSkipped);
  100. } /* end of iFindFirstPixelData */
  101.  
  102. /*
  103.  * iFindNextPixelData - find the next pixeldata if a PNG image
  104.  *
  105.  * returns the length of the pixeldata or -1 in case of error
  106.  */
  107. static int
  108. iFindNextPixelData(FILE *pFile, int iMaxBytes, int *piSkipped)
  109. {
  110.     fail(pFile == NULL);
  111.     fail(iMaxBytes <= 0);
  112.     fail(piSkipped == NULL);
  113.  
  114.     if (iMaxBytes < 4) {
  115.         DBG_DEC(iMaxBytes);
  116.         return -1;
  117.     }
  118.  
  119.     /* Skip over the crc */
  120.     (void)iSkipBytes(pFile, 4);
  121.     *piSkipped = 4;
  122.  
  123.     return iSkipToData(pFile, iMaxBytes, piSkipped);
  124. } /* end of iFindNextPixelData */
  125.  
  126. #if defined(DEBUG)
  127. /*
  128.  * vCopy2File
  129.  */
  130. static void
  131. vCopy2File(FILE *pFile, long lFileOffset, int iPictureLen)
  132. {
  133.     FILE    *pOutFile;
  134.     int    iIndex, iTmp;
  135.     char    szFilename[30];
  136.  
  137.     if (!bSetDataOffset(pFile, lFileOffset)) {
  138.         return;
  139.     }
  140.  
  141.     sprintf(szFilename, "/tmp/pic/pic%04d.png", ++iPicCounter);
  142.     pOutFile = fopen(szFilename, "wb");
  143.     if (pOutFile == NULL) {
  144.         return;
  145.     }
  146.     for (iIndex = 0; iIndex < iPictureLen; iIndex++) {
  147.         iTmp = iNextByte(pFile);
  148.         if (putc(iTmp, pOutFile) == EOF) {
  149.             break;
  150.         }
  151.     }
  152.     (void)fclose(pOutFile);
  153. } /* end of vCopy2File */
  154. #endif /* DEBUG */
  155.  
  156. /*
  157.  * bTranslatePNG - translate a PNG image
  158.  *
  159.  * This function translates an image from png to eps
  160.  *
  161.  * return TRUE when sucessful, otherwise FALSE
  162.  */
  163. BOOL
  164. bTranslatePNG(diagram_type *pDiag, FILE *pFile,
  165.     long lFileOffset, int iPictureLen, const imagedata_type *pImg)
  166. {
  167.     int    iMaxBytes, iDataLength, iSkipped;
  168.  
  169. #if defined(DEBUG)
  170.     vCopy2File(pFile, lFileOffset, iPictureLen);
  171. #endif /* DEBUG */
  172.  
  173.     /* Seek to start position of PNG data */
  174.     if (!bSetDataOffset(pFile, lFileOffset)) {
  175.         return FALSE;
  176.     }
  177.  
  178.     iMaxBytes = iPictureLen;
  179.     iDataLength = iFindFirstPixelData(pFile, iMaxBytes, &iSkipped);
  180.     if (iDataLength < 0) {
  181.         return FALSE;
  182.     }
  183.  
  184.     vImagePrologue(pDiag, pImg);
  185.     do {
  186.         iMaxBytes -= iSkipped;
  187.         vASCII85EncodeArray(pFile, pDiag->pOutFile, iDataLength);
  188.         iMaxBytes -= iDataLength;
  189.         iDataLength = iFindNextPixelData(pFile, iMaxBytes, &iSkipped);
  190.     } while (iDataLength >= 0);
  191.     vASCII85EncodeByte(pDiag->pOutFile, EOF);
  192.     vImageEpilogue(pDiag);
  193.  
  194.     return TRUE;
  195. } /* end of bTranslatePNG */
  196.