Last active
          October 5, 2019 03:18 
        
      - 
      
- 
        Save JettMonstersGoBoom/0226dd55686aeadcb31cf755a3455246 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include <stdio.h> | |
| #include <string.h> | |
| int encode_rle(unsigned char *src,int size,unsigned char *dst) | |
| { | |
| int stat[256]; | |
| int i,tag,sym,sym_prev,len,ptr; | |
| // count how often each byte is used , to find an unused byte | |
| memset(stat,0,sizeof(stat)); | |
| for(i=0;i<size;++i) ++stat[src[i]]; | |
| // find a free one | |
| tag=-1; | |
| for(i=0;i<256;++i) | |
| { | |
| if(!stat[i]) | |
| { | |
| tag=i; | |
| break; | |
| } | |
| } | |
| // if we fail , just fall out, I've not hit this yet | |
| if(tag<0) return -1; | |
| ptr=0; | |
| len=1; | |
| sym_prev=-1; | |
| // 1st byte is the tag | |
| // | |
| dst[ptr++]=tag; | |
| for(i=0;i<size;++i) | |
| { | |
| sym=src[i]; | |
| if(sym_prev!=sym||len>=255||i==size-1) | |
| { | |
| if(len>1) | |
| { | |
| if(len==2) | |
| { | |
| dst[ptr++]=sym_prev; | |
| } | |
| else | |
| { | |
| dst[ptr++]=tag; | |
| dst[ptr++]=len; | |
| } | |
| } | |
| dst[ptr++]=sym; | |
| sym_prev=sym; | |
| len=1; | |
| } | |
| else | |
| { | |
| ++len; | |
| } | |
| } | |
| dst[ptr++]=tag; //end of file marked with zero length rle | |
| dst[ptr++]=0; | |
| return ptr; | |
| } | |
| int main(int argc,char *argv[]) | |
| { | |
| unsigned char buffer[65536]; | |
| unsigned char obuffer[65536]; | |
| int len=0; | |
| int size; | |
| FILE *file=fopen(argv[1],"rb"); | |
| if(file) | |
| { | |
| fseek(file,0,SEEK_END); | |
| size=ftell(file); | |
| fseek(file,0,SEEK_SET); | |
| fread(&buffer[0x00], 1, size, file); | |
| fclose(file); | |
| } | |
| len = encode_rle(buffer,size,obuffer); | |
| FILE *fou = fopen(argv[2],"wb"); | |
| fwrite(obuffer,len,1,fou); | |
| fclose(fou); | |
| printf("original %d packed %d\n",size,len); | |
| return 0; | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | void vunrle(word dest,pointer source) { | |
| word index | |
| byte counter | |
| byte i | |
| byte value | |
| byte tag | |
| byte itag | |
| // vram address | |
| vout(VDP_CTRL,dest.lo) | |
| vout(VDP_CTRL,dest.hi) | |
| // tag byte is the 1st byte in the data stream | |
| tag = source[0] | |
| index = 1 | |
| while true { | |
| value = source[index] | |
| index+=1 | |
| itag = source[index] | |
| counter = 0 | |
| if itag==tag { | |
| index+=1 | |
| counter= source[index] | |
| if counter == 0 { | |
| return | |
| } | |
| index+=1 | |
| } | |
| // just write the byte out | |
| if counter==0 { | |
| vout(VDP_DATA,value) | |
| } | |
| else { | |
| // write a run of them out | |
| for i,0,until,counter { | |
| vout(VDP_DATA,value) | |
| } | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment