📄 ximadsp.cpp
字号:
// xImaDsp.cpp : DSP functions
/* 07/08/2001 v1.00 - Davide Pizzolato - www.xdp.it
* CxImage version 6.0.0 02/Feb/2008
*/
#include "ximage.h"
#include "ximaiter.h"
#if CXIMAGE_SUPPORT_DSP
////////////////////////////////////////////////////////////////////////////////
/**
* Converts the image to B&W.
* The OptimalThreshold() function can be used for calculating the optimal threshold.
* \param level: the lightness threshold.
* \return true if everything is ok
*/
bool CxImage::Threshold(BYTE level)
{
if (!pDib) return false;
if (head.biBitCount == 1) return true;
GrayScale();
CxImage tmp(head.biWidth,head.biHeight,1);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
for (long y=0;y<head.biHeight;y++){
info.nProgress = (long)(100*y/head.biHeight);
if (info.nEscape) break;
for (long x=0;x<head.biWidth;x++){
if (BlindGetPixelIndex(x,y)>level)
tmp.BlindSetPixelIndex(x,y,1);
else
tmp.BlindSetPixelIndex(x,y,0);
}
}
tmp.SetPaletteColor(0,0,0,0);
tmp.SetPaletteColor(1,255,255,255);
Transfer(tmp);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Converts the image to B&W, using a threshold mask
* \param pThresholdMask: the lightness threshold mask.
* the pThresholdMask image must be grayscale with same with and height of the current image
* \return true if everything is ok
*/
bool CxImage::Threshold(CxImage* pThresholdMask)
{
if (!pDib) return false;
if (head.biBitCount == 1) return true;
if (!pThresholdMask) return false;
if (!pThresholdMask->IsValid() ||
!pThresholdMask->IsGrayScale() ||
pThresholdMask->GetWidth() != GetWidth() ||
pThresholdMask->GetHeight() != GetHeight()){
strcpy(info.szLastError,"invalid ThresholdMask");
return false;
}
GrayScale();
CxImage tmp(head.biWidth,head.biHeight,1);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
for (long y=0;y<head.biHeight;y++){
info.nProgress = (long)(100*y/head.biHeight);
if (info.nEscape) break;
for (long x=0;x<head.biWidth;x++){
if (BlindGetPixelIndex(x,y)>pThresholdMask->BlindGetPixelIndex(x,y))
tmp.BlindSetPixelIndex(x,y,1);
else
tmp.BlindSetPixelIndex(x,y,0);
}
}
tmp.SetPaletteColor(0,0,0,0);
tmp.SetPaletteColor(1,255,255,255);
Transfer(tmp);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Filters only the pixels with a lightness less (or more) than the threshold level,
* and preserves the colors for the unfiltered pixels.
* \param level = the lightness threshold.
* \param bDirection = false: filter dark pixels, true: filter light pixels
* \param nBkgndColor = filtered pixels are set to nBkgndColor color
* \param bSetAlpha = if true, sets also the alpha component for the filtered pixels, with nBkgndColor.rgbReserved
* \return true if everything is ok
* \author [DP], [wangsongtao]
*/
////////////////////////////////////////////////////////////////////////////////
bool CxImage::Threshold2(BYTE level, bool bDirection, RGBQUAD nBkgndColor, bool bSetAlpha)
{
if (!pDib) return false;
if (head.biBitCount == 1) return true;
CxImage tmp(*this, true, false, false);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
tmp.GrayScale();
long xmin,xmax,ymin,ymax;
if (pSelection){
xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
} else {
xmin = ymin = 0;
xmax = head.biWidth; ymax=head.biHeight;
}
for(long y=ymin; y<ymax; y++){
info.nProgress = (long)(100*y/head.biHeight);
if (info.nEscape) break;
for(long x=xmin; x<xmax; x++){
#if CXIMAGE_SUPPORT_SELECTION
if (BlindSelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
{
BYTE i = tmp.BlindGetPixelIndex(x,y);
if (!bDirection && i<level) BlindSetPixelColor(x,y,nBkgndColor,bSetAlpha);
if (bDirection && i>=level) BlindSetPixelColor(x,y,nBkgndColor,bSetAlpha);
}
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract RGB channels from the image. Each channel is an 8 bit grayscale image.
* \param r,g,b: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitRGB(CxImage* r,CxImage* g,CxImage* b)
{
if (!pDib) return false;
if (r==NULL && g==NULL && b==NULL) return false;
CxImage tmpr(head.biWidth,head.biHeight,8);
CxImage tmpg(head.biWidth,head.biHeight,8);
CxImage tmpb(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(long y=0; y<head.biHeight; y++){
for(long x=0; x<head.biWidth; x++){
color = BlindGetPixelColor(x,y);
if (r) tmpr.BlindSetPixelIndex(x,y,color.rgbRed);
if (g) tmpg.BlindSetPixelIndex(x,y,color.rgbGreen);
if (b) tmpb.BlindSetPixelIndex(x,y,color.rgbBlue);
}
}
if (r) tmpr.SetGrayPalette();
if (g) tmpg.SetGrayPalette();
if (b) tmpb.SetGrayPalette();
/*for(long j=0; j<256; j++){
BYTE i=(BYTE)j;
if (r) tmpr.SetPaletteColor(i,i,0,0);
if (g) tmpg.SetPaletteColor(i,0,i,0);
if (b) tmpb.SetPaletteColor(i,0,0,i);
}*/
if (r) r->Transfer(tmpr);
if (g) g->Transfer(tmpg);
if (b) b->Transfer(tmpb);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract CMYK channels from the image. Each channel is an 8 bit grayscale image.
* \param c,m,y,k: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitCMYK(CxImage* c,CxImage* m,CxImage* y,CxImage* k)
{
if (!pDib) return false;
if (c==NULL && m==NULL && y==NULL && k==NULL) return false;
CxImage tmpc(head.biWidth,head.biHeight,8);
CxImage tmpm(head.biWidth,head.biHeight,8);
CxImage tmpy(head.biWidth,head.biHeight,8);
CxImage tmpk(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(long yy=0; yy<head.biHeight; yy++){
for(long xx=0; xx<head.biWidth; xx++){
color = BlindGetPixelColor(xx,yy);
if (c) tmpc.BlindSetPixelIndex(xx,yy,(BYTE)(255-color.rgbRed));
if (m) tmpm.BlindSetPixelIndex(xx,yy,(BYTE)(255-color.rgbGreen));
if (y) tmpy.BlindSetPixelIndex(xx,yy,(BYTE)(255-color.rgbBlue));
if (k) tmpk.BlindSetPixelIndex(xx,yy,(BYTE)RGB2GRAY(color.rgbRed,color.rgbGreen,color.rgbBlue));
}
}
if (c) tmpc.SetGrayPalette();
if (m) tmpm.SetGrayPalette();
if (y) tmpy.SetGrayPalette();
if (k) tmpk.SetGrayPalette();
if (c) c->Transfer(tmpc);
if (m) m->Transfer(tmpm);
if (y) y->Transfer(tmpy);
if (k) k->Transfer(tmpk);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract YUV channels from the image. Each channel is an 8 bit grayscale image.
* \param y,u,v: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitYUV(CxImage* y,CxImage* u,CxImage* v)
{
if (!pDib) return false;
if (y==NULL && u==NULL && v==NULL) return false;
CxImage tmpy(head.biWidth,head.biHeight,8);
CxImage tmpu(head.biWidth,head.biHeight,8);
CxImage tmpv(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(long yy=0; yy<head.biHeight; yy++){
for(long x=0; x<head.biWidth; x++){
color = RGBtoYUV(BlindGetPixelColor(x,yy));
if (y) tmpy.BlindSetPixelIndex(x,yy,color.rgbRed);
if (u) tmpu.BlindSetPixelIndex(x,yy,color.rgbGreen);
if (v) tmpv.BlindSetPixelIndex(x,yy,color.rgbBlue);
}
}
if (y) tmpy.SetGrayPalette();
if (u) tmpu.SetGrayPalette();
if (v) tmpv.SetGrayPalette();
if (y) y->Transfer(tmpy);
if (u) u->Transfer(tmpu);
if (v) v->Transfer(tmpv);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract YIQ channels from the image. Each channel is an 8 bit grayscale image.
* \param y,i,q: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitYIQ(CxImage* y,CxImage* i,CxImage* q)
{
if (!pDib) return false;
if (y==NULL && i==NULL && q==NULL) return false;
CxImage tmpy(head.biWidth,head.biHeight,8);
CxImage tmpi(head.biWidth,head.biHeight,8);
CxImage tmpq(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(long yy=0; yy<head.biHeight; yy++){
for(long x=0; x<head.biWidth; x++){
color = RGBtoYIQ(BlindGetPixelColor(x,yy));
if (y) tmpy.BlindSetPixelIndex(x,yy,color.rgbRed);
if (i) tmpi.BlindSetPixelIndex(x,yy,color.rgbGreen);
if (q) tmpq.BlindSetPixelIndex(x,yy,color.rgbBlue);
}
}
if (y) tmpy.SetGrayPalette();
if (i) tmpi.SetGrayPalette();
if (q) tmpq.SetGrayPalette();
if (y) y->Transfer(tmpy);
if (i) i->Transfer(tmpi);
if (q) q->Transfer(tmpq);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract XYZ channels from the image. Each channel is an 8 bit grayscale image.
* \param x,y,z: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitXYZ(CxImage* x,CxImage* y,CxImage* z)
{
if (!pDib) return false;
if (x==NULL && y==NULL && z==NULL) return false;
CxImage tmpx(head.biWidth,head.biHeight,8);
CxImage tmpy(head.biWidth,head.biHeight,8);
CxImage tmpz(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(long yy=0; yy<head.biHeight; yy++){
for(long xx=0; xx<head.biWidth; xx++){
color = RGBtoXYZ(BlindGetPixelColor(xx,yy));
if (x) tmpx.BlindSetPixelIndex(xx,yy,color.rgbRed);
if (y) tmpy.BlindSetPixelIndex(xx,yy,color.rgbGreen);
if (z) tmpz.BlindSetPixelIndex(xx,yy,color.rgbBlue);
}
}
if (x) tmpx.SetGrayPalette();
if (y) tmpy.SetGrayPalette();
if (z) tmpz.SetGrayPalette();
if (x) x->Transfer(tmpx);
if (y) y->Transfer(tmpy);
if (z) z->Transfer(tmpz);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract HSL channels from the image. Each channel is an 8 bit grayscale image.
* \param h,s,l: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitHSL(CxImage* h,CxImage* s,CxImage* l)
{
if (!pDib) return false;
if (h==NULL && s==NULL && l==NULL) return false;
CxImage tmph(head.biWidth,head.biHeight,8);
CxImage tmps(head.biWidth,head.biHeight,8);
CxImage tmpl(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(long y=0; y<head.biHeight; y++){
for(long x=0; x<head.biWidth; x++){
color = RGBtoHSL(BlindGetPixelColor(x,y));
if (h) tmph.BlindSetPixelIndex(x,y,color.rgbRed);
if (s) tmps.BlindSetPixelIndex(x,y,color.rgbGreen);
if (l) tmpl.BlindSetPixelIndex(x,y,color.rgbBlue);
}
}
if (h) tmph.SetGrayPalette();
if (s) tmps.SetGrayPalette();
if (l) tmpl.SetGrayPalette();
/* pseudo-color generator for hue channel (visual debug)
if (h) for(long j=0; j<256; j++){
BYTE i=(BYTE)j;
RGBQUAD hsl={120,240,i,0};
tmph.SetPaletteColor(i,HSLtoRGB(hsl));
}*/
if (h) h->Transfer(tmph);
if (s) s->Transfer(tmps);
if (l) l->Transfer(tmpl);
return true;
}
////////////////////////////////////////////////////////////////////////////////
#define HSLMAX 255 /* H,L, and S vary over 0-HSLMAX */
#define RGBMAX 255 /* R,G, and B vary over 0-RGBMAX */
/* HSLMAX BEST IF DIVISIBLE BY 6 */
/* RGBMAX, HSLMAX must each fit in a BYTE. */
/* Hue is undefined if Saturation is 0 (grey-scale) */
/* This value determines where the Hue scrollbar is */
/* initially set for achromatic colors */
#define HSLUNDEFINED (HSLMAX*2/3)
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::RGBtoHSL(RGBQUAD lRGBColor)
{
BYTE R,G,B; /* input RGB values */
BYTE H,L,S; /* output HSL values */
BYTE cMax,cMin; /* max and min RGB values */
WORD Rdelta,Gdelta,Bdelta; /* intermediate value: % of spread from max*/
R = lRGBColor.rgbRed; /* get R, G, and B out of DWORD */
G = lRGBColor.rgbGreen;
B = lRGBColor.rgbBlue;
cMax = max( max(R,G), B); /* calculate lightness */
cMin = min( min(R,G), B);
L = (BYTE)((((cMax+cMin)*HSLMAX)+RGBMAX)/(2*RGBMAX));
if (cMax==cMin){ /* r=g=b --> achromatic case */
S = 0; /* saturation */
H = HSLUNDEFINED; /* hue */
} else { /* chromatic case */
if (L <= (HSLMAX/2)) /* saturation */
S = (BYTE)((((cMax-cMin)*HSLMAX)+((cMax+cMin)/2))/(cMax+cMin));
else
S = (BYTE)((((cMax-cMin)*HSLMAX)+((2*RGBMAX-cMax-cMin)/2))/(2*RGBMAX-cMax-cMin));
/* hue */
Rdelta = (WORD)((((cMax-R)*(HSLMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
Gdelta = (WORD)((((cMax-G)*(HSLMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
Bdelta = (WORD)((((cMax-B)*(HSLMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
if (R == cMax)
H = (BYTE)(Bdelta - Gdelta);
else if (G == cMax)
H = (BYTE)((HSLMAX/3) + Rdelta - Bdelta);
else /* B == cMax */
H = (BYTE)(((2*HSLMAX)/3) + Gdelta - Rdelta);
// if (H < 0) H += HSLMAX; //always false
if (H > HSLMAX) H -= HSLMAX;
}
RGBQUAD hsl={L,S,H,0};
return hsl;
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::HueToRGB(float n1,float n2, float hue)
{
//<F. Livraghi> fixed implementation for HSL2RGB routine
float rValue;
if (hue > 360)
hue = hue - 360;
else if (hue < 0)
hue = hue + 360;
if (hue < 60)
rValue = n1 + (n2-n1)*hue/60.0f;
else if (hue < 180)
rValue = n2;
else if (hue < 240)
rValue = n1+(n2-n1)*(240-hue)/60;
else
rValue = n1;
return rValue;
}
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::HSLtoRGB(COLORREF cHSLColor)
{
return HSLtoRGB(RGBtoRGBQUAD(cHSLColor));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -