File: sRGB2HSV.c

package info (click to toggle)
vips 8.18.0-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 54,344 kB
  • sloc: ansic: 172,638; cpp: 12,257; python: 5,077; sh: 773; perl: 40; makefile: 25; javascript: 6
file content (182 lines) | stat: -rw-r--r-- 3,999 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* to HSV ... useful for compatibility with other packages
 *
 * 9/6/15
 * 	- from LabS2Lab.c
 */

/*

	This file is part of VIPS.

	VIPS 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 of the License, 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 Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
	02110-1301  USA

 */

/*

	These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>

#include <stdio.h>

#include <vips/vips.h>

#include "pcolour.h"

typedef VipsColourCode VipssRGB2HSV;
typedef VipsColourCodeClass VipssRGB2HSVClass;

G_DEFINE_TYPE(VipssRGB2HSV, vips_sRGB2HSV, VIPS_TYPE_COLOUR_CODE);

static void
vips_sRGB2HSV_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++) {
		unsigned char c_max;
		unsigned char c_min;
		float secondary_diff;
		float wrap_around_hue;

		if (p[1] < p[2]) {
			if (p[2] < p[0]) {
				/* Center red (at top).
				 */
				c_max = p[0];
				c_min = p[1];
				secondary_diff = p[1] - p[2];
				wrap_around_hue = 255.0F;
			}
			else {
				/* Center blue.
				 */
				c_max = p[2];
				c_min = VIPS_MIN(p[1], p[0]);
				secondary_diff = p[0] - p[1];
				wrap_around_hue = 170.0F;
			}
		}
		else {
			if (p[1] < p[0]) {
				/* Center red (at bottom)
				 */
				c_max = p[0];
				c_min = p[2];
				secondary_diff = p[1] - p[2];
				wrap_around_hue = 0.0F;
			}
			else {
				/* Center green
				 */
				c_max = p[1];
				c_min = VIPS_MIN(p[2], p[0]);
				secondary_diff = p[2] - p[0];
				wrap_around_hue = 85.0F;
			}
		}

		if (c_max == 0) {
			q[0] = 0;
			q[1] = 0;
			q[2] = 0;
		}
		else {
			unsigned char delta;

			q[2] = c_max;
			delta = c_max - c_min;

			if (delta == 0)
				q[0] = 0;
			else
				q[0] = 42.5 * (secondary_diff / (float) delta) +
					wrap_around_hue;

			q[1] = delta * 255.0 / (float) c_max;
		}

		p += 3;
		q += 3;
	}
}

static void
vips_sRGB2HSV_class_init(VipssRGB2HSVClass *class)
{
	VipsObjectClass *object_class = (VipsObjectClass *) class;
	VipsColourClass *colour_class = VIPS_COLOUR_CLASS(class);

	object_class->nickname = "sRGB2HSV";
	object_class->description = _("transform sRGB to HSV");

	colour_class->process_line = vips_sRGB2HSV_line;
}

static void
vips_sRGB2HSV_init(VipssRGB2HSV *sRGB2HSV)
{
	VipsColour *colour = VIPS_COLOUR(sRGB2HSV);
	VipsColourCode *code = VIPS_COLOUR_CODE(sRGB2HSV);

	colour->interpretation = VIPS_INTERPRETATION_HSV;
	colour->format = VIPS_FORMAT_UCHAR;
	colour->bands = 3;
	colour->input_bands = 3;

	code->input_coding = VIPS_CODING_NONE;
	code->input_format = VIPS_FORMAT_UCHAR;
	code->input_interpretation = VIPS_INTERPRETATION_sRGB;
}

/**
 * vips_sRGB2HSV: (method)
 * @in: input image
 * @out: (out): output image
 * @...: `NULL`-terminated list of optional named arguments
 *
 * Convert to HSV.
 *
 * HSV is a crude polar coordinate system for RGB images. It is provided for
 * compatibility with other image processing systems. See [method@Image.Lab2LCh] for a
 * much better colour space.
 *
 * ::: seealso
 *     [method@Image.HSV2sRGB], [method@Image.Lab2LCh].
 *
 * Returns: 0 on success, -1 on error.
 */
int
vips_sRGB2HSV(VipsImage *in, VipsImage **out, ...)
{
	va_list ap;
	int result;

	va_start(ap, out);
	result = vips_call_split("sRGB2HSV", ap, in, out);
	va_end(ap);

	return result;
}