AWARE SYSTEMS
TIFF and LibTiff Mail List Archive

Thread

1999.12.17 11:29 "TIFF previews in EPS files", by Flemming Bengtsson
1999.12.17 14:55 "Re: TIFF previews in EPS files", by Bjorn Brox
1999.12.17 15:54 "RE: TIFF previews in EPS files", by Dean Grimm

1999.12.17 15:54 "RE: TIFF previews in EPS files", by Dean Grimm

Hi Flemming,

I use the following C++ code on Windows:

#define EPS_BUFFER_LEN                  8192
#define EPS_THUMBTYPE_ERROR             -2
#define EPS_THUMBTYPE_NONE              -1
#define EPS_THUMBTYPE_TIFF              0
#define EPS_THUMBTYPE_META              1

const BYTE EPS_HDR_MAGIC_PS_ASCII[] = "%!";
const BYTE EPS_HDR_MAGIC_PS_WIN[] = {0xc5,0xd0,0xd3,0xc6};

typedef struct tagEPSHEADER {
        BYTE magic[4];
        DWORD offPS;
        DWORD lenPS;
        DWORD offMeta;
        DWORD lenMeta;
        DWORD offTiff;
        DWORD lenTiff;
        WORD checksum;

}EPSHEADER;

int ExtractEPSThumbnail(LPCTSTR pszEPSPath, LPCTSTR pszOutPath)
{
        int result = EPS_THUMBTYPE_NONE;
        HANDLE hf;

      // open the EPS file
      if ((hf=FOPEN(pszEPSPath)) != INVALID_HANDLE_VALUE)
      {
              EPSHEADER hdr;
              DWORD dwRead, dwOffset=0, dwLength=0;

              // read the header
              if (!ReadFile(hf, &hdr, sizeof(EPSHEADER), &dwRead, NULL)
                      || dwRead != sizeof(EPSHEADER))
              {
                      CloseHandle(hf);
                      return EPS_THUMBTYPE_NONE;
              }

              // check the type
              if (hdr.magic[0] == EPS_HDR_MAGIC_PS_ASCII[0]
                      && hdr.magic[1] == EPS_HDR_MAGIC_PS_ASCII[1])
              {
                      result = EPS_THUMBTYPE_NONE;
              }
              else if (hdr.magic[0] == EPS_HDR_MAGIC_PS_WIN[0]
                      && hdr.magic[1] == EPS_HDR_MAGIC_PS_WIN[1]
                      && hdr.magic[2] == EPS_HDR_MAGIC_PS_WIN[2]
                      && hdr.magic[3] == EPS_HDR_MAGIC_PS_WIN[3])
              {
                      // thumbnail is TIFF
                      if (hdr.lenTiff>0)
                      {
                              result = EPS_THUMBTYPE_TIFF;
                              dwOffset = hdr.offTiff;
                              dwLength = hdr.lenTiff;
                      }
                      // thumbnail is metafile
                      else if (hdr.lenMeta>0)
                      {
                              result = EPS_THUMBTYPE_META;
                              dwOffset = hdr.offMeta;
                              dwLength = hdr.lenMeta;
                      }
                      else
                              result = EPS_THUMBTYPE_NONE;

                      // extract the file
                      if (dwLength > 0)
                      {
                              HANDLE hfOut;
                              DWORD dwOut = 0, dwSize, dwWrite;

                                // open the EPS file
                                if ((hfOut=FOPEN(pszOutPath, GENERIC_WRITE,
CREATE_ALWAYS)) != INVALID_HANDLE_VALUE)
                                {
                                        char buf[EPS_BUFFER_LEN];

                                        // seek to the thumbnail offset
                                        if (SetFilePointer(hf, dwOffset,
NULL, FILE_BEGIN) != 0xFFFFFFFF)
                                        {
                                                while (dwOut < dwLength)
                                                {
                                                        // read, at most,
bufferlen bytes from input file
                                                        dwSize =
min(EPS_BUFFER_LEN, dwLength-dwOut);
                                                        if (!ReadFile(hf,
buf, dwSize, &dwRead, NULL)
                                                                || dwRead !=
dwSize)
                                                        {
                                                                result =
EPS_THUMBTYPE_ERROR;
                                                                break;
                                                        }

                                                        // write bytes to
output file
                                                        if
(!WriteFile(hfOut, buf, dwRead, &dwWrite, NULL)
                                                                || dwWrite
!= dwRead)
                                                        {
                                                                result =
EPS_THUMBTYPE_ERROR;
                                                                break;
                                                        }
                                                        dwOut += dwSize;
                                                }
                                        }
                                        else
                                                result =
EPS_THUMBTYPE_ERROR;

                                      CloseHandle(hfOut);
                              }
                      }
              }

              CloseHandle(hf);
      }

        return result;
}