tga_map.c

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 2.0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 - 2007 OpenFX Development Team
00004 -- */
00005 
00006 /*  TGA_MAP.C  */
00007 
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 
00011 #include <windows.h>
00012 
00013 
00014 typedef struct SCREENBUFFER {
00015  unsigned char R,G,B,A;
00016 } fullscreenbuffer;
00017 
00018 static long LoadTgaImageMap(char *fn, long *xxx, long *yyy, BOOL size_only,
00019    unsigned char *p24R, unsigned char *p24G, unsigned char *p24B);
00020 
00021 static HINSTANCE hThisInstance;
00022 
00023 #if __WATCOMC__
00024 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00025 #else
00026 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00027 #endif
00028   switch (dwReason) {
00029     case DLL_PROCESS_ATTACH: {
00030       hThisInstance=hDLL;
00031       break;
00032     }
00033     case DLL_PROCESS_DETACH:
00034       break;
00035   }
00036   return TRUE;
00037 }
00038 
00039 BOOL _GetExternalImageSize
00040  (char *filename,
00041   long *x,
00042   long *y,
00043   unsigned char *source_buffer){
00044  return LoadTgaImageMap(filename,x,y,1,NULL,NULL,NULL);
00045 }
00046 
00047 BOOL _GetExternalImage
00048  (char *filename,
00049   long x, long y,
00050   unsigned char *Red,
00051   unsigned char *Green,
00052   unsigned char *Blue,
00053   unsigned char *source_buffer){
00054  return LoadTgaImageMap(filename,&x,&y,0,Red,Green,Blue);
00055 }
00056 
00057 BOOL _PutExternalImage
00058  (char *szOutFileName,
00059   long nx, long ny,
00060   long quality,
00061   fullscreenbuffer *image){
00062  return FALSE;
00063 }
00064 
00065 static short R_getvariword(long n, FILE *f, long *vw){
00066  long c;
00067  if((c=(long)getc(f)) == EOF)return 0;
00068  *vw = c;         if(n == 1)return 1;
00069  if((c=(long)getc(f)) == EOF)return 0;
00070  *vw |= (c << 8);  if(n == 2)return 1;
00071  if((c=(long)getc(f)) == EOF)return 0;
00072  *vw |= (c << 16); if(n == 3)return 1;
00073  if((c=(long)getc(f)) == EOF)return 0;
00074  *vw |= (c << 24);
00075  return 1;
00076 }
00077 
00078 
00079 #define UPSIDEDOWN  {l++; if(l == wi){l=0; pr -= wi*2; pg -= wi*2; pb -= wi*2; }}
00080 
00081 
00082 static long LoadTgaImageMap(char *fn, long *xxx, long *yyy, BOOL size_only,
00083    unsigned char *p24R, unsigned char *p24G, unsigned char *p24B){
00084  char c0,c1,c2;
00085  unsigned char *pr,*pg,*pb;
00086  long i,j,k,l,n,cmo,cml,xor,yor,wi,hi,iok=0,ialloc=0,idsize,cm_type,vw,
00087       image_type,cmes,pixel_size,image_descriptor,n_attr,upsidedown,b_size,
00088       s1,s2,s,pat;
00089  FILE *t24;
00090  t24=fopen((char *)fn,"rb");
00091  if(t24 != NULL){
00092    if(! R_getvariword(1,t24,&idsize))goto CLOSEOUT;
00093    if(! R_getvariword(1,t24,&cm_type))goto CLOSEOUT; /* 0 for non mapped */
00094    if(! R_getvariword(1,t24,&image_type))goto CLOSEOUT; /* image type */
00095    if(image_type != 2 && image_type != 10)goto CLOSEOUT;
00096    if(! R_getvariword(2,t24,&cmo))goto CLOSEOUT;  /* color map 5 bytes */
00097    if(! R_getvariword(2,t24,&cml))goto CLOSEOUT;
00098    if(! R_getvariword(1,t24,&cmes))goto CLOSEOUT;
00099    if(! R_getvariword(2,t24,&xor))goto CLOSEOUT;  /* Image spec 10 bytes */
00100    if(! R_getvariword(2,t24,&yor))goto CLOSEOUT;
00101    if(! R_getvariword(2,t24,&wi))goto CLOSEOUT;
00102    if(! R_getvariword(2,t24,&hi))goto CLOSEOUT;
00103    if(! R_getvariword(1,t24,&pixel_size))goto CLOSEOUT;
00104    if(! R_getvariword(1,t24,&image_descriptor))goto CLOSEOUT;
00105    if(idsize > 0)for(i=0;i<idsize;i++)if(getc(t24) == EOF)goto CLOSEOUT;
00106    if(cm_type == 1 && cml > 0){
00107      for(i=cmo;i<cml;i++)if(! R_getvariword(cmes/8,t24,&j))goto CLOSEOUT;
00108    }
00109    n_attr = image_descriptor & 0xf;
00110    upsidedown = (image_descriptor & 0x20) > 0 ? 0:1;
00111    if((image_descriptor & 0xc0) > 0)goto CLOSEOUT; /* interlaced */
00112    b_size=i=wi*hi; *xxx = (short)wi; *yyy = (short)hi;
00113    if(size_only){
00114      fclose(t24);
00115      return 1;
00116    }
00117    ialloc=1;
00118 
00119    if(upsidedown){
00120      pr=p24R+wi*(hi-1);
00121      pg=p24G+wi*(hi-1);
00122      pb=p24B+wi*(hi-1);
00123    }
00124    else{
00125      pr=p24R; pg=p24G; pb=p24B;
00126    }
00127    if     (pixel_size == 16){n=2;s1=5;s2=10;s=3;pat=0x1f;}
00128    else if(pixel_size == 24){n=3;s1=8;s2=16;s=0;pat=0xff;}
00129    else                     {n=4;s1=8;s2=16;s=0;pat=0xff;}
00130    if(image_type == 2){ /* uncompressed */
00131      l=0; for(i=0;i<hi;i++)for(j=0;j<wi;j++){
00132        if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00133        *pb++ = (char)( vw        & pat) << s;
00134        *pg++ = (char)((vw >> s1) & pat) << s;
00135        *pr++ = (char)((vw >> s2) & pat) << s;
00136        if(upsidedown)UPSIDEDOWN
00137      }
00138    }
00139    else{  /* compressed */
00140      l=0; k=0; while(k < b_size){
00141        i=getc(t24); if(feof(t24))goto CLOSEOUT;
00142         if(i < 128){
00143          for(j=0;j<i+1;j++){
00144            if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00145            *pb++ = (char)( vw        & pat) << s;
00146            *pg++ = (char)((vw >> s1) & pat) << s;
00147            *pr++ = (char)((vw >> s2) & pat) << s;
00148            k++;
00149            if(upsidedown)UPSIDEDOWN
00150          }
00151        }
00152        else{
00153          if(! R_getvariword(n,t24,&vw))goto CLOSEOUT;
00154          c0 = (char)( vw        & pat) << s;
00155          c1 = (char)((vw >> s1) & pat) << s;
00156          c2 = (char)((vw >> s2) & pat) << s;
00157          i -= 128;
00158          for(j=0;j<i+1;j++){
00159            *pb++ = c0;  *pg++ = c1;  *pr++ = c2; k++;
00160            if(upsidedown)UPSIDEDOWN
00161          }
00162        }
00163      }
00164    }
00165    iok=1;
00166    CLOSEOUT:
00167    fclose(t24);
00168  }
00169  if(iok == 0){
00170    return 0;
00171  }
00172  return 1;
00173 }
00174 #undef UPSIDEDOWN

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