📄 ximahist.cpp
字号:
// xImaHist.cpp : histogram functions
/* 28/01/2004 v1.00 - www.xdp.it
* CxImage version 6.0.0 02/Feb/2008
*/
#include "ximage.h"
#if CXIMAGE_SUPPORT_DSP
////////////////////////////////////////////////////////////////////////////////
long CxImage::Histogram(long* red, long* green, long* blue, long* gray, long colorspace)
{
if (!pDib) return 0;
RGBQUAD color;
if (red) memset(red,0,256*sizeof(long));
if (green) memset(green,0,256*sizeof(long));
if (blue) memset(blue,0,256*sizeof(long));
if (gray) memset(gray,0,256*sizeof(long));
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++){
for(long x=xmin; x<xmax; x++){
#if CXIMAGE_SUPPORT_SELECTION
if (BlindSelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
{
switch (colorspace){
case 1:
color = HSLtoRGB(BlindGetPixelColor(x,y));
break;
case 2:
color = YUVtoRGB(BlindGetPixelColor(x,y));
break;
case 3:
color = YIQtoRGB(BlindGetPixelColor(x,y));
break;
case 4:
color = XYZtoRGB(BlindGetPixelColor(x,y));
break;
default:
color = BlindGetPixelColor(x,y);
}
if (red) red[color.rgbRed]++;
if (green) green[color.rgbGreen]++;
if (blue) blue[color.rgbBlue]++;
if (gray) gray[(BYTE)RGB2GRAY(color.rgbRed,color.rgbGreen,color.rgbBlue)]++;
}
}
}
long n=0;
for (int i=0; i<256; i++){
if (red && red[i]>n) n=red[i];
if (green && green[i]>n) n=green[i];
if (blue && blue[i]>n) n=blue[i];
if (gray && gray[i]>n) n=gray[i];
}
return n;
}
////////////////////////////////////////////////////////////////////////////////
/**
* HistogramStretch
* \param method: 0 = luminance (default), 1 = linked channels , 2 = independent channels.
* \param threshold: minimum percentage level in the histogram to recognize it as meaningful. Range: 0.0 to 1.0; default = 0; typical = 0.005 (0.5%);
* \return true if everything is ok
* \author [dave] and [nipper]; changes [DP]
*/
bool CxImage::HistogramStretch(long method, double threshold)
{
if (!pDib) return false;
double dbScaler = 50.0/head.biHeight;
long x,y;
if ((head.biBitCount==8) && IsGrayScale()){
double p[256];
memset(p, 0, 256*sizeof(double));
for (y=0; y<head.biHeight; y++)
{
info.nProgress = (long)(y*dbScaler);
if (info.nEscape) break;
for (x=0; x<head.biWidth; x++) {
p[BlindGetPixelIndex(x, y)]++;
}
}
double maxh = 0;
for (y=0; y<255; y++) if (maxh < p[y]) maxh = p[y];
threshold *= maxh;
int minc = 0;
while (minc<255 && p[minc]<=threshold) minc++;
int maxc = 255;
while (maxc>0 && p[maxc]<=threshold) maxc--;
if (minc == 0 && maxc == 255) return true;
if (minc >= maxc) return true;
// calculate LUT
BYTE lut[256];
for (x = 0; x <256; x++){
lut[x] = (BYTE)max(0,min(255,(255 * (x - minc) / (maxc - minc))));
}
for (y=0; y<head.biHeight; y++) {
if (info.nEscape) break;
info.nProgress = (long)(50.0+y*dbScaler);
for (x=0; x<head.biWidth; x++)
{
BlindSetPixelIndex(x, y, lut[BlindGetPixelIndex(x, y)]);
}
}
} else {
switch(method){
case 1:
{ // <nipper>
double p[256];
memset(p, 0, 256*sizeof(double));
for (y=0; y<head.biHeight; y++)
{
info.nProgress = (long)(y*dbScaler);
if (info.nEscape) break;
for (x=0; x<head.biWidth; x++) {
RGBQUAD color = BlindGetPixelColor(x, y);
p[color.rgbRed]++;
p[color.rgbBlue]++;
p[color.rgbGreen]++;
}
}
double maxh = 0;
for (y=0; y<255; y++) if (maxh < p[y]) maxh = p[y];
threshold *= maxh;
int minc = 0;
while (minc<255 && p[minc]<=threshold) minc++;
int maxc = 255;
while (maxc>0 && p[maxc]<=threshold) maxc--;
if (minc == 0 && maxc == 255) return true;
if (minc >= maxc) return true;
// calculate LUT
BYTE lut[256];
for (x = 0; x <256; x++){
lut[x] = (BYTE)max(0,min(255,(255 * (x - minc) / (maxc - minc))));
}
// normalize image
for (y=0; y<head.biHeight; y++) {
if (info.nEscape) break;
info.nProgress = (long)(50.0+y*dbScaler);
for (x=0; x<head.biWidth; x++)
{
RGBQUAD color = BlindGetPixelColor(x, y);
color.rgbRed = lut[color.rgbRed];
color.rgbBlue = lut[color.rgbBlue];
color.rgbGreen = lut[color.rgbGreen];
BlindSetPixelColor(x, y, color);
}
}
}
break;
case 2:
{ // <nipper>
double pR[256];
memset(pR, 0, 256*sizeof(double));
double pG[256];
memset(pG, 0, 256*sizeof(double));
double pB[256];
memset(pB, 0, 256*sizeof(double));
for (y=0; y<head.biHeight; y++)
{
info.nProgress = (long)(y*dbScaler);
if (info.nEscape) break;
for (long x=0; x<head.biWidth; x++) {
RGBQUAD color = BlindGetPixelColor(x, y);
pR[color.rgbRed]++;
pB[color.rgbBlue]++;
pG[color.rgbGreen]++;
}
}
double maxh = 0;
for (y=0; y<255; y++) if (maxh < pR[y]) maxh = pR[y];
double threshold2 = threshold*maxh;
int minR = 0;
while (minR<255 && pR[minR]<=threshold2) minR++;
int maxR = 255;
while (maxR>0 && pR[maxR]<=threshold2) maxR--;
maxh = 0;
for (y=0; y<255; y++) if (maxh < pG[y]) maxh = pG[y];
threshold2 = threshold*maxh;
int minG = 0;
while (minG<255 && pG[minG]<=threshold2) minG++;
int maxG = 255;
while (maxG>0 && pG[maxG]<=threshold2) maxG--;
maxh = 0;
for (y=0; y<255; y++) if (maxh < pB[y]) maxh = pB[y];
threshold2 = threshold*maxh;
int minB = 0;
while (minB<255 && pB[minB]<=threshold2) minB++;
int maxB = 255;
while (maxB>0 && pB[maxB]<=threshold2) maxB--;
if (minR == 0 && maxR == 255 && minG == 0 && maxG == 255 && minB == 0 && maxB == 255)
return true;
// calculate LUT
BYTE lutR[256];
BYTE range = maxR - minR;
if (range != 0) {
for (x = 0; x <256; x++){
lutR[x] = (BYTE)max(0,min(255,(255 * (x - minR) / range)));
}
} else lutR[minR] = minR;
BYTE lutG[256];
range = maxG - minG;
if (range != 0) {
for (x = 0; x <256; x++){
lutG[x] = (BYTE)max(0,min(255,(255 * (x - minG) / range)));
}
} else lutG[minG] = minG;
BYTE lutB[256];
range = maxB - minB;
if (range != 0) {
for (x = 0; x <256; x++){
lutB[x] = (BYTE)max(0,min(255,(255 * (x - minB) / range)));
}
} else lutB[minB] = minB;
// normalize image
for (y=0; y<head.biHeight; y++)
{
info.nProgress = (long)(50.0+y*dbScaler);
if (info.nEscape) break;
for (x=0; x<head.biWidth; x++)
{
RGBQUAD color = BlindGetPixelColor(x, y);
color.rgbRed = lutR[color.rgbRed];
color.rgbBlue = lutB[color.rgbBlue];
color.rgbGreen = lutG[color.rgbGreen];
BlindSetPixelColor(x, y, color);
}
}
}
break;
default:
{ // <dave>
double p[256];
memset(p, 0, 256*sizeof(double));
for (y=0; y<head.biHeight; y++)
{
info.nProgress = (long)(y*dbScaler);
if (info.nEscape) break;
for (x=0; x<head.biWidth; x++) {
RGBQUAD color = BlindGetPixelColor(x, y);
p[RGB2GRAY(color.rgbRed, color.rgbGreen, color.rgbBlue)]++;
}
}
double maxh = 0;
for (y=0; y<255; y++) if (maxh < p[y]) maxh = p[y];
threshold *= maxh;
int minc = 0;
while (minc<255 && p[minc]<=threshold) minc++;
int maxc = 255;
while (maxc>0 && p[maxc]<=threshold) maxc--;
if (minc == 0 && maxc == 255) return true;
if (minc >= maxc) return true;
// calculate LUT
BYTE lut[256];
for (x = 0; x <256; x++){
lut[x] = (BYTE)max(0,min(255,(255 * (x - minc) / (maxc - minc))));
}
for(y=0; y<head.biHeight; y++){
info.nProgress = (long)(50.0+y*dbScaler);
if (info.nEscape) break;
for(x=0; x<head.biWidth; x++){
RGBQUAD color = BlindGetPixelColor( x, y );
RGBQUAD yuvClr = RGBtoYUV(color);
yuvClr.rgbRed = lut[yuvClr.rgbRed];
color = YUVtoRGB(yuvClr);
BlindSetPixelColor( x, y, color );
}
}
}
}
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -