home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume26 / hp2pbm / part02 / pbm2e24.c < prev   
Encoding:
C/C++ Source or Header  |  1991-12-11  |  3.5 KB  |  165 lines

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