LOADMAPS.C

Go to the documentation of this file.
00001 // file loadmaps.c
00002 
00003 // load and prepare any image maps used by the renderer
00004 
00005 
00006 #define MODULE_LOADMAPS
00007 
00008 /* Output file writing + image map loading */
00009 
00010 #include "render.h"
00011 //#undef OK                     /* OK is defined again in giflib.h */
00012 //#include "giflib.h"
00013 
00014 typedef struct RenderIMAP_tag {
00015  unsigned char *p24R,*p24G,*p24B;
00016  short xmax,ymax;
00017 } RenderIMAP;
00018 
00019 typedef struct ImageStoreEntry_tag {
00020  unsigned char *name;
00021  unsigned char *p24R,*p24G,*p24B;
00022  unsigned char *pImage;
00023  unsigned short x,y,xs,ys;
00024 } ImageStoreEntry;
00025 
00026 extern char gszHomeDir[];
00027 
00028 static short LoadExternalImageMap(char *fn, char *ofname, RenderIMAP *map);
00029 static long LoadRenderImageMap(imap *Map, char *mfname, char *ofname,
00030                            RenderIMAP *map);
00031 static unsigned char *MakeScaledImage(BOOL combined, long *xx, long *yy,
00032           unsigned char **RR, unsigned char **GG, unsigned char **BB);
00033 static unsigned char *MakeUnscaledImage(BOOL, long xx, long yy,
00034           unsigned char **RR, unsigned char **GG, unsigned char **BB, unsigned char **AA);
00035 static short LoadExternalMovieFrame(char *fn, char *ofname, RenderIMAP *map);
00036 
00037 static ImageStoreEntry *ImageStoreList=NULL;
00038 static long nImageStoreEntries=0;
00039 
00040 
00041 int AddImageToImageStore(char *name,  unsigned short x, unsigned short y,
00042                           unsigned char *r, unsigned char *g, unsigned char *b,
00043                           unsigned char *Image){
00044   ImageStoreEntry *tImageStoreList=NULL;
00045   char *n;
00046   int l;
00047   if(ImageStoreList == NULL){
00048     if((tImageStoreList=(ImageStoreEntry *)X__Malloc(sizeof(ImageStoreEntry))) == NULL)return 0;
00049   }
00050   else{
00051     if((tImageStoreList=(ImageStoreEntry *)X__Realloc(ImageStoreList,
00052       sizeof(ImageStoreEntry)*(nImageStoreEntries+1))) == NULL)return 0;
00053   }
00054   l=strlen(name)+2;
00055   ImageStoreList = tImageStoreList;
00056   if((n=(char *)X__Malloc(l)) == NULL)return 0;
00057   ImageStoreList[nImageStoreEntries].name=n;
00058   strcpy(n,name);
00059   //if(debug != NULL){fprintf(debug,"Added image map [%s] to store\n",name); fflush(debug);}
00060   ImageStoreList[nImageStoreEntries].x=x;
00061   ImageStoreList[nImageStoreEntries].y=y;
00062   ImageStoreList[nImageStoreEntries].p24R=r;
00063   ImageStoreList[nImageStoreEntries].p24G=g;
00064   ImageStoreList[nImageStoreEntries].p24B=b;
00065   ImageStoreList[nImageStoreEntries].pImage=Image;
00066   nImageStoreEntries++;
00067   return 1;
00068 }
00069 
00070 int CheckImageStoreForEntry(char *name,
00071                             unsigned short *x, unsigned short *y,
00072                             unsigned char **r, unsigned char **g, unsigned char **b,
00073                             unsigned char **Image){
00074  int i;
00075  //MessageBox(NULL,name,"Checking for map",MB_OK);
00076  if(nImageStoreEntries > 0 && ImageStoreList != NULL)for(i=0;i<nImageStoreEntries;i++){
00077    if(ImageStoreList[i].name != NULL && strcmp(name,ImageStoreList[i].name) == 0){
00078      if(ImageStoreList[i].p24R != NULL &&
00079         ImageStoreList[i].p24G != NULL &&
00080         ImageStoreList[i].p24B != NULL){
00081        *x=ImageStoreList[i].x;
00082        *y=ImageStoreList[i].y;
00083        *r=ImageStoreList[i].p24R;
00084        *g=ImageStoreList[i].p24G;
00085        *b=ImageStoreList[i].p24B;
00086        *Image=ImageStoreList[i].pImage;
00087        //if(debug != NULL){fprintf(debug,"Found image map [%s] in store\n",name); fflush(debug);}
00088        return 1;
00089      }
00090    }
00091  }
00092  return 0;
00093 }
00094 
00095 void ReleaseImageStore(void){
00096   int i;
00097   if(nImageStoreEntries > 0 && ImageStoreList != NULL)for(i=0;i<nImageStoreEntries;i++){
00098     //if(debug != NULL){fprintf(debug,"Releasing image map [%s] from store\n",ImageStoreList[i].name); fflush(debug);}
00099     X__Free(ImageStoreList[i].name);
00100     if(ImageStoreList[i].p24R != NULL)X__Free(ImageStoreList[i].p24R);
00101     if(ImageStoreList[i].p24G != NULL)X__Free(ImageStoreList[i].p24G);
00102     if(ImageStoreList[i].p24B != NULL)X__Free(ImageStoreList[i].p24B);
00103     if(ImageStoreList[i].pImage != NULL)X__Free(ImageStoreList[i].pImage);
00104   }
00105   X__Free(ImageStoreList);
00106   ImageStoreList=NULL;
00107   nImageStoreEntries=0;
00108   //if(debug != NULL){fprintf(debug,"ImageStoreList Released\n"); fflush(debug);}
00109 }
00110 
00112 
00113 long SetupImageMap(imap *Map, char *objectfilename){
00114  RenderIMAP temp_map,*map;
00115  long success;
00116  //MessageBox(NULL,objectfilename,"SetupImageMap",MB_OK);
00117  map=&temp_map;
00118  success=1;
00119  if(!Map->bLoadedS && !Map->bAnimS){
00120    if(Map->pp >= 0){
00121      //if(debug != NULL)fprintf(debug,"Attempting to load map [%s]\n",Map->filename);
00122      if(LoadRenderImageMap(Map,Map->filename,objectfilename,map) == 1){
00123        Map->p24R=map->p24R; Map->p24G=map->p24G; Map->p24B=map->p24B;
00124        Map->xmax=map->xmax;
00125        Map->ymax=map->ymax;
00126        Map->bLoadedS=TRUE;
00127      }
00128      else Map->pp = -1; // No image for this map so don't try and render anything with it.
00129    }
00130  } 
00131  else {
00132   ;//if(debug != NULL)fprintf(debug,"[%s] already loaded %ld or AVI\n",Map->filename,Map->pp);
00133  }
00134  if(!Map->bLoadedR && !Map->bAnimR){
00135    if(Map->rp >= 0){
00136      if(LoadRenderImageMap(Map,Map->frefl,objectfilename,map) == 1){
00137        Map->p24Rrefl=map->p24R;
00138        Map->p24Grefl=map->p24G;
00139        Map->p24Brefl=map->p24B;
00140        Map->xmaxrefl=map->xmax;
00141        Map->ymaxrefl=map->ymax;
00142        Map->bLoadedR=TRUE;
00143      }
00144      else Map->rp = -1;
00145    }
00146  }
00147  else {
00148   ;//if(debug != NULL)fprintf(debug,"Refl map [%s] already loaded %ld\n",Map->filename,Map->pp);
00149  }
00150  if(!Map->bLoadedB && !Map->bAnimB){
00151    if(Map->bp >= 0){
00152      if(LoadRenderImageMap(Map,Map->fbump,objectfilename,map) == 1){
00153        Map->p24Rbump=map->p24R;
00154        Map->p24Gbump=map->p24G;
00155        Map->p24Bbump=map->p24B;
00156        Map->xmaxbump=map->xmax;
00157        Map->ymaxbump=map->ymax;
00158        Map->bLoadedB=TRUE;
00159      }
00160      else Map->bp = -1;
00161    }
00162  }
00163  if(!Map->bLoadedT && !Map->bAnimT){
00164    if(Map->tp >= 0){
00165      if(LoadRenderImageMap(Map,Map->ftran,objectfilename,map) == 1){
00166        Map->p24Rtran=map->p24R;
00167        Map->p24Gtran=map->p24G;
00168        Map->p24Btran=map->p24B;
00169        Map->xmaxtran=map->xmax;
00170        Map->ymaxtran=map->ymax;
00171        Map->bLoadedT=TRUE;
00172      }
00173      else Map->tp = -1;
00174    }
00175  }
00176  return success;
00177 }
00178 
00179 BOOL CheckSingleRenderImageMap(imap *Map, char *mfname, char *ofname, char *tempfile, int type){
00180  RenderIMAP temp_map,*map;
00181  unsigned char *dummy;
00182  char *fn;
00183  long status;
00184  //MessageBox(NULL,objectfilename,"SetupImageMap",MB_OK);
00185  map=&temp_map;
00186  fn=(char *)strlwr(mfname);
00187  map->p24R=NULL; map->p24G=NULL; map->p24B=NULL;
00188  status=0;
00189  if(CheckImageStoreForEntry(mfname,&(map->xmax),&(map->ymax),
00190                             &(map->p24R),&(map->p24G),&(map->p24B),&dummy)){
00191    if(type == 0){
00192        Map->p24R=map->p24R; Map->p24G=map->p24G; Map->p24B=map->p24B;
00193        Map->xmax=map->xmax;
00194        Map->ymax=map->ymax;
00195        Map->bLoadedS=TRUE;
00196    }
00197    else if(type == 1){
00198        Map->p24Rrefl=map->p24R;
00199        Map->p24Grefl=map->p24G;
00200        Map->p24Brefl=map->p24B;
00201        Map->xmaxrefl=map->xmax;
00202        Map->ymaxrefl=map->ymax;
00203        Map->bLoadedR=TRUE;
00204    }
00205    else if(type == 2){
00206        Map->p24Rbump=map->p24R;
00207        Map->p24Gbump=map->p24G;
00208        Map->p24Bbump=map->p24B;
00209        Map->xmaxbump=map->xmax;
00210        Map->ymaxbump=map->ymax;
00211        Map->bLoadedB=TRUE;
00212    }
00213    else if(type == 3){
00214        Map->p24Rtran=map->p24R;
00215        Map->p24Gtran=map->p24G;
00216        Map->p24Btran=map->p24B;
00217        Map->xmaxtran=map->xmax;
00218        Map->ymaxtran=map->ymax;
00219        Map->bLoadedT=TRUE;
00220    }
00221    //if(debug != NULL)fprintf(debug,"Map [%s] temp [%s] already in store\n",mfname,tempfile);
00222    return TRUE;
00223  }
00224  return FALSE;
00225 }
00226 
00227 void LoadSingleRenderImageMap(imap *Map, char *mfname, char *ofname, char *tempfile, int type){
00228  RenderIMAP temp_map,*map;
00229  unsigned char *dummy;
00230  char *fn;
00231  long status;
00232  //MessageBox(NULL,objectfilename,"SetupImageMap",MB_OK);
00233  map=&temp_map;
00234  fn=(char *)strlwr(mfname);
00235  map->p24R=NULL; map->p24G=NULL; map->p24B=NULL;
00236  status=LoadExternalImageMap(tempfile,ofname,map);
00237  if(status == 1){
00238    status=AddImageToImageStore(mfname,(map->xmax),(map->ymax),
00239                           (map->p24R),(map->p24G),(map->p24B),NULL);
00240    if(status == 1){
00241      if(type == 0){
00242        Map->p24R=map->p24R; Map->p24G=map->p24G; Map->p24B=map->p24B;
00243        Map->xmax=map->xmax;
00244        Map->ymax=map->ymax;
00245        Map->bLoadedS=TRUE;
00246      }
00247      else if(type == 1){
00248        Map->p24Rrefl=map->p24R;
00249        Map->p24Grefl=map->p24G;
00250        Map->p24Brefl=map->p24B;
00251        Map->xmaxrefl=map->xmax;
00252        Map->ymaxrefl=map->ymax;
00253        Map->bLoadedR=TRUE;
00254      }
00255      else if(type == 2){
00256        Map->p24Rbump=map->p24R;
00257        Map->p24Gbump=map->p24G;
00258        Map->p24Bbump=map->p24B;
00259        Map->xmaxbump=map->xmax;
00260        Map->ymaxbump=map->ymax;
00261        Map->bLoadedB=TRUE;
00262      }
00263      else if(type == 3){
00264        Map->p24Rtran=map->p24R;
00265        Map->p24Gtran=map->p24G;
00266        Map->p24Btran=map->p24B;
00267        Map->xmaxtran=map->xmax;
00268        Map->ymaxtran=map->ymax;
00269        Map->bLoadedT=TRUE;
00270      }
00271      //if(debug != NULL)fprintf(debug,"Single Image [%s] from temp [%s]  status=%ld flags %ld %ld %ld %ld\n",mfname,
00272      //  tempfile,status,Map->bLoadedS,Map->bLoadedR,Map->bLoadedB,Map->bLoadedT);
00273    }
00274  }
00275 }
00276 
00277 //static long GetAVIframeImage(RenderIMAP *map, char * mfname, char *ofname, long GLobalFrameID){
00278 //  return 0;
00279 //}
00280 
00281 static long LoadRenderImageMap(imap *Map, char *mfname, char *ofname,
00282                            RenderIMAP *map){
00283    long i,j,status=0,istga=0,isbmp=0,isgif=0,isext=0;
00284    char last4[8],*fn;
00285    unsigned char *dummy;
00286    map->p24R=NULL; map->p24G=NULL; map->p24B=NULL;
00287    fn=(char *)strlwr(mfname);
00288    if(CheckImageStoreForEntry(mfname,&(map->xmax),&(map->ymax),
00289                             &(map->p24R),&(map->p24G),&(map->p24B),&dummy))return 1;
00290    strncpy(last4,(fn+strlen(fn)-4),4);
00291    if(strncmp((char *)last4,".gif",4) == 0)isgif=1;
00292    if(strncmp((char *)last4,".tga",4) == 0)istga=1;
00293    if(strncmp((char *)last4,".tif",4) == 0)isext=1;
00294    if(strncmp((char *)last4,".jpg",4) == 0)isext=1;
00295    if(strncmp((char *)last4,".png",4) == 0)isext=1;
00296    if(strncmp((char *)last4,".bmp",4) == 0)isbmp=1;
00297    if     (isbmp)status=LoadExternalImageMap(mfname,ofname,map);
00298    else if(istga)status=LoadExternalImageMap(mfname,ofname,map);
00299    else if(isgif)status=LoadExternalImageMap(mfname,ofname,map);
00300    else          status=LoadExternalImageMap(mfname,ofname,map);
00301    if(status == 1)status=AddImageToImageStore(mfname,(map->xmax),(map->ymax),
00302                             (map->p24R),(map->p24G),(map->p24B),NULL);
00303    return status;
00304 }
00305 
00306 static short LoadExternalImageMap(char *fn, char *ofname, RenderIMAP *map){
00307  long x,y;
00308  FILE *t24;
00309  char lname[256],last3[8],dll[256],rep_name[256];
00310  HMODULE hLib;
00311  FARPROC fpFun;
00312  BOOL (*fpGetMapSize)(char *,
00313                       long *, long *, unsigned char *);
00314  BOOL (*fpGetMap)(char *,
00315                   long, long,
00316                   unsigned char *, unsigned char *, unsigned char *, unsigned char *);
00317  Render_Message(61,0,fn);
00318  strcpy(rep_name,"%%**%%"); //some dummy string that will never be a filename
00319  //MessageBox(NULL,ofname,"External Map Object",MB_OK);
00320  t24=GetRenderRelativeFile(fn,ofname,rep_name);
00321  if(t24 == NULL)return 0;
00322  fclose(t24);
00323  if(strcmp(rep_name,"%%**%%") != 0){ // string was replaced
00324    fn=rep_name;
00325  }
00326  strcpy(lname,gszHomeDir);
00327  strncpy(last3,(fn+strlen(fn)-3),3);
00328  last3[3]=0;  // to ensure end of string null
00329  sprintf(dll,"%s_map.dll",last3);
00330  strcat(lname,dll);
00331  hLib=AddLoadedLibrary(lname);
00332  //MessageBox(NULL,fn,lname,MB_OK);
00333  if(hLib != NULL){
00334    if((fpFun=GetProcAddress(hLib,"_GetExternalImageSize")) != NULL){
00335      fpGetMapSize = (void *)fpFun;
00336      if((*fpGetMapSize)(fn,&x,&y,NULL)){
00337        if((map->p24R = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00338        if((map->p24G = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00339        if((map->p24B = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00340        if((fpFun=GetProcAddress(hLib,"_GetExternalImage")) != NULL){
00341          fpGetMap = (void *)fpFun;
00342          if(!(*fpGetMap)(fn,x,y,map->p24R,map->p24G,map->p24B,NULL))goto CLOSEOUT;
00343        }
00344        else goto CLOSEOUT;
00345      }
00346      else goto CLOSEOUT;
00347    }
00348    else goto CLOSEOUT;
00349  }
00350  else return 0;
00351  // Use this to make small image maps for OpenGL mapping  - comment out if not in use
00352  MakeScaledImage(FALSE,&x,&y,&(map->p24R),&(map->p24G),&(map->p24B));
00353  map->xmax = (short)x;
00354  map->ymax = (short)y;
00355  //{char ttt[256]; sprintf(ttt,"map size %ld %ld  [%s] [%s]",x,y,fn,lname);
00356  //MessageBox(NULL,ttt,"Load External",MB_OK);}
00357  return 1;
00358  CLOSEOUT:
00359  if(map->p24R != NULL)X__Free(map->p24R); map->p24R = NULL;
00360  if(map->p24G != NULL)X__Free(map->p24G); map->p24G = NULL;
00361  if(map->p24B != NULL)X__Free(map->p24B); map->p24B = NULL;
00362  Render_Message(62,0,fn);
00363  return 0;
00364 }
00365 
00366 unsigned char *LoadSystemMAP(BOOL bScale, BOOL b32, char *fn, long *xx,long *yy){ // Load a default map from file THIS FILE MUST EXIST
00367  unsigned char *pix;
00368  unsigned char *R,*G,*B,*A=NULL;
00369  long x,y;
00370  char lname[256],last3[8],dll[256],rep_name[256];
00371  HMODULE hLib;
00372  FARPROC fpFun;
00373  BOOL (*fpGetMapSize)(char *,
00374                       long *, long *, unsigned char *);
00375  BOOL (*fpGetMap)(char *,
00376                   long, long,
00377                   unsigned char *, unsigned char *, unsigned char *, unsigned char *);
00378  BOOL (*fpGetMap32)(char *,
00379                   long, long,
00380                   unsigned char *, unsigned char *, unsigned char *, unsigned char *, unsigned char *);
00381  strcpy(lname,gszHomeDir);
00382  strncpy(last3,(fn+strlen(fn)-3),3);
00383  last3[3]=0;  // to ensure end of string null
00384  sprintf(dll,"%s_map.dll",last3);
00385  strcat(lname,dll);
00386  hLib=AddLoadedLibrary(lname);
00387  //MessageBox(NULL,fn,lname,MB_OK);
00388  if(hLib != NULL){
00389    if((fpFun=GetProcAddress(hLib,"_GetExternalImageSize")) != NULL){
00390      fpGetMapSize = (void *)fpFun;
00391      if((*fpGetMapSize)(fn,&x,&y,NULL)){
00392        if((R = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00393        if((G = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00394        if((B = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00395        if(b32){
00396          if((A = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00397          if((fpFun=GetProcAddress(hLib,"_GetExternalImageA")) != NULL){
00398            fpGetMap32 = (void *)fpFun;
00399            if(!(*fpGetMap32)(fn,x,y,R,G,B,A,NULL))goto CLOSEOUT;
00400          }
00401          else goto CLOSEOUT;
00402        }
00403        else{
00404          if((fpFun=GetProcAddress(hLib,"_GetExternalImage")) != NULL){
00405            fpGetMap = (void *)fpFun;
00406            if(!(*fpGetMap)(fn,x,y,R,G,B,NULL))goto CLOSEOUT;
00407          }
00408          else goto CLOSEOUT;
00409        }
00410      }
00411      else goto CLOSEOUT;
00412    }
00413    else goto CLOSEOUT;
00414  }
00415  else return NULL;
00416  if(bScale)pix=MakeScaledImage(TRUE,&x,&y,&R,&G,&B);
00417  else if(!b32)pix=MakeUnscaledImage(b32,x,y,&R,&G,&B,NULL);
00418  else         pix=MakeUnscaledImage(b32,x,y,&R,&G,&B,&A);  // 32 bit with alpha channel
00419  *xx = x;
00420  *yy = y;
00421  if(R != NULL)X__Free(R); 
00422  if(G != NULL)X__Free(G); 
00423  if(B != NULL)X__Free(B);
00424  if(b32 && A != NULL)X__Free(A); 
00425  //{char ttt[256]; sprintf(ttt,"map size %ld %ld  [%s] [%s]",x,y,fn,lname);
00426  //MessageBox(NULL,ttt,"Load System Map",MB_OK);}
00427  return pix;
00428  CLOSEOUT:
00429  if(R != NULL)X__Free(R); 
00430  if(G != NULL)X__Free(G); 
00431  if(B != NULL)X__Free(B); 
00432  if(b32 && A != NULL)X__Free(A); 
00433  return NULL;
00434 }
00435 
00436 #define MIPMAP1 1024
00437 #define MIPMAP2 MIPMAP1/2
00438 #define MIPMAP3 MIPMAP1/4
00439 #define MIPMAP4 MIPMAP1/8
00440 #define MIPMAP5 MIPMAP1/16
00441 #define MIPMAP6 MIPMAP1/32
00442 
00443 static unsigned char *MakeScaledImage(BOOL combined, long *xx, long *yy,
00444           unsigned char **RR, unsigned char **GG, unsigned char **BB){
00445  long imagesize,i,j,x,y,xi,yi;
00446  unsigned char *R,*G,*B;
00447  unsigned char *r,*g,*b;
00448  unsigned char *p, *px;
00449  x = *xx; y = *yy;
00450  R = *RR; G = *GG; B = *BB;
00451  imagesize = x*y*3;
00452  if((p=(unsigned char *)X__Malloc(imagesize+3)) == NULL){
00453    return NULL;
00454  }
00455 //if(debug != NULL)fprintf(debug,"Scaling image from %ld %ld\n",x,y);
00456  for(j=0;j<y;j++){  // combine the colour components
00457   px=(p+(y-j-1)*x*3);
00458   r=(R+j*x); g=(G+j*x); b=(B+j*x);
00459   for(i=0;i<x;i++){
00460     *px++ = *r++;
00461     *px++ = *g++;
00462     *px++ = *b++;
00463   }
00464  }
00465  X__Free(R); *RR = NULL; 
00466  X__Free(G); *GG = NULL;
00467  X__Free(B); *BB = NULL;
00468  *xx = 0; *yy = 0;
00469 
00470  if(x < MIPMAP5)xi=MIPMAP6; else 
00471  if(x < MIPMAP4)xi=MIPMAP5; else 
00472  if(x < MIPMAP3)xi=MIPMAP4; else 
00473  if(x < MIPMAP2)xi=MIPMAP3; else 
00474  if(x < MIPMAP1)xi=MIPMAP2; else xi=MIPMAP1;
00475  if(y < MIPMAP5)yi=MIPMAP6; else 
00476  if(y < MIPMAP4)yi=MIPMAP5; else 
00477  if(y < MIPMAP3)yi=MIPMAP4; else 
00478  if(y < MIPMAP2)yi=MIPMAP3; else 
00479  if(y < MIPMAP1)yi=MIPMAP2; else yi=MIPMAP1;
00480   //xi=yi=64; 
00481  if((px=(unsigned char *)X__Malloc(xi*yi*3)) == NULL){
00482    X__Free((char *)p);
00483    return NULL;
00484  }
00485  ScaleImageBuffer3(x,y,p,xi,yi,px); // Our own scaling function
00486  X__Free((char *)p);
00487  //px=p; xi=x; yi=y; // this line substitutes for the two above if no scaling used
00488  *xx = xi; *yy = yi;
00489  if(combined)return px;
00490  x= xi; y=yi;
00491  if((R = (unsigned char *)X__Malloc(x*y)) != NULL){
00492    if((G = (unsigned char *)X__Malloc(x*y)) != NULL){
00493      if((B = (unsigned char *)X__Malloc(x*y)) != NULL){
00494        for(j=0;j<y;j++){  // combine the colour components
00495          p=(px+(y-j-1)*x*3);
00496          r=(R+j*x); g=(G+j*x); b=(B+j*x);
00497          for(i=0;i<x;i++){
00498            *r++ = *p++;
00499            *g++ = *p++;
00500            *b++ = *p++;
00501          }
00502        }
00503      }
00504      else{X__Free(G); X__Free(R); G=NULL; R=NULL;}
00505    }
00506    else {X__Free(R); R=NULL;}
00507  }
00508  *RR = R;
00509  *GG = G;
00510  *BB = B;
00511  *xx = x; *yy = y;
00512  X__Free(px);
00513  //if(debug != NULL)fprintf(debug,"Scaled down to %ld %ld\n",x,y);
00514  return NULL;
00515 }
00516 
00517 
00518 static unsigned char *MakeUnscaledImage(BOOL b32, long xx, long yy,
00519           unsigned char **RR, unsigned char **GG, unsigned char **BB, unsigned char **AA){
00520  long imagesize,i,j,x,y,xi,yi;
00521  unsigned char *R,*G,*B,*A;
00522  unsigned char *r,*g,*b,*a;
00523  unsigned char *p, *px;
00524  x = xx; y = yy;
00525  R = *RR; G = *GG; B = *BB;
00526  if(b32){A = *AA; imagesize = x*y*4;}
00527  else  imagesize = x*y*3;
00528  if((p=(unsigned char *)X__Malloc(imagesize+3)) == NULL){
00529    return NULL;
00530  }
00531 //if(debug != NULL)fprintf(debug,"Converting image to standard %ld %ld\n",x,y);
00532  if(b32){
00533   for(j=0;j<y;j++){  // combine the colour components
00534     px=(p+(y-j-1)*x*4);
00535     r=(R+j*x); g=(G+j*x); b=(B+j*x); a=(A+j*x);
00536     for(i=0;i<x;i++){
00537       *px++ = *r++;
00538       *px++ = *g++;
00539       *px++ = *b++;
00540       *px++ = *a++;
00541     }
00542   }
00543  }
00544  else{
00545   for(j=0;j<y;j++){  // combine the colour components
00546     px=(p+(y-j-1)*x*3);
00547     r=(R+j*x); g=(G+j*x); b=(B+j*x); 
00548     for(i=0;i<x;i++){
00549       *px++ = *r++;
00550       *px++ = *g++;
00551       *px++ = *b++;
00552     }
00553   }
00554  }
00555  X__Free(R); *RR = NULL; 
00556  X__Free(G); *GG = NULL;
00557  X__Free(B); *BB = NULL;
00558  if(b32){X__Free(A); *AA = NULL;}
00559  return p;
00560 }
00561 
00562 
00564 static void GetRowPixelValues4(long rgb, long row, long n, unsigned char *buffer, double *values){
00565  int i;
00566  unsigned char *p;
00567  p=(buffer + row*n*4 + rgb);
00568  for(i=0;i<n;i++){
00569    values[i] = (double)(*p);  p += 4;
00570  }
00571 }
00572 
00573 static void GetColumnPixelValues4(long rgb, long col, long n, long w, unsigned char *buffer, double *values){
00574  int i;
00575  unsigned char *p;
00576  p=(buffer + col*4 + rgb);
00577  for(i=0;i<n;i++){
00578    values[i] = (double)(*p);  p += w*4;
00579  } 
00580 }
00581 
00582 static void PutRowPixelValues4(long rgb, long row, long n, unsigned char *buffer, double *values){
00583  int i;
00584  unsigned char *p;
00585  p=(buffer + row*n*4 + rgb);
00586  for(i=0;i<n;i++){
00587    *p = (unsigned char)values[i]; p += 4;
00588  }
00589 }
00590 
00591 static void PutColumnPixelValues4(long rgb, long col, long n, long w, unsigned char *buffer, double *values){
00592  int i;
00593  unsigned char *p;
00594  p=(buffer + col*4 + rgb);
00595  for(i=0;i<n;i++){
00596    *p = (unsigned char)values[i]; p += w*4;
00597  } 
00598 }
00599 
00601 
00602 static void GetRowPixelValues(long rgb, long row, long n, unsigned char *buffer, double *values){
00603  int i;
00604  unsigned char *p;
00605  p=(buffer + row*n*3 + rgb);
00606  for(i=0;i<n;i++){
00607    values[i] = (double)(*p);  p += 3;
00608  }
00609 }
00610 
00611 static void GetColumnPixelValues(long rgb, long col, long n, long w, unsigned char *buffer, double *values){
00612  int i;
00613  unsigned char *p;
00614  p=(buffer + col*3 + rgb);
00615  for(i=0;i<n;i++){
00616    values[i] = (double)(*p);  p += w*3;
00617  } 
00618 }
00619 
00620 static void PutRowPixelValues(long rgb, long row, long n, unsigned char *buffer, double *values){
00621  int i;
00622  unsigned char *p;
00623  p=(buffer + row*n*3 + rgb);
00624  for(i=0;i<n;i++){
00625    *p = (unsigned char)values[i]; p += 3;
00626  }
00627 }
00628 
00629 static void PutColumnPixelValues(long rgb, long col, long n, long w, unsigned char *buffer, double *values){
00630  int i;
00631  unsigned char *p;
00632  p=(buffer + col*3 + rgb);
00633  for(i=0;i<n;i++){
00634    *p = (unsigned char)values[i]; p += w*3;
00635  } 
00636 }
00637 
00638 static void InterpolateLine(long ni, double *pi, long no, double *po, long ij){ // ij used for debug only
00639  int i,xl,xr;
00640  double x,dx,dh;
00641  for(i=0;i<no;i++){
00642    x=((double)i*(double)(ni-1))/(double)(no-1);
00643    xl=(int)x; xr=min(ni-1,xl+1);
00644    dx=x-(double)xl;
00645    dh=pi[xr]-pi[xl];
00646    po[i]=pi[xl]+dx*dh;
00647    //x=(double)i * (double)(ni-1) /(double)(no-1); po[i]=pi[min((int)x,ni-1)];   // nearest
00648  } 
00649 }
00650 
00651 void FilterLine(long ni, long no , double *v, long *nii, long ij){
00652  int i,j,k,subsample,nsamples,remainder;
00653  double sample;
00654  sample = ((double)ni/(double)no);
00655  subsample=(int)sample;
00656  if(sample - (double)subsample > 0.5)subsample += 1;
00657  nsamples = ni / subsample;
00658  remainder = ni - nsamples*subsample;
00659  //if(debug != NULL && ij == 0)fprintf(debug,"Filtering ni=%ld no=%ld subsample=%ld nsamples=%ld remainder=%ld\n",ni,no,subsample,nsamples,remainder);
00660  if(nsamples > 1)for(i=0,k=0;i<nsamples;i++,k += subsample){
00661    *(v+i) = *(v+k);  
00662    if(subsample > 1){
00663      for(j=k+1;j<k+subsample;j++) *(v+i) += *(v+j); 
00664      *(v+i) /= (double)subsample;
00665    }
00666  }
00667  if(remainder == 0)*nii = nsamples;
00668  else{
00669    *(v+i) = *(v+k);
00670    if(remainder > 1){
00671      for(j=k+1;j<k+remainder;j++) *(v+i) += *(v+j);
00672      *(v+i) /= (double)remainder;
00673    }
00674    *nii = nsamples + 1;
00675   }
00676 }
00677 
00678 void ScaleImageBuffer3(long xi, long yi, unsigned char *pi, long xo, long yo, unsigned char *po){
00679  double i,j;
00680  double *vi,*vo;
00681  long xii,yii;
00682  unsigned char *p;
00683  if((vi=(double *)X__Malloc(max(xi,yi)*sizeof(double))) == NULL){return;}
00684  if((vo=(double *)X__Malloc(max(xo,yo)*sizeof(double))) == NULL){X__Free(vi); return;}
00685  if((p=(unsigned char *)X__Malloc(xo*yi*3)) == NULL){ // temp buffer xout by yin
00686    X__Free(p); X__Free(vi); X__Free(vo);
00687    return;
00688  }
00689  //if(debug != NULL)fprintf(debug,"Scale image from (%ld x %ld) to (%ld %ld)\n",xi,yi,xo,yo);
00690  //MessageBox(NULL,"Starting First Pass","Scaling Image",MB_OK);
00691  for(i=0;i<yi;i++)for(j=0;j<3;j++){
00692    GetRowPixelValues(j,i,xi,pi,vi);
00693    if(xi > xo)FilterLine(xi,xo,vi,&xii,i+j); else xii=xi;
00694    InterpolateLine(xii,vi,xo,vo,i+j);
00695    PutRowPixelValues(j,i,xo,p,vo);
00696  }
00697  //MessageBox(NULL,"Starting Second Pass","Scaling Image",MB_OK);
00698  for(i=0;i<xo;i++)for(j=0;j<3;j++){
00699    GetColumnPixelValues(j,i,yi,xo,p,vi);
00700    if(yi > yo)FilterLine(yi,yo,vi,&yii,i+j); else yii=yi;
00701    InterpolateLine(yii,vi,yo,vo,i+j);
00702    PutColumnPixelValues(j,i,yo,xo,po,vo);
00703  }
00704  X__Free(p);
00705  X__Free(vi);
00706  X__Free(vo);
00707 }
00708 
00709 void ScaleImageBuffer4(long xi, long yi, unsigned char *pi, long xo, long yo, unsigned char *po){
00710  double i,j;
00711  double *vi,*vo;
00712  long xii,yii;
00713  unsigned char *p;
00714  if((vi=(double *)X__Malloc(max(xi,yi)*sizeof(double))) == NULL){return;}
00715  if((vo=(double *)X__Malloc(max(xo,yo)*sizeof(double))) == NULL){X__Free(vi); return;}
00716  if((p=(unsigned char *)X__Malloc(xo*yi*4)) == NULL){ // temp buffer xout by yin
00717    X__Free(p); X__Free(vi); X__Free(vo);
00718    return;
00719  }
00720  //if(debug != NULL)fprintf(debug,"Scale image 4 way from (%ld x %ld) to (%ld %ld)\n",xi,yi,xo,yo);
00721  //MessageBox(NULL,"Starting First Pass","Scaling Image",MB_OK);
00722  for(i=0;i<yi;i++)for(j=0;j<4;j++){
00723    GetRowPixelValues4(j,i,xi,pi,vi);
00724    if(xi > xo)FilterLine(xi,xo,vi,&xii,i+j); else xii=xi;
00725    InterpolateLine(xii,vi,xo,vo,i+j);
00726    PutRowPixelValues4(j,i,xo,p,vo);
00727  }
00728  //MessageBox(NULL,"Starting Second Pass","Scaling Image",MB_OK);
00729  for(i=0;i<xo;i++)for(j=0;j<4;j++){
00730    GetColumnPixelValues4(j,i,yi,xo,p,vi);
00731    if(yi > yo)FilterLine(yi,yo,vi,&yii,i+j); else yii=yi;
00732    InterpolateLine(yii,vi,yo,vo,i+j);
00733    PutColumnPixelValues4(j,i,yo,xo,po,vo);
00734  }
00735  X__Free(p);
00736  X__Free(vi);
00737  X__Free(vo);
00738 }
00739 
00740 
00742 
00743 void CompleteMovieMapLoading(long ObjectID, char *objectfilename){
00744  // Load any outstanding image maps that have been found as embedded maps
00745  // in the MFX file.
00746  int i,n;
00747  imap *Map;
00748  RenderIMAP temp_map,*map;
00749  map=&temp_map;
00750  if((n=Object[ObjectID].NoMaps) > 0 &&
00751     (Map=Object[ObjectID].Mbase) != NULL){
00752    for(i=0;i<n;i++,Map++){
00753      if(Map->bAnimS && Map->pp >= 0){
00754        //if(debug != NULL){fprintf(debug,"Getting Movie map parts for Map %ld Object[%s]\n",i,objectfilename); fflush(debug);}
00755        map->p24R=NULL; map->p24G=NULL; map->p24B=NULL;
00756        if(LoadExternalMovieFrame(Map->filename,objectfilename,map) == 1){
00757          Map->p24R=map->p24R; Map->p24G=map->p24G; Map->p24B=map->p24B;
00758          Map->xmax=map->xmax;
00759          Map->ymax=map->ymax;
00760          Map->bLoadedS=TRUE;
00761        }
00762        else Map->pp = -1;
00763      }
00764      if(Map->bAnimR && Map->rp >= 0){
00765        map->p24R=NULL; map->p24G=NULL; map->p24B=NULL;
00766        if(LoadExternalMovieFrame(Map->frefl,objectfilename,map) == 1){
00767          Map->p24Rrefl=map->p24R;
00768          Map->p24Grefl=map->p24G;
00769          Map->p24Brefl=map->p24B;
00770          Map->xmaxrefl=map->xmax;
00771          Map->ymaxrefl=map->ymax;
00772          Map->bLoadedR=TRUE;
00773        }
00774        else Map->rp = -1;
00775      }
00776      if(Map->bAnimB && Map->bp >= 0){
00777        map->p24R=NULL; map->p24G=NULL; map->p24B=NULL;
00778        if(LoadExternalMovieFrame(Map->fbump,objectfilename,map) == 1){
00779          Map->p24Rbump=map->p24R;
00780          Map->p24Gbump=map->p24G;
00781          Map->p24Bbump=map->p24B;
00782          Map->xmaxbump=map->xmax;
00783          Map->ymaxbump=map->ymax;
00784          Map->bLoadedB=TRUE;
00785        }
00786        else Map->bp = -1;
00787      }
00788      if(Map->bAnimT && Map->tp >= 0){
00789        map->p24R=NULL; map->p24G=NULL; map->p24B=NULL;
00790        if(LoadExternalMovieFrame(Map->ftran,objectfilename,map) == 1){
00791          Map->p24Rtran=map->p24R;
00792          Map->p24Gtran=map->p24G;
00793          Map->p24Btran=map->p24B;
00794          Map->xmaxtran=map->xmax;
00795          Map->ymaxtran=map->ymax;
00796          Map->bLoadedT=TRUE;
00797        }
00798        else Map->tp = -1;
00799      }
00800    }
00801  }
00802 }
00803 
00804 void FreeMovieMapImages(long ObjectID){
00805  int i,n;
00806  imap *Map;
00807  if((n=Object[ObjectID].NoMaps) > 0 &&
00808     (Map=Object[ObjectID].Mbase) != NULL){
00809    for(i=0;i<n;i++,Map++){
00810      if(Map->bAnimS && Map->pp >= 0){
00811        //if(debug != NULL){fprintf(debug,"Freeing Movie map parts for Map %ld\n",i); fflush(debug);}
00812        if(Map->p24R != NULL)X__Free(Map->p24R); Map->p24R=NULL;
00813        if(Map->p24G != NULL)X__Free(Map->p24G); Map->p24G=NULL;
00814        if(Map->p24B != NULL)X__Free(Map->p24B); Map->p24B=NULL;
00815        Map->bLoadedS=FALSE;
00816      }
00817      if(Map->bAnimR && Map->rp >= 0){
00818        if(Map->p24R != NULL)X__Free(Map->p24Rrefl); Map->p24Rrefl=NULL;
00819        if(Map->p24G != NULL)X__Free(Map->p24Grefl); Map->p24Grefl=NULL;
00820        if(Map->p24B != NULL)X__Free(Map->p24Brefl); Map->p24Brefl=NULL;
00821        Map->bLoadedR=FALSE;
00822      }
00823      if(Map->bAnimB && Map->bp >= 0){
00824        if(Map->p24R != NULL)X__Free(Map->p24Rbump); Map->p24Rbump=NULL;
00825        if(Map->p24G != NULL)X__Free(Map->p24Gbump); Map->p24Gbump=NULL;
00826        if(Map->p24B != NULL)X__Free(Map->p24Bbump); Map->p24Bbump=NULL;
00827        Map->bLoadedB=FALSE;
00828      }
00829      if(Map->bAnimT && Map->tp >= 0){
00830        if(Map->p24R != NULL)X__Free(Map->p24Rtran); Map->p24Rtran=NULL;
00831        if(Map->p24G != NULL)X__Free(Map->p24Gtran); Map->p24Gtran=NULL;
00832        if(Map->p24B != NULL)X__Free(Map->p24Btran); Map->p24Btran=NULL;
00833        Map->bLoadedT=FALSE;
00834      }
00835    }
00836  }
00837 }
00838 
00839 static short LoadExternalMovieFrame(char *fn, char *ofname, RenderIMAP *map){
00840  long x,y;
00841  FILE *t24;
00842  char lname[256],rep_name[256];
00843  HMODULE hLib;
00844  FARPROC fpFun;
00845  BOOL (*fpGetMapSize)(char *,
00846                       long *, long *, long);
00847  BOOL (*fpGetMap)(char *,
00848                   long, long,
00849                   unsigned char *, unsigned char *, unsigned char *, long);
00850  Render_Message(61,0,fn);
00851  strcpy(rep_name,"%%**%%"); //some dummy string that will never be a filename
00852  //MessageBox(NULL,ofname,"External Map Object",MB_OK);
00853  t24=GetRenderRelativeFile(fn,ofname,rep_name);
00854  if(t24 == NULL)return 0;
00855  fclose(t24);
00856  if(strcmp(rep_name,"%%**%%") != 0){ // string was replaced
00857    fn=rep_name;
00858  }
00859  strcpy(lname,gszHomeDir);
00860  strcat(lname,"avi_map.dll");
00861  hLib=AddLoadedLibrary(lname);
00862  //MessageBox(NULL,fn,lname,MB_OK);
00863  if(hLib != NULL){
00864    if((fpFun=GetProcAddress(hLib,"_GetExternalImageSize")) != NULL){
00865      fpGetMapSize = (void *)fpFun;
00866      if((*fpGetMapSize)(fn,&x,&y,RenderCurrentFrame)){
00867        //if(debug != NULL)fprintf(debug,"Loading AVI [%s] size %ld by %ld frame %ld\n",fn,x,y,RenderCurrentFrame);
00868        if((map->p24R = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00869        if((map->p24G = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00870        if((map->p24B = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00871        if((fpFun=GetProcAddress(hLib,"_GetExternalImage")) != NULL){
00872          fpGetMap = (void *)fpFun;
00873          if(!(*fpGetMap)(fn,x,y,map->p24R,map->p24G,map->p24B,RenderCurrentFrame))goto CLOSEOUT;
00874        }
00875        else goto CLOSEOUT;
00876      }
00877      else goto CLOSEOUT;
00878    }
00879    else goto CLOSEOUT;
00880  }
00881  else return 0;
00882 
00883  MakeScaledImage(FALSE,&x,&y,&(map->p24R),&(map->p24G),&(map->p24B)); 
00884 
00885  map->xmax = (short)x;
00886  map->ymax = (short)y;
00887 //{int i,j; unsigned char *r, *b, *g; r=map->p24B; g=map->p24G; b=map->p24B; 
00888 // for(i=0;i<y;i++)for(j=0;j<x;j++){*r++=0; *g++=0; *b++=255;} }
00889  //if(debug != NULL)fprintf(debug,"Loaded AVI [%s] size %ld by %ld frame %ld\n",fn,x,y,RenderCurrentFrame);
00890  return 1;
00891  CLOSEOUT:
00892  //if(debug != NULL)fprintf(debug,"Failed to load movie frame\n");
00893  if(map->p24R != NULL)X__Free(map->p24R); map->p24R = NULL;
00894  if(map->p24G != NULL)X__Free(map->p24G); map->p24G = NULL;
00895  if(map->p24B != NULL)X__Free(map->p24B); map->p24B = NULL;
00896  Render_Message(62,0,fn);
00897  return 0;
00898 }

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