BMP_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 /*  BMP_MAP.C  */
00007 
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 
00011 #include <windows.h>
00012 
00013 #define BFT_BITMAP 0x4d42   /* 'BM' */
00014 
00015 #include "../common/mem.h"
00016 
00017 typedef struct SCREENBUFFER {
00018  unsigned char R,G,B,A;
00019 } fullscreenbuffer;
00020 
00021 static long LoadBmpImageMap(char *fn, long *xxx, long *yyy, BOOL size_only,
00022    unsigned char *pR, unsigned char *pG, unsigned char *pB, unsigned char *source_buffer);
00023 static long LoadBmpImageMap32(char *fn, long *xxx, long *yyy, BOOL size_only,
00024    unsigned char *pR, unsigned char *pG, unsigned char *pB, unsigned char *pA, unsigned char *source_buffer);
00025 static HINSTANCE hThisInstance;
00026 
00027 #if __WATCOMC__
00028 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00029 #else
00030 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00031 #endif
00032   switch (dwReason) {
00033     case DLL_PROCESS_ATTACH: {
00034       hThisInstance=hDLL;
00035       break;
00036     }
00037     case DLL_PROCESS_DETACH:
00038       break;
00039   }
00040   return TRUE;
00041 }
00042 
00043 BOOL _GetExternalImageSize
00044  (char *filename,
00045   long *x,
00046   long *y,
00047   unsigned char *source_buffer){
00048  return LoadBmpImageMap(filename,x,y,1,NULL,NULL,NULL,source_buffer);
00049 }
00050 
00051 BOOL _GetExternalImage
00052  (char *filename,
00053   long x, long y,
00054   unsigned char *Red,
00055   unsigned char *Green,
00056   unsigned char *Blue,
00057   unsigned char *source_buffer){
00058  return LoadBmpImageMap(filename,&x,&y,0,Red,Green,Blue,source_buffer);
00059 }
00060 
00061 BOOL _GetExternalImageA
00062  (char *filename,
00063   long x, long y,
00064   unsigned char *Red,
00065   unsigned char *Green,
00066   unsigned char *Blue,
00067   unsigned char *Alpha,
00068   unsigned char *source_buffer){
00069  return LoadBmpImageMap32(filename,&x,&y,0,Red,Green,Blue,Alpha,source_buffer);
00070 }
00071 
00072 
00073 BOOL _PutExternalImage
00074  (char *szOutFileName,
00075   long nx, long ny,
00076   long quality,
00077   fullscreenbuffer *image){
00078  HANDLE hfbm;
00079  DWORD dwRead;
00080  BITMAPFILEHEADER hdr,*lphdr;
00081  BITMAPINFOHEADER bfi,*lpbi;
00082  long i,j,imagesize,mx;
00083  unsigned char *Screen1,*p;
00084  mx=((nx*24+31)/32) * 4;
00085  if((Screen1=(unsigned char *)X__Malloc(nx*ny*3)) == NULL){
00086    MessageBox(NULL,"Out of Memory","Warning",MB_OK);
00087    return FALSE;
00088  }
00089  p=Screen1;
00090  for(i=0;i<ny;i++){
00091    p=Screen1+(ny-i-1)*nx*3;
00092    for(j=0;j<nx;j++){
00093      *p++ = (unsigned char)image->B;
00094      *p++ = (unsigned char)image->G;
00095      *p++ = (unsigned char)image->R;
00096      image++;
00097    }
00098  }
00099  if((hfbm=CreateFile(szOutFileName,GENERIC_WRITE,FILE_SHARE_WRITE,
00100    NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,
00101    (HANDLE)NULL)) == INVALID_HANDLE_VALUE){
00102    X__Free(Screen1);
00103    MessageBox(NULL,szOutFileName,"File Open Fail",MB_OK);
00104    MessageBeep(MB_OK);
00105    return FALSE;
00106  }
00107  lpbi=&bfi;
00108  lpbi->biSize=sizeof(BITMAPINFOHEADER);
00109  lpbi->biWidth=(DWORD)nx;
00110  lpbi->biHeight=(DWORD)ny;
00111  lpbi->biPlanes=1;
00112  lpbi->biBitCount=24;
00113  lpbi->biCompression=BI_RGB;
00114  lpbi->biSizeImage = imagesize = (DWORD)(mx*ny);
00115  lpbi->biXPelsPerMeter=0;
00116  lpbi->biYPelsPerMeter=0;
00117  lpbi->biClrUsed=0;
00118  lpbi->biClrImportant=0;
00119  lphdr=&hdr;
00120  hdr.bfType          = BFT_BITMAP;
00121  hdr.bfSize          = (sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)
00122                         + imagesize);
00123  hdr.bfReserved1     = 0;
00124  hdr.bfReserved2     = 0;
00125  hdr.bfOffBits       = (DWORD)sizeof(BITMAPFILEHEADER) + lpbi->biSize;
00126  WriteFile(hfbm,lphdr,sizeof(BITMAPFILEHEADER),&dwRead,(LPOVERLAPPED)NULL);
00127  WriteFile(hfbm,lpbi,sizeof(BITMAPINFOHEADER),&dwRead,(LPOVERLAPPED)NULL);
00128  WriteFile(hfbm,Screen1,imagesize,&dwRead,(LPOVERLAPPED)NULL);
00129  if(dwRead != imagesize){
00130    MessageBeep(MB_OK);
00131    MessageBox(NULL,"Bad write",NULL,MB_OK);
00132  }
00133  if(!CloseHandle(hfbm)){
00134    MessageBeep(MB_OK);
00135    MessageBox(NULL,"Bad close",NULL,MB_OK);
00136  }
00137  X__Free(Screen1);
00138  return TRUE;
00139 }
00140 
00141 
00142 static long LoadBmpImageMap(char *fn, long *xxx, long *yyy, BOOL size_only,
00143    unsigned char *pR, unsigned char *pG, unsigned char *pB,
00144    unsigned char *source){
00145  BITMAPFILEHEADER bmfh;
00146  BITMAPINFOHEADER bmih;
00147  WORD bits;
00148  RGBQUAD *pRgb;
00149  FILE *t24;
00150  unsigned char *lpBitmapBits,*p;
00151  long imagesize,nc,wi,hi,fail=0,i,j,b;
00152 
00153  if((pRgb=(RGBQUAD *)X__Malloc(300*sizeof(RGBQUAD))) == NULL)return 0;
00154 
00155  t24=fopen((char *)fn,"rb");
00156  if(t24 == NULL)return 0;
00157  fread((char *)&bmfh,sizeof(BITMAPFILEHEADER),1,t24);
00158  fread((char *)&bmih,sizeof(BITMAPINFOHEADER),1,t24);
00159  if(bmih.biClrUsed != 0)nc=bmih.biClrUsed;
00160  else{
00161    bits = bmih.biBitCount;
00162    switch (bits){
00163      case 1:    nc=2;   break;
00164      case 4:    nc=16;  break;
00165      case 8:    nc=256; break;
00166      default:   nc=0;   break;
00167    }
00168  }
00169  *xxx=bmih.biWidth; 
00170  *yyy=bmih.biHeight;
00171  if(size_only){
00172    fclose(t24);
00173    X__Free(pRgb);
00174    return 1;
00175  }
00176 
00177  if(nc > 0)fread((char *)pRgb,nc*sizeof(RGBQUAD),1,t24);
00178  imagesize=bmfh.bfSize-bmfh.bfOffBits;
00179  if((lpBitmapBits=(unsigned char *)X__Malloc(imagesize)) == NULL){
00180    fclose(t24);
00181    X__Free(pRgb);
00182    return 0;
00183  }
00184  memset(lpBitmapBits,0,imagesize);
00185  fread((char *)lpBitmapBits,imagesize,1,t24);
00186  fclose(t24);
00187  wi=bmih.biWidth; hi=bmih.biHeight;
00188 
00189  imagesize=wi*hi;
00190 
00191  for(i=0;i<hi;i++){
00192    if(nc > 0)p=(lpBitmapBits+wi*(hi-i-1));
00193    else      p=(lpBitmapBits+wi*(hi-i-1)*3);
00194 
00195    for(j=0;j<wi;j++){
00196      if(nc > 0){
00197        b=(long)(*p++);
00198        *pR++ = pRgb[b].rgbRed;
00199        *pG++ = pRgb[b].rgbGreen;
00200        *pB++ = pRgb[b].rgbBlue;
00201      }
00202      else{
00203        *pB++ = *p++; *pG++ = *p++; *pR++ = *p++;
00204      }
00205    }
00206  }
00207  X__Free(pRgb);
00208  X__Free(lpBitmapBits);
00209  return 1;
00210 }
00211 
00212 
00213 static long LoadBmpImageMap32(char *fn, long *xxx, long *yyy, BOOL size_only,
00214    unsigned char *pR, unsigned char *pG, unsigned char *pB, unsigned char *pA,
00215    unsigned char *source){
00216  BITMAPFILEHEADER bmfh;
00217  BITMAPINFOHEADER bmih;
00218  WORD bits;
00219  RGBQUAD *pRgb;
00220  FILE *t24;
00221  unsigned char *lpBitmapBits,*p;
00222  long imagesize,nc,wi,hi,fail=0,i,j,b;
00223 
00224  if((pRgb=(RGBQUAD *)X__Malloc(300*sizeof(RGBQUAD))) == NULL)return 0;
00225 
00226  t24=fopen((char *)fn,"rb");
00227  if(t24 == NULL)return 0;
00228  fread((char *)&bmfh,sizeof(BITMAPFILEHEADER),1,t24);
00229  fread((char *)&bmih,sizeof(BITMAPINFOHEADER),1,t24);
00230  if(bmih.biClrUsed != 0)nc=bmih.biClrUsed;
00231  else{
00232    bits = bmih.biBitCount;
00233    switch (bits){
00234      case 1:    nc=2;   break;
00235      case 4:    nc=16;  break;
00236      case 8:    nc=256; break;
00237      default:   nc=0;   break;
00238    }
00239  }
00240  *xxx=bmih.biWidth; 
00241  *yyy=bmih.biHeight;
00242  if(size_only){
00243    fclose(t24);
00244    X__Free(pRgb);
00245    return 1;
00246  }
00247 
00248  if(nc > 0)fread((char *)pRgb,nc*sizeof(RGBQUAD),1,t24);
00249  imagesize=bmfh.bfSize-bmfh.bfOffBits;
00250  if((lpBitmapBits=(unsigned char *)X__Malloc(imagesize)) == NULL){
00251    fclose(t24);
00252    X__Free(pRgb);
00253    return 0;
00254  }
00255  memset(lpBitmapBits,0,imagesize);
00256  fread((char *)lpBitmapBits,imagesize,1,t24);
00257  fclose(t24);
00258  wi=bmih.biWidth; hi=bmih.biHeight;
00259 
00260  imagesize=wi*hi;
00261 
00262  for(i=0;i<hi;i++){
00263    if(nc > 0)p=(lpBitmapBits+wi*(hi-i-1));
00264    else      p=(lpBitmapBits+wi*(hi-i-1)*4);
00265 
00266    for(j=0;j<wi;j++){
00267      if(nc > 0){
00268        b=(long)(*p++);
00269        *pR++ = pRgb[b].rgbRed;
00270        *pG++ = pRgb[b].rgbGreen;
00271        *pB++ = pRgb[b].rgbBlue;
00272        *pA++ = 0;
00273      }
00274      else{
00275        *pB++ = *p++; *pG++ = *p++; *pR++ = *p++; *pA++ = *p++;
00276      }
00277    }
00278  }
00279  X__Free(pRgb);
00280  X__Free(lpBitmapBits);
00281  return 1;
00282 }

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