📄 rgb2hsv.cpp
字号:
//
// Rgb2Hsv
// Converts an input color image into an output hue, saturation, value image.
// This code is directly derived from http://www.cs.rit.edu/~ncs/color/t_convert.html.
//
// Copyright (C) 2006 by Jon A. Webb (Contact via GMail; username is jonawebb)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#include "Rgb2Hsv.h"
#include "Debug.h"
#include "GrayImageIter.h"
#include "Image.h"
#include "ImageSize.h"
#include "Replicate.h"
#include "RGBImageIter.h"
#include <e32std.h>
#include <fbs.h>
using namespace Core;
namespace Algorithm
{
EXPORT_C CRgb2Hsv* CRgb2Hsv::NewL()
{
return new (ELeave) CRgb2Hsv();
}
CRgb2Hsv::~CRgb2Hsv()
{
}
CRgb2Hsv::CRgb2Hsv() :
ibEmpty(true)
{
}
CImage CRgb2Hsv::FrontL()
{
if (ibEmpty) {
User::Leave(KErrGeneral);
return CImage();
}
ibEmpty = true;
return iImage;
}
// RGBtoHSV
// Converts input rgb color value to HSV value
// All data are unsigned char
// r,g,b values are from 0 to 255
// h = [0,242],255
// magenta = 0 red = 40 yellow = 80
// yellowish-green = 81 green = 121 cyan = 161
// cyanish-blue = 162 blue = 202 bluish-magenta = 242
// if s == 0 or r = g = b, then h = 255 (undefined)
// s = [0,255],
// v = [0,255]
//
void CRgb2Hsv::RGBtoHSV(
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char *h,
unsigned char *s,
unsigned char *v )
{
int min, max, delta;
if (r > g) { // r > g
if (g > b) { // r > g > b
max = r;
min = b;
} else { // r > g, b >= g
min = g;
if (r > b) { // r > b >= g
max = r;
} else { // b >= r > g
max = b;
}
}
} else { // g >= r
if (r > b) { // g >= r > b
max = g;
min = b;
} else { // g >= r, b >= r
min = r;
if (g > b) { // g > b >= r
max = g;
} else { // b >= g >= r
max = b;
}
}
}
*v = (unsigned char) max; // v
delta = max - min;
if( max != 0 )
*s = (unsigned char) (255 * int(delta) / max); // s
else {
// r = g = b = 0 // s = 0, v is undefined
*s = 0;
*h = 255;
return;
}
if (delta == 0) {
*h = 255;
} else {
if( r == max )
*h = (unsigned char) (40 + 40 * ( g - b ) / delta); // magenta = 0 red = 40 yellow = 80
else if( g == max )
*h = (unsigned char) (121 + 40 * ( b - r ) / delta); // yellow = 81- green = 121 cyan = 161
else
*h = (unsigned char) (202 + 40 * ( r - g ) / delta); // cyan = 162- blue = 202 magenta = 242+
}
}
void CRgb2Hsv::PushL(CImage image)
{
image.LockLC();
IRgbImageIter rRgb(image);
for (; !rRgb.End(); rRgb.NextRow()) {
for (; !rRgb.REnd(); rRgb++) {
unsigned char h, s, v;
RGBtoHSV(rRgb[R], rRgb[G], rRgb[B], &h, &s, &v);
rRgb[R] = h;
rRgb[G] = s;
rRgb[B] = v;
}
}
CleanupStack::PopAndDestroy(); // unlock bitmaps
iImage = image;
CDebug::ShowImage(iImage);
ibEmpty = false;
}
bool CRgb2Hsv::Empty() const
{
return ibEmpty;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -