/* * reads all the TIM informations out of the file and stores them into * the dataStructure defined in TIM.h * * all routines written by Yannick Suter aka AsC/Nowadays! 1998 * * no comments. if you have questions email me: [asc@netlink.ch] */ #include #include #include #include "tim.h" extern FILE *TIMf; extern TIMinf TIMinfo; extern unsigned char infile2[100]; int openTIM(char *file){ if(TIMf = fopen(file,"rb")) return(1); else return(0); } void closeTIM(){ fclose(TIMf); } void read_TIMid(){ unsigned char ID[4]; fread(&ID,1,4,TIMf); TIMinfo.ID = ID[0]; TIMinfo.version = ID[1]; } void read_TIMflag(){ unsigned char FLAG[4]; int *t,fl,cts; printf(" : READING [TIM] flags\n"); fread(&FLAG,1,4,TIMf); t = (int *)FLAG; fl = (*t & 7); cts = (*t & 8)>>3; TIMinfo.pmode = fl; TIMinfo.cts = cts; } void read_TIMclut(){ unsigned char CLUTlen[4]; unsigned char CLUTframe[4]; unsigned char CLUTsize[4]; unsigned long *t,fx,fy; printf(" : READING [TIM] cluts\n"); fread(&CLUTlen,1,4,TIMf); t = (unsigned long *)CLUTlen; fread(&CLUTframe,1,4,TIMf); t = (unsigned long *)CLUTframe; fx = fy = *t; fx = fx & 65535; fy = (fy>>16) & 65535; TIMinfo.cDX = fx; TIMinfo.cDY = fy; fread(&CLUTsize,1,4,TIMf); t = (unsigned long *)CLUTsize; fx = fy = *t; fx = fx & 65535; fy = (fy>>16) & 65535; TIMinfo.cW = fx; TIMinfo.cH = fy; printf(" : READING [TIM] palette\n"); if(TIMinfo.pmode==0)read_TIMpal_4bit(); if(TIMinfo.pmode==1)read_TIMpal_8bit(); } void read_TIMdata(){ unsigned char CLUTlen[4]; unsigned char CLUTframe[4]; unsigned char CLUTsize[4]; unsigned long *t,fx,fy; printf(" : READING [TIM] picture\n"); fread(&CLUTlen,1,4,TIMf); t = (unsigned long *)CLUTlen; fread(&CLUTframe,1,4,TIMf); t = (unsigned long *)CLUTframe; fx = fy = *t; fx = fx & 65535; fy = (fy>>16) & 65535; TIMinfo.pDX = fx; TIMinfo.pDY = fy; fread(&CLUTsize,1,4,TIMf); t = (unsigned long *)CLUTsize; fx = fy = *t; fx = fx & 65535; fy = (fy>>16) & 65535; TIMinfo.pW = fx; TIMinfo.pH = fy; if(TIMinfo.pmode == 0)read_TIMpic_4bit(); if(TIMinfo.pmode == 1)read_TIMpic_8bit(); if(TIMinfo.pmode == 2)read_TIMpic_15bit(); if(TIMinfo.pmode == 3)read_TIMpic_24bit_reve(); } void read_TIMpal_4bit(){ unsigned char clutp[2]; unsigned int x,*r,rr,*g,gg,*b,bb,ind = 0; for(x=0;x<16;x++){ fread(&clutp,1,2,TIMf); r = (unsigned int *)clutp; rr = *r; rr = (rr & 31)*8; g = (unsigned int *)clutp; gg = *g; gg = ((gg>>5) & 31)*8; b = (unsigned int *)clutp; bb = *b; bb = ((bb>>10) & 31)*8; TIMinfo.bit4_pal[ind] = rr;TIMinfo.bit4_pal[ind+1] = gg;TIMinfo.bit4_pal[ind+2] = bb; ind+=3; } } void read_TIMpal_8bit(){ unsigned char clutp[2]; unsigned int x,*r,rr,*g,gg,*b,bb,ind = 0; for(x=0;x<256;x++){ fread(&clutp,1,2,TIMf); r = (unsigned int *)clutp; rr = *r; rr = (rr & 31)*8; g = (unsigned int *)clutp; gg = *g; gg = ((gg>>5) & 31)*8; b = (unsigned int *)clutp; bb = *b; bb = ((bb>>10) & 31)*8; TIMinfo.bit8_pal[ind] = rr;TIMinfo.bit8_pal[ind+1] = gg;TIMinfo.bit8_pal[ind+2] = bb; ind+=3; } } void read_TIMpic_4bit(){ long x,y,ind=0; unsigned char *picP; TIMinfo.picW = TIMinfo.pW*4; TIMinfo.picH = TIMinfo.pH; TIMinfo.picdata_char = (unsigned char *)malloc((TIMinfo.picW>>1)*TIMinfo.picH); picP = TIMinfo.picdata_char; ind = (TIMinfo.picW>>1)*TIMinfo.picH; fread(picP,1,ind,TIMf); } void read_TIMpic_8bit(){ long x,y,ind=0; unsigned char c,*picP; TIMinfo.picW = TIMinfo.pW*2; TIMinfo.picH = TIMinfo.pH; TIMinfo.picdata_char = (unsigned char *)malloc(TIMinfo.picW*TIMinfo.picH); picP = TIMinfo.picdata_char; for(y=0;y>5) & 31); b = (unsigned int *)clutp; bb = *b; bb = ((bb>>10) & 31); *(picP) = bb<<3; picP++; *(picP) = gg<<3; picP++; *(picP) = rr<<3; picP++; } } void read_TIMpic_24bit_reve(){ long datalen = 0; unsigned char *picP; TIMinfo.picW = ((long)(TIMinfo.pW/3)<<1); TIMinfo.picH = TIMinfo.pH; datalen = TIMinfo.picW*TIMinfo.picH*3; TIMinfo.picdata_char = (unsigned char *)malloc(datalen); picP = (unsigned char *)TIMinfo.picdata_char; fread(picP,1,datalen,TIMf); } void read_TIM(char *filename){ if(openTIM(filename)){ printf("  OPEN [TIM] file [%s]\n",infile2); read_TIMid(); read_TIMflag(); if(TIMinfo.cts)read_TIMclut(); read_TIMdata(); closeTIM(); } else printf(" : couldn't open TIMfile [%s]\n",filename); }