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

  1. #include<stdio.h>
  2. #include<fcntl.h>
  3. #include<sys\types.h>
  4. #include<sys\stat.h>
  5. #include<stdlib.h>
  6. #include<malloc.h>
  7.  
  8. int rfxfd;
  9.  
  10. struct header_s
  11.     {
  12.     unsigned char mfgr;
  13.     unsigned char version;
  14.     unsigned char encoding;
  15.     unsigned char bppix;
  16.     unsigned short xmin;
  17.     unsigned short ymin;
  18.     unsigned short xmax;
  19.     unsigned short ymax;
  20.     unsigned short hdpi;
  21.     unsigned short vdpi;
  22.     unsigned char cmap[48];
  23.     unsigned char reserved;
  24.     unsigned char nplanes;
  25.     unsigned short bypl;
  26.     unsigned short paltinf;
  27.     unsigned short hscrnsz;
  28.     unsigned short vscrnsz;
  29.     unsigned char filler[54];
  30.     };
  31.  
  32. int dcsvr;
  33.  
  34. int pcxfd, faxlines;
  35. struct header_s header;
  36.  
  37. pcxwriteheader()
  38.     {
  39.     int i;
  40.     header.mfgr=10;
  41.     header.version=45;
  42.     header.encoding=1;
  43.     header.bppix=1;
  44.     header.xmin=0;
  45.     header.ymin=0;
  46.     header.xmax=1727;
  47.     header.ymax=(faxlines*(2-dcsvr))-1;
  48.     header.hdpi=200;
  49.     header.vdpi=192;
  50.     header.reserved=0;
  51.     header.nplanes=1;
  52.     header.bypl=216;
  53.     header.paltinf=1;
  54.     header.hscrnsz=0;
  55.     header.vscrnsz=0;
  56.     for(i=0;i<54;++i)
  57.         header.filler[i]=0;
  58.     lseek(pcxfd, (long)0, SEEK_SET);
  59.     if(write(pcxfd, &header, 128)!=128)
  60.         {
  61.         printf("Error overwriting final pcx file header.\n");
  62.         exit(9);
  63.         }
  64.     }
  65.  
  66. #define RBUFSIZ 16384
  67.  
  68. unsigned char rbuf[RBUFSIZ];
  69. int rbufindex, rbufn;
  70.  
  71. int rch(cptr)
  72.     unsigned char *cptr;
  73.     {
  74.     if(rbufindex>=rbufn)
  75.         {
  76.         if((rbufn=read(rfxfd, rbuf, RBUFSIZ))<=0)
  77.             return(0);
  78.         rbufindex=0;
  79.         }
  80.     (*cptr)=rbuf[rbufindex++];
  81.     return(1);
  82.     }        
  83.  
  84. unsigned char fcfr[]="+FCFR\r\n\r\n";
  85. unsigned char fhng[]="+FHNG";
  86. unsigned char connect[]="CONNECT\r\n";
  87.  
  88. int scanfcfr()
  89.     {
  90.     int i, j, k;
  91.     unsigned char c;
  92.     i=j=k=0;
  93.     while(rch(&c))
  94.         {
  95.         if(fcfr[i]==c)
  96.             {
  97.             i++;
  98.             if(fcfr[i]=='\0')
  99.                 return(1);
  100.             }
  101.         else
  102.             i=0;
  103.         if(connect[k]==c)
  104.             {
  105.             k++;
  106.             if(connect[k]=='\0')
  107.                 return(0);
  108.             }
  109.         else
  110.             k=0;
  111.         if(fhng[j]==c)
  112.             {
  113.             j++;
  114.             if(fhng[j]=='\0')
  115.                 {
  116.                 printf("End of document. Exit code +FHNG");
  117.                 while(1)
  118.                     {
  119.                     if(!rch(&c))
  120.                         {
  121.                         printf("\nPremature end of file while seeking +FHNG code.\n");
  122.                         exit(6);
  123.                         }
  124.                     putchar(c);
  125.                     if(c=='\n')
  126.                         break;
  127.                     }
  128.                 exit(0);
  129.                 }
  130.             }
  131.         else
  132.             j=0;
  133.         }
  134.     printf("Premature end of file while seeking +FCFR.\n");
  135.     exit(5);
  136.     }
  137.  
  138. int scanconnect()
  139.     {
  140.     int i;
  141.     unsigned char c;
  142.     i=0;
  143.     while(rch(&c))
  144.         {
  145.         if(connect[i]==c)
  146.             {
  147.             i++;
  148.             if(connect[i]=='\0')
  149.                 return(0);
  150.             }
  151.         else
  152.             i=0;
  153.         }
  154.     printf("Premature end of file while seeking CONNECT.\n");
  155.     exit(7);
  156.     }
  157.  
  158. int rfxgets(str)
  159.     unsigned char *str;
  160.     {
  161.     int i;
  162.     i=0;
  163.     while(rch(&str[i]))
  164.         if(str[i]=='\r')
  165.             {
  166.             str[i]='\0';
  167.             return(i);
  168.             }
  169.         else
  170.             i++;
  171.     return(-1);
  172.     }
  173.  
  174. #define WBUFSIZ 17248
  175. #define WBUFTHRESH 16384
  176.  
  177. unsigned char wbuf[WBUFSIZ];
  178. int wbufindex, wlinebeg;
  179.  
  180. int linenbits;
  181. unsigned char curwbyte;
  182. int curwbytebit;
  183.  
  184. int putwhite(n)
  185.     int n;
  186.     {
  187.     int i, fragn;
  188.     unsigned char mask;
  189.     linenbits+=n;
  190.     if(linenbits>1728)
  191.         return(0);
  192.     if(n<(fragn=8-curwbytebit))
  193.         {
  194.         for(i=fragn,mask=1;i;--i)
  195.             {
  196.             curwbyte|=mask;
  197.             mask<<=1;
  198.             }
  199.         curwbytebit+=n;
  200.         return(1);
  201.         }
  202.     for(i=fragn, mask=1;i;i--)
  203.         {
  204.         curwbyte|=mask;
  205.         mask<<=1;
  206.         }
  207.     n-=fragn;
  208.     if((curwbyte&0xc0)==0xc0)
  209.         wbuf[wbufindex++]=0xc1;
  210.     wbuf[wbufindex++]=curwbyte;
  211.     curwbytebit=0;
  212.     fragn=(n>>3)&0x1fff;
  213.     while(fragn>=63)
  214.         {
  215.         wbuf[wbufindex++]=0xff;
  216.         wbuf[wbufindex++]=0xff;
  217.         fragn-=63;
  218.         }
  219.     if(fragn)
  220.         {
  221.         wbuf[wbufindex++]=(0xc0|fragn);
  222.         wbuf[wbufindex++]=0xff;
  223.         }
  224.     curwbyte=0xff;
  225.     curwbytebit=(n&0x0007);
  226.     return(1);
  227.     }
  228.  
  229. int putblack(n)
  230.     int n;
  231.     {
  232.     int i, fragn;
  233.     unsigned char mask;
  234.     linenbits+=n;
  235.     if(linenbits>1728)
  236.         return(0);
  237.     if(n<(fragn=8-curwbytebit))
  238.         {
  239.         for(i=fragn,mask=0xfe;i;--i)
  240.             {
  241.             curwbyte&=mask;
  242.             mask<<=1;
  243.             }
  244.         curwbytebit+=n;
  245.         return(1);
  246.         }
  247.     for(i=fragn, mask=0xfe;i;i--)
  248.         {
  249.         curwbyte&=mask;
  250.         mask<<=1;
  251.         }
  252.     n-=fragn;
  253.     if((curwbyte&0xc0)==0xc0)
  254.         wbuf[wbufindex++]=0xc1;
  255.     wbuf[wbufindex++]=curwbyte;
  256.     curwbytebit=0;
  257.     fragn=(n>>3)&0x1fff;
  258.     while(fragn>=63)
  259.         {
  260.         wbuf[wbufindex++]=0xff;
  261.         wbuf[wbufindex++]=0x00;
  262.         fragn-=63;
  263.         }
  264.     if(fragn)
  265.         {
  266.         wbuf[wbufindex++]=(0xc0|fragn);
  267.         wbuf[wbufindex++]=0x00;
  268.         }
  269.     curwbyte=0x00;
  270.     curwbytebit=(n&0x0007);
  271.     return(1);
  272.     }
  273.  
  274. int pcxwriteline()
  275.     {
  276.     int i, count;
  277.     if(!linenbits)
  278.         return(0); /* Special case extra EOLs (start and finish) */
  279.     if(linenbits!=1728)
  280.         {
  281.         printf("\rLine %d bad length: %d pels.\n", faxlines, linenbits);
  282.         wbufindex=wlinebeg;
  283.         return(0);
  284.         }
  285.     if((faxlines++&0x001f)==0x001f)
  286.         printf("\rLine %d", faxlines);
  287.     if(!dcsvr)
  288.         for(i=wlinebeg, count=wbufindex-wlinebeg;count;i++, wbufindex++, count--)
  289.             wbuf[wbufindex]=wbuf[i];
  290.     if((wlinebeg=wbufindex)>=WBUFTHRESH)
  291.         {
  292.         if(write(pcxfd, wbuf, wbufindex)!=wbufindex)
  293.             {
  294.             printf("\nPCX write error.\n");
  295.             exit(111);
  296.             }
  297.         wbufindex=wlinebeg=0;
  298.         }
  299.     linenbits=0;
  300.     }
  301.  
  302. abortpage()
  303.     {
  304.     unsigned char c;
  305.     printf("Flushing page...\n");
  306.     while(1)
  307.         {
  308.         if(!rch(&c))
  309.             {
  310.             printf("Premature end of file while flushing bad page.\n");
  311.             exit(30);
  312.             }
  313.         if(c==0x10)
  314.             {
  315.             if(!rch(&c))
  316.                 {
  317.                 printf("Premature end of file while flushing bad page.\n");
  318.                 exit(30);
  319.                 }
  320.             if(c==0x03)
  321.                 break;
  322.             }
  323.         }
  324.     }
  325.  
  326. unsigned char *states, *results1, *results2, *results3;
  327.  
  328. main(argc, argv)
  329.     int argc;
  330.     char **argv;
  331.     {
  332.     int fd, dcswd, dcsdf, page, bitcount, oflag;
  333.     int npelschk, gotfcfr;
  334.     unsigned char c, str[256];
  335.     union
  336.         {
  337.         unsigned word;
  338.         struct
  339.             {
  340.             unsigned char lobyte;
  341.             unsigned char hibyte;
  342.             }
  343.             bytes;
  344.         }
  345.         mess;
  346.     unsigned char *tilo, *tihi;
  347.     unsigned *tindex;
  348.     printf("Copyright (C) 1992 Peter Edward Cann\n\n");
  349.     if(argc!=2)
  350.         {
  351.         printf("USAGE: rfx2pcx <rfx filename>\n");
  352.         exit(10);
  353.         }
  354.     tilo=&(mess.bytes.lobyte);
  355.     tihi=&(mess.bytes.hibyte);
  356.     tindex=&(mess.word);
  357.     rbufindex=rbufn=wbufindex=wlinebeg=0;
  358.     curwbytebit=0;
  359.     if((states=malloc(57856))==NULL)
  360.         {
  361.         printf("Malloc error for states.\n");
  362.         exit(1);
  363.         }
  364.     if((results1=malloc(57856))==NULL)
  365.         {
  366.         printf("Malloc error for results1.\n");
  367.         exit(1);
  368.         }
  369.     if((results2=malloc(57856))==NULL)
  370.         {
  371.         printf("Malloc error for results2.\n");
  372.         exit(1);
  373.         }
  374.     if((results3=malloc(57856))==NULL)
  375.         {
  376.         printf("Malloc error for results3.\n");
  377.         exit(1);
  378.         }
  379.     if(getenv("PCCPPATH")==NULL)
  380.         sprintf(str, "statmach.dat");
  381.     else
  382.         sprintf(str, "%s\\statmach.dat", getenv("PCCPPATH"));
  383.     if((fd=open(str, O_RDONLY|O_BINARY))==-1)
  384.         {
  385.         printf("Unable to open state machine file %s.\n", str);
  386.         exit(2);
  387.         }
  388.     if(read(fd, states, 57856)!=(unsigned)57856)
  389.         {
  390.         printf("Read error on states.\n");
  391.         exit(3);
  392.         }
  393.     if(read(fd, results1, 57856)!=(unsigned)57856)
  394.         {
  395.         printf("Read error on results1.\n");
  396.         exit(3);
  397.         }
  398.     if(read(fd, results2, 57856)!=(unsigned)57856)
  399.         {
  400.         printf("Read error on results2.\n");
  401.         exit(3);
  402.         }
  403.     if(read(fd, results3, 57856)!=(unsigned)57856)
  404.         {
  405.         printf("Read error on results3.\n");
  406.         exit(3);
  407.         }
  408.     close(fd);
  409.     printf("State machine table allocated and loaded successfully.\n\n");
  410.     (*tindex)=0;
  411.     if((rfxfd=open(argv[1], O_RDONLY|O_BINARY))==-1)
  412.         {
  413.         printf("Error opening %s for read.\n", argv[1]);
  414.         exit(11);
  415.         }
  416.     oflag=0;
  417.     while(1)
  418.         {
  419.         if(!rch(&c))
  420.             {
  421.             printf("Premature end of file while seeking initial doublequote.\n");
  422.             exit(12);
  423.             }
  424.         if(c=='"')
  425.             {
  426.             oflag=0;
  427.             break;
  428.             }
  429.         if(oflag)
  430.             if(c=='K')
  431.                 break;
  432.             else
  433.                 oflag=0;
  434.         if(c=='O')
  435.             oflag=1;
  436.         }
  437.     printf("Sender ID: ");
  438.     if(oflag)
  439.         printf("Not specified.");
  440.     else
  441.         while(1)
  442.             {
  443.             if(!rch(&c))
  444.                 {
  445.                 printf("Premature end of file while seeking final doublequote.\n");
  446.                 exit(13);
  447.                 }
  448.             if(c=='"')
  449.                 break;
  450.             putchar(c);
  451.             }
  452.     putchar('\n');
  453.     page=0;
  454.     gotfcfr=0;
  455.     while(1)
  456.         {
  457.         page++;
  458.         if(scanfcfr()) /* Normal exit is in this function */
  459.             {
  460.             if(rfxgets(str)==-1)
  461.                 {
  462.                 printf("Premature end of file while seeking +FDCS.\n");
  463.                 exit(15);
  464.                 }
  465.             if(sscanf(str, " +FDCS: %d,%*d,%d,%*d,%d,%*d,%*d,%*d",
  466.                 &dcsvr, &dcswd, &dcsdf)!=3)
  467.                 {
  468.                 printf("Error scanning +FDCS params.\n");
  469.                 exit(14);
  470.                 }
  471.             else
  472.                 gotfcfr=1;
  473.             if(dcsvr)
  474.                 printf("High vertical resolution.\n");
  475.             else
  476.                 printf("Standard vertical resolution.\n");
  477.             if(dcswd)
  478.                 {
  479.                 printf("Non-supported width!\n");
  480.                 exit(13);
  481.                 }
  482.             if(dcsdf)
  483.                 {
  484.                 printf("Non-supported coding scheme (must be 1-D)!\n");
  485.                 exit(14);
  486.                 }
  487.             scanconnect();
  488.             gotfcfr=1;
  489.             }
  490.         if(!gotfcfr)
  491.             {
  492.             printf("Never got initial +FCFR; aborting.\n");
  493.             exit(60);
  494.             }
  495.         while(1)
  496.             {
  497.             printf("Enter filename for Page %d (include .PCX):\n--> ", page);
  498.             gets(str);
  499.             if((pcxfd=open(str, O_CREAT|O_BINARY|O_TRUNC|O_WRONLY, S_IWRITE))!=-1)
  500.                 break;
  501.             else
  502.                 printf("Error opening file.\n");
  503.             }
  504.         /* Save a dummy block for PCX file header */
  505.         if(write(pcxfd, str, 128)!=128)
  506.             {
  507.             printf("Error writing dummy header to file.\n");
  508.             exit(20);
  509.             }
  510.         /* Translate */
  511.         faxlines=0;
  512.         linenbits=0;
  513.         /* flush 1 bits */
  514.         do
  515.             if(rch(&c)!=1)
  516.                 break;
  517.         while(c==0xff);
  518.         /* If we prime state machine with 0x0000 we can trash first */
  519.         /* byte that isn't 0xff and still recognize EOL. */
  520.         (*tihi)=states[0x0000];
  521.         (*tilo)=0x00;
  522.         (*tihi)=states[*tindex];
  523.         while(1)
  524.             {
  525.             if(rbufindex>=rbufn)
  526.                 if(rch(tilo)!=1)
  527.                     {
  528.                     printf("Premature EOF.\n");
  529.                     break;
  530.                     }
  531.                 else;
  532.             else
  533.                 (*tilo)=rbuf[rbufindex++];
  534.             if((*tilo)==0x10)
  535.                 {
  536.                 if(rch(tilo)!=1)
  537.                     {
  538.                     printf("Premature EOF.\n");
  539.                     break;
  540.                     }
  541.                 if((*tilo)!=0x10)
  542.                     break; /* No randomness allowed. */
  543.                 }
  544.             if((c=results1[*tindex])!=255)
  545.                 {
  546.                 if(c==254)
  547.                     pcxwriteline();
  548.                 else
  549.                     {
  550.                     if(c&0x80)
  551.                         if(c&0x40)
  552.                             putblack(((int)(c&63))<<6);
  553.                         else
  554.                             putblack(c&63);
  555.                     else
  556.                         if(c&0x40)
  557.                             putwhite(((int)(c&63))<<6);
  558.                         else
  559.                             putwhite(c);
  560.                     }
  561.                 if((c=results2[*tindex])!=255)
  562.                     {
  563.                     if(c==254)
  564.                         pcxwriteline();
  565.                     else
  566.                         {
  567.                         if(c&0x80)
  568.                             if(c&0x40)
  569.                                 putblack(((int)(c&63))<<6);
  570.                             else
  571.                                 putblack(c&63);
  572.                         else
  573.                             if(c&0x40)
  574.                                 putwhite(((int)(c&63))<<6);
  575.                             else
  576.                                 putwhite(c);
  577.                         }
  578.                     if((c=results3[*tindex])!=255)
  579.                         {
  580.                         if(c==254)
  581.                             pcxwriteline();
  582.                         else
  583.                             {
  584.                             if(c&0x80)
  585.                                 if(c&0x40)
  586.                                     putblack(((int)(c&63))<<6);
  587.                                 else
  588.                                     putblack(c&63);
  589.                             else
  590.                                 if(c&0x40)
  591.                                     putwhite(((int)(c&63))<<6);
  592.                                 else
  593.                                     putwhite(c);
  594.                             }
  595.                         }
  596.                     }
  597.                 }
  598.             (*tihi)=states[*tindex];
  599.             }
  600.         printf("\rLine %d\n", faxlines);
  601.         if(wbufindex) /* Flush */
  602.             {
  603.             if(write(pcxfd, wbuf, wbufindex)!=wbufindex)
  604.                 {
  605.                 printf("PCX write error.\n");
  606.                 exit(112);
  607.                 }
  608.             wbufindex=wlinebeg=0;
  609.             }
  610.         pcxwriteheader();
  611.         close(pcxfd);
  612.         printf("Page translate completed.\n");
  613.         }
  614.     }
  615.