📄 cbitmap.cpp
字号:
#include "stdio.h"
#include "cbitmap.h"
#include "cdc.h"
#include "ugl\ext\jpeg\ugljpeg.h"
#include "CWMLHelp.h"
//##ModelId=40501B860221
CBitmap::CBitmap()
{
CWMLHelp::RegisterClass("CBitmap" , (int)this);
uglDriverFind (UGL_DISPLAY_TYPE, 0, (UGL_UINT32 *)&m_uglDevId);
m_uglDDB_ID = UGL_NULL;
}
CBitmap::CBitmap(CBitmap *bitmap)
{
CWMLHelp::RegisterClass("CBitmap" , (int)this);
uglDriverFind (UGL_DISPLAY_TYPE, 0, (UGL_UINT32 *)&m_uglDevId);
m_uglDDB_ID = bitmap->m_uglDDB_ID;
bitmap->m_uglDDB_ID = UGL_NULL;
}
//##ModelId=40501B860222
CBitmap::~CBitmap()
{
CWMLHelp::UnRegisterClass("CBitmap" , (int)this);
DeleteObject();
}
//##ModelId=40514E48005C
void CBitmap::SetDDBID(UGL_DDB_ID dibId)
{
m_uglDDB_ID = dibId;
}
//##ModelId=40514E480098
UGL_DDB_ID CBitmap::GetDDBID()
{
return m_uglDDB_ID;
}
const CBitmap & CBitmap::operator=(CBitmap &right)
{
uglDriverFind (UGL_DISPLAY_TYPE, 0, (UGL_UINT32 *)&m_uglDevId);
m_uglDDB_ID = right.m_uglDDB_ID;
right.m_uglDDB_ID = UGL_NULL;
return *this;
}
//##ModelId=40501B86022B
bool CBitmap::CreateCompatibleBitmap(int nWidth, int nHeight)
{
UGL_DIB imageDib =
{
nWidth, /* width */
nHeight, /* height */
nWidth, /* stride */
UGL_DIRECT , /* imageFormat */
UGL_ARGB8888, /* colorFormat */
0, /* clutSize */
UGL_NULL, /* pClut */
UGL_NULL /* pImage */
};
//初始化位图,底色是黑色
m_uglDDB_ID = uglBitmapCreate(m_uglDevId , &imageDib , UGL_DIB_INIT_VALUE , 0 , UGL_DEFAULT_MEM);
if(m_uglDDB_ID == UGL_NULL)
return false;
return true;
}
//##ModelId=40501B86022F
bool CBitmap::CreateBitmapIndirect(LPBITMAP lpBitmap)
{
UGL_COLOR_FORMAT colorFormat;
if(lpBitmap->bmBitsPixel == 16)
colorFormat = UGL_RGB565;
else if(lpBitmap->bmBitsPixel == 32)
colorFormat = UGL_ARGB8888;
if(lpBitmap->bmBitsPixel == 16 || lpBitmap->bmBitsPixel == 32){
UGL_DIB imageDib =
{
lpBitmap->bmWidth, /* width */
lpBitmap->bmHeight, /* height */
lpBitmap->bmWidth, /* stride */
UGL_DIRECT, /* imageFormat */
colorFormat, /* colorFormat */
0, /* clutSize */
UGL_NULL, /* pClut */
lpBitmap->bmBits /* pImage */
};
//初始化位图
m_uglDDB_ID = uglBitmapCreate(m_uglDevId , &imageDib , UGL_DIB_INIT_DATA , UGL_NULL , UGL_DEFAULT_MEM);
if(m_uglDDB_ID == UGL_NULL)
return false;
}else if (lpBitmap->bmBitsPixel == 1){
UGL_MDIB imageDib =
{
lpBitmap->bmWidth, /* width */
lpBitmap->bmHeight, /* height */
lpBitmap->bmWidth, /* stride */
(UGL_UINT8 *)lpBitmap->bmBits /* pImage */
};
m_uglDDB_ID = uglMonoBitmapCreate(m_uglDevId , &imageDib , UGL_DIB_INIT_DATA , UGL_NULL , UGL_DEFAULT_MEM);
if(m_uglDDB_ID == UGL_NULL)
return false;
}else
assert(false);
return true;
}
//##ModelId=40501B860234
bool CBitmap::LoadFromFile(const char* fileName)
{
FILE *fp;
if((fp = fopen(fileName,"r")) == UGL_NULL){
printf("can't open the %s!\n",fileName);
return false;
}
UGL_JPEG_ID jpegId = uglJpegInit(m_uglDevId , UGL_NULL);
// UGL_JPEG_ID jpegId = uglJpegInit(GetDevId() , UGL_NULL);
if(jpegId == UGL_NULL){
printf("can not init jpeg function!\n");
return false;
}
if(uglJpegToDDBFromFile(jpegId , fp, &m_uglDDB_ID , UGL_NULL , 0 , 0) != UGL_STATUS_OK){
printf("can not converto to ddb!\n");
fclose(fp);
return false;
}
fclose(fp);
return true;
}
//##ModelId=40501B860236
bool CBitmap::CreateBitmap(int nWidth, int nHeight, int nPlanes, int nBitcount, const void* lpBits)
{
BITMAP bitmap =
{
0,
nWidth,
nHeight,
nWidth * nBitcount / 8,
1,
nBitcount,
(void *)lpBits
};
return CreateBitmapIndirect(&bitmap);
}
int CBitmap::GetBitmap( BITMAP* pBitMap )
{
if(pBitMap == UGL_NULL || m_uglDDB_ID == UGL_NULL)
return 0;
uglBitmapSizeGet(m_uglDDB_ID , &pBitMap->bmWidth , &pBitMap->bmHeight);
UGL_DIB *pDib = new UGL_DIB;
pDib->imageFormat = UGL_DIRECT;
pDib->colorFormat = UGL_DEVICE_COLOR_32;
pDib->width = pBitMap->bmWidth;
pDib->height = pBitMap->bmHeight;
pDib->stride = pDib->width;
pDib->pImage = new UGL_COLOR[pDib->width * pDib->height];
uglBitmapRead(m_uglDevId , m_uglDDB_ID , 0 , 0 , pBitMap->bmWidth - 1 , pBitMap->bmHeight - 1 , pDib , 0 , 0);
pBitMap->bmBits = (void *)pDib->pImage;
pBitMap->bmBitsPixel = 32;
pBitMap->bmWidthBytes = pBitMap->bmBitsPixel/8 * pBitMap->bmWidth;
delete pDib;
return 1;
}
bool CBitmap::LoadBitmapPattern(int pattern)
{
UGL_MDIB patternDib;
UGL_LOCAL struct
{
int width;
int height;
unsigned char data[32];
} patternData =
{
16, 16,
{
0xFF, 0xFF, /* a brick pattern */
0x00, 0x01,
0x00, 0x01, /* 8 pixels per byte */
0x00, 0x01,
0x00, 0x01,
0x00, 0x01,
0x00, 0x01,
0x00, 0x01,
0xFF, 0xFF,
0x01, 0x00,
0x01, 0x00,
0x01, 0x00,
0x01, 0x00,
0x01, 0x00,
0x01, 0x00,
0x01, 0x00
}
};
patternDib.width = patternDib.stride = patternData.width;
patternDib.height = patternData.height;
patternDib.pImage = patternData.data;
m_uglDDB_ID = uglMonoBitmapCreate(m_uglDevId, &patternDib,
UGL_DIB_INIT_DATA, 0, UGL_NULL);
if(m_uglDDB_ID == NULL)
return false;
return true;
}
bool CBitmap::GetBitmapSize(int *width , int *height)
{
uglBitmapSizeGet(m_uglDDB_ID , width , height);
}
bool CBitmap::DeleteObject()
{
if(m_uglDDB_ID != UGL_NULL)
uglBitmapDestroy(m_uglDevId,m_uglDDB_ID);
m_uglDDB_ID = UGL_NULL;
return true;
}
bool CBitmap::LoadBmpFile(char *strBMPFileName)
{
short bfType;
FILE *fp;
short color_depth;
if((fp=fopen(strBMPFileName,"rb"))==NULL)
{
printf("Cannot open %s file\n",strBMPFileName);
return false; /*如果打开文件失败则图像大小为0,数据指向为0*/
}
fread (&bfType,2,1,fp); /*位图文件头*/
if(bfType != 0x4D42) /*若文件数字签名不是BM则为打开失败*/
{
printf("%s is not bmp file\n",strBMPFileName);
fclose(fp);
return false;
}
fseek(fp,28,SEEK_SET);
fread(&color_depth,2,1,fp); /*检查色深*/
fclose(fp);;
if((color_depth == 8))
{
//256色的bmp图片
return Load256BmpFile(strBMPFileName);
}else if(color_depth == 24){
//24位的bmp图片
return Load24BmpFile(strBMPFileName);
}else{
/*若色深不对也为打开失败*/
printf("%s is not compatible bmp file\n",strBMPFileName);
fclose(fp);
return false;
}
}
bool CBitmap::Load256BmpFile(const char *strFileName)
{
FILE *fp;
if((fp = fopen(strFileName , "r")) == 0)
{
printf("can not open file.");
return false;
}
int width , height;
fseek(fp , 18 , SEEK_SET);
fread(&width , 4 , 1, fp);
fread(&height , 4 , 1, fp);
UGL_RGB *imageClut = new UGL_RGB[256];
fseek(fp , 54 , SEEK_SET);
int i;
for(i = 0; i < 256;i++)
{
UINT8 r , g ,b , alpha;
fread(&b , 1, 1 , fp);
fread(&g , 1, 1 , fp);
fread(&r , 1, 1 , fp);
fread(&alpha , 1, 1 , fp);
imageClut[i] = UGL_MAKE_ARGB(alpha , r , g, b);
}
//fread(imageClut , 4 , 256 , fp);
//整个图片的缓存
UINT32 * colorData = new UINT32[width * height];
fseek(fp , - width , SEEK_END);
for(i = 0; i < height;i ++ )
{
int j;
for(j = 0;j < width;j++)
{
UINT8 index;
fread(&index , 1 , 1 , fp);
colorData[i *width + j] = imageClut[index];
}
fseek(fp , - width * 2 , SEEK_CUR);
}
UGL_DIB transDib;
transDib.pImage = colorData;
transDib.colorFormat = UGL_ARGB8888;
transDib.clutSize = 0;
transDib.pClut = NULL;
transDib.imageFormat = UGL_DIRECT;
transDib.width = width;
// transDib.stride = transDib.width + (transDib.width%4)?(4-transDib.width%4):0; /*宽度修正值*/
transDib.stride = transDib.width ;
transDib.height = height;
if((m_uglDDB_ID = uglBitmapCreate(m_uglDevId, &transDib, UGL_DIB_INIT_DATA, 0,
UGL_NULL)) == NULL){
fclose(fp);
delete[] imageClut;
delete[] colorData;
return false;
}
fclose(fp);
delete[] imageClut;
delete[] colorData;
return true;
}
bool CBitmap::Load24BmpFile(const char *strFileName)
{
FILE *fp;
if((fp = fopen(strFileName , "r")) == 0)
{
printf("can not open file.");
return false;
}
int width , height;
fseek(fp , 18 , SEEK_SET);
fread(&width , 4 , 1, fp);
fread(&height , 4 , 1, fp);
//整个图片的缓存
UINT32 * colorData = new UINT32[width * height];
fseek(fp , - width * 3 , SEEK_END);
int i;
for(i = 0; i < height;i ++ )
{
int j;
for(j = 0; j < width ;j++)
{
UINT8 r, g ,b;
fread(&b , 1 , 1, fp);
fread(&g , 1 , 1, fp);
fread(&r , 1 , 1, fp);
colorData[i *width + j] = UGL_MAKE_ARGB(0xff ,r ,g , b);
}
fseek(fp , - width * 2 * 3 , SEEK_CUR);
}
UGL_DDB_ID ddbBitmapId;
UGL_DIB transDib;
transDib.pImage = colorData;
transDib.colorFormat = UGL_ARGB8888;
transDib.clutSize = 0;
transDib.pClut = NULL;
transDib.imageFormat = UGL_DIRECT;
transDib.width = width;
// transDib.stride = transDib.width + (transDib.width%4)?(4-transDib.width%4):0; /*宽度修正值*/
transDib.stride = transDib.width;
transDib.height = height;
if((m_uglDDB_ID = uglBitmapCreate(m_uglDevId, &transDib, UGL_DIB_INIT_DATA, 0,
UGL_NULL)) == NULL){
fclose(fp);
delete[] colorData;
return false;
}
fclose(fp);
delete[] colorData;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -