home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3937 / pbm2e24.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-30  |  3.3 KB  |  156 lines

  1. #include <stdio.h>
  2. #include <varargs.h>
  3.  
  4. /*
  5.     version 1.00, Copyright (C) 1991, klaus schallhorn, <klaus@cnix.uucp>
  6.  
  7.     Permission to use, copy, modify, and distribute this software and its
  8.     documentation for any purpose and without fee is hereby granted, provided
  9.     that the above copyright notice appear in all copies and that both that
  10.     copyright notice and this permission notice appear in supporting
  11.     documentation.  This software is provided "as is" without express or
  12.     implied warranty.
  13. */
  14.  
  15. #ifndef    min
  16. #define    min(a,b)    ((a<b)?a:b)
  17. #define    max(a,b)    ((a>b)?a:b)
  18. #endif
  19.  
  20. unsigned char io[512];
  21. unsigned char pixrow[24][512];
  22. unsigned char bitval[]=
  23. {
  24.     0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01,
  25.     0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01,
  26.     0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  27. };
  28.  
  29. die(va_alist)
  30. va_dcl
  31. {
  32.     va_list args;
  33.     char *fmt;
  34.  
  35.     va_start(args);
  36.     fmt = va_arg(args, char *);
  37.     fprintf(stderr,"pbmtoe24: ");
  38.     vfprintf(stderr, fmt, args);
  39.     va_end(args);
  40.     exit(1);
  41. }
  42. main(ac,av)
  43. int ac;
  44. char *av[];
  45. {
  46.     FILE *in;
  47.     int cols, rows, row, eof, i, j, k, maxcol;
  48.     unsigned char a;
  49.     char *fname;
  50.  
  51.     in = NULL;
  52.     if (ac == 2)
  53.     {
  54.         if ((in = fopen(av[1], "r")) == NULL)
  55.             die("can't open %s\n",av[1]);
  56.         fname = av[1];
  57.     }
  58.     if (in == NULL)
  59.     {
  60.         if (isatty(fileno(stdin)))
  61.             die(" need infile [or stdin]\n");
  62.         in = stdin;
  63.         fname = "(stdin)";
  64.     }
  65.     if (fgets((char*)io, 20, in) == NULL || strncmp(io, "P4", 2))
  66.         die("%s doesn't look like a pbm file\n",fname);
  67.     for (;;)
  68.     {
  69.         if (fgets((char*)io, 256, in) == NULL)
  70.             die("%s doesn't look like a pbm file\n",fname);
  71.         if (io[0] != '#')
  72.         {
  73.             if (sscanf(io, "%d %d",&cols,&rows) != 2)
  74.                 die("%s doesn't look like a pbm file\n",fname);
  75.             break;
  76.         }
  77.     }
  78.     if (!(cols % 8))
  79.         cols /= 8;
  80.     else cols = 1+(cols/8);
  81.  
  82.     printf("\033@\0333\030");
  83.     /* ESC @ to sane printer */
  84.     /* ESC 3 \030 24/180 line spacing */
  85.  
  86.     for (eof=row=0; ; )
  87.     {
  88.         for (i=0; i<24 && row<rows; i++, row++)
  89.         {
  90.             if (fread(pixrow[i], cols, 1, in) != 1)
  91.             {
  92.                 eof = TRUE;
  93.                 break;
  94.             }
  95.         }
  96.         if (i < 24)
  97.         {
  98.             for (; i<24; i++)
  99.                 memset(pixrow[i], '\0', cols);
  100.             eof = TRUE;
  101.         }
  102.                         /* trim white space */
  103.         for (maxcol = cols-1; maxcol >= 0; --maxcol)
  104.             if (pixrow[0][maxcol] || 
  105.                 pixrow[1][maxcol] || pixrow[2][maxcol] || 
  106.                 pixrow[3][maxcol] || pixrow[4][maxcol] || 
  107.                 pixrow[5][maxcol] || pixrow[6][maxcol] || 
  108.                 pixrow[7][maxcol] || pixrow[8][maxcol] || 
  109.                 pixrow[9][maxcol] || pixrow[10][maxcol] || 
  110.                 pixrow[11][maxcol] || pixrow[12][maxcol] || 
  111.                 pixrow[13][maxcol] || pixrow[14][maxcol] || 
  112.                 pixrow[15][maxcol] || pixrow[16][maxcol] || 
  113.                 pixrow[17][maxcol] || pixrow[18][maxcol] || 
  114.                 pixrow[19][maxcol] || pixrow[20][maxcol] || 
  115.                 pixrow[21][maxcol] || pixrow[22][maxcol] || 
  116.                 pixrow[23][maxcol])
  117.                 break;
  118.  
  119.         if (maxcol >= 0)
  120.         {
  121.             ++maxcol;
  122.             printf("\033*'");
  123.             putc((maxcol*8)%256,stdout);
  124.             putc((maxcol*8)/256,stdout);
  125.             for (j=0; j<=maxcol; j++)
  126.             {
  127.                 for (k=0; k<8; k++)
  128.                 {
  129.                     a = '\0';
  130.                     for (i=0; i<8; i++)
  131.                         if (pixrow[i][j] & bitval[k])
  132.                             a |= bitval[i];
  133.                     putc(a,stdout);
  134.  
  135.                     a = '\0';
  136.                     for (; i<16; i++)
  137.                         if (pixrow[i][j] & bitval[k])
  138.                             a |= bitval[i];
  139.                     putc(a,stdout);
  140.  
  141.                     a = '\0';
  142.                     for (; i<24; i++)
  143.                         if (pixrow[i][j] & bitval[k])
  144.                             a |= bitval[i];
  145.                     putc(a, stdout);
  146.                 }
  147.             }
  148.         }
  149.         putc('\n', stdout);
  150.         if (eof)
  151.             break;
  152.     }
  153.     printf("\f\033@");
  154.     exit(0);
  155. }
  156.