summaryrefslogtreecommitdiff
path: root/lib/colord/cd-transform.c
blob: c9299fbfcec46caffcb795abb4427e681aeb071c (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2013 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * 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
 */

/**
 * SECTION:cd-transform
 * @short_description: An ICC transform
 *
 * This object is a simple ICC transform that allows mapping of simple RGB
 * spaces to other simple RGB spaces using one, two or three ICC profiles.
 *
 * This object is not supposed to re-implement LCMS, and if you need anything more
 * complicated then you are better off using lcms2 directly.
 */

#include "config.h"

#include <glib.h>
#include <lcms2.h>

#include "cd-context-lcms.h"
#include "cd-transform.h"

static void	cd_transform_class_init		(CdTransformClass	*klass);
static void	cd_transform_init		(CdTransform		*transform);
static void	cd_transform_finalize		(GObject		*object);

#define GET_PRIVATE(o) (cd_transform_get_instance_private (o))

/**
 * CdTransformPrivate:
 *
 * Private #CdTransform data
 **/
typedef struct
{
	CdIcc			*input_icc;
	CdIcc			*output_icc;
	CdIcc			*abstract_icc;
	CdPixelFormat		 input_pixel_format;
	CdPixelFormat		 output_pixel_format;
	CdRenderingIntent	 rendering_intent;
	cmsContext		 context_lcms;
	cmsHPROFILE		 srgb;
	cmsHTRANSFORM		 lcms_transform;
	gboolean		 bpc;
	guint			 max_threads;
	guint			 bpp_input;
	guint			 bpp_output;
} CdTransformPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (CdTransform, cd_transform, G_TYPE_OBJECT)

enum {
	PROP_0,
	PROP_BPC,
	PROP_RENDERING_INTENT,
	PROP_INPUT_PIXEL_FORMAT,
	PROP_OUTPUT_PIXEL_FORMAT,
	PROP_INPUT_ICC,
	PROP_OUTPUT_ICC,
	PROP_ABSTRACT_ICC,
	PROP_LAST
};

/**
 * cd_transform_error_quark:
 *
 * Return value: An error quark.
 *
 * Since: 0.1.34
 **/
GQuark
cd_transform_error_quark (void)
{
	static GQuark quark = 0;
	if (!quark)
		quark = g_quark_from_static_string ("cd_transform_error");
	return quark;
}

static void
cd_transform_invalidate (CdTransform *transform)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	if (priv->lcms_transform != NULL)
		cmsDeleteTransform (priv->lcms_transform);
	priv->lcms_transform = NULL;
}

/**
 * cd_transform_set_input_icc:
 * @transform: a #CdTransform instance.
 * @icc: a #CdIcc instance or %NULL.
 *
 * Sets the input profile to use for the transform.
 *
 * Since: 1.0.0
 **/
void
cd_transform_set_input_icc (CdTransform *transform, CdIcc *icc)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);

	g_return_if_fail (CD_IS_TRANSFORM (transform));
	g_return_if_fail (icc == NULL || CD_IS_ICC (icc));

	/* no change */
	if (priv->input_icc == icc)
		return;

	if (priv->input_icc != NULL)
		g_clear_object (&priv->input_icc);
	if (icc != NULL)
		priv->input_icc = g_object_ref (icc);
	cd_transform_invalidate (transform);
}

/**
 * cd_transform_get_input_icc:
 * @transform: a #CdTransform instance.
 *
 * Gets the input profile to use for the transform.
 *
 * Return value: (transfer none): The input profile
 *
 * Since: 1.0.0
 **/
CdIcc *
cd_transform_get_input_icc (CdTransform *transform)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	g_return_val_if_fail (CD_IS_TRANSFORM (transform), NULL);
	return priv->input_icc;
}

/**
 * cd_transform_set_output_icc:
 * @transform: a #CdTransform instance.
 * @icc: a #CdIcc instance or %NULL.
 *
 * Sets the output profile to use for the transform.
 *
 * Since: 1.0.0
 **/
void
cd_transform_set_output_icc (CdTransform *transform, CdIcc *icc)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);

	g_return_if_fail (CD_IS_TRANSFORM (transform));
	g_return_if_fail (icc == NULL || CD_IS_ICC (icc));

	/* no change */
	if (priv->output_icc == icc)
		return;

	if (priv->output_icc != NULL)
		g_clear_object (&priv->output_icc);
	if (icc != NULL)
		priv->output_icc = g_object_ref (icc);
	cd_transform_invalidate (transform);
}

/**
 * cd_transform_get_output_icc:
 * @transform: a #CdTransform instance.
 *
 * Gets the input profile to use for the transform.
 *
 * Return value: (transfer none): The output profile
 *
 * Since: 1.0.0
 **/
CdIcc *
cd_transform_get_output_icc (CdTransform *transform)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	g_return_val_if_fail (CD_IS_TRANSFORM (transform), NULL);
	return priv->output_icc;
}

/**
 * cd_transform_set_abstract_icc:
 * @transform: a #CdTransform instance.
 * @icc: a #CdIcc instance or %NULL.
 *
 * Sets the abstract profile to use for the transform.
 * This is typically only needed for soft-proofing.
 *
 * Since: 1.0.0
 **/
void
cd_transform_set_abstract_icc (CdTransform *transform, CdIcc *icc)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);

	g_return_if_fail (CD_IS_TRANSFORM (transform));
	g_return_if_fail (icc == NULL || CD_IS_ICC (icc));

	/* no change */
	if (priv->abstract_icc == icc)
		return;

	if (priv->abstract_icc != NULL)
		g_clear_object (&priv->abstract_icc);
	if (icc != NULL)
		priv->abstract_icc = g_object_ref (icc);
	cd_transform_invalidate (transform);
}

/**
 * cd_transform_get_abstract_icc:
 * @transform: a #CdTransform instance.
 *
 * Gets the abstract profile to use for the transform.
 *
 * Return value: (transfer none): The abstract profile
 *
 * Since: 1.0.0
 **/
CdIcc *
cd_transform_get_abstract_icc (CdTransform *transform)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	g_return_val_if_fail (CD_IS_TRANSFORM (transform), NULL);
	return priv->abstract_icc;
}

/**
 * cd_transform_set_input_pixel_format:
 * @transform: a #CdTransform instance.
 * @pixel_format: The pixel format, e.g. %CD_PIXEL_FORMAT_RGBA_8
 *
 * Sets the pixel format to use for the transform.
 *
 * Since: 1.0.0
 **/
void
cd_transform_set_input_pixel_format (CdTransform *transform, CdPixelFormat pixel_format)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);

	g_return_if_fail (CD_IS_TRANSFORM (transform));
	g_return_if_fail (pixel_format != CD_PIXEL_FORMAT_UNKNOWN);

	priv->input_pixel_format = pixel_format;
	cd_transform_invalidate (transform);
}

/**
 * cd_transform_get_input_pixel_format:
 * @transform: a #CdTransform instance.
 *
 * Gets the pixel format to use for the transform.
 *
 * Return value: the pixel format, e.g. %CD_PIXEL_FORMAT_RGBA_8
 *
 * Since: 1.0.0
 **/
CdPixelFormat
cd_transform_get_input_pixel_format (CdTransform *transform)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	g_return_val_if_fail (CD_IS_TRANSFORM (transform), CD_PIXEL_FORMAT_UNKNOWN);
	return priv->input_pixel_format;
}

/**
 * cd_transform_set_output_pixel_format:
 * @transform: a #CdTransform instance.
 * @pixel_format: The pixel format, e.g. %CD_PIXEL_FORMAT_RGBA_8
 *
 * Sets the pixel format to use for the transform.
 *
 * Since: 1.0.0
 **/
void
cd_transform_set_output_pixel_format (CdTransform *transform, CdPixelFormat pixel_format)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);

	g_return_if_fail (CD_IS_TRANSFORM (transform));
	g_return_if_fail (pixel_format != CD_PIXEL_FORMAT_UNKNOWN);

	priv->output_pixel_format = pixel_format;
	cd_transform_invalidate (transform);
}

/**
 * cd_transform_get_output_pixel_format:
 * @transform: a #CdTransform instance.
 *
 * Gets the pixel format to use for the transform.
 *
 * Return value: the pixel format, e.g. %CD_PIXEL_FORMAT_RGBA_8
 *
 * Since: 1.0.0
 **/
CdPixelFormat
cd_transform_get_output_pixel_format (CdTransform *transform)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	g_return_val_if_fail (CD_IS_TRANSFORM (transform), CD_PIXEL_FORMAT_UNKNOWN);
	return priv->output_pixel_format;
}

/**
 * cd_transform_set_rendering_intent:
 * @transform: a #CdTransform instance.
 * @rendering_intent: the rendering intent, e.g. %CD_RENDERING_INTENT_PERCEPTUAL
 *
 * Sets the rendering intent to use for the transform.
 *
 * Since: 1.0.0
 **/
void
cd_transform_set_rendering_intent (CdTransform *transform, CdRenderingIntent rendering_intent)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);

	g_return_if_fail (CD_IS_TRANSFORM (transform));
	g_return_if_fail (rendering_intent != CD_RENDERING_INTENT_UNKNOWN);

	priv->rendering_intent = rendering_intent;
	cd_transform_invalidate (transform);
}

/**
 * cd_transform_get_rendering_intent:
 * @transform: a #CdTransform instance.
 *
 * Gets the rendering intent to use for the transform.
 *
 * Return value: The rendering intent, e.g. %CD_RENDERING_INTENT_PERCEPTUAL
 *
 * Since: 1.0.0
 **/
CdRenderingIntent
cd_transform_get_rendering_intent (CdTransform *transform)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	g_return_val_if_fail (CD_IS_TRANSFORM (transform), CD_RENDERING_INTENT_UNKNOWN);
	return priv->rendering_intent;
}

/**
 * cd_transform_set_bpc:
 * @transform: a #CdTransform instance.
 * @bpc: black point compensation
 *
 * Sets if black point compensation should be used for the transform.
 *
 * Since: 1.0.0
 **/
void
cd_transform_set_bpc (CdTransform *transform, gboolean bpc)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);

	g_return_if_fail (CD_IS_TRANSFORM (transform));

	priv->bpc = bpc;
	cd_transform_invalidate (transform);
}

/**
 * cd_transform_get_bpc:
 * @transform: a #CdTransform instance.
 *
 * Gets the rendering bpc to use for the transform.
 *
 * Return value: If black point compensation should be used for the transform.
 *
 * Since: 1.0.0
 **/
gboolean
cd_transform_get_bpc (CdTransform *transform)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	g_return_val_if_fail (CD_IS_TRANSFORM (transform), FALSE);
	return priv->bpc;
}

/**
 * cd_transform_set_max_threads:
 * @transform: a #CdTransform instance.
 * @max_threads: number of threads, or 0 for the number of cores on the CPU
 *
 * Sets the maximum number of threads to be used for the transform.
 *
 * Since: 1.1.1
 **/
void
cd_transform_set_max_threads (CdTransform *transform, guint max_threads)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	g_return_if_fail (CD_IS_TRANSFORM (transform));
	priv->max_threads = max_threads;
}

/**
 * cd_transform_get_max_threads:
 * @transform: a #CdTransform instance.
 *
 * Gets the maximum number of threads to be used for the transform.
 *
 * Return value: number of threads
 *
 * Since: 1.1.1
 **/
guint
cd_transform_get_max_threads (CdTransform *transform)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	g_return_val_if_fail (CD_IS_TRANSFORM (transform), FALSE);
	return priv->max_threads;
}

