View Single Post
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
  363.  
  364. CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", NULL,
  365.  
  366. WS_CHILD | WS_VISIBLE,
  367.  
  368. 70, 260, 250, 50, window, (HMENU) ID_FNAME, NULL, NULL);
  369.  
  370. // Create the Draw button
  371.  
  372. CreateWindowEx(0, "Button", "Draw", WS_CHILD | WS_VISIBLE,
  373.  
  374. 330, 260, 60, 50, window, (HMENU) ID_BTTN, NULL, NULL);
  375.  
  376. }
  377.  
  378. // Function MakeMenu(): builds menu objects on a window
  379.  
  380. void MakeMenu(HWND window)
  381.  
  382. {
  383.  
  384. // Declare local variables
  385.  
  386. HMENU filemenu, subfilemenu;
  387. HMENU imagemenu, subimagemenu;
  388.  
  389. // Create a blank file menu
  390.  
  391. filemenu = CreateMenu();
  392. imagemenu = CreateMenu();
  393.  
  394. // Create a blank sub-menu (popup menu) for the file menu
  395.  
  396. subfilemenu = CreatePopupMenu();
  397. subimagemenu = CreatePopupMenu();
  398.  
  399. // Place Open and Exit items onto the blank popup sub-menu
  400.  
  401. AppendMenu(subfilemenu, MF_STRING, ID_FOPEN, "&Open");
  402.  
  403. AppendMenu(subfilemenu, MF_STRING, ID_ROTATE, "&Rotate");
  404.  
  405. AppendMenu(subfilemenu, MF_STRING, ID_BLUR, "&Blur");
  406.  
  407. AppendMenu(subfilemenu, MF_STRING, ID_FLIP, "&Flip");
  408.  
  409. AppendMenu(subfilemenu, MF_STRING, ID_FEXIT, "&Exit");
  410.  
  411. AppendMenu(subfilemenu, MF_STRING, ID_Sharp, "&Sharpen");
  412.  
  413.  
  414. // Place the popup sub-menu on the file menu
  415.  
  416. AppendMenu(filemenu, MF_STRING | MF_POPUP, (UINT)subfilemenu, "&File");
  417. AppendMenu(imagemenu, MF_STRING | MF_POPUP, (UINT)subimagemenu, "&Image");
  418. // Place the file menu on the Main Window of the program
  419.  
  420. SetMenu(window, filemenu);
  421. //InsertMenuItem(window, 0,FALSE, imagemenu);
  422. //http://msdn.microsoft.com/en-us/library/ms647616(VS.85).aspx
  423. }
  424.  
  425.  
  426. //----------------------------------------------------------------------
  427.  
  428. // SECTION 4: Event Dispatcher
  429.  
  430. //----------------------------------------------------------------------
  431.  
  432. // Function ProcessMessages(): Specifies how to respond to user interactions
  433.  
  434. LRESULT CALLBACK ProcessMessages(HWND window, UINT message,
  435.  
  436. WPARAM wParam, LPARAM lParam)
  437.  
  438. {
  439.  
  440. switch(message)
  441.  
  442. {
  443.  
  444. case WM_CREATE: // what to do when the window is created
  445.  
  446. MakeControls( window );
  447.  
  448. MakeMenu( window );
  449.  
  450. break;
  451.  
  452. case WM_DESTROY: // what to do when the window is X-ed
  453.  
  454. PostQuitMessage(0);
  455.  
  456. break;
  457.  
  458. case WM_COMMAND: // what to do if a control was activated
  459.  
  460. switch(LOWORD( wParam))
  461.  
  462. {
  463.  
  464. case ID_FEXIT: // control was Exit submenu?
  465.  
  466. PostMessage(window, WM_CLOSE, 0, 0);
  467.  
  468. break;
  469.  
  470. case ID_FOPEN: // control was Open submenu?
  471.  
  472. GetFileName( window );
  473.  
  474. break;
  475.  
  476. case ID_BTTN: // control was Draw button?
  477.  
  478. if ( !LoadData(window) )
  479.  
  480. DrawData( window );
  481.  
  482. break;
  483.  
  484. case ID_INV: // control was Flip button?
  485.  
  486. if ( !LoadData(window) )
  487.  
  488. Inv( window );
  489.  
  490. break;
  491.  
  492. case ID_BLUR: //Control Blur submenu
  493. Blur( window );
  494.  
  495. break;
  496.  
  497. case ID_ROTATE: //Control Rotate submenu
  498.  
  499. Rotate90Degrees(window);
  500.  
  501. break;
  502.  
  503. case ID_FLIP: //Control Flip submenu
  504.  
  505. Inv(window);
  506.  
  507. break;
  508.  
  509. case ID_Sharp:
  510. Sharp(window);
  511. break;
  512.  
  513. }
  514.  
  515. default: // what to do otherwise ( default )
  516.  
  517. return DefWindowProc( window, message, wParam, lParam);
  518.  
  519. }
  520.  
  521. return( 0 );
  522.  
  523. }
  524.  
  525. //----------------------------------------------------------------------
  526.  
  527. // SECTION 5: Main Window and Application Code
  528.  
  529. //----------------------------------------------------------------------
  530.  
  531. // The main program loop starts here
  532.  
  533. int WINAPI WinMain( HINSTANCE thisprogram, HINSTANCE dummy1, LPSTR dummy2, int startmode)
  534.  
  535. {
  536.  
  537. // Define local variables
  538.  
  539. HWND mainwindow;
  540.  
  541. DWORD style;
  542.  
  543. unsigned int dflt = CW_USEDEFAULT;
  544.  
  545. MSG message;
  546.  
  547. WNDCLASSEX wc;
  548.  
  549. // Define the class for the main window
  550.  
  551. wc.hInstance = thisprogram; // what program this class applies to
  552.  
  553. wc.lpszClassName = "Main"; // the name of this class
  554.  
  555. wc.lpfnWndProc = ProcessMessages; // the message processor
  556.  
  557. wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); // the icon (large)
  558.  
  559. wc.hIconSm = NULL; // the icon (small)
  560.  
  561. wc.hCursor = LoadCursor( NULL, IDC_ARROW ); // the type of cursor
  562.  
  563. wc.hbrBackground = ( HBRUSH )( COLOR_WINDOW ); // background color
  564.  
  565. wc.cbSize = sizeof( WNDCLASSEX );
  566.  
  567. wc.style = 0; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.lpszMenuName = NULL;
  568.  
  569. // Register this "Main" window class with the OS
  570.  
  571. RegisterClassEx( &wc );
  572.  
  573. // Define the main window's style
  574.  
  575. style = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
  576.  
  577. // Create the main window
  578.  
  579. mainwindow = CreateWindowEx(0, "Main", "ENBE 241: Imager", style,
  580.  
  581. dflt, dflt, 400, 370, NULL, NULL, NULL, NULL);
  582.  
  583. // Display the main window and all of its visible children
  584.  
  585. ShowWindow( mainwindow, startmode );
  586.  
  587. // Continuously get and process messages
  588.  
  589. while (GetMessage(&message, NULL, 0, 0) > 0)
  590.  
  591. {
  592.  
  593. TranslateMessage( &message );
  594.  
  595. DispatchMessage( &message );
  596.  
  597. }
  598.  
  599. // That's It!
  600.  
  601. return( 0 );
  602. }

I realize that the sharpening isnt as good as it should be, but i couldnt think of any other way to do it. I still need to write parts for thresholding and coarsening the image, which i would really appreciate help with. thanks
Cisnotforcookie is offline   Reply With Quote