aniramimage.c

Go to the documentation of this file.
00001 /* file ANIRAMIMAGE.C */
00002 
00003 // This file manages RAM images for the iamge maps;
00004 
00005 #define MODULE_RAMIMAGE 1
00006 
00007 #include "animate.h"
00008 
00009 #define ALIGNULONG(i)   ((i+3)/4*4)
00010 #define  temp_image_name "OFX_T_$$"
00011 #define  temp_image_nameG "OFX_G_$$_"
00012 #define  temp_image_nameS "OFX_S_$$_"
00013 
00014 static int IdentifyImageFileType(char *fn);
00015 static int  number_of_file_extensions=6;
00016 static char *image_file_extensions[]={".gif",".tga",".tif",".jpg",".png",".bmp"};
00017 
00018 
00019 void InitialiseRamImage(RAMIMAGE *im){
00020  im->size=0;
00021  im->filename=NULL;
00022  im->data=NULL;
00023 }
00024 
00025 BOOL LoadRamImage(RAMIMAGE *image, char *file){ // Read the RAM image file into store
00026  HANDLE fq;
00027  DWORD dwSize,dwRead;
00028  char *pfilename,*pdata;
00029  if(file == NULL)return FALSE;
00030  if(!bEmbedImageMaps)return TRUE;
00031  if(image->filename != NULL || image->data != NULL)UnloadRamImage(image);
00032  fq=CreateFile(file,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,
00033     FILE_ATTRIBUTE_READONLY,(HANDLE)NULL);
00034  if(fq == INVALID_HANDLE_VALUE){
00035    MessageBox(NULL,file,"Bad Handle",MB_OK);
00036    return FALSE;
00037  }
00038  dwSize = GetFileSize (fq, NULL) ; 
00039  //{char xxx[256]; sprintf(xxx,"image [%s] size=%ld",file,dwSize);
00040  //MessageBox(NULL,xxx,"Loading",MB_OK);}
00041  if(dwSize == INVALID_FILE_SIZE || dwSize == 0){
00042    CloseHandle(fq);
00043    return FALSE;
00044  }
00045  if((pfilename=(char *)X__Malloc(strlen(file)+2)) == NULL){
00046    CloseHandle(fq);
00047    MessageBox(NULL,"Bad Read 1",NULL,MB_OK);
00048    return FALSE;
00049  }
00050  if((pdata=(unsigned char*)X__Malloc(dwSize)) == NULL){
00051    X__Free(pfilename);
00052    CloseHandle(fq);
00053    MessageBox(NULL,"Bad Read 2",NULL,MB_OK);
00054    return FALSE;   
00055  }
00056  if(ReadFile(fq,pdata,dwSize,&dwRead,NULL) == 0 || dwRead != dwSize){
00057    X__Free(pfilename);
00058    X__Free(pdata);
00059    CloseHandle(fq);
00060    MessageBox(NULL,"Bad Read 3",NULL,MB_OK);
00061    return FALSE;   
00062  } 
00063  strcpy(pfilename,file);
00064  image->filename=pfilename;
00065  image->data=pdata;
00066  image->size=dwSize;
00067  CloseHandle(fq);
00068  return TRUE;
00069 }
00070 
00071 BOOL UnloadRamImage(RAMIMAGE *image){
00072  //if(image->filename != NULL)MessageBox(NULL,image->filename,"Unloading",MB_OK);
00073  if(image->data != NULL)X__Free(image->data);
00074  if(image->filename != NULL)X__Free(image->filename);
00075  InitialiseRamImage(image);
00076  return TRUE;
00077 }
00078 
00079 BOOL CopyRamImage(RAMIMAGE *dest,RAMIMAGE *source){
00080  char *pfilename,*pdata;
00081  InitialiseRamImage(dest);
00082  if(source->filename != NULL && source->data != NULL){
00083    if((pfilename=(char *)X__Malloc(strlen(source->filename)+2)) == NULL)return FALSE;
00084    if((pdata=(unsigned char*)X__Malloc(source->size)) == NULL){
00085      X__Free(pfilename); return FALSE;   
00086    }
00087    dest->filename=pfilename;
00088    dest->data=pdata;
00089    dest->size=source->size;
00090    memcpy(dest->filename,source->filename,strlen(source->filename)+2);
00091    memcpy(dest->data,source->data,source->size);
00092    //MessageBox(NULL,dest->filename,"RAMIMAGE copied",MB_OK);
00093    return TRUE;
00094  }
00095  return FALSE;
00096 }
00097 
00098 BOOL UnstoreRamImage(RAMIMAGE *image, char *filename){ // used for saving images to files
00099  HANDLE fq;
00100  DWORD dwSize,dwWrite;
00101  char file[256];
00102  if(image->filename != NULL &&  image->data != NULL && image->size > 0){
00103    if(filename == NULL) // unstore to original location
00104          strcpy(file,image->filename);
00105    else strcpy(file,filename);
00106    fq=CreateFile(file,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,
00107                   FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
00108    if(fq == INVALID_HANDLE_VALUE){
00109     MessageBox(NULL,file,"Bad Handle",MB_OK);
00110     return FALSE;
00111    }
00112    dwSize=image->size;
00113    if(WriteFile(fq,image->data ,dwSize,&dwWrite,NULL) != 0 && dwWrite == dwSize){
00114      CloseHandle(fq);
00115      return TRUE;
00116    }
00117    else CloseHandle(fq);
00118  }
00119  return FALSE;
00120 }
00121 
00122 static int IdentifyImageFileType(char *fn){
00123  int i; char last4[5];
00124  last4[4]=0;
00125  strncpy(last4,(fn+strlen(fn)-4),4);
00126  strlwr(last4);
00127  for(i=0;i<number_of_file_extensions;i++){
00128    if(strncmp((char *)last4,image_file_extensions[i],4) == 0)return i;
00129  }
00130  return -1; 
00131 }
00132 
00133 void DisplayImageFromStore(RAMIMAGE *image, char *alternate_name){
00134  HANDLE fq;
00135  DWORD dwSize,dwWrite;
00136  int id;
00137  char file[256];
00138  if(image->filename != NULL &&  image->data != NULL && image->size > 0){
00139    id=IdentifyImageFileType(image->filename);
00140    if(id < 0){
00141      MessageBox(NULL,"Bad Type",NULL,MB_OK);
00142      return;
00143    }
00144    sprintf(file,"%s%s%s",TempPath,temp_image_name,image_file_extensions[id]);
00145    fq=CreateFile(file,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,
00146                   FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
00147    if(fq == INVALID_HANDLE_VALUE){
00148      MessageBox(NULL,file,"Bad Handle",MB_OK);
00149      return;
00150    }
00151    dwSize=image->size;
00152    if(WriteFile(fq,image->data ,dwSize,&dwWrite,NULL) != 0 && dwWrite == dwSize){
00153      CloseHandle(fq);
00154      Sleep(10);
00155      WritePrivateProfileString("VIEWER","MAPFILE",file,IniFilename);
00156      //MessageBox(NULL,file,"Displaying Image",MB_OK); 
00157      LoadViewer(2);
00158    }
00159    else CloseHandle(fq);
00160  }
00161  else{
00162    //MessageBox(NULL,alternate_name,"Displaying Alternate",MB_OK); 
00163    WritePrivateProfileString("VIEWER","MAPFILE",alternate_name,IniFilename);
00164    LoadViewer(2);
00165  }
00166 }
00167 
00168 void EraseTempImageFiles(void){
00169  int i,j;
00170  BOOL bRemoved;
00171  char file[256];
00172  for(i=0;i<number_of_file_extensions;i++){
00173    sprintf(file,"%s%s%s",TempPath,temp_image_name,image_file_extensions[i]);
00174    remove(file);
00175  }
00176  // Remove the temporary Ground and sky Image files that might have been
00177  // written during a script write.
00178  for(j=1;j<100;j++){  // try up to 100 
00179    bRemoved=FALSE;
00180    for(i=0;i<number_of_file_extensions;i++){
00181      sprintf(file,"%s%s%ld%s",TempPath,temp_image_nameS,j,image_file_extensions[i]);
00182      if(remove(file) == 0)bRemoved=TRUE;
00183    }
00184    if(!bRemoved)break;  // stop when none were removoved
00185  }
00186  for(j=1;j<100;j++){
00187    bRemoved=FALSE;
00188    for(i=0;i<number_of_file_extensions;i++){
00189      sprintf(file,"%s%s%ld%s",TempPath,temp_image_nameG,j,image_file_extensions[i]);
00190      if(remove(file) == 0)bRemoved=TRUE;
00191    }
00192    if(!bRemoved)break;
00193  }
00194 }
00195 
00196 static char LastSkyImageFile[256],LastGroundImageFile[256];
00197  
00198 void WriteTemporaryFileForScript(BOOL bSky, RAMIMAGE *image, 
00199                                  char *temp_name, long *lastID){
00200  HANDLE fq;
00201  DWORD dwSize,dwWrite;
00202  int id;
00203  char file[256];
00204  BOOL bFound=FALSE;
00205  if(image->filename != NULL &&  image->data != NULL && image->size > 0){
00206    id=IdentifyImageFileType(image->filename);
00207    if(id < 0){
00208      MessageBox(NULL,"Bad Type",NULL,MB_OK);
00209      return;
00210    }
00211    if(*lastID == 0){
00212      *lastID=1;
00213      if(bSky){
00214        sprintf(file,"%s%s%ld%s",TempPath,temp_image_nameS,1,image_file_extensions[id]);
00215        strcpy(LastSkyImageFile,image->filename);
00216      }
00217      else{
00218        sprintf(file,"%s%s%ld%s",TempPath,temp_image_nameG,1,image_file_extensions[id]);
00219        strcpy(LastGroundImageFile,image->filename);
00220      }
00221    }
00222    else {
00223      if(bSky){
00224        if(strcmp(image->filename,LastSkyImageFile) == 0)bFound=TRUE;
00225        else { 
00226          *lastID = (*lastID) + 1; strcpy(LastSkyImageFile,image->filename);
00227        }
00228        sprintf(file,"%s%s%ld%s",TempPath,temp_image_nameS,*lastID,image_file_extensions[id]);
00229      }
00230      else{
00231        if(strcmp(image->filename,LastGroundImageFile) == 0)bFound=TRUE;
00232        else { 
00233          *lastID = (*lastID) + 1; strcpy(LastGroundImageFile,image->filename);
00234        }
00235        sprintf(file,"%s%s%ld%s",TempPath,temp_image_nameG,*lastID,image_file_extensions[id]);
00236      }
00237    }
00238    strcpy(temp_name,file);
00239    if(bFound)return;
00240    fq=CreateFile(file,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,
00241                   FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
00242    if(fq == INVALID_HANDLE_VALUE){
00243      MessageBox(NULL,file,"Bad Handle",MB_OK);
00244      return;
00245    }
00246    dwSize=image->size;
00247    if(WriteFile(fq,image->data ,dwSize,&dwWrite,NULL) != 0 && dwWrite == dwSize){
00248      CloseHandle(fq);
00249    }
00250    else CloseHandle(fq);
00251  }
00252 }
00253 
00255 
00256 BOOL ReadRamImage(RAMIMAGE *image, FILE *fr){
00257  char *pfilename,*pdata;
00258  DWORD dwRead,dwSize,dwLen;
00259  long l,ls,ld;
00260  getlon_s(&l,fr);
00261  getlon_s(&ls,fr);
00262  //if(l)MessageBox(NULL,"In use","Reading Ram Image",MB_OK);
00263  //else MessageBox(NULL,"Not in use","Reading",MB_OK);
00264  if(l != 1 || ls == 0)return FALSE;
00265  getlon_s(&ld,fr);
00266  if((pfilename=(char *)X__Malloc(ls+1)) == NULL)return FALSE;
00267  if((pdata=(unsigned char*)X__Malloc(ld)) == NULL)return FALSE;
00268  fread(pfilename,ls+1,1,fr);  // Image name
00269  //{char xxx[256]; sprintf(xxx,"[%s] text len=%ld size=%ld",pfilename,ls,ld);
00270  //MessageBox(NULL,xxx,"Reading",MB_OK);}
00271  fread(pdata,ld,1,fr);  // data
00272  image->filename=pfilename;
00273  image->data=pdata;
00274  image->size=ld;
00275  return TRUE;
00276 }
00277 
00278 BOOL WriteRamImage(RAMIMAGE *image, FILE *fp){
00279  long l,zero=0,one;
00280  //if(image->in_use)MessageBox(NULL,image->filename,"Writing Ram Image",MB_OK);
00281  //else MessageBox(NULL,"Not in use","Writing",MB_OK);
00282  outlng_s(1,fp);
00283  if(image->size == 0 || image->filename == NULL || image->data == NULL){
00284    outlng_s(zero,fp);
00285    return TRUE;
00286  }
00287  l=strlen(image->filename);
00288  if(l <= 0){
00289    outlng_s(zero,fp);
00290    return TRUE;
00291  }
00292  outlng_s(l,fp);
00293  outlng_s(image->size,fp);
00294  fwrite(image->filename,strlen(image->filename)+1,1,fp);
00295  fwrite(image->data,image->size,1,fp);
00296  //{char xxx[256]; sprintf(xxx,"[%s] len=%ld size=%ld",image->filename,l,image->size);
00297  //MessageBox(NULL,xxx,"Writing Ram Image",MB_OK);}
00298  return TRUE;
00299 }

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