CodeForge QQ客服 CodeForge 400电话 客服电话 4006316121

File.cpp ( 文件浏览 )

  • 发布于2010-11-24
  • 浏览次数:30
  • 下载次数:1
  • 下载需 1 积分
  • 侵权举报
			//  位图文件读写  File.cpp  ,鲍捷,1998年4月
//
//  Source file for Device-Independent Bitmap (DIB) API.  Provides
//  the following functions:
//
//  SaveDIB()           - Saves the specified dib in a file
//  ReadDIBFile()       - Loads a DIB from a file
//

#include "stdafx.h"
#include <math.h>
#include <io.h>
#include <direct.h>
#include "dibapi.h"

/*
 * Dib Header Marker - used in writing DIBs to files
 */
#define DIB_HEADER_MARKER   ((WORD) ('M' << 8) | 'B')

#ifdef _MAC
#define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
#define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
void ByteSwapHeader(BITMAPFILEHEADER* bmiHeader);
void ByteSwapInfo(LPSTR lpHeader, BOOL fWin30Header);
#endif

/*************************************************************************
 *
 * SaveDIB()
 *
 * Saves the specified DIB into the specified CFile.  The CFile
 * is opened and closed by the caller.
 *
 * Parameters:
 *
 * HDIB hDib - Handle to the dib to save
 *
 * CFile& file - open CFile used to save DIB
 *
 * Return value: TRUE if successful, else FALSE or CFileException
 *
 *************************************************************************/


BOOL WINAPI SaveDIB(HDIB hDib, CFile& file)
{

	BITMAPFILEHEADER bmfHdr; // Header for Bitmap file
	LPBITMAPINFOHEADER lpBI;   // Pointer to DIB info structure
	DWORD dwDIBSize;

	if (hDib == NULL)
		return FALSE;

	/*
	 * Get a pointer to the DIB memory, the first of which contains
	 * a BITMAPINFO structure
	 */
	lpBI = (LPBITMAPINFOHEADER) ::GlobalLock((HGLOBAL) hDib);
	if (lpBI == NULL)
		return FALSE;

	if (!IS_WIN30_DIB(lpBI))
	{

		::GlobalUnlock((HGLOBAL) hDib);
		return FALSE;       // It's an other-style DIB (save not supported)
	
}

	/*
	 * Fill in the fields of the file header
	 */

	/* Fill in file type (first 2 bytes must be "BM" for a bitmap) */
	bmfHdr.bfType = DIB_HEADER_MARKER;  // "BM"

	// Calculating the size of the DIB is a bit tricky (if we want to
	// do it right).  The easiest way to do this is to call GlobalSize()
	// on our global handle, but since the size of our global memory may have
	// been padded a few bytes, we may end up writing out a few too
	// many bytes to the file (which may cause problems with some apps).
	//
	// So, instead let's calculate the size manually (if we can)
	//
	// First, find size of header plus size of color table.  Since the
	// first DWORD in both BITMAPINFOHEADER and BITMAPCOREHEADER conains
	// the size of the structure, let's use this.

	dwDIBSize = *(LPDWORD)lpBI + ::PaletteSize((LPSTR)lpBI);  // Partial Calculation

	// Now calculate the size of the image

	if ((lpBI->biCompression == BI_RLE8) || (lpBI->biCompression == BI_RLE4))
	{

		// It's an RLE bitmap, we can't calculate size, so trust the
		// biSizeImage field

		dwDIBSize += lpBI->biSizeImage;
	
}
	else
	{

		DWORD dwBmBitsSize;  // Size of Bitmap Bits only

		// It's not RLE, so size is Width (DWORD aligned) * Height

		dwBmBitsSize = WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI->biBitCount)) * lpBI->biHeight;

		dwDIBSize += dwBmBitsSize;

		// Now, since we have calculated the correct size, why don't we
		// fill in the biSizeImage field (this will fix any .BMP files which
		// have this field incorrect).

		lpBI->biSizeImage = dwBmBitsSize;
	
}


	// Calculate the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)

	bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
	bmfHdr.bfReserved1 = 0;
	bmfHdr.bfReserved2 = 0;

	/*
	 * Now, calculate the offset the actual bitmap bits will be in
	 * the file -- It's the Bitmap file header plus the DIB header,
	 * plus the size of the color table.
	 */
	bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + lpBI->biSize
											  + PaletteSize((LPSTR)lpBI);
#ifdef _MAC
	ByteSwapHeader(&bmfHdr);

	// First swap the size field
	*((LPDWORD)lpBI) = SWAPLONG(*((LPDWORD)lpBI));

	// Now swap the rest of the structure (we don't save < Win30 files)
	ByteSwapInfo((LPSTR)lpBI, TRUE);
#endif

	TRY
	{

		// Write the file header
		file.Write((LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER));
		//
		// Write the DIB header and the bits
		//
		file.WriteHuge(lpBI, dwDIBSize);
	
}
	CATCH (CFileException, e)
	{

#ifdef _MAC
		// Swap everything back
		*((LPDWORD)lpBI) = SWAPLONG(*((LPDWORD)lpBI));
		ByteSwapInfo((LPSTR)lpBI, TRUE);
#endif
		::GlobalUnlock((HGLOBAL) hDib);
		THROW_LAST();
	
}
	END_CATCH

#ifdef _MAC
	// Swap everything back
	*((LPDWORD)lpBI) = SWAPLONG(*((LPDWORD)lpBI));
	ByteSwapInfo((LPSTR)lpBI, TRUE);
#endif

	::GlobalUnlock((HGLOBAL) hDib);
	return TRUE;

}