/* map lcms intent to colord type */
const struct {
	gint					lcms;
	CdRenderingIntent			colord;
} map_rendering_intent[] = {
	{ INTENT_PERCEPTUAL,		CD_RENDERING_INTENT_PERCEPTUAL },
	{ INTENT_ABSOLUTE_COLORIMETRIC,	CD_RENDERING_INTENT_ABSOLUTE_COLORIMETRIC },
	{ INTENT_RELATIVE_COLORIMETRIC,	CD_RENDERING_INTENT_RELATIVE_COLORIMETRIC },
	{ INTENT_SATURATION,		CD_RENDERING_INTENT_SATURATION },
	{ 0,				CD_RENDERING_INTENT_LAST }
};

static guint
cd_transform_get_bpp (CdPixelFormat format)
{
	switch (format) {
	case CD_PIXEL_FORMAT_RGB24:
		return 3;
	case CD_PIXEL_FORMAT_ARGB32:
	case CD_PIXEL_FORMAT_CMYK32:
	case CD_PIXEL_FORMAT_BGRA32:
	case CD_PIXEL_FORMAT_RGBA32:
		return 4;
	case CD_PIXEL_FORMAT_UNKNOWN:
	default:
		return 0;
	}
}

static gboolean
cd_transform_setup (CdTransform *transform, GError **error)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	cmsHPROFILE profile_in;
	cmsHPROFILE profile_out;
	cmsUInt32Number lcms_flags = 0;
	gboolean ret = TRUE;
	gint lcms_intent = -1;
	guint i;
	g_autoptr(GError) error_local = NULL;

	/* find native rendering intent */
	for (i = 0; map_rendering_intent[i].colord != CD_RENDERING_INTENT_LAST; i++) {
		if (map_rendering_intent[i].colord == priv->rendering_intent) {
			lcms_intent = map_rendering_intent[i].lcms;
			break;
		}
	}
	g_assert (lcms_intent != -1);

	/* get input profile */
	if (priv->input_icc != NULL) {
		g_debug ("using input profile of %s",
			 cd_icc_get_filename (priv->input_icc));
		profile_in = cd_icc_get_handle (priv->input_icc);
	} else {
		g_debug ("no input profile, assume sRGB");
		profile_in = priv->srgb;
	}

	/* get output profile */
	if (priv->output_icc != NULL) {
		g_debug ("using output profile of %s",
			 cd_icc_get_filename (priv->output_icc));
		profile_out = cd_icc_get_handle (priv->output_icc);
	} else {
		g_debug ("no output profile, assume sRGB");
		profile_out = priv->srgb;
	}

	/* get flags */
	if (priv->bpc)
		lcms_flags |= cmsFLAGS_BLACKPOINTCOMPENSATION;

	/* get abstract profile */
	if (priv->abstract_icc != NULL) {
		cmsHPROFILE profiles[3];

		if (cd_icc_get_colorspace (priv->abstract_icc) != CD_COLORSPACE_LAB) {
			ret = FALSE;
			g_set_error_literal (error,
					     CD_TRANSFORM_ERROR,
					     CD_TRANSFORM_ERROR_INVALID_COLORSPACE,
					     "abstract colorspace has to be Lab");
			goto out;
		}

		/* generate a devicelink */
		profiles[0] = profile_in;
		profiles[1] = cd_icc_get_handle (priv->abstract_icc);
		profiles[2] = profile_out;
		priv->lcms_transform = cmsCreateMultiprofileTransformTHR (priv->context_lcms,
									  profiles,
									  3,
									  priv->input_pixel_format,
									  priv->output_pixel_format,
									  lcms_intent,
									  lcms_flags);

	} else {
		/* create basic transform */
		priv->lcms_transform = cmsCreateTransformTHR (priv->context_lcms,
							      profile_in,
							      priv->input_pixel_format,
							      profile_out,
							      priv->output_pixel_format,
							      lcms_intent,
							      lcms_flags);
	}

	/* find the bpp value */
	priv->bpp_input = cd_transform_get_bpp (priv->input_pixel_format);
	priv->bpp_output = cd_transform_get_bpp (priv->input_pixel_format);

	/* failed? */
	if (priv->lcms_transform == NULL) {
		ret = cd_context_lcms_error_check (priv->context_lcms, &error_local);
		if (!ret) {
			g_set_error_literal (error,
					     CD_TRANSFORM_ERROR,
					     CD_TRANSFORM_ERROR_FAILED_TO_SETUP_TRANSFORM,
					     error_local->message);
			goto out;
		}
		ret = FALSE;
		g_set_error_literal (error,
				     CD_TRANSFORM_ERROR,
				     CD_TRANSFORM_ERROR_FAILED_TO_SETUP_TRANSFORM,
				     "failed to setup transform, unspecified error");
		goto out;
	}
