📄 jzimage.cpp
字号:
////////////////////////////////////////////////////////////////////////////////
void CJzImage::SetPixelColor(long x,long y,RGBQUAD c, bool bSetAlpha)
{
if ((pDib==NULL)||(x<0)||(y<0)||
(x>=head.biWidth)||(y>=head.biHeight)) return;
//if (head.biClrUsed)
// SetPixelIndex(x,y,GetNearestIndex(c));
// else {
BYTE* iDst = info.pImage + y*info.dwEffWidth + x*3;
*iDst++ = c.rgbBlue;
*iDst++ = c.rgbGreen;
*iDst = c.rgbRed;
#if CXIMAGE_SUPPORT_ALPHA
// if (bSetAlpha) AlphaSet(x,y,c.rgbReserved);
#endif //CXIMAGE_SUPPORT_ALPHA
// }
}
////////////////////////////////////////////////////////////////////////////////
/**
* Returns the palette index of the specified pixel.
*/
BYTE CJzImage::GetPixelIndex(long x,long y)
{
if ((pDib==NULL)||(head.biClrUsed==0)) return 0;
if ((x<0)||(y<0)||(x>=head.biWidth)||(y>=head.biHeight)) {
if (info.nBkgndIndex != -1) return (BYTE)info.nBkgndIndex;
else return *info.pImage;
}
if (head.biBitCount==8){
return info.pImage[y*info.dwEffWidth + x];
} else {
BYTE pos;
BYTE iDst= info.pImage[y*info.dwEffWidth + (x*head.biBitCount >> 3)];
if (head.biBitCount==4){
pos = (BYTE)(4*(1-x%2));
iDst &= (0x0F<<pos);
return (BYTE)(iDst >> pos);
} else if (head.biBitCount==1){
pos = (BYTE)(7-x%8);
iDst &= (0x01<<pos);
return (BYTE)(iDst >> pos);
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CJzImage::GetPixelColor(long x,long y, bool bGetAlpha)
{
// RGBQUAD rgb={0,0,0,0};
RGBQUAD rgb=info.nBkgndColor; //<mpwolski>
if ((pDib==NULL)||(x<0)||(y<0)||
(x>=head.biWidth)||(y>=head.biHeight)){
if (info.nBkgndIndex != -1){
if (head.biBitCount<24) return GetPaletteColor((BYTE)info.nBkgndIndex);
else return info.nBkgndColor;
} else if (pDib) return GetPixelColor(0,0);
return rgb;
}
if (head.biClrUsed){
rgb = GetPaletteColor(GetPixelIndex(x,y));
} else {
BYTE* iDst = info.pImage + y*info.dwEffWidth + x*3;
rgb.rgbBlue = *iDst++;
rgb.rgbGreen= *iDst++;
rgb.rgbRed = *iDst;
}
#if CXIMAGE_SUPPORT_ALPHA
// if (pAlpha && bGetAlpha) rgb.rgbReserved = AlphaGet(x,y);
#else
rgb.rgbReserved = 0;
#endif //CXIMAGE_SUPPORT_ALPHA
return rgb;
}
//////////////////////////////////////////////////////////////////////////////
void CJzImage::Copy(const CJzImage &src, bool copypixels, bool copyselection, bool copyalpha)
{
//copy the attributes
memcpy(&info,&src.info,sizeof(CXIMAGEINFO));
//rebuild the image
Create(src.GetWidth(),src.GetHeight(),src.GetBpp(),src.GetType());
//copy the pixels and the palette, or at least copy the palette only.
if (copypixels && pDib && src.pDib)
{ //printf(" ======== copy pDib,src.pDib,GetSize ============= \n");
memcpy(pDib,src.pDib,GetSize());
}
else
{
//SetPalette(src.GetPalette());
}
long nSize = head.biWidth * head.biHeight;
/*
//copy the selection
if (copyselection && src.pSelection){
if (pSelection) free(pSelection);
pSelection = (BYTE*)malloc(nSize);
memcpy(pSelection,src.pSelection,nSize);
}
//copy the alpha channel
if (copyalpha && src.pAlpha){
if (pAlpha) free(pAlpha);
pAlpha = (BYTE*)malloc(nSize);
memcpy(pAlpha,src.pAlpha,nSize);
}
*/
}
////////////////////////////////////////////////////////////////////////////////
/**
* \return pointer to the image pixels. <b> USE CAREFULLY </b>
*/
BYTE* CJzImage::GetBits(DWORD row)
{
if (pDib){
if (row) {
if (row<(DWORD)head.biHeight){
return ((BYTE*)pDib + *(DWORD*)pDib + GetPaletteSize() + (info.dwEffWidth * row));
} else {
return NULL;
}
} else {
return ((BYTE*)pDib + *(DWORD*)pDib + GetPaletteSize());
}
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
bool CJzImage::Decode(CxFile *hFile)
{
return false;
}
void * CJzImage::CreateImageInfo(DWORD dwWidth, DWORD dwHeight, DWORD wBpp, DWORD imagetype)
{
DWORD swap;
// prevent further actions if width or height are not vaild <Balabasnia>
if ((dwWidth == 0) || (dwHeight == 0)){
strcpy(info.szLastError,"CxImage::Create : width and height must be greater than zero");
return NULL;
}
// Make sure bits per pixel is valid
if (wBpp <= 1) wBpp = 1;
else if (wBpp <= 4) wBpp = 4;
else if (wBpp <= 8) wBpp = 8;
else wBpp = 24;
/*
float scale, scale1, scale2;
fxscale = (float)pScr->w / dwHeight;
fyscale = (float)pScr->h / dwWidth;
scale2 = ( fxscale > fyscale )?fxscale:fyscale;
fxscale = (float)pScr->w / dwWidth;
fyscale = (float)pScr->h / dwHeight;
scale1 = ( fxscale > fyscale )?fxscale:fyscale;
if( scale1 >= 1.0 || scale2 >= 1.0 )
{
scale = (float)1.000001;
info.dwimaWidth = dwWidth;
info.dwimaHeight = dwHeight;
}
else
{
scale = ( scale1 > scale2 )?scale1:scale2;
info.dwimaWidth = (int)(dwWidth*scale);
info.dwimaHeight = (int)(dwHeight*scale);
}
*/
//printf(" CreateImageInfo info.dwimaWidth: %d, info.dwimaHeight: %d\n", info.dwimaWidth, info.dwimaHeight );
//for test
/*
if(fxscale >= 1.0)
{
fxscale = (float)1.000001;
info.dwimaWidth = dwWidth;
}else
info.dwimaWidth = pScr->w;
if(fyscale >= 1.0)
{
fyscale = (float)1.000001;
info.dwimaHeight = dwHeight;
}else
info.dwimaHeight = pScr->h;
//set the common image informations
*/
info.dwEffWidth = ((((wBpp * dwWidth) + 31) / 32) * 4);
info.dwType = imagetype;
info.wBpp = (WORD)wBpp;
return pScr; //return handle to the DIB
//return (void*)1; //return handle to the DIB
}
void CJzImage::Bitfield2BPP(unsigned char wBpp, unsigned int *mask, unsigned char *psrc, RGBQUAD *c)
{
unsigned short d;
switch(wBpp)
{
case 32:
{
RGBQUAD *src = (RGBQUAD *)psrc;
c->rgbRed = src->rgbBlue;
c->rgbGreen = src->rgbGreen;
c->rgbBlue = src->rgbRed;
break;
}
case 24:
c->rgbRed = *psrc++;
c->rgbGreen = *psrc++;
c->rgbBlue = *psrc++;
break;
case 16:
{
DWORD ns[3]={0,0,0};
WORD redmask = mask[0], greenmask = mask[1], bluemask = mask[2];
// 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;
d = (WORD)(*psrc) + ( ( (WORD)(*(psrc+1)) ) <<8 );
c->rgbRed = (BYTE)((d &bluemask)<<ns[0]);
c->rgbGreen = (BYTE)((d &greenmask)>>ns[1]);
c->rgbBlue = (BYTE)((d &redmask)>>ns[2]);
break;
}
case 8:
d = *psrc;
c->rgbBlue = pRgb[d].rgbRed;
c->rgbGreen = pRgb[d].rgbGreen;
c->rgbRed = pRgb[d].rgbBlue;
case 4:
d = *psrc & 0xf;
c->rgbBlue = pRgb[d].rgbRed;
c->rgbGreen = pRgb[d].rgbGreen;
c->rgbRed = pRgb[d].rgbBlue;
break;
case 1:
if(*psrc & 1)
{
d = 255;
}else
{
d = 0;
}
c->rgbBlue = (BYTE)d;
c->rgbGreen = (BYTE)d;
c->rgbRed = (BYTE)d;
break;
}
}
RGBQUAD * CJzImage::CreateGrayPalette(DWORD wBpp)
{
DWORD biClrUsed = (1 << wBpp);
pRgb = new RGBQUAD[biClrUsed];
if(pRgb == 0)
return 0;
for (DWORD ni=0;ni<biClrUsed;ni++)
pRgb[ni].rgbBlue=pRgb[ni].rgbGreen = pRgb[ni].rgbRed = (BYTE)(ni*(255/(biClrUsed-1)));
return pRgb;
}
RGBQUAD* CJzImage::CreatePalette(DWORD n, BYTE *r, BYTE *g, BYTE *b)
{
pRgb = new RGBQUAD[n];
if(pRgb == 0)
return 0;
for (DWORD i=0; i<n;i++){
pRgb[i].rgbRed=r[i];
pRgb[i].rgbGreen=g[i];
pRgb[i].rgbBlue=b[i];
}
return pRgb;
}
////////////////////////////////////////////////////////////////////////////////
void CJzImage::SetPalette(DWORD n, BYTE *r, BYTE *g, BYTE *b)
{
if ((!r)||(pDib==NULL)||(head.biClrUsed==0)) return;
if (!g) g = r;
if (!b) b = g;
RGBQUAD* ppal=GetPalette();
DWORD m=min(n,head.biClrUsed);
for (DWORD i=0; i<m;i++){
ppal[i].rgbRed=r[i];
ppal[i].rgbGreen=g[i];
ppal[i].rgbBlue=b[i];
}
//info.last_c_isvalid = false;
}
////////////////////////////////////////////////////////////////////////////////
void CJzImage::SetPalette(rgb_color *rgb,DWORD nColors)
{
if (!rgb) return;
RGBQUAD* ppal=pRgb;
for (DWORD i=0; i<nColors;i++){
ppal[i].rgbRed=rgb[i].r;
ppal[i].rgbGreen=rgb[i].g;
ppal[i].rgbBlue=rgb[i].b;
}
}
////////////////////////////////////////////////////////////////////////////////
void CJzImage::SetPalette(RGBQUAD* pPal,DWORD nColors)
{
if (!pRgb) return;
memcpy(pRgb,pPal,nColors*sizeof(RGBQUAD));
}
////////////////////////////////////////////////////////////////////////////////
void CJzImage::SetPaletteColor(BYTE idx, BYTE r, BYTE g, BYTE b, BYTE alpha)
{
if (pRgb){
BYTE* iDst = (BYTE*)pRgb;
long ldx=idx*sizeof(RGBQUAD);
iDst[ldx++] = (BYTE) b;
iDst[ldx++] = (BYTE) g;
iDst[ldx++] = (BYTE) r;
iDst[ldx] = (BYTE) alpha;
}
}
////////////////////////////////////////////////////////////////////////////////
void CJzImage::SetPaletteColor(BYTE idx, RGBQUAD c)
{
if (pRgb){
BYTE* iDst = (BYTE*)pRgb;
long ldx=idx*sizeof(RGBQUAD);
iDst[ldx++] = (BYTE) c.rgbBlue;
iDst[ldx++] = (BYTE) c.rgbGreen;
iDst[ldx++] = (BYTE) c.rgbRed;
iDst[ldx] = (BYTE) c.rgbReserved;
}
}
////////////////////////////////////////////////////////////////////////////////
void CJzImage::SetPaletteColor(BYTE idx, COLORREF cr)
{
if (pRgb){
BYTE* iDst = (BYTE*)pRgb;
long ldx=idx*sizeof(RGBQUAD);
iDst[ldx++] = (BYTE) GetBValue(cr);
iDst[ldx++] = (BYTE) GetGValue(cr);
iDst[ldx++] = (BYTE) GetRValue(cr);
iDst[ldx] = (BYTE) 0;
}
}
bool CJzImage::CreatePalette(DWORD nums)
{
pRgb = new RGBQUAD[nums];
if(pRgb == NULL)
return false;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -