请教一个位图保存的问题??

zilili2000 2003-10-31 09:41:16
我在VC下,把.bmp文件读入
BYTE* lpBuf; //图像数据;
BITMAPINFO* pbi; //信息头;
请问,怎么序列化啊??
...全文
40 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zilili2000 2003-11-05
  • 打赏
  • 举报
回复
我的意思是说,怎么用CArchive& ar来实现位图的保存。
比如,图像的高度ar<<m_bmpHeight,
对于BYTE* lpBuf;BITMAPINFO* pbi;
ar不能序列化这种结构,该怎么办呢??
hahu 2003-11-04
  • 打赏
  • 举报
回复
hehe
memset(&fileHdr,0,sizeof(BITMAPINFOHEADER))
我不填充也没有出现什么问题
zilili2000 2003-11-04
  • 打赏
  • 举报
回复
谢谢各位了!!
我说的序列化,就是重载Serialize()。
老兄的代码很好,小弟研究以下先!!
回头一定给分。。
zilili2000 2003-11-04
  • 打赏
  • 举报
回复
谢谢各位了!!
我说的序列化,就是重载Serialize()。
老兄的代码很好,小弟研究以下先!!
回头一定给分。。
GoogleGeek 2003-11-03
  • 打赏
  • 举报
回复
continuing...........
---------------------------------------------------
/*
* Function: Draw
* Purpose¡GDraw the bitmap In the Client's DC
* Entry: [in] CDC *pDC:the client DC
* Return¡GTRUE if successful ,otherwise,FALSE
* External Ref.: NULL
*
*/
BOOL CxBitmap::Draw(CDC *pDC)
{
if(m_lpBMIH!=NULL)//windows bitmap
{
::StretchDIBits(pDC->GetSafeHdc(), 0,0,m_lpBMIH->biWidth,m_lpBMIH->biHeight,
0, 0, m_lpBMIH->biWidth,m_lpBMIH->biHeight,
m_lpImage, (LPBITMAPINFO) m_lpBMIH, DIB_RGB_COLORS, SRCCOPY);
return true;
}

if(m_lpBCIH!=NULL)//OS2 bitmap
{
::StretchDIBits(pDC->GetSafeHdc(), 0,0,m_lpBCIH->bcWidth,m_lpBCIH->bcHeight,
0, 0, m_lpBCIH->bcWidth, m_lpBCIH->bcHeight,
m_lpImage, (LPBITMAPINFO) m_lpBCIH, DIB_RGB_COLORS, SRCCOPY);
return true;
}

return false;
}

/*
* Function: ComputePaletteSize
* Purpose¡GCompulates the Color palette's size of specified Bitmap
* Entry: [in] int nBitCount:specified the bit deepth of the bitmap
* Return¡GNULL
* External Ref.: NULL
*
*/
void CxBitmap::ComputePaletteSize(int nBitCount)
{
switch(nBitCount)
{
case 1:
m_nColorTableEntries = 2;
break;
case 4:
m_nColorTableEntries = 16;
break;
case 8:
m_nColorTableEntries = 256;
break;
case 16:
case 24:
case 32:
m_nColorTableEntries = 0;
break;
default:
ASSERT(FALSE);
}
}

/*
* Function: LoadBitmapFile
* Purpose¡Gthe main interface,Load a bitmap from a file,
* Entry: NULL
* Return¡GTRUE if successful ,otherwise,FALSE
* External Ref.: NULL
*
*/
BOOL CxBitmap::LoadBitmapFile()
{
CFileDialog fileBitmapDlg(true,
"bmp",
NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"Bitmap files(*.bmp)|*.bmp",
NULL);

if (fileBitmapDlg.DoModal() != IDOK)
{
return false;
}

CString strFileName=fileBitmapDlg.GetPathName();
CFile fileBitmap;
fileBitmap.Open(strFileName, CFile::modeRead |CFile::shareDenyNone|CFile::typeBinary);

Read(&fileBitmap);

fileBitmap.Close();

return true;
}