out:
	return ret;
}

typedef struct {
	guint8	*p_in;
	guint8	*p_out;
	guint	 width;
	guint	 rowstride;
	guint	 rows_to_process;
} CdTransformJob;

static void
cd_transform_process_func (gpointer data, gpointer user_data)
{
	CdTransformJob *job = (CdTransformJob *) data;
	CdTransform *transform = CD_TRANSFORM (user_data);
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	guint i;

	for (i = 0; i < job->rows_to_process; i++) {
		cmsDoTransformStride (priv->lcms_transform,
				      job->p_in,
				      job->p_out,
				      job->width,
				      job->rowstride);
		job->p_in += job->rowstride;
		job->p_out += job->rowstride;
	}
	g_slice_free (CdTransformJob, job);
}

static gboolean
cd_transform_set_max_threads_default (CdTransform *transform, GError **error)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	gchar *tmp;
	g_autofree gchar *data = NULL;

	/* use "cpu cores" to work out best number of threads */
	if (!g_file_get_contents ("/proc/cpuinfo", &data, NULL, error))
		return FALSE;
	tmp = g_strstr_len (data, -1, "cpu cores\t: ");
	if (tmp == NULL) {
		/* some processors do not provide this info */
		priv->max_threads = 1;
		return TRUE;
	}
	priv->max_threads = g_ascii_strtoull (tmp + 12, NULL, 10);
	if (priv->max_threads == 0) {
		priv->max_threads = 1;
		g_set_error_literal (error,
				     CD_TRANSFORM_ERROR,
				     CD_TRANSFORM_ERROR_LAST,
				     "Failed to parse number of cores");
		return FALSE;
	}
	return TRUE;
}

/**
 * cd_transform_process:
 * @transform: a #CdTransform instance.
 * @data_in: the data buffer to convert
 * @data_out: the data buffer to return, which can be the same as @data_in
 * @width: the width of @data_in
 * @height: the height of @data_in
 * @rowstride: the rowstride of @data_in, typically the same as @width
 * @cancellable: A %GError, or %NULL
 * @error: A %GCancellable, or %NULL
 *
 * Processes a block of data through the transform.
 * Once the transform has been setup it is cached and only re-created if any
 * of the formats, input, output or abstract profiles are changed.
 *
 * Return value: %TRUE if the pixels were successfully transformed.
 *
 * Since: 0.1.34
 **/
