home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sources.misc
- From: tommasso@hacktic.nl (Thomas Okken)
- Subject: v40i130: pbmtoiw - convert portable bitmap to Apple Imagewriter graphics, Part01/01
- Message-ID: <1993Nov14.032205.1697@sparky.sterling.com>
- X-Md4-Signature: 4ea3c8f8cc223a7bc97eb98d9b4b504b
- Sender: kent@sparky.sterling.com (Kent Landfield)
- Organization: Hack-Tic, networking for the masses
- Date: Sun, 14 Nov 1993 03:22:05 GMT
- Approved: kent@sparky.sterling.com
-
- Submitted-by: tommasso@hacktic.nl (Thomas Okken)
- Posting-number: Volume 40, Issue 130
- Archive-name: pbmtoiw/part01
- Environment: pbm
-
- Pbmtoiw reads a portable bitmap and converts it to graphics codes for the
- Apple Imagewriter printer. You can use it to print a bitmap by saying
- "pbmtoiw picture.pbm | lpr".
- I tested it with an Imageriter I; I see no reason why it shouldn't work with
- the Imagewriter II as well.
-
- - Thomas.
- ---
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then feed it
- # into a shell via "sh file" or similar. To overwrite existing files,
- # type "sh file -c".
- # Contents: README iw.setup pbmtoiw.1 pbmtoiw.c
- # Wrapped by kent@sparky on Sat Nov 13 21:19:44 1993
- PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin:$PATH ; export PATH
- echo If this archive is complete, you will see the following message:
- echo ' "shar: End of archive 1 (of 1)."'
- if test -f 'README' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'README'\"
- else
- echo shar: Extracting \"'README'\" \(991 characters\)
- sed "s/^X//" >'README' <<'END_OF_FILE'
- XPbmtoiw reads a portable bitmap and converts it to graphics codes for the
- XApple Imagewriter printer. You can use it to print a bitmap by saying
- X"pbmtoiw picture.pbm | lpr".
- X
- XBuild/install instructions:
- X* If you don't like "/usr/local/lib/iw.setup" as the configuration file,
- X change the #define in pbmtoiw.c.
- X* Compile pbmtoiw.c: "cc -o pbmtoiw pbmtoiw.c". Any C compiler should work,
- X ANSI is not required. Optimize to taste.
- X* Put the executable and the manpage in useful places. "/usr/bin" and
- X "/usr/man/man1" would be obvious choices on most systems.
- X* Change the configuration file to match your preferences. The manpage tells
- X you how. The default file supplied here is set for 12" paper, 1" of tear-off
- X margin around the paper, and don't leave tear-off margin between vertically
- X adjacent pages (i.e., join pages = 1).
- X Put the configuration file where the #define (from the first step) says
- X it is.
- X
- XWritten by Thomas Okken (thomas@animal.hobby.nl or tommasso@hacktic.nl).
- END_OF_FILE
- if test 991 -ne `wc -c <'README'`; then
- echo shar: \"'README'\" unpacked with wrong size!
- fi
- # end of 'README'
- fi
- if test -f 'iw.setup' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'iw.setup'\"
- else
- echo shar: Extracting \"'iw.setup'\" \(9 characters\)
- sed "s/^X//" >'iw.setup' <<'END_OF_FILE'
- X864 72 1
- END_OF_FILE
- if test 9 -ne `wc -c <'iw.setup'`; then
- echo shar: \"'iw.setup'\" unpacked with wrong size!
- fi
- # end of 'iw.setup'
- fi
- if test -f 'pbmtoiw.1' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'pbmtoiw.1'\"
- else
- echo shar: Extracting \"'pbmtoiw.1'\" \(1514 characters\)
- sed "s/^X//" >'pbmtoiw.1' <<'END_OF_FILE'
- X.TH pbmtoiw 1 "11 November 1993"
- X.SH NAME
- Xpbmtoiw - convert a portable bitmap into Apple Imagewriter graphics
- X.SH SYNOPSIS
- X.B pbmtoiw
- X.RI [-< hres >x< vres >]
- X.RI [ pbmfile ]
- X.SH DESCRIPTION
- XReads a portable bitmap as input.
- XProduces Apple Imagewriter graphics escape sequences as output.
- X.PP
- XNote that there is no iwtopbm tool - this transformation is one way.
- X.SH OPTIONS
- X.TP
- X.RI -< hres >x< vres >
- XSpecifies the horizontal and vertical resolutions at which to print, in
- Xdpi. Possible values for
- X.I hres
- Xare 72, 80, 96, 107, 120, 136, 144 and 160; for
- X.I vres
- Xyou can choose between 72 (non-interleaved) and 144 (interleaved).
- XThe default is hres = vres = 72 dpi.
- X.SH CONFIGURATION
- XSince paper sizes are not the same everywhere,
- X.B pbmtoiw
- Xneeds to know what paper size you actually use. It gets this information
- Xfrom the
- X.B iw.setup
- Xfile. This file contains one line of text, containing three numbers.
- XThe first is the total page height, from perforation to perforation. The
- Xsecond is the amount of space to be left blank around the perforation.
- XThe third is a boolean, specifying whether vertically adjacent pages
- Xshould be printed as one, across the perforation: 0 = don't join pages,
- X1 = join pages.
- X.PP
- XThe page height and the amount of space around the perforation are specified
- Xin units of 1/72".
- X.SH FILES
- X.PD 0
- X.TP 20
- X.B /usr/local/lib/iw.setup
- Xpaper geometry configuration file
- X
- X.SH "SEE ALSO"
- X.BR pbm (5)
- X
- X.SH AUTHOR
- XCopyright (C) 1993 by Thomas Okken (thomas@animal.hobby.nl or
- Xtommasso@hacktic.nl).
- END_OF_FILE
- if test 1514 -ne `wc -c <'pbmtoiw.1'`; then
- echo shar: \"'pbmtoiw.1'\" unpacked with wrong size!
- fi
- # end of 'pbmtoiw.1'
- fi
- if test -f 'pbmtoiw.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'pbmtoiw.c'\"
- else
- echo shar: Extracting \"'pbmtoiw.c'\" \(13607 characters\)
- sed "s/^X//" >'pbmtoiw.c' <<'END_OF_FILE'
- X/* pbmtoiw -- Convert a portable bitmap into Apple Imagewriter printer
- X graphics. Use: "pbmtoiw [-<hres>x<vres>] [pbmfile]". Hres is one
- X of 72, 80, 96, 107, 120, 136, 144 or 160; vres is 72 or 144. The
- X default is "-72x72". If the pbmfile argument is absent, pbmtoiw
- X reads standard input. The Imagewriter graphics are always sent to
- X standard output. */
- X
- X#include <stdio.h>
- X#include <ctype.h>
- X#include <malloc.h>
- X
- X/* -------------------------------------------------------------------------
- X Some miscellaneous defines
- X ------------------------------------------------------------------------- */
- X
- X#define SETUP_FILE "/usr/local/lib/iw.setup"
- X#define FIELDLENGTH 10
- X#define FALSE 0
- X#define TRUE 1
- X#define PBM_ASCII 0
- X#define PBM_RAW 1
- X
- X/* -------------------------------------------------------------------------
- X These globals are initialized by read_setup_file()
- X ------------------------------------------------------------------------- */
- X
- Xint paper_height, tearoff_margin, join_pages;
- X
- X/* -------------------------------------------------------------------------
- X These globals are initialized by read_command_line()
- X ------------------------------------------------------------------------- */
- X
- XFILE *input_file;
- Xint pix_per_line, interleaved;
- Xchar esc_char;
- X
- X/* -------------------------------------------------------------------------
- X These globals are used to parse the "-<hres>x<vres>" option
- X ------------------------------------------------------------------------- */
- X
- X#define NUM_RESOLUTIONS 16
- X#define MAX_PIX_PER_LINE 1280
- X
- Xchar *RES_OPTION[] =
- X{
- X "72x72", "80x72", "96x72", "107x72", "120x72", "136x72", "144x72",
- X "160x72", "72x144", "80x144", "96x144", "107x144", "120x144",
- X "136x144", "144x144", "160x144"
- X};
- X
- Xint PIX_PER_LINE[] =
- X{
- X 576, 640, 768, 856, 960, 1088, 1152, 1280,
- X 576, 640, 768, 856, 960, 1088, 1152, 1280
- X};
- X
- Xint INTERLEAVED[] =
- X{
- X FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
- X TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE
- X};
- X
- Xchar ESC_CHAR[] = "nNEeqQpPnNEeqQpP";
- X
- X/* -------------------------------------------------------------------------
- X Other globals
- X ------------------------------------------------------------------------- */
- X
- Xint image_type, image_width, image_height, original_height, bytes_per_line;
- Xunsigned char *bitmap;
- Xint delayed_feed = 0;
- X
- X
- X/* -------------------------------------------------------------------------
- X Read paper setup parameters from file
- X ------------------------------------------------------------------------- */
- X
- Xvoid read_setup_file()
- X{
- X FILE *setup_file;
- X int n;
- X
- X if ((setup_file = fopen(SETUP_FILE, "r")) == NULL)
- X {
- X fprintf(stderr, "pbmtoiw: can't open %s\n", SETUP_FILE);
- X exit(1);
- X }
- X n = fscanf(setup_file, "%d %d %d", &paper_height, &tearoff_margin,
- X &join_pages);
- X fclose(setup_file);
- X if (n != 3)
- X {
- X fprintf(stderr, "pbmtoiw: error reading %s\n", SETUP_FILE);
- X exit(1);
- X }
- X}
- X
- X/* -------------------------------------------------------------------------
- X Free education
- X ------------------------------------------------------------------------- */
- X
- Xvoid syntax_abort()
- X{
- X fputs("usage: pbmtoiw [-<hres>x<vres>] [pbmfile]\n", stderr);
- X fputs(" hres = 72, 80, 96, 107, 120, 136, 144 or 160\n", stderr);
- X fputs(" vres = 72 or 144\n", stderr);
- X fputs(" default: hres = vres = 72\n", stderr);
- X exit(1);
- X}
- X
- X/* -------------------------------------------------------------------------
- X Parse command line arguments
- X ------------------------------------------------------------------------- */
- X
- Xvoid read_command_line(argc, argv)
- X int argc;
- X char *argv[];
- X{
- X int i, res_opt = FALSE, res_index = 0, file_arg = 0;
- X
- X for (i = 1; i < argc; i++)
- X if (argv[i][0] == '-')
- X {
- X if (res_opt)
- X syntax_abort();
- X res_index = 0;
- X while (res_index < NUM_RESOLUTIONS &&
- X strcmp(argv[i] + 1, RES_OPTION[res_index]))
- X res_index++;
- X if (res_index == NUM_RESOLUTIONS)
- X syntax_abort();
- X res_opt = TRUE;
- X }
- X else
- X {
- X if (file_arg)
- X syntax_abort();
- X file_arg = i;
- X }
- X
- X pix_per_line = PIX_PER_LINE[res_index];
- X interleaved = INTERLEAVED[res_index];
- X esc_char = ESC_CHAR[res_index];
- X
- X if (file_arg)
- X {
- X input_file = fopen(argv[file_arg], "r");
- X if (input_file == NULL)
- X {
- X fprintf(stderr, "pbmtoiw: can't open %s\n",
- X argv[file_arg]);
- X exit(1);
- X }
- X }
- X else
- X input_file = stdin;
- X}
- X
- X/* -------------------------------------------------------------------------
- X Read a character from the PBM stream, skipping comments
- X ------------------------------------------------------------------------- */
- X
- Xint get_a_char()
- X{
- X int c, comment = FALSE;
- X
- X do
- X {
- X if ((c = getc(input_file)) == EOF)
- X {
- X fputs("pbmtoiw: unexpected end of file\n", stderr);
- X exit(1);
- X }
- X if (c == '#')
- X comment = TRUE;
- X else if (c == '\n' || c == '\r')
- X comment = FALSE;
- X }
- X while (comment);
- X return c;
- X}
- X
- X/* -------------------------------------------------------------------------
- X Read a whitespace-delimited string from the PBM stream
- X ------------------------------------------------------------------------- */
- X
- Xvoid get_a_string(s, n)
- X char *s;
- X int n;
- X{
- X int pos = 0, c;
- X
- X do
- X c = get_a_char();
- X while (isspace(c));
- X s[pos] = c;
- X do
- X {
- X c = get_a_char();
- X if (pos < n - 1)
- X s[++pos] = c;
- X }
- X while (!isspace(c));
- X s[pos] = 0;
- X}
- X
- X/* -------------------------------------------------------------------------
- X Read a non-blank character from the PBM stream, skipping comments
- X ------------------------------------------------------------------------- */
- X
- Xint get_a_nonspace_char()
- X{
- X int c;
- X
- X while (isspace(c = get_a_char()));
- X return c;
- X}
- X
- X/* -------------------------------------------------------------------------
- X Read PBM header
- X ------------------------------------------------------------------------- */
- X
- Xvoid read_header()
- X{
- X char field[FIELDLENGTH];
- X
- X /* Get image type */
- X get_a_string(field, FIELDLENGTH);
- X if (!strcmp(field, "P1"))
- X image_type = PBM_ASCII;
- X else if (!strcmp(field, "P4"))
- X image_type = PBM_RAW;
- X else
- X {
- X fputs("pbmtoiw: unsupported image type\n", stderr);
- X exit(1);
- X }
- X
- X /* Get image dimensions */
- X get_a_string(field, FIELDLENGTH);
- X image_width = atoi(field);
- X get_a_string(field, FIELDLENGTH);
- X original_height = atoi(field);
- X
- X /* Vertical padding to simplify printing algorithm */
- X if (interleaved)
- X image_height = (original_height + 15) & -16;
- X else
- X image_height = (original_height + 7) & -8;
- X}
- X
- X/* -------------------------------------------------------------------------
- X Allocate memory for bitmap image
- X ------------------------------------------------------------------------- */
- X
- Xvoid alloc_bitmap()
- X{
- X int bitmap_size;
- X
- X /* Lines are padded out to an even byte boundary */
- X bytes_per_line = ((image_width + 15) >> 3) & -2;
- X
- X /* Allocate bitmap and initialize it to all zeros */
- X bitmap_size = bytes_per_line * image_height;
- X bitmap = (unsigned char *) malloc(bitmap_size);
- X if (bitmap == NULL)
- X {
- X fputs("pbmtoiw: out of memory\n", stderr);
- X exit(1);
- X }
- X memset(bitmap, 0, bitmap_size);
- X}
- X
- X/* -------------------------------------------------------------------------
- X Read ascii PBM file
- X ------------------------------------------------------------------------- */
- X
- Xvoid read_pbm_ascii()
- X{
- X unsigned char in_chr, cur_chr, cur_bit;
- X int row, column, pos = 0;
- X
- X pos = 0;
- X for (row = 0; row < original_height; row++)
- X {
- X cur_bit = 0x80;
- X cur_chr = 0;
- X for (column = 0; column < image_width; column++)
- X {
- X in_chr = get_a_nonspace_char();
- X if (in_chr == '1')
- X cur_chr |= cur_bit;
- X else if (in_chr != '0')
- X {
- X fputs("pbmtoiw: pbm format error\n", stderr);
- X exit(1);
- X }
- X cur_bit >>= 1;
- X if (!cur_bit)
- X {
- X bitmap[pos++] = cur_chr;
- X cur_bit = 0x80;
- X cur_chr = 0;
- X }
- X }
- X if (cur_bit != 0x80)
- X bitmap[pos++] = cur_chr;
- X if (pos & 1)
- X pos++;
- X }
- X}
- X
- X/* -------------------------------------------------------------------------
- X Read raw PBM file
- X ------------------------------------------------------------------------- */
- X
- Xvoid read_pbm_raw()
- X{
- X int row, count, pos = 0, bytes_per_pbm_line;
- X
- X bytes_per_pbm_line = (image_width + 7) >> 3;
- X for (row = 0; row < original_height; row++)
- X {
- X count = fread(&bitmap[pos], 1, bytes_per_pbm_line, input_file);
- X if (count != bytes_per_pbm_line)
- X {
- X fputs("pbmtoiw: pbm format error\n", stderr);
- X exit(1);
- X }
- X pos += bytes_per_line;
- X }
- X}
- X
- X/* -------------------------------------------------------------------------
- X Slurp bitmap into core
- X ------------------------------------------------------------------------- */
- X
- Xvoid read_file()
- X{
- X switch (image_type)
- X {
- X case PBM_ASCII:
- X read_pbm_ascii();
- X break;
- X
- X case PBM_RAW:
- X read_pbm_raw();
- X break;
- X
- X default:
- X fputs("pbmtoiw: this can't happen\n", stderr);
- X exit(1);
- X }
- X}
- X
- X/* -------------------------------------------------------------------------
- X Perform all delayed paper feed actions
- X ------------------------------------------------------------------------- */
- X
- Xvoid position_paper()
- X{
- X int a, b;
- X static int lf_pitch = 0;
- X
- X if (delayed_feed)
- X {
- X a = delayed_feed >> 6;
- X b = delayed_feed & 63;
- X if (a)
- X {
- X if (lf_pitch != 64)
- X {
- X fputs("\033T64", stdout);
- X lf_pitch = 64;
- X }
- X while (a--)
- X fputs("\r\n", stdout);
- X }
- X if (b)
- X {
- X if (lf_pitch != b)
- X {
- X printf("\033T%02u", b);
- X lf_pitch = b;
- X }
- X fputs("\r\n", stdout);
- X }
- X delayed_feed = 0;
- X }
- X}
- X
- X/* -------------------------------------------------------------------------
- X Skip n pixels vertically
- X ------------------------------------------------------------------------- */
- X
- Xvoid skip_pixels(n)
- X int n;
- X{
- X delayed_feed += interleaved ? n : n << 1;
- X}
- X
- X/* -------------------------------------------------------------------------
- X Print one line of the bitmap (one pass of the print head)
- X ------------------------------------------------------------------------- */
- X
- Xvoid print_line(x, y, width, feed)
- X int x, y, width, feed;
- X{
- X int out_pos, i, first_nonblank = -1, last_nonblank, num_chars;
- X unsigned char bitmask, *in_char[8], out_char;
- X unsigned char pixbuf[MAX_PIX_PER_LINE];
- X
- X /* Find bits to print in bitmap */
- X in_char[0] = bitmap + (x >> 3) + y * bytes_per_line;
- X for (i = 1; i < 8; i++)
- X in_char[i] = in_char[i - 1] + (bytes_per_line << interleaved);
- X
- X /* Shuffle bits for printer output */
- X bitmask = 0x80;
- X for (out_pos = 0; out_pos < width; out_pos++)
- X {
- X out_char = 0;
- X for (i = 0; i < 8; i++)
- X {
- X out_char >>= 1;
- X if (*in_char[i] & bitmask)
- X out_char |= 0x80;
- X }
- X pixbuf[out_pos] = out_char;
- X if (out_char)
- X {
- X if (first_nonblank == -1)
- X first_nonblank = out_pos;
- X last_nonblank = out_pos;
- X }
- X bitmask >>= 1;
- X if (!bitmask)
- X {
- X bitmask = 0x80;
- X for (i = 0; i < 8; i++)
- X in_char[i]++;
- X }
- X }
- X
- X /* Now print them. If there is anything to print, that is */
- X if (first_nonblank == -1)
- X {
- X delayed_feed += feed;
- X return;
- X }
- X position_paper();
- X if (first_nonblank)
- X printf("\033F%04u", first_nonblank);
- X num_chars = last_nonblank - first_nonblank + 1;
- X printf("\033G%04u", num_chars);
- X fwrite(&pixbuf[first_nonblank], 1, num_chars, stdout);
- X delayed_feed = feed;
- X}
- X
- X/* -------------------------------------------------------------------------
- X Print one page from the bitmap
- X ------------------------------------------------------------------------- */
- X
- Xvoid print_page(x, y, width, height)
- X int x, y, width, height;
- X{
- X int cur_y;
- X
- X /* Print lines */
- X cur_y = y;
- X if (interleaved)
- X while (cur_y < y + height)
- X {
- X print_line(x, cur_y, width, 1);
- X print_line(x, cur_y + 1, width, 15);
- X cur_y += 16;
- X }
- X else
- X {
- X while (cur_y < y + height)
- X {
- X print_line(x, cur_y, width, 16);
- X cur_y += 8;
- X }
- X }
- X}
- X
- X/* -------------------------------------------------------------------------
- X Print the bitmap!
- X ------------------------------------------------------------------------- */
- X
- Xvoid print_bitmap()
- X{
- X int max_height, x, y, print_width, print_height;
- X int image_ready, column_ready;
- X
- X max_height = (paper_height - tearoff_margin) & -8;
- X tearoff_margin = paper_height - max_height;
- X if (interleaved)
- X {
- X paper_height <<= 1;
- X tearoff_margin <<= 1;
- X max_height <<= 1;
- X }
- X else
- X fputs("\033>", stdout);
- X
- X fputc('\033', stdout);
- X fputc(esc_char, stdout);
- X
- X x = 0;
- X print_width = pix_per_line;
- X image_ready = FALSE;
- X while (!image_ready)
- X {
- X if (x + print_width >= image_width)
- X {
- X print_width = image_width - x;
- X image_ready = TRUE;
- X }
- X
- X if (join_pages)
- X {
- X y = 0;
- X print_page(x, y, print_width, image_height);
- X skip_pixels(paper_height -
- X image_height % paper_height);
- X }
- X else
- X {
- X y = 0;
- X print_height = max_height;
- X column_ready = FALSE;
- X while (!column_ready)
- X {
- X if (y + print_height >= image_height)
- X {
- X print_height = image_height - y;
- X column_ready = TRUE;
- X }
- X print_page(x, y, print_width, print_height);
- X skip_pixels(paper_height - print_height);
- X y += max_height;
- X }
- X }
- X
- X x += pix_per_line;
- X }
- X position_paper();
- X}
- X
- X/* -------------------------------------------------------------------------
- X Final cleanup
- X ------------------------------------------------------------------------- */
- X
- Xvoid cleanup()
- X{
- X free(bitmap);
- X fputs("\033c", stdout);
- X}
- X
- X/* -------------------------------------------------------------------------
- X The Function Of Supreme Importance
- X ------------------------------------------------------------------------- */
- X
- Xvoid main(argc, argv)
- X int argc;
- X char *argv[];
- X{
- X read_setup_file();
- X read_command_line(argc, argv);
- X read_header();
- X alloc_bitmap();
- X read_file();
- X print_bitmap();
- X cleanup();
- X exit(0);
- X}
- END_OF_FILE
- if test 13607 -ne `wc -c <'pbmtoiw.c'`; then
- echo shar: \"'pbmtoiw.c'\" unpacked with wrong size!
- fi
- # end of 'pbmtoiw.c'
- fi
- echo shar: End of archive 1 \(of 1\).
- cp /dev/null ark1isdone
- MISSING=""
- for I in 1 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have the archive.
- rm -f ark[1-9]isdone
- else
- echo You still must unpack the following archives:
- echo " " ${MISSING}
- fi
- exit 0
- exit 0 # Just in case...
-