home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: DFÜ und Kommunikation / SOS-DFUE.ISO / programm / dos / utility / pccp076 / bjfaxpcx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-19  |  5.6 KB  |  246 lines

  1. #include<stdio.h>
  2. #include<fcntl.h>
  3. #include<bios.h>
  4.  
  5. struct header_s
  6.     {
  7.     unsigned char mfgr;
  8.     unsigned char version;
  9.     unsigned char encoding;
  10.     unsigned char bppix;
  11.     unsigned short xmin;
  12.     unsigned short ymin;
  13.     unsigned short xmax;
  14.     unsigned short ymax;
  15.     unsigned short hdpi;
  16.     unsigned short vdpi;
  17.     unsigned char cmap[48];
  18.     unsigned char reserved;
  19.     unsigned char nplanes;
  20.     unsigned short bypl;
  21.     unsigned short paltinf;
  22.     unsigned short hscrnsz;
  23.     unsigned short vscrnsz;
  24.     unsigned char filler[54];
  25.     };
  26.  
  27. struct header_s header;
  28.  
  29. unsigned char buf[17280], bybuf[216], fbuf[8192];
  30.  
  31. main(argc, argv)
  32.     int argc;
  33.     char **argv;
  34.     {
  35.     int ifd, vsiz, totbyts, n, curbyt, lptnum, fbufn;
  36.     int i, icnt, j, nbyts, totbpl, vdstate, hdstate, fbufindex, stripen;
  37.     unsigned char c, nlo, nhi, imask, jmask, bitmask;
  38.     if(argc!=3)
  39.         {
  40.         fprintf(stderr, "USAGE: bjfaxpcx <lpt #> <file>\n");
  41.         exit(1);
  42.         }
  43.     if(sscanf(argv[1], "%d", &lptnum)!=1)
  44.         {
  45.         fprintf(stderr, "Bad scan of LPT number.\n");
  46.         exit(10);
  47.         }
  48.     lptnum--;
  49.     if((ifd=open(argv[2], O_RDONLY|O_BINARY))==-1)
  50.         {
  51.         fprintf(stderr, "Couldn't open %s for read.\n", argv[2]);
  52.         exit(2);
  53.         }
  54.     read(ifd, &header, 128);
  55.     fprintf(stderr, "Got header OK.\n");
  56.     if(header.encoding!=1)
  57.         {
  58.         fprintf(stderr, "Bad type of encoding declared in header.\n");
  59.         exit();
  60.         }
  61.     if(header.nplanes!=1)
  62.         {
  63.         fprintf(stderr, "Only one plane allowed.\n");
  64.         exit();
  65.         }
  66.     if(header.bppix!=1)
  67.         {
  68.         fprintf(stderr, "Grayscale.\n");
  69.         exit();
  70.         }
  71.     if((header.xmax-header.xmin)!=1727)
  72.         {
  73.         printf("Wrong width.\n");
  74.         exit();    
  75.         }
  76.     fprintf(stderr, "All checks OK.\n");
  77.     vsiz=header.ymax-header.ymin+1;
  78.     totbpl=header.bypl;
  79.     totbyts=totbpl*vsiz;
  80.     curbyt=n=vdstate=hdstate=stripen=0;
  81.     bitmask=0x80;
  82.     for(i=0;i<17280;++i)
  83.         buf[i]=0;
  84.     fbufindex=fbufn=0;
  85.     for(;;)
  86.         {
  87.         if(fbufindex>=fbufn)
  88.             {
  89.             fbufn=read(ifd, fbuf, 8192);
  90.             fbufindex=0;
  91.             }
  92.         c=fbuf[fbufindex++];
  93.         if((c & (unsigned char) 0xc0)== (unsigned char) 0xc0)
  94.             {
  95.             i = c & (unsigned char) 0x3f;
  96.             if(fbufindex>=fbufn)
  97.                 {
  98.                 fbufn=read(ifd, fbuf, 8192);
  99.                 fbufindex=0;
  100.                 }
  101.             c=fbuf[fbufindex++];
  102.             while(i--)
  103.                 bybuf[n++]=c;
  104.             }
  105.         else
  106.             bybuf[n++]=c;
  107.         if(n>=totbpl)
  108.             {
  109.             if(n!=totbpl)
  110.                 {
  111.                 fprintf(stderr, "Mismatch.\n");
  112.                 exit();
  113.                 }
  114.             if(vdstate)
  115.                 {
  116.                 for(i=0,j=0,jmask=0x80,hdstate=0,icnt=216;icnt;i++,icnt--)
  117.                     for(imask=0x80;imask;imask>>=1)
  118.                         {
  119.                         if(bybuf[i]&imask)
  120.                             ;
  121.                         else
  122.                             buf[j+curbyt]|=bitmask;
  123.                         j+=6;
  124.                         if(hdstate)
  125.                             {
  126.                             if(bybuf[i]&imask)
  127.                                 ;
  128.                             else
  129.                                 buf[j+curbyt]|=bitmask;
  130.                             j+=6;
  131.                             }
  132.                         if(hdstate++==2)
  133.                             hdstate=0;
  134.                         }
  135.                 if((!(bitmask>>=1))||(totbyts==n))
  136.                     {
  137.                     bitmask=0x80;
  138.                     if((++curbyt==6)||(totbyts==n))
  139.                         {
  140.                         fprintf(stderr, "Sending stripe %d\n", ++stripen);
  141.                         nbyts=17280+1;
  142.                         nlo=nbyts%256;
  143.                         nhi=nbyts/256;
  144.                         c='\033';
  145.                         _bios_printer(_PRINTER_WRITE, lptnum, c);
  146.                         c='[';
  147.                         _bios_printer(_PRINTER_WRITE, lptnum, c);
  148.                         c='g';
  149.                         _bios_printer(_PRINTER_WRITE, lptnum, c);
  150.                         _bios_printer(_PRINTER_WRITE, lptnum, nlo);
  151.                         _bios_printer(_PRINTER_WRITE, lptnum, nhi);
  152.                         c=16;
  153.                         _bios_printer(_PRINTER_WRITE, lptnum, c);
  154.                         for(i=0,icnt=17280;icnt;i++,icnt--)
  155.                             _bios_printer(_PRINTER_WRITE, lptnum, buf[i]);
  156.                         c='\r';
  157.                         _bios_printer(_PRINTER_WRITE, lptnum, c);
  158.                         c='\034';
  159.                         _bios_printer(_PRINTER_WRITE, lptnum, c);
  160.                         c='C';
  161.                         _bios_printer(_PRINTER_WRITE, lptnum, c);
  162.                         c='J';
  163.                         _bios_printer(_PRINTER_WRITE, lptnum, c);
  164.                         c=0;
  165.                         _bios_printer(_PRINTER_WRITE, lptnum, c);
  166.                         c=24;
  167.                         _bios_printer(_PRINTER_WRITE, lptnum, c);
  168.                         curbyt=0;
  169.                         for(i=0,icnt=17280;icnt;++i,--icnt)
  170.                             buf[i]=0;
  171.                         }
  172.                     }
  173.                 }
  174.             if(vdstate++==3)
  175.                 vdstate=0;
  176.             for(i=0,j=0,jmask=0x80,hdstate=0,icnt=216;icnt;i++,icnt--)
  177.                 for(imask=0x80;imask;imask>>=1)
  178.                     {
  179.                     if(bybuf[i]&imask)
  180.                         ;
  181.                     else
  182.                         buf[j+curbyt]|=bitmask;
  183.                     j+=6;
  184.                     if(hdstate)
  185.                         {
  186.                         if(bybuf[i]&imask)
  187.                             ;
  188.                         else
  189.                             buf[j+curbyt]|=bitmask;
  190.                         j+=6;
  191.                         }
  192.                     if(hdstate++==2)
  193.                         hdstate=0;
  194.                     }
  195.             if((!(bitmask>>=1))||(totbyts==n))
  196.                 {
  197.                 bitmask=0x80;
  198.                 if((++curbyt==6)||(totbyts==n))
  199.                     {
  200.                     fprintf(stderr, "\rSending stripe %d\n", ++stripen);
  201.                     nbyts=17280+1;
  202.                     nlo=nbyts%256;
  203.                     nhi=nbyts/256;
  204.                     c='\033';
  205.                     _bios_printer(_PRINTER_WRITE, lptnum, c);
  206.                     c='[';
  207.                     _bios_printer(_PRINTER_WRITE, lptnum, c);
  208.                     c='g';
  209.                     _bios_printer(_PRINTER_WRITE, lptnum, c);
  210.                     _bios_printer(_PRINTER_WRITE, lptnum, nlo);
  211.                     _bios_printer(_PRINTER_WRITE, lptnum, nhi);
  212.                     c=16;
  213.                     _bios_printer(_PRINTER_WRITE, lptnum, c);
  214.                     for(i=0,icnt=17280;icnt;i++,icnt--)
  215.                         _bios_printer(_PRINTER_WRITE, lptnum, buf[i]);
  216.                     c='\r';
  217.                     _bios_printer(_PRINTER_WRITE, lptnum, c);
  218.                     c='\034';
  219.                     _bios_printer(_PRINTER_WRITE, lptnum, c);
  220.                     c='C';
  221.                     _bios_printer(_PRINTER_WRITE, lptnum, c);
  222.                     c='J';
  223.                     _bios_printer(_PRINTER_WRITE, lptnum, c);
  224.                     c=0;
  225.                     _bios_printer(_PRINTER_WRITE, lptnum, c);
  226.                     c=24;
  227.                     _bios_printer(_PRINTER_WRITE, lptnum, c);
  228.                     curbyt=0;
  229.                     for(i=0,icnt=17280;icnt;++i,icnt--)
  230.                         buf[i]=0;
  231.                     }
  232.                 }
  233.             totbyts-=n;
  234.             if(!totbyts)
  235.                 {
  236.                 close(ifd);
  237.                 c='\n';
  238.                 _bios_printer(_PRINTER_WRITE, lptnum, 0x0c);
  239.                 fprintf(stderr, "\nFinished correctly.\n");
  240.                 exit();
  241.                 }
  242.             n=0;
  243.             }
  244.         }
  245.     }
  246.