gboolean
cd_transform_process (CdTransform *transform,
		      gpointer data_in,
		      gpointer data_out,
		      guint width,
		      guint height,
		      guint rowstride,
		      GCancellable *cancellable,
		      GError **error)
{
	CdTransformJob *job;
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	gboolean ret = TRUE;
	GThreadPool *pool = NULL;
	guint8 *p_in;
	guint8 *p_out;
	guint i;
	guint rows_to_process;

	g_return_val_if_fail (CD_IS_TRANSFORM (transform), FALSE);
	g_return_val_if_fail (data_in != NULL, FALSE);
	g_return_val_if_fail (data_out != NULL, FALSE);
	g_return_val_if_fail (width != 0, FALSE);
	g_return_val_if_fail (height != 0, FALSE);
	g_return_val_if_fail (rowstride != 0, FALSE);

	/* check stuff that should have been set */
	if (priv->rendering_intent == CD_RENDERING_INTENT_UNKNOWN) {
		ret = FALSE;
		g_set_error_literal (error,
				     CD_TRANSFORM_ERROR,
				     CD_TRANSFORM_ERROR_FAILED_TO_SETUP_TRANSFORM,
				     "rendering intent not set");
		goto out;
	}
	if (priv->input_pixel_format == CD_PIXEL_FORMAT_UNKNOWN ||
	    priv->output_pixel_format == CD_PIXEL_FORMAT_UNKNOWN) {
		ret = FALSE;
		g_set_error_literal (error,
				     CD_TRANSFORM_ERROR,
				     CD_TRANSFORM_ERROR_FAILED_TO_SETUP_TRANSFORM,
				     "pixel format not set");
		goto out;
	}

	/* get the best number of threads */
	if (priv->max_threads == 0) {
		ret = cd_transform_set_max_threads_default (transform, error);
		if (!ret)
			goto out;
	}

	/* setup the transform if required */
	if (priv->lcms_transform == NULL) {
		ret = cd_transform_setup (transform, error);
		if (!ret)
			goto out;
	}

	/* non-threaded conversion */
	if (priv->max_threads == 1) {
		p_in = data_in;
		p_out = data_out;
		for (i = 0; i < height; i++) {
			cmsDoTransformStride (priv->lcms_transform,
					      p_in,
					      p_out,
					      width,
					      rowstride);
			p_in += rowstride * priv->bpp_input;
			p_out += rowstride * priv->bpp_output;
		}
		goto out;
	}

	/* create a threadpool for each line of the image */
	pool = g_thread_pool_new (cd_transform_process_func,
				  transform,
				  priv->max_threads,
				  TRUE,
				  error);
	if (pool == NULL)
		goto out;

	/* do conversion */
	p_in = data_in;
	p_out = data_out;
	rows_to_process = height / priv->max_threads;
	for (i = 0; i < height; i += rows_to_process) {
		job = g_slice_new (CdTransformJob);
		job->p_in = p_in;
		job->p_out = p_out;
		job->width = width;
		job->rowstride = rowstride;
		if (i + rows_to_process > height)
			job->rows_to_process = height - i;
		else
			job->rows_to_process = rows_to_process;
		ret = g_thread_pool_push (pool, job, error);
		if (!ret)
			goto out;
		p_in += rowstride * rows_to_process * priv->bpp_input;
		p_out += rowstride * rows_to_process * priv->bpp_output;
	}
out:
	if (pool != NULL)
		g_thread_pool_free (pool, FALSE, TRUE);
	return ret;
}

static void
cd_transform_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
	CdTransform *transform = CD_TRANSFORM (object);
	CdTransformPrivate *priv = GET_PRIVATE (transform);

	switch (prop_id) {
	case PROP_INPUT_ICC:
		g_value_set_object (value, priv->input_icc);
		break;
	case PROP_OUTPUT_ICC:
		g_value_set_object (value, priv->output_icc);
		break;
	case PROP_ABSTRACT_ICC:
		g_value_set_object (value, priv->abstract_icc);
		break;
	case PROP_RENDERING_INTENT:
		g_value_set_uint (value, priv->rendering_intent);
		break;
	case PROP_BPC:
		g_value_set_boolean (value, priv->bpc);
		break;
	case PROP_INPUT_PIXEL_FORMAT:
		g_value_set_uint (value, priv->input_pixel_format);
		break;
	case PROP_OUTPUT_PIXEL_FORMAT:
		g_value_set_uint (value, priv->output_pixel_format);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
cd_transform_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
	CdTransform *transform = CD_TRANSFORM (object);
	switch (prop_id) {
	case PROP_RENDERING_INTENT:
		cd_transform_set_rendering_intent (transform, g_value_get_uint (value));
		break;
	case PROP_BPC:
		cd_transform_set_bpc (transform, g_value_get_boolean (value));
		break;
	case PROP_INPUT_PIXEL_FORMAT:
		cd_transform_set_input_pixel_format (transform, g_value_get_uint (value));
		break;
	case PROP_OUTPUT_PIXEL_FORMAT:
		cd_transform_set_output_pixel_format (transform, g_value_get_uint (value));
		break;
	case PROP_INPUT_ICC:
		cd_transform_set_input_icc (transform, g_value_get_object (value));
		break;
	case PROP_OUTPUT_ICC:
		cd_transform_set_output_icc (transform, g_value_get_object (value));
		break;
	case PROP_ABSTRACT_ICC:
		cd_transform_set_abstract_icc (transform, g_value_get_object (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
cd_transform_class_init (CdTransformClass *klass)
{
	GParamSpec *pspec;
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = cd_transform_finalize;
	object_class->get_property = cd_transform_get_property;
	object_class->set_property = cd_transform_set_property;

	/**
	 * CdTransform: rendering-intent:
	 */
	pspec = g_param_spec_uint ("rendering-intent", NULL, NULL,
				   0, G_MAXUINT, 0,
				   G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_RENDERING_INTENT, pspec);

	/**
	 * CdTransform: bpc:
	 */
	pspec = g_param_spec_boolean ("bpc", NULL, NULL,
				      FALSE,
				      G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_BPC, pspec);

	/**
	 * CdTransform: input-pixel-format:
	 */
	pspec = g_param_spec_uint ("input-pixel-format", NULL, NULL,
				   0, G_MAXUINT, 0,
				   G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_INPUT_PIXEL_FORMAT, pspec);

	/**
	 * CdTransform: output-pixel-format:
	 */
	pspec = g_param_spec_uint ("output-pixel-format", NULL, NULL,
				   0, G_MAXUINT, 0,
				   G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_OUTPUT_PIXEL_FORMAT, pspec);

	/**
	 * CdTransform: input-icc:
	 */
	pspec = g_param_spec_object ("input-icc", NULL, NULL,
				     CD_TYPE_ICC,
				     G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_INPUT_ICC, pspec);

	/**
	 * CdTransform: output-icc:
	 */
	pspec = g_param_spec_object ("output-icc", NULL, NULL,
				     CD_TYPE_ICC,
				     G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_OUTPUT_ICC, pspec);

	/**
	 * CdTransform: abstract-icc:
	 */
	pspec = g_param_spec_object ("abstract-icc", NULL, NULL,
				     CD_TYPE_ICC,
				     G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_ABSTRACT_ICC, pspec);
}

static void
cd_transform_init (CdTransform *transform)
{
	CdTransformPrivate *priv = GET_PRIVATE (transform);
	priv->context_lcms = cd_context_lcms_new ();
	priv->rendering_intent = CD_RENDERING_INTENT_UNKNOWN;
	priv->input_pixel_format = CD_PIXEL_FORMAT_UNKNOWN;
	priv->output_pixel_format = CD_PIXEL_FORMAT_UNKNOWN;
	priv->srgb = cmsCreate_sRGBProfileTHR (priv->context_lcms);
	priv->max_threads = 1;
}

static void
cd_transform_finalize (GObject *object)
{
	CdTransform *transform = CD_TRANSFORM (object);
	CdTransformPrivate *priv = GET_PRIVATE (transform);

	cmsCloseProfile (priv->srgb);
	if (priv->input_icc != NULL)
		g_object_unref (priv->input_icc);
	if (priv->output_icc != NULL)
		g_object_unref (priv->output_icc);
	if (priv->abstract_icc != NULL)
		g_object_unref (priv->abstract_icc);
	if (priv->lcms_transform != NULL)
		cmsDeleteTransform (priv->lcms_transform);
	cd_context_lcms_free (priv->context_lcms);

	G_OBJECT_CLASS (cd_transform_parent_class)->finalize (object);
}

/**
 * cd_transform_new:
 *
 * Creates a new #CdTransform object.
 *
 * Return value: a new CdTransform object.
 *
 * Since: 0.1.34
 **/
CdTransform *
cd_transform_new (void)
{
	CdTransform *transform;
	transform = g_object_new (CD_TYPE_TRANSFORM, NULL);
	return CD_TRANSFORM (transform);
}