00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "..\animate\gif.c"
00027
00028 static int rowcount;
00029 static unsigned char *pixels = NULL;
00030
00031 int out_line(unsigned char *linepixels, int linelen){
00032 register long l;
00033 l = (long)(rowcount)*(long)(x_size);
00034 memcpy(pixels + l, linepixels, linelen);
00035 rowcount++;
00036 return 0;
00037 }
00038
00039 static unsigned char *LoadGIF(char *GIFfile, int *xx, int *yy){
00040 unsigned char *px,*lpB,*pixels24;
00041 long i,j,k,imagesize,x,y,xi,yi;
00042 rowcount=0;
00043 if(read_gif_image(GIFfile,1) < 0)return 0;
00044 x=x_size; y=y_size;
00045 imagesize=x_size*y_size;
00046 if((pixels=(unsigned char*)X__Malloc(imagesize)) == NULL)return 0;
00047 if((pixels24=(unsigned char*)X__Malloc(imagesize*3)) == NULL){
00048 X__Free(pixels);
00049 return NULL;
00050 }
00051 if(read_gif_image(GIFfile,0) < 0){
00052 X__Free(pixels);
00053 return NULL;
00054 }
00055 px=pixels24;
00056 lpB=pixels;
00057 for(j=0;j<y;j++){
00058 for(i=0;i<x;i++){
00059 k=(long)(*lpB++);
00060 *px++ = (unsigned char)(p_red[k] << 2);
00061 *px++ = (unsigned char)(p_green[k] << 2);
00062 *px++ = (unsigned char)(p_blue[k] << 2);
00063 }
00064 }
00065 *xx=x; *yy=y;
00066 X__Free(pixels);
00067 return pixels24;
00068 }
00069
00070 static void tga_display_line(unsigned char *prgb,
00071 long wide, long line,
00072 long upsidedown, long hi){
00073 register long l,x,k;
00074 if(!upsidedown)l = 3L*(long)(line)*(long)(x_size);
00075 else l = 3L*(long)(hi - 1 - line)*(long)(x_size);
00076 memcpy(pixels + l, prgb, (int)(wide*3));
00077 }
00078
00079 static short R_getvariword(long n, FILE *f, long *vw){
00080 long c;
00081 if((c=(long)getc(f)) == EOF)return 0;
00082 *vw = c; if(n == 1)return 1;
00083 if((c=(long)getc(f)) == EOF)return 0;
00084 *vw |= (c << 8); if(n == 2)return 1;
00085 if((c=(long)getc(f)) == EOF)return 0;
00086 *vw |= (c << 16); if(n == 3)return 1;
00087 if((c=(long)getc(f)) == EOF)return 0;
00088 *vw |= (c << 24);
00089 return 1;
00090 }
00091
00092 static short read_tga_image(char *filename, short info_only){
00093 char c0,c1,c2;
00094 long i,j,k,l,m,n,cmo,cml,xor,yor,wi,hi,iok=0,idsize,cm_type,vw,
00095 image_type,cmes,pixel_size,image_descriptor,n_attr,upsidedown,b_size,
00096 s1,s2,s,pat,wi3;
00097 FILE *t24;
00098 unsigned char *prgb;
00099 prgb=NULL;
00100 if((t24=fopen(filename,"rb")) != NULL){
00101 if(! R_getvariword(1,t24,&idsize))goto CLOSEOUT;
00102 if(! R_getvariword(1,t24,&cm_type))goto CLOSEOUT;
00103 if(! R_getvariword(1,t24,&image_type))goto CLOSEOUT;
00104 if(image_type != 2 && image_type != 10)goto CLOSEOUT;
00105 if(! R_getvariword(2,t24,&cmo))goto CLOSEOUT;
00106 if(! R_getvariword(2,t24,&cml))goto CLOSEOUT;
00107 if(! R_getvariword(1,t24,&cmes))goto CLOSEOUT;
00108 if(! R_getvariword(2,t24,&xor))goto CLOSEOUT;
00109 if(! R_getvariword(2,t24,&yor))goto CLOSEOUT;
00110 if(! R_getvariword(2,t24,&wi))goto CLOSEOUT;
00111 if(! R_getvariword(2,t24,&hi))goto CLOSEOUT;
00112 if(! R_getvariword(1,t24,&pixel_size))goto CLOSEOUT;
00113 if(! R_getvariword(1,t24,&image_descriptor))goto CLOSEOUT;
00114 if(idsize > 0)for(i=0;i<idsize;i++)if(getc(t24) == EOF)goto CLOSEOUT;
00115 if(cm_type == 1 && cml > 0){
00116 for(i=cmo;i<cml;i++)if(! R_getvariword(cmes/8,t24,&j))goto CLOSEOUT;
00117 }
00118 n_attr = image_descriptor & 0xf;
00119 upsidedown = (image_descriptor & 0x20) > 0 ? 0 : 1 ;
00120 if(info_only){
00121 x_size=(int)wi;
00122 y_size=(int)hi;
00123 fclose(t24);
00124 return 1;
00125 }
00126 if((prgb=(unsigned char *)X__Malloc((size_t)wi*3)) == NULL)goto CLOSEOUT;
00127 if((image_descriptor & 0xc0) > 0)goto CLOSEOUT;
00128 b_size=i=wi*hi;
00129 if (pixel_size == 16){n=2;s1=5;s2=10;s=3;pat=0x1f;}
00130 else if(pixel_size == 24){n=3;s1=8;s2=16;s=0;pat=0xff;}
00131 else {n=4;s1=8;s2=16;s=0;pat=0xff;}
00132 wi3=wi*3;
00133 if(image_type == 2){
00134 m=0; k=0; for(i=0;i<hi;i++)for(j=0;j<wi;j++){
00135 if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00136 prgb[m+2] = (char)( vw & pat) << s;
00137 prgb[m+1] = (char)((vw >> s1) & pat) << s;
00138 prgb[m ] = (char)((vw >> s2) & pat) << s;
00139 k++; m+=3;
00140 if(m == wi3){
00141 tga_display_line(prgb,wi,i,upsidedown,hi); m=0;
00142 }
00143 }
00144 }
00145 else{
00146 m=0; l=0; k=0; while(k < b_size){
00147 i=getc(t24); if(feof(t24))goto CLOSEOUT;
00148 if(i < 128){
00149 for(j=0;j<i+1;j++){
00150 if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00151 prgb[m+2] = (char)( vw & pat) << s;
00152 prgb[m+1] = (char)((vw >> s1) & pat) << s;
00153 prgb[m ] = (char)((vw >> s2) & pat) << s;
00154 k++; m+=3;
00155 if(m == wi3){
00156 tga_display_line(prgb,wi,l,upsidedown,hi); m=0; l++;
00157 }
00158 }
00159 }
00160 else{
00161 if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00162 c0 = (char)( vw & pat) << s;
00163 c1 = (char)((vw >> s1) & pat) << s;
00164 c2 = (char)((vw >> s2) & pat) << s;
00165 i -= 128;
00166 for(j=0;j<i+1;j++){
00167 prgb[m+2] = c0;
00168 prgb[m+1] = c1;
00169 prgb[m ] = c2;
00170 k++; m+=3;
00171 if(m == wi3){
00172 tga_display_line(prgb,wi,l,upsidedown,hi); m=0; l++;
00173 }
00174 }
00175 }
00176 }
00177 }
00178 iok=1;
00179 CLOSEOUT:
00180 fclose(t24);
00181 if(prgb != NULL)X__Free((char *)prgb);
00182 }
00183 return (short)iok;
00184 }
00185
00186 static unsigned char *LoadTGA(char *TGAfile, int *xx, int *yy){
00187 long imagesize;
00188 rowcount=0;
00189 if(read_tga_image(TGAfile,1) < 0)return 0;
00190 imagesize=x_size*y_size*3;
00191 if((pixels=(unsigned char*)X__Malloc(imagesize)) == NULL)return NULL;
00192 if(read_tga_image(TGAfile,0) < 0){
00193 X__Free(pixels);
00194 return NULL;
00195 }
00196 *xx=x_size; *yy=y_size;
00197 return pixels;
00198 }
00199
00200 unsigned char *LoadMAP(char *CurrentFile, int *xx, int *yy){
00201 int ff;
00202 if (strstr(CurrentFile,".GIF") || strstr(CurrentFile,".gif"))
00203 return LoadGIF(CurrentFile,xx,yy);
00204 else if(strstr(CurrentFile,".TGA") || strstr(CurrentFile,".tga"))
00205 return LoadTGA(CurrentFile,xx,yy);
00206 return NULL;
00207 }