home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <varargs.h>
-
- #ifndef lint
- char SCCSID[] = "@(#)pbm2e24.c: 1.1 91/12/03 00:34:50";
- #endif
-
- #ifndef TRUE
- #define TRUE 1
- #define FALSE 0
- #endif
-
- /*
- version 1.00, Copyright (C) 1991, klaus schallhorn, <klaus@cnix.uucp>
-
- Permission to use, copy, modify, and distribute this software and its
- documentation for any purpose and without fee is hereby granted, provided
- that the above copyright notice appear in all copies and that both that
- copyright notice and this permission notice appear in supporting
- documentation. This software is provided "as is" without express or
- implied warranty.
- */
-
- #ifndef min
- #define min(a,b) ((a<b)?a:b)
- #define max(a,b) ((a>b)?a:b)
- #endif
-
- unsigned char io[512];
- unsigned char pixrow[24][512];
- unsigned char bitval[]=
- {
- 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01,
- 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01,
- 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
- };
-
- die(va_alist)
- va_dcl
- {
- va_list args;
- char *fmt;
-
- va_start(args);
- fmt = va_arg(args, char *);
- fprintf(stderr,"pbmtoe24: ");
- vfprintf(stderr, fmt, args);
- va_end(args);
- exit(1);
- }
- main(ac,av)
- int ac;
- char *av[];
- {
- FILE *in;
- int cols, rows, row, eof, i, j, k, maxcol;
- unsigned char a;
- char *fname;
-
- in = NULL;
- if (ac == 2)
- {
- if ((in = fopen(av[1], "r")) == NULL)
- die("can't open %s\n",av[1]);
- fname = av[1];
- }
- if (in == NULL)
- {
- if (isatty(fileno(stdin)))
- die(" need infile [or stdin]\n");
- in = stdin;
- fname = "(stdin)";
- }
- if (fgets((char*)io, 20, in) == NULL || strncmp(io, "P4", 2))
- die("%s doesn't look like a pbm file\n",fname);
- for (;;)
- {
- if (fgets((char*)io, 256, in) == NULL)
- die("%s doesn't look like a pbm file\n",fname);
- if (io[0] != '#')
- {
- if (sscanf(io, "%d %d",&cols,&rows) != 2)
- die("%s doesn't look like a pbm file\n",fname);
- break;
- }
- }
- if (!(cols % 8))
- cols /= 8;
- else cols = 1+(cols/8);
-
- printf("\033@\0333\030");
- /* ESC @ to sane printer */
- /* ESC 3 \030 24/180 line spacing */
-
- for (eof=row=0; ; )
- {
- for (i=0; i<24 && row<rows; i++, row++)
- {
- if (fread(pixrow[i], cols, 1, in) != 1)
- {
- eof = TRUE;
- break;
- }
- }
- if (i < 24)
- {
- for (; i<24; i++)
- memset(pixrow[i], '\0', cols);
- eof = TRUE;
- }
- /* trim white space */
- for (maxcol = cols-1; maxcol >= 0; --maxcol)
- if (pixrow[0][maxcol] ||
- pixrow[1][maxcol] || pixrow[2][maxcol] ||
- pixrow[3][maxcol] || pixrow[4][maxcol] ||
- pixrow[5][maxcol] || pixrow[6][maxcol] ||
- pixrow[7][maxcol] || pixrow[8][maxcol] ||
- pixrow[9][maxcol] || pixrow[10][maxcol] ||
- pixrow[11][maxcol] || pixrow[12][maxcol] ||
- pixrow[13][maxcol] || pixrow[14][maxcol] ||
- pixrow[15][maxcol] || pixrow[16][maxcol] ||
- pixrow[17][maxcol] || pixrow[18][maxcol] ||
- pixrow[19][maxcol] || pixrow[20][maxcol] ||
- pixrow[21][maxcol] || pixrow[22][maxcol] ||
- pixrow[23][maxcol])
- break;
-
- if (maxcol >= 0)
- {
- ++maxcol;
- printf("\033*'");
- putc((maxcol*8)%256,stdout);
- putc((maxcol*8)/256,stdout);
- for (j=0; j<=maxcol; j++)
- {
- for (k=0; k<8; k++)
- {
- a = '\0';
- for (i=0; i<8; i++)
- if (pixrow[i][j] & bitval[k])
- a |= bitval[i];
- putc(a,stdout);
-
- a = '\0';
- for (; i<16; i++)
- if (pixrow[i][j] & bitval[k])
- a |= bitval[i];
- putc(a,stdout);
-
- a = '\0';
- for (; i<24; i++)
- if (pixrow[i][j] & bitval[k])
- a |= bitval[i];
- putc(a, stdout);
- }
- }
- }
- putc('\n', stdout);
- if (eof)
- break;
- }
- printf("\f\033@");
- exit(0);
- }
-