/*************************************************************************

  Function:  ReadDIBFile (CFile&)

   Purpose:  Reads in the specified DIB file into a global chunk of
			 memory.

   Returns:  A handle to a dib (hDIB) if successful.
			 NULL if an error occurs.

  Comments:  BITMAPFILEHEADER is stripped off of the DIB.  Everything
			 from the end of the BITMAPFILEHEADER structure on is
			 returned in the global memory handle.

*************************************************************************/


HDIB WINAPI ReadDIBFile(CFile& 
...
...
(完整源码请下载查看)
			
...
展开> <收缩

下载源码到电脑,阅读使用更方便

1 积分

快速下载
还剩0行未阅读,继续阅读
云测速

源码文件列表

温馨提示: 点击源码文件名可预览文件内容哦 ^_^
...
名称 大小 修改日期
011.00 B
ChildFrm.cpp2.00 kB04-03-99|22:14
ChildFrm.h1.47 kB04-03-99|22:14
COLOR.CPP3.28 kB06-07-98|18:45
Dibapi.cpp27.34 kB03-13-99|14:08
Dibapi.h2.39 kB03-17-99|19:59
File.cpp7.70 kB12-31-98|10:59
Graydoc.cpp1.45 kB04-04-99|09:14
Graydoc.h680.00 B04-04-99|09:26
GrayDocS.cpp565.00 B04-04-99|09:27
GrayDocS.h582.00 B04-04-99|09:28
Hough.cpp10.02 kB04-08-99|19:01
Hough.h708.00 B04-08-99|10:39
ImageApp.cpp4.95 kB04-06-99|11:52
ImageApp.h926.00 B04-06-99|11:51
ImageAReadMe.txt8.22 kB04-05-99|14:50
ImageDoc.cpp7.93 kB04-05-99|11:26
ImageDoc.h2.31 kB04-03-99|22:28
ImageRegion.cpp1.44 kB04-03-99|22:28
ImageRegion.h877.00 B04-03-99|22:28
ImageS.clw6.82 kB04-12-99|19:26
ImageS.cpp8.05 kB04-05-99|17:22
ImageS.dsp12.17 kB11-25-02|00:16
ImageS.dsw537.00 B04-08-99|13:44
ImageS.h1.33 kB04-05-99|17:22
ImageS.idl402.00 B04-05-99|17:22
ImageS.rc21.37 kB04-08-99|10:27
ImageS.reg1.52 kB04-03-99|22:13
ImageS.rgs175.00 B04-05-99|17:22
ImageView.cpp28.45 kB04-04-99|20:48
ImageView.h6.77 kB04-03-99|22:28
ImgDocS.cpp2.30 kB04-04-99|08:28
ImgDocS.h1.64 kB04-04-99|08:29
ImgVwS.cpp10.22 kB04-08-99|13:36
ImgVwS.h2.29 kB04-08-99|10:09
IpFrame.cpp3.80 kB04-03-99|22:14
IpFrame.h1.65 kB04-03-99|22:14
MainFrm.cpp2.70 kB04-03-99|22:13
MainFrm.h1.51 kB04-03-99|22:13
MakeHelp.bat1.35 kB04-03-99|22:13
Plugins.cpp2.36 kB04-04-99|10:13
Plugins.h863.00 B04-03-99|10:06
ProgDlg.cpp7.36 kB04-03-99|22:28
ProgDlg.h2.78 kB04-03-99|22:28
Rangeann.cpp4.48 kB04-03-99|22:28
Rangeann.h1.97 kB04-03-99|22:28
ReadMe.txt4.11 kB04-05-99|16:21
011.00 B
resource.h2.07 kB04-08-99|10:27
idr_main.ico1.05 kB04-05-99|16:51
ImageS.ico1.05 kB04-05-99|16:51
ImageS.rc2398.00 B04-03-99|22:13
ImgDocS.ico1.05 kB04-03-99|22:13
IToolbar.bmp718.00 B04-06-99|21:26
Toolbar.bmp2.58 kB04-07-99|20:07
SrvrItem.cpp3.04 kB04-03-99|22:13
SrvrItem.h1.30 kB04-03-99|22:13
StdAfx.cpp294.00 B04-05-99|17:22
StdAfx.h1.51 kB04-05-99|17:22
UnRedoStack.cpp2.63 kB04-04-99|20:46
UnRedoStack.h1.88 kB03-18-99|10:32
VCTW32MS.LIB9.40 kB12-11-97|15:46
VGrayPic.cpp854.00 B04-04-99|19:10
VGrayPic.h997.00 B04-04-99|11:04
VIC32MS.LIB90.82 kB01-12-99|16:07
VICDEFS.H39.49 kB01-13-99|14:25
VicPict.cpp58.94 kB04-05-99|11:32
VicPict.h11.88 kB04-05-99|10:44
VPicEx.cpp11.22 kB04-03-99|22:28
VPicEx.h885.00 B04-03-99|22:28
云测速

File.cpp (125.94 kB)

需要 1 积分
您持有 积分

CodeForge积分(原CF币)全新升级,功能更强大,使用更便捷,不仅可以用来下载海量源代码马上还可兑换精美小礼品了 了解更多

您的积分不足

支付宝优惠套餐快速获取 30 积分

订单支付完成后,积分将自动加入到您的账号。以下是优惠期的人民币价格,优惠期过后将恢复美元价格。

更多付款方式:网银PayPal

上传代码,免费获取

您本次下载所消耗的积分将转交上传作者。

同一源码,30天内重复下载,只扣除一次积分。

登录 CodeForge

还没有CodeForge账号? 立即注册
关注微博
联系客服

Switch to the English version?

Yes
CodeForge 英文版
No
CodeForge 中文版

完善个人资料,获价值¥30元积分奖励!

^_^"呃 ...

Sorry!这位大神很神秘,未开通博客呢,请浏览一下其他的吧
好的