/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(EZFunc)--------------------------------------------------------------- LEAD functions used: L_LoadBitmap L_CreatePaintPalette L_PaintDC L_FreeBitmap This example does the following: 1. Gets an image file name from the command line. 2. Loads the image into a bitmap, and updates the display palette. 3. Creates a window, using the default window size. 4. Fits the image in the window, while preserving the aspect ratio. 5. Displays the image. Usage: EZFUNC --------------------------------------------------------------------------*/ #include /* required for all Windows applications */ #include #include #include #include "tchar.h" #include "..\\..\\..\\include\\l_bitmap.h" /* LEADTOOLS main header file */ #include "..\\..\\..\\include\\l_error.h" /* LEADTOOLS error messages */ #include "EZFunc.h" /* Application specific header file */ L_BOOL ExtractCommandData ( ); /*---[WinMain]--------------------------------------------------------------- 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 the Windows SDK for more information. */ /* Macro defined in ltlck.h for conveniently implementing support for LZW file formats and LEADTOOLS Express features */ UNLOCKSUPPORT(); /* If no other instances of the application are running, initialize the application. Exit if the initialization fails */ if (!hPrevInstance) if (!InitApplication (hInstance)) return (FALSE); ExtractCommandData ( ) ; /* Call InitInstance, which saves an instance handle and creates the main window */ if (!InitInstance (hInstance, nCmdShow)) return (FALSE); /* Acquire and dispatch messages until a WM_QUIT message is received. */ while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); /* Translate virtual key codes. */ DispatchMessage (&msg); /* Dispatche message to window. */ } return (msg.wParam); /* Return the value from PostQuitMessage. */ } /*---[InitApplication]------------------------------------------------------ Parameters: hInstance Current instance. ProtoType: EZFunc.h Notes: Initializes the window class structure and registers the window class. --------------------------------------------------------------------------*/ L_BOOL InitApplication (HANDLE hInstance) { WNDCLASS wcWindowClass; wcWindowClass.style = 0; /* Class styles */ 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]---------------------------------------------------------- Parameters: hInstance Current instance. nCmdShow Param for first ShowWindow() call. ProtoType: EZFunc.h Notes: Saves instance handle and creates main window. --------------------------------------------------------------------------*/ L_BOOL InitInstance (HANDLE hInstance, L_INT nCmdShow) { HWND hWnd; /* Create a main window for this application instance. */ hWnd = CreateWindow ( TEXT("LEADWClass"), TEXT("LEADTOOLS Sample Application"), /* Window title */ WS_OVERLAPPEDWINDOW, /* Window style */ CW_USEDEFAULT, /* Default X position */ CW_USEDEFAULT, /* Default Y positiond */ CW_USEDEFAULT, /* Default window width */ CW_USEDEFAULT, /* Default 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 the window could not be created, return FALSE */ if (hWnd == NULL) return (FALSE); /* Show the window. */ ShowWindow (hWnd, nCmdShow); /* Send a WM_PAINT message. */ UpdateWindow (hWnd); return (TRUE); } /*---[MainWndProc]----------------------------------------------------------- Parameters: hWnd Window handle. message Type of message. wParam Additional information. lParam Additional information. ProtoType: EZFunc.h Notes: This procedure is responsible for handling window messages. --------------------------------------------------------------------------*/ L_INT32 EXT_FUNCTION MainWndProc (HWND hWnd, L_UINT Message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; /* Structure for processing the WM_PAINT message */ HPALETTE hPalette = NULL; /* Temporary copy of the current system palette */ HDC hdc; /* Device context used with the palette functions */ L_INT nRet; /* Return value */ L_INT nNoColors; /* Number of colors changed in the system palette */ L_TCHAR achBuff[1024]; /* Buffer for MessageBox strings */ int AdjustedWidth, AdjustedHeight; /* Adjusted dimensions of the window */ switch (Message) { case WM_CREATE: /* Load the bitmap, keeping the bits per pixel of the file */ nRet = L_LoadBitmap (szFilename, &LeadBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR, NULL, NULL); if (nRet != SUCCESS) { wsprintf (achBuff, TEXT("Error %d loading %s"), nRet, (LPTSTR) szFilename); MessageBox (NULL, achBuff, TEXT("Error"), MB_OK); /* We have an error, so post WM_DESTROY */ PostMessage (hWnd, WM_DESTROY, 0, 0); return (FALSE); } /* Get the current window size and client area */ GetWindowRect(hWnd, &rWndSize); GetClientRect(hWnd,&rClientSize); /* Use this function to fit the displayed image in the client area. */ CalcDisplay ( rClientSize.right, /* Width allowed */ rClientSize.bottom, /* Height allowed */ BITMAPWIDTH(&LeadBitmap), /* Width factor, for aspect ratio */ BITMAPHEIGHT(&LeadBitmap), /* Height factor, for aspect ratio */ NULL, /* Resulting left value, not used */ NULL, /* Resulting top value, not used */ &DisplayWidth, /* Resulting width value */ &DisplayHeight); /* Resulting height value */ /* Adjust the window size to remove blank space at the right or bottom */ AdjustedWidth = rWndSize.right - rWndSize.left + DisplayWidth - rClientSize.right; AdjustedHeight = rWndSize.bottom - rWndSize.top + DisplayHeight - rClientSize.bottom; MoveWindow(hWnd, rWndSize.left, rWndSize.top, AdjustedWidth, AdjustedHeight, FALSE); /* Get the client area of the adjusted window */ GetClientRect(hWnd,&rClientSize); /* Make the destination rectangle for painting the same as the client area */ rLeadDest = rClientSize; /* Set the source rectangle to use the whole bitmap */ SetRect(&rLeadSource, 0, 0, LeadBitmap.Width, LeadBitmap.Height); /* Force paint palette creation */ SendMessage (hWnd, WM_QUERYNEWPALETTE, 0, 0L); return (TRUE); case WM_PALETTECHANGED: if (hWnd == (HWND) wParam)/* Avoid infinite loop */ return (FALSE); /* Free up the old paint palette */ if (hpalPaint) { DeleteObject (hpalPaint); hpalPaint = NULL; } /* Does this window have a bitmap and a palette? */ if (LeadBitmap.Flags.Allocated) { hdc = GetDC (hWnd); hpalPaint = L_CreatePaintPalette (hdc, &LeadBitmap); /* Select and Realize the palette. */ hPalette = SelectPalette (hdc, hpalPaint, TRUE); RealizePalette (hdc); /* Force a repaint. */ InvalidateRect (hWnd, NULL, FALSE); /* Return the old palette. */ SelectPalette (hdc, hPalette, TRUE); ReleaseDC (hWnd, hdc); } return (0); /* Fall through to querynewpalette */ case WM_QUERYNEWPALETTE: /* Free up the old paint palette */ if (hpalPaint) { DeleteObject (hpalPaint); hpalPaint = NULL; } nNoColors = 0; if (LeadBitmap.Flags.Allocated) /* Do we have a bitmap ? */ { /* Create a paint palette and realize it */ hdc = GetDC (hWnd); if ((hpalPaint = L_CreatePaintPalette (hdc, &LeadBitmap)) != NULL) { hPalette = SelectPalette (hdc, hpalPaint, FALSE); nNoColors = RealizePalette (hdc); /* Repaint the the whole image if the palette has changed */ if (nNoColors) InvalidateRect (hWnd, NULL, FALSE); /* Restore the previous palette */ SelectPalette (hdc, hPalette, TRUE); } ReleaseDC (hWnd, hdc); } return (nNoColors ? TRUE : FALSE); case WM_ACTIVATE: FORWARD_WM_QUERYNEWPALETTE(hWnd, SendMessage); case WM_PALETTEISCHANGING: FORWARD_WM_PALETTECHANGED (hWnd, (HWND) (UINT) (lParam), SendMessage); case WM_SYSCOLORCHANGE: FORWARD_WM_QUERYNEWPALETTE(hWnd, SendMessage); case WM_PAINT: /* Get the handle to the device context */ hdc = BeginPaint (hWnd, &ps); if (LeadBitmap.Flags.Allocated) /* Do we have an image? */ { if (hpalPaint) /* If we have a paint palette, select it */ { hPalette = SelectPalette (hdc, hpalPaint, TRUE); /* Uncomment this if you do not process WM_QUERYNEWPALETTE */ /* RealizePalette (hdc); */ } /* Paint the image */ L_PaintDC (hdc, &LeadBitmap, &rLeadSource, /* Source rectangle */ NULL, /* Default source clip area */ &rLeadDest, /* Destination rectangle */ &ps.rcPaint, /* Dest clip set by WM_PAINT */ SRCCOPY); /* Normal Paint */ if (hpalPaint) /* Return old palette */ SelectPalette (hdc, hPalette, TRUE); } EndPaint (hWnd, &ps); /* Return DC */ return (0); case WM_DESTROY: /* Delete the paint palette, if there is one */ if (hpalPaint) DeleteObject (hpalPaint); /* Free the image, if there is one */ if (LeadBitmap.Flags.Allocated) L_FreeBitmap (&LeadBitmap); /* Post WM_QUIT to end the application */ PostQuitMessage (0); break; default: return (DefWindowProc (hWnd, Message, wParam, lParam)); } return (0); } /*---[CalcDisplay]----------------------------------------------------------- Parameters: WidthAllowed Maximum width HeightAllowed Maximum height WidthFactor For preserving aspect ratio, usually bitmap width HeightFactor For preserving aspect ratio, usually bitmap height *ResultTop, Pass NULL if you do not care about centering Otherwise, this is updated with the Y offset *ResultLeft Pass NULL if you do not care about centering Otherwise, this is updated with the X offset *ResultWidth Address of the width variable to update *ResultHeight Address of the height variable to update ProtoType: EZFunc.h Notes: Use this function to fit a displayed image in a particular space, while preserving the aspect ratio. --------------------------------------------------------------------------*/ void CalcDisplay (L_INT WidthAllowed, L_INT HeightAllowed, L_INT WidthFactor, L_INT HeightFactor, L_INT *ResultTop, L_INT *ResultLeft, L_INT *ResultWidth, L_INT *ResultHeight) { /* Local variables for calculating results */ L_INT Left, Top, Width, Height; /* See if using the maximum width will make the image too tall */ if(MulDiv(WidthAllowed, HeightFactor, WidthFactor) < HeightAllowed) { /* Use the maximum width, and calculate the height and top values */ Left = 0; Width = WidthAllowed; Height = MulDiv(Width, HeightFactor, WidthFactor); Top = (HeightAllowed - Height) / 2; } else { /* Use the maximum height, and calculate the width and left values */ Top = 0; Height = HeightAllowed; Width = MulDiv(Height, WidthFactor, HeightFactor); Left = (WidthAllowed - Width) / 2; } /* Update the top and left results, if the caller did not pass NULL */ if (ResultTop != NULL) *ResultTop = Top; if (ResultLeft != NULL) *ResultLeft = Left; /* Update the width and height results */ *ResultWidth = Width; *ResultHeight = Height; return; } /*---[ExtractCommandData]---------------------------------------------------- Syntax: L_BOOL ExtractCommandData( ); Parameters: None. ProtoType: ezfunc.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 ; L_BOOL fCommandLine = FALSE ; 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 (szFilename, 0, sizeof(szFilename)); lstrcpyn ( szFilename, pszFirst - 1, ( nStringLen - nFirstPos ) ) ; return TRUE; }