ramimage.c

Go to the documentation of this file.
00001 /* --
00002 OpenFX - Modelling, Animation and Rendering Package
00003 -- */
00004 
00005 /* file RAMIMAGE.C */
00006 
00007 // This file manages RAM images for the iamge maps;
00008 
00009 #define MODULE_RAMIMAGE 1
00010 
00011 #include "design.h"
00012 
00013 #define ALIGNULONG(i)   ((i+3)/4*4)
00014 #define  temp_image_name "OFX_T_$$"
00015 
00016 static void MakeThumbnailFromMap(RAMIMAGE *image);
00017 static unsigned char *MakeThumbnailFromFile(BOOL bThumb, char *file, 
00018                             long *xx, long *yy, long type);
00019 static int IdentifyImageFileType(char *fn);
00020 static int  number_of_file_extensions=6;
00021 static char *image_file_extensions[]={".gif",".tga",".tif",".jpg",".png",".bmp"};
00022 void ScaleImageBuffer4(long xi, long yi, unsigned char *pi, long xo, long yo, unsigned char *po);
00023 
00024 
00025 void InitialiseRamImage(RAMIMAGE *im){
00026  im->version=1;
00027  im->in_use=FALSE;
00028  im->size=0;
00029  im->filename=NULL;
00030  im->data=NULL;
00031  im->thumbx=0;
00032  im->thumby=0;
00033  im->thumb=NULL;
00034 }
00035 
00036 BOOL LoadRamImage(RAMIMAGE *image, char *file){ // Read the RAM image file into store
00037  HANDLE fq;
00038  DWORD dwSize,dwRead;
00039  char *pfilename,*pdata;
00040  if(file == NULL)return FALSE;
00041  if(!bEmbedImageMaps)return TRUE;
00042  if(image->in_use)UnloadRamImage(image);
00043  //MessageBox(NULL,file,"Load Image File",MB_OK);
00044  fq=CreateFile(file,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,
00045     FILE_ATTRIBUTE_READONLY,(HANDLE)NULL);
00046  if(fq == INVALID_HANDLE_VALUE){
00047    MessageBox(NULL,file,"Bad Handle Load Ram Image",MB_OK);
00048    return FALSE;
00049  }
00050  dwSize = GetFileSize (fq, NULL) ; 
00051  //{char xxx[256]; sprintf(xxx,"image [%s] size=%ld",file,dwSize);
00052  //MessageBox(NULL,xxx,"Loading",MB_OK);}
00053  if(dwSize == INVALID_FILE_SIZE || dwSize == 0){
00054    CloseHandle(fq);
00055    return FALSE;
00056  }
00057  if((pfilename=(char *)X__Malloc(strlen(file)+2)) == NULL){
00058    CloseHandle(fq);
00059    MessageBox(NULL,"Bad Read 1",NULL,MB_OK);
00060    return FALSE;
00061  }
00062  if((pdata=(unsigned char*)X__Malloc(dwSize)) == NULL){
00063    X__Free(pfilename);
00064    CloseHandle(fq);
00065    MessageBox(NULL,"Bad Read 2",NULL,MB_OK);
00066    return FALSE;   
00067  }
00068  if(ReadFile(fq,pdata,dwSize,&dwRead,NULL) == 0 || dwRead != dwSize){
00069    X__Free(pfilename);
00070    X__Free(pdata);
00071    CloseHandle(fq);
00072    MessageBox(NULL,"Bad Read 3",NULL,MB_OK);
00073    return FALSE;   
00074  } 
00075  strcpy(pfilename,file);
00076  image->in_use=TRUE;
00077  image->filename=pfilename;
00078  image->data=pdata;
00079  image->size=dwSize;
00080  CloseHandle(fq);
00081  { // Make the small thumbnal image - this requres reading the file again
00082    // later it should be possible to make the thumbnail by passing the already
00083    // loaded image (JPEG etc.) to MakeThumbnailFromMap() but as yet
00084    // this has to write it to temporary files and re-read those.
00085    unsigned char *p;
00086    long x,y,id;
00087    if(image->in_use && image->filename != NULL && 
00088       image->data != NULL && image->size > 0){
00089       id=IdentifyImageFileType(image->filename);
00090      if(id >= 0){
00091        if((p=MakeThumbnailFromFile(TRUE,file,&x,&y,id)) != NULL){
00092          image->thumb  = p;
00093          image->thumbx = x;
00094          image->thumby = y;
00095        }
00096      }
00097    }
00098  }
00099  return TRUE;
00100 }
00101 
00102 BOOL UnloadRamImage(RAMIMAGE *image){
00103  if(!image->in_use)return TRUE;
00104  //MessageBox(NULL,image->filename,"Image Unloaded From Store",MB_OK);
00105  if(image->data != NULL)X__Free(image->data);
00106  if(image->filename != NULL)X__Free(image->filename);
00107  if(image->thumb != NULL)X__Free(image->thumb);
00108  InitialiseRamImage(image);
00109  return TRUE;
00110 }
00111 
00112 void UnloadRamImagesFromMap(int i){   // called as part of the unload to files 
00113  UnloadRamImage(&(iMap[i].si));
00114  UnloadRamImage(&(iMap[i].ri));
00115  UnloadRamImage(&(iMap[i].bi));
00116  UnloadRamImage(&(iMap[i].ti));
00117 }
00118 
00119 void UnloadRamImagesFromAllMaps(void){   
00120  int i;
00121  if(nImaps == 0)return;
00122  if(SendPrgmQuery(IDQ_CONFIRM,1) != IDYES)return;
00123  for(i=0;i<nImaps;i++){
00124    UnloadRamImage(&(iMap[i].si));
00125    UnloadRamImage(&(iMap[i].ri));
00126    UnloadRamImage(&(iMap[i].bi));
00127    UnloadRamImage(&(iMap[i].ti));
00128  }
00129 }
00130 
00131 void EmbedMapsForMap(int m){
00132  int i;
00133  char filename[256],*p;
00134  IMAGEMAP *map;
00135  RAMIMAGE *image;
00136   map = &(iMap[m]);
00137  for(i=0;i<4;i++){
00138    image=NULL;
00139    if(i == 0 && map->s == 1 && map->S != NULL && map->Type != ANIMBRUSH){
00140       image  = &(map->si); p = map->S;
00141    } 
00142    else if(i == 1 && map->r == 1 && map->R != NULL && map->TypeR != ANIMBRUSH){
00143       image  = &(map->ri); p = map->R;
00144    } 
00145    else if(i == 2 && map->b == 1 && map->B != NULL && map->TypeB != ANIMBRUSH){
00146       image  = &(map->bi); p = map->B;
00147    }
00148    else if(i == 3 && map->t == 1 && map->T != NULL && map->TypeT != ANIMBRUSH){
00149       image  = &(map->ti); p = map->T;
00150    } 
00151    if(image != NULL){
00152      if( ! image->in_use && image->filename == NULL && 
00153             image->data == NULL && image->size == 0){
00154        //MessageBox(NULL,p,"Embedding Map",MB_OK); 
00155        LoadRamImage(image,p);
00156      }
00157    }
00158  }
00159 }
00160 
00161 void EmbedAllMapsImages(void){       
00162   int i;
00163   if(nImaps > 0)for(i=0;i<nImaps;i++){
00164     EmbedMapsForMap(i);
00165   } 
00166 }
00167 
00168 void SaveRamImagesToFiles(int m){ // map m  
00169  int i,j;
00170  char filename[MAX_FILE],new_dir[MAX_DIR],temp[MAX_FILE],text_string[64];
00171  RAMIMAGE *image;
00172  strcpy(temp,"temp.txt"); strcpy(new_dir,TempPath);
00173  LoadString(ghinst_main,IDX_MISC_EXPORTMAPFOLDER,text_string,sizeof(text_string));
00174  if(SelectDirectoryName(temp,new_dir,text_string,"(*.txt)|*.txt|",ghwnd_main) == FALSE)return;
00175  if(strlen(new_dir) == 2)strcat(new_dir,"\\");
00176  if((j=strlen(new_dir)) < 3)return;  if(new_dir[j-1] != '\\')strcat(new_dir,"\\");
00177  for(i=0;i<4;i++){
00178    if(i == 0)image  = &(iMap[m].si);
00179    if(i == 1)image  = &(iMap[m].ri);
00180    if(i == 2)image  = &(iMap[m].bi);
00181    if(i == 3)image  = &(iMap[m].ti);
00182    if(image->in_use && image->filename != NULL && 
00183      image->data != NULL && image->size > 0){
00184      //sprintf(filename,"%s%s",TempPath,short_form(image->filename));
00185      sprintf(filename,"%s%s",new_dir,short_form(image->filename));
00186      UnstoreRamImage(image,filename);
00187    }
00188  }
00189 }
00190 
00191 void SaveRamImagesFromAllMaps(void){ 
00192  int i,m,j;
00193  char filename[MAX_FILE],new_dir[MAX_DIR],temp[MAX_FILE],text_string[64];
00194  RAMIMAGE *image;
00195  if(nImaps < 1)return;
00196  strcpy(temp,"temp.txt"); strcpy(new_dir,TempPath);
00197  LoadString(ghinst_main,IDX_MISC_EXPORTMAPFOLDER,text_string,sizeof(text_string));
00198  if(SelectDirectoryName(temp,new_dir,text_string,"(*.txt)|*.txt|",ghwnd_main) == FALSE)return;
00199  if(strlen(new_dir) == 2)strcat(new_dir,"\\");
00200  if((j=strlen(new_dir)) < 3)return;  if(new_dir[j-1] != '\\')strcat(new_dir,"\\");
00201  for(m=0; m<nImaps; m++){
00202    for(i=0;i<4;i++){
00203      if(i == 0)image  = &(iMap[m].si);
00204      if(i == 1)image  = &(iMap[m].ri);
00205      if(i == 2)image  = &(iMap[m].bi);
00206      if(i == 3)image  = &(iMap[m].ti);
00207      if(image->in_use && image->filename != NULL && 
00208        image->data != NULL && image->size > 0){
00209        //sprintf(filename,"%s%s",TempPath,short_form(image->filename));
00210        sprintf(filename,"%s%s",new_dir,short_form(image->filename));
00211        UnstoreRamImage(image,filename);
00212      }
00213    }
00214  }
00215 }
00216 
00217 BOOL UnstoreRamImage(RAMIMAGE *image, char *filename){ // used for saving images to files
00218  HANDLE fq;
00219  DWORD dwSize,dwWrite;
00220  char file[256];
00221  if(image->in_use && image->filename != NULL && 
00222    image->data != NULL && image->size > 0){
00223    if(filename == NULL) // unstore to original location
00224          strcpy(file,image->filename);
00225    else strcpy(file,filename);
00226    fq=CreateFile(file,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,
00227                   FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
00228    if(fq == INVALID_HANDLE_VALUE){
00229     MessageBox(NULL,file,"Bad Handle Unstore Ram Image",MB_OK);
00230     return FALSE;
00231    }
00232    dwSize=image->size;
00233    if(WriteFile(fq,image->data ,dwSize,&dwWrite,NULL) != 0 && dwWrite == dwSize){
00234      CloseHandle(fq);
00235      return TRUE;
00236    }
00237    else CloseHandle(fq);
00238  }
00239  return FALSE;
00240 }
00241 
00242 static int IdentifyImageFileType(char *fn){
00243  int i; char last4[5];
00244  last4[4]=0;
00245  strncpy(last4,(fn+strlen(fn)-4),4);
00246  strlwr(last4);
00247  for(i=0;i<number_of_file_extensions;i++){
00248    if(strncmp((char *)last4,image_file_extensions[i],4) == 0)return i;
00249  }
00250  return -1; 
00251 }
00252 
00253 void DisplayImageFromStore(RAMIMAGE *image, char *alternate_name){
00254  HANDLE fq;
00255  DWORD dwSize,dwWrite;
00256  int id;
00257  char file[256];
00258  if(image->in_use && image->filename != NULL && 
00259     image->data != NULL && image->size > 0){
00260    id=IdentifyImageFileType(image->filename);
00261    if(id < 0){
00262      MessageBox(NULL,"Bad Type",NULL,MB_OK);
00263      return;
00264    }
00265    sprintf(file,"%s%s%s",TempPath,temp_image_name,image_file_extensions[id]);
00266    fq=CreateFile(file,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,
00267                   FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
00268    if(fq == INVALID_HANDLE_VALUE){
00269      MessageBox(NULL,file,"Bad Handle Display from Store",MB_OK);
00270      return;
00271    }
00272    dwSize=image->size;
00273    if(WriteFile(fq,image->data ,dwSize,&dwWrite,NULL) != 0 && dwWrite == dwSize){
00274      CloseHandle(fq);
00275      Sleep(10);
00276      WritePrivateProfileString("VIEWER","MAPFILE",file,IniFilename);
00277      //MessageBox(NULL,file,"Displaying Image",MB_OK); 
00278      LoadViewer(2);
00279    }
00280    else CloseHandle(fq);
00281  }
00282  else{
00283    //MessageBox(NULL,alternate_name,"Displaying Alternate",MB_OK); 
00284    WritePrivateProfileString("VIEWER","MAPFILE",alternate_name,IniFilename);
00285    LoadViewer(2);
00286  }
00287 }
00288 
00289 void EraseTempImageFiles(void){
00290  int i;
00291  char file[256];
00292  for(i=0;i<number_of_file_extensions;i++){
00293    sprintf(file,"%s%s%s",TempPath,temp_image_name,image_file_extensions[i]);
00294    remove(file);
00295  }
00296  //MessageBox(NULL,"Temp Image files deleted",NULL,MB_OK);
00297 }
00298 
00299 unsigned char *MakeFullMemoryImage(RAMIMAGE *image, long *w, long *h){
00300  HANDLE fq;   // make a full "in ram" image from the map's image data
00301  DWORD dwSize,dwWrite;
00302  int id;
00303  char file[256];
00304  if(image->in_use && image->filename != NULL && 
00305     image->data != NULL && image->size > 0){
00306    id=IdentifyImageFileType(image->filename);
00307    if(id < 0){
00308      MessageBox(NULL,"Bad Type",NULL,MB_OK);
00309      return NULL;
00310    }
00311    sprintf(file,"%s%s%s",TempPath,temp_image_name,image_file_extensions[id]);
00312    fq=CreateFile(file,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,
00313                   FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
00314    if(fq == INVALID_HANDLE_VALUE){
00315      MessageBox(NULL,file,"Bad Handle Make Image data from map",MB_OK);
00316      return NULL;
00317    }
00318    dwSize=image->size;
00319    if(WriteFile(fq,image->data ,dwSize,&dwWrite,NULL) != 0 && dwWrite == dwSize){
00320      unsigned char *p;
00321      long x,y;
00322      CloseHandle(fq);
00323      if((p=MakeThumbnailFromFile(FALSE,file,&x,&y,id)) != NULL){ // make full image
00324        *w = x;
00325        *h = y;
00326        return p;
00327      }
00328    }
00329    else CloseHandle(fq);
00330  }
00331  return NULL;
00332 }
00333 
00334 
00336 
00337 static void MakeThumbnailFromMap(RAMIMAGE *image){
00338  // at present this function is only used by the reading code because there is NO
00339  // original file to work with and we will need to write out the image to a temp file
00340  // and build the thumbnail from that.
00341  // The openGL drawing code EXPECTS that this thumbnail exists (is made here) for ALL embedded
00342  // maps since the OpenGL map loading functions cannot access the embedded maps.
00343  HANDLE fq;
00344  DWORD dwSize,dwWrite;
00345  int id;
00346  char file[256];
00347  if(image->in_use && image->filename != NULL && 
00348     image->data != NULL && image->size > 0){
00349    id=IdentifyImageFileType(image->filename);
00350    if(id < 0){
00351      MessageBox(NULL,"Bad Type",NULL,MB_OK);
00352      return;
00353    }
00354    sprintf(file,"%s%s%s",TempPath,temp_image_name,image_file_extensions[id]);
00355    fq=CreateFile(file,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,
00356                   FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
00357    if(fq == INVALID_HANDLE_VALUE){
00358      MessageBox(NULL,file,"Bad Handle Make Thumbnail from map",MB_OK);
00359      return;
00360    }
00361    dwSize=image->size;
00362    if(WriteFile(fq,image->data ,dwSize,&dwWrite,NULL) != 0 && dwWrite == dwSize){
00363      unsigned char *p;
00364      long x,y;
00365      CloseHandle(fq);
00366      if((p=MakeThumbnailFromFile(TRUE,file,&x,&y,id)) != NULL){
00367        image->thumb  = p;
00368        image->thumbx = x;
00369        image->thumby = y;
00370      }
00371    }
00372    else CloseHandle(fq);
00373  }
00374 }
00375 
00376 static unsigned char *MakeThumbnailFromFile(BOOL bThumb, char *file, 
00377                             long *xx, long *yy, long type){
00378  // make the thumbnail image from the file containing the image 
00379  // will need to identify the file type so that the correct DLL is loaded
00380  int x_size,y_size;
00381  unsigned char *p,*px,*R,*G,*B,*r,*g,*b;
00382  long i,j,imagesize,xi,yi;
00383  long x,y;
00384  FILE *t24;
00385  char lname[256];
00386  HMODULE hLib;
00387  FARPROC fpFun;
00388  BOOL (*fpGetMapSize)(char *, long *, long *);
00389  BOOL (*fpGetMap)(char *, long, long,
00390                           unsigned char *, unsigned char *, unsigned char *);
00391  strcpy(lname,gszHomeDir);
00392  if     (type == 0)strcat(lname,"gif_map.dll");
00393  else if(type == 1)strcat(lname,"tga_map.dll");
00394  else if(type == 2)strcat(lname,"tif_map.dll");
00395  else if(type == 3)strcat(lname,"jpg_map.dll");
00396  else if(type == 4)strcat(lname,"png_map.dll");
00397  else if(type == 5)strcat(lname,"bmp_map.dll");
00398  R=G=B=NULL;
00399  //MessageBox(NULL,lname,file,MB_OK);
00400  hLib=LoadLibrary(lname);
00401  if(hLib != NULL){
00402    if((fpFun=GetProcAddress(hLib,"_GetExternalImageSize")) != NULL){
00403      fpGetMapSize = (void *)fpFun;
00404      if((*fpGetMapSize)(file,&x,&y)){
00405        if((R = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00406        if((G = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00407        if((B = (unsigned char *)X__Malloc(x*y)) == NULL)goto CLOSEOUT;
00408        if((fpFun=GetProcAddress(hLib,"_GetExternalImage")) != NULL){
00409          fpGetMap = (void *)fpFun;
00410          if(!(*fpGetMap)(file,x,y,R,G,B))goto CLOSEOUT;
00411        }
00412        else goto CLOSEOUT;
00413      }
00414      else goto CLOSEOUT;
00415    }
00416    else goto CLOSEOUT;
00417    FreeLibrary(hLib);
00418  }
00419  else return NULL;
00420 // x_size = x; bad when used with scale image and not `long' aligned
00421  x_size=(int)ALIGNULONG(x);
00422  y_size = y;
00423  imagesize=x_size*y_size*3;
00424  if((p=(unsigned char *)X__Malloc(imagesize+3)) == NULL){
00425    X__Free(R);
00426    X__Free(G);
00427    X__Free(B);
00428    return NULL;
00429  }
00430  for(j=0;j<y;j++){  // combine the colour components
00431   px=(p+(y-j-1)*x_size*3);
00432   r=(R+j*x); g=(G+j*x); b=(B+j*x);
00433   for(i=0;i<x;i++){
00434 if(!bThumb){
00435     *px++ = *b++;
00436     *px++ = *g++;
00437     *px++ = *r++;
00438 }
00439 else{
00440     *px++ = *r++;
00441     *px++ = *g++;
00442     *px++ = *b++;
00443 }
00444   }
00445  }
00446  X__Free(R);
00447  X__Free(G);
00448  X__Free(B);
00449  if(!bThumb){ // return the full buffer size buffer
00450    *xx = x;
00451    *yy = y;
00452    return p;
00453  }
00454  if     (x_size <  128)xi=32; else xi=64;
00455  if     (y_size <  128)yi=32; else yi=64;
00456  if((px=(unsigned char *)X__Malloc(xi*yi*3+3)) == NULL){
00457    X__Free((char *)p);
00458    return NULL;
00459  }
00460  ScaleImageBuffer(x_size,y_size,p,xi,yi,px);
00461 // gluScaleImage(GL_RGB,x_size,y_size,GL_UNSIGNED_BYTE,  // Dont use becuase only works
00462 //               p,xi,yi,GL_UNSIGNED_BYTE,px);           // when OpenGL is active
00463  X__Free((char *)p);
00464  *xx = xi;
00465  *yy = yi;
00466  return px;
00467  CLOSEOUT:
00468  if(R != NULL)X__Free(R);
00469  if(G != NULL)X__Free(G);
00470  if(B != NULL)X__Free(B);
00471  FreeLibrary(hLib);
00472  return NULL;
00473 }
00474 
00475 
00477 static void GetRowPixelValues4(long rgb, long row, long n, unsigned char *buffer, double *values){
00478  int i;
00479  unsigned char *p;
00480  p=(buffer + row*n*4 + rgb);
00481  for(i=0;i<n;i++){
00482    values[i] = (double)(*p);  p += 4;
00483  }
00484 }
00485 
00486 static void GetColumnPixelValues4(long rgb, long col, long n, long w, unsigned char *buffer, double *values){
00487  int i;
00488  unsigned char *p;
00489  p=(buffer + col*4 + rgb);
00490  for(i=0;i<n;i++){
00491    values[i] = (double)(*p);  p += w*4;
00492  } 
00493 }
00494 
00495 static void PutRowPixelValues4(long rgb, long row, long n, unsigned char *buffer, double *values){
00496  int i;
00497  unsigned char *p;
00498  p=(buffer + row*n*4 + rgb);
00499  for(i=0;i<n;i++){
00500    *p = (unsigned char)values[i]; p += 4;
00501  }
00502 }
00503 
00504 static void PutColumnPixelValues4(long rgb, long col, long n, long w, unsigned char *buffer, double *values){
00505  int i;
00506  unsigned char *p;
00507  p=(buffer + col*4 + rgb);
00508  for(i=0;i<n;i++){
00509    *p = (unsigned char)values[i]; p += w*4;
00510  } 
00511 }
00512 
00513 
00514 static void GetRowPixelValues(long rgb, long row, long n, unsigned char *buffer, double *values){
00515  int i;
00516  unsigned char *p;
00517  p=(buffer + row*n*3 + rgb);
00518  for(i=0;i<n;i++){
00519    values[i] = (double)(*p);  p += 3;
00520  }
00521 }
00522 
00523 static void GetColumnPixelValues(long rgb, long col, long n, long w, unsigned char *buffer, double *values){
00524  int i;
00525  unsigned char *p;
00526  p=(buffer + col*3 + rgb);
00527  for(i=0;i<n;i++){
00528    values[i] = (double)(*p);  p += w*3;
00529  } 
00530 }
00531 
00532 static void PutRowPixelValues(long rgb, long row, long n, unsigned char *buffer, double *values){
00533  int i;
00534  unsigned char *p;
00535  p=(buffer + row*n*3 + rgb);
00536  for(i=0;i<n;i++){
00537    *p = (unsigned char)values[i]; p += 3;
00538  }
00539 }
00540 
00541 static void PutColumnPixelValues(long rgb, long col, long n, long w, unsigned char *buffer, double *values){
00542  int i;
00543  unsigned char *p;
00544  p=(buffer + col*3 + rgb);
00545  for(i=0;i<n;i++){
00546    *p = (unsigned char)values[i]; p += w*3;
00547  } 
00548 }
00549 
00550 static void InterpolateLine(long ni, double *pi, long no, double *po, long ij){ // ij used for debug only
00551  int i,xl,xr;
00552  double x,dx,dh;
00553  for(i=0;i<no;i++){
00554    x=((double)i*(double)(ni-1))/(double)(no-1);
00555    xl=(int)x; xr=min(ni-1,xl+1);
00556    dx=x-(double)xl;
00557    dh=pi[xr]-pi[xl];
00558    po[i]=pi[xl]+dx*dh;
00559    //x=(double)i * (double)(ni-1) /(double)(no-1); po[i]=pi[min((int)x,ni-1)];   // nearest
00560  } 
00561 }
00562 
00563 void FilterLine(long ni, long no , double *v, long *nii, long ij){
00564  int i,j,k,subsample,nsamples,remainder;
00565  double sample;
00566  sample = ((double)ni/(double)no);
00567  subsample=(int)sample;
00568  if(sample - (double)subsample > 0.5)subsample += 1;
00569  nsamples = ni / subsample;
00570  remainder = ni - nsamples*subsample;
00571  //if(debug != NULL && ij == 0)fprintf(debug,"Filtering ni=%ld no=%ld subsample=%ld nsamples=%ld remainder=%ld\n",ni,no,subsample,nsamples,remainder);
00572  if(nsamples > 1)for(i=0,k=0;i<nsamples;i++,k += subsample){
00573    *(v+i) = *(v+k);  
00574    if(subsample > 1){
00575      for(j=k+1;j<k+subsample;j++) *(v+i) += *(v+j); 
00576      *(v+i) /= (double)subsample;
00577    }
00578  }
00579  if(remainder == 0)*nii = nsamples;
00580  else{
00581    *(v+i) = *(v+k);
00582    if(remainder > 1){
00583      for(j=k+1;j<k+remainder;j++) *(v+i) += *(v+j);
00584      *(v+i) /= (double)remainder;
00585    }
00586    *nii = nsamples + 1;
00587   }
00588 }
00589 
00590 void ScaleImageBuffer(long xi, long yi, unsigned char *pi, long xo, long yo, unsigned char *po){
00591  double i,j;
00592  double *vi,*vo;
00593  long xii,yii;
00594  unsigned char *p;
00595  if((vi=(double *)X__Malloc(max(xi,yi)*sizeof(double))) == NULL){return;}
00596  if((vo=(double *)X__Malloc(max(xo,yo)*sizeof(double))) == NULL){X__Free(vi); return;}
00597  if((p=(unsigned char *)X__Malloc(xo*yi*3)) == NULL){ // temp buffer xout by yin
00598    X__Free(p); X__Free(vi); X__Free(vo);
00599    return;
00600  }
00601  //if(debug != NULL)fprintf(debug,"Scale image from (%ld x %ld) to (%ld %ld)\n",xi,yi,xo,yo);
00602  //MessageBox(NULL,"Starting First Pass","Scaling Image",MB_OK);
00603  for(i=0;i<yi;i++)for(j=0;j<3;j++){
00604    GetRowPixelValues(j,i,xi,pi,vi);
00605    if(xi > xo)FilterLine(xi,xo,vi,&xii,i+j); else xii=xi;
00606    InterpolateLine(xii,vi,xo,vo,i+j);
00607    PutRowPixelValues(j,i,xo,p,vo);
00608  }
00609  //MessageBox(NULL,"Starting Second Pass","Scaling Image",MB_OK);
00610  for(i=0;i<xo;i++)for(j=0;j<3;j++){
00611    GetColumnPixelValues(j,i,yi,xo,p,vi);
00612    if(yi > yo)FilterLine(yi,yo,vi,&yii,i+j); else yii=yi;
00613    InterpolateLine(yii,vi,yo,vo,i+j);
00614    PutColumnPixelValues(j,i,yo,xo,po,vo);
00615  }
00616  X__Free(p);
00617  X__Free(vi);
00618  X__Free(vo);
00619 }
00620 
00621 void ScaleImageBuffer4(long xi, long yi, unsigned char *pi, long xo, long yo, unsigned char *po){  // INCOMPLETE
00622  double i,j;
00623  double *vi,*vo;
00624  long xii,yii;
00625  unsigned char *p;
00626  if((vi=(double *)X__Malloc(max(xi,yi)*sizeof(double))) == NULL){return;}
00627  if((vo=(double *)X__Malloc(max(xo,yo)*sizeof(double))) == NULL){X__Free(vi); return;}
00628  if((p=(unsigned char *)X__Malloc(xo*yi*4)) == NULL){ // temp buffer xout by yin
00629    X__Free(p); X__Free(vi); X__Free(vo);
00630    return;
00631  }
00632  //if(debug != NULL)fprintf(debug,"Scale image from (%ld x %ld) to (%ld %ld)\n",xi,yi,xo,yo);
00633  //MessageBox(NULL,"Starting First Pass","Scaling Image",MB_OK);
00634  for(i=0;i<yi;i++)for(j=0;j<4;j++){
00635    GetRowPixelValues4(j,i,xi,pi,vi);
00636    if(xi > xo)FilterLine(xi,xo,vi,&xii,i+j); else xii=xi;
00637    InterpolateLine(xii,vi,xo,vo,i+j);
00638    PutRowPixelValues4(j,i,xo,p,vo);
00639  }
00640  //MessageBox(NULL,"Starting Second Pass","Scaling Image",MB_OK);
00641  for(i=0;i<xo;i++)for(j=0;j<4;j++){
00642    GetColumnPixelValues4(j,i,yi,xo,p,vi);
00643    if(yi > yo)FilterLine(yi,yo,vi,&yii,i+j); else yii=yi;
00644    InterpolateLine(yii,vi,yo,vo,i+j);
00645    PutColumnPixelValues4(j,i,yo,xo,po,vo);
00646  }
00647  X__Free(p);
00648  X__Free(vi);
00649  X__Free(vo);
00650 }
00651 
00653 
00654 BOOL ReadRamImage(RAMIMAGE *image, HANDLE *fq){
00655  char *pfilename,*pdata;
00656  DWORD dwRead,dwSize,dwLen;
00657  long l,ls,ld;
00658  ReadLongInteger(&l,fq);
00659  image->version=l;
00660  ReadLongInteger(&l,fq);
00661  //if(l)MessageBox(NULL,"In use","Reading Ram Image",MB_OK);
00662  //else MessageBox(NULL,"Not in use","Reading",MB_OK);
00663  if((BOOL)l == FALSE)return FALSE;
00664  ReadLongInteger(&ls,fq);
00665  ReadLongInteger(&ld,fq);
00666  if((pfilename=(char *)X__Malloc(ls+1)) == NULL)return FALSE;
00667  if((pdata=(unsigned char*)X__Malloc(ld)) == NULL)return FALSE;
00668  ReadFile(fq,pfilename,ls+1,&dwRead,NULL);  // Image name
00669  //{char xxx[256]; sprintf(xxx,"[%s] text len=%ld size=%ld",pfilename,ls,ld);
00670  //MessageBox(NULL,xxx,"Reading",MB_OK);}
00671  ReadFile(fq,pdata,ld,&dwRead,NULL);  // data
00672  image->in_use=TRUE;
00673  image->filename=pfilename;
00674  image->data=pdata;
00675  image->size=ld;
00676  MakeThumbnailFromMap(image);  // make the thumbnail for OpenGL renderer
00677  return TRUE;
00678 }
00679 
00680 BOOL WriteRamImage(RAMIMAGE *image, FILE *fp){
00681  long l,zero=0;
00682 //if(image->in_use)MessageBox(NULL,image->filename,"Writing Ram Image",MB_OK);
00683 //else MessageBox(NULL,"Not in use","Writing",MB_OK);
00684  WriteLongInteger(image->version,fp);
00685  if(! image->in_use || image->size == 0 || image->filename == NULL || image->data == NULL){
00686    WriteLongInteger(zero,fp);
00687    return TRUE;
00688  }
00689  l=strlen(image->filename);
00690  if(l <= 0){
00691    WriteLongInteger(zero,fp);
00692    return TRUE;
00693  }
00694  WriteLongInteger(image->in_use,fp);
00695  if( ! image->in_use)return TRUE;
00696  WriteLongInteger(l,fp);
00697  WriteLongInteger(image->size,fp);
00698  fwrite(image->filename,strlen(image->filename)+1,1,fp);
00699  fwrite(image->data,image->size,1,fp);
00700  //{char xxx[256]; sprintf(xxx,"[%s] len=%ld size=%ld",image->filename,l,image->size);
00701  //MessageBox(NULL,xxx,"Writing Ram Image",MB_OK);}
00702  return TRUE;
00703 }

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