File: XYZ2CMYK.c

package info (click to toggle)
vips 8.14.1-3%2Bdeb12u2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 35,912 kB
  • sloc: ansic: 165,449; cpp: 10,987; python: 4,462; xml: 4,212; sh: 471; perl: 40; makefile: 23
file content (239 lines) | stat: -rw-r--r-- 5,926 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
/* Use lcms to move from XYZ to CMYK, if we can. This needs a working
 * vips_icc_export.
 *
 * 21/12/18
 *      - from CMYK2XYZ.c
 * 09/01/2019
 *  	- add CMYK <-> XYZ conversions if no lcms2 has been found
 */

/*

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

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

#include <vips/internal.h>

#include "pcolour.h"
#include "profiles.h"

#ifdef HAVE_LCMS2

typedef struct _VipsXYZ2CMYK {
	VipsOperation parent_instance;

	VipsImage *in;
	VipsImage *out;
} VipsXYZ2CMYK;

typedef VipsColourCodeClass VipsXYZ2CMYKClass;

G_DEFINE_TYPE( VipsXYZ2CMYK, vips_XYZ2CMYK, VIPS_TYPE_OPERATION );

/* Our actual processing, as a VipsColourTransformFn.
 */
static int
vips_XYZ2CMYK_process( VipsImage *in, VipsImage **out, ... )
{
	return( vips_icc_export( in, out,
		"pcs", VIPS_PCS_XYZ,
		NULL ) );
}

static int
vips_XYZ2CMYK_build( VipsObject *object )
{
	VipsXYZ2CMYK *XYZ2CMYK = (VipsXYZ2CMYK *) object;
	VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 );

	VipsImage *out; 

	if( VIPS_OBJECT_CLASS( vips_XYZ2CMYK_parent_class )->build( object ) )
		return( -1 );

	out = vips_image_new();
	g_object_set( object, "out", out, NULL ); 

	if( vips_copy( XYZ2CMYK->in, &t[0], NULL ) ||
		vips__profile_set( t[0], "cmyk" ) ||
		vips__colourspace_process_n( "XYZ2CMYK", 
			t[0], &t[1], 3, vips_XYZ2CMYK_process ) ||
		vips_image_write( t[1], out ) )
		return( -1 );

	return( 0 );
}

static void
vips_XYZ2CMYK_class_init( VipsXYZ2CMYKClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *object_class = (VipsObjectClass *) class;
	VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( class );

	gobject_class->set_property = vips_object_set_property;
	gobject_class->get_property = vips_object_get_property;

	object_class->nickname = "XYZ2CMYK";
	object_class->description = _( "transform XYZ to CMYK" );

	object_class->build = vips_XYZ2CMYK_build;
	operation_class->flags = VIPS_OPERATION_SEQUENTIAL;

	VIPS_ARG_IMAGE( class, "in", 1,
		_( "Input" ),
		_( "Input image" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsXYZ2CMYK, in ) );

	VIPS_ARG_IMAGE( class, "out", 100,
		_( "Output" ),
		_( "Output image" ),
		VIPS_ARGUMENT_REQUIRED_OUTPUT,
		G_STRUCT_OFFSET( VipsXYZ2CMYK, out ) );
}

static void
vips_XYZ2CMYK_init( VipsXYZ2CMYK *XYZ2CMYK )
{
}

#else /*!HAVE_LCMS2*/

typedef VipsColourCode VipsXYZ2CMYK;
typedef VipsColourCodeClass VipsXYZ2CMYKClass;

G_DEFINE_TYPE(VipsXYZ2CMYK, vips_XYZ2CMYK, VIPS_TYPE_COLOUR_CODE);

void
vips_XYZ2CMYK_line( VipsColour *colour, VipsPel *out, VipsPel **in, int width )
{
	float *p = (float *) in[0];
	unsigned char *q = (unsigned char *) out;

	const float epsilon = 0.00001;

	int i;

	for( i = 0; i < width; i++ ) {
		float r = p[0] / VIPS_D65_X0;
		float g = p[1] / VIPS_D65_Y0;
		float b = p[2] / VIPS_D65_Z0;

		float c = 1.0 - r;
		float m = 1.0 - g;
		float y = 1.0 - b;
		float k = VIPS_MIN( c, VIPS_MIN( m, y ) );
		float ik = 1.0 - k;

		if( ik < epsilon ) {
			q[0] = 255;
			q[1] = 255;
			q[2] = 255;
			q[3] = 255;
		}
		else {
			q[0] = VIPS_CLIP( 0, 255 * (c - k) / ik, 255 );
			q[1] = VIPS_CLIP( 0, 255 * (m - k) / ik, 255 );
			q[2] = VIPS_CLIP( 0, 255 * (y - k) / ik, 255 );
			q[3] = VIPS_CLIP( 0, 255 * k, 255 );
		}

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

static void
vips_XYZ2CMYK_class_init( VipsXYZ2CMYKClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *object_class = (VipsObjectClass *) class;
	VipsColourClass *colour_class = VIPS_COLOUR_CLASS( class );

	gobject_class->set_property = vips_object_set_property;
	gobject_class->get_property = vips_object_get_property;

	object_class->nickname = "XYZ2CMYK";
	object_class->description = _( "transform XYZ to CMYK" );
    
	colour_class->process_line = vips_XYZ2CMYK_line;
}

static void
vips_XYZ2CMYK_init( VipsXYZ2CMYK *XYZ2CMYK )
{
	VipsColour *colour = VIPS_COLOUR( XYZ2CMYK );
	VipsColourCode *code = VIPS_COLOUR_CODE( XYZ2CMYK );

	colour->interpretation = VIPS_INTERPRETATION_CMYK;
	colour->format = VIPS_FORMAT_UCHAR;
	colour->bands = 4;
	colour->input_bands = 3;

	code->input_coding = VIPS_CODING_NONE;
	code->input_format = VIPS_FORMAT_FLOAT;
	code->input_interpretation = VIPS_INTERPRETATION_XYZ;
}

#endif /*HAVE_LCMS2*/

/**
 * vips_XYZ2CMYK: (method)
 * @in: input image
 * @out: (out): output image
 * @...: %NULL-terminated list of optional named arguments
 *
 * Turn XYZ to CMYK. If the image has an embedded ICC profile this will be
 * used for the conversion. If there is no embedded profile, a generic
 * fallback profile will be used. 
 *
 * Conversion is from D65 XYZ with relative intent. If you need more control 
 * over the process, use vips_icc_export() instead.
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_XYZ2CMYK( VipsImage *in, VipsImage **out, ... )
{
	va_list ap;
	int result;

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

	return( result );
}