[cairo] how to import .BMP without other libs(GTK+ etc)?
伯 张
zhizhonghe at yahoo.com.cn
Tue Feb 3 20:19:03 PST 2009
Hello everyone!
As we know, cairo lib don’t support the creation of a surface from BMP directly.
we could do it with GTK+. However, is there some other way to import BMP or dib
in cairo without the suppot of other libs?
I had tried to do it whit cairo_image_surface_create_for_data, but failed L.
I could not see the right image.
Do you have any solution?
Thanks~
That is my attempt below:
//===========================================================================
// Summary:
// BMPReader class
// Header:
// BMPReader.h
// Remarks:
// Readin BMP & create cairo surface
//===========================================================================
#ifndef __BMPREADER_H__
#define __BMPREADER_H__
//===========================================================================
#include <fstream>
#include <iostream>
//===========================================================================
// Struct Member Alignment
// default 8 bytes
// here 1 byte alignment
#pragma pack(1)
//===========================================================================
using namespace std;
//===========================================================================
// Struct BMPFormat
//===========================================================================
// BMP include file head and info head
struct BMPFormat
{
short Head_Flag; //1 00-01
int File_Size; //2 02-05
int Reserved; //3 06-09
int Data_Offset; //4 0A-0D
int Head_Info_Size; //5 0E-11
int Pic_Width; //6 12-15
int Pic_High; //7 16-19
short Pic_Plans; //8 1A-1B
short Color_Bit; //9
int Compress; //10
int Data_Size; //11
int Pic_Xppm; //12
int Pic_Yppm; //13
int Color_Used; //14
int Color_Imp; //15
};
//===========================================================================
// class BMPReader
//===========================================================================
class BMPReader
{
public:
BMPReader(const char *pstrFName);
~BMPReader();
protected:
BMPReader(){};
public:
unsigned char * GetData() const;
cairo_format_t GetCairoFormat() const;
int GetHeight() const;
int GetWidth() const;
int GetRowsStride() const;
private:
ifstream * m_pfBmp;
BMPFormat * m_pBMPFormat;
unsigned char * m_pData;
};
//===========================================================================
// resume default struct alignment
#pragma pack(8)
//===========================================================================
#endif // __BMPREADER_H__
//===========================================================================
// Summary:
// BMPReader class
// Usage:
// BMPReader.cpp
// Remarks:
// Readin BMP & create cairo surface
//===========================================================================
#include "stdafx.h"
#include "BMPReader.h"
//===========================================================================
BMPReader::BMPReader( const char *pstrFName)
{
// menbers' initialization
m_pfBmp = NULL;
m_pData = NULL;
m_pBMPFormat = NULL;
assert(pstrFName != NULL);
// new ifstream
m_pfBmp = new ifstream;
m_pfBmp->open(pstrFName, ios_base::binary);
if (!m_pfBmp)
{
cout << pstrFName << " can not be opened !"<< endl;
return;
}
// readin BMPFormat 0x00-0x35
m_pBMPFormat = new BMPFormat;
m_pfBmp->seekg(0, ios_base::beg);
m_pfBmp->read((char *)m_pBMPFormat, sizeof(BMPFormat));
// readin Data 0x36-Data_Size
m_pData = new unsigned char[m_pBMPFormat->Data_Size];
m_pfBmp->seekg(0x36, ios_base::beg);
m_pfBmp->read((char *)m_pData, m_pBMPFormat->Data_Size);
// close file
m_pfBmp->close();
}
cairo_format_t BMPReader::GetCairoFormat() const
{
if (m_pBMPFormat)
{
switch (m_pBMPFormat->Color_Bit)
{
case 32: return CAIRO_FORMAT_ARGB32;
case 24: return CAIRO_FORMAT_RGB24;
case 8: return CAIRO_FORMAT_A8;
case 1: return CAIRO_FORMAT_A1;
default: cout << "Colorbit " << m_pBMPFormat->Color_Bit << " is unsupported !" << endl;
}
}
// 函数序号改写
// 返回值只表示状态
}
int BMPReader::GetHeight() const
{
if (m_pBMPFormat)
{
return m_pBMPFormat->Pic_High;
}
return -1;
}
int BMPReader::GetWidth() const
{
if (m_pBMPFormat)
{
return m_pBMPFormat->Pic_Width;
}
return -1;
}
int BMPReader::GetRowsStride() const
{
if (m_pBMPFormat)
{
// stride的大小由Cairo根据width定制
return cairo_format_stride_for_width(GetCairoFormat(), GetWidth());
}
return -1;
}
unsigned char * BMPReader::GetData() const
{
if (m_pData)
{
return m_pData;
}
return 0;
}
BMPReader::~BMPReader()
{
if (m_pBMPFormat)
{
delete m_pBMPFormat;
m_pBMPFormat = NULL;
}
if (m_pData)
{
delete m_pData;
m_pData = NULL;
}
if (m_pfBmp)
{
delete m_pfBmp;
m_pfBmp = NULL;
}
}
//---------------------------------------------------------------------
// main
//---------------------------------------------------------------------
char * pBmpName = "rgb.bmp";
BMPReader bmpRdr(pBmpName);
cairo_format_t cftBMPFormat = bmpRdr.GetCairoFormat();
unsigned char * pcBMPData = bmpRdr.GetData();
int nBMPHeight = bmpRdr.GetHeight();
int nBMPWidth = bmpRdr.GetWidth();
int nBMPStride = bmpRdr.GetRowsStride();
pImage = cairo_image_surface_create_for_data(
pcBMPData,
cairo_format_t(1),
nBMPWidth,
nBMPHeight,
nBMPStride);
___________________________________________________________
好玩贺卡等你发,邮箱贺卡全新上线!
http://card.mail.cn.yahoo.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cairographics.org/archives/cairo/attachments/20090204/a750bb25/attachment-0001.html
More information about the cairo
mailing list