/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(MemoryCB)------------------------------------------------------------ LEAD Functions Used. L_SetDisplayMode L_FileInfoMemory L_InitBitmap L_LoadMemory L_CreatePaintPalette L_PaintDC L_FreeBitmap L_PaintDCBuffer L_AccessBitmap L_PutBitmapRow L_ReleaseBitmap We have made the assumption that the user has the knowledge of programing in C and Windows. This example will: 1. load an image file (name passed as a command line parameter) into allocated memory, 2. load the image from memory to a bitmap with L_LoadMemory and a Callback function, painting while loading, 3. display the loaded image in a window. Usage: MEMORYCB --------------------------------------------------------------------------*/ #include /* Required for all Windows applications. */ #include /* Needed for message cracker. */ #include /* Standard C library for _fmemcpy. */ #include #include "TCHAR.H" #include #include #include #include "..\\..\\..\\include\\l_bitmap.h" /* LEADTOOLS main header file. */ #include "..\\..\\..\\include\\l_error.h" /* LEADTOOLS error definition header file. */ #include "MemoryCB.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: MemoryCB.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"); /* Name. */ /* Register the window class and return the result code. */ return (RegisterClass (&wcWindowClass)); } /*---[InitInstance]---------------------------------------------------------- Syntax: L_BOOL InitInstance( HANDLE hInstance, L_INT nCmdShow ) Parameters: hInstance Current instance. nCmdShow Parameter for first ShowWindow() call. Prototype: MemoryCB.h Notes: Saves instance handle and creates main window. --------------------------------------------------------------------------*/ L_BOOL InitInstance (HANDLE hInstance, L_INT nCmdShow) { HWND hWnd; hInst = hInstance; /* Set Ordered Dithering for the Display */ L_SetDisplayMode (DISPLAYMODE_ORDEREDDITHER, DISPLAYMODE_ORDEREDDITHER); hWnd = CreateWindow ( TEXT("LEADWClass"), TEXT("LEADTOOLS Sample Application"), /* Window title */ WS_OVERLAPPEDWINDOW, /* Window style. */ CW_USEDEFAULT, /* Default horizontal position. */ CW_USEDEFAULT, /* Default vertical position. */ CW_USEDEFAULT, /* Window width. */ CW_USEDEFAULT, /* 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: MemoryCB.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_CREATE, Window_OnCreate); 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_OnCreate]----------------------------------------------------- Syntax: BOOL Window_OnCreate(HWND hWnd, CREATESTRUCT FAR* lpCreateStruct); Parameters: hWnd Window handle. lpCreateStruct Windows Create Structure. Prototype: MemoryCB.h Notes: This procedure is responsible for handling WM_CREATE. --------------------------------------------------------------------------*/ BOOL Window_OnCreate (HWND hWnd, CREATESTRUCT FAR * lpCreateStruct) { L_INT32 lSizeToRead; L_UINT32 uNumOfBytesRead; L_UINT32 uFileSize ; HANDLE hFileOpen; UNREFERENCED_PARAMETER (lpCreateStruct); /* Tell Window_OnPaint to fire off the WM_COMMAND message. */ bFirst = TRUE; /* Open the file to load to memory. */ hFileOpen = CreateFile ( Data.szFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if(hFileOpen== NULL || hFileOpen == INVALID_HANDLE_VALUE) { MessageBox (hWnd, TEXT("Error Opening File!"), TEXT("Error"), MB_OK); return (FALSE); } // Try to obtain hFile's size uFileSize = SetFilePointer(hFileOpen, 0, NULL, FILE_END ); SetFilePointer(hFileOpen, 0, NULL, FILE_BEGIN ); // If failed ... if (uFileSize == INVALID_FILE_SIZE) { MessageBox (hWnd, TEXT("File is empty!"), TEXT("Error"), MB_OK); } else { Data.lFileSize = uFileSize; } /* Allocate a buffer to hold the file. */ if ((hBuf = GlobalAlloc (GMEM_MOVEABLE, Data.lFileSize)) == NULL) { MessageBox (hWnd, TEXT("Not enough memory!"), TEXT("Error"), MB_OK); CloseHandle(hFileOpen); return (FALSE); } pBuf = (L_CHAR L_HUGE *) GlobalLock (hBuf); pMoverBuf = pBuf; lSizeToRead = 0; /* Read the file in a loop, because the file could be larger than a single read can request. */ while (lSizeToRead < Data.lFileSize) { if ( !ReadFile(hFileOpen, pMoverBuf, min (Data.lFileSize - lSizeToRead, 32000), &uNumOfBytesRead, NULL)) { MessageBox (hWnd, TEXT("Error reading file!"), TEXT("Error"), MB_OK); GlobalUnlock (hBuf); GlobalFree (hBuf); CloseHandle(hFileOpen); return (FALSE); /* Failure on creation! */ } else { lSizeToRead += uNumOfBytesRead; pMoverBuf += uNumOfBytesRead; } } CloseHandle(hFileOpen); return (TRUE); /* Creation is OK. */ } /*---[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_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: MemoryCB.h Notes: This procedure is responsible for handling WM_ONCOMMAND. --------------------------------------------------------------------------*/ VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify) { HDC hDC; RECT rWndSize; RECT rWndRect; L_INT nRet; L_UINT uFlags = LOADFILE_ALLOCATE | LOADFILE_NOINTERLACE; L_TCHAR buf[1024]; IMAGECBPARM Parm; UNREFERENCED_PARAMETER (id); UNREFERENCED_PARAMETER (hwndCtl); UNREFERENCED_PARAMETER (codeNotify); /* Get information about the file in memory. */ Data.FileInfo.uStructSize = sizeof(FILEINFO); nRet = L_FileInfoMemory (pBuf, &Data.FileInfo, sizeof(FILEINFO), Data.lFileSize, 0, NULL); if (nRet == SUCCESS) { GetWindowRect (hWnd, &rWndSize); /* Set up RECT for sizing the window to the image dimensions. */ rWndRect.left = 0; rWndRect.top = 0; rWndRect.right = INFOWIDTH(&Data.FileInfo); rWndRect.bottom = INFOHEIGHT(&Data.FileInfo); /* Create RECT to size of image plus window frame. */ AdjustWindowRect (&rWndRect, WS_OVERLAPPEDWINDOW, FALSE); /* Change the window size to match the image. */ MoveWindow (hWnd, rWndSize.left, rWndSize.top, (rWndRect.right - rWndRect.left), (rWndRect.bottom - rWndRect.top), TRUE); /* Setup the CallBack User parameters. */ hDC = GetDC (hWnd); GetClientRect (hWnd, &Parm.rcClient); Parm.hwnd = hWnd; Parm.hdc = hDC; Parm.hPalette = NULL; Parm.rcPaint = Parm.rcClient; /* Init. the bitmap to the image width, height, and bits per pixel. */ L_InitBitmap (&Data.BitmapHandle, sizeof(BITMAPHANDLE), Data.FileInfo.Width, Data.FileInfo.Height, Data.FileInfo.BitsPerPixel); /* Load the bitmap. */ nRet = L_LoadMemory (pBuf, &Data.BitmapHandle, sizeof(BITMAPHANDLE), Data.BitmapHandle.BitsPerPixel, Data.BitmapHandle.Order, uFlags, LoadImageCB, &Parm, Data.lFileSize, NULL, &Data.FileInfo); ReleaseDC (hWnd, hDC); InvalidateRect ( hWnd, NULL, TRUE ) ; UpdateWindow ( hWnd ) ; if (nRet != SUCCESS) { wsprintf (buf, TEXT("Error %d loading %s."), nRet, (LPTSTR) Data.szFilename); MessageBox (hWnd, buf, TEXT("Error"), MB_OK); FORWARD_WM_CLOSE (hWnd, PostMessage); } else /* Tell the window to get a new palette for painting. */ FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage); } else { wsprintf (buf, TEXT("L_FileInfoMemory returned error %d."), nRet); MessageBox (hWnd, buf, TEXT("Error"), MB_OK); FORWARD_WM_CLOSE (hWnd, PostMessage); } return; } /*----(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: MemoryCB.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_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_OnQueryNewPalette)-------------------------------------------- Syntax: BOOL Window_OnQueryNewPalette( HWND hWnd ) Parameters: hWnd Handle to a window. Prototype: MemoryCB.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: MemoryCB.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, 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 (bFirst) { bFirst = FALSE; /* Post message to load the image. */ FORWARD_WM_COMMAND (hWnd, NULL, 0, 0, PostMessage); } return; } /*---[Window_OnDestroy]---------------------------------------------------- Syntax: VOID Window_OnDestroy( HWND hWnd ); Parameters: hWnd Window handle. Prototype: MemoryCB.h Notes: This procedure is responsible for handling WM_DESTROY. --------------------------------------------------------------------------*/ VOID Window_OnDestroy (HWND hWnd) { UNREFERENCED_PARAMETER (hWnd); if (hBuf) { GlobalUnlock (hBuf); GlobalFree (hBuf); } if (Data.BitmapHandle.Flags.Allocated) /* Do we have an image? */ L_FreeBitmap (&Data.BitmapHandle); if (Data.hPalette) /* Delete the palette, if there is one. */ DeleteObject (Data.hPalette); PostQuitMessage (0); /* Post WM_QUIT, to end the application. */ return; } /*---[LoadImageCB]----------------------------------------------------------- Syntax: L_INT L_EXPORT LoadImageCB (pFILEINFO pFileInfo, pBITMAPHANDLE pBitmap, L_UCHAR L_FAR * pBuffer, L_UINT uFlags, L_INT nRow, L_INT nLines, IMAGECBPARM L_FAR * pParm) Parameters: pBitmap Pointer to the bitmap handle. pBuffer Pointer to the buffer that contains the decompress data. nLines The number of decompressed lines. pParam Additional information used to know the number of rows. Prototype: MemoryCB.h Notes: This procedure is responsible for handling the image data L_LoadMemory passes during the load. --------------------------------------------------------------------------*/ L_INT L_EXPORT LoadImageCB (pFILEINFO pFileInfo, pBITMAPHANDLE pBitmap, L_UCHAR L_FAR * pBuffer, L_UINT uFlags, L_INT nRow, L_INT nLines, IMAGECBPARM L_FAR * pParm) { RECT rc; if ((uFlags & FILEREAD_FIRSTPASS) && (uFlags & FILEREAD_FIRSTROW)) { if (GetFocus () == pParm->hwnd) FORWARD_WM_QUERYNEWPALETTE (pParm->hwnd, SendMessage); if (Data.hPalette) pParm->hPalette = SelectPalette (pParm->hdc, Data.hPalette, TRUE); } SetRect (&rc, 0, 0, BITMAPWIDTH(pBitmap), BITMAPHEIGHT(pBitmap)); L_PaintDCBuffer (pParm->hdc, pBitmap, NULL, NULL, &rc, &pParm->rcClient, SRCCOPY, pBuffer, nRow, nLines); /* Also store it in the bitmap so we can use it again if we want to redraw the image. */ L_AccessBitmap (pBitmap); /* Lock the bitmap */ L_PutBitmapRow (pBitmap, pBuffer, nRow, (L_UINT32) pBitmap->BytesPerLine * nLines); L_ReleaseBitmap (pBitmap); return (SUCCESS); } /*---[ExtractCommandData]---------------------------------------------------- Syntax: L_BOOL ExtractCommandData( ); Parameters: None. ProtoType: Memorycb.c Notes: - This procedure is responsible for extracting the file name passed through the command line. - You should call this function only in WinMain function. --------------------------------------------------------------------------*/ L_BOOL ExtractCommandData ( ) { LPTSTR pszFirst = NULL; LPTSTR pszCmdLine = NULL; LPTSTR psz = NULL; L_INT nFirstPos = 0 ; L_INT nStringLen = 0 ; pszCmdLine = GetCommandLine(); nStringLen = lstrlen ( pszCmdLine ) + 1 ; // 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 ; } memset (Data.szFilename, 0, sizeof(Data.szFilename)); lstrcpyn ( Data.szFilename, pszFirst - 1, ( nStringLen - nFirstPos ) ) ; return TRUE; }