home *** CD-ROM | disk | FTP | other *** search
- /*
- Copyright 1990 by John Wiley & Sons, Inc.
- All Rights Reserved.
- */
- /****************************************************************/
- /* Simple Graphics Print Program */
- /* prtsimp.c */
- /* Prints simple black and white images on an IBM ProPrinter */
- /* utilizing thresholding techniques */
- /* written in Turbo C 2.0 */
- /* */
- /* written by Craig A. Lindley */
- /* Vers: 1.0 Last Update: 06/08/89 */
- /****************************************************************/
- /*
- This program prints a black and white rendering of a graphics image
- on an IBM ProPrinter.
- */
-
- #include <stdio.h>
- #include <dos.h>
- #include <conio.h>
- #include <graphics.h>
- #include "printer.h"
-
- union REGS regs;
- unsigned MaxScreenCol; /* max pixel x value for display mode */
- unsigned MaxScreenRow; /* max pixel y value for display mode */
- unsigned N1, N2; /* least and most significant byte count */
-
-
- /* IBM ProPrinter Specific Printer Control Code Strings */
- BYTE *OneDirection = "\x1BU\x31"; /* ESC U 1 */
- BYTE *LowRes = "\x1BK"; /* ESC K */
- BYTE *MedRes = "\x1BY"; /* ESC Y */
- BYTE *HighRes = "\x1BZ"; /* ESC Z */
- BYTE *TextLineFeed = "\x1B\x31"; /* ESC 1 - normal 7/72 text line feed */
- BYTE *GraphicLineFeed= "\x1B\x33\x18"; /* ESC 3 24 - 24/216ths line feed */
- BYTE *CrLf = "\r\n"; /* graphics carrage ret line feed */
- BYTE *DisAutoLf ="\x1B\x35\x30"; /* ESC 5 0 disable auto line feed */
-
- /* Printer Interface Functions */
-
- /* This function initializes the line printer for operation */
- void PrtInit (unsigned PrtNum)
- {
- regs.h.ah = INITPRTCODE;
- regs.x.dx = PrtNum;
- int86(PRINTERINT, ®s, ®s);
- }
-
- /* This function reads the status of the line printer */
- BYTE PrtStatus (unsigned PrtNum)
- {
- regs.h.ah = GETPRTSTATUSCODE;
- regs.x.dx = PrtNum;
- int86(PRINTERINT, ®s, ®s);
- return (regs.h.ah);
- }
-
-
- /* this function prints a character on the specified printer */
- BYTE PrtChar(unsigned PrtNum, BYTE Character)
- {
-
- while(!(PrtStatus(PrtNum) & PRTBUSYBIT)); /* wait until not busy */
- regs.h.ah = PRTCHARCODE; /* prt a character code */
- regs.h.al = Character;
- regs.x.dx = PrtNum; /* select the printer */
- int86(PRINTERINT, ®s, ®s);
- return(regs.h.ah); /* return operation status */
- }
-
- /* this function prints a null terminated string of characters
- to the named printer */
-
- unsigned PrtString(unsigned PrtNum, BYTE *String)
- {
- unsigned RetCode;
-
- RetCode = 0;
- while ((*String != NULL)&&(!RetCode)) /* do until the null char is found */
- RetCode = ((PrtChar(PrtNum,*String++) & PRTTIMEOUT) == 1);
- return(RetCode);
- }
-
- /* start of graphic printer functions */
-
- BYTE BuildByte(unsigned Threshold, unsigned Col, unsigned Row)
- {
- static BYTE PinBits[PIXELSPERPASS] = {128, 64, 32, 16, 8, 4, 2, 1};
- register unsigned PixelValue, NextRow;
- BYTE Pins;
-
- Pins = 0; /* start with all pins off */
- Row <<= 3; /* get start of 8 pin row */
- for (NextRow=0; NextRow < 8; NextRow++) /* accumulate 8 vert. pixels */
- {
- PixelValue = getpixel(Col,Row+NextRow); /* get pixel value */
- if (PixelValue > Threshold) /* is pixel considered on ? */
- Pins |= PinBits[NextRow]; /* if so set pin value on */
- }
- return(Pins);
- }
-
-
- void PrtRow(unsigned Threshold, unsigned Rev,
- PrinterModes PrtMode, unsigned ScreenRow)
- {
- unsigned CurrentCol;
- BYTE NextByte;
-
- switch (PrtMode) /* send printer the graphic mode */
- { /* control code sequence */
- case HighResMode: {
- PrtString(LPT1,HighRes);
- break;
- }
- case MedResMode: {
- PrtString(LPT1,MedRes);
- break;
- }
- case LowResMode:
- default: {
- PrtString(LPT1,LowRes);
- break;
- }
- }
-
- /* tell printer how many bytes to follow */
- PrtChar(LPT1,N1);
- PrtChar(LPT1,N2);
-
- for (CurrentCol=0; CurrentCol < MaxScreenCol; CurrentCol++)
- {
- NextByte = BuildByte(Threshold, CurrentCol, ScreenRow);
- if (Rev)
- NextByte ^= 0xFF;
- PrtChar(LPT1,NextByte);
- }
- PrtString(LPT1,CrLf); /* output a carrage ret line feed */
- }
-
-
- void PrtScreen (unsigned Threshold, unsigned Rev, PrinterModes PrtMode)
- {
- unsigned ScreenRow;
-
- MaxScreenCol = getmaxx() + 1; /* get current screen mode dimensions */
- MaxScreenRow = 200;
- /* MaxScreenRow = getmaxy() + 1; */
- N2 = MaxScreenCol >> 8; /* calculate byte counts */
- N1 = MaxScreenCol & 0xFF;
-
- /*
- printf("%s\n",getmodename(getgraphmode()));
- printf("max Y = %d\n",getmaxy());
- */
- PrtInit(LPT1); /* initialize the printer */
- PrtString(LPT1,OneDirection); /* set printer in one dir mode */
- PrtString(LPT1,DisAutoLf); /* disable auto line feed */
- PrtString(LPT1,GraphicLineFeed); /* set 24/216 line feed as required */
- /* for 8 pin bit mapped printing */
-
- for (ScreenRow=0; ScreenRow <= (MaxScreenRow/PIXELSPERPASS); ScreenRow++)
- {
- if (kbhit()) /* check for abort */
- { /* if key hit */
- getch(); /* consume it */
- break; /* and quit */
- }
- PrtRow(Threshold, Rev, PrtMode, ScreenRow);
- }
- PrtString(LPT1,TextLineFeed); /* set text line feed */
- PrtChar(LPT1,'\f'); /* do form feed when finished */
-
- }