/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(FeedLoad)--------------------------------------------------------------- LEAD Functions Used. L_FileInfo L_SetDisplayMode L_AllocateBitmap L_AccessBitmap L_StartFeedLoad L_FeedLoad L_StopFeedLoad L_ReleaseBitmap L_PaintDC L_InitBitmap L_FreeBitmap L_PaintDCBuffer L_PutBitmapRow We have made the assumption that the user has the knowledge of programming in C and Windows. This example will: 1. simulate receiving data through a comm port by time decoding the file specified on the command line. 2. display the image as it is loaded. We used L_StartFeedLoad, passing LoadImageCB as its callback function. L_FeedLoad calls LoadImageCB periodically, passing a buffer of data. The callback in this example paints the buffer to the screen and loads the data to the bitmap. If the device is 8 bit, then L_StartFeedLoad is requested to load the file as an 8 bit image. If the device is greater than 8 bit (i.e. 16, 24, or 32 bits) then L_StartFeedLoad is requested to load the image as a 24 bit image. Usage: FeedLoad --------------------------------------------------------------------------*/ #include /* Required for all Windows applications. */ #include /* Windows header for message cracker. */ #include /* Standard C library for _fmemcpy. */ #include #include #include "TCHAR.H" #include #include #include "..\\..\\..\\include\\l_bitmap.h" /* LEADTOOLS main header file. */ #include "..\\..\\..\\include\\l_error.h" /* LEADTOOLS error definition header file. */ #include "FeedLoad.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; /* Windows' Message variable for holding messages */ 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)) /* Do 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: FeedLoad.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. */ 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 Param for first ShowWindow() call. Prototype: FeedLoad.h Notes: Saves instance and Creates main window. --------------------------------------------------------------------------*/ L_BOOL InitInstance (HANDLE hInstance, L_INT nCmdShow) { HWND hWnd; /* Window handle. */ L_INT nRet; L_TCHAR buf[1024]; /* Get image information. */ Data.FileInfo.uStructSize = sizeof(FILEINFO); if ((nRet = L_FileInfo (Data.szFilename, &Data.FileInfo, sizeof(FILEINFO), 0, NULL)) != SUCCESS) { wsprintf (buf, TEXT("ERROR %d from L_FileInfo on file %s"), nRet, (LPTSTR) Data.szFilename); MessageBox (NULL, buf, TEXT("ERROR"), MB_OK); return (FALSE); } hInst = hInstance; bFirst = TRUE; /* Set Ordered Dithering for the Display */ L_SetDisplayMode (DISPLAYMODE_ORDEREDDITHER, DISPLAYMODE_ORDEREDDITHER); /* Set up RECT for window sizing. */ rWndSize.top = 0; rWndSize.left = 0; 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: FeedLoad.h Notes: This procedure is responsible for dispatching window messages. --------------------------------------------------------------------------*/ L_INT32 EXT_FUNCTION MainWndProc (HWND hWnd, L_UINT Message, WPARAM wParam, LPARAM lParam) { switch (Message) { HANDLE_MSG (hWnd, WM_TIMER, Window_OnTimer); HANDLE_MSG (hWnd, WM_COMMAND, Window_OnCommand); HANDLE_MSG (hWnd, WM_QUERYNEWPALETTE, Window_OnQueryNewPalette); HANDLE_MSG (hWnd, WM_PALETTECHANGED, Window_OnPaletteChanged); HANDLE_MSG (hWnd, WM_ACTIVATE, Window_OnActivate); HANDLE_MSG (hWnd, WM_PALETTEISCHANGING, Window_OnPaletteChanging); HANDLE_MSG (hWnd, WM_SYSCOLORCHANGE, Window_SysColorChange); HANDLE_MSG (hWnd, WM_PAINT, Window_OnPaint); HANDLE_MSG (hWnd, WM_DESTROY, Window_OnDestroy); } return DefWindowProc (hWnd, Message, wParam, lParam); } /*---[Window_OnTimer]------------------------------------------------------- Syntax: VOID Window_OnTimer( HWND hWnd, UINT id ); Parameters: hWnd Window handle. id ID of the Timer. Prototype: FeedLoad.h Notes: This procedure is responsible for handling WM_TIMER. --------------------------------------------------------------------------*/ VOID Window_OnTimer (HWND hWnd, UINT id) { static HCURSOR hOrigCursor; UNREFERENCED_PARAMETER (id); nCounter++; /* Increment the Counter. */ /* Has the image been displayed about 3 seconds? (this is reset in the function Window_OnPaint) */ if (nCounter == 1) { hOrigCursor = SetCursor (LoadCursor (NULL, IDC_SIZENS)); return; } if (nCounter == 2) { SetCursor (LoadCursor (NULL, IDC_SIZENESW)); return; } if (nCounter == 3) { SetWindowText (hWnd, TEXT("LEADTOOLS Sample Application - Calling the Simulated Receive Function")); PostMessage (hWnd, WM_COMMAND, 0, 0); /* Send Message for */ /* L_DecompressFile. */ } SetCursor (hOrigCursor); return; } /*---[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: FeedLoad.h Notes: This procedure is responsible for handling WM_ONCOMMAND. --------------------------------------------------------------------------*/ VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify) { HDC hDC; L_TCHAR buf[1024]; /* Hold error message. */ IMAGECBPARM Parm; L_INT nRet; HCURSOR hCursor; UNREFERENCED_PARAMETER (id); UNREFERENCED_PARAMETER (hwndCtl); UNREFERENCED_PARAMETER (codeNotify); /* Kill the timer */ KillTimer (hWnd, nTimer); hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); hDC = GetDC (hWnd); GetClientRect (hWnd, &Parm.rcClient); Parm.hwnd = hWnd; Parm.hdc = hDC; Parm.hPalette = NULL; Parm.rcPaint = Parm.rcClient; Parm.nRow = 0; /* Load the image. */ nRet = SimulatedRecv (Data.szFilename, &Data.BitmapHandle, Data.FileInfo.BitsPerPixel, ORDER_BGR, LOADFILE_ALLOCATE, LoadImageCB, &Parm); SetCursor (hCursor); if (nRet != SUCCESS) { wsprintf (buf, TEXT("Error %d Loading %s"), nRet, (LPTSTR) Data.szFilename); MessageBox (hWnd, buf, TEXT("Error"), MB_OK); } SetWindowText (hWnd, TEXT("LEADTOOLS Sample Application, Example Is Complete")); ReleaseDC (hWnd, hDC); 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: FeedLoad.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: FeedLoad.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); /* Is a palette needed? */ if (Data.hPalette) { 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_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_ACTIVATE. --------------------------------------------------------------------------*/ 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_OnPaint]------------------------------------------------------- Syntax: VOID Window_OnPaint( HWND hWnd ); Parameters: hWnd Window handle. Prototype: FeedLoad.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; /* Reset the counter and start a timer to be used for display delay. */ nCounter = 0; nTimer = SetTimer (hWnd, 1, 1000, NULL); if (!nTimer) { MessageBox (hWnd, TEXT("No Timers Are Available!"), TEXT("ERROR - Resources"), MB_OK); /* No timers are available, so quit the application. */ PostMessage (hWnd, WM_DESTROY, 0, 0); } } } /*---[Window_OnDestroy]---------------------------------------------------- Syntax: VOID Window_OnDestroy( HWND hWnd ); Parameters: hWnd Window handle. Prototype: FeedLoad.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. */ 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: FeedLoad.h Notes: This procedure is responsible for handling the image data L_LoadFile 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); } /*---[SimulatedRecv]----------------------------------------------------------- Syntax: L_INT SimulatedRecv (L_CHAR L_FAR * pszFile, pBITMAPHANDLE pBitmap, L_INT nBitsPerPixel, L_INT nOrder, L_UINT uFlags, FILEREADCALLBACK pfnCallback, L_VOID L_FAR * pUserData) Parameters: same as L_LoadFile Prototype: FeedLoad.h notes: simulates recieving and display image data through a comm port at a buad rate of SIMULATEDBAUDRATE --------------------------------------------------------------------------*/ L_INT SimulatedRecv (L_TCHAR L_FAR * pszFile, pBITMAPHANDLE pBitmap, L_INT nBitsPerPixel, L_INT nOrder, L_UINT uFlags, FILEREADCALLBACK pfnCallback, L_VOID L_FAR * pUserData) { HANDLE hFileOpen; HANDLE hLoad; L_UCHAR cBuf[1024]; L_INT nRead; L_INT nRet; L_UINT32 dwBaseTime; L_UINT32 dwReceiveTotal; L_UINT32 dwReceiveRead; LOADFILEOPTION LoadFileOption; L_UINT32 uNumOfBytesRead; /* Display all meaningful scans for Progressive JPEG and CMP files */ memset( &LoadFileOption, 0, sizeof(LOADFILEOPTION) ); L_GetDefaultLoadFileOption(&LoadFileOption, sizeof(LOADFILEOPTION) ); LoadFileOption.Passes = CALLBACK_WHEN_MEANINGFUL; LoadFileOption.Flags |= ELO_ROTATED; dwBaseTime = GetTickCount(); dwReceiveTotal = 0; dwReceiveRead = 0; hFileOpen = CreateFile( pszFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if( hFileOpen== NULL || hFileOpen == INVALID_HANDLE_VALUE) return (ERROR_FILE_OPEN); nRet = L_StartFeedLoad (pBitmap, sizeof(BITMAPHANDLE), nBitsPerPixel, nOrder, uFlags, pfnCallback, pUserData, &hLoad, &LoadFileOption, NULL); if (nRet != SUCCESS) { CloseHandle(hFileOpen); return (nRet); } for( ; ; ) { dwReceiveTotal = ((GetTickCount() - dwBaseTime) * (SIMULATEDBAUDRATE / 100) / 100); nRead = (L_INT) min(dwReceiveTotal - dwReceiveRead, (L_UINT32) sizeof(cBuf)); if(nRead) { if(!ReadFile(hFileOpen, cBuf, nRead, &uNumOfBytesRead, NULL)) { nRet = ERROR_FILE_READ; goto done; } if(!uNumOfBytesRead) goto done; nRet = L_FeedLoad (hLoad, cBuf, uNumOfBytesRead); if (nRet != SUCCESS) goto done; dwReceiveRead += (L_UINT32) uNumOfBytesRead; } } done: CloseHandle(hFileOpen); nRet = L_StopFeedLoad (hLoad); return (nRet); } /*---[ExtractCommandData]---------------------------------------------------- Syntax: L_BOOL ExtractCommandData( ); Parameters: None. ProtoType: Feedload.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; }