/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(SaveCB)--------------------------------------------------------------- LEAD Functions Used. L_FileInfo L_InitBitmap L_LoadFile L_FreeBitmap L_CreatePaintPalette L_PaintDC L_SizeBitmap L_SaveFile L_AccessBitmap L_GetBitmapRow L_ReleaseBitmap We have made the assumption that the user has the knowledge of programing in C and Windows. This example will: 1. load the image from a file, whose name is sent through the command line, using L_LoadFile, 2. display the image, 3. save the image to the output directory using a callback and status window. Usage: SAVECB --------------------------------------------------------------------------*/ #include /* required for all Windows applications */ #include /* Needed for message crackers. */ #include #include "tchar.h" #include "..\\..\\..\\include\\l_bitmap.h" /* LEADTOOLS main header file. */ #include "..\\..\\..\\include\\l_error.h" /* LEADTOOLS error definition header file. */ #include "SaveCB.h" /* Application specific header file. */ #define DO_1 #define DO_4 #define DO_8 /* #define DO_16 */ /* #define DO_24 */ /* #define DO_32 */ #define FORMAT FILE_WPG /* ACTUAL FILE FORMAT */ L_TCHAR *pFormat = TEXT("FILE_WPG"); /* WINDOW TITLE */ L_TCHAR *pExtension = TEXT("WPG"); /* MAX OF 3 LETTERS */ #define QFACTOR 30 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. */ ExtractCommandData ( ) ; 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: SaveCB.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 = TEXT("MENU_STATUS"); /* Menu name. */ 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: SaveCB.h Notes: Saves instance handle and creates main window. --------------------------------------------------------------------------*/ L_BOOL InitInstance (HANDLE hInstance, L_INT nCmdShow) { 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; /* Set up RECT for window sizing. */ rWndSize.top = 0; rWndSize.left = 0; rWndSize.bottom = INFOHEIGHT(&Data.FileInfo); rWndSize.right = INFOWIDTH(&Data.FileInfo); /* Create RECT to size of image plus window frame. */ AdjustWindowRect (&rWndSize, WS_OVERLAPPEDWINDOW, FALSE); /* Create a main window for this application instance. */ MainWnd = 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 (MainWnd == NULL) return (FALSE); /* If window could not be created, return "failure". */ ShowWindow (MainWnd, nCmdShow); /* Show the window. */ UpdateWindow (MainWnd); /* Send WM_PAINT message. */ return (TRUE); } /*---[StatusWndProc]----------------------------------------------------------- Syntax: L_INT32 EXT_FUNCTION StatusWndProc( HWND hWnd, L_UINT message, WPARAM wParam, LPARAM lParam ) Parameters: hWnd Window handle. message Type of message. wParam Additional information. lParam Additional information. Prototype: SaveCB.h Notes: This procedure is responsible for handling window messages. --------------------------------------------------------------------------*/ L_INT32 EXT_FUNCTION StatusWndProc (HWND hWnd, L_UINT Message, WPARAM wParam, LPARAM lParam) { switch (Message) { HANDLE_MSG (hWnd, WM_CREATE, Status_OnCreate); HANDLE_MSG (hWnd, WM_COMMAND, Status_OnCommand); } return DefWindowProc (hWnd, Message, wParam, lParam); } /*---[Status_OnCreate]----------------------------------------------------- Syntax: BOOL Status_OnCreate(HWND hWnd, CREATESTRUCT FAR* lpCreateStruct); Parameters: hWnd Window handle. lpCreateStruct Windows Create Structure. Prototype: SaveCB.h Notes: This procedure is responsible for handling WM_CREATE. --------------------------------------------------------------------------*/ BOOL Status_OnCreate (HWND hWnd, CREATESTRUCT FAR * lpCreateStruct) { HMENU hMenu; UNREFERENCED_PARAMETER (lpCreateStruct); hMenu = GetMenu (hWnd); EnableMenuItem (hMenu, 0, MF_BYPOSITION | MF_DISABLED | MF_GRAYED); return (TRUE); } /*---[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); } /*---[Status_OnCommand]----------------------------------------------------- Syntax: VOID Status_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: SaveCB.h Notes: This procedure is responsible for handling WM_ONCOMMAND. --------------------------------------------------------------------------*/ VOID Status_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify) { UNREFERENCED_PARAMETER (hwndCtl); UNREFERENCED_PARAMETER (codeNotify); UNREFERENCED_PARAMETER (hWnd); switch (id) { case IDM_QUIT: bKillSave = TRUE; break; } return; } /*---[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: SaveCB.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_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: SaveCB.h Notes: This procedure is responsible for handling WM_CREATE. --------------------------------------------------------------------------*/ BOOL Window_OnCreate (HWND hWnd, CREATESTRUCT FAR * lpCreateStruct) { HMENU hMenu; WNDCLASS wcWindowClass; UNREFERENCED_PARAMETER (lpCreateStruct); StatusWnd = 0; wcWindowClass.style = 0; /* Class style(s). */ wcWindowClass.lpfnWndProc = StatusWndProc; /* 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 = hInst; /* Owner. */ wcWindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION); wcWindowClass.hCursor = LoadCursor (NULL, IDC_ARROW); wcWindowClass.hbrBackground = GetStockObject (WHITE_BRUSH); wcWindowClass.lpszMenuName = TEXT("MENU_STATUS"); /* Menu name. */ wcWindowClass.lpszClassName = TEXT("StatusClass"); /* Name. */ /* Register the window class. */ if (RegisterClass (&wcWindowClass) == 0) MessageBox (hWnd, TEXT("Error registering status window!"), TEXT("Error"), MB_OK); StatusWnd = CreateWindow ( TEXT("StatusClass"), TEXT("LEADTOOLS Sample SaveFile Status"), /* Window title. */ WS_THICKFRAME | WS_CAPTION | WS_POPUP | WS_VISIBLE, /* Window style. */ 80,/* Horizontal position. */ 80,/* Vertical position. */ 270, /* Window width. */ 140, /* Window height. */ MainWnd, /* Parent. */ NULL, /* Use the window class menu. */ hInst, /* This instance owns this window. */ NULL /* Pointer not needed. */ ); if (StatusWnd) { hMenu = GetMenu (StatusWnd); EnableMenuItem (hMenu, 0, MF_BYPOSITION | MF_DISABLED | MF_GRAYED); EnableMenuItem (hMenu, 1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED); DrawMenuBar (StatusWnd); } return (TRUE); } /*---[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: SaveCB.h Notes: This procedure is responsible for handling WM_ONCOMMAND. --------------------------------------------------------------------------*/ VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify) { static SAVEPARM Parm; L_INT nRet = IDM_QUIT; L_TCHAR buf[1024]; /* buffer to hold the error message */ HMENU hMenu; HCURSOR hCursor; UNREFERENCED_PARAMETER (hwndCtl); UNREFERENCED_PARAMETER (codeNotify); if (StatusWnd != 0) { Parm.hWnd = StatusWnd; BringWindowToTop (Parm.hWnd); } if (id == IDM_QUIT) FORWARD_WM_CLOSE (MainWnd, SendMessage); else { /* Load the bitmap and save it as per #defines */ hMenu = GetMenu (hWnd); EnableMenuItem (hMenu, 0, MF_BYPOSITION | MF_DISABLED | MF_GRAYED); EnableMenuItem (hMenu, 1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED); DrawMenuBar (hWnd); hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); id = id; L_InitBitmap (&Data.BitmapHandle, sizeof(BITMAPHANDLE), Data.FileInfo.Width, Data.FileInfo.Height, id); nRet = L_LoadFile (Data.szFilename, &Data.BitmapHandle, sizeof(BITMAPHANDLE), 0, ORDER_BGRORGRAY, LOADFILE_STORE | LOADFILE_ALLOCATE | LOADFILE_NOINTERLACE, NULL, NULL, NULL, &Data.FileInfo); if (nRet == SUCCESS) { FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage); wsprintf (buf, TEXT("Image displayed is %d bit"), id); SetWindowText (hWnd, buf); InvalidateRect (hWnd, NULL, TRUE); UpdateWindow (hWnd); } else { wsprintf (buf, TEXT("Error %d Loading %s"), nRet, (LPTSTR) Data.szFilename); MessageBox (hWnd, buf, TEXT("Error"), MB_OK); } if (Parm.hWnd != 0) { Parm.hDC = GetDC (Parm.hWnd); hMenu = GetMenu (Parm.hWnd); EnableMenuItem (hMenu, 1, MF_BYPOSITION | MF_ENABLED); DrawMenuBar (Parm.hWnd); } Data.BitmapHandle.DitheringMethod = FLOYD_STEIN_DITHERING; SetCursor (hCursor); #ifdef DO_1 nRet = SaveTheImage (id, 1, SaveImageCB, &Parm); if (nRet != SUCCESS && nRet != -1000) { wsprintf (buf, TEXT("Error %d Saving 1 bit"), nRet); MessageBox (Parm.hWnd, buf, TEXT("Error"), MB_OK); } UpdateWindow (hWnd); #endif #ifdef DO_4 nRet = SaveTheImage (id, 4, SaveImageCB, &Parm); if (nRet != SUCCESS && nRet != -1000) { wsprintf (buf, TEXT("Error %d Saving 4 bit"), nRet); MessageBox (Parm.hWnd, buf, TEXT("Error"), MB_OK); } UpdateWindow (hWnd); #endif #ifdef DO_8 nRet = SaveTheImage (id, 8, SaveImageCB, &Parm); if (nRet != SUCCESS && nRet != -1000) { wsprintf (buf, TEXT("Error %d Saving 8 bit"), nRet); MessageBox (Parm.hWnd, buf, TEXT("Error"), MB_OK); } UpdateWindow (hWnd); #endif #ifdef DO_16 nRet = SaveTheImage (id, 16, SaveImageCB, &Parm); if (nRet != SUCCESS && nRet != -1000) { wsprintf (buf, "Error %d Saving 16 bit", nRet); MessageBox (Parm.hWnd, buf, "Error", MB_OK); } UpdateWindow (hWnd); #endif #ifdef DO_24 nRet = SaveTheImage (id, 24, SaveImageCB, &Parm); if (nRet != SUCCESS && nRet != -1000) { wsprintf (buf, "Error %d Saving 24 bit", nRet); MessageBox (Parm.hWnd, buf, "Error", MB_OK); } UpdateWindow (hWnd); #endif #ifdef DO_32 nRet = SaveTheImage (id, 32, SaveImageCB, &Parm); if (nRet != SUCCESS && nRet != -1000) { wsprintf (buf, "Error %d Saving 32 bit", nRet); MessageBox (Parm.hWnd, buf, "Error", MB_OK); } UpdateWindow (hWnd); #endif InvalidateRect (hWnd, NULL, TRUE); if (nRet == SUCCESS) MessageBox (Parm.hWnd, TEXT("The save was successful!"), TEXT("SUCCESS"), MB_OK); else { wsprintf (buf, TEXT("Error %d Saving %d bit files."), nRet, id); MessageBox (Parm.hWnd, buf, TEXT("Error"), MB_OK); } if (Parm.hWnd != 0) { hMenu = GetMenu (Parm.hWnd); EnableMenuItem (hMenu, 0, MF_BYPOSITION | MF_DISABLED | MF_GRAYED); EnableMenuItem (hMenu, 1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED); DrawMenuBar (Parm.hWnd); SelectObject (Parm.hDC, GetStockObject (WHITE_BRUSH)); SelectObject (Parm.hDC, GetStockObject (WHITE_PEN)); Rectangle (Parm.hDC, 0, 0, 270, 140); ReleaseDC (Parm.hWnd, Parm.hDC); } hMenu = GetMenu (hWnd); EnableMenuItem (hMenu, 0, MF_BYPOSITION | MF_ENABLED); EnableMenuItem (hMenu, 1, MF_BYPOSITION | MF_ENABLED); DrawMenuBar (hWnd); } 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: SaveCB.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: SaveCB.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: SaveCB.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 */ return; } /*---[Window_OnDestroy]---------------------------------------------------- Syntax: VOID Window_OnDestroy( HWND hWnd ); Parameters: hWnd Window handle. Prototype: SaveCB.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) { DeleteObject (Data.hPalette); Data.hPalette = NULL; } if (StatusWnd != 0) DestroyWindow (StatusWnd); PostQuitMessage (0); /* Post WM_QUIT, to end the application. */ return; } /*---[SaveTheImage]-------------------------------------------------------- Syntax: L_INT SaveTheImage( L_INT nBitsFrom, L_INT nBitsTo, FILESAVECALLBACK lpfnCallBack, SAVEPARM L_FAR *pParm) Parameters: nBitsFrom Bits per pixel of the image. nBitsTo Bits per pixel to save the image as. lpfnCallBack Callback function. pParm User structure. Prototype: SaveCB.h Notes: This procedure is responsible for saving the image. --------------------------------------------------------------------------*/ L_INT SaveTheImage (L_INT nBitsFrom, L_INT nBitsTo, FILESAVECALLBACK lpfnCallBack, SAVEPARM L_FAR * pParm) { L_INT nRet; L_UINT uFlags = 0; L_TCHAR BufText[1024]; L_TCHAR BufFile[1024]; bKillSave = FALSE; #if FORMAT == FILE_MAC L_SizeBitmap (&Data.BitmapHandle, 576, 720); #endif wsprintf (BufText, TEXT("Saving %d as %d bit %s"), nBitsFrom, nBitsTo, (LPTSTR) pFormat); SetWindowText (pParm->hWnd, BufText); SelectObject (pParm->hDC, GetStockObject (WHITE_BRUSH)); SelectObject (pParm->hDC, GetStockObject (NULL_PEN)); Rectangle (pParm->hDC, 0, 0, 265, 119); wsprintf (BufFile, TEXT("%s\\BIT%d_%d.%s"), (LPTSTR) Data.szImageDir, nBitsFrom, nBitsTo, (LPTSTR) pExtension); if (nBitsFrom > nBitsTo) uFlags = SAVEFILE_FIXEDPALETTE; else uFlags = SAVEFILE_OPTIMIZEDPALETTE; Data.BitmapHandle.DitheringMethod = FLOYD_STEIN_DITHERING; pParm->nRow = 1; nRet = L_SaveFile (BufFile, &Data.BitmapHandle, FORMAT, nBitsTo, QFACTOR, uFlags, lpfnCallBack, (L_VOID L_FAR *) pParm, NULL); return (nRet); } /*---[SaveImageCB]----------------------------------------------------------- Syntax: L_INT L_FAR SaveImageCB( pBITMAPHANDLE pBitmap, L_UCHAR L_FAR *pBuffer, L_UINT nRowBegin, L_UINT nRowsToGet, L_VOID L_FAR *pParm ); Parameters: pBitmap Pointer to the bitmap handle. pBuffer Pointer to the buffer to fill. nRowBegin The first row number of the image to get. nRowsToGet The number of rows to get (in increasing order!). pParm Additional information used for status bar. Notes: This procedure is responsible for getting the image data requested by L_SaveFile. This function will obtain the rows of data from the bitmap and update a statusbar. --------------------------------------------------------------------------*/ L_INT L_EXPORT L_FAR SaveImageCB (pBITMAPHANDLE pBitmap, L_UCHAR L_FAR * pBuffer, L_UINT nRowBegin, L_UINT nRowsToGet, L_VOID L_FAR * pParm) { MSG msg; L_UINT i; L_UINT nRowLast = nRowBegin + nRowsToGet; L_INT32 lRet32; L_TCHAR Buf[1024]; L_INT nCompletion; while (PeekMessage (&msg, StatusWnd, 0, 0, PM_REMOVE)) { TranslateMessage (&msg); DispatchMessage (&msg); } for (i = nRowBegin; i < nRowLast; i++) { L_AccessBitmap (pBitmap); lRet32 = L_GetBitmapRow (pBitmap, pBuffer, i, pBitmap->BytesPerLine); L_ReleaseBitmap (pBitmap); if (lRet32 != (L_INT32) pBitmap->BytesPerLine) { MessageBox (MainWnd, TEXT("Error in callback getting data!"), TEXT("ERROR"), MB_OK); return (FAILURE); } else { if (((SAVEPARM L_FAR *) pParm)->hWnd) { nCompletion = (L_INT) ((L_INT32) ((SAVEPARM L_FAR *) pParm)->nRow++ * 100 / pBitmap->Height); wsprintf ((LPTSTR) Buf, TEXT("The save is %d%% complete!"), nCompletion); SelectObject (((SAVEPARM L_FAR *) pParm)->hDC, GetStockObject (GRAY_BRUSH)); SelectObject (((SAVEPARM L_FAR *) pParm)->hDC, GetStockObject (NULL_PEN)); Rectangle (((SAVEPARM L_FAR *) pParm)->hDC, 31, 51, 30 + 2 * nCompletion, 80); SelectObject (((SAVEPARM L_FAR *) pParm)->hDC, GetStockObject (NULL_BRUSH)); SelectObject (((SAVEPARM L_FAR *) pParm)->hDC, GetStockObject (BLACK_PEN)); Rectangle (((SAVEPARM L_FAR *) pParm)->hDC, 30, 50, 230, 80); TextOut (((SAVEPARM L_FAR *) pParm)->hDC, 30, 20, (LPTSTR) Buf, lstrlen ((LPTSTR) Buf)); } } pBuffer += pBitmap->BytesPerLine; } if (bKillSave) return (-1000); else return (SUCCESS); } /*---[ExtractCommandData]---------------------------------------------------- Syntax: L_BOOL ExtractCommandData( ); Parameters: None. ProtoType: savecb.c Notes: - This procedure is responsible for extracting the file name and dir name 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.szFilename, 0, sizeof(Data.szFilename)); memset (Data.szImageDir, 0, sizeof(Data.szImageDir)); lstrcpyn ( Data.szFilename, pszFirst - 1, abs( ( nStringLen - nFirstPos ) - ( nStringLen - nSecondPos ) ) ) ; lstrcpyn ( Data.szImageDir, pszSecond - 1, ( nStringLen - ( nStringLen - nSecondPos ) ) ) ; /* tolerate directory names that end in '\\' */ if( Data.szImageDir [lstrlen(Data.szImageDir)-1] == '\\' ) Data.szImageDir [lstrlen(Data.szImageDir)-1] = 0; return TRUE; }