00001
00002
00003
00004
00005
00006
00007
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <math.h>
00011 #include <setjmp.h>
00012 #include <windows.h>
00013
00014 #include "struct.h"
00015 #include "callback.h"
00016 #include "height.h"
00017 #include "..\animate\spin_dll.h"
00018
00019 #define DESELECTED 0
00020 #define SELECTED 1
00021
00022 #include "..\common\mem.h"
00023
00024 #if __X__MIPS__
00025 BOOL WINAPI _CRT_INIT(HINSTANCE ,DWORD , LPVOID );
00026 #endif
00027
00028 extern short read_gif_image(char *, short);
00029 extern unsigned char p_red[],p_green[],p_blue[];
00030 extern int colourmap_size,x_size,y_size;
00031
00032 int out_line(unsigned char *linepixels, int linelen);
00033
00034 static BOOL CALLBACK HeightFieldDlgProc(HWND hwnd, UINT msg,
00035 WPARAM wparam, LPARAM lparam);
00036
00037 static HWND hParent;
00038 static HINSTANCE hThisInstance;
00039 static unsigned char *pixels;
00040 static long inter=0,gridx=10,gridy=10;
00041 static vertex *MainVp;
00042 static edge *MainEp;
00043 static face *MainFp;
00044 static long Nvert,Nedge,Nface,NvertSelect;
00045 static void (*CreateVertex)(void);
00046 static void (*CreateEdge)(long, long);
00047 static void (*CreateFace)(long, long, long);
00048
00049 #if __WATCOMC__
00050 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00051 #elif __BC__
00052 BOOL WINAPI DllEntryPoint(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00053 #else
00054 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00055 #endif
00056 switch (dwReason) {
00057 case DLL_PROCESS_ATTACH: {
00058 #if __X__MIPS__
00059 if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00060 #endif
00061 hThisInstance=hDLL;
00062
00063 break;
00064 }
00065 case DLL_PROCESS_DETACH:
00066 #if __X__MIPS__
00067 if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00068 #endif
00069
00070 break;
00071 }
00072 return TRUE;
00073 }
00074
00075 #if __SC__
00076 #pragma startaddress(DllMain)
00077 #endif
00078
00079 BOOL _HeightField
00080 (HWND parent_window, char *filename,X__STRUCTURE *lpEVI){
00081 char str[256];
00082 unsigned char *lpBitmapBits,val,r,g,b;
00083 long x,y,lx,ly,total,height,fvp,vp;
00084 long NpointerX,NpointerY,NpointerZ;
00085 long TVsizeX,TVsizeY,TVsizeZ;
00086 long TVpointX,TVpointY,TVpointZ;
00087 hParent=parent_window;
00088 gridx=10; gridy=10; inter=4;
00089 if(DialogBox(hThisInstance,MAKEINTRESOURCE(DLG_HEIGHT),GetFocus(),
00090 (DLGPROC)HeightFieldDlgProc) != OK)return FALSE;
00091 if(read_gif_image(filename,1) < 0){
00092 MessageBox ( GetFocus(), (LPCTSTR) "Height Field: Invalid GIF file",
00093 (LPCTSTR) "", MB_OK | MB_SYSTEMMODAL );
00094 return FALSE;
00095 }
00096
00097
00098 if((lpBitmapBits=(unsigned char *)X__Malloc(x_size*y_size)) == NULL){
00099 MessageBox ( GetFocus(), (LPCTSTR) "Height Field: Out of memory",
00100 (LPCTSTR) "", MB_OK | MB_SYSTEMMODAL );
00101 return FALSE;
00102 }
00103 pixels=lpBitmapBits;
00104 if(read_gif_image(filename,0) < 0){
00105 MessageBox ( GetFocus(), (LPCTSTR) "Height Field: Error reading GIF file",
00106 (LPCTSTR) "", MB_OK | MB_SYSTEMMODAL );
00107 X__Free(lpBitmapBits);
00108 return FALSE;
00109 }
00110 NpointerX = *(lpEVI->NpointerX);
00111 NpointerY = *(lpEVI->NpointerY);
00112 NpointerZ = *(lpEVI->NpointerZ);
00113 TVsizeX = *(lpEVI->TVsizeX);
00114 TVsizeY = *(lpEVI->TVsizeY);
00115 TVsizeZ = *(lpEVI->TVsizeZ);
00116 TVpointX = *(lpEVI->TVpointX);
00117 TVpointY = *(lpEVI->TVpointY);
00118 TVpointZ = *(lpEVI->TVpointZ);
00119 Nvert = *(lpEVI->Nvert);
00120 Nedge = *(lpEVI->Nedge);
00121 Nface = *(lpEVI->Nface);
00122 NvertSelect = *(lpEVI->NvertSelect);
00123 CreateVertex = lpEVI->fpCV;
00124 CreateEdge = lpEVI->fpCE;
00125 CreateFace = lpEVI->fpCF;
00126 SendMessage(hParent,WM_COMMAND,IDC_UPDATE_VERTEXHEAP,
00127 (LPARAM)(Nvert+gridx*gridy));
00128 SendMessage(hParent,WM_COMMAND,IDC_UPDATE_EDGEHEAP,
00129 (LPARAM)(Nedge+3*gridx*gridy-2*(gridx+gridy)+1));
00130 SendMessage(hParent,WM_COMMAND,IDC_UPDATE_FACEHEAP,
00131 (LPARAM)(Nface+2*(gridx-1)*(gridy-1)));
00132 MainVp = *(lpEVI->MainVp);
00133 MainEp = *(lpEVI->MainEp);
00134 MainFp = *(lpEVI->MainFp);
00135 for(y=0; y<(y_size/gridy)*gridy; y+=y_size/gridy){
00136 for(x=0; x<(x_size/gridx)*gridx; x+=x_size/gridx){
00137 total=0;
00138 for(ly=0; ly<y_size/gridy; ++ly){
00139 for(lx=0; lx<x_size/gridx; ++lx){
00140 val = *(lpBitmapBits+(long)((long)(y+ly)*x_size+(x+lx)));
00141 r=(unsigned char)p_red[val] << 2;
00142 g=(unsigned char)p_green[val] << 2;
00143 b=(unsigned char)p_blue[val] << 2;
00144 switch(inter){
00145 case 0: height=r; break;
00146 case 1: height=g; break;
00147 case 2: height=b; break;
00148 case 3: height=max(r,g); height=max(height,b); break;
00149 case 4: height=(r+g+b)/3; break;
00150 }
00151 total+=height;
00152 }
00153 }
00154 total /= (y_size/gridy) * (x_size/gridx);
00155 CreateVertex();
00156 Nvert = *(lpEVI->Nvert);
00157 NvertSelect = *(lpEVI->NvertSelect);
00158 if(x == 0 && y == 0)fvp=Nvert-1;
00159 (MainVp+Nvert-1)->xyz[0] = TVpointX + (long)(x*TVsizeX)/(long)x_size;
00160 (MainVp+Nvert-1)->xyz[1] = TVpointY+TVsizeY-(long)(y*TVsizeY)/(long)y_size;
00161 (MainVp+Nvert-1)->xyz[2] = NpointerZ + (NpointerZ+TVsizeZ)/(4*256) * total;
00162 }
00163 }
00164 vp=fvp;
00165 for(y=0;y<gridy;y++){
00166 for(x=0;x<gridx;x++){
00167 if(x > 0){
00168 CreateEdge(vp,vp-1); Nedge = *(lpEVI->Nedge);
00169 }
00170 if(y > 0){
00171 CreateEdge(vp,vp-gridx); Nedge = *(lpEVI->Nedge);
00172 if(x > 0){
00173 CreateEdge(vp,vp-gridx-1); Nedge = *(lpEVI->Nedge);
00174 CreateFace(vp,vp-gridx,vp-gridx-1); Nface = *(lpEVI->Nface);
00175 CreateFace(vp,vp-gridx-1,vp-1); Nface = *(lpEVI->Nface);
00176 }
00177 }
00178 vp++;
00179 }
00180 }
00181 X__Free(lpBitmapBits);
00182 return TRUE;
00183 }
00184
00185 static BOOL CALLBACK HeightFieldDlgProc(HWND hwnd, UINT msg,
00186 WPARAM wparam, LPARAM lparam){
00187 int i;
00188 BOOL err;
00189 switch( msg ) {
00190 case WM_INITDIALOG:
00191 SetDlgItemInt(hwnd,DLG_HEIGHT_COLUMNS,gridx,FALSE);
00192 SendDlgItemMessage(hwnd,DLG_HEIGHT_COLUMNS,EM_LIMITTEXT,(WPARAM)6,0);
00193 SendDlgItemMessage(hwnd,DLG_HEIGHT_COLUMNSS,SPNM_SETRANGE,0,
00194 MAKELPARAM(2,200));
00195 SendDlgItemMessage(hwnd,DLG_HEIGHT_COLUMNSS,SPNM_SETCRNTVALUE,
00196 (WPARAM)gridx,0);
00197 SendDlgItemMessage(hwnd,DLG_HEIGHT_COLUMNSS,SPNM_SETEDITCTRL,0,
00198 (LPARAM)GetDlgItem(hwnd,DLG_HEIGHT_COLUMNS));
00199 SetDlgItemInt(hwnd,DLG_HEIGHT_ROWS,gridy,FALSE);
00200 SendDlgItemMessage(hwnd,DLG_HEIGHT_ROWS,EM_LIMITTEXT,(WPARAM)6,0);
00201 SendDlgItemMessage(hwnd,DLG_HEIGHT_ROWSS,SPNM_SETRANGE,0,
00202 MAKELPARAM(2,200));
00203 SendDlgItemMessage(hwnd,DLG_HEIGHT_ROWSS,SPNM_SETCRNTVALUE,
00204 (WPARAM)gridy,0);
00205 SendDlgItemMessage(hwnd,DLG_HEIGHT_ROWSS,SPNM_SETEDITCTRL,0,
00206 (LPARAM)GetDlgItem(hwnd,DLG_HEIGHT_ROWS));
00207 SendDlgItemMessage(hwnd,DLG_HEIGHT_LOW,BM_SETCHECK,TRUE,0);
00208 SendDlgItemMessage(hwnd,DLG_HEIGHT_AVERAGE,BM_SETCHECK,TRUE,0);
00209 SendMessage(hParent,WM_COMMAND,IDC_CENTRE_DIALOG_CURSOR,(LPARAM)hwnd);
00210 return (TRUE);
00211 case WM_COMMAND:
00212 switch(LOWORD(wparam)){
00213 case DLG_HEIGHT_RED: inter=0; break;
00214 case DLG_HEIGHT_GREEN: inter=1; break;
00215 case DLG_HEIGHT_BLUE: inter=2; break;
00216 case DLG_HEIGHT_MAX: inter=3; break;
00217 case DLG_HEIGHT_AVERAGE: inter=4; break;
00218 case DLG_HEIGHT_LOW: i=10; goto SETSIZE;
00219 case DLG_HEIGHT_MEDIUM: i=40; goto SETSIZE;
00220 case DLG_HEIGHT_HIGH: i=80;
00221 SETSIZE:
00222 gridx=gridy=i;
00223 SetDlgItemInt(hwnd,DLG_HEIGHT_COLUMNS,gridx,FALSE);
00224 SendDlgItemMessage(hwnd,DLG_HEIGHT_COLUMNSS,SPNM_SETCRNTVALUE,
00225 (WPARAM)gridx,0);
00226 SetDlgItemInt(hwnd,DLG_HEIGHT_ROWS,gridy,FALSE);
00227 SendDlgItemMessage(hwnd,DLG_HEIGHT_ROWSS,SPNM_SETCRNTVALUE,
00228 (WPARAM)gridy,0);
00229 break;
00230 case DLG_HEIGHT_CANCEL:
00231 EndDialog(hwnd, FAIL);
00232 return(TRUE);
00233 case DLG_HEIGHT_OK:
00234 i=GetDlgItemInt(hwnd,DLG_HEIGHT_COLUMNS,&err,FALSE);
00235 if(!err)EndDialog(hwnd, FAIL);
00236 if(i > 200 || i < 2)EndDialog(hwnd, FAIL);
00237 gridx=i;
00238 i=GetDlgItemInt(hwnd,DLG_HEIGHT_ROWS,&err,FALSE);
00239 if(!err)EndDialog(hwnd, FAIL);
00240 if(i > 200 || i < 2)EndDialog(hwnd, FAIL);
00241 gridy=i;
00242 EndDialog(hwnd,OK);
00243 return(TRUE);
00244 default:
00245 break;
00246 }
00247 break;
00248 default: break;
00249 }
00250 return(FALSE);
00251 }
00252
00253 int out_line(unsigned char *linepixels, int linelen){
00254 memcpy(pixels, linepixels, linelen);
00255 pixels += linelen;
00256 return 0;
00257 }
00258