home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gsview13 / src / gvweps.c < prev    next >
C/C++ Source or Header  |  1995-12-09  |  7KB  |  243 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. #include "gvceps.h"
  23.  
  24.  
  25. HGLOBAL make_dib(void);  /* in gvwclip.c */
  26.  
  27. static HGLOBAL get_bitmap_hglobal;
  28. static BOOL get_bitmap_made_dib = FALSE;
  29.  
  30. LPBITMAP2
  31. get_bitmap()
  32. {
  33.     get_bitmap_hglobal = 0;
  34.     get_bitmap_made_dib = FALSE;
  35.     if (!OpenClipboard(hwndimg))
  36.         return NULL;
  37.     if (IsClipboardFormatAvailable(CF_DIB))
  38.         get_bitmap_hglobal = GetClipboardData(CF_DIB);
  39.     else if (IsClipboardFormatAvailable(CF_BITMAP)) {
  40.         /* untested */
  41.         get_bitmap_hglobal = make_dib(); /* convert to DIB format */
  42.         if (get_bitmap_hglobal == (HGLOBAL)NULL) {
  43.             CloseClipboard();
  44.         return NULL;
  45.         }
  46.         get_bitmap_made_dib = TRUE;
  47.     }
  48.     else {
  49.         CloseClipboard();
  50.         return NULL;
  51.     }
  52.     return (LPBITMAP2)GlobalLock(get_bitmap_hglobal);
  53. }
  54.  
  55. void
  56. release_bitmap()
  57. {
  58.     GlobalUnlock(get_bitmap_hglobal);
  59.     if (get_bitmap_made_dib)
  60.         GlobalFree(get_bitmap_hglobal);
  61.     CloseClipboard();
  62. }
  63.  
  64. #ifdef __WIN32__
  65. long
  66. hugewrite(HFILE hf, const void _huge *hpvBuffer, long cbBuffer)
  67. {
  68.         return _hwrite(hf, hpvBuffer, cbBuffer);
  69. }
  70. #else
  71. /* Write data to file - blocks > 64k permitted */
  72. long
  73. hugewrite(HFILE hf, const void _huge *hpvBuffer, long cbBuffer)
  74. {
  75. DWORD count;
  76. long written, done;
  77. char _huge *hp;
  78.     if (is_win31)
  79.         return _hwrite(hf, hpvBuffer, cbBuffer);
  80.     done = 0;
  81.     hp = (char _huge *)hpvBuffer;
  82.     while (cbBuffer > 0) {
  83.         count = min( min(32768UL, cbBuffer), (DWORD)(65536UL-OFFSETOF(hp)) );
  84.         written = _lwrite(hf, hp, (UINT)count);
  85.         if (written == (long)HFILE_ERROR)
  86.         return (long)HFILE_ERROR;
  87.         done += written;
  88.         cbBuffer -= written;
  89.         hp += written;
  90.     }
  91.     return done;
  92. }
  93. #endif
  94.  
  95. /* convert a clipboard bitmap to a metafile picture */
  96. HMETAFILE
  97. make_metafile(void)
  98. {
  99. HDC hdc;
  100. HMETAFILE hmf;
  101. HGLOBAL hglobal;
  102.     if (IsClipboardFormatAvailable(CF_DIB)) {
  103.         LPBITMAPINFOHEADER pbmih;
  104.         BYTE _huge *lpDibBits;
  105.         hglobal = GetClipboardData(CF_DIB);
  106.         pbmih = (LPBITMAPINFOHEADER)GlobalLock(hglobal);
  107.         lpDibBits = ((BYTE _huge *)pbmih) + pbmih->biSize;
  108.         if (pbmih->biSize == sizeof(BITMAPCOREHEADER))
  109.         lpDibBits += dib_pal_colors((LPBITMAP2)pbmih) * sizeof(RGBTRIPLE); 
  110.         else
  111.         lpDibBits += dib_pal_colors((LPBITMAP2)pbmih) * sizeof(RGBQUAD); 
  112.         /* now make a Metafile from it */
  113.         hdc = CreateMetaFile(NULL);
  114.         SetWindowOrg(hdc, 0, 0);
  115.         SetWindowExt(hdc, (int)pbmih->biWidth, (int)pbmih->biHeight);
  116.         StretchDIBits(hdc, 0, 0, (int)pbmih->biWidth, (int)pbmih->biHeight,
  117.             0, 0, (int)pbmih->biWidth, (int)pbmih->biHeight,
  118.             (void FAR *)lpDibBits, (LPBITMAPINFO)pbmih,
  119.             DIB_RGB_COLORS, SRCCOPY);
  120.         hmf = CloseMetaFile(hdc);
  121.         GlobalUnlock(hglobal);
  122.     }
  123.     else if (IsClipboardFormatAvailable(CF_BITMAP)) {
  124.         HBITMAP hbitmap, hbitmap_old;
  125.         HDC hdc_bit;
  126.         BITMAP bm;
  127.         hbitmap = GetClipboardData(CF_BITMAP);
  128.         hdc = GetDC((HWND)NULL);
  129.         hdc_bit = CreateCompatibleDC(hdc);
  130.         ReleaseDC((HWND)NULL,hdc);
  131.         GetObject(hbitmap, sizeof(BITMAP), &bm);
  132.         hdc = CreateMetaFile(NULL);
  133.         SetWindowOrg(hdc, 0, 0);
  134.         SetWindowExt(hdc, bm.bmWidth, bm.bmHeight);
  135.         hbitmap_old = SelectBitmap(hdc_bit, hbitmap);
  136.         StretchBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight,
  137.             hdc_bit, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  138.         SelectBitmap(hdc_bit, hbitmap_old);
  139.         DeleteDC(hdc_bit);
  140.         hmf = CloseMetaFile(hdc);
  141.     }
  142.     else {
  143.         play_sound(SOUND_ERROR);
  144.         hmf = NULL;
  145.     }
  146.     return hmf;
  147. }
  148.  
  149. /* make a PC EPS file with a Windows Metafile Preview */
  150. /* from a PS file and a clipboard bitmap */
  151. void
  152. make_eps_metafile(void)
  153. {
  154. char epsname[MAXSTR];
  155. HMETAFILE hmf;
  156. HGLOBAL hglobal;
  157. char *buffer;
  158. UINT count;
  159. HFILE hfEPS;
  160. struct eps_header_s eps_header;
  161. #ifdef __WIN32__
  162. UINT size;
  163. LPCVOID lpmf;
  164. #else
  165. LPSTR lpmf;
  166. #endif
  167.  
  168.     if (!OpenClipboard(hwndimg)) {
  169.         play_sound(SOUND_ERROR);
  170.         return;
  171.     }
  172.  
  173.     if ( (hmf = make_metafile()) == (HMETAFILE)NULL ) {
  174.         play_sound(SOUND_ERROR);
  175.         CloseClipboard();
  176.         return;
  177.     }
  178.  
  179.     CloseClipboard();
  180.  
  181.     /* get memory handle to metafile */
  182. #ifdef __WIN32__
  183.     size = GetMetaFileBitsEx(hmf, 0, NULL);
  184.     hglobal = GlobalAlloc(GHND, size);
  185.     lpmf = GlobalLock(hglobal);
  186.     GetMetaFileBitsEx(hmf, size, lpmf);
  187.     GlobalUnlock(hglobal);
  188. #else
  189.     hglobal = GetMetaFileBits(hmf);
  190. #endif
  191.  
  192.     /* create buffer for PS file copy */
  193.     buffer = malloc(COPY_BUF_SIZE);
  194.     if (buffer == (char *)NULL) {
  195.         GlobalFree(hglobal);
  196.         gserror(IDS_BADEPS, NULL, MB_ICONEXCLAMATION, SOUND_ERROR);
  197.         return;
  198.     }
  199.  
  200.     /* create EPS file */
  201.     epsname[0] = '\0';
  202.     if (!get_filename(epsname, TRUE, FILTER_EPS, 0, IDS_TOPICEDIT)) {
  203.         GlobalFree(hglobal);
  204.         return;
  205.     }
  206.     hfEPS = _lcreat(epsname, 0);
  207.  
  208.     /* write DOS EPS binary header */
  209.     eps_header.id[0] = 0xc5;    /* "EPSF" with bit 7 set */
  210.     eps_header.id[1] = 0xd0;
  211.     eps_header.id[2] = 0xd3;
  212.     eps_header.id[3] = 0xc6;
  213.     eps_header.ps_begin = sizeof(eps_header);
  214.     fseek(psfile.file, 0, SEEK_END);
  215.     eps_header.ps_length = ftell(psfile.file);
  216.     eps_header.mf_begin = eps_header.ps_begin + eps_header.ps_length;
  217. #ifdef __WIN32__
  218.     eps_header.mf_length = size;
  219. #else
  220.     eps_header.mf_length = GlobalSize(hglobal);
  221. #endif
  222.     eps_header.tiff_begin = 0;
  223.     eps_header.tiff_length = 0;
  224.     eps_header.checksum = -1;
  225.     _lwrite(hfEPS, (void _huge *)&eps_header, sizeof(eps_header));
  226.  
  227.     /* copy PS file */
  228.     rewind(psfile.file);
  229.     do {
  230.         count = fread(buffer, 1, COPY_BUF_SIZE, psfile.file);
  231.         _lwrite(hfEPS, buffer, count);
  232.     } while (count != 0);
  233.     free(buffer);
  234.  
  235.     /* copy metafile */
  236.     lpmf = GlobalLock(hglobal);
  237.     hugewrite(hfEPS, lpmf, eps_header.mf_length);
  238.     GlobalUnlock(hglobal);
  239.     GlobalFree(hglobal);
  240.  
  241.     _lclose(hfEPS);
  242. }
  243.