/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(SaveMem)------------------------------------------------------------ LEAD Functions Used. L_CreatePaintPalette L_PaintDC L_FreeBitmap L_FileInfo L_FreeBitmap L_InitBitmap L_LoadBitmap L_SaveBitmapMemory 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, selected using Windows' common dialog box for opening a file, to a bitmap, 2. display the image in a window. 3. get a filename, format, and bits per pixel to save to, using Windows' common dialog box, 4. save the image. This is a menu driven example for loading and saving images. Usage: SAVEMEM --------------------------------------------------------------------------*/ #include /* Required for all Windows applications. */ #include /* Needed for message crackers. */ #include /* Windows' header for common dialog box. */ #include /* Standard C header files. */ #include #include #include #include "TCHAR.H" #define FOR_DLL /* Tell L_Bitmap.h to include only DLL specific definitions. */ #include "..\\..\\..\\include\\l_bitmap.h" /* LEADTOOLS main header file. */ #include "..\\..\\..\\include\\l_error.h" /* LEADTOOLS error definition header file. */ #include "SaveMem.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. */ 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: SaveMem.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_MAIN"); /* Menu Name. */ wcWindowClass.lpszClassName = TEXT("LEADWClass"); /* Class 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: SaveMem.h Notes: Saves instance handle and creates main window. --------------------------------------------------------------------------*/ L_BOOL InitInstance (HANDLE hInstance, L_INT nCmdShow) { /* Create a main window for this application instance. */ hMainWnd = 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, /* Default width. */ CW_USEDEFAULT, /* Default height. */ NULL, /* Overlapped windows have no parent. */ NULL, /* Use the window class menu. */ hInstance, /* This instance owns this window. */ NULL/* Pointer not needed. */ ); if (hMainWnd == NULL) return (FALSE); /* If window could not be created, return "failure". */ ShowWindow (hMainWnd, nCmdShow); /* Show the window. */ UpdateWindow (hMainWnd); /* Send 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: SaveMem.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: SaveMem.h Notes: This procedure is responsible for handling WM_CREATE. --------------------------------------------------------------------------*/ BOOL Window_OnCreate (HWND hWnd, CREATESTRUCT FAR * lpCreateStruct) { HMENU hMenu; UNREFERENCED_PARAMETER (lpCreateStruct); UNREFERENCED_PARAMETER (hWnd); hMenu = GetMenu (hWnd); ENABLEMENUITEM (hMenu, IDM_SAVE, Data.BitmapHandle.Flags.Allocated); Data.hPalette = NULL; nSaveTypeIdx = 1; L_DlgInit(DLG_INIT_COLOR); 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); } /*---[Window_OnCommand]----------------------------------------------------- Syntax: VOID Window_OnCommand( HWND hWnd, 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, 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 IDM_LOAD: nRet = Get_OpenFile (hWnd); if (nRet == 2) ; /* No file was selected for opening, so do nothing. */ else if (nRet == SUCCESS) FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage); else { wsprintf (buf, TEXT("Error %d Loading %s"), nRet, (LPTSTR) Data.szFilename); MessageBox (hWnd, buf, TEXT("Error"), MB_OK); } break; case IDM_SAVE: nRet = SaveBitmapFile (hWnd, &Data.BitmapHandle); if (nRet < SUCCESS) { wsprintf (buf, TEXT("Error %d Saving %s"), nRet, (LPTSTR) Data.szFilename); MessageBox (hWnd, buf, TEXT("Error"), MB_OK); } break; case IDM_EXIT: DestroyWindow (hWnd); break; } 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: SaveMem.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: SaveMem.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: SaveMem.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? */ { // SetRect (&rc, 0, 0, Data.BitmapHandle.Width, Data.BitmapHandle.Height); SetRect (&rc, 0, 0, BITMAPWIDTH(&Data.BitmapHandle), BITMAPHEIGHT(&Data.BitmapHandle)); if (Data.hPalette) /* Is there a palette that needs to be selected? */ hOldPal = SelectPalette (hdc, Data.hPalette, TRUE); /* Select it. */ /* 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: SaveMem.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); L_DlgFree(); PostQuitMessage (0); /* Post WM_QUIT, to end the application. */ return; } L_VOID CleanOpenDlgParam(LPOPENDLGPARAMS pFOParam) { int i=0; if(pFOParam!=NULL && pFOParam->pFileData!= NULL ) { for(i=0; i < pFOParam->nNumOfFiles; ++i ) { if(pFOParam->pFileData[i].pBitmap != NULL) { L_FreeBitmap(pFOParam->pFileData[i].pBitmap); GlobalFreePtr(pFOParam->pFileData[i].pBitmap); } if(pFOParam->pFileData[i].pThumbnail != NULL) { L_FreeBitmap(pFOParam->pFileData[i].pThumbnail); GlobalFreePtr(pFOParam->pFileData[i].pThumbnail); } if(pFOParam->pFileData[i].pFileInfo != NULL) { GlobalFreePtr(pFOParam->pFileData[i].pFileInfo); } } GlobalFreePtr(pFOParam->pFileData); pFOParam->pFileData =NULL; pFOParam->nNumOfFiles = 0; } } /*---[Get_OpenFile]--------------------------------------------------------- Syntax: L_INT Get_OpenFile( HWND hWnd ); Parameters: hWnd Window handle. Prototype: SaveMem.h Notes: This procedure is responsible for getting the filename to load. --------------------------------------------------------------------------*/ L_INT Get_OpenFile (HWND hWnd) { static OPENDLGPARAMS FOParm; static OPENFILENAME OpenFileName; static LOADFILEOPTION LoadFileOption; HMENU hMenu; HCURSOR hCursor; L_INT nRet; _fmemset(&FOParm, 0, sizeof(OPENDLGPARAMS)); FOParm.uStructSize = sizeof(OPENDLGPARAMS); FOParm.bShowLoadOptions = FALSE ; FOParm.bPreviewEnabled = TRUE; FOParm.uDlgFlags = DLG_OPEN_LOADBITMAP | DLG_OPEN_SHOW_PREVIEW | DLG_OPEN_SHOW_FILEINFO; OpenFileName.lStructSize = sizeof(OPENFILENAME); OpenFileName.hwndOwner = hWnd; OpenFileName.lpstrFilter = NULL; OpenFileName.lpstrCustomFilter = NULL; OpenFileName.nMaxCustFilter = 0; OpenFileName.nFilterIndex = 1; OpenFileName.lpstrInitialDir = Data.szImageDir; OpenFileName.lpstrTitle = TEXT("Open File"); OpenFileName.lpstrDefExt = NULL; OpenFileName.Flags = 0; L_GetDefaultLoadFileOption(&LoadFileOption, sizeof(LOADFILEOPTION)); LoadFileOption.Flags |= ELO_ROTATED; nRet = L_DlgOpen( hWnd, &OpenFileName, &FOParm); if(nRet == SUCCESS_DLG_OK) { nRet=FAILURE; if ( ( FOParm.pFileData != NULL ) && ( FOParm.nNumOfFiles > 0 ) ) { lstrcpy (Data.szFilename, FOParm.pFileData[ 0 ].szFileName); if ( FOParm.pFileData[ 0 ].pBitmap != NULL ) { if ( FOParm.pFileData[ 0 ].pBitmap->Flags.Allocated ) { hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); Data.FileInfo.uStructSize = sizeof(FILEINFO); nRet = L_FileInfo (Data.szFilename, &Data.FileInfo, sizeof(FILEINFO), 0, &LoadFileOption); if ( nRet == SUCCESS ) { /* If there is a bitmap, free it. */ if (Data.BitmapHandle.Flags.Allocated) L_FreeBitmap (&Data.BitmapHandle); Data.BitmapHandle = *FOParm.pFileData[ 0 ].pBitmap; nRet = L_LoadBitmap (Data.szFilename, &Data.BitmapHandle, sizeof(BITMAPHANDLE), Data.FileInfo.BitsPerPixel, ORDER_BGR, &LoadFileOption, &Data.FileInfo); } SetCursor (hCursor); } } } CleanOpenDlgParam(&FOParm); } if (nRet == SUCCESS) { FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage); InvalidateRect (hWnd, NULL, TRUE); hMenu = GetMenu (hWnd); ENABLEMENUITEM (hMenu, IDM_SAVE, Data.BitmapHandle.Flags.Allocated); DrawMenuBar (hWnd); return nRet; } return 2; } /*----(SaveBitmapFile)----------------------------------------------------- Syntax: L_INT SaveBitmapFile( HWND hWnd, pBITMAPHANDLE pBitmapHandle ) Prototype: SaveMem.h Parameters: hWnd Handle of the window owner. pBitmapHandle Pointer to the bitmap handle of the image to be saved. Notes: This function will control the saving of an image to a file. --------------------------------------------------------------------------*/ L_INT SaveBitmapFile (HWND hWnd, pBITMAPHANDLE pBitmapHandle) { OPENFILENAME ofn; HCURSOR hCursor; L_INT nRet; static L_BOOL fInit = FALSE; L_TCHAR szFileTitle[256]; L_TCHAR szFilename[256]; L_TCHAR szFilter[1024]= { TEXT("CALS 1\0*.cal\0") TEXT("CCITT 1\0*.tif\0CCITT G3 1D\0*.tif\0CCITT G3 2D\0*.tif\0CCITT G4\0*.tif\0") TEXT("EPS\0*.eps\0") TEXT("GIF\0*.gif\0") TEXT("GEM Image 1\0*.img\0") TEXT("JFIF 4:1:1\0*.jff\0JFIF 4:2:2\0*.jff\0") TEXT("JTIF 4:1:1\0*.jtf\0JTIF 4:2:2\0*.jtf\0") TEXT("JFIF 4:4:4\0*.jpg\0JTIF 4:4:4\0*.jpg\0") TEXT("LEAD\0*.cmp\0LEAD 1 bit\0*.cmp\0") TEXT("MacPaint 1\0*.mac\0") TEXT("Microsoft Paint 1\0*.msp\0") TEXT("OS/2 BMP 1\0*.bmp\0OS/2 BMP 4\0*.bmp\0") TEXT("OS/2 BMP 8\0*.bmp\0OS/2 BMP 24\0*.bmp\0") TEXT("Macintosh PICTURE 1\0*.pct\0Macintosh PICTURE 4\0*.pct\0") TEXT("Macintosh PICTURE 8\0*.pct\0Macintosh PICTURE 24\0*.pct\0") TEXT("PCX 1\0*.pcx\0PCX 4\0*.pcx\0PCX 8\0*.pcx\0PCX 24\0*.pcx\0") TEXT("SUN Raster 1\0*.ras\0SUN Raster 4\0*.ras\0") TEXT("SUN Raster 8\0*.ras\0SUN Raster 24\0*.ras\0SUN Raster 32\0*.ras\0") TEXT("TGA 8\0*.tga\0TGA 16\0*.tga\0TGA 24\0*.tga\0TGA 32\0*.tga\0") TEXT("TIF 1\0*.tif\0TIF LZW 1\0*.tif\0TIF 4\0*.tif\0TIF LZW 4\0*.tif\0TIF 8\0*.tif\0TIF LZW 8\0*.tif\0") TEXT("TIF 16\0*.tif\0TIF LZW 16\0*.tif\0TIF 24\0*.tif\0TIF LZW 24\0*.tif\0") TEXT("Windows BMP 1\0*.bmp\0Windows BMP 4\0*.bmp\0") TEXT("Windows BMP 8\0*.bmp\0Windows BMP 16\0*.bmp\0") TEXT("Windows BMP 24\0*.bmp\0Windows BMP 32\0*.bmp\0") TEXT("WordPerfect 1\0*.wpg\0WordPerfect 4\0*.wpg\0WordPerfect 8\0*.wpg\0") TEXT("WMF 8\0*.wmf\0WMF 24\0*.wmf\0\0") }; _fmemset (szFileTitle, 0, sizeof (szFileTitle)); _fmemset (szFilename, 0, sizeof (szFilename)); _fmemset (&ofn, 0, sizeof (OPENFILENAME)); ofn.lStructSize = sizeof (OPENFILENAME); ofn.hwndOwner = hWnd; ofn.lpstrFilter = szFilter; ofn.nFilterIndex = nSaveTypeIdx; ofn.lpstrFile = szFilename; ofn.nMaxFile = 256; ofn.lpstrFileTitle = szFileTitle; ofn.nMaxFileTitle = sizeof (szFileTitle)/sizeof(L_TCHAR); if (!fInit) { ofn.lpstrInitialDir = Data.szImageDir; fInit = TRUE; } ofn.lpstrTitle = TEXT("Save Bitmap File"); ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY; if (GetSaveFileName (&ofn)) { hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); lstrcpy (Data.szFilename, ofn.lpstrFile); nSaveTypeIdx = (L_INT) ofn.nFilterIndex; nRet = SaveBitmapData (hWnd,Data.szFilename, pBitmapHandle, (L_INT) ofn.nFilterIndex); SetCursor (hCursor); return (nRet); } return (2); } /*----(SaveBitmapData)-------------------------------------------------- Syntax: L_INT SaveBitmapData( HWND hWnd,LPSTR pFilename, pBITMAPHANDLE pBitmapHandle, L_INT nType ) Prototype: SaveMem.h Parameters: hWnd Window Handle, pFilename Pointer to the file name to be used for saving. pBitmapHandle Pointer to the handle of the bitmap holding the image to be saved. nType The type of file the image is to be saved as. Notes: This function will save an image (in a bitmap) to a file. --------------------------------------------------------------------------*/ L_INT SaveBitmapData (HWND hWnd,LPTSTR pFilename, pBITMAPHANDLE pBitmapHandle, L_INT nType) { L_INT nRet; HANDLE hData=NULL; L_CHAR L_FAR *pData; L_CHAR L_HUGE *pDataMover; L_UINT32 uSize; HMENU hMenu; HANDLE hFile=NULL; L_BOOL bRet=FALSE; L_INT nWrittenBytes=0; switch (nType) { case SAVEMEM_TGA8: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TGA, 8, 0, &uSize, NULL); break; case SAVEMEM_TGA16: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TGA, 16, 0, &uSize, NULL); break; case SAVEMEM_TGA24: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TGA, 24, 0, &uSize, NULL); break; case SAVEMEM_TGA32: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TGA, 32, 0, &uSize, NULL); break; case SAVEMEM_TIF1: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TIF, 1, 0, &uSize, NULL); break; case SAVEMEM_TIFLZW1: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TIFLZW, 1, 0, &uSize, NULL); break; case SAVEMEM_CCITT: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_CCITT, 1, 0, &uSize, NULL); break; case SAVEMEM_LEAD1BIT: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_LEAD1BIT, 1, LEAD_0, &uSize, NULL); break; case SAVEMEM_TIF_G3_1D: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_CCITT_GROUP3_1DIM, 1, 0, &uSize, NULL); break; case SAVEMEM_TIF_G3_2D: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_CCITT_GROUP3_2DIM, 1, 0, &uSize, NULL); break; case SAVEMEM_TIF_G4: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_CCITT_GROUP4, 1, 0, &uSize, NULL); break; case SAVEMEM_CALS_G4: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_CALS, 1, 0, &uSize, NULL); break; case SAVEMEM_MAC: nRet = L_SizeBitmap (pBitmapHandle, 576, 720, 0); if (nRet == SUCCESS) nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_MAC, 1, 0, &uSize, NULL); break; case SAVEMEM_IMG1: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_IMG, 1, 0, &uSize, NULL); break; case SAVEMEM_MSP: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_MSP, 1, 0, &uSize, NULL); break; case SAVEMEM_WPG1: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_WPG, 1, 0, &uSize, NULL); break; case SAVEMEM_WPG4: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_WPG, 4, 0, &uSize, NULL); break; case SAVEMEM_WPG8: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_WPG, 8, 0, &uSize, NULL); break; case SAVEMEM_RAS1: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_RAS, 1, 0, &uSize, NULL); break; case SAVEMEM_RAS4: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_RAS, 4, 0, &uSize, NULL); break; case SAVEMEM_RAS8: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_RAS, 8, 0, &uSize, NULL); break; case SAVEMEM_RAS24: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_RAS, 24, 0, &uSize, NULL); break; case SAVEMEM_RAS32: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_RAS, 32, 0, &uSize, NULL); break; case SAVEMEM_PCT1: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_PCT, 1, 0, &uSize, NULL); break; case SAVEMEM_PCT4: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_PCT, 4, 0, &uSize, NULL); break; case SAVEMEM_PCT8: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_PCT, 8, 0, &uSize, NULL); break; case SAVEMEM_PCT24: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_PCT, 24, 0, &uSize, NULL); break; case SAVEMEM_TIF4: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TIF, 4, 0, &uSize, NULL); break; case SAVEMEM_TIF8: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TIF, 8, 0, &uSize, NULL); break; case SAVEMEM_TIF16: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TIF, 16, 0, &uSize, NULL); break; case SAVEMEM_TIF24: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TIF, 24, 0, &uSize, NULL); break; case SAVEMEM_TIFLZW4: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TIFLZW, 4, 0, &uSize, NULL); break; case SAVEMEM_TIFLZW8: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TIFLZW, 8, 0, &uSize, NULL); break; case SAVEMEM_TIFLZW16: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TIFLZW, 16, 0, &uSize, NULL); break; case SAVEMEM_TIFLZW24: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_TIFLZW, 24, 0, &uSize, NULL); break; case SAVEMEM_WBMP1: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_BMP, 1, 0, &uSize, NULL); break; case SAVEMEM_WBMP4: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_BMP, 4, 0, &uSize, NULL); break; case SAVEMEM_WBMP8: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_BMP, 8, 0, &uSize, NULL); break; case SAVEMEM_WBMP16: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_BMP, 16, 0, &uSize, NULL); break; case SAVEMEM_WBMP24: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_BMP, 24, 0, &uSize, NULL); break; case SAVEMEM_WBMP32: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_BMP, 32, 0, &uSize, NULL); break; case SAVEMEM_OBMP1: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_OS2, 1, 0, &uSize, NULL); break; case SAVEMEM_OBMP4: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_OS2, 4, 0, &uSize, NULL); break; case SAVEMEM_OBMP8: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_OS2, 8, 0, &uSize, NULL); break; case SAVEMEM_OBMP24: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_OS2, 24, 0, &uSize, NULL); break; case SAVEMEM_PCX1: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_PCX, 1, 0, &uSize, NULL); break; case SAVEMEM_PCX4: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_PCX, 4, 0, &uSize, NULL); break; case SAVEMEM_PCX8: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_PCX, 8, 0, &uSize, NULL); break; case SAVEMEM_PCX24: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_PCX, 24, 0, &uSize, NULL); break; case SAVEMEM_GIF: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_GIF, 8, 0, &uSize, NULL); break; case SAVEMEM_WMF8: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_WMF, 8, 0, &uSize, NULL); break; case SAVEMEM_WMF24: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_WMF, 24, 0, &uSize, NULL); break; case SAVEMEM_EPS: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_EPS, 8, 0, &uSize, NULL); break; case SAVEMEM_LEAD: if (L_IsGrayScaleBitmap(pBitmapHandle) == GRAY_ORDEREDNORMAL) nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_LEAD, 8, PQ1, &uSize, NULL); else nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_LEAD, 24, PQ1, &uSize, NULL); break; case SAVEMEM_LEAD1JFIF: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_LEAD1JFIF, 24, 30, &uSize, NULL); break; case SAVEMEM_LEAD2JFIF: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_LEAD2JFIF, 24, 30, &uSize, NULL); break; case SAVEMEM_LEAD1JTIF: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_LEAD1JTIF, 24, 30, &uSize, NULL); break; case SAVEMEM_LEAD2JTIF: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_LEAD2JTIF, 24, 30, &uSize, NULL); break; case SAVEMEM_JFIF: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_JFIF, 24, 30, &uSize, NULL); break; case SAVEMEM_JTIF: nRet = L_SaveBitmapMemory (&hData, pBitmapHandle, FILE_JTIF, 24, 30, &uSize, NULL); break; default: nRet = ERROR_FEATURE_NOT_SUPPORTED; } pData = (L_CHAR L_FAR *) GlobalLock (hData); pDataMover = pData; if (nRet == SUCCESS) { MessageBox (NULL, TEXT("Image Saved To Memory!"), TEXT("SUCCESS"), MB_OK); L_FreeBitmap (&Data.BitmapHandle); hMenu = GetMenu (hWnd); ENABLEMENUITEM (hMenu, IDM_SAVE, FALSE); DrawMenuBar (hWnd); InvalidateRect (hMainWnd, NULL, TRUE); UpdateWindow (hMainWnd); } /* Open/Create the file to write. */ hFile = CreateFile( pFilename, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_HIDDEN, NULL ); if ( 0 == hFile ) { MessageBox (NULL, TEXT("Error Opening File!"), TEXT("Error"), MB_OK); return (FALSE); /* Failure on creation! */ } /* Write the file in a loop, because the file could be larger than a single write can request. */ while (uSize > 0) { bRet = WriteFile( hFile, pDataMover, (L_INT) min (uSize, (L_UINT32) 32000), &nWrittenBytes, NULL ); if ( 0 == bRet ) { MessageBox (NULL, TEXT("Error writing file!"), TEXT("Error"), MB_OK); nRet = FALSE; /* Failure on write! */ break; } else { uSize -= nWrittenBytes; pDataMover += nWrittenBytes; } } if ( bRet ) MessageBox (NULL, TEXT("Memory File Has Been Written To Disk!"), TEXT("SUCCESS"), MB_OK); if ( 0 != hFile ) CloseHandle( hFile ); GlobalUnlock (hData); GlobalFree (hData); return (nWrittenBytes); } /*---[ExtractCommandData]---------------------------------------------------- Syntax: L_BOOL ExtractCommandData( ); Parameters: None. ProtoType: savemem.c Notes: - This procedure is responsible for extracting the dir 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.szImageDir, 0, sizeof ( Data.szImageDir) ); lstrcpyn ( Data.szImageDir, pszFirst - 1, ( nStringLen - nFirstPos ) ) ; return TRUE; }