home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / IMGPROC.ZIP / C7.ZIP / PRTSIMP.C < prev   
Encoding:
C/C++ Source or Header  |  1990-04-06  |  6.0 KB  |  178 lines

  1. /*  
  2. Copyright 1990 by John Wiley & Sons, Inc.
  3.           All Rights Reserved.
  4. */
  5. /****************************************************************/
  6. /*               Simple Graphics Print Program                  */
  7. /*                       prtsimp.c                              */
  8. /*   Prints simple black and white images on an IBM ProPrinter  */
  9. /*            utilizing thresholding techniques                 */
  10. /*                  written in Turbo C 2.0                      */
  11. /*                                                              */
  12. /*               written by Craig A. Lindley                    */
  13. /*             Vers: 1.0  Last Update: 06/08/89                 */
  14. /****************************************************************/
  15. /*
  16. This program prints a black and white rendering of a graphics image
  17. on an IBM ProPrinter.
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <dos.h>
  22. #include <conio.h>
  23. #include <graphics.h>
  24. #include "printer.h"
  25.  
  26. union    REGS regs;
  27. unsigned MaxScreenCol;             /* max pixel x value for display mode */
  28. unsigned MaxScreenRow;             /* max pixel y value for display mode */
  29. unsigned N1, N2;                   /* least and most significant byte count */
  30.  
  31.  
  32. /* IBM ProPrinter Specific Printer Control Code Strings */
  33. BYTE *OneDirection   = "\x1BU\x31";    /* ESC U 1 */
  34. BYTE *LowRes         = "\x1BK";        /* ESC K */
  35. BYTE *MedRes         = "\x1BY";        /* ESC Y */
  36. BYTE *HighRes        = "\x1BZ";        /* ESC Z */
  37. BYTE *TextLineFeed   = "\x1B\x31";     /* ESC 1 - normal 7/72 text line feed */
  38. BYTE *GraphicLineFeed= "\x1B\x33\x18"; /* ESC 3 24 -  24/216ths line feed */
  39. BYTE *CrLf           = "\r\n";         /* graphics carrage ret line feed */
  40. BYTE *DisAutoLf      ="\x1B\x35\x30";  /* ESC 5 0 disable auto line feed */
  41.  
  42. /* Printer Interface Functions */
  43.  
  44. /* This function initializes the line printer for operation */
  45. void PrtInit (unsigned PrtNum)
  46. {
  47.    regs.h.ah = INITPRTCODE;
  48.    regs.x.dx = PrtNum;
  49.    int86(PRINTERINT, ®s, ®s);
  50. }
  51.  
  52. /* This function reads the status of the line printer */
  53. BYTE PrtStatus (unsigned PrtNum)
  54. {
  55.    regs.h.ah = GETPRTSTATUSCODE;
  56.    regs.x.dx = PrtNum;
  57.    int86(PRINTERINT, ®s, ®s);
  58.    return (regs.h.ah);
  59. }
  60.  
  61.  
  62. /* this function prints a character on the specified printer */
  63. BYTE PrtChar(unsigned PrtNum, BYTE Character)
  64. {
  65.  
  66.    while(!(PrtStatus(PrtNum) & PRTBUSYBIT)); /* wait until not busy */
  67.    regs.h.ah = PRTCHARCODE;            /* prt a character code */
  68.    regs.h.al = Character;
  69.    regs.x.dx = PrtNum;                 /* select the printer */
  70.    int86(PRINTERINT, ®s, ®s);
  71.    return(regs.h.ah);                  /* return operation status */
  72. }
  73.  
  74. /* this function prints a null terminated string of characters
  75.    to the named printer */
  76.  
  77. unsigned PrtString(unsigned PrtNum, BYTE *String)
  78. {
  79.    unsigned RetCode;
  80.  
  81.    RetCode = 0;
  82.    while ((*String != NULL)&&(!RetCode)) /* do until the null char is found */
  83.       RetCode = ((PrtChar(PrtNum,*String++) & PRTTIMEOUT) == 1);
  84.    return(RetCode);
  85. }
  86.  
  87. /* start of graphic printer functions */
  88.  
  89. BYTE BuildByte(unsigned Threshold, unsigned Col, unsigned Row)
  90. {
  91.    static   BYTE PinBits[PIXELSPERPASS] = {128, 64, 32, 16, 8, 4, 2, 1};
  92.    register unsigned PixelValue, NextRow;
  93.    BYTE     Pins;
  94.  
  95.    Pins = 0;                      /* start with all pins off */
  96.    Row <<= 3;                                 /* get start of 8 pin row */
  97.    for (NextRow=0; NextRow < 8; NextRow++)    /* accumulate 8 vert. pixels */
  98.    {
  99.       PixelValue = getpixel(Col,Row+NextRow); /* get pixel value */
  100.       if (PixelValue > Threshold)             /* is pixel considered on ? */
  101.          Pins |= PinBits[NextRow];            /* if so set pin value on */
  102.    }
  103.    return(Pins);
  104. }
  105.  
  106.  
  107. void PrtRow(unsigned Threshold, unsigned Rev,
  108.         PrinterModes PrtMode, unsigned ScreenRow)
  109. {
  110.    unsigned CurrentCol;
  111.    BYTE NextByte;
  112.  
  113.    switch (PrtMode)                    /* send printer the graphic mode */
  114.    {                                   /* control code sequence */
  115.      case HighResMode: {
  116.              PrtString(LPT1,HighRes);
  117.              break;
  118.                }
  119.      case MedResMode:  {
  120.              PrtString(LPT1,MedRes);
  121.              break;
  122.                }
  123.      case LowResMode:
  124.          default:  {
  125.              PrtString(LPT1,LowRes);
  126.              break;
  127.                }
  128.    }
  129.  
  130.    /* tell printer how many bytes to follow */
  131.    PrtChar(LPT1,N1);
  132.    PrtChar(LPT1,N2);
  133.  
  134.    for (CurrentCol=0; CurrentCol < MaxScreenCol; CurrentCol++)
  135.    {
  136.       NextByte = BuildByte(Threshold, CurrentCol, ScreenRow);
  137.       if (Rev)
  138.          NextByte ^=  0xFF;
  139.       PrtChar(LPT1,NextByte);
  140.    }
  141.    PrtString(LPT1,CrLf);               /* output a carrage ret line feed */
  142. }
  143.  
  144.  
  145. void PrtScreen (unsigned Threshold, unsigned Rev, PrinterModes PrtMode)
  146. {
  147.    unsigned ScreenRow;
  148.  
  149.    MaxScreenCol = getmaxx() + 1;       /* get current screen mode dimensions */
  150.    MaxScreenRow = 200;
  151. /*   MaxScreenRow = getmaxy() + 1; */
  152.    N2 = MaxScreenCol >> 8;             /* calculate byte counts */
  153.    N1 = MaxScreenCol & 0xFF;
  154.  
  155. /*
  156.    printf("%s\n",getmodename(getgraphmode()));
  157.    printf("max Y = %d\n",getmaxy());
  158. */
  159.    PrtInit(LPT1);                      /* initialize the printer */
  160.    PrtString(LPT1,OneDirection);       /* set printer in one dir mode */
  161.    PrtString(LPT1,DisAutoLf);          /* disable auto line feed */
  162.    PrtString(LPT1,GraphicLineFeed);    /* set 24/216 line feed as required */
  163.                                        /* for 8 pin bit mapped printing */
  164.  
  165.    for (ScreenRow=0; ScreenRow <= (MaxScreenRow/PIXELSPERPASS); ScreenRow++)
  166.    {
  167.       if (kbhit())                     /* check for abort */
  168.       {                                /* if key hit */
  169.          getch();                      /* consume it */
  170.          break;                        /* and quit */
  171.       }
  172.       PrtRow(Threshold, Rev, PrtMode, ScreenRow);
  173.    }
  174.    PrtString(LPT1,TextLineFeed);        /* set text line feed */
  175.    PrtChar(LPT1,'\f');                  /* do form feed when finished */
  176.  
  177. }
  178.