home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / print / las2bmp.zip / LA2BMP.C < prev    next >
C/C++ Source or Header  |  1993-05-31  |  9KB  |  382 lines

  1.   /**********************************************************************/
  2.   /*                                    */
  3.   /*                                                                    */
  4.   /* This routine converts HP Laserjet Series II Code to .BMP (windows) */
  5.   /* It was written in may 1993 by                    */
  6.   /*                                     */
  7.   /*        David Schlesinger                    */
  8.   /*                                    */
  9.   /* It is distributed and copyrighted by                 */
  10.   /*                                    */
  11.   /*                TIER ZWO VERLAG                */
  12.   /*                Auf dem Steinchen 22            */
  13.   /*                5300 Bonn 1  (from 1.7.93: 53127 Bonn)    */
  14.   /*                GERMANY                    */
  15.   /*  ----------------------------------------------------------------- */
  16.   /*                                    */
  17.   /*  It may be freely distributed as long this heading is not changed! */
  18.   /*  If You add code or change parts of the code, please append Your    */
  19.   /*  name and increase the version number!                */
  20.   /*                                    */
  21.   /*                                    */
  22.   /*            ==   VERSION 1.0   ==                */
  23.   /*                                    */
  24.   /*  ----------------------------------------------------------------- */
  25.   /*                                    */
  26.   /*  If You have written a program that should be distributed as    */
  27.   /*  SHAREWARE with ISBN - or as a commercial program            */
  28.   /*  please feel free to send a DISK, a DISCRIBTION, and Your price-    */
  29.   /*  suggestions to:    TIER ZWO VERLAG, address above.            */
  30.   /*                                    */
  31.   /*  We also publish books (only in german)!                */
  32.   /*                                      */
  33.   /*  Feel free to send us Your questions and suggestions!        */
  34.   /*                                    */
  35.   /**********************************************************************/
  36.  
  37. #include    <stdio.h>
  38. #include    <stdlib.h>
  39. #include    <ctype.h>
  40. #include    <string.h>
  41.  
  42.   const dpi=100,fak=3,printing=0;
  43.   FILE  *eing,*ausg;
  44.   unsigned int x,y,z,y3,y1,x1,z1,i,k,l,length,m,n;
  45.   long int test;
  46.   int ESC=27,pos,posc,j,zeilpos;
  47.   char zeile[2400];
  48.   unsigned char drzeile[3][100];
  49.   unsigned char zeich[20],*name,*name2,*name3,drpos,nulli,volli,c,c1,c2,c3,*ch;
  50.  
  51.  
  52.  
  53.  
  54.  
  55.   main (int argc,char *argv[])
  56.  
  57. {
  58.     char *input,*output;
  59.     int ana;
  60.  
  61.     name2="8x1300.ref";
  62.     printf ("Laserjet Series II -> .bmp converter   by David Schlesinger\n");
  63.  
  64.     if (printing)printf ("crgc:%d argv[1]:%s argv[2]:%s\n",argc,argv[1],argv[2]);
  65.  
  66.     if ((int)argc!=3) {printf("usage: la2bmp input.ext output.ext\n");return -1; }
  67.     input=argv[1];
  68.     output=argv[2];
  69.  
  70.  
  71.     printf ("name of file to be analyzed: %s\n",input);
  72.  
  73.  
  74.     if (!(eing=fopen (name2,"r"))) {printf("Cannot open file(%s)!\n",name2);return -1;}
  75.  
  76.     if (!(ausg=fopen (output,"w"))) {printf("Cannot open file(%s)!\n",output);return -1;}
  77.  
  78.  
  79.  
  80.     for (j=1; j<=62;j++)   /* the 8x1300.ref file holds the header!!*/
  81.        {
  82.         c=fgetc(eing);
  83.         fputc (c,ausg);
  84.        }
  85.      fclose (eing);
  86.                    /* header completely transferred, now let's
  87.                   start analyzing...*/
  88.  
  89.  
  90.  
  91.  
  92.  
  93.     if (!(eing=fopen (input,"r"))) {printf("Cannot open file (%s)!\n",input);return -1;}
  94.  
  95.  
  96.     /* definitionen: */
  97.  
  98.     length=3900;x=0;y=0;x1=0;y1=0;y3=0;      /*length=3900 * 300 dpi =13 inches!     */
  99.     nulli=0;volli=255;test=0;drpos=0;        /* resolution of .bmp : 100 dpi => 1:3  */
  100.                          /* test shows number of actually written*/
  101.                          /* lines-necessary because of truncation*/
  102.                          /* drpos=0-2 to do 1:3 transfer     */
  103.                          /* nulli,volli to fill bitmap           */
  104.  
  105.  
  106.     ana=suche_daten();         /* suche_daten () analyzing routine, see below*/
  107.  
  108.     if (ana==50&&printing) printf("File starts as it should!\n");
  109.  
  110.     while (1)
  111.  
  112.     {
  113.     ana=suche_daten();
  114.  
  115.     if (ana==-1) {printf ("Error reported!!\n");goto endf;}
  116.     if (ana==50) {if (printing) printf ("Analyze nearly finished..\n");
  117.               fuelle_zeilen(y1,length);
  118.               goto end;}
  119.     if (ana==100) {printf ("End Of File ohne ESC E!!\n");goto endf;}
  120.  
  121.     if (ana==8) {
  122.             fuelle_zeilen(y1,y); /* fills row to y pos...*/
  123.             uebertragen(x,z);    /* passes code from printerfile to drzeile[drpos][]*/
  124.  
  125.             }
  126.  
  127.     }
  128.  
  129.  
  130.  
  131.  
  132.  
  133. end:
  134.     if (printing){
  135.             printf("Analyze finished!\n");
  136.             printf("%d rows written!\n",test);
  137.              }
  138.  
  139.     while (test<1300)        /* if truncation lead to less rows...*/
  140.      fuelle_zeilen (0,fak);
  141.  
  142.  
  143.  
  144.     printf("Finished!\n");
  145. endf:    fclose (ausg);
  146.     return 0;
  147. }
  148.  
  149.  
  150.  
  151. int suche_daten ()
  152. {
  153.  
  154.  int rel,jj,j1,j2;
  155.  
  156. schleife:       while (fgetc(eing)!=ESC) {}          /*wait for ESC*/
  157.  
  158.               c=fgetc(eing);
  159.               if (c=='E') return 50;
  160.               if (c!='*')goto schleife;
  161.               c=fgetc(eing);
  162.               c1=fgetc(eing);
  163.  
  164.               if (c=='r'&& c1=='B') return 5;
  165.               if (c=='r') {
  166.                     c2=fgetc(eing);
  167.                     if (c2!='A') {
  168.                             printf("Error: After ESC*r # no 'A' found!\n");
  169.                             return -1;
  170.                              }
  171.  
  172.                     if (c1=='1') return 4;
  173.                     x=0;y=0;
  174.                     return 4;
  175.                       }
  176.  
  177.               if (c=='p') {
  178.                     z=0;jj=1,rel=0;j=0;
  179.  
  180.                     if (c1=='-') {jj=-1;rel=1;c1=fgetc(eing);}
  181.                     if (c1=='+') {rel=1;c1=fgetc(eing);}
  182.  
  183.                     for (i=1;i<10;++i)
  184.                     {
  185.                     j1=c1-48;
  186.                     if (j1<0||j1>9) {
  187.                             printf("Error: character after ESC*p.%d. not readible!\n",j1+48);
  188.                             return -1;
  189.                             }
  190.  
  191.                     j=10*j;
  192.                     j=j+j1*jj;
  193.  
  194.                     c1=fgetc(eing);
  195.                     if (c1=='X'&& rel==0)   {
  196.                                  x=j;return 6;
  197.                                 }
  198.                     if (c1=='Y'&& rel==0)   {
  199.                                  y=j;return 7;
  200.                                 }
  201.                     if (c1=='X'&& rel!=0)   {
  202.                                  x=x+j;return 6;
  203.                                 }
  204.                     if (c1=='Y'&& rel!=0)   {
  205.                                  y=y+j;return 7;
  206.                                 }
  207.  
  208.                     }
  209.                       }
  210.               if (c=='b') {
  211.                     z=0;j=0;
  212.  
  213.                     for (i=1;i<10;++i)
  214.                     {
  215.                     j1=c1-48;
  216.                     if (j1<0||j1>9){
  217.                             printf ("Error: character after ESC*b.%d. not readible!\n",j1+48);
  218.                             printf ("Xpos: %d Ypos: %d \n",x,y);
  219.                             return -1;
  220.                                }
  221.  
  222.                     j=10*j;
  223.                     j=j+j1;
  224.  
  225.                     c1=fgetc(eing);
  226.                     if (c1=='W') {z=j;return 8;}
  227.  
  228.                     }
  229.  
  230.                        }
  231.              if (fgetc(eing)==EOF) return 100;
  232.  
  233.              goto schleife;
  234. }
  235.  
  236. fuelle_zeilen (a,b)
  237. int a,b;
  238.  
  239. {
  240.  
  241.   if (a>=b) return -1;
  242.   if (printing) printf ("Fills printer-rows %d - %d ...\n",a,b);
  243.  
  244.     if (zeilpos&&zeilpos<2399){
  245.                 for(i=zeilpos;i<2400;i++)zeile[i]=1;
  246.  
  247.                 printzeile();  /* printzeile () transfers the code and does 1:3 conversion..*/
  248.                 }
  249.     for (i=0;i<2400;i++) zeile[i]=1;
  250.  
  251.     for (i=a;i<b-1;i++) printzeile ();
  252.  
  253.     y1=b;
  254.  
  255. return 1;
  256. }
  257.  
  258. uebertragen(a,menge)
  259. int a,menge;
  260. {
  261.   unsigned char ze[300];
  262.  
  263.  
  264.  
  265.  for (i=0;i<menge;i++) ze[i]=fgetc(eing);
  266.  
  267.  
  268.  
  269.                    /* zeily=0 :row finished */
  270.  
  271.         if (y1<=y)  {
  272.                 for (i=zeilpos;i<2400;i++)zeile[i]=1;
  273.                 if (printing) printf("y1<y: line ready to print!\n");
  274.                 printzeile();
  275.                  }
  276.  
  277.  if (printing) printf("%d bytes at xpos %d ypos %d..\n",menge,a,y);
  278.  
  279.  
  280.  
  281.         for (i=zeilpos;i<x;i++)zeile[i]=1;
  282.              zeilpos=x;
  283.  
  284.         for (i=0;i<menge;i++)
  285.           {
  286.  
  287.            zeile[zeilpos++]=((ze[i]&128)/128)^1;
  288.  
  289.  
  290.            zeile[zeilpos++]=((ze[i]&64)/64)^1;
  291.  
  292.  
  293.            zeile[zeilpos++]=((ze[i]&32)/32)^1;
  294.  
  295.  
  296.            zeile[zeilpos++]=((ze[i]&16)/16)^1;
  297.  
  298.  
  299.            zeile[zeilpos++]=((ze[i]&8)/8)^1;
  300.  
  301.  
  302.            zeile[zeilpos++]=((ze[i]&4)/4)^1;
  303.  
  304.  
  305.            zeile[zeilpos++]=((ze[i]&2)/2)^1;
  306.  
  307.  
  308.            zeile[zeilpos++]=(ze[i]&1)^1;
  309.  
  310.  
  311.           }
  312.  
  313.         y++;y1=y;
  314.  
  315.  
  316.  
  317.  return 1;
  318.  
  319. }
  320.  
  321. printzeile()
  322. {
  323.  int counter,zp,ii;
  324.  char bits;
  325.  
  326.  zeilpos=0;  /*reset current printposition in line...*/
  327.  zp=0;
  328.  
  329.            /*  This must be changed in case of other relations
  330.  
  331.            than 1:3 !!!!!
  332.            */
  333.  
  334. for (counter=0;counter<dpi;counter++)
  335.  {
  336.  bits=128*(zeile[zp++]&zeile[zp++]&zeile[zp++]);
  337.  bits+=64*(zeile[zp++]&zeile[zp++]&zeile[zp++]);
  338.  bits+=32*(zeile[zp++]&zeile[zp++]&zeile[zp++]);
  339.  bits+=16*(zeile[zp++]&zeile[zp++]&zeile[zp++]);
  340.  bits+=8*(zeile[zp++]&zeile[zp++]&zeile[zp++]);
  341.  bits+=4*(zeile[zp++]&zeile[zp++]&zeile[zp++]);
  342.  bits+=2*(zeile[zp++]&zeile[zp++]&zeile[zp++]);
  343.  bits+=zeile[zp++]&zeile[zp++]&zeile[zp++];
  344.  
  345.  
  346.  drzeile[drpos][counter]=bits;
  347.  
  348. }
  349.  if (y3<y-1) {                                                          /* 3 lines are joint via OR - because of negative logic */
  350.                                     /* AND must be used....*/
  351.         if (test>=length/fak) {drpos=0;return -2;}
  352.  
  353.         for (ii=0;ii<dpi;ii++) {
  354.                     if (drpos==0) fputc(drzeile[0][ii],ausg);
  355.                     if (drpos==1) fputc(drzeile[0][ii]&drzeile[1][ii],ausg);
  356.                     if (drpos==2) fputc(drzeile[0][ii]&drzeile[1][ii]&drzeile[2][ii],ausg);
  357.  
  358.                        }
  359.                    /*    fputc(volli,ausg);fputc(volli,ausg);      */   /* this line can be necessary at other resolutions, see below..! */
  360.         if (printing)printf("Row %d transmitted because of jump >1!!\n",test);
  361.         test++;
  362.         y3=y;
  363.         drpos=0;
  364.         return 2;
  365.         }
  366.  
  367.  
  368.  drpos++;
  369.  if (drpos==fak) {
  370.            drpos=0;                                /* see above: 3 lines are joined to give one...*/
  371.            if (test>=length/fak) return -2;
  372.  
  373.            for (ii=0;ii<dpi;ii++) fputc (drzeile[0][ii]&drzeile[1][ii]&drzeile[2][ii],ausg);
  374.            /*          fputc(volli,ausg);if (test!=1299)fputc(volli,ausg);           */   /* This line can be necessary at other resolutions...*/
  375.            if (printing) printf("Row %d transmitted!\n",test);
  376.            test++;
  377.          }
  378.  
  379.  
  380.  y3=y;
  381. return 1;
  382. }