bmptotiffdlg.cpp
来自「BMP 格式的图片 转为Tiff格式的图片」· C++ 代码 · 共 450 行
CPP
450 行
// BmpToTiffDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BmpToTiff.h"
#include "BmpToTiffDlg.h"
#include "TiffStruct.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBmpToTiffDlg dialog
CBmpToTiffDlg::CBmpToTiffDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBmpToTiffDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBmpToTiffDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBmpToTiffDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBmpToTiffDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBmpToTiffDlg, CDialog)
//{{AFX_MSG_MAP(CBmpToTiffDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOPEN, OnOpen)
ON_BN_CLICKED(IDCOVERT, OnCovert)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBmpToTiffDlg message handlers
BOOL CBmpToTiffDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// m_bmphr = 0;
// m_bmpnf = 0;
m_pDIB = NULL;
m_pPalette = NULL;
return TRUE; // return TRUE unless you set the focus to a control
}
void CBmpToTiffDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CBmpToTiffDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CBmpToTiffDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBmpToTiffDlg::OnOpen()
{
// TODO: Add your control notification handler code here
CString filename;
char szFilter[] = "Bmp Files(*.bmp)|*.bmp|All Files|*.*||";
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter);
if(dlg.DoModal()==IDOK)
{
filename = dlg.GetPathName();
}
CFile file;
if(!file.Open(filename, CFile::modeRead))
return;
//读入数据
file.Read(&m_bmphr, sizeof(BITMAPFILEHEADER));
file.Read(&m_bmpnf, sizeof(BITMAPINFOHEADER));
if(m_bmphr.bfType != 19778)
{
AfxMessageBox("非位图文件!");
return;
}
m_pDIB = new BYTE[m_bmphr.bfSize - m_bmphr.bfOffBits];
if(m_bmpnf.biBitCount!=24)//如果不是24位图,则先读入颜色表
{
m_pPalette = new RGBQUAD[m_bmpnf.biClrUsed];
memset(m_pPalette, 0, sizeof(RGBQUAD)*m_bmpnf.biClrUsed);
file.Read(m_pPalette, sizeof(RGBQUAD)*m_bmpnf.biClrUsed);
}
else
{
m_pPalette = NULL;
}
file.Read(m_pDIB, m_bmphr.bfSize - m_bmphr.bfOffBits);//读入图像数据
}
void CBmpToTiffDlg::OnCovert()
{
// TODO: Add your control notification handler code here
int i = 0;
int j = 0;//循环变量
CString filename;//存储文件名
CFileDialog dlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, NULL, NULL);
if(dlg.DoModal() == IDOK)
{
filename = dlg.GetPathName();
}
CFile file;//将所有数据写入file文件
if(!file.Open(filename, CFile::modeCreate|CFile::modeWrite))
return;
CFile *f = &file;
DWORD imgData = m_bmphr.bfSize - m_bmphr.bfOffBits;
IFH tiffIFH;//tiff图像文件头信息
//填充IFH
tiffIFH.ByteOrder = 'II';
tiffIFH.Version = 42;
tiffIFH.OffsetToIFD = imgData + 8;//图像数据放在tiff文件头和tiff文件目录描述之间
f->Write(&tiffIFH, sizeof(IFH));//将图像文件头写入文件
WORD DEC;
if(m_bmpnf.biBitCount==24)
{
DEC = 15;//DEC:目录入口数
}
else
{
DEC =15;//16 索引图多添加一个目录存放索引表
}
DE *tiffDE = new DE[DEC];//tiff图像目录入口,欲定义dec个属性
//填充DE
tiffDE[0].Tag = 254; //NewSubfileType
tiffDE[0].Type = 4; //LONG
tiffDE[0].Length = 1;
tiffDE[0].ValueOffset = 0;
tiffDE[1].Tag = 256; //ImageWidth
tiffDE[1].Type = 3; //SHORT
tiffDE[1].Length = 1;
tiffDE[1].ValueOffset = m_bmpnf.biWidth;
tiffDE[2].Tag = 257; //ImageHeight
tiffDE[2].Type = 3; //SHORT
tiffDE[2].Length = 1;
tiffDE[2].ValueOffset = m_bmpnf.biHeight;
tiffDE[3].Tag = 258; //BitPerSample
tiffDE[3].Type = 3; //SHORT
if(m_bmpnf.biBitCount == 24)
{
tiffDE[3].Length = 3;
tiffDE[3].ValueOffset = 8 + imgData + 2 + DEC*12;//tiff文件组织:IFH-ImgData-DEC-DE-VaOfSt>4
}
else
{
tiffDE[3].Length = 1;
tiffDE[3].ValueOffset = 8;//8位索引图
}
tiffDE[4].Tag = 259;//Compression
tiffDE[4].Type = 3;
tiffDE[4].Length = 1;
tiffDE[4].ValueOffset = 1;//not compression
tiffDE[5].Tag = 262;//PhotometricInterpretation
tiffDE[5].Type = 3;
tiffDE[5].Length = 1;
if(m_bmpnf.biBitCount==24)
tiffDE[5].ValueOffset = 2;//RGB图取2
else
tiffDE[5].ValueOffset = 1;//索引图取3,灰度图取1或者0,1:黑色是0,0:白色是0,即图像采用反色显示
tiffDE[6].Tag = 273;//StripOffset
tiffDE[6].Type = 4;
tiffDE[6].Length = 1;
tiffDE[6].ValueOffset = 8;
tiffDE[7].Tag = 277;//SamplesPerPixel
tiffDE[7].Type = 3;
if(m_bmpnf.biBitCount==24)
{
tiffDE[7].ValueOffset = 3;
}
else
{
tiffDE[7].ValueOffset = 1;
}
tiffDE[7].Length = 1;
tiffDE[8].Tag = 278;//RowsPerStrip
tiffDE[8].Type = 3;
tiffDE[8].Length = 1;
tiffDE[8].ValueOffset = m_bmpnf.biHeight;
tiffDE[9].Tag = 279;//StripByteCounts
tiffDE[9].Type = 4;
tiffDE[9].Length = 1;
tiffDE[9].ValueOffset = imgData;
tiffDE[10].Tag = 282;//XResolution
tiffDE[10].Type = 5;
tiffDE[10].Length = 1;
if(m_bmpnf.biBitCount==24)
{
tiffDE[10].ValueOffset = 8 + imgData + 2 + DEC*12 + 3*16;//3个SHORT
}
else
{
tiffDE[10].ValueOffset = 8 + imgData + 2 + DEC*12;
}
tiffDE[11].Tag = 283;//YResolution
tiffDE[11].Type = 5;
tiffDE[11].Length = 1;
if(m_bmpnf.biBitCount==24)
{
tiffDE[11].ValueOffset = 8 + imgData + 2 + DEC*12 + 3*16 + 8;//3个SHORT + XResolution
}
else
{
tiffDE[11].ValueOffset = 8 + imgData + 2 + DEC*12 + 8;
}
tiffDE[12].Tag = 284;//PlanarConfiguration 图像数据的平面排列方式
tiffDE[12].Type = 3;
tiffDE[12].Length = 1;
tiffDE[12].ValueOffset = 1;
tiffDE[13].Tag = 296;//ResolutionUnit
tiffDE[13].Type = 3;
tiffDE[13].Length = 1;
tiffDE[13].ValueOffset = 2; //英寸
tiffDE[14].Tag = 317;//Predictor
tiffDE[14].Type = 3;
tiffDE[14].Length = 1;
tiffDE[14].ValueOffset = 1;
//填充第16个DE
/* if(m_bmpnf.biBitCount == 8)
{
tiffDE[15].Tag = 320;//Palette
tiffDE[15].Type = 3;//SHORT
tiffDE[15].Length = 3*256;
tiffDE[15].ValueOffset = 8 + imgData + 2 + DEC *12;
}
*/
//将图像数据写进文件,需要将图像数据倒置
BYTE *imgTemp = new BYTE[imgData];
if(m_bmpnf.biBitCount == 24)
{
for(i=0; i<m_bmpnf.biHeight-1; i++)
{
for(j=0; j<m_bmpnf.biWidth-1; j++)
{
imgTemp[i*3*m_bmpnf.biWidth + j*3] = m_pDIB[(m_bmpnf.biHeight - 1- i)*3*m_bmpnf.biWidth + 3*j + 2];
imgTemp[i*3*m_bmpnf.biWidth + j*3 + 1] = m_pDIB[(m_bmpnf.biHeight - 1- i)*3*m_bmpnf.biWidth + 3*j + 1];
imgTemp[i*3*m_bmpnf.biWidth + j*3 + 2] = m_pDIB[(m_bmpnf.biHeight - 1- i)*3*m_bmpnf.biWidth + 3*j + 0];
}
}
}
else //8位图
{
for(i=0; i<m_bmpnf.biHeight-1; i++)
{
for(j=0; j<m_bmpnf.biWidth-1; j++)
{
imgTemp[i*m_bmpnf.biWidth + j] = m_pDIB[(m_bmpnf.biHeight - 1 - i)*m_bmpnf.biWidth + j];
}
}
}
//写入图像数据
f->Write(imgTemp, imgData);
delete []imgTemp;
imgTemp = NULL;
//将目录入口数写入文件
f->Write(&DEC, sizeof(WORD));
//将各个DE写进文件
for(i=0; i<DEC; i++)
f->Write(&tiffDE[i],sizeof(DE));
//将大于4字节的valueoffset写入文件
if(m_bmpnf.biBitCount == 24)
{
SHORT *pBitPerSample = new SHORT[3];
pBitPerSample[0] = pBitPerSample[1] = pBitPerSample[2] = 8;
f->Write(pBitPerSample, 3*16);//3*sizeof(SHORT));
delete []pBitPerSample;
pBitPerSample = NULL;
}
LONG XResolution[2];
XResolution[0] = 0;//m_bmpnf.biXPelsPerMeter;
XResolution[1] = 0;//39.37;//1 meter = 39.37 inch
f->Write(XResolution, 32*2);//sizeof(LONG)*2);
LONG YResolution[2];
YResolution[0] = 0;//m_bmpnf.biYPelsPerMeter;
YResolution[1] = 0;//39.37;
f->Write(YResolution, 32*2);//sizeof(LONG)*2);
delete []m_pDIB; //回收内存
m_pDIB = NULL;
file.Close();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?