/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(Offset)------------------------------------------------------------- LEAD Functions Used. L_FileInfo L_LoadFile L_SaveFileOffset L_FillBitmap L_LoadFileOffset L_CreatePaintPalette L_PaintDC L_FreeBitmap We have made the assumption that the user has the knowledge of programing in C and Windows. This example will: 1. Load an image from a file (name passed as a command line parameter), 2. display the image, 3. save the image to a file, (name passed as a command line parameter), prepending a false header, 4. fill the bitmap with a color and display it, 5. load the image from the "offset" file, 6. display the new image. Usage: OFFSET --------------------------------------------------------------------------*/ #include /* Required for all Windows applications. */ #include /* Needed for message cracker. */ #include #include #include "tchar.h" #include "..\\..\\..\\include\\l_bitmap.h" /* LEADTOOLS main header file. */ #include "..\\..\\..\\include\\l_error.h" /* LEADTOOLS error definition header file. */ #include "Offset.h" /* Application specific header file. */ L_BOOL ExtractCommandData ( ) ; /*---[WinMain]--------------------------------------------------------------- Syntax: int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) Parameters: hInstance Current instance. hPrevInstance previous instance. lpCmdLine command line. nCmdShow show-window type. Prototype: Windows.h Notes: Windows main function, calls initialization function and processes message loop. --------------------------------------------------------------------------*/ int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; /* Message structure, See Windows SDK for more information. */ UNLOCKSUPPORT(); if (!hPrevInstance) /* Other instances of app running? */ if (!InitApplication (hInstance)) /* Initialize shared things */ return (FALSE); /* Exits if unable to initialize */ if (!ExtractCommandData ()) { return FALSE; } if (!InitInstance (hInstance, nCmdShow)) /* Perform instance initializations */ return (FALSE); /* Acquire and dispatch messages until a WM_QUIT message is received. */ while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); /* Translates virtual key codes */ DispatchMessage (&msg); /* Dispatches message to window */ } return (msg.wParam); /* Returns the value from PostQuitMessage */ } /*---[InitApplication]------------------------------------------------------ Syntax: L_BOOL InitApplication(HANDLE hInstance) Parameters: hInstance Current instance. ProtoType: Offset.h Notes: Initializes window class structure and registers window class. --------------------------------------------------------------------------*/ L_BOOL InitApplication (HANDLE hInstance) { WNDCLASS wcWindowClass; wcWindowClass.style = 0; /* Class style(s). */ wcWindowClass.lpfnWndProc = MainWndProc; /* Function to retrieve messages */ /* for windows of this class. */ wcWindowClass.cbClsExtra = 0;/* No per-class extra data. */ wcWindowClass.cbWndExtra = 0;/* No per-window extra data. */ wcWindowClass.hInstance = hInstance; /* Owner Application. */ wcWindowClass.hIcon = LoadIcon (hInstance, TEXT("LEAD")); wcWindowClass.hCursor = LoadCursor (NULL, IDC_ARROW); wcWindowClass.hbrBackground = GetStockObject (WHITE_BRUSH); wcWindowClass.lpszMenuName = NULL; /* No menu */ wcWindowClass.lpszClassName = TEXT("LEADWClass"); return (RegisterClass (&wcWindowClass)); /* Register the window class and */ /* return the result code. */ } /*---[InitInstance]---------------------------------------------------------- Syntax: L_BOOL InitInstance( HANDLE hInstance, L_INT nCmdShow ) Parameters: hInstance Current instance. nCmdShow Parameter for first ShowWindow() call. ProtoType: Offset.h Notes: Saves instance handle and creates main window. --------------------------------------------------------------------------*/ L_BOOL InitInstance (HANDLE hInstance, L_INT nCmdShow) { HWND hWnd; L_INT nRet; L_TCHAR buf[1024]; /* Save the instance handle. */ hInst = hInstance; fFirst = TRUE; /* Create a main window for this application instance. */ Data.FileInfo.uStructSize = sizeof(FILEINFO); if ((nRet = L_FileInfo (Data.szFilenameIn, &Data.FileInfo, sizeof(FILEINFO), 0, NULL)) != SUCCESS) { wsprintf (buf, TEXT("Error %d from L_FileInfo on file %s."), nRet, (LPTSTR) Data.szFilenameIn); MessageBox (NULL, buf, TEXT("ERROR - ABORTING"), MB_OK); return (FALSE); } rWndSize.top = 0; rWndSize.left = 0; // rWndSize.bottom = Data.FileInfo.Height; // rWndSize.right = Data.FileInfo.Width; rWndSize.bottom = INFOHEIGHT(&Data.FileInfo); rWndSize.right = INFOWIDTH(&Data.FileInfo); AdjustWindowRect (&rWndSize, WS_OVERLAPPEDWINDOW, FALSE); hWnd = CreateWindow ( TEXT("LEADWClass"), TEXT("LEADTOOLS Sample Application"), /* Window title */ WS_OVERLAPPEDWINDOW, /* Window style. */ CW_USEDEFAULT, /* Default horizontal position. */ CW_USEDEFAULT, /* Default vertical position. */ rWndSize.right - rWndSize.left, /* Window width. */ rWndSize.bottom - rWndSize.top, /* Window height. */ NULL, /* Overlapped windows have no parent. */ NULL, /* Use the window class menu. */ hInstance, /* This instance owns this window. */ NULL /* Pointer not needed. */ ); if (hWnd == NULL) return (FALSE); /* If window could not be created, return "failure" */ ShowWindow (hWnd, nCmdShow); /* Show the window. */ UpdateWindow (hWnd); /* Sends WM_PAINT message. */ return (TRUE); } /*---[MainWndProc]----------------------------------------------------------- Syntax: L_INT32 EXT_FUNCTION MainWndProc( HWND hWnd, L_UINT message, WPARAM wParam, LPARAM lParam ) Parameters: hWnd Window handle. message Type of message. wParam Additional information. lParam Additional information. ProtoType: Offset.h Notes: This procedure is responsible for handling window messages. --------------------------------------------------------------------------*/ L_INT32 EXT_FUNCTION MainWndProc (HWND hWnd, L_UINT Message, WPARAM wParam, LPARAM lParam) { switch (Message) { HANDLE_MSG (hWnd, WM_COMMAND, Window_OnCommand); HANDLE_MSG (hWnd, WM_PALETTECHANGED, Window_OnPaletteChanged); HANDLE_MSG (hWnd, WM_QUERYNEWPALETTE, Window_OnQueryNewPalette); HANDLE_MSG (hWnd, WM_PALETTEISCHANGING, Window_OnPaletteChanging); HANDLE_MSG (hWnd, WM_SYSCOLORCHANGE, Window_SysColorChange); HANDLE_MSG (hWnd, WM_ACTIVATE, Window_OnActivate); HANDLE_MSG (hWnd, WM_PAINT, Window_OnPaint); HANDLE_MSG (hWnd, WM_DESTROY, Window_OnDestroy); } return DefWindowProc (hWnd, Message, wParam, lParam); } /*---[Window_OnCommand]----------------------------------------------------- Syntax: VOID Window_OnCommand( HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify ) Parameters: hWnd Window handle. id Menu item or Control ID. hwndCtl 0 if menu item selected, else window handle of the control. codeNotify 1 if accelerator keystroke, else notification code, such as BN_CLICKED. Prototype: Color.h Notes: This procedure is responsible for handling WM_ONCOMMAND. --------------------------------------------------------------------------*/ VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify) { L_INT nRet; L_TCHAR buf[1024]; /* Buffer to hold the error message. */ UNREFERENCED_PARAMETER (hwndCtl); UNREFERENCED_PARAMETER (codeNotify); switch (id) { case ORIG_LOAD: nRet = L_LoadFile (Data.szFilenameIn, &Data.BitmapHandle, sizeof(BITMAPHANDLE), 0, ORDER_BGR, LOADFILE_ALLOCATE | LOADFILE_STORE, NULL, NULL, NULL, NULL); if (nRet != SUCCESS) { wsprintf (buf, TEXT("Error %d loading %s"), nRet, (LPTSTR) Data.szFilenameIn); MessageBox (hWnd, buf, TEXT("Error"), MB_OK); nRet = FALSE; } else { FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage); InvalidateRect (hWnd, NULL, TRUE); UpdateWindow (hWnd); FORWARD_WM_COMMAND (hWnd, SAVE_FILE, 0, 0, PostMessage); } break; case SAVE_FILE: #ifdef UNICODE Data.fd = _wcreat ( Data.szFilenameOut, _S_IREAD | _S_IWRITE ); #else Data.fd = _lcreat ( Data.szFilenameOut, _S_IREAD | _S_IWRITE ); #endif if (Data.fd == -1) { MessageBox (NULL, TEXT("Error Opening File!"), TEXT("Error"), MB_OK); return; } _lwrite (Data.fd, "Offset is at 15", 15); nRet = L_SaveFileOffset (Data.fd, 15, &Data.nSizeFile, &Data.BitmapHandle, FILE_LEAD, 24, PQ1, 0, NULL, NULL, NULL); _lclose (Data.fd); if (nRet == SUCCESS) { MessageBox (hWnd, TEXT("Image written to file with a false header."), TEXT("SUCCESS"), MB_OK); L_FillBitmap (&Data.BitmapHandle, RGB (200, 0, 255)); InvalidateRect (hWnd, NULL, TRUE); UpdateWindow (hWnd); FORWARD_WM_COMMAND (hWnd, OFFSET_LOAD, 0, 0, PostMessage); } break; case OFFSET_LOAD: #ifdef UNICODE Data.fd = _wopen (Data.szFilenameOut, OF_READ | OF_SHARE_DENY_WRITE); #else Data.fd = _lopen (( LPCSTR ) Data.szFilenameOut, OF_READ | OF_SHARE_DENY_WRITE); #endif if (Data.fd == -1) { MessageBox (hWnd, TEXT("Error Opening File!"), TEXT("Error"), MB_OK); return; } /* Load the bitmap as the file's bits per pixel */ nRet = L_LoadFileOffset (Data.fd, 15, Data.nSizeFile, &Data.BitmapHandle, sizeof(BITMAPHANDLE), 0, ORDER_BGRORGRAY, LOADFILE_ALLOCATE | LOADFILE_STORE, NULL, NULL, NULL, NULL); _lclose (Data.fd); if (nRet != SUCCESS) { wsprintf (buf, TEXT("Error %d loading %s"), nRet, (LPTSTR) Data.szFilenameOut); MessageBox (hWnd, buf, TEXT("Error"), MB_OK); SetWindowText (hWnd, TEXT("Example Is Complete")); } else { FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage); InvalidateRect (hWnd, NULL, TRUE); SetWindowText (hWnd, TEXT("Offset File Loaded, Example Is Complete")); } break; } return; } /*---[Window_OnActivate]----------------------------------------------------- Syntax: VOID Window_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized) Parameters: hWnd Window handle. state WA_ACTIVE | WA_CLICKACTIVE | WA_INACTIVE hwndActDeact the winodw habdle that deactivate fMinimized Window is minimized Prototype: Color.h Notes: This procedure is responsible for handling WM_ONCOMMAND. --------------------------------------------------------------------------*/ void Window_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized) { if(state!=WA_INACTIVE) Window_OnQueryNewPalette (hwnd); } /*====(Window_OnPaletteChanging)========================================== Description: Enumerates all child windows and asks them to realize their logical palettes beside the physical palette. Syntax : VOID Window_OnPaletteChanging(HWND hwnd, HWND hWndPaletteChange) Parameters : hwnd Handle to a window. hWndPaletteChange Handle to a window that has the palette realized. Return Value: None. ==========================================================================*/ VOID Window_OnPaletteChanging(HWND hWnd, HWND hWndPaletteChange) { Window_OnPaletteChanged (hWnd, hWndPaletteChange); } /*====(Window_SysColorChange)========================================== Syntax: VOID Window_SysColorChange( HWND hWnd ) Parameters: hWnd Handle to a window. Prototype: Loadsave.h Notes: This procedure is responsible for handling WM_SYSCOLORCHANGE. --------------------------------------------------------------------------*/ VOID Window_SysColorChange(HWND hwnd) { Window_OnQueryNewPalette (hwnd); } /*----(Window_OnPaletteChanged)-------------------------------------------- Syntax: VOID Window_OnPaletteChanged( HWND hWnd, HWND hWndPaletteChange ) Parameters: hwnd Handle to a window. hWndPaletteChange Handle to a window that has the palette realized. Prototype: Offset.h Notes: This procedure is responsible for handling WM_PALETTECHANGED. --------------------------------------------------------------------------*/ VOID Window_OnPaletteChanged (HWND hWnd, HWND hWndPaletteChange) { HDC hDC; HPALETTE hPalette; /* If this window initiated the palette change, do nothing. */ if (hWnd == hWndPaletteChange) return; /* Delete the previous palette, if there is one. */ if (Data.hPalette) { DeleteObject (Data.hPalette); Data.hPalette = NULL; } /* Does this window have a bitmap and a palette? */ if (Data.BitmapHandle.Flags.Allocated ) { hDC = GetDC (hWnd); /* Generate a new logical palette (if needed) for painting. */ Data.hPalette = L_CreatePaintPalette (hDC, &Data.BitmapHandle); /* Select and Realize the palette. */ hPalette = SelectPalette (hDC, Data.hPalette, TRUE); RealizePalette (hDC); /* Force a repaint. */ InvalidateRect (hWnd, NULL, FALSE); /* Return the old palette. */ SelectPalette (hDC, hPalette, TRUE); ReleaseDC (hWnd, hDC); } return; } /*----(Window_OnQueryNewPalette)-------------------------------------------- Syntax: BOOL Window_OnQueryNewPalette( HWND hWnd ) Parameters: hWnd Handle to a window. Prototype: Offset.h Notes: This procedure is responsible for handling WM_QUERYNEWPALETTE. --------------------------------------------------------------------------*/ BOOL Window_OnQueryNewPalette (HWND hWnd) { HDC hDC; HPALETTE hPalette; L_INT nNoColors = 0; /* Delete the previous palette, if there is one. */ if (Data.hPalette) { DeleteObject (Data.hPalette); Data.hPalette = NULL; } if (Data.BitmapHandle.Flags.Allocated) { hDC = GetDC (hWnd); /* Generate a new logical palette (if needed) for painting. */ Data.hPalette = L_CreatePaintPalette (hDC, &Data.BitmapHandle); if (Data.hPalette) /* Is a palette needed? */ { hPalette = SelectPalette (hDC, Data.hPalette, FALSE); nNoColors = RealizePalette (hDC); if (nNoColors) /* If the palette changed, force a WM_PAINT */ InvalidateRect (hWnd, NULL, FALSE); /* Restore the old palette. */ SelectPalette (hDC, hPalette, TRUE); } ReleaseDC (hWnd, hDC); } return (nNoColors); } /*---[Window_OnPaint]------------------------------------------------------- Syntax: VOID Window_OnPaint( HWND hWnd ); Parameters: hWnd Window handle. Prototype: Offset.h Notes: This procedure is responsible for handling WM_PAINT. --------------------------------------------------------------------------*/ VOID Window_OnPaint (HWND hWnd) { HDC hdc; RECT rc; PAINTSTRUCT ps; HPALETTE hOldPal = NULL; hdc = BeginPaint (hWnd, &ps);/* Get DC */ if (Data.BitmapHandle.Flags.Allocated) /* Do we have an image? */ { if (Data.hPalette) /* Is there a palette that needs to be selected? */ hOldPal = SelectPalette (hdc, Data.hPalette, TRUE); /* Select it. */ /* Setup the Destination Rectangle. */ // SetRect (&rc, 0, 0, Data.BitmapHandle.Width, Data.BitmapHandle.Height); SetRect (&rc, 0, 0, BITMAPWIDTH(&Data.BitmapHandle), BITMAPHEIGHT(&Data.BitmapHandle)); /* Paint it */ L_PaintDC (hdc, &Data.BitmapHandle, NULL, NULL, &rc, &ps.rcPaint, SRCCOPY); if (Data.hPalette) /* Return old palette if there is one. */ SelectPalette (hdc, hOldPal, TRUE); } EndPaint (hWnd, &ps); /* Return DC */ if (fFirst) { fFirst = FALSE; FORWARD_WM_COMMAND (hWnd, ORIG_LOAD, 0, 0, PostMessage); } return; } /*---[Window_OnDestroy]---------------------------------------------------- Syntax: VOID Window_OnDestroy( HWND hWnd ); Parameters: hWnd Window handle. ProtoType: Offset.h Notes: This procedure is responsible for handling WM_DESTROY. --------------------------------------------------------------------------*/ VOID Window_OnDestroy (HWND hWnd) { UNREFERENCED_PARAMETER (hWnd); if (Data.BitmapHandle.Flags.Allocated) /* Do we have an image? */ L_FreeBitmap (&Data.BitmapHandle); if (Data.hPalette) /* Delete palette if there is one. */ DeleteObject (Data.hPalette); PostQuitMessage (0); /* Post WM_QUIT, to end the application */ } /*---[ExtractCommandData]---------------------------------------------------- Syntax: L_BOOL ExtractCommandData( ); Parameters: None. ProtoType: Offset.c Notes: - This procedure is responsible for extracting the In and Out file names passed through the command line. - You should call this function only in WinMain function. - Consider that between the two images name should be only one space. --------------------------------------------------------------------------*/ L_BOOL ExtractCommandData ( ) { LPTSTR pszFirst = NULL; LPTSTR pszSecond = NULL; LPTSTR pszCmdLine = NULL; LPTSTR psz = NULL; L_INT nFirstPos = 0 ; L_INT nSecondPos = 0 ; L_INT nStringLen = 0 ; pszCmdLine = GetCommandLine(); nStringLen = lstrlen ( pszCmdLine ) ; // To specify that it is command line or not. if(( pszCmdLine[1] == ':' ) || // Shortcut case ( pszCmdLine[2] == ':' )) //VS case { // 1- TRY TO EXPOSE THE EXE NAME FROM THE COMMAND LINE psz = _tcschr ( pszCmdLine, ':' ) ; if ( NULL == psz ) { return FALSE ; } } else { psz = pszCmdLine ; } pszFirst = _tcschr ( psz + 1, ':' ) ; if ( NULL == pszFirst ) { return FALSE ; } // Calc the char number to the image file name. nFirstPos = pszFirst - pszCmdLine - 1 ; if ( 0 > nFirstPos ) { return FALSE ; } // 2- TRY TO EXPOSE THE EXE NAME AND THE FIRST FILE NAME FROM THE COMMAND LINE psz = pszFirst ; pszSecond = _tcschr ( psz + 1, ':' ) ; if ( NULL == pszSecond ) { return FALSE ; } // Calc the char number to the image file name. nSecondPos = pszSecond - pszCmdLine - 1 ; if ( 0 > nSecondPos ) { return FALSE ; } memset (Data.szFilenameIn, 0, sizeof(Data.szFilenameIn)); memset (Data.szFilenameOut, 0, sizeof(Data.szFilenameOut)); lstrcpyn ( Data.szFilenameIn, pszFirst - 1, abs( ( nStringLen - nFirstPos ) - ( nStringLen - nSecondPos ) ) ) ; lstrcpyn ( Data.szFilenameOut, pszSecond - 1, ( nStringLen - ( nStringLen - nSecondPos ) ) ) ; return TRUE; }