/*
* Function: Read
* Purpose¡GRead a bitmap from a file,
* Entry: [in],CFile *pFile:the file pointer to the specified bitmap file
* Return¡GTRUE if successful ,otherwise,FALSE
* External Ref.: called in the function:LoadBitmapFile and Serialize
*
*/
BOOL CxBitmap::Read(CFile *pFile)
{
FreeAll();
BITMAPFILEHEADER bmfh;

try
{
int nCount,nSize;
nCount=pFile->Read((LPVOID)&bmfh,sizeof(BITMAPFILEHEADER));
nSize = bmfh.bfOffBits - sizeof(BITMAPFILEHEADER);

//conform BMP file
if(bmfh.bfType!=0x4d42)
throw new CFileException;

//Firstly, conform the format: windows BMP ? IBM OS2 BMP
DWORD dwSize=0;
int n=sizeof(DWORD);
pFile->Read(&dwSize,n);
this->m_bIsWindowBMP=(dwSize==40)?true:false;
//rewind
pFile->Seek(-n,CFile::current);

if(!this->m_bIsWindowBMP)//OS2
{
this->m_lpBCIH=(LPBITMAPCOREHEADER) new char[nSize];
this->m_lpvColorTable=(char*)(void*)this->m_lpBCIH+sizeof(BITMAPCOREHEADER);
nCount = pFile->Read(m_lpBCIH, nSize);
this->ComputePaletteSize(this->m_lpBCIH->bcBitCount);
m_dwSizeImage=(this->m_lpBCIH->bcWidth*this->m_lpBCIH->bcBitCount+31)/8*this->m_lpBCIH->bcHeight;
}
else//window
{
this->m_lpBMIH=(LPBITMAPINFOHEADER) new char[nSize];
this->m_lpvColorTable=(char*)(void*)this->m_lpBMIH+sizeof(BITMAPINFOHEADER);
nCount = pFile->Read(m_lpBMIH, nSize);
ComputePaletteSize(m_lpBMIH->biBitCount);
m_dwSizeImage=(this->m_lpBMIH->biWidth*this->m_lpBMIH->biBitCount+31)/8*this->m_lpBMIH->biHeight;
}

//Get the image data
m_lpImage = (LPBYTE) new char[m_dwSizeImage];
nCount = pFile->ReadHuge(m_lpImage, m_dwSizeImage);
}
catch(CFileException *pe)
{
AfxMessageBox("Failed to load the file : "+pFile->GetFileName());
pe->Delete();
return false;
}

return true;
}

/*
* Function: Write
* Purpose¡GWrite a bitmap to a file,
* Entry: [in],CFile *pFile:the file pointer to the specified bitmap file
* Return¡GTRUE if successful ,otherwise,FALSE
* External Ref.: called in the function: Serialize
*
*/
BOOL CxBitmap::Write(CFile *pFile)
{
BITMAPFILEHEADER bmfh;
bmfh.bfType = 0x4d42; // 'BM'
bmfh.bfSize = 0;//not important in here
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;

try
{

if(this->m_bIsWindowBMP)
{
int nSizeHdrWin = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_nColorTableEntries;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + nSizeHdrWin;
pFile->Write((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER));
pFile->Write((LPVOID) m_lpBMIH, nSizeHdrWin);
}
else
{
int nSizeHdrOS2=sizeof(BITMAPCOREHEADER)+sizeof(RGBTRIPLE)*m_nColorTableEntries;
bmfh.bfOffBits=sizeof(BITMAPFILEHEADER)+nSizeHdrOS2;
pFile->Write((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER));
pFile->Write((LPVOID) m_lpBCIH, nSizeHdrOS2);

}

pFile->WriteHuge((LPVOID) m_lpImage, m_dwSizeImage);
}
catch(CException* pe)
{
pe->Delete();
AfxMessageBox("write error");
return FALSE;
}

return TRUE;
}

/*
* Function: Serialize
* Purpose¡Gvirtual function,used to load and save file
* Entry: [in],CArchive& ar, a kind of binary stream,can interview with the file
* Return¡GNULL
* External Ref.: calls the function:Read and Write to undergo
*
*/
void CxBitmap::Serialize(CArchive& ar)
{
if(ar.IsStoring())
{
Write(ar.GetFile());
}
else
{
Read(ar.GetFile());
}
}
GoogleGeek 2003-11-03
  • 打赏
  • 举报
回复

/*
* Project: MPG
* Module: bitmap process
* File Name: xBitmap.cpp
* Author: Song,Ye Wen
* Start Date: June 30,2003
* Language: Microsoft Visual C++ 6.0/C for ARM-gcc
* Target: Implemention of the CxBitmap class used for bitmap inverting process
* Summary: This file contains the functions's implemention to invert bitmap file/picture
* Ref document:
* 1. << ying xiang dang bao dian---windows shizuo>> tp31-0188
*
* Change Note:
* Copyright: Copyright 2001-2003 Inventec Electronics (NanJing) Co. Ltd.
* All rights reserved.
*/

#include "stdafx.h"
#include "xBitmap.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_SERIAL(CxBitmap, CObject, 0);

/*
* Function: CxBitmap
* Purpose¡Gthe Constructor,initialize all the resources
* Entry: NULL
* Return¡GNULL
* External Ref.: NULL
*
*/
CxBitmap::CxBitmap()
{
this->m_bIsWindowBMP=true;
this->m_lpBMIH=NULL;
this->m_lpImage=NULL;
this->m_lpBCIH=NULL;
}

/*
* Function: ~CxBitmap
* Purpose¡Gthe destructor,free all the resources
* Entry: NULL
* Return¡GNULL
* External Ref.: NULL
*
*/
CxBitmap::~CxBitmap()
{
this->FreeAll();
}

/*
* Function: MakePalette
* Purpose¡Gchange the bitmap's color palette(for 1,4,8 bit) or the pixel(for 16,24,32 bit)
* Entry: NULL
* Return¡GTRUE for 1,4,8 bit bitmap ,otherwise,FALSE
* External Ref.: NULL
*
*/
BOOL CxBitmap::MakePalette()
{
if(m_nColorTableEntries == 0)
{
LPBYTE lpTemp=m_lpImage;
for(DWORD i=0;i<this->m_dwSizeImage;i++)
*lpTemp=~(*lpTemp++);
return FALSE;
}

if(!this->m_bIsWindowBMP)//OS2 bitmap
{
RGBTRIPLE *pDibQuad = (RGBTRIPLE*) m_lpvColorTable;

for(int i = 0; i < m_nColorTableEntries; i++)
{
pDibQuad->rgbtRed = ~pDibQuad->rgbtRed;
pDibQuad->rgbtGreen = ~pDibQuad->rgbtGreen;
pDibQuad->rgbtBlue = ~pDibQuad->rgbtBlue;
pDibQuad++;
}
}
else//windows bitmap
{
LPRGBQUAD pDibQuad = (LPRGBQUAD) m_lpvColorTable;

for(int i = 0; i < m_nColorTableEntries; i++)
{
pDibQuad->rgbRed = ~pDibQuad->rgbRed;
pDibQuad->rgbGreen= ~pDibQuad->rgbGreen;
pDibQuad->rgbBlue = ~pDibQuad->rgbBlue;
pDibQuad++;
}
}

return true;
}

/*
* Function: GetSizeImage
* Purpose¡GGet the size of bitmap's pixel array
* Entry: NULL
* Return¡Gthe size of bitmap's image ,in bytes
* External Ref.: NULL
*
*/
int CxBitmap::GetSizeImage()
{
return m_dwSizeImage;
}

/*
* Function: GetDimensions
* Purpose¡GGet the bitmap's dimensions:(width,height)
* Entry: NULL
* Return¡Gthe size of bitmap's dimention ,in bytes
* External Ref.: NULL
*
*/
CSize CxBitmap::GetDimensions()
{
if(m_lpBMIH == NULL&&m_lpBCIH==NULL)
return CSize(0, 0);
else if(this->m_bIsWindowBMP)
return CSize((int) m_lpBMIH->biWidth, (int) m_lpBMIH->biHeight);

return CSize((int) m_lpBCIH->bcWidth,(int)m_lpBCIH->bcHeight);
}

/*
* Function: GetSizeHeader
* Purpose¡GGet the bitmap's information of BITMAPINFOHEADR /BIMAPCOREHEADER
* Entry: NULL
* Return¡Gthe size of bitmap's information ,in bytes
* External Ref.: NULL
*
*/
int CxBitmap::GetSizeHeader()
{
if(this->m_bIsWindowBMP)
return sizeof(BITMAPINFOHEADER) +sizeof(RGBQUAD) * m_nColorTableEntries;

return sizeof(BITMAPCOREHEADER)+sizeof(RGBTRIPLE)*m_nColorTableEntries;
}

/*
* Function: FreeAll
* Purpose¡GFree all the resources ever allocated
* Entry: NULL
* Return¡GNULL
* External Ref.: called in function:Read ,Destructor
*
*/
void CxBitmap::FreeAll()
{
if(this->m_lpBMIH!=NULL)
{
delete[] this->m_lpBMIH;
this->m_lpBMIH=NULL;
}
if(this->m_lpImage!=NULL)
{
delete[] this->m_lpImage;
this->m_lpImage=NULL;
}
if(this->m_lpBCIH!=NULL)
{
delete this->m_lpBCIH;
this->m_lpBCIH=NULL;
}

this->m_bIsWindowBMP=true;
}
GoogleGeek 2003-11-03
  • 打赏
  • 举报
回复
下面是我写的一个用于bitmap颜色反色的程序,里面实现了所谓的serialize,希望对你有帮助

/*
* Project: MPG
* Module: bitmap process
* File Name: xBitmap.h
* Author: Song,Ye Wen
* Start Date: June 30,2003
* Language: Microsoft Visual C++ 6.0/C for ARM-gcc
* Target: Declare the interface of the CxBitmap class used for bitmap inverting process
* Summary: This file contains the functions interface to invert bitmap file/picture
* Ref document:
* 1. << ying xiang dang bao dian---windows shizuo>> tp31-0188
*
* Change Note:
* Copyright: Copyright 2001-2003 Inventec Electronics (NanJing) Co. Ltd.
* All rights reserved.
*/

#if !defined(AFX_XBITMAP_H__A56E79C8_1DFF_432E_93CE_788CD2627F3C__INCLUDED_)
#define AFX_XBITMAP_H__A56E79C8_1DFF_432E_93CE_788CD2627F3C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CxBitmap : public CObject
{
DECLARE_SERIAL(CxBitmap)
private:
//Common data: same to window and os2
BOOL m_bIsWindowBMP;
LPVOID m_lpvColorTable;
// the pixel buffer
LPBYTE m_lpImage;
//the bitmap size
DWORD m_dwSizeImage;
//the items of color table
int m_nColorTableEntries;
//-----------------------------------------------------------------

//Window format bitmap
LPBITMAPINFOHEADER m_lpBMIH;
//OS2 format bitmap
BITMAPCOREHEADER *m_lpBCIH;
private:
void ComputePaletteSize(int nBitCount);
void FreeAll();
public:
CxBitmap();
virtual ~CxBitmap();
// can read /write automaticly
void Serialize(CArchive& ar);

// get the bitmap's property
int GetSizeHeader();
CSize GetDimensions();
int GetSizeImage();

//the interface of UI
BOOL LoadBitmapFile();
BOOL MakePalette();
BOOL Draw(CDC* pDC);
BOOL Write(CFile *pFile);
BOOL Read(CFile *pFile);
};
#endif // !defined(AFX_XBITMAP_H__A56E79C8_1DFF_432E_93CE_788CD2627F3C__INCLUDED_)
GoogleGeek 2003-11-01
  • 打赏
  • 举报
回复
very easy
I can give the source code next week
adamchao 2003-11-01
  • 打赏
  • 举报
回复
to hahu
filehdr当中的内容要填充一下
hahu 2003-10-31
  • 打赏
  • 举报
回复

应该是WriteHuge(lpBuf,lLineBytes*lHeight)

上面没有用到调色板
如果有调色板,则看看支持几种颜色
如果16色
BITMAPINFOHEADER 后面就有16个RGBQUAD
后面的WriteHuge(lpBuf,lWidth*lHeight)
hahu 2003-10-31
  • 打赏
  • 举报
回复
CFile file;
file.Open("C:\HELLO.bmp",CFile::modeCreate|Cfile::modeWrite);
BITMAPFILEHEADER fileHdr;
fileHdr.

...
file.Write(&filehdr,sizeof(BITMAPFILEHEADER));
file.Write(&infohdr.sizeof(BITMAPINFOHEADER));
file.WriteHuge(&lpBuf,sizeof(lLineBytes*lHeight));
//lLineBytes是每行的字节数,要求整除于4,如果256宽的RGB真彩色图像
//lLineBytes=3*lWidth


loveisbug 2003-10-31
  • 打赏
  • 举报
回复
??
zzwu 2003-10-31
  • 打赏
  • 举报
回复
“序列化”的含义是 --- ?

4,445

社区成员

发帖
与我相关
我的任务
社区描述
图形图像/机器视觉
社区管理员
  • 机器视觉
  • 迪菲赫尔曼
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