home *** CD-ROM | disk | FTP | other *** search
/ CD-X 1 / cdx_01.iso / demodisc / basq / source / poweru / dcomp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-14  |  6.7 KB  |  339 lines

  1.     #include    <stdio.h>
  2.     #include    <process.h>
  3.     #include    <stdlib.h>
  4.     #include    <io.h>
  5.  
  6.     #include    "cache.h"
  7.  
  8.     int                        infile,
  9.                                 outfile,
  10.                                 samplerate;
  11.     unsigned char    header[44];
  12.  
  13. void compressing()
  14.     {
  15.     unsigned long    up=0,
  16.                                 tmed=0,
  17.                                 med=0,
  18.                                 down=0,
  19.                                 compcik,
  20.                                 infilelength,
  21.                                 pp,
  22.                                 prp;
  23.     unsigned char    top,
  24.                                 presbuf,
  25.                                 prevbuf,
  26.                                 prevfunc=2,
  27.                                 convbuf,
  28.                                 headercik;
  29.  
  30.     readablecache        cachein(infile);
  31.     writeablecache    cacheout(outfile);
  32.     if(cachein.status==BADCACHE||cacheout.status==BADCACHE)
  33.         {
  34.         printf("Not enough memory for caches!!\n\n");
  35.         exit(EXIT_FAILURE);
  36.         }
  37.     if(cachein.status==BADCACHE||cacheout.status==BADCACHE)
  38.         {
  39.         printf("Disk error!!\n\n");
  40.         exit(EXIT_FAILURE);
  41.         }
  42.     infilelength=filelength(infile)-1;
  43.     printf("Please wait...\n");
  44.  
  45.     for(headercik=0;headercik<44;headercik++)
  46.         {
  47.         header[headercik]=cachein.readitem();
  48.         if(cachein.status==BADCACHE)
  49.             {
  50.             printf("Read error in header checking!\n\n");
  51.             exit(EXIT_FAILURE);
  52.             }
  53.         }
  54.     prevbuf=cachein.readitem();
  55.     cacheout.writeitem(prevbuf);
  56.     pp=infilelength/1000;
  57.     prp=pp;
  58.     for(compcik=0;compcik<infilelength;compcik++)
  59.         {
  60.         presbuf=cachein.readitem();
  61. recucc:
  62.         if(presbuf<prevbuf)
  63.             {
  64.             if(up!=0&&down==0&&med==0)
  65.                 {
  66.                 med=tmed;
  67.                 tmed=0;
  68.                 top=prevbuf;
  69.                 }
  70.             if(prevfunc==1&&down!=0)
  71.                 {
  72.                 while(down)
  73.                     {
  74.                     if(down>63)
  75.                         {
  76.                         convbuf=127;
  77.                         down-=63;
  78.                         }
  79.                     else
  80.                         {
  81.                         convbuf=(char) down;
  82.                         if(med) convbuf|=128;
  83.                         down=0;
  84.                         }
  85.                     cacheout.writeitem(convbuf);
  86.                     }
  87.                 cacheout.writeitem(top);
  88.                 if(med)
  89.                     {
  90.                     while(med)
  91.                         {
  92.                         if(med>63)
  93.                             {
  94.                             convbuf=127;
  95.                             med-=63;
  96.                             }
  97.                         else
  98.                             {
  99.                             convbuf=(char) med;
  100.                             med=0;
  101.                             }
  102.                         cacheout.writeitem(convbuf);
  103.                         }
  104.                     }
  105.                 while(up)
  106.                     {
  107.                     if(up>63)
  108.                         {
  109.                         convbuf=127;
  110.                         up-=63;
  111.                         }
  112.                     else
  113.                         {
  114.                         convbuf=(char) up;
  115.                         up=0;
  116.                         }
  117.                     cacheout.writeitem(convbuf);
  118.                     }
  119.                 cacheout.writeitem(prevbuf);
  120.                 goto recucc;
  121.                 }
  122.             ++down+=tmed;
  123.             prevfunc=0;
  124.             tmed=0;
  125.             }
  126.         if(presbuf>prevbuf)
  127.             {
  128.             if(down!=0&&up==0&&med==0)
  129.                 {
  130.                 med=tmed;
  131.                 tmed=0;
  132.                 top=prevbuf;
  133.                 }
  134.             if(prevfunc==0&&up!=0)
  135.                 {
  136.                 while(up)
  137.                     {
  138.                     if(up>63)
  139.                         {
  140.                         convbuf=127;
  141.                         up-=63;
  142.                         }
  143.                     else
  144.                         {
  145.                         convbuf=(char) up;
  146.                         if(med) convbuf|=128;
  147.                         up=0;
  148.                         }
  149.                     cacheout.writeitem(convbuf);
  150.                     }
  151.                 cacheout.writeitem(top);
  152.                 if(med)
  153.                     {
  154.                     while(med)
  155.                         {
  156.                         if(med>63)
  157.                             {
  158.                             convbuf=127;
  159.                             med-=63;
  160.                             }
  161.                         else
  162.                             {
  163.                             convbuf=(char) med;
  164.                             med=0;
  165.                             }
  166.                         cacheout.writeitem(convbuf);
  167.                         }
  168.                     }
  169.                 while(down)
  170.                     {
  171.                     if(down>63)
  172.                         {
  173.                         convbuf=127;
  174.                         down-=63;
  175.                         }
  176.                     else
  177.                         {
  178.                         convbuf=(char) down;
  179.                         down=0;
  180.                         }
  181.                     cacheout.writeitem(convbuf);
  182.                     }
  183.                 cacheout.writeitem(prevbuf);
  184.                 goto recucc;
  185.                 }
  186.             ++up+=tmed;
  187.             prevfunc=1;
  188.             tmed=0;
  189.             }
  190.         if(presbuf==prevbuf) tmed++;
  191.         prevbuf=presbuf;
  192.         prp++;
  193.         if(prp>pp||infilelength-2<compcik)
  194.             {
  195.             prp=0;
  196.             printf("\r%.1f%% compressed.",((double)compcik*100/infilelength));
  197.             }
  198.         }
  199.     cacheout.~writeablecache();
  200.     cachein.~readablecache();
  201.     printf("\nCompressing has successful.\n");
  202.  
  203.     long    iflength=filelength(infile),
  204.                 oflength=filelength(outfile);
  205.  
  206.     printf("The wav file size:%li bytes\n",iflength);
  207.     printf("The dgm file size:%li bytes\n",oflength);
  208.     printf("Compressed ratio:%f%%",((float)oflength/iflength)*100);
  209.     }
  210.  
  211. void decompressing()
  212.     {
  213.     long                    flength,
  214.                                 filelengthcik,
  215.                                 extractcount,
  216.                                 extractcik,
  217.                                 pp,
  218.                                 prp,
  219.                                 exc;
  220.     int                        data;
  221.     unsigned char    equ=0,
  222.                                 presbuf,
  223.                                 prevbuf,
  224.                                 equbuf;
  225.  
  226.     readablecache   cachein(infile);
  227.     writeablecache  cacheout(outfile);
  228.     if(cachein.status==BADCACHE||cacheout.status==BADCACHE)
  229.         {
  230.         printf("Cache memory error!!\n\n");
  231.         exit(EXIT_FAILURE);
  232.         }
  233.     printf("Please wait...\n");
  234.     prevbuf=cachein.readitem();
  235.     cacheout.writeitem(prevbuf);
  236.     flength=filelength(infile)-1;
  237.     pp=flength/1000;
  238.     prp=pp;
  239.     for(filelengthcik=0;filelengthcik<flength;filelengthcik++)
  240.         {
  241.         extractcount=0;
  242. rread:
  243.         presbuf=cachein.readitem();
  244.         if((presbuf&64)!=0)
  245.             {
  246.             presbuf-=64;
  247.             extractcount+=(long) presbuf;
  248.             filelengthcik++;
  249.             goto rread;
  250.             }
  251.         if((presbuf&128)!=0)
  252.             {
  253.             equ=1;
  254.             presbuf-=128;
  255.             }
  256.         extractcount+=(long) presbuf;
  257.         presbuf=cachein.readitem();
  258.         filelengthcik++;
  259.         data=presbuf-prevbuf;
  260.         for(extractcik=1;extractcik<=extractcount;extractcik++)
  261.             {
  262.             exc=((long)data*extractcik/extractcount);
  263.             cacheout.writeitem(prevbuf+(char)exc);
  264.             }
  265.         if(equ)
  266.             {
  267.             equ=0;
  268.             extractcount=0;
  269. rreadd:
  270.             equbuf=cachein.readitem();
  271.             filelengthcik++;
  272.             if((equbuf&64)!=0)
  273.                 {
  274.                 equbuf-=64;
  275.                 extractcount+=(long) equbuf;
  276.                 goto rreadd;
  277.                 }
  278.             extractcount+=(long) equbuf;
  279.             for(extractcik=1;extractcik<=extractcount;extractcik++) cacheout.writeitem(presbuf);
  280.             }
  281.         prevbuf=presbuf;
  282.         prp++;
  283.         if(pp<=prp||flength-2<filelengthcik)
  284.             {
  285.             prp=0;
  286.             printf("\r%.1f%% decompressed.",((double)filelengthcik*100/flength));
  287.             }
  288.         }
  289.     printf("\nDecompressing has successful.\n");
  290.     cacheout.~writeablecache();
  291.     cachein.~readablecache();
  292.  
  293.     long    iflength=filelength(infile),
  294.                 oflength=filelength(outfile);
  295.  
  296.     printf("The dgm file size:%li bytes\n",iflength);
  297.     printf("The wav file size:%li bytes",oflength);
  298.     }
  299.  
  300. void main(int parnum,char *pars[])
  301.     {
  302.     printf("Digi Compresser v1.0ß by Psycho/TSI (C) '93\n\n");
  303.     if(parnum!=4)
  304.         {
  305.         printf("Too ");
  306.         if(parnum<4) printf("few ");
  307.         else printf("much ");
  308. exiting:
  309.         printf("parameters!!\n");
  310. exits:
  311.         printf("USAGE: dcomp <command> <infile> <outfile>\n");
  312.         printf("             commands: d - convert wav to dgm\n");
  313.         printf("                       w - convert dgm to wav\n\n");
  314.         exit(EXIT_FAILURE);
  315. foperror:
  316.         printf("File opening error!!\n");
  317.         goto exits;
  318.         }
  319.     if(*pars[1]=='d')
  320.         {
  321.         if((infile=open(pars[2],O_RDONLY|O_BINARY))==-1||(outfile=open(pars[3],O_RDWR|O_BINARY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE))==-1)
  322.             goto foperror;
  323.         compressing();
  324.         }
  325.     else
  326.         if(*pars[1]!='w')
  327.             {
  328.             printf("Bad ");
  329.             goto exiting;
  330.             }
  331.         else
  332.             {
  333.             if((infile=open(pars[2],O_RDONLY|O_BINARY))==-1||(outfile=open(pars[3],O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IREAD|S_IWRITE))==-1) goto foperror;
  334.             decompressing();
  335.             }
  336.     printf("\n\n");
  337.     exit(EXIT_FAILURE);
  338.     }
  339.