File: UCS2LCh.c

package info (click to toggle)
vips 8.4.5-1%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 25,724 kB
  • sloc: ansic: 102,605; cpp: 57,527; sh: 4,957; xml: 3,317; python: 3,105; makefile: 1,089; perl: 40
file content (310 lines) | stat: -rw-r--r-- 5,893 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Turn CMC to LCh
 *
 * 15/11/94 JC
 *	- error messages added
 *	- memory leak fixed
 * 16/11/94 JC
 *	- uses im_wrap_oneonebuf() now
 * 2/11/09
 * 	- gtkdoc
 * 30/11/09
 * 	- argh, im_col_make_tables_CMC(); missing, thanks Peter
 * 19/9/12
 * 	- redone as a class
 */

/*

    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 <vips/intl.h>

#include <stdio.h>
#include <math.h>

#include <vips/vips.h>

#include "pcolour.h"

/* Arrays for lookup tables for the inverse function.
 */
static float LI[1001];
static float CI[3001];
static float hI[101][361];

typedef VipsColourTransform VipsCMC2LCh;
typedef VipsColourTransformClass VipsCMC2LChClass;

G_DEFINE_TYPE( VipsCMC2LCh, vips_CMC2LCh, VIPS_TYPE_COLOUR_TRANSFORM );

/* Generate LI (inverse) tables. 
 */
static void
make_LI( void )
{
	int i;
	float Ll[1001];

	for( i = 0; i < 1001; i++ ) 
		Ll[i] = vips_col_L2Lcmc( i / 10.0 ); 

	for( i = 0; i < 1001; i++ ) {
		int j;

		/* Must be 1000, since j will be +1 on exit.
		 */
		for( j = 0; j < 1000 && Ll[j] <= i / 10.0; j++ ) 
			;

		LI[i] = (j - 1) / 10.0 + 
			(i / 10.0 - Ll[j - 1]) / ((Ll[j] - Ll[j - 1]) * 10.0);
	}
}

/* Generate Ccmc table. 
 */
static void
make_CI( void )
{
	int i;
	float Cl[3001];

	for( i = 0; i < 3001; i++ ) 
		Cl[i] = vips_col_C2Ccmc( i / 10.0 ); 

	for( i = 0; i < 3001; i++ ) {
		int j;

		/* Must be 3000, since j will be +1 on exit.
		 */
		for( j = 0; j < 3000 && Cl[j] <= i / 10.0; j++ )
			;

		CI[i] = (j - 1) / 10.0 + 
			(i / 10.0 - Cl[j - 1]) / ((Cl[j] - Cl[j - 1]) * 10.0);
	}
}

/* The difficult one: hcmc. 
 */
static void
make_hI( void )
{
	int i, j;
	float hl[101][361];

	for( i = 0; i < 361; i++ ) 
		for( j = 0; j < 101; j++ ) 
			hl[j][i] = vips_col_Ch2hcmc( j * 2.0, i );

	for( j = 0; j < 101; j++ ) {
		for( i = 0; i < 361; i++ ) {
			int k;

			for( k = 0; k < 360 && hl[j][k] <= i; k++ ) 
				;

			hI[j][i] = k - 1 + (i - hl[j][k - 1]) / 
				(hl[j][k] - hl[j][k - 1]);
		}
	}
}

/**
 * vips_col_Lcmc2L:
 * @Lcmc: L cmc
 *
 * Calculate L from Lcmc using a table. Call vips_col_make_tables_CMC() at
 * least once before using this function.
 *
 * Returns: L*
 */
float
vips_col_Lcmc2L( float Lcmc )
{	
	int known;

	known = VIPS_FLOOR( Lcmc * 10.0 );
	known = VIPS_CLIP( 0, known, 999 );

	return( LI[known] + 
		(LI[known + 1] - LI[known]) * (Lcmc * 10.0 - known) );
}

/**
 * vips_col_Ccmc2C:
 * @Ccmc: Ccmc
 *
 * Calculate C from Ccmc using a table. 
 * Call vips_col_make_tables_CMC() at
 * least once before using this function.
 *
 * Returns: C.
 */
float
vips_col_Ccmc2C( float Ccmc )
{	
	int known;

	known = VIPS_FLOOR( Ccmc * 10.0 );
	known = VIPS_CLIP( 0, known, 2999 );

	return( CI[known] + 
		(CI[known + 1] - CI[known]) * (Ccmc * 10.0 - known) );
}

/**
 * vips_col_Chcmc2h:
 * @C: Chroma
 * @hcmc: Hue cmc (degrees)
 *
 * Calculate h from C and hcmc, using a table.
 * Call vips_col_make_tables_CMC() at
 * least once before using this function.
 *
 * Returns: h.
 */
float
vips_col_Chcmc2h( float C, float hcmc )
{	
	int r;
	int known;

	/* Which row of the table?
	 */
	r = (int) ((C + 1.0) / 2.0);
	r = VIPS_CLIP( 0, r, 99 ); 

	known = VIPS_FLOOR( hcmc );
	known = VIPS_CLIP( 0, known, 359 ); 

	return( hI[r][known] + 
		(hI[r][(known + 1) % 360] - hI[r][known]) * (hcmc - known) );
}

static void *
tables_init( void *client )
{	
	make_LI();
	make_CI();
	make_hI();

	return( NULL );
}

/**
 * vips_col_make_tables_CMC:
 * 
 * Make the lookup tables for cmc.
 */
void
vips_col_make_tables_CMC( void )
{
	static GOnce once = G_ONCE_INIT;

	(void) g_once( &once, tables_init, NULL );
}

/* Process a buffer of data.
 */
void
vips_CMC2LCh_line( VipsColour *colour, VipsPel *out, VipsPel **in, int width )
{
	float *p = (float *) in[0];
	float *q = (float *) out;

	int x;

	for( x = 0; x < width; x++ ) {
		float Lcmc = p[0];
		float Ccmc = p[1];
		float hcmc = p[2];

		/* Turn from CMC.
		 */
		float C = vips_col_Ccmc2C( Ccmc );
		float h = vips_col_Chcmc2h( C, hcmc );
		float L = vips_col_Lcmc2L( Lcmc );

		p += 3;

		q[0] = L;
		q[1] = C;
		q[2] = h;

		q += 3;
	}
}

static void
vips_CMC2LCh_class_init( VipsCMC2LChClass *class )
{
	VipsObjectClass *object_class = (VipsObjectClass *) class;
	VipsColourClass *colour_class = VIPS_COLOUR_CLASS( class );

	object_class->nickname = "CMC2LCh";
	object_class->description = _( "transform LCh to CMC" );

	colour_class->process_line = vips_CMC2LCh_line;
}

static void
vips_CMC2LCh_init( VipsCMC2LCh *CMC2LCh )
{
	VipsColour *colour = VIPS_COLOUR( CMC2LCh );

	vips_col_make_tables_CMC();
	colour->interpretation = VIPS_INTERPRETATION_LCH;
}

/**
 * vips_CMC2LCh:
 * @in: input image
 * @out: output image
 * @...: %NULL-terminated list of optional named arguments
 *
 * Turn LCh to CMC.
 *
 * See also: vips_LCh2CMC(). 
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_CMC2LCh( VipsImage *in, VipsImage **out, ... )
{
	va_list ap;
	int result;

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

	return( result );
}