From bfaf40d1e059daa93134533b9a1a2b2cc13a78d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Sun, 13 Mar 2011 02:47:19 +0000 Subject: [PATCH] [GB.IMAGE.EFFECT] * NEW: Image.Histogram() is a new method that returns the image histogram. * NEW: ImageHistogram is a new class that represents an image histogram. git-svn-id: svn://localhost/gambas/trunk@3647 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- main/lib/image.effect/CImage.cpp | 78 ++++++++++++++++++++++++++++ main/lib/image.effect/CImage.h | 8 +++ main/lib/image.effect/kimageeffect.h | 3 +- main/lib/image.effect/main.cpp | 33 ++++++------ 4 files changed, 105 insertions(+), 17 deletions(-) diff --git a/main/lib/image.effect/CImage.cpp b/main/lib/image.effect/CImage.cpp index d6ca3e05a..bfa9e1be7 100644 --- a/main/lib/image.effect/CImage.cpp +++ b/main/lib/image.effect/CImage.cpp @@ -324,6 +324,81 @@ BEGIN_METHOD(CIMAGE_implode, GB_FLOAT factor; GB_INTEGER background) END_METHOD +BEGIN_METHOD_VOID(Image_Histogram) + + CIMAGEHISTOGRAM *hist; + int *histogram; + QImage image(THIS); + unsigned int *p, *pm; + + GB.New(POINTER(&hist), GB.FindClass("ImageHistogram"), NULL, NULL); + + GB.Alloc(POINTER(&histogram), sizeof(int) * 256 * 4); + + memset(histogram, 0, 256 * 4 * sizeof(int)); + + p = (unsigned int *)image.bits(); + pm = &p[image.width() * image.height()]; + + while (p < pm) + { + histogram[qBlue(*p)]++; + histogram[qGreen(*p) + 256]++; + histogram[qRed(*p) + 256 * 2]++; + histogram[qAlpha(*p) + 256 * 3]++; + p++; + } + + hist->histogram = histogram; + GB.ReturnObject(hist); + +END_METHOD + + +BEGIN_METHOD_VOID(ImageHistogram_free) + + GB.Free(POINTER(&THIS_HISTOGRAM->histogram)); + +END_METHOD + + +BEGIN_METHOD(ImageHistogram_get, GB_INTEGER channel; GB_INTEGER value) + + int channel; + int value; + + switch(VARG(channel)) + { + case KImageEffect::Blue: channel = 0; break; + case KImageEffect::Green: channel = 1; break; + case KImageEffect::Red: channel = 2; break; + case KImageEffect::Alpha: channel = 3; break; + default: GB.Error("Bad channel"); return; + } + + value = VARG(value); + if (value < 0 || value > 255) + { + GB.Error("Out of bounds"); + return; + } + + GB.ReturnInteger(THIS_HISTOGRAM->histogram[channel * 256 + value]); + +END_METHOD + + +GB_DESC ImageHistogramDesc[] = +{ + GB_DECLARE("ImageHistogram", sizeof(CIMAGEHISTOGRAM)), + GB_NOT_CREATABLE(), + + GB_METHOD("_free", NULL, ImageHistogram_free, NULL), + GB_METHOD("_get", "i", ImageHistogram_get, "(Channel)i(Value)i"), + + GB_END_DECLARE +}; + GB_DESC CImageDesc[] = { GB_DECLARE("Image", 0), @@ -341,6 +416,7 @@ GB_DESC CImageDesc[] = GB_CONSTANT("Red", "i", KImageEffect::Red), GB_CONSTANT("Green", "i", KImageEffect::Green), GB_CONSTANT("Blue", "i", KImageEffect::Blue), + GB_CONSTANT("Alpha", "i", KImageEffect::Alpha), GB_CONSTANT("Uniform", "i", KImageEffect::UniformNoise), GB_CONSTANT("Gaussian", "i", KImageEffect::GaussianNoise), @@ -376,6 +452,8 @@ GB_DESC CImageDesc[] = GB_METHOD("Wave", "Image", CIMAGE_wave, "[(Amplitude)f(WaveLength)f(Background)i]"), GB_METHOD("Noise", "Image", CIMAGE_noise, "(Noise)i"), GB_METHOD("Implode", "Image", CIMAGE_implode, "[(Factor)f(Background)i]"), + + GB_METHOD("Histogram", "ImageHistogram", Image_Histogram, NULL), GB_END_DECLARE }; diff --git a/main/lib/image.effect/CImage.h b/main/lib/image.effect/CImage.h index 8aa71371c..8555f6814 100644 --- a/main/lib/image.effect/CImage.h +++ b/main/lib/image.effect/CImage.h @@ -28,12 +28,20 @@ #ifndef __CIMAGE_CPP extern GB_DESC CImageDesc[]; +extern GB_DESC ImageHistogramDesc[]; #else #define THIS ((GB_IMAGE)_object) +#define THIS_HISTOGRAM ((CIMAGEHISTOGRAM *)_object) #endif +typedef + struct { + GB_BASE ob; + int *histogram; + } + CIMAGEHISTOGRAM; #endif diff --git a/main/lib/image.effect/kimageeffect.h b/main/lib/image.effect/kimageeffect.h index 0147ddb57..0c6e3db25 100644 --- a/main/lib/image.effect/kimageeffect.h +++ b/main/lib/image.effect/kimageeffect.h @@ -93,7 +93,8 @@ public: Green = 2, //!< Green channel Blue = 4, //!< Blue channel All = 7, //!< All channels - Gray = 15 //!< Grey channel + Alpha = 8 + //Gray = 15 //!< Grey channel }; /** diff --git a/main/lib/image.effect/main.cpp b/main/lib/image.effect/main.cpp index 5d1429c5d..56944adbc 100644 --- a/main/lib/image.effect/main.cpp +++ b/main/lib/image.effect/main.cpp @@ -1,22 +1,22 @@ /*************************************************************************** - main.cpp + main.cpp - (c) 2000-2009 Benoît Minisini + (c) 2000-2009 Benoît Minisini - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ***************************************************************************/ @@ -32,14 +32,15 @@ GB_INTERFACE IMAGE EXPORT; GB_DESC *GB_CLASSES[] EXPORT = { - CImageDesc, - NULL + ImageHistogramDesc, + CImageDesc, + NULL }; int EXPORT GB_INIT(void) { GB.GetInterface("gb.image", IMAGE_INTERFACE_VERSION, &IMAGE); - return 0; + return 0; } void EXPORT GB_EXIT()