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

  1. /*
  2.    Copyright (c) 1991 - 1993 Claus H. Langhans.  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. /**
  25.  ** to_pbm.c: PortableBitMap (PBM) converter part of project "hp2xx"
  26.  **
  27.  ** 92/04/14  V 1.00  CHL  Originating: Copied from to_pcx.c and to_gnu.c
  28.  ** 92/04/16  V 1.01  CHL  Better error handling
  29.  ** 92/05/17  V 1.01b HWW  Output to stdout if outfile == '-'
  30.  ** 92/05/19  V 1.01c HWW  Abort if color mode
  31.  **/
  32.  
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include "bresnham.h"
  37. #include "hp2xx.h"
  38.  
  39. #define GGE >>=
  40. #define MAXOUTPUTROWS 70
  41.  
  42.  
  43.  
  44. void
  45. PicBuf_to_PBM(PicBuf * picbuf, PAR * p)
  46. {
  47. FILE           *fd;
  48. int             row_count = 0;
  49. int             row_c, byte_c, bit, x;
  50. RowBuf         *row;
  51.  
  52.   if (picbuf->depth > 1)
  53.   {
  54.     fprintf(stderr, "\nPBM mode does not support colors yet -- sorry\n");
  55.     goto ERROR_EXIT_2;
  56.   }
  57.  
  58.   if (!p->quiet)
  59.     fprintf(stderr, "\nWriting PBM output: %s\n",p->outfile);
  60.   if (*p->outfile != '-')
  61.   {
  62.  
  63. #ifdef VAX
  64.     if ((fd = fopen(p->outfile, WRITE_BIN, "rfm=var", "mrs=512")) == NULL)
  65. #else
  66.     if ((fd = fopen(p->outfile, WRITE_BIN)) == NULL)
  67. #endif
  68.         goto ERROR_EXIT;
  69.   }
  70.   else
  71.     fd = stdout;
  72.  
  73.   if (fprintf(fd, "P1\n")== EOF)
  74.     goto ERROR_EXIT;
  75.   if (fprintf(fd, "%d %d\n", (picbuf->nb) * 8, picbuf->nr)== EOF)
  76.     goto ERROR_EXIT;
  77.  
  78.   for (row_c = 0; row_c < picbuf->nr; row_c++)
  79.   {
  80.     row = get_RowBuf(picbuf, picbuf->nr - row_c - 1);
  81.  
  82.     for (byte_c = x = 0; byte_c < picbuf->nb; byte_c++)
  83.     {
  84.         for (bit = 128; bit; bit GGE 1, x++)
  85.         if (bit & row->buf[byte_c])
  86.         {
  87.             if(putc('1', fd)== EOF) goto ERROR_EXIT;
  88.             row_count++;
  89.             if (row_count >= MAXOUTPUTROWS)
  90.             {
  91.             row_count = 0;
  92.             if(putc('\n', fd)== EOF) goto ERROR_EXIT;
  93.             }
  94.         }
  95.         else
  96.         {
  97.             putc('0', fd);
  98.             row_count++;
  99.             if (row_count >= MAXOUTPUTROWS)
  100.             {
  101.             row_count = 0;
  102.             if(putc('\n', fd)== EOF) goto ERROR_EXIT;
  103.             }
  104.         }
  105.     }
  106.     if ((!p->quiet) && (row_c % 10 == 0))
  107.         /* For the impatients among us ...     */
  108.         putc('.', stderr);
  109.     row_count = 0;
  110.     putc('\n', fd);
  111.   }
  112.  
  113.   if (!p->quiet)
  114.     fputc('\n', stderr);
  115.   if (fd != stdout)
  116.     fclose(fd);
  117.   return;
  118.  
  119. ERROR_EXIT:
  120.   perror      ("write_PBM");
  121. ERROR_EXIT_2:
  122.   free_PicBuf (picbuf, p->swapfile);
  123.   exit          (ERROR);
  124. }
  125.