📄 ximage.cpp
字号:
* \return the size in bytes of the internal pDib object
*/
long CxImage::GetSize()
{
return head.biSize + head.biSizeImage + GetPaletteSize();
}
////////////////////////////////////////////////////////////////////////////////
/**
* Checks if the coordinates are inside the image
* \return true if x and y are both inside the image
*/
bool CxImage::IsInside(long x, long y)
{
return (0<=y && y<head.biHeight && 0<=x && x<head.biWidth);
}
////////////////////////////////////////////////////////////////////////////////
/**
* Sets the image bits to the specified value
* - for indexed images, the output color is set by the palette entries.
* - for RGB images, the output color is a shade of gray.
*/
void CxImage::Clear(BYTE bval)
{
if (pDib == 0) return;
if (GetBpp() == 1){
if (bval > 0) bval = 255;
}
if (GetBpp() == 4){
bval = (BYTE)(17*(0x0F & bval));
}
memset(info.pImage,bval,head.biSizeImage);
}
////////////////////////////////////////////////////////////////////////////////
/**
* Transfers the image from an existing source image. The source becomes empty.
* \return true if everything is ok
*/
bool CxImage::Transfer(CxImage &from)
{
if (!Destroy())
return false;
memcpy(&head,&from.head,sizeof(BITMAPINFOHEADER));
memcpy(&info,&from.info,sizeof(CXIMAGEINFO));
pDib = from.pDib;
pSelection = from.pSelection;
pAlpha = from.pAlpha;
pLayers = from.pLayers;
memset(&from.head,0,sizeof(BITMAPINFOHEADER));
memset(&from.info,0,sizeof(CXIMAGEINFO));
from.pDib = from.pSelection = from.pAlpha = NULL;
from.pLayers = NULL;
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* (this) points to the same pDib owned by (*from), the image remains in (*from)
* but (this) has the access to the pixels. <b>Use carefully !!!</b>
*/
void CxImage::Ghost(CxImage *from)
{
if (from){
memcpy(&head,&from->head,sizeof(BITMAPINFOHEADER));
memcpy(&info,&from->info,sizeof(CXIMAGEINFO));
pDib = from->pDib;
pSelection = from->pSelection;
pAlpha = from->pAlpha;
pLayers = from->pLayers;
info.pGhost=from;
}
}
////////////////////////////////////////////////////////////////////////////////
/**
* turns a 16 or 32 bit bitfield image into a RGB image
*/
void CxImage::Bitfield2RGB(BYTE *src, WORD redmask, WORD greenmask, WORD bluemask, BYTE bpp)
{
switch (bpp){
case 16:
{
DWORD ns[3]={0,0,0};
// compute the number of shift for each mask
for (int i=0;i<16;i++){
if ((redmask>>i)&0x01) ns[0]++;
if ((greenmask>>i)&0x01) ns[1]++;
if ((bluemask>>i)&0x01) ns[2]++;
}
ns[1]+=ns[0]; ns[2]+=ns[1]; ns[0]=8-ns[0]; ns[1]-=8; ns[2]-=8;
// dword aligned width for 16 bit image
long effwidth2=(((head.biWidth + 1) / 2) * 4);
WORD w;
long y2,y3,x2,x3;
BYTE *p=info.pImage;
// scan the buffer in reverse direction to avoid reallocations
for (long y=head.biHeight-1; y>=0; y--){
y2=effwidth2*y;
y3=info.dwEffWidth*y;
for (long x=head.biWidth-1; x>=0; x--){
x2 = 2*x+y2;
x3 = 3*x+y3;
w = (WORD)(src[x2]+256*src[1+x2]);
p[ x3]=(BYTE)((w & bluemask)<<ns[0]);
p[1+x3]=(BYTE)((w & greenmask)>>ns[1]);
p[2+x3]=(BYTE)((w & redmask)>>ns[2]);
}
}
break;
}
case 32:
{
// dword aligned width for 32 bit image
long effwidth4 = head.biWidth * 4;
long y4,y3,x4,x3;
BYTE *p=info.pImage;
// scan the buffer in reverse direction to avoid reallocations
for (long y=head.biHeight-1; y>=0; y--){
y4=effwidth4*y;
y3=info.dwEffWidth*y;
for (long x=head.biWidth-1; x>=0; x--){
x4 = 4*x+y4;
x3 = 3*x+y3;
p[ x3]=src[ x4];
p[1+x3]=src[1+x4];
p[2+x3]=src[2+x4];
}
}
}
}
return;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Creates an image from a generic buffer
* \param pArray: source memory buffer
* \param dwWidth: image width
* \param dwHeight: image height
* \param dwBitsperpixel: can be 1,4,8,24,32
* \param dwBytesperline: line alignment, in bytes, for a single row stored in pArray
* \param bFlipImage: tune this parameter if the image is upsidedown
* \return true if everything is ok
*/
bool CxImage::CreateFromArray(BYTE* pArray,DWORD dwWidth,DWORD dwHeight,DWORD dwBitsperpixel, DWORD dwBytesperline, bool bFlipImage)
{
if (pArray==NULL) return false;
if (!((dwBitsperpixel==1)||(dwBitsperpixel==4)||(dwBitsperpixel==8)||
(dwBitsperpixel==24)||(dwBitsperpixel==32))) return false;
if (!Create(dwWidth,dwHeight,dwBitsperpixel)) return false;
if (dwBitsperpixel<24) SetGrayPalette();
#if CXIMAGE_SUPPORT_ALPHA
if (dwBitsperpixel==32) AlphaCreate();
#endif //CXIMAGE_SUPPORT_ALPHA
BYTE *dst,*src;
for (DWORD y = 0; y<dwHeight; y++) {
dst = info.pImage + (bFlipImage?(dwHeight-1-y):y) * info.dwEffWidth;
src = pArray + y * dwBytesperline;
if (dwBitsperpixel==32){
for(DWORD x=0;x<dwWidth;x++){
*dst++=src[0];
*dst++=src[1];
*dst++=src[2];
#if CXIMAGE_SUPPORT_ALPHA
AlphaSet(x,y,src[3]);
#endif //CXIMAGE_SUPPORT_ALPHA
src+=4;
}
} else {
memcpy(dst,src,min(info.dwEffWidth,dwBytesperline));
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* \sa CreateFromArray
*/
bool CxImage::CreateFromMatrix(BYTE** ppMatrix,DWORD dwWidth,DWORD dwHeight,DWORD dwBitsperpixel, DWORD dwBytesperline, bool bFlipImage)
{
if (ppMatrix==NULL) return false;
if (!((dwBitsperpixel==1)||(dwBitsperpixel==4)||(dwBitsperpixel==8)||
(dwBitsperpixel==24)||(dwBitsperpixel==32))) return false;
if (!Create(dwWidth,dwHeight,dwBitsperpixel)) return false;
if (dwBitsperpixel<24) SetGrayPalette();
#if CXIMAGE_SUPPORT_ALPHA
if (dwBitsperpixel==32) AlphaCreate();
#endif //CXIMAGE_SUPPORT_ALPHA
BYTE *dst,*src;
for (DWORD y = 0; y<dwHeight; y++) {
dst = info.pImage + (bFlipImage?(dwHeight-1-y):y) * info.dwEffWidth;
src = ppMatrix[y];
if (src){
if (dwBitsperpixel==32){
for(DWORD x=0;x<dwWidth;x++){
*dst++=src[0];
*dst++=src[1];
*dst++=src[2];
#if CXIMAGE_SUPPORT_ALPHA
AlphaSet(x,y,src[3]);
#endif //CXIMAGE_SUPPORT_ALPHA
src+=4;
}
} else {
memcpy(dst,src,min(info.dwEffWidth,dwBytesperline));
}
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* \return lightness difference between elem1 and elem2
*/
int CxImage::CompareColors(const void *elem1, const void *elem2)
{
RGBQUAD* c1 = (RGBQUAD*)elem1;
RGBQUAD* c2 = (RGBQUAD*)elem2;
int g1 = (int)RGB2GRAY(c1->rgbRed,c1->rgbGreen,c1->rgbBlue);
int g2 = (int)RGB2GRAY(c2->rgbRed,c2->rgbGreen,c2->rgbBlue);
return (g1-g2);
}
////////////////////////////////////////////////////////////////////////////////
/**
* simply calls "if (memblock) free(memblock);".
* Useful when calling Encode for a memory buffer,
* from a DLL compiled with different memory management options.
* CxImage::FreeMemory will use the same memory environment used by Encode.
*/
void CxImage::FreeMemory(void* memblock)
{
if (memblock)
free(memblock);
}
////////////////////////////////////////////////////////////////////////////////
//EOF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -