Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 1st, 2008, 3:24 PM   #1
Cisnotforcookie
Newbie
 
Join Date: May 2008
Posts: 6
Rep Power: 0 Cisnotforcookie is on a distinguished road
Smile image processing

i have to make a gui program to blur,sharpen, and flip an image. i have the general idea behind blurring (averaging the pixels around other pixels), but i dont get how to write the actual algorithim into C. here is what i have so far:
c Syntax (Toggle Plain Text)
  1.  
  2.  
  3. //----------------------------------------------------------------------
  4.  
  5. // SECTION 1: Preprocessor Commands
  6.  
  7. //----------------------------------------------------------------------
  8.  
  9. // Include Win32 API Libraries (headers)
  10.  
  11. #include <windows.h>
  12.  
  13. #include <windowsx.h>
  14.  
  15. #include <commctrl.h>
  16.  
  17. #include <string.h>
  18.  
  19. #include <stdio.h>
  20.  
  21. #include <math.h>
  22.  
  23. // Define the maximum number of letters that text control objects can have
  24.  
  25. #define MAXLETTERS 32000
  26.  
  27. #define MaxSize 512
  28.  
  29. // Define event-ID numbers for the control objects
  30.  
  31. #define ID_BTTN 1001
  32.  
  33. #define ID_FNAME 1002
  34.  
  35. #define ID_FOPEN 2001
  36.  
  37. #define ID_FEXIT 2002
  38.  
  39. #define ID_INV 2003
  40.  
  41. //----------------------------------------------------------------------
  42.  
  43. // SECTION 2: Computational Code
  44.  
  45. //----------------------------------------------------------------------
  46.  
  47. // Declare global variables: flowdata is available to all subsequent functions
  48.  
  49. unsigned char imgdata[MaxSize][MaxSize];
  50.  
  51. int imgheight, imgwidth;
  52.  
  53. // Function DrawData(): draws data from flowdata array onto screen
  54.  
  55. void DrawData(HWND window)
  56.  
  57. {
  58.  
  59. // Declare local variables
  60.  
  61. int col, row;
  62.  
  63. unsigned char shade;
  64.  
  65. HDC drawing_context = GetDC(window);
  66.  
  67. // Draw the pixels
  68.  
  69. for (row = 0 ; row < imgheight ; row++)
  70.  
  71. for (col = 0 ; col < imgwidth ; col++)
  72.  
  73. {
  74.  
  75. shade = imgdata[row][col];
  76.  
  77. SetPixel(drawing_context, col+50, row+50, RGB(shade, shade, shade));
  78.  
  79. }
  80.  
  81. }
  82.  
  83. // flips data from flowdata array onto screen
  84.  
  85. void Inv(HWND window)
  86.  
  87. {
  88.  
  89. // Declare local variables
  90.  
  91. int col, row;
  92.  
  93. unsigned char shade;
  94.  
  95. HDC drawing_context = GetDC(window);
  96.  
  97. // Draw the pixels
  98.  
  99.  
  100. for (row = 0 ; row < imgheight ; row++)
  101.  
  102. for (col = 0 ; col < imgwidth ; col++)
  103.  
  104. {
  105.  
  106. shade = imgdata[row][col];
  107.  
  108. SetPixel(drawing_context, col+50, 195-row, RGB(shade, shade, shade));
  109.  
  110. }
  111.  
  112. }
  113.  
  114. // Function LoadData(): loads data from file name in file name box
  115.  
  116. int LoadData(HWND window)
  117.  
  118. {
  119.  
  120. // Declare local variables
  121.  
  122. HWND file_name_box;
  123.  
  124. char filename[MAXLETTERS], imgformat[80];
  125.  
  126. FILE * infile;
  127.  
  128. int row, col, imgdepth;
  129.  
  130. // Get the input file's name from the File Text Box
  131.  
  132. file_name_box = GetDlgItem(window, ID_FNAME); // get file box handle
  133.  
  134. GetWindowText(file_name_box, filename, MAXLETTERS); // get file name from file box
  135.  
  136. // Open the input file, for reading
  137.  
  138. infile = fopen( filename , "r");
  139.  
  140. if (!infile)
  141.  
  142. {
  143.  
  144. SetWindowText(file_name_box, "ERROR!"); // signal if file does not exist
  145.  
  146. return( -1 );
  147.  
  148. }
  149.  
  150. // Read-in image information from the input file
  151.  
  152. fscanf(infile,"%s", imgformat);
  153.  
  154. fscanf(infile,"%d", &imgwidth);
  155.  
  156. fscanf(infile,"%d", &imgheight);
  157.  
  158. fscanf(infile,"%d", &imgdepth);
  159.  
  160. fgetc(infile);
  161.  
  162. // Read-in the image data from the input file
  163.  
  164. for (row = 0 ; row < imgheight ; row++)
  165.  
  166. for (col = 0 ; col < imgwidth ; col++)
  167.  
  168. imgdata[row][col] = fgetc(infile);
  169.  
  170. // close the input file
  171.  
  172. fclose(infile);
  173.  
  174. return(0);
  175.  
  176. }
  177.  
  178. // Function GetFileName(): opens a file window and gets user-selected file name
  179.  
  180. void GetFileName(HWND window)
  181.  
  182. {
  183.  
  184. // Declare local variables
  185.  
  186. OPENFILENAME open_file_dialog;
  187.  
  188. char filename[ MAXLETTERS ] = "";
  189.  
  190. HWND file_name_box;
  191.  
  192. // Initialize the Open File Dialog Data Structure
  193.  
  194. ZeroMemory(&open_file_dialog, sizeof(open_file_dialog)); // is this really needed?
  195.  
  196. open_file_dialog.lStructSize = sizeof(open_file_dialog);
  197.  
  198. open_file_dialog.hwndOwner = window; // name of parent window
  199.  
  200. open_file_dialog.lpstrFile = filename; // name of file name variable
  201.  
  202. open_file_dialog.nMaxFile = MAXLETTERS; // max No of letters in file name
  203.  
  204. open_file_dialog.lpstrFilter = "PGM Files (*.pgm)\0*.pgm\0All Files (*.*)\0*.*\0"; // type
  205.  
  206. open_file_dialog.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST; // style
  207.  
  208. open_file_dialog.lpstrDefExt = "pgm"; // file extension
  209.  
  210. // Open the File dialog, get the file name from the user and process it if non-NULL
  211.  
  212. if ( GetOpenFileName(&open_file_dialog) )
  213.  
  214. {
  215.  
  216. file_name_box = GetDlgItem(window, ID_FNAME); // get file box handle
  217.  
  218. SetWindowText(file_name_box, filename); // put file name in file box
  219.  
  220. }
  221.  
  222. }
  223.  
  224. //----------------------------------------------------------------------
  225.  
  226. // SECTION 3: GUI Building
  227.  
  228. //----------------------------------------------------------------------
  229.  
  230. // Function MakeControls(): builds control objects on a window
  231.  
  232. void MakeControls(HWND window)
  233.  
  234. {
  235.  
  236. // Create the Label for the file name text box
  237.  
  238. CreateWindowEx(WS_EX_STATICEDGE, "Static", "File Name:",
  239.  
  240. WS_CHILD | WS_VISIBLE,
  241.  
  242. 10, 260, 50, 50, window, NULL, NULL, NULL);
  243.  
  244. // Create the File Name Text box
  245.  
  246. CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", NULL,
  247.  
  248. WS_CHILD | WS_VISIBLE,
  249.  
  250. 70, 260, 250, 50, window, (HMENU) ID_FNAME, NULL, NULL);
  251.  
  252. // Create the Draw button
  253.  
  254. CreateWindowEx(0, "Button", "Draw", WS_CHILD | WS_VISIBLE,
  255.  
  256. 330, 260, 60, 50, window, (HMENU) ID_BTTN, NULL, NULL);
  257.  
  258. // Create the Draw button
  259.  
  260. CreateWindowEx(0, "Button", "Flip", WS_CHILD | WS_VISIBLE,
  261.  
  262. 330, 205, 60, 50, window, (HMENU) ID_INV, NULL, NULL);
  263.  
  264. }
  265.  
  266. // Function MakeMenu(): builds menu objects on a window
  267.  
  268. void MakeMenu(HWND window)
  269.  
  270. {
  271.  
  272. // Declare local variables
  273.  
  274. HMENU filemenu, subfilemenu;
  275.  
  276. // Create a blank file menu
  277.  
  278. filemenu = CreateMenu();
  279.  
  280. // Create a blank sub-menu (popup menu) for the file menu
  281.  
  282. subfilemenu = CreatePopupMenu();
  283.  
  284. // Place Open and Exit items onto the blank popup sub-menu
  285.  
  286. AppendMenu(subfilemenu, MF_STRING, ID_FOPEN, "&Open");
  287.  
  288. AppendMenu(subfilemenu, MF_STRING, ID_FEXIT, "E&xit");
  289.  
  290. // Place the popup sub-menu on the file menu
  291.  
  292. AppendMenu(filemenu, MF_STRING | MF_POPUP, (UINT)subfilemenu, "&File");
  293.  
  294. // Place the file menu on the Main Window of the program
  295.  
  296. SetMenu(window, filemenu);
  297.  
  298. }
  299.  
  300.  
  301. //----------------------------------------------------------------------
  302.  
  303. // SECTION 4: Event Dispatcher
  304.  
  305. //----------------------------------------------------------------------
  306.  
  307. // Function ProcessMessages(): Specifies how to respond to user interactions
  308.  
  309. LRESULT CALLBACK ProcessMessages(HWND window, UINT message,
  310.  
  311. WPARAM wParam, LPARAM lParam)
  312.  
  313. {
  314.  
  315. switch(message)
  316.  
  317. {
  318.  
  319. case WM_CREATE: // what to do when the window is created
  320.  
  321. MakeControls( window );
  322.  
  323. MakeMenu( window );
  324.  
  325. break;
  326.  
  327. case WM_DESTROY: // what to do when the window is X-ed
  328.  
  329. PostQuitMessage(0);
  330.  
  331. break;
  332.  
  333. case WM_COMMAND: // what to do if a control was activated
  334.  
  335. switch(LOWORD( wParam))
  336.  
  337. {
  338.  
  339. case ID_FEXIT: // control was Exit submenu?
  340.  
  341. PostMessage(window, WM_CLOSE, 0, 0);
  342.  
  343. break;
  344.  
  345. case ID_FOPEN: // control was Open submenu?
  346.  
  347. GetFileName( window );
  348.  
  349. break;
  350.  
  351. case ID_BTTN: // control was Draw button?
  352.  
  353. if ( !LoadData(window) )
  354.  
  355. DrawData( window );
  356.  
  357. break;
  358.  
  359. case ID_INV: // control was Flip button?
  360.  
  361. if ( !LoadData(window) )
  362.  
  363. Inv( window );
  364.  
  365. break;
  366.  
  367. }
  368.  
  369. default: // what to do otherwise ( default )
  370.  
  371. return DefWindowProc( window, message, wParam, lParam);
  372.  
  373. }
  374.  
  375. return( 0 );
  376.  
  377. }
  378.  
  379. //----------------------------------------------------------------------
  380.  
  381. // SECTION 5: Main Window and Application Code
  382.  
  383. //----------------------------------------------------------------------
  384.  
  385. // The main program loop starts here
  386.  
  387. int WINAPI WinMain( HINSTANCE thisprogram, HINSTANCE dummy1, LPSTR dummy2, int startmode)
  388.  
  389. {
  390.  
  391. // Define local variables
  392.  
  393. HWND mainwindow;
  394.  
  395. DWORD style;
  396.  
  397. unsigned int dflt = CW_USEDEFAULT;
  398.  
  399. MSG message;
  400.  
  401. WNDCLASSEX wc;
  402.  
  403. // Define the class for the main window
  404.  
  405. wc.hInstance = thisprogram; // what program this class applies to
  406.  
  407. wc.lpszClassName = "Main"; // the name of this class
  408.  
  409. wc.lpfnWndProc = ProcessMessages; // the message processor
  410.  
  411. wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); // the icon (large)
  412.  
  413. wc.hIconSm = NULL; // the icon (small)
  414.  
  415. wc.hCursor = LoadCursor( NULL, IDC_ARROW ); // the type of cursor
  416.  
  417. wc.hbrBackground = ( HBRUSH )( COLOR_WINDOW ); // background color
  418.  
  419. wc.cbSize = sizeof( WNDCLASSEX );
  420.  
  421. wc.style = 0; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.lpszMenuName = NULL;
  422.  
  423. // Register this "Main" window class with the OS
  424.  
  425. RegisterClassEx( &wc );
  426.  
  427. // Define the main window's style
  428.  
  429. style = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
  430.  
  431. // Create the main window
  432.  
  433. mainwindow = CreateWindowEx(0, "Main", "ENBE 241: Imager", style,
  434.  
  435. dflt, dflt, 400, 370, NULL, NULL, NULL, NULL);
  436.  
  437. // Display the main window and all of its visible children
  438.  
  439. ShowWindow( mainwindow, startmode );
  440.  
  441. // Continuously get and process messages
  442.  
  443. while (GetMessage(&message, NULL, 0, 0) > 0)
  444.  
  445. {
  446.  
  447. TranslateMessage( &message );
  448.  
  449. DispatchMessage( &message );
  450.  
  451. }
  452.  
  453. // That's It!
  454.  
  455. return( 0 );
  456. }

any help would be greatly appreciated! thanks in advance!

Last edited by Cisnotforcookie; May 1st, 2008 at 3:36 PM. Reason: code tags
Cisnotforcookie is offline   Reply With Quote
Old May 1st, 2008, 3:39 PM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Re: image processing

First I would suggest that you learn how to use code tags . Second you might want to take a look at this or this article. Hopefully that clears things up a bit.
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old May 1st, 2008, 3:43 PM   #3
Cisnotforcookie
Newbie
 
Join Date: May 2008
Posts: 6
Rep Power: 0 Cisnotforcookie is on a distinguished road
Re: image processing

i have been reading those two sites for the past hour, and i still am not getting how to write it in C. I was thinking that i could take 3X3 areas of pixels and average those together, then redisplay the averaged value in all of the 9 pixels, how can i go about this?

and i thought i did use the code tags?
Cisnotforcookie is offline   Reply With Quote
Old May 1st, 2008, 3:48 PM   #4
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: image processing

read in each pixel, store each rgb value seperate, add all the red values divide by 9 and do the same with the green and blue. then take the new r g b values.

something along those lines anyway

Chris
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old May 1st, 2008, 3:52 PM   #5
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Re: image processing

Yeah, you managed to edit it before i hit post. Here is an article that is a bit easier to understand
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old May 2nd, 2008, 1:18 PM   #6
Cisnotforcookie
Newbie
 
Join Date: May 2008
Posts: 6
Rep Power: 0 Cisnotforcookie is on a distinguished road
Re: image processing

ok so i decided to not go the mathematical route for blurring/sharpening, something which i know is the dumb BS way out, but i just couldnt get the algorithim to work after an entire night of trying. here is what i have so far:

c Syntax (Toggle Plain Text)
  1. //----------------------------------------------------------------------
  2.  
  3. // SECTION 1: Preprocessor Commands
  4.  
  5. //----------------------------------------------------------------------
  6.  
  7. // Include Win32 API Libraries (headers)
  8.  
  9. #include <windows.h>
  10.  
  11. #include <windowsx.h>
  12.  
  13. #include <commctrl.h>
  14.  
  15. #include <string.h>
  16.  
  17. #include <stdio.h>
  18.  
  19. // Define the maximum number of letters that text control objects can have
  20.  
  21. #define MAXLETTERS 32000
  22.  
  23. #define MaxSize 512
  24.  
  25. // Define event-ID numbers for the control objects
  26.  
  27. #define ID_BTTN 1001
  28.  
  29. #define ID_FNAME 1002
  30.  
  31. #define ID_FOPEN 2001
  32.  
  33. #define ID_FEXIT 2002
  34.  
  35. #define ID_INV 2003
  36.  
  37. #define ID_ROTATE 2004
  38.  
  39. #define ID_BLUR 2005
  40.  
  41. #define ID_FLIP 2006
  42.  
  43. #define ID_Sharp 2007
  44. //----------------------------------------------------------------------
  45.  
  46. // SECTION 2: Computational Code
  47.  
  48. //----------------------------------------------------------------------
  49.  
  50. // Declare global variables: flowdata is available to all subsequent functions
  51.  
  52. unsigned char imgdata[MaxSize][MaxSize];
  53.  
  54. int imgheight, imgwidth;
  55.  
  56. // Function DrawData(): draws data from flowdata array onto screen
  57.  
  58. void DrawData(HWND window)
  59.  
  60. {
  61.  
  62. // Declare local variables
  63.  
  64. int col, row;
  65.  
  66. unsigned char shade;
  67.  
  68. HDC drawing_context = GetDC(window);
  69.  
  70. // Draw the pixels
  71.  
  72. for (row = 0 ; row < imgheight ; row++)
  73.  
  74. for (col = 0 ; col < imgwidth ; col++)
  75.  
  76. {
  77.  
  78. shade = imgdata[row][col];
  79.  
  80. SetPixel(drawing_context, col+50, row+50, RGB(shade, shade, shade));
  81.  
  82. }
  83.  
  84. }
  85.  
  86. // flips data from flowdata array onto screen
  87.  
  88. void Inv(HWND window)
  89.  
  90. {
  91.  
  92. // Declare local variables
  93.  
  94. int col, row;
  95.  
  96. unsigned char shade;
  97.  
  98. HDC drawing_context = GetDC(window);
  99.  
  100. // Draw the pixels
  101.  
  102.  
  103. for (row = 0 ; row < imgheight ; row++)
  104.  
  105. for (col = 0 ; col < imgwidth ; col++)
  106.  
  107. {
  108.  
  109. shade = imgdata[row][col];
  110.  
  111. SetPixel(drawing_context, col+50, 195-row, RGB(shade, shade, shade));
  112.  
  113. }
  114.  
  115. }
  116.  
  117. //Function to blur the image
  118.  
  119. void Blur(HWND window)
  120.  
  121. {
  122.  
  123. // Declare local variables
  124.  
  125. int col, row;
  126. unsigned char shade;
  127. unsigned char shade0;
  128. unsigned char shade1;
  129. unsigned char shade2;
  130. unsigned char shade3;
  131. unsigned char shade4;
  132. unsigned char shade5;
  133. unsigned char shade6;
  134. unsigned char shade7;
  135. unsigned char shadeBlur;
  136. HDC drawing_context = GetDC(window);
  137.  
  138. // Draw the pixels
  139.  
  140.  
  141. for (row = 0 ; row < imgheight ; row++)
  142.  
  143. for (col = 0 ; col < imgwidth ; col++)
  144.  
  145. {
  146. shade0 = imgdata[row-4][col-4];
  147. shade1 = imgdata[row-3][col-3];
  148. shade2 = imgdata[row-2][col-2];
  149. shade3 = imgdata[row-1][col-1];
  150. shade = imgdata[row][col];
  151. shade4 = imgdata[row+1][col+1];
  152. shade5 = imgdata[row+2][col+2];
  153. shade6 = imgdata[row+4][col+3];
  154. shade7 = imgdata[row+4][col+4];
  155. shadeBlur = (shade + shade0 + shade1 + shade2 + shade3 + shade4 + shade5 + shade5 + shade6 + shade7 )/9;
  156. SetPixel(drawing_context, col+50, row+50, RGB(shadeBlur, shadeBlur, shadeBlur));
  157.  
  158. }
  159.  
  160. }//end blur
  161.  
  162. void Sharp(HWND window)
  163.  
  164. {
  165.  
  166. // Declare local variables
  167.  
  168. int col, row;
  169. unsigned char shade;
  170. unsigned char shade0;
  171. unsigned char shade1;
  172. unsigned char shade2;
  173. unsigned char shade3;
  174. unsigned char shade4;
  175. unsigned char shade5;
  176. unsigned char shade6;
  177. unsigned char shade7;
  178. unsigned char shadeSharp;
  179. HDC drawing_context = GetDC(window);
  180.  
  181. // Draw the pixels
  182.  
  183.  
  184. for (row = 0 ; row < imgheight ; row++)
  185.  
  186. for (col = 0 ; col < imgwidth ; col++)
  187.  
  188. {
  189. shade0 = imgdata[row-4][col-4];
  190. shade1 = imgdata[row-3][col-3];
  191. shade2 = imgdata[row-2][col-2];
  192. shade3 = imgdata[row-1][col-1];
  193. shade = imgdata[row][col];
  194. shade4 = imgdata[row+1][col+1];
  195. shade5 = imgdata[row+2][col+2];
  196. shade6 = imgdata[row+4][col+3];
  197. shade7 = imgdata[row+4][col+4];
  198. shadeSharp = (shade + shade0 + shade1 + shade2)*1.3;
  199. SetPixel(drawing_context, col+50, row+50, RGB(shadeSharp, shadeSharp, shadeSharp));
  200.  
  201. }
  202. }
  203. //Function to rotate image 90 degrees
  204.  
  205. void Rotate90Degrees(HWND window)
  206.  
  207. {
  208.  
  209. // Declare local variables
  210.  
  211. int col, row;
  212.  
  213. unsigned char shade;
  214.  
  215. HDC drawing_context = GetDC(window);
  216.  
  217. // Draw the pixels
  218.  
  219. for (row = 0 ; row < imgwidth ; row++)
  220.  
  221. for (col = 0 ; col < imgheight ; col++)
  222.  
  223. {
  224.  
  225. shade = imgdata[col][row];
  226.  
  227. SetPixel(drawing_context, col+50, row-23, RGB(shade, shade, shade));
  228.  
  229. }
  230. }//end Rotate90Degrees
  231.  
  232. // Function LoadData(): loads data from file name in file name box
  233.  
  234. int LoadData(HWND window)
  235.  
  236. {
  237.  
  238. // Declare local variables
  239.  
  240. HWND file_name_box;
  241.  
  242. char filename[MAXLETTERS], imgformat[80];
  243.  
  244. FILE * infile;
  245.  
  246. int row, col, imgdepth;
  247.  
  248. // Get the input file's name from the File Text Box
  249.  
  250. file_name_box = GetDlgItem(window, ID_FNAME); // get file box handle
  251.  
  252. GetWindowText(file_name_box, filename, MAXLETTERS); // get file name from file box
  253.  
  254. // Open the input file, for reading
  255.  
  256. infile = fopen( filename , "r");
  257.  
  258. if (!infile)
  259.  
  260. {
  261.  
  262. SetWindowText(file_name_box, "ERROR!"); // signal if file does not exist
  263.  
  264. return( -1 );
  265.  
  266. }
  267.  
  268. // Read-in image information from the input file
  269.  
  270. fscanf(infile,"%s", imgformat);
  271.  
  272. fscanf(infile,"%d", &imgwidth);
  273.  
  274. fscanf(infile,"%d", &imgheight);
  275.  
  276. fscanf(infile,"%d", &imgdepth);
  277.  
  278. fgetc(infile);
  279.  
  280. // Read-in the image data from the input file
  281.  
  282. for (row = 0 ; row < imgheight ; row++)
  283.  
  284. for (col = 0 ; col < imgwidth ; col++)
  285.  
  286. imgdata[row][col] = fgetc(infile);
  287.  
  288. // close the input file
  289.  
  290. fclose(infile);
  291.  
  292. return(0);
  293.  
  294. }
  295.  
  296. // Function GetFileName(): opens a file window and gets user-selected file name
  297.  
  298. void GetFileName(HWND window)
  299.  
  300. {
  301.  
  302. // Declare local variables
  303.  
  304. OPENFILENAME open_file_dialog;
  305.  
  306. char filename[ MAXLETTERS ] = "";
  307.  
  308. HWND file_name_box;
  309.  
  310. // Initialize the Open File Dialog Data Structure
  311.  
  312. ZeroMemory(&open_file_dialog, sizeof(open_file_dialog)); // is this really needed?
  313.  
  314. open_file_dialog.lStructSize = sizeof(open_file_dialog);
  315.  
  316. open_file_dialog.hwndOwner = window; // name of parent window
  317.  
  318. open_file_dialog.lpstrFile = filename; // name of file name variable
  319.  
  320. open_file_dialog.nMaxFile = MAXLETTERS; // max No of letters in file name
  321.  
  322. open_file_dialog.lpstrFilter = "PGM Files (*.pgm)\0*.pgm\0All Files (*.*)\0*.*\0"; // type
  323.  
  324. open_file_dialog.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST; // style
  325.  
  326. open_file_dialog.lpstrDefExt = "pgm"; // file extension
  327.  
  328. // Open the File dialog, get the file name from the user and process it if non-NULL
  329.  
  330. if ( GetOpenFileName(&open_file_dialog) )
  331.  
  332. {
  333.  
  334. file_name_box = GetDlgItem(window, ID_FNAME); // get file box handle
  335.  
  336. SetWindowText(file_name_box, filename); // put file name in file box
  337.  
  338. }
  339.  
  340. }
  341.  
  342. //----------------------------------------------------------------------
  343.  
  344. // SECTION 3: GUI Building
  345.  
  346. //----------------------------------------------------------------------
  347.  
  348. // Function MakeControls(): builds control objects on a window
  349.  
  350. void MakeControls(HWND window)
  351.  
  352. {
  353.  
  354. // Create the Label for the file name text box
  355.  
  356. CreateWindowEx(WS_EX_STATICEDGE, "Static", "File Name:",
  357.  
  358. WS_CHILD | WS_VISIBLE,
  359.  
  360. 10, 260, 50, 50, window, NULL, NULL, NULL);
  361.  
  362. // Create the File Name Text box