home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / devices / printer / epsonq / render.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  8.1 KB  |  271 lines

  1. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  2.  *
  3.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  4.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  5.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  6.  * information on the correct usage of the techniques and operating system
  7.  * functions presented in this example.  The source and executable code of
  8.  * this example may only be distributed in free electronic form, via bulletin
  9.  * board or as part of a fully non-commercial and freely redistributable
  10.  * diskette.  Both the source and executable code (including comments) must
  11.  * be included, without modification, in any copy.  This example may not be
  12.  * published in printed form or distributed with any commercial product.
  13.  * However, the programming techniques and support routines set forth in
  14.  * this example may be used in the development of original executable
  15.  * software products for Commodore Amiga computers.
  16.  * All other rights reserved.
  17.  * This example is provided "as-is" and is subject to change; no warranties
  18.  * are made.  All use is at your own risk.  No liability or responsibility
  19.  * is assumed.
  20.  */
  21.  
  22. #include <exec/types.h>
  23. #include <exec/nodes.h>
  24. #include <exec/lists.h>
  25. #include <exec/memory.h>
  26. #include <devices/printer.h>
  27. #include <devices/prtbase.h>
  28. #include <devices/prtgfx.h>
  29.  
  30. #define NUMSTARTCMD    8    /* # of cmd bytes before binary data */
  31. #define NUMENDCMD    1    /* # of cmd bytes after binary data */
  32. #define NUMTOTALCMD (NUMSTARTCMD + NUMENDCMD)    /* total of above */
  33. #define NUMLFCMD    4    /* # of cmd bytes for linefeed */
  34. #define MAXCOLORBUFS    4    /* max # of color buffers */
  35.  
  36. #define STARTLEN    16
  37. #define PITCH        1
  38. #define CONDENSED    2
  39. #define LMARG        8
  40. #define RMARG        11
  41. #define DIREC        15
  42.  
  43. Render(ct, x, y, status)
  44. long ct, x, y, status;
  45. {
  46.     extern void *AllocMem(), FreeMem();
  47.  
  48.     extern struct PrinterData *PD;
  49.     extern struct PrinterExtendedData *PED;
  50.  
  51.     static UWORD RowSize, ColorSize, BufSize, TotalBufSize;
  52.     static UWORD dataoffset, dpi_code;
  53.     static UWORD colors[MAXCOLORBUFS]; /* color ptrs */
  54.     static UWORD colorcodes[MAXCOLORBUFS]; /* printer color codes */
  55.     static UWORD NumColorBufs; /* actually number of color buffers req. */
  56.     /*
  57.         00-01    \003P        set pitch (10 or 12 cpi)
  58.         02-02    \022        set condensed fine (on or off)
  59.         03-05    \033W\000    enlarge off
  60.         06-08    \033ln        set left margin to n
  61.         09-11    \033Qn        set right margin to n
  62.         12-12    \015        carriage return
  63.         13-15    \033U1        set uni-directional mode
  64.     */
  65.     static UBYTE StartBuf[STARTLEN] =
  66.         {0x1b,'P',0x12,0x1b,'W',0x0,0x1b,'l','n',0x1b,'Q','n',0x0d,0x1b,'U','1'};
  67.  
  68.     UBYTE *ptr, *ptrstart, *ptr2, *ptr2start, **dummy;
  69.  
  70.     int i, err;
  71.  
  72.     switch(status) {
  73.         case 0:  /* Master Initialization */
  74.             /*
  75.                 ct    - pointer to IODRPReq structure.
  76.                 x    - width of printed picture in pixels.
  77.                 y    - height of printed picture in pixels.
  78.             */
  79.             RowSize = x * 3;
  80.             ColorSize = RowSize + NUMTOTALCMD;
  81.             if (PD->pd_Preferences.PrintShade == SHADE_COLOR) {
  82.                 NumColorBufs = MAXCOLORBUFS;
  83.                 colors[0] = ColorSize * 3; /* Black */
  84.                 colors[1] = ColorSize * 0; /* Yellow */
  85.                 colors[2] = ColorSize * 1; /* Magenta */
  86.                 colors[3] = ColorSize * 2; /* Cyan */
  87.                 colorcodes[0] = 4; /* Yellow */
  88.                 colorcodes[1] = 1; /* Magenta */
  89.                 colorcodes[2] = 2; /* Cyan */
  90.                 colorcodes[3] = 0; /* Black */
  91.             }
  92.             else { /* grey-scale or black&white */
  93.                 NumColorBufs = 1;
  94.                 colors[0] = ColorSize * 0; /* Black */
  95.                 colorcodes[0] = 0; /* Black */
  96.             }
  97.             BufSize = ColorSize * NumColorBufs + NUMLFCMD;
  98.             TotalBufSize = BufSize * 2;
  99.             PD->pd_PrintBuf = AllocMem(TotalBufSize, MEMF_PUBLIC);
  100.             if (PD->pd_PrintBuf == NULL) {
  101.                 err = PDERR_BUFFERMEMORY; /* no mem */
  102.             }
  103.             else {
  104.                 dataoffset = NUMSTARTCMD;
  105.                 /*
  106.                     This printer prints graphics within its
  107.                     text margins.  This code makes sure the
  108.                     printer is in 10 cpi and then sets the
  109.                     left and right margins to their minimum
  110.                     and maximum values (respectively).  A
  111.                     carriage return is sent so that the
  112.                     print head is at the leftmost position
  113.                     as this printer starts printing from
  114.                     the print head's position.  The printer
  115.                     is put into unidirectional mode to
  116.                     reduce wavy vertical lines.
  117.                 */
  118.                 StartBuf[PITCH] = 'P'; /* 10 cpi */
  119.                 StartBuf[CONDENSED] = '\022'; /* off */
  120.                 /* left margin of 1 */
  121.                 StartBuf[LMARG] = 0;
  122.                 /* right margin of 80 or 136 */
  123.                 StartBuf[RMARG] = PD->pd_Preferences.
  124.                     PaperSize == W_TRACTOR ? 136 : 80;
  125.                 /* uni-directional mode */
  126.                 StartBuf[DIREC] = '1';
  127.                 err = (*(PD->pd_PWrite))(StartBuf, STARTLEN);
  128.             }
  129.             break;
  130.  
  131.         case 1: /* Scale, Dither and Render */
  132.             /*
  133.                 ct    - pointer to PrtInfo structure.
  134.                 x    - 0.
  135.                 y    - row # (0 to Height - 1).
  136.             */
  137.             Transfer(ct, y, &PD->pd_PrintBuf[dataoffset], colors);
  138.             err = PDERR_NOERR; /* all ok */
  139.             break;
  140.  
  141.         case 2: /* Dump Buffer to Printer */
  142.             /*
  143.                 ct    - 0.
  144.                 x    - 0.
  145.                 y    - # of rows sent (1 to NumRows).
  146.             */
  147.             /* white-space strip */
  148.             ptrstart = &PD->pd_PrintBuf[dataoffset];
  149.             ptr2start = ptr2 = ptrstart - NUMSTARTCMD;
  150.             x = 0; /* flag no transfer required yet */
  151.             for (ct=0; ct<NumColorBufs;
  152.                 ct++, ptrstart += ColorSize) {
  153.                 i = RowSize;
  154.                 ptr = ptrstart + i - 1;
  155.                 while (i > 0 && *ptr == 0) {
  156.                     i--;
  157.                     ptr--;
  158.                 }
  159.                 if (i != 0) { /* if data */
  160.                     /* convert to # of pixels */
  161.                     i = (i + 2) / 3;
  162.                     ptr = ptrstart - NUMSTARTCMD;
  163.                     *ptr++ = 27;
  164.                     *ptr++ = 'r';
  165.                     *ptr++ = colorcodes[ct]; /* color */
  166.                     *ptr++ = 27;
  167.                     *ptr++ = '*';
  168.                     *ptr++ = dpi_code;    /* density */
  169.                     *ptr++ = i & 0xff;
  170.                     *ptr++ = i >> 8;    /* size */
  171.                     i *= 3; /* back to # of bytes used */
  172.                     *(ptrstart + i) = 13; /* cr */
  173.                     i += NUMTOTALCMD;
  174.                     /* if must transfer data */
  175.                     if (x != 0) {
  176.                         /* get src start */
  177.                         ptr = ptrstart - NUMSTARTCMD;
  178.                                                 /* otherwise Lattice looses
  179.                                                    track of the pointer.... */
  180.                                                 dummy = &ptr;
  181.                         /* xfer and update dest ptr */
  182.                         do {
  183.                             *ptr2++ = *ptr++;
  184.                         } while (--i);
  185.                     }
  186.                     else { /* no transfer required */
  187.                         /* update dest ptr */
  188.                         ptr2 += i;
  189.                     }
  190.                 }
  191.                 /* if compacted or 0 */
  192.                 if (i != RowSize + NUMTOTALCMD) {
  193.                     /* we need to transfer next time */
  194.                     x = 1;
  195.                 }
  196.             }
  197.             *ptr2++ = 13; /* cr */
  198.             *ptr2++ = 27;
  199.             *ptr2++ = 'J';
  200.             *ptr2++ = y; /* y/180 lf */
  201.             err = (*(PD->pd_PWrite))(ptr2start, ptr2 - ptr2start);
  202.             if (err == PDERR_NOERR) {
  203.                 dataoffset = (dataoffset == NUMSTARTCMD ?
  204.                     BufSize : 0) + NUMSTARTCMD;
  205.             }
  206.             break;
  207.  
  208.         case 3: /* Clear and Init Buffer */
  209.             /*
  210.                 ct    - 0.
  211.                 x    - 0.
  212.                 y    - 0.
  213.             */
  214.             ptr = &PD->pd_PrintBuf[dataoffset];
  215.             i = BufSize - NUMTOTALCMD - NUMLFCMD;
  216.             do {
  217.                 *ptr++ = 0;
  218.             } while (--i);
  219.             err = PDERR_NOERR; /* all ok */
  220.             break;
  221.  
  222.         case 4: /* Close Down */
  223.             /*
  224.                 ct    - error code.
  225.                 x    - io_Special flag from IODRPReq.
  226.                 y    - 0.
  227.             */
  228.             err = PDERR_NOERR; /* assume all ok */
  229.             /* if user did not cancel print */
  230.             if (ct != PDERR_CANCEL) {
  231.                 /* restore preferences pitch and margins */
  232.                 if (PD->pd_Preferences.PrintPitch == ELITE) {
  233.                     StartBuf[PITCH] = 'M'; /* 12 cpi */
  234.                 }
  235.                 else if (PD->pd_Preferences.PrintPitch == FINE) {
  236.                     StartBuf[CONDENSED] = '\017'; /* on */
  237.                 }
  238.                 StartBuf[LMARG] =
  239.                     PD->pd_Preferences.PrintLeftMargin - 1;
  240.                 StartBuf[RMARG] =
  241.                     PD->pd_Preferences.PrintRightMargin;
  242.                 StartBuf[DIREC] = '0'; /* bi-directional */
  243.                 err = (*(PD->pd_PWrite))(StartBuf, STARTLEN);
  244.             }
  245.              /* wait for both buffers to empty */
  246.             (*(PD->pd_PBothReady))();
  247.             if (PD->pd_PrintBuf != NULL) {
  248.                 FreeMem(PD->pd_PrintBuf, TotalBufSize);
  249.             }
  250.             break;
  251.  
  252.         case 5:   /* Pre-Master Initialization */
  253.             /*
  254.                 ct    - 0 or pointer to IODRPReq structure.
  255.                 x    - io_Special flag from IODRPReq.
  256.                 y    - 0.
  257.             */
  258.             /*
  259.                 Kludge for weak power supplies.
  260.                 FANFOLD - use all 24 pins (default).
  261.                 SINGLE  - use only 16 pins.
  262.             */
  263.             PED->ped_NumRows = PD->pd_Preferences.PaperType ==
  264.                 SINGLE ? 16 : 24;
  265.             dpi_code = SetDensity(x & SPECIAL_DENSITYMASK);
  266.             err = PDERR_NOERR; /* all ok */
  267.             break;
  268.     }
  269.     return(err);
  270. }
  271.