#include "stdafx.h"
#include "math.h"
#include "DIB.h"
#define BIG_DISTANCE 10000000L
#define DIST(r1,g1,b1,r2,g2,b2) \
(long) (3L*(long)((r1)-(r2))*(long)((r1)-(r2)) + \
4L*(long)((g1)-(g2))*(long)((g1)-(g2)) + \
2L*(long)((b1)-(b2))*(long)((b1)-(b2)))
static unsigned char masktable[] = { 0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01 };
CDIB::CDIB(HANDLE hDib,int nBits)
{
m_pVoid = NULL;
m_pLinePtr = NULL;
m_bUseGamma=FALSE;
width=height=0;
if(hDib)
{
CreateFromHandle(hDib,nBits);
}
}
CDIB::~CDIB()
{
DestroyDIB();
}
void CDIB::DestroyDIB()
{
if(m_pVoid) free(m_pVoid);
m_pVoid = NULL;
if(m_pLinePtr) free(m_pLinePtr);
m_pLinePtr = NULL;
}
BOOL CDIB::Create(int width,int height,int bits)
{
/*
Free existing image
*/
DestroyDIB();
// ASSERT(bits == 24 || bits == 8);
BITMAPINFOHEADER bmInfo;
memset(&bmInfo,0,sizeof(BITMAPINFOHEADER));
bmInfo.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.biWidth = width;
bmInfo.biHeight = height;
bmInfo.biPlanes = 1;
bmInfo.biBitCount = bits;
bmInfo.biCompression = BI_RGB;
return Create(bmInfo);
}
BOOL CDIB::Create(BITMAPINFOHEADER& bmInfo)
{
bytes = (bmInfo.biBitCount*bmInfo.biWidth)>>3;
height = bmInfo.biHeight;
width = bmInfo.biWidth;
// bmInfo.biHeight *= -1;
while(bytes%4) bytes++;
int size;
size = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*GetPaletteSize(bmInfo) + bytes*height;
m_pVoid = (void *)malloc(size);
if(!m_pVoid) return FALSE;
m_pInfo = (PBITMAPINFO )m_pVoid;
memcpy((void *)&m_pInfo->bmiHeader,(void *)&bmInfo,sizeof(BITMAPINFOHEADER));
m_pRGB = (RGBQUAD *)((unsigned char *)m_pVoid + sizeof(BITMAPINFOHEADER)) ;
m_pBits = (unsigned char *)(m_pVoid) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*GetPaletteSize();
int i;
BYTE **ptr;
m_pLinePtr = (BYTE **)malloc(sizeof(BYTE *)*height);
if(!m_pLinePtr) return FALSE;
for(i=0,ptr=m_pLinePtr; i < height; i++,ptr++)
{
//*ptr = (int)(m_pBits)+(i*bytes);
//*ptr = (int)GetLinePtr(i);
*ptr = m_pBits + (height-i-1)*bytes;
}
m_nFlags = 0;
return TRUE;
}
void CDIB::SetPalette(unsigned char *palette)
{
int i,size;
RGBQUAD *rgb;
if(!palette) return;
size = GetPaletteSize();
for(i=0,rgb = m_pRGB; i < size; i++,rgb++,palette+=3)
{
if(m_bUseGamma)
{
rgb->rgbRed = Gamma[palette[0]];
rgb->rgbGreen = Gamma[palette[1]];
rgb->rgbBlue = Gamma[palette[2]];
}
else
{
rgb->rgbRed = palette[0];
rgb->rgbGreen = palette[1];
rgb->rgbBlue = palette[2];
}
rgb->rgbReserved = (BYTE)0;
}
}
void CDIB::SetPalette(RGBQUAD *pRGB)
{
int size;
if(!pRGB) return;
size = GetPaletteSize();
memcpy(m_pRGB,pRGB,size*sizeof(RGBQUAD));
}
int CDIB::GetPaletteSize()
{
return GetPaletteSize(m_pInfo->bmiHeader);
}
int CDIB::GetPaletteSize(BITMAPINFOHEADER& bmInfo)
{
switch(bmInfo.biBitCount)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
default:
return 0;
}
}
void CDIB::SetPixel(int x,int y,COLORREF color)
{
unsigned char *ptr;
ASSERT(x >= 0 && y >=0);
ASSERT(x < width && y < height);
// ptr = m_pBits + (y*bytes) + x * 3;
ptr = (unsigned char *)m_pLinePtr[y];
ptr += x*3;
*ptr++ = (unsigned char)GetBValue(color);
*ptr++ = (unsigned char)GetGValue(color);
*ptr++ = (unsigned char)GetRValue(color);
}
void CDIB::SetPixel8(int x,int y,unsigned char color)
{
unsigned char *ptr,*aptr;
ASSERT(x >= 0 && y >=0);
ASSERT(x < width && y < height);
// ptr = m_pBits + (y*bytes) + x ;
// ptr = (unsigned char *)m_pLinePtr[y] ;
ptr = GetLinePtr(y);
aptr = ptr;
ptr += x;
*ptr = color;
}
COLORREF CDIB::GetPixel(int x,int y)
{
unsigned char *ptr;
COLORREF color;
if(x < 0 || y < 0) return 0;
if(x >= width || y >= height) return 0;
// ptr = m_pBits + (y*bytes) + x * 3;
ptr = GetLinePtr(y);
ptr += (x*3);
color = RGB(*(ptr+2),*(ptr+1),*ptr);
return color;
}
CBitmap *CDIB::GetTempBitmap(CDC& dc)
{
HBITMAP hBitmap;
CBitmap *temp;
ASSERT(m_pVoid != NULL);
hBitmap = CreateDIBitmap(dc.m_hDC,
(PBITMAPINFOHEADER)m_pInfo,
CBM_INIT,
(const void *)m_pBits,
m_pInfo,
DIB_RGB_COLORS);
if(hBitmap == NULL) return NULL;
temp = CBitmap::FromHandle(hBitmap);
return temp;
}
CBitmap *CDIB::GetBitmap(CDC& dc)
{
HBITMAP hBitmap;
CBitmap *temp;
ASSERT(m_pVoid != NULL);
hBitmap = CreateDIBitmap(dc.m_hDC,
(PBITMAPINFOHEADER)m_pInfo,
CBM_INIT,
(const void *)m_pBits,
m_pInfo,
DIB_RGB_COLORS);
if(hBitmap == NULL) return NULL;
temp = CBitmap::FromHandle(hBitmap);
if(temp)
{
BITMAP bmp;
LPVOID lpVoid;
temp->GetBitmap(&bmp);
lpVoid = malloc(bmp.bmWidthBytes*bmp.bmHeight);
if(!lpVoid) return NULL;
temp->GetBitmapBits(bmp.bmWidthBytes*bmp.bmHeight,lpVoid);
CBitmap *newBmp = new CBitmap;
newBmp->CreateBitmapIndirect(&bmp);
newBmp->SetBitmapBits(bmp.bmWidthBytes*bmp.bmHeight,lpVoid);
free(lpVoid);
return newBmp;
}
else return NULL;
}
void CDIB::CopyLine(int source,int dest)
{
unsigned char *src,*dst;
ASSERT(source <= height && source >= 0);
ASSERT(dest <= height && dest >= 0);
if(source == dest) return;
src = GetLinePtr(source);
dst = GetLinePtr(dest);
memcpy(dst,src,bytes);
}
void CDIB::InitDIB(COLORREF color)
{
int i,j;
unsigned char *ptr;
if(m_pInfo->bmiHeader.biBitCount == 24)
{
unsigned char col[3];
col[0]=GetBValue(color);
col[1]=GetGValue(color);
col[2]=GetRValue(color);
for(i=0,ptr = m_pBits; i < height; i++)
{
ptr = m_pBits + i*bytes;
for(j=0; j < width ; j++,ptr+=3)
{
memcpy(ptr,col,3);
}
}
}
else
{
for(i=0,ptr = m_pBits; i < height; i++,ptr+=bytes)
{
memset(ptr,(BYTE)color,bytes);
}
}
}
void CDIB::BitBlt(HDC hDest,int nXDest,int nYDest,int nWidth,int nHeight,int xSrc,int ySrc)
{
SetDIBitsToDevice(hDest,nXDest,nYDest,nWidth,nHeight,xSrc,Height()-ySrc-nHeight,0,Height(),m_pBits,m_pInfo,DIB_RGB_COLORS);
}
void CDIB::StretchBlt(HDC hDest,int nXDest,int nYDest,int nDWidth,int nDHeight,int xSrc,int ySrc,int nSWidth,int nSHeight)
{
int err;
err = StretchDIBits(hDest,nXDest,nYDest,nDWidth,nDHeight,xSrc,ySrc,nSWidth,nSHeight,m_pBits,(CONST BITMAPINFO * )&m_pInfo->bmiHeader,DIB_RGB_COLORS,SRCCOPY);
}
void CDIB::ExpandBlt(int nXDest,int nYDest,int xRatio,int yRatio,CDIB& dibSrc,int xSrc,int ySrc,int nSWidth,int nSHeight)
{
SetPalette(dibSrc.m_pRGB);
nSWidth = xSrc+nSWidth > dibSrc.width ? dibSrc.width-xSrc : nSWidth;
nSHeight = ySrc+nSHeight > dibSrc.height? dibSrc.height-ySrc : nSHeight;
Expand(nXDest,nYDest,xRatio,yRatio,dibSrc,xSrc,ySrc,nSWidth,nSHeight);
}
void CDIB::Expand(int nXDest,int nYDest,int xRatio,int yRatio,CDIB& dibSrc,int xSrc,int ySrc,int nSWidth,int nSHeight)
{
int xNum,yNum,xErr,yErr;
int nDWidth,nDHeight;
nDWidth = nSWidth*xRatio;
nDHeight = nSHeight*yRatio;
nDWidth = nXDest+nDWidth > width ? width-nXDest : nDWidth ;
nDHeight = nYDest+nDHeight > height ? height-nYDest : nDHeight;
xNum = nDWidth/xRatio;
yNum = nDHeight/yRatio;
xErr = nDWidth%xRatio;
yErr = nDHeight%yRatio;
unsigned char *buffer,*srcPtr,*destPtr,*ptr;
int i,j,k;
buffer = (unsigned char *)malloc(nDWidth+20);
if(!buffer) return;
for(i=0; i < yNum; i++,ySrc++)
{
srcPtr = dibSrc.GetLinePtr(ySrc) + xSrc;
ptr = buffer;
for(j=0; j < xNum; j++,ptr+=xRatio)
{
memset(ptr,*(srcPtr+j),xRatio);
k=*(srcPtr+j);
}
memset(ptr,(unsigned char)k,xErr);
for(j=0; j < yRatio ; j++,nYDest++)
{
destPtr = GetLinePtr(nYDest) + nXDest;
memcpy(destPtr,buffer,nDWidth);
}
}
for(j=0; j < yErr; j++,nYDest++)
{
destPtr = GetLinePtr(nYDest) + nXDest;
memcpy(destPtr,buffer,nDWidth);
}
free(buffer);
}
void CDIB::StretchBlt(int nXDest,int nYDest,int nDWidth,int nDHeight,CDIB& dibSrc,int xSrc,in
DIB.rar_DIB_bitmap_independent bitm
版权申诉
83 浏览量
2022-09-21
05:27:02
上传
评论
收藏 6KB RAR 举报
JaniceLu
- 粉丝: 78
- 资源: 1万+
最新资源
- F103-霸道开发板2.8寸电阻触摸屏例程.rar
- Google(高德)地图瓦片python代码下载
- Python实现输出杨辉三角形
- polsarpro官方教程、操作说明 PolSARpro v5.0 Software Training Course
- STM32 TouchGFX的使用二图片显示
- buildx镜像文件,也可以通过网上其他方式获取
- 【中级软件设计师】上午题12-软件工程(2):单元测试、黑盒测试、白盒测试、软件运行与维护
- 免费计算机毕业设计-医院住院管理系统的设计与实现(包含代码+论文)
- tt100k数据转换yolo格式
- 免费计算机毕业设计-学生在线网络考试系统的设计与实现(包含论文+源码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