|
Newbie
Join Date: May 2008
Posts: 6
Rep Power: 0 
|
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:
//---------------------------------------------------------------------- // SECTION 1: Preprocessor Commands //---------------------------------------------------------------------- // Include Win32 API Libraries (headers) #include <windows.h> #include <windowsx.h> #include <commctrl.h> #include <string.h> #include <stdio.h> #include <math.h> // Define the maximum number of letters that text control objects can have #define MAXLETTERS 32000 #define MaxSize 512 // Define event-ID numbers for the control objects #define ID_BTTN 1001 #define ID_FNAME 1002 #define ID_FOPEN 2001 #define ID_FEXIT 2002 #define ID_INV 2003 //---------------------------------------------------------------------- // SECTION 2: Computational Code //---------------------------------------------------------------------- // Declare global variables: flowdata is available to all subsequent functions unsigned char imgdata[MaxSize][MaxSize]; int imgheight, imgwidth; // Function DrawData(): draws data from flowdata array onto screen void DrawData(HWND window) { // Declare local variables int col, row; unsigned char shade; HDC drawing_context = GetDC(window); // Draw the pixels for (row = 0 ; row < imgheight ; row++) for (col = 0 ; col < imgwidth ; col++) { shade = imgdata[row][col]; SetPixel(drawing_context, col+50, row+50, RGB(shade, shade, shade)); } } // flips data from flowdata array onto screen void Inv(HWND window) { // Declare local variables int col, row; unsigned char shade; HDC drawing_context = GetDC(window); // Draw the pixels for (row = 0 ; row < imgheight ; row++) for (col = 0 ; col < imgwidth ; col++) { shade = imgdata[row][col]; SetPixel(drawing_context, col+50, 195-row, RGB(shade, shade, shade)); } } // Function LoadData(): loads data from file name in file name box int LoadData(HWND window) { // Declare local variables HWND file_name_box; char filename[MAXLETTERS], imgformat[80]; FILE * infile; int row, col, imgdepth; // Get the input file's name from the File Text Box file_name_box = GetDlgItem(window, ID_FNAME); // get file box handle GetWindowText(file_name_box, filename, MAXLETTERS); // get file name from file box // Open the input file, for reading infile = fopen( filename , "r"); if (!infile) { SetWindowText(file_name_box, "ERROR!"); // signal if file does not exist return( -1 ); } // Read-in image information from the input file fscanf(infile,"%s", imgformat); fscanf(infile,"%d", &imgwidth); fscanf(infile,"%d", &imgheight); fscanf(infile,"%d", &imgdepth); fgetc(infile); // Read-in the image data from the input file for (row = 0 ; row < imgheight ; row++) for (col = 0 ; col < imgwidth ; col++) imgdata[row][col] = fgetc(infile); // close the input file fclose(infile); return(0); } // Function GetFileName(): opens a file window and gets user-selected file name void GetFileName(HWND window) { // Declare local variables OPENFILENAME open_file_dialog; char filename[ MAXLETTERS ] = ""; HWND file_name_box; // Initialize the Open File Dialog Data Structure ZeroMemory(&open_file_dialog, sizeof(open_file_dialog)); // is this really needed? open_file_dialog.lStructSize = sizeof(open_file_dialog); open_file_dialog.hwndOwner = window; // name of parent window open_file_dialog.lpstrFile = filename; // name of file name variable open_file_dialog.nMaxFile = MAXLETTERS; // max No of letters in file name open_file_dialog.lpstrFilter = "PGM Files (*.pgm)\0*.pgm\0All Files (*.*)\0*.*\0"; // type open_file_dialog.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST; // style open_file_dialog.lpstrDefExt = "pgm"; // file extension // Open the File dialog, get the file name from the user and process it if non-NULL if ( GetOpenFileName(&open_file_dialog) ) { file_name_box = GetDlgItem(window, ID_FNAME); // get file box handle SetWindowText(file_name_box, filename); // put file name in file box } } //---------------------------------------------------------------------- // SECTION 3: GUI Building //---------------------------------------------------------------------- // Function MakeControls(): builds control objects on a window void MakeControls(HWND window) { // Create the Label for the file name text box CreateWindowEx(WS_EX_STATICEDGE, "Static", "File Name:", WS_CHILD | WS_VISIBLE, 10, 260, 50, 50, window, NULL, NULL, NULL); // Create the File Name Text box CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", NULL, WS_CHILD | WS_VISIBLE, 70, 260, 250, 50, window, (HMENU) ID_FNAME, NULL, NULL); // Create the Draw button CreateWindowEx(0, "Button", "Draw", WS_CHILD | WS_VISIBLE, 330, 260, 60, 50, window, (HMENU) ID_BTTN, NULL, NULL); // Create the Draw button CreateWindowEx(0, "Button", "Flip", WS_CHILD | WS_VISIBLE, 330, 205, 60, 50, window, (HMENU) ID_INV, NULL, NULL); } // Function MakeMenu(): builds menu objects on a window void MakeMenu(HWND window) { // Declare local variables HMENU filemenu, subfilemenu; // Create a blank file menu filemenu = CreateMenu(); // Create a blank sub-menu (popup menu) for the file menu subfilemenu = CreatePopupMenu(); // Place Open and Exit items onto the blank popup sub-menu AppendMenu(subfilemenu, MF_STRING, ID_FOPEN, "&Open"); AppendMenu(subfilemenu, MF_STRING, ID_FEXIT, "E&xit"); // Place the popup sub-menu on the file menu AppendMenu(filemenu, MF_STRING | MF_POPUP, (UINT)subfilemenu, "&File"); // Place the file menu on the Main Window of the program SetMenu(window, filemenu); } //---------------------------------------------------------------------- // SECTION 4: Event Dispatcher //---------------------------------------------------------------------- // Function ProcessMessages(): Specifies how to respond to user interactions LRESULT CALLBACK ProcessMessages(HWND window, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_CREATE: // what to do when the window is created MakeControls( window ); MakeMenu( window ); break; case WM_DESTROY: // what to do when the window is X-ed PostQuitMessage(0); break; case WM_COMMAND: // what to do if a control was activated switch(LOWORD( wParam)) { case ID_FEXIT: // control was Exit submenu? PostMessage(window, WM_CLOSE, 0, 0); break; case ID_FOPEN: // control was Open submenu? GetFileName( window ); break; case ID_BTTN: // control was Draw button? if ( !LoadData(window) ) DrawData( window ); break; case ID_INV: // control was Flip button? if ( !LoadData(window) ) Inv( window ); break; } default: // what to do otherwise ( default ) return DefWindowProc( window, message, wParam, lParam); } return( 0 ); } //---------------------------------------------------------------------- // SECTION 5: Main Window and Application Code //---------------------------------------------------------------------- // The main program loop starts here int WINAPI WinMain( HINSTANCE thisprogram, HINSTANCE dummy1, LPSTR dummy2, int startmode) { // Define local variables HWND mainwindow; DWORD style; unsigned int dflt = CW_USEDEFAULT; MSG message; WNDCLASSEX wc; // Define the class for the main window wc.hInstance = thisprogram; // what program this class applies to wc.lpszClassName = "Main"; // the name of this class wc.lpfnWndProc = ProcessMessages; // the message processor wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); // the icon (large) wc.hIconSm = NULL; // the icon (small) wc.hCursor = LoadCursor( NULL, IDC_ARROW ); // the type of cursor wc.hbrBackground = ( HBRUSH )( COLOR_WINDOW ); // background color wc.cbSize = sizeof( WNDCLASSEX ); wc.style = 0; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.lpszMenuName = NULL; // Register this "Main" window class with the OS RegisterClassEx( &wc ); // Define the main window's style style = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN; // Create the main window mainwindow = CreateWindowEx(0, "Main", "ENBE 241: Imager", style, dflt, dflt, 400, 370, NULL, NULL, NULL, NULL); // Display the main window and all of its visible children ShowWindow( mainwindow, startmode ); // Continuously get and process messages while (GetMessage(&message, NULL, 0, 0) > 0) { TranslateMessage( &message ); DispatchMessage( &message ); } // That's It! return( 0 ); }
any help would be greatly appreciated! thanks in advance! 
Last edited by Cisnotforcookie; May 1st, 2008 at 4:36 PM.
Reason: code tags
|