home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gle / gle / dvilj300.c < prev    next >
C/C++ Source or Header  |  1992-11-29  |  6KB  |  263 lines

  1. /* data compression removed from sendline bit as some hp printers
  2.    don't  understand the compressed data format,  -plus will re-enable it */
  3.  
  4. #define XSIZECM 19        /* at 120 DPI  */
  5. #define YSIZECM 27        /* at 72 DPI */
  6. #define NXBITS 2256     /* (XSIZECM/2.54)*300  rounded up to nearest byte */
  7. #define NYBITS 3200
  8.  
  9. extern int hp_plus;
  10. #ifdef __TURBOC__
  11. #include <alloc.h>
  12. #endif
  13. #include "all.h"
  14. #include <math.h>
  15. #include "core.h"
  16. #include "mygraph.h"
  17. #include "mydev.h"
  18. #define dp if (dop)
  19. #ifndef __TURBOC__
  20. #define huge
  21. #endif
  22. #define true (!false)
  23. #define false 0
  24. int dop=false;
  25.  
  26. int d_open(double x, double y);
  27. extern int gdebug;
  28. extern int nxbits,nybits;
  29. #define dbg if (gdebug==true)
  30. int bitmap_print(void);
  31. int bitmap_free(void);
  32. int bitmap_alloc(void);
  33. int bitmap_paint(int row, int x1, int x2);
  34. int printmem(char *x,int n);
  35. void pprintf(va_list arg_list, ...);
  36. int dvitype(void);
  37.  
  38. /*--------------------------------------------------------------*/
  39. /*    Bitmap output for HP Laserjet printers             */
  40. /*--------------------------------------------------------------*/
  41. int bitmap_print(void);
  42. char *bitmap_line(int y);
  43. int bitmap_size(int *xbits, int *ybits, double *width, double *height);
  44. bitmap_size(int *xbits, int *ybits, double *width, double *height)
  45. {
  46.     *xbits = NXBITS; *ybits = NYBITS; *width = XSIZECM; *height = YSIZECM;
  47. }
  48. int print_row(int i);
  49. #ifndef __TURBOC__
  50. unsigned long coreleft(void)
  51. {
  52.     return 4000000;
  53. }
  54. #endif
  55.  
  56. bitmap_free(void)
  57. {
  58.     printf("core left %ld \n",coreleft());
  59. }
  60. bitmap_alloc(void)
  61. {
  62. }
  63. unsigned int grey_bits[] = {0x0000, 0x0200, 0x0802, 0x0a02,
  64.         0x5050, 0x5250, 0x5852, 0x5a52,
  65.         0xa5a5, 0xa7a5, 0xada7, 0xafa7,
  66.         0xf5f5, 0xf7f5, 0xfdf7, 0xfff7, 0xffff};
  67. unsigned int cur_bits[4];
  68. #define NGREY 17
  69. set_grey(float f)
  70. {
  71.     int i,j,m;
  72.     i = (1.0-f)*(NGREY-.8);
  73.     m = grey_bits[i];
  74.     for (j=0; j<4; j++) {
  75.         cur_bits[j] = m & 0x000f;
  76.         m = m >> 4;
  77.     }
  78. }
  79. bitmap_paint(int row, int x1, int x2)
  80. {
  81.     int i,xm,ym,xadd=0;
  82.     unsigned char *line;
  83.     x1-=1; x2-=1;
  84.     if (x1<0) x1 = 0;
  85.     if (x2>=nxbits) x2 = nxbits-1;
  86.     ym = row % 4;
  87.     if ((row & 4)>0) xadd = 1;
  88.     line = bitmap_line(row);
  89.     for (;x1<=x2;x1++) {
  90.         xm = 1 << ((x1+xadd) % 4);
  91.         if ((cur_bits[ym] & xm)!=0) {
  92.             i = line[x1/8];
  93.             line[x1/8] = i | (1 << (x1 % 8));
  94.         }
  95.     }
  96. }
  97. unsigned char *bitmapv[NYBITS];
  98. int bitmapn[NYBITS];
  99. char *bitmap_line(int y)
  100. {
  101.     static unsigned char bitrow[NXBITS/8+3];
  102.     static unsigned char bitc[NXBITS],*o,*bc,c;
  103.     static int range_low,range_hi;
  104.     int nc,x,i,j;
  105.     static lasty = -2;
  106.     if (lasty==y) return bitrow;
  107.     if (y<0) {
  108.       y=0; if (range_low==0) {range_low=true; printf("y pixel off bottom\n");}
  109.     }
  110.     if (y>=NYBITS) {
  111.        y=NYBITS-1; if (range_hi==0) {range_hi=true; printf("y pixel off top\n");}
  112.     }
  113.     dp printf("--Core left %ld  line %d \n",coreleft(),y);
  114.  
  115.     if (lasty != -2) {
  116.       o = &bitc[0];
  117.       for (x=0; x<(nxbits/8) ; x++) {
  118.         c = bitrow[x];
  119.         nc = 0;
  120.         for (;x<(nxbits/8-1) && bitrow[x+1]==c && nc<250; x++) nc++;
  121.         *o++ = nc;
  122.         *o++ = c;
  123.       }
  124.       nc = o-bitc;
  125.       bitmapn[lasty] = nc;
  126.       if (bitmapv[lasty]!=NULL) bitmapv[lasty] = realloc(bitmapv[lasty],nc);
  127.       else bitmapv[lasty] = malloc(nc);
  128.       if (bitmapv[lasty]==0) {
  129.         printf("Failed to allocate enough memory for bitmap \n");
  130.         abort();
  131.       }
  132.       dp printf("saving line length %d \n",nc);
  133.       memcpy(bitmapv[lasty],bitc,nc);
  134.       o = bitmapv[lasty];
  135.     }
  136.     lasty = y;
  137.     if (bitmapv[y]==NULL) {
  138.         memset(bitrow,0,NXBITS/8);
  139.  dp        printf("returning blank line\n");
  140.         return bitrow;
  141.     }
  142.     dp printf("Expanding line %d (%p) \n",y,bitmapv[y]);
  143.  
  144.     nc = 0;
  145.     bc = bitmapv[y];
  146.     for (i=0, o = bitrow; i<bitmapn[y]; i+=2) {
  147.         dp printf("E, %d %d \n",bc[i],bc[i+1]);
  148.         for (j=0; j<=bc[i]; j++) {
  149.             *o++ = bc[i+1]; nc++;
  150.             if (nc>NXBITS/8) {
  151.                 printf("gone past end\n");
  152.                 return bitrow;
  153.             }
  154.         }
  155.     }
  156.     return bitrow;
  157. }
  158. bitmap_pixel(int x, int y)
  159. {
  160.     static char *line;
  161.     static int ly= -2;
  162. /*    if (x<0 || x>nxbits || y < 0 || y > nybits) return; */
  163.     line = bitmap_line(y);
  164.     line[x/8] |= (1 << (x % 8));
  165.     ly = y;
  166. }
  167.  
  168. dvitype(void)
  169. {
  170.     printf("Usage:  DVILJ300  [-old] [-debug] [outfile.prt]\n");
  171.     printf("Deskjet/Laserjet");
  172. }
  173. int ljsendline(char *s, int nc, int y);
  174.  
  175. /*
  176. #ifdef __TURBOC__
  177. #include <graphics.h>
  178. xxbitmap_print()
  179. {
  180.     int x,y,z;
  181.     int d_graphmode=0,g_driver=0,g_error;
  182.     double f;
  183.     unsigned char *line;
  184.     printf("press return to enter graphics \n");
  185.     getch();
  186.     dop = false;
  187.     detectgraph(&g_driver, &d_graphmode);
  188.     if (g_driver<0) {
  189.         printf("No graphics hardware detected !!!!! \n");
  190.         gle_abort("Could not load BGI graphics\n");
  191.     }
  192.     initgraph(&g_driver,&d_graphmode,"");
  193.     g_error = graphresult();
  194.     if (g_error<0) {
  195.         printf("Init graph error %s\n",grapherrormsg(g_error));
  196.         gle_abort("Init graph error\n");
  197.     }
  198.  
  199.     for (y=1; y<399; y++) {
  200.       line = bitmap_line(y);
  201.       for (x=0; x<620 ; x++) {
  202.         z = 1 << (x % 8);
  203.         if ((z & line[x/8]) != 0) {
  204.              putpixel(x,y,2);
  205.         }
  206.  
  207.       }
  208.     }
  209.  
  210.     getch();
  211.  
  212.     closegraph();
  213. }
  214. #endif
  215. */
  216.  
  217. bitmap_print(void)
  218. {
  219.     int x,y,nout;
  220.     char *line;
  221.     static unsigned char out_buff[NXBITS/8+20];
  222.     unsigned char swapbit[256];
  223.     unsigned char swapnib[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
  224.     unsigned char *o,c;
  225.     int nc,i,n1,n2;
  226.  
  227.     printf("Writing out bitmap\n");
  228.     for (i=0;i<256;i++) {
  229.         n1 = i & 0xf;
  230.         n2 = (i >> 4) & 0xf;
  231.         swapbit[i] = swapnib[n2] | (swapnib[n1] << 4);
  232.     }
  233.     pprintf("%cE",27);     /* reset  */
  234.     pprintf("\x1b*t300R");    /* set resolution 150 dpi */
  235.     /* pprintf("\x1b&a%.1fH",0.0); */        /* move x */
  236.     /* pprintf("\x1b&a%.1fV",((NYBITS-y)/300.0)*720.0); */  /* move y */
  237.     if (hp_plus) pprintf("\x1b*b1M");        /* compact */
  238.     pprintf("\x1b*r1A");        /* start graphics at cur pos */
  239.     for (y=(nybits-1); y>=0; y--) {
  240.       line = bitmap_line(y);
  241.       for (nout=(nxbits/8); line[nout-1]==0 && nout>0; nout--);
  242.           o = &out_buff[0];
  243.       for (x=0; x<nout; x++) {
  244.           c = line[x];
  245.           nc = 0;
  246.            if (hp_plus) {
  247.           for (;x<nout-1 && line[x+1]==c && nc<250; x++) nc++;
  248.           *o++ = nc;
  249.           }
  250.           *o++ = swapbit[c];
  251.       }
  252.       ljsendline(out_buff,o-out_buff,y);
  253.     }
  254.     pprintf("\x1b*rB");        /* end graphics */
  255.     pprintf("\x0c"); /* form feed, reset origin */
  256. }
  257.  
  258. ljsendline(char *s, int nc,int y)
  259. {
  260.     pprintf("\x1b*b%dW",nc);    /* send bytes */
  261.     printmem(s,nc);
  262. }
  263.