home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / POSTSCPT / GSVIEW / SRC / GVWEPS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-01  |  6.5 KB  |  242 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvweps.c */
  19. /* EPS file manipulation module for Windows GSview */
  20.  
  21. #include "gvwin.h"
  22.  
  23.  
  24. HGLOBAL make_dib(void);  /* in gvwclip.c */
  25.  
  26. static HGLOBAL get_bitmap_hglobal;
  27. static BOOL get_bitmap_made_dib = FALSE;
  28.  
  29. LPBITMAP2
  30. get_bitmap()
  31. {
  32.     get_bitmap_hglobal = 0;
  33.     get_bitmap_made_dib = FALSE;
  34.     if (!OpenClipboard(hwndimg))
  35.         return NULL;
  36.     if (IsClipboardFormatAvailable(CF_DIB))
  37.         get_bitmap_hglobal = GetClipboardData(CF_DIB);
  38.     else if (IsClipboardFormatAvailable(CF_BITMAP)) {
  39.         /* untested */
  40.         get_bitmap_hglobal = make_dib(); /* convert to DIB format */
  41.         if (get_bitmap_hglobal == (HGLOBAL)NULL) {
  42.             CloseClipboard();
  43.         return NULL;
  44.         }
  45.         get_bitmap_made_dib = TRUE;
  46.     }
  47.     else {
  48.         CloseClipboard();
  49.         return NULL;
  50.     }
  51.     return (LPBITMAP2)GlobalLock(get_bitmap_hglobal);
  52. }
  53.  
  54. void
  55. release_bitmap()
  56. {
  57.     GlobalUnlock(get_bitmap_hglobal);
  58.     if (get_bitmap_made_dib)
  59.         GlobalFree(get_bitmap_hglobal);
  60.     CloseClipboard();
  61. }
  62.  
  63. #ifdef __WIN32__
  64. long
  65. hugewrite(HFILE hf, const void _huge *hpvBuffer, long cbBuffer)
  66. {
  67.         return _hwrite(hf, hpvBuffer, cbBuffer);
  68. }
  69. #else
  70. /* Write data to file - blocks > 64k permitted */
  71. long
  72. hugewrite(HFILE hf, const void _huge *hpvBuffer, long cbBuffer)
  73. {
  74. DWORD count;
  75. long written, done;
  76. char _huge *hp;
  77.     if (is_win31)
  78.         return _hwrite(hf, hpvBuffer, cbBuffer);
  79.     done = 0;
  80.     hp = (char _huge *)hpvBuffer;
  81.     while (cbBuffer > 0) {
  82.         count = min( min(32768UL, cbBuffer), (DWORD)(65536UL-OFFSETOF(hp)) );
  83.         written = _lwrite(hf, hp, (UINT)count);
  84.         if (written == (long)HFILE_ERROR)
  85.         return (long)HFILE_ERROR;
  86.         done += written;
  87.         cbBuffer -= written;
  88.         hp += written;
  89.     }
  90.     return done;
  91. }
  92. #endif
  93.  
  94. /* convert a clipboard bitmap to a metafile picture */
  95. HMETAFILE
  96. make_metafile(void)
  97. {
  98. HDC hdc;
  99. HMETAFILE hmf;
  100. HGLOBAL hglobal;
  101.     if (IsClipboardFormatAvailable(CF_DIB)) {
  102.         LPBITMAPINFOHEADER pbmih;
  103.         BYTE _huge *lpDibBits;
  104.         hglobal = GetClipboardData(CF_DIB);
  105.         pbmih = (LPBITMAPINFOHEADER)GlobalLock(hglobal);
  106.         lpDibBits = ((BYTE _huge *)pbmih) + pbmih->biSize;
  107.         if (pbmih->biSize == sizeof(BITMAPCOREHEADER))
  108.         lpDibBits += dib_pal_colors((LPBITMAP2)pbmih) * sizeof(RGBTRIPLE); 
  109.         else
  110.         lpDibBits += dib_pal_colors((LPBITMAP2)pbmih) * sizeof(RGBQUAD); 
  111.         /* now make a Metafile from it */
  112.         hdc = CreateMetaFile(NULL);
  113.         SetWindowOrg(hdc, 0, 0);
  114.         SetWindowExt(hdc, (int)pbmih->biWidth, (int)pbmih->biHeight);
  115.         StretchDIBits(hdc, 0, 0, (int)pbmih->biWidth, (int)pbmih->biHeight,
  116.             0, 0, (int)pbmih->biWidth, (int)pbmih->biHeight,
  117.             (void FAR *)lpDibBits, (LPBITMAPINFO)pbmih,
  118.             DIB_RGB_COLORS, SRCCOPY);
  119.         hmf = CloseMetaFile(hdc);
  120.         GlobalUnlock(hglobal);
  121.     }
  122.     else if (IsClipboardFormatAvailable(CF_BITMAP)) {
  123.         HBITMAP hbitmap, hbitmap_old;
  124.         HDC hdc_bit;
  125.         BITMAP bm;
  126.         hbitmap = GetClipboardData(CF_BITMAP);
  127.         hdc = GetDC((HWND)NULL);
  128.         hdc_bit = CreateCompatibleDC(hdc);
  129.         ReleaseDC((HWND)NULL,hdc);
  130.         GetObject(hbitmap, sizeof(BITMAP), &bm);
  131.         hdc = CreateMetaFile(NULL);
  132.         SetWindowOrg(hdc, 0, 0);
  133.         SetWindowExt(hdc, bm.bmWidth, bm.bmHeight);
  134.         hbitmap_old = SelectBitmap(hdc_bit, hbitmap);
  135.         StretchBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight,
  136.             hdc_bit, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  137.         SelectBitmap(hdc_bit, hbitmap_old);
  138.         DeleteDC(hdc_bit);
  139.         hmf = CloseMetaFile(hdc);
  140.     }
  141.     else {
  142.         play_sound(SOUND_ERROR);
  143.         hmf = NULL;
  144.     }
  145.     return hmf;
  146. }
  147.  
  148. /* make a PC EPS file with a Windows Metafile Preview */
  149. /* from a PS file and a clipboard bitmap */
  150. void
  151. make_eps_metafile(void)
  152. {
  153. char epsname[MAXSTR];
  154. HMETAFILE hmf;
  155. HGLOBAL hglobal;
  156. char *buffer;
  157. UINT count;
  158. HFILE hfEPS;
  159. struct eps_header_s eps_header;
  160. #ifdef __WIN32__
  161. UINT size;
  162. LPCVOID lpmf;
  163. #else
  164. LPSTR lpmf;
  165. #endif
  166.  
  167.     if (!OpenClipboard(hwndimg)) {
  168.         play_sound(SOUND_ERROR);
  169.         return;
  170.     }
  171.  
  172.     if ( (hmf = make_metafile()) == (HMETAFILE)NULL ) {
  173.         play_sound(SOUND_ERROR);
  174.         CloseClipboard();
  175.         return;
  176.     }
  177.  
  178.     CloseClipboard();
  179.  
  180.     /* get memory handle to metafile */
  181. #ifdef __WIN32__
  182.     size = GetMetaFileBitsEx(hmf, 0, NULL);
  183.     hglobal = GlobalAlloc(GHND, size);
  184.     lpmf = GlobalLock(hglobal);
  185.     GetMetaFileBitsEx(hmf, size, lpmf);
  186.     GlobalUnlock(hglobal);
  187. #else
  188.     hglobal = GetMetaFileBits(hmf);
  189. #endif
  190.  
  191.     /* create buffer for PS file copy */
  192.     buffer = malloc(COPY_BUF_SIZE);
  193.     if (buffer == (char *)NULL) {
  194.         GlobalFree(hglobal);
  195.         gserror(IDS_BADEPS, NULL, MB_ICONEXCLAMATION, SOUND_ERROR);
  196.         return;
  197.     }
  198.  
  199.     /* create EPS file */
  200.     epsname[0] = '\0';
  201.     if (!get_filename(epsname, TRUE, FILTER_EPS, 0, IDS_TOPICEDIT)) {
  202.         GlobalFree(hglobal);
  203.         return;
  204.     }
  205.     hfEPS = _lcreat(epsname, 0);
  206.  
  207.     /* write DOS EPS binary header */
  208.     eps_header.id[0] = 0xc5;    /* "EPSF" with bit 7 set */
  209.     eps_header.id[1] = 0xd0;
  210.     eps_header.id[2] = 0xd3;
  211.     eps_header.id[3] = 0xc6;
  212.     eps_header.ps_begin = sizeof(eps_header);
  213.     fseek(psfile.file, 0, SEEK_END);
  214.     eps_header.ps_length = ftell(psfile.file);
  215.     eps_header.mf_begin = eps_header.ps_begin + eps_header.ps_length;
  216. #ifdef __WIN32__
  217.     eps_header.mf_length = size;
  218. #else
  219.     eps_header.mf_length = GlobalSize(hglobal);
  220. #endif
  221.     eps_header.tiff_begin = 0;
  222.     eps_header.tiff_length = 0;
  223.     eps_header.checksum = -1;
  224.     _lwrite(hfEPS, (void _huge *)&eps_header, sizeof(eps_header));
  225.  
  226.     /* copy PS file */
  227.     rewind(psfile.file);
  228.     do {
  229.         count = fread(buffer, 1, COPY_BUF_SIZE, psfile.file);
  230.         _lwrite(hfEPS, buffer, count);
  231.     } while (count != 0);
  232.     free(buffer);
  233.  
  234.     /* copy metafile */
  235.     lpmf = GlobalLock(hglobal);
  236.     hugewrite(hfEPS, lpmf, eps_header.mf_length);
  237.     GlobalUnlock(hglobal);
  238.     GlobalFree(hglobal);
  239.  
  240.     _lclose(hfEPS);
  241. }
  242.