home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / os2 / hpgl312.zip / TO_PIC.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  5KB  |  206 lines

  1. /*
  2.    Copyright (c) 1991 - 1993 Heinz W. Werntges.  All rights reserved.
  3.    Distributed by Free Software Foundation, Inc.
  4.  
  5. This file is part of HP2xx.
  6.  
  7. HP2xx is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  9. to anyone for the consequences of using it or for whether it serves any
  10. particular purpose or works at all, unless he says so in writing.  Refer
  11. to the GNU General Public License, Version 2 or later, for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. HP2xx, but only under the conditions described in the GNU General Public
  15. License.  A copy of this license is supposed to have been
  16. given to you along with HP2xx so you can know your rights and
  17. responsibilities.  It should be in a file named COPYING.  Among other
  18. things, the copyright notice and this notice must be preserved on all
  19. copies.
  20.  
  21. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. */
  23.  
  24. /** to_pic.c: PIC (ATARI "32k" bitmap) formatter part of project "hp2xx"
  25.  **
  26.  ** 91/08/28  V 1.00  HWW  Originating
  27.  ** 91/09/08  V 1.01  HWW  Bug fixed: Repeated block columns
  28.  ** 91/10/09  V 1.02  HWW  ANSI-C definitions; new ATARI file name convention
  29.  ** 91/10/16  V 1.03b HWW  STAD mode (output file packing) added
  30.  ** 91/10/21  V 1.03d HWW  Plain "pic", packing done by pic2pac; VAX_C active
  31.  ** 91/03/01  V 1.03e NM   Bug fixed: numbering of files was incorrect
  32.  ** 91/05/19  V 1.03f HWW  Abort if color mode
  33.  **
  34.  **          NOTE:   This code is not part of the supported modules
  35.  **              of hp2xx. Include it if needed only.
  36.  **/
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #ifdef    TURBO_C
  42.   #include <io.h>
  43. #endif
  44. #include "bresnham.h"
  45. #include "hp2xx.h"
  46.  
  47.  
  48.  
  49. #define ATARI_XRES    640    /* MUST be multiple of 8    */
  50. #define ATARI_YRES    400
  51. #define BYTES_PER_LINE    (ATARI_XRES>>3)
  52.  
  53.  
  54.  
  55.  
  56. int    Init_PIC_files (char *basename, FILE **fd, int nb, int nr, int yb)
  57. {
  58. #define    FNAME_LEN    80
  59. char    fname[FNAME_LEN], ext[8];
  60. int    i, n, yb_tot;
  61. #ifdef VAX
  62. int    hd;
  63. #endif
  64.  
  65.  
  66.   yb_tot = 1 + (nr - 1) / ATARI_YRES;    /* Total # of y blocks */
  67.  
  68.  
  69.   for (i=0; nb > 0; i++, nb -= BYTES_PER_LINE)
  70.   {
  71.     if (fd[i])
  72.     {
  73.         fclose (fd[i]);
  74.         fd[i] = NULL;
  75.     }
  76.  
  77.     n = yb + i * yb_tot;
  78.     if (n > 99)
  79.     {
  80.         fprintf (stderr,"ERROR: Too many PIC files per column!\n");
  81.         for (; i > -1; i--)
  82.             if (fd[i])
  83.             {
  84.                 fclose (fd[i]);
  85.                 fd[i] = NULL;
  86.             }
  87.         return ERROR;
  88.     }
  89.  
  90.     sprintf(ext,"%02d.pic", n);
  91.  
  92.     strcpy (fname, basename);
  93.     strncat (fname, ext, FNAME_LEN - strlen(basename) - 1);
  94.  
  95. #ifdef VAX
  96.     if ((fd[i] = fopen(fname, WRITE_BIN, "rfm=var","mrs=512")) == NULL)
  97.     {
  98. #else
  99.     if ((fd[i] = fopen(fname, WRITE_BIN)) == NULL)
  100.     {
  101. #endif
  102.         perror ("hp2xx -- opening PIC file(s)");
  103.         return ERROR;
  104.     }
  105.   }
  106.   return 0;
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113. void    PicBuf_to_PIC (PicBuf *picbuf, PAR *p)
  114. {
  115. #define    N_BLOCKS 10
  116.  
  117. FILE    *fd[N_BLOCKS];
  118. int    row_c, i, nb, nr, yb;
  119.  
  120.   if (picbuf->depth > 1)
  121.   {
  122.     fprintf(stderr, "\nPIC mode does not support colors yet -- sorry\n");
  123.     free_PicBuf (picbuf, p->swapfile);
  124.     exit (ERROR);
  125.   }
  126.  
  127.   if (picbuf->nb > (ATARI_XRES * N_BLOCKS) / 8)
  128.   {
  129.     fprintf (stderr, "hp2xx -- Too many PIC files per row");
  130.     free_PicBuf (picbuf, p->swapfile);
  131.     exit (ERROR);
  132.   }
  133.  
  134.   if (! p->quiet)
  135.     fprintf(stderr, "\nWriting PIC output: %d rows of %d bytes\n",
  136.         picbuf->nr, picbuf->nb);
  137.  
  138.   if (! *p->outfile)
  139.     p->outfile = "bitmap";        /* Default name    */
  140.  
  141.   for (i=0, nb = picbuf->nb; nb > 0; i++, nb -= BYTES_PER_LINE)
  142.     fd[i] = NULL;
  143.  
  144.  
  145.   /* Backward since highest index is lowest line on screen! */
  146.  
  147.   for (yb=nr=0, row_c = picbuf->nr - 1; row_c >= 0; nr++, row_c--)
  148.   {
  149.     if (nr % ATARI_YRES == 0)
  150.     {
  151.         if (Init_PIC_files (p->outfile, fd,
  152.                 picbuf->nb, picbuf->nr, yb))
  153.         {
  154.             free_PicBuf (picbuf, p->swapfile);
  155.             exit (ERROR);
  156.         }
  157.         yb++;
  158.     }
  159.     if ((!p->quiet) && (row_c % 10 == 0))
  160.           /* For the impatients among us ...    */
  161.         putc('.',stderr);
  162.     RowBuf_to_PIC (get_RowBuf(picbuf, row_c), picbuf->nb, fd);
  163.   }
  164.  
  165.   get_RowBuf(picbuf, 0);        /* Use row 0 for padding */
  166.   for (i=0; i < picbuf->nb; i++)    /* Clear it         */
  167.     picbuf->row[0].buf[i] = '\0';
  168.  
  169.   while (nr % ATARI_YRES != 0)
  170.   {
  171.     RowBuf_to_PIC (&picbuf->row[0], picbuf->nb, fd);
  172.     nr++;
  173.   }
  174.  
  175.  
  176.   if (!p->quiet)
  177.     fputc ('\n', stderr);
  178.  
  179.   for (i=0, nb = picbuf->nb; nb > 0; i++, nb -= BYTES_PER_LINE)
  180.   {
  181.     fclose (fd[i]);
  182.     fd[i] = NULL;
  183.   }
  184. }
  185.  
  186.  
  187.  
  188.  
  189. void    RowBuf_to_PIC (RowBuf *row, int nb, FILE **fd)
  190. {
  191. int    i,j, n_pad=0, n_wr=BYTES_PER_LINE;
  192.  
  193. /* VAX peculiarity: Writing one big object is faster than many smaller */
  194.  
  195.   if (nb % BYTES_PER_LINE)    /* padding required    */
  196.     n_pad = (nb/BYTES_PER_LINE + 1)*BYTES_PER_LINE - nb;
  197.  
  198.   for (i=0; nb > 0; i++, nb -= n_wr)
  199.     fwrite ((char *) &row->buf[i*BYTES_PER_LINE],
  200.          n_wr=MIN(nb,BYTES_PER_LINE), 1, fd[i]);
  201.  
  202.   for (i--, j=0; j < n_pad; j++)/* Fill last block with zero    */
  203.     fputc ('\0', fd[i]);
  204. }
  205.  
  206.