TRACE1.C

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 2.0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 - 2008 OpenFX Development Team
00004 -- */
00005 
00006 /* file TRACE1.C  include in trace.c */
00007 
00008 static void tga_display_line(unsigned char *prgb,
00009                              long wide, long line,
00010                              long upsidedown, long hi){
00011  register long l,x,k;
00012  if(upsidedown)l = 3L*(long)(line)*(long)(x4_size);
00013  else          l = 3L*(long)(hi - 1 - line)*(long)(x4_size);
00014  memcpy(pixels + l, prgb, (int)(wide*3));
00015 }
00016 
00017 static short R_getvariword(long n, FILE *f, long *vw){
00018  long c;
00019  if((c=(long)getc(f)) == EOF)return 0;
00020  *vw = c;          if(n == 1)return 1;
00021  if((c=(long)getc(f)) == EOF)return 0;
00022  *vw |= (c << 8);  if(n == 2)return 1;
00023  if((c=(long)getc(f)) == EOF)return 0;
00024  *vw |= (c << 16); if(n == 3)return 1;
00025  if((c=(long)getc(f)) == EOF)return 0;
00026  *vw |= (c << 24);
00027  return 1;
00028 }
00029 
00030 static short LoadTGA(char *filename, short info_only){
00031  char c0,c1,c2;
00032  long i,j,k,l,m,n,cmo,cml,xor,yor,wi,hi,iok=0,idsize,cm_type,vw,
00033       image_type,cmes,pixel_size,image_descriptor,n_attr,upsidedown,b_size,
00034       s1,s2,s,pat,wi3;
00035  FILE *t24;
00036  unsigned char *prgb;
00037  prgb=NULL;
00038  if((t24=fopen(filename,"rb")) != NULL){
00039    if(! R_getvariword(1,t24,&idsize))goto CLOSEOUT;
00040    if(! R_getvariword(1,t24,&cm_type))goto CLOSEOUT; /* 0 for non mapped */
00041    if(! R_getvariword(1,t24,&image_type))goto CLOSEOUT; /* image type */
00042    if(image_type != 2 && image_type != 10)goto CLOSEOUT;
00043    if(! R_getvariword(2,t24,&cmo))goto CLOSEOUT;  /* color map 5 bytes */
00044    if(! R_getvariword(2,t24,&cml))goto CLOSEOUT;
00045    if(! R_getvariword(1,t24,&cmes))goto CLOSEOUT;
00046    if(! R_getvariword(2,t24,&xor))goto CLOSEOUT;  /* Image spec 10 bytes */
00047    if(! R_getvariword(2,t24,&yor))goto CLOSEOUT;
00048    if(! R_getvariword(2,t24,&wi))goto CLOSEOUT;
00049    if(! R_getvariword(2,t24,&hi))goto CLOSEOUT;
00050    if(! R_getvariword(1,t24,&pixel_size))goto CLOSEOUT;
00051    if(! R_getvariword(1,t24,&image_descriptor))goto CLOSEOUT;
00052    if(idsize > 0)for(i=0;i<idsize;i++)if(getc(t24) == EOF)goto CLOSEOUT;
00053    if(cm_type == 1 && cml > 0){
00054      for(i=cmo;i<cml;i++)if(! R_getvariword(cmes/8,t24,&j))goto CLOSEOUT;
00055    }
00056    n_attr = image_descriptor & 0xf;
00057    upsidedown = (image_descriptor & 0x20) > 0 ? 0 : 1 ;
00058    if(info_only){
00059      x_size=(int)wi;
00060      y_size=(int)hi;
00061      fclose(t24);
00062      return 1;
00063    }
00064    if((prgb=(unsigned char *)X__Malloc((size_t)wi*3)) == NULL)goto CLOSEOUT;
00065    if((image_descriptor & 0xc0) > 0)goto CLOSEOUT; /* interlaced */
00066    b_size=i=wi*hi;
00067    if     (pixel_size == 16){n=2;s1=5;s2=10;s=3;pat=0x1f;}
00068    else if(pixel_size == 24){n=3;s1=8;s2=16;s=0;pat=0xff;}
00069    else                     {n=4;s1=8;s2=16;s=0;pat=0xff;}
00070    wi3=wi*3;
00071    if(image_type == 2){ /* uncompressed */
00072      m=0; k=0; for(i=0;i<hi;i++)for(j=0;j<wi;j++){
00073        if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00074        prgb[m  ] = (char)( vw        & pat) << s;
00075        prgb[m+1] = (char)((vw >> s1) & pat) << s;
00076        prgb[m+2] = (char)((vw >> s2) & pat) << s;
00077        k++; m+=3;
00078        if(m == wi3){
00079          tga_display_line(prgb,wi,i,upsidedown,hi); m=0;
00080        }
00081      }
00082    }
00083    else{  /* compressed */
00084      m=0; l=0; k=0; while(k < b_size){
00085        i=getc(t24); if(feof(t24))goto CLOSEOUT;
00086         if(i < 128){
00087          for(j=0;j<i+1;j++){
00088            if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00089            prgb[m  ] = (char)( vw        & pat) << s;
00090            prgb[m+1] = (char)((vw >> s1) & pat) << s;
00091            prgb[m+2] = (char)((vw >> s2) & pat) << s;
00092            k++; m+=3;
00093            if(m == wi3){
00094              tga_display_line(prgb,wi,l,upsidedown,hi); m=0; l++;
00095            }
00096          }
00097        }
00098        else{
00099          if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00100          c0 = (char)( vw        & pat) << s;
00101          c1 = (char)((vw >> s1) & pat) << s;
00102          c2 = (char)((vw >> s2) & pat) << s;
00103          i -= 128;
00104          for(j=0;j<i+1;j++){
00105            prgb[m  ] = c0;
00106            prgb[m+1] = c1;
00107            prgb[m+2] = c2;
00108            k++;  m+=3;
00109            if(m == wi3){
00110              tga_display_line(prgb,wi,l,upsidedown,hi); m=0; l++;
00111            }
00112          }
00113        }
00114      }
00115    }
00116    iok=1;
00117    CLOSEOUT:
00118    fclose(t24);
00119    if(prgb != NULL)X__Free((char *)prgb);
00120  }
00121  return (short)iok;
00122 }
00123 
00124 static int tgaview(HWND hWnd,char *CurrentFile){
00125  int i;
00126  long imagesize;
00127  LPBITMAPINFOHEADER lpbi;
00128  if(LoadTGA(CurrentFile,1) == 0){
00129    ErrMsg("'%ls' is NOT a valid TGA file type",CurrentFile);
00130    return 0;
00131  }
00132  x4_size=ALIGNULONG(x_size);
00133  FreeDib();
00134  lpBitmapHeader = (LPSTR)X__Malloc((long)sizeof(BITMAPINFOHEADER));
00135  if(lpBitmapHeader == NULL){
00136    ErrMsg("Out of memory");
00137    return 0;
00138  }
00139  lpbi=(LPBITMAPINFOHEADER)lpBitmapHeader;
00140  lpbi->biSize=sizeof(BITMAPINFOHEADER);
00141  lpbi->biWidth=(DWORD)x4_size;
00142  lpbi->biHeight=(DWORD)y_size;
00143  lpbi->biPlanes=1;
00144  lpbi->biBitCount=24;
00145  lpbi->biCompression=BI_RGB;
00146  imagesize = lpbi->biSizeImage = (DWORD)x4_size*(DWORD)y_size*3L;
00147  lpbi->biXPelsPerMeter=0;
00148  lpbi->biYPelsPerMeter=0;
00149  lpbi->biClrUsed=0;
00150  lpbi->biClrImportant=0;
00151  hpalCurrent=CreateImagePalette(lpbi);  /* will set up a dummy palette */
00152  lpBitmapBits=(LPSTR)X__Malloc(imagesize);
00153  if(lpBitmapBits == NULL){
00154    ErrMsg("Out of memory");
00155    FreeDib();
00156    return 0;
00157  }
00158  memset(lpBitmapBits,0,imagesize);
00159  pixels=lpBitmapBits;
00160  if(LoadTGA(CurrentFile,0) == 0){
00161    ErrMsg("Error reading TGA file");
00162    FreeDib();
00163    return 0;
00164  }
00165  hbmCurrent=BitmapFromDib(lpBitmapHeader,lpBitmapBits,hpalCurrent);
00166  if(hbmCurrent == NULL)ErrMsg("Could not create bitmap");
00167  SizeWindow(hWnd);
00168  return 1;
00169 }
00170 
00171 static FILE *Bit_File;
00172 static unsigned char *Screen=NULL;
00173 
00174 #if 0 
00175 // REMOVED 
00176 static short RenderFlcBrun(FILE *f);
00177 static short RenderFlcColor(FILE *f, short type);
00178 static short LoadRenderAnimCel(FILE *f, Flc_head *flc, short info_only);
00179 
00180 static long nx,ny,nx4;
00181 
00182 static long LoadCEL(char *name, short info_only){
00183  long i,cel_type;
00184  struct pic_header aa_cel;
00185  Flc_head flc_cel;
00186  unsigned char cc,*pix;
00187  if((Bit_File=fopen(name,"rb")) == NULL)return 0;
00188  if(fread(&aa_cel,sizeof(struct pic_header),1,Bit_File) != 1)cel_type=0;
00189  else if(aa_cel.type == PIC_MAGIC){
00190    cel_type=1;
00191    nx = x_size = aa_cel.w;
00192    ny = y_size = aa_cel.h;
00193  }
00194  else{
00195    fseek(Bit_File,0,SEEK_SET);
00196    if(fread(&flc_cel,sizeof(Flc_head),1,Bit_File) != 1)cel_type=0;
00197    else if(flc_cel.type == FLCH_MAGIC){
00198      cel_type=2;
00199      nx = x_size = flc_cel.width;
00200      ny = y_size = flc_cel.height;
00201    }
00202  }
00203  nx4=ALIGNULONG(nx);
00204  if(cel_type == 0){fclose(Bit_File); return(0);}
00205  if(cel_type == 2){
00206    if(LoadRenderAnimCel(Bit_File,&flc_cel,info_only) < 0){
00207      fclose(Bit_File);
00208      return(0);
00209    }
00210  }
00211  else{
00212    for(i = 0; i < 256 ; i++) {
00213      fread(&cc,1,1,Bit_File); p_red[i]  = cc << 2;
00214      fread(&cc,1,1,Bit_File); p_green[i]  = cc << 2;
00215      fread(&cc,1,1,Bit_File); p_blue[i]  = cc << 2;
00216    }
00217    if(info_only){
00218      fclose(Bit_File);
00219      return 1;
00220    }
00221    for(i=0;i<ny;i++){
00222      pix=Screen+(ny-1-i)*nx4;
00223      if(fread(pix,1,nx,Bit_File) != nx){
00224        fclose(Bit_File);
00225        return(0);
00226      }
00227    }
00228  }
00229  fclose(Bit_File);
00230  return(1);
00231 }
00232 
00233 static short LoadRenderAnimCel(FILE *f,Flc_head *flc, short info_only){
00234  Flc_frame f_frame;
00235  Flc_chunk f_chunk;
00236  long i,j,k;
00237  char c;
00238  unsigned char *p;
00239 //ErrMsg("Reading anim cel");
00240 /*printf("size of flc head %ld\n",(long)sizeof(Flc_head));*/
00241  fseek(f,flc->oframe1,SEEK_SET);
00242  if(fread(&f_frame,sizeof(Flc_frame),1,f) != 1 ||
00243            f_frame.type != FLCF_MAGIC)return -1;
00244 /*printf("point2  %ld chunks\n",f_frame.chunks);*/
00245  for(i=0;i<f_frame.chunks;i++){
00246    if(fread(&f_chunk,sizeof(Flc_chunk),1,f) != 1)return -1;
00247 /*printf("chunk type %ld\n",(long)f_chunk.type);*/
00248    if(f_chunk.type == FLC_COLOR256 || f_chunk.type == FLC_COLOR){
00249      if(RenderFlcColor(f,f_chunk.type) < 0)return -1;
00250      if(info_only)return 0;
00251    }
00252    else if(!info_only && f_chunk.type == FLC_BRUN){
00253      if(RenderFlcBrun(f) < 0)return -1;
00254    }
00255    else if(!info_only && f_chunk.type == FLC_COPY){
00256      for (k=0;k<ny;k++){
00257        p=Screen+(ny-1-k)*nx4;
00258       if(fread(p,1,nx,f) != nx)return -1;
00259      }
00260    }
00261    else{
00262      for(j=0;j<f_chunk.size-6;j++)fread(&c,1,1,f);
00263    }
00264  }
00265  return 0;
00266 }
00267 
00268 static short RenderFlcColor(FILE *f, short type){
00269  short i,j,pk,packets,ci=0;
00270  unsigned char skip=0,change,colour;
00271  if(fread(&packets,2,1,f) != 1)return -1;
00272  for (pk=0; pk<packets; pk++ ) {
00273    if(fread(&skip,1,1,f) != 1)return -1;
00274    ci += skip;
00275    if(fread(&change,1,1,f) != 1)return -1;
00276    if (change == 0) change=255;
00277    for (i=0; i<=change; i++) {
00278      if(fread(&colour,1,1,f) != 1)return -1;
00279      if(type == FLC_COLOR)colour = colour << 2;
00280      p_red[ci] = colour;
00281      if(fread(&colour,1,1,f) != 1)return -1;
00282      if(type == FLC_COLOR)colour = colour << 2;
00283      p_green[ci] = colour;
00284      if(fread(&colour,1,1,f) != 1)return -1;
00285      if(type == FLC_COLOR)colour = colour << 2;
00286      p_blue[ci] = colour;
00287      ci++;
00288    }
00289  }
00290  return 0;
00291 }
00292 
00293 static short RenderFlcBrun(FILE *f){
00294  char size=0;
00295  unsigned char packet,*p,colour;
00296  short i,line,pk;
00297  for (line=0;line<ny;line++){
00298    p=Screen+(ny-1-line)*nx4;
00299    if(fread(&packet,1,1,f) != 1)return -1;
00300    for(pk=0; pk<nx; pk+=abs(size)){
00301      if(fread(&size,1,1,f) != 1)return -1;
00302      if(size > 0){
00303        if(fread(&colour,1,1,f) != 1)return -1;
00304        for (i=0; i<size; i++) *p++ = colour;
00305      }
00306      else{
00307        for (i = size; i<0; i++){
00308          if(fread(&colour,1,1,f) != 1)return -1;
00309          *p++ = colour;
00310        }
00311      }
00312    }
00313  }
00314  return 0;
00315 }
00316 
00317 static int celview(HWND hWnd,char *temp){
00318  int i;
00319  long imagesize;
00320  LPBITMAPINFOHEADER lpbi;
00321  FreeDib();
00322  rowcount=0;
00323  if(LoadCEL(temp,1) < 0){
00324    ErrMsg("'%ls' is NOT a valid CEL file", (LPSTR)temp);
00325    return 0;
00326  }
00327  x4_size = ALIGNULONG(x_size);
00328  lpBitmapHeader = (LPSTR)X__Malloc((long)sizeof(BITMAPINFOHEADER)
00329                                 +256L*sizeof(RGBQUAD));
00330  if(lpBitmapHeader == NULL){
00331    ErrMsg("Out of memory");
00332    return 0;
00333  }
00334  lpbi=(LPBITMAPINFOHEADER)lpBitmapHeader;
00335  lpbi->biSize=sizeof(BITMAPINFOHEADER);
00336  lpbi->biWidth=(DWORD)x4_size;
00337  lpbi->biHeight=(DWORD)y_size;
00338  lpbi->biPlanes=1;
00339  lpbi->biBitCount=8;
00340  lpbi->biCompression=BI_RGB;
00341  imagesize = lpbi->biSizeImage = (DWORD)x4_size*(DWORD)y_size;
00342  lpbi->biXPelsPerMeter=0;
00343  lpbi->biYPelsPerMeter=0;
00344  lpbi->biClrUsed=256;
00345  lpbi->biClrImportant=256;
00346  pRgb=(RGBQUAD *)((LPSTR)lpbi + lpbi->biSize);
00347  for(i=0;i<256;i++){
00348    pRgb[i].rgbRed = p_red[i];
00349    pRgb[i].rgbGreen = p_green[i];
00350    pRgb[i].rgbBlue = p_blue[i];
00351  }
00352  hpalCurrent=CreateImagePalette(lpbi);
00353  lpBitmapBits=(LPSTR)X__Malloc(imagesize);
00354  if(lpBitmapBits == NULL){
00355    ErrMsg("Out of memory");
00356    FreeDib();
00357    return 0;
00358  }
00359  memset(lpBitmapBits,0,imagesize);
00360  Screen=(unsigned char *)lpBitmapBits;
00361  if(LoadCEL(temp,0) < 0){
00362    PutMsg("Error reading CEL file");
00363    FreeDib();
00364    return 0;
00365  }
00366  hbmCurrent=BitmapFromDib(lpBitmapHeader,lpBitmapBits,hpalCurrent);
00367  if(hbmCurrent == NULL)ErrMsg("Could not create bitmap");
00368  SizeWindow(hWnd);
00369  return 1;
00370 }
00371 // END of REMOVED
00372 #endif

Generated on Sun Apr 27 14:20:12 2014 for OpenFX by  doxygen 1.5.6