From b24c7ee54647fe5c87a9a3e52de543348f505842 Mon Sep 17 00:00:00 2001 From: JonDeen Date: Fri, 12 Jun 2015 01:40:00 +0200 Subject: [PATCH] Initial HSV --- libvips/colour/HSV2sRGB.c | 53 +++++++++++++++++++++++++++++++++------ libvips/colour/sRGB2HSV.c | 12 ++++++--- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/libvips/colour/HSV2sRGB.c b/libvips/colour/HSV2sRGB.c index f7df9d88..14c006f5 100644 --- a/libvips/colour/HSV2sRGB.c +++ b/libvips/colour/HSV2sRGB.c @@ -38,26 +38,65 @@ #include +#include + #include #include "pcolour.h" +#define sixth_of_char 42.5 +#define H p[0] +#define S p[1] +#define V p[2] +#define R p[0] +#define G p[1] +#define B p[2] + typedef VipsColourCode VipsHSV2sRGB; typedef VipsColourCodeClass VipsHSV2sRGBClass; G_DEFINE_TYPE( VipsHSV2sRGB, vips_HSV2sRGB, VIPS_TYPE_COLOUR_CODE ); -static void -vips_HSV2sRGB_line( VipsColour *colour, VipsPel *out, VipsPel **in, int width ) -{ +static void vips_HSV2sRGB_line(VipsColour *colour, VipsPel *out, VipsPel **in, + int width) { unsigned char *p = (unsigned char *) in[0]; unsigned char *q = (unsigned char *) out; int i; - for( i = 0; i < width; i++ ) { - q[0] = p[0]; - q[1] = p[1]; - q[2] = p[2]; + + + float c, x, m, s, t, u; + for (i = 0; i < width; i++) { + + c = V* S / 255.0f; + x = C * (1 - abs(fmod(H / sixth_of_char, 2) - 1)); + m = V - c; + + if (H < sixth_of_char) { + R= (c+m); + G= (x+m); + B= (0+m); + } else if (H < 2*sixth_of_char) { + R= (x+m); + G= (c+m); + B= (0+m); + } else if (H < 3*sixth_of_char) { + R= (0+m); + G= (c+m); + B= (x+m); + } else if (H < 4*sixth_of_char) { + R= (0+m); + G= (x+m); + B= (c+m); + } else if (H < 5*sixth_of_char) { + R= (x+m); + G= (0+m); + B= (c+m); + } else { + R= (c+m); + G= (0+m); + B= (x+m); + } p += 3; q += 3; diff --git a/libvips/colour/sRGB2HSV.c b/libvips/colour/sRGB2HSV.c index 82802b09..e45aa155 100644 --- a/libvips/colour/sRGB2HSV.c +++ b/libvips/colour/sRGB2HSV.c @@ -58,6 +58,10 @@ vips_sRGB2HSV_line( VipsColour *colour, VipsPel *out, VipsPel **in, int width ) float wrap_around_hue, secondary_diff; + #define RED 255.0f; + #define GREEN 85.0f; + #define BLUE 170.0f; + for( i = 0; i < width; i++ ) { @@ -66,12 +70,12 @@ vips_sRGB2HSV_line( VipsColour *colour, VipsPel *out, VipsPel **in, int width ) c_max = p[0]; c_min = p[1]; secondary_diff = p[1] - p[2]; - wrap_around_hue = 255.0f; + wrap_around_hue = RED; } else { c_max = p[2]; c_min = VIPS_MIN(p[1], p[0]); secondary_diff = p[0] - p[1]; - wrap_around_hue = 170.0f; + wrap_around_hue = BLUE; } } else { if (p[1] < p[0]) { @@ -83,7 +87,7 @@ vips_sRGB2HSV_line( VipsColour *colour, VipsPel *out, VipsPel **in, int width ) c_max = p[1]; c_min = VIPS_MIN(p[2], p[0]); secondary_diff = p[2] - p[0]; - wrap_around_hue = 85.0f; + wrap_around_hue = GREEN; } } @@ -99,7 +103,7 @@ vips_sRGB2HSV_line( VipsColour *colour, VipsPel *out, VipsPel **in, int width ) if (delta == 0) { q[0] = 0; } else { - q[0] = (unsigned char) (42.5f*(secondary_diff / (float) delta) + wrap_around_hue); + q[0] = (42.5f*(secondary_diff / (float) delta) + wrap_around_hue); } q[1] = (( delta*255.0f / (float) c_max));