VIEW_TGA.C

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 1.0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 OpenFX Development Team
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019 You may contact the OpenFX development team via elecronic mail
00020 at core@openfx.org, or visit our website at http://openfx.org for
00021 further information and support details.
00022 -- */
00023 
00024 /* view_tga.c */
00025 
00026 static void tga_display_line(unsigned char *prgb,
00027                              long wide, long line,
00028                              long upsidedown, long hi){
00029  register long l,x,k;
00030  if(upsidedown)l = 3L*(long)(line)*(long)(x4_size);
00031  else          l = 3L*(long)(hi - 1 - line)*(long)(x4_size);
00032  memcpy(pixels + l, prgb, (int)(wide*3));
00033 }
00034 
00035 static short R_getvariword(long n, FILE *f, long *vw){
00036  long c;
00037  if((c=(long)getc(f)) == EOF)return 0;
00038  *vw = c;          if(n == 1)return 1;
00039  if((c=(long)getc(f)) == EOF)return 0;
00040  *vw |= (c << 8);  if(n == 2)return 1;
00041  if((c=(long)getc(f)) == EOF)return 0;
00042  *vw |= (c << 16); if(n == 3)return 1;
00043  if((c=(long)getc(f)) == EOF)return 0;
00044  *vw |= (c << 24);
00045  return 1;
00046 }
00047 
00048 static short LoadTGA(char *filename, short info_only){
00049  char c0,c1,c2;
00050  long i,j,k,l,m,n,cmo,cml,xor,yor,wi,hi,iok=0,idsize,cm_type,vw,
00051       image_type,cmes,pixel_size,image_descriptor,n_attr,upsidedown,b_size,
00052       s1,s2,s,pat,wi3;
00053  FILE *t24;
00054  unsigned char *prgb;
00055  prgb=NULL;
00056  if((t24=fopen(filename,"rb")) != NULL){
00057    if(! R_getvariword(1,t24,&idsize))goto CLOSEOUT;
00058    if(! R_getvariword(1,t24,&cm_type))goto CLOSEOUT; /* 0 for non mapped */
00059    if(! R_getvariword(1,t24,&image_type))goto CLOSEOUT; /* image type */
00060    if(image_type != 2 && image_type != 10)goto CLOSEOUT;
00061    if(! R_getvariword(2,t24,&cmo))goto CLOSEOUT;  /* color map 5 bytes */
00062    if(! R_getvariword(2,t24,&cml))goto CLOSEOUT;
00063    if(! R_getvariword(1,t24,&cmes))goto CLOSEOUT;
00064    if(! R_getvariword(2,t24,&xor))goto CLOSEOUT;  /* Image spec 10 bytes */
00065    if(! R_getvariword(2,t24,&yor))goto CLOSEOUT;
00066    if(! R_getvariword(2,t24,&wi))goto CLOSEOUT;
00067    if(! R_getvariword(2,t24,&hi))goto CLOSEOUT;
00068    if(! R_getvariword(1,t24,&pixel_size))goto CLOSEOUT;
00069    if(! R_getvariword(1,t24,&image_descriptor))goto CLOSEOUT;
00070    if(idsize > 0)for(i=0;i<idsize;i++)if(getc(t24) == EOF)goto CLOSEOUT;
00071    if(cm_type == 1 && cml > 0){
00072      for(i=cmo;i<cml;i++)if(! R_getvariword(cmes/8,t24,&j))goto CLOSEOUT;
00073    }
00074    n_attr = image_descriptor & 0xf;
00075    upsidedown = (image_descriptor & 0x20) > 0 ? 0 : 1 ;
00076    if(info_only){
00077      x_size=(int)wi;
00078      y_size=(int)hi;
00079      fclose(t24);
00080      return 1;
00081    }
00082    if((prgb=(unsigned char *)X__Malloc((size_t)wi*3)) == NULL)goto CLOSEOUT;
00083    if((image_descriptor & 0xc0) > 0)goto CLOSEOUT; /* interlaced */
00084    b_size=i=wi*hi;
00085    if     (pixel_size == 16){n=2;s1=5;s2=10;s=3;pat=0x1f;}
00086    else if(pixel_size == 24){n=3;s1=8;s2=16;s=0;pat=0xff;}
00087    else                     {n=4;s1=8;s2=16;s=0;pat=0xff;}
00088    wi3=wi*3;
00089    if(image_type == 2){ /* uncompressed */
00090      m=0; k=0; for(i=0;i<hi;i++)for(j=0;j<wi;j++){
00091        if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00092        prgb[m  ] = (char)( vw        & pat) << s;
00093        prgb[m+1] = (char)((vw >> s1) & pat) << s;
00094        prgb[m+2] = (char)((vw >> s2) & pat) << s;
00095        k++; m+=3;
00096        if(m == wi3){
00097          tga_display_line(prgb,wi,i,upsidedown,hi); m=0;
00098        }
00099      }
00100    }
00101    else{  /* compressed */
00102      m=0; l=0; k=0; while(k < b_size){
00103        i=getc(t24); if(feof(t24))goto CLOSEOUT;
00104         if(i < 128){
00105          for(j=0;j<i+1;j++){
00106            if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00107            prgb[m  ] = (char)( vw        & pat) << s;
00108            prgb[m+1] = (char)((vw >> s1) & pat) << s;
00109            prgb[m+2] = (char)((vw >> s2) & pat) << s;
00110            k++; m+=3;
00111            if(m == wi3){
00112              tga_display_line(prgb,wi,l,upsidedown,hi); m=0; l++;
00113            }
00114          }
00115        }
00116        else{
00117          if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00118          c0 = (char)( vw        & pat) << s;
00119          c1 = (char)((vw >> s1) & pat) << s;
00120          c2 = (char)((vw >> s2) & pat) << s;
00121          i -= 128;
00122          for(j=0;j<i+1;j++){
00123            prgb[m  ] = c0;
00124            prgb[m+1] = c1;
00125            prgb[m+2] = c2;
00126            k++;  m+=3;
00127            if(m == wi3){
00128              tga_display_line(prgb,wi,l,upsidedown,hi); m=0; l++;
00129            }
00130          }
00131        }
00132      }
00133    }
00134    iok=1;
00135    CLOSEOUT:
00136    fclose(t24);
00137    if(prgb != NULL)X__Free((char *)prgb);
00138  }
00139  return (short)iok;
00140 }
00141 
00142 static int tgaview(HWND hWnd){
00143  int i;
00144  long imagesize;
00145  LPBITMAPINFOHEADER lpbi;
00146  if(LoadTGA(CurrentFile,1) == 0){
00147    LoadErrMsg(IDX_ERR_NOTGA);
00148    return 0;
00149  }
00150  x4_size=ALIGNULONG(x_size);
00151  FreeDib();
00152  lpBitmapHeader = (LPSTR)X__Malloc((long)sizeof(BITMAPINFOHEADER));
00153  if(lpBitmapHeader == NULL){
00154    LoadErrMsg(IDX_ERR_MEMORY);
00155    return 0;
00156  }
00157  lpbi=(LPBITMAPINFOHEADER)lpBitmapHeader;
00158  lpbi->biSize=sizeof(BITMAPINFOHEADER);
00159  lpbi->biWidth=(DWORD)x4_size;
00160  lpbi->biHeight=(DWORD)y_size;
00161  lpbi->biPlanes=1;
00162  lpbi->biBitCount=24;
00163  lpbi->biCompression=BI_RGB;
00164  imagesize = lpbi->biSizeImage = (DWORD)x4_size*(DWORD)y_size*3L;
00165  lpbi->biXPelsPerMeter=0;
00166  lpbi->biYPelsPerMeter=0;
00167  lpbi->biClrUsed=0;
00168  lpbi->biClrImportant=0;
00169  hpalCurrent=CreateImagePalette(lpbi);  /* will set up a dummy palette */
00170  lpBitmapBits=(LPSTR)X__Malloc(imagesize);
00171  if(lpBitmapBits == NULL){
00172    LoadErrMsg(IDX_ERR_MEMORY);
00173    FreeDib();
00174    return 0;
00175  }
00176  memset(lpBitmapBits,0,imagesize);
00177  pixels=lpBitmapBits;
00178  if(LoadTGA(CurrentFile,0) == 0){
00179    LoadErrMsg(IDX_ERR_BADREAD);
00180    FreeDib();
00181    return 0;
00182  }
00183  hbmCurrent=BitmapFromDib(lpBitmapHeader,lpBitmapBits,hpalCurrent);
00184  if(hbmCurrent == NULL)LoadErrMsg(IDX_ERR_NOBITMAP);
00185  SizeWindow(hWnd);
00186  return 1;
00187 }

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