summaryrefslogtreecommitdiffstats
path: root/src/powervr_iep_lite/csc/csc2.c
blob: d99d0dd993cbd4cc58c51f4a1803a8d813356d71 (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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/******************************************************************************
 @file   : csc2.c

         <b>Copyright 2008 by Imagination Technologies Limited.</b>\n
         All rights reserved.  No part of this software, either
         material or conceptual may be copied or distributed,
         transmitted, transcribed, stored in a retrieval system
         or translated into any human or computer language in any
         form by any means, electronic, mechanical, manual or
         other-wise, or disclosed to third parties without the
         express written permission of Imagination Technologies
         Limited, Unit 8, HomePark Industrial Estate,
         King's Langley, Hertfordshire, WD4 8LZ, U.K.

 <b>Description:</b>\n
         This module contains functions for calculating a 3x3 colour
		 space conversion matrix.

******************************************************************************/

/********************************************************************
	DISCLAIMER:
	This code is provided for demonstration purposes only. It has
	been ported from other projects and has not been tested with
	real hardware. It is not provided in a state that can be run
	with real hardware - this is not intended as the basis for a
	production driver. This code should only be used as an example 
	of the algorithms to be used for setting up the IEP lite 
	hardware.
 ********************************************************************/

#include <memory.h>

#include "img_iep_defs.h"
#include "csc2.h"
#include "csc2_data.h"
#include "fixedpointmaths.h"

/*************************************************************************************************************
	
	The combined equations for a general purpose transfer (CSC combined with Procamp) are shown below:
	(matrices in CAPITALS, scaler multiplies in lower case).

	As the colour primary conversions needs to be performed on RGB data, and the procamp (H/S/C) adjustment 
	needs to be performed on YUV data,  there are 4 cases to consider. All cases have the same input and 
	output offsets shown in YUV to RGB.

	If no HSBC adjustment is required, leave out the conSatHue matrix, though see the RGB-RGB case 
	for the exception. If no primary adjustment is required, leave out the inoutPrimaries matrix, 
	though see YUV-YUV case for the exception.

	Where a matrix name begins with 'INOUT_', the matrix has been pre-generated to convert from one
	thing to another (e.g.: from an input colour space to an output colour space, or from an input primary
	to an output primary) - the 'INOUT_' matrix is just chosen from a table of pregenerated matrices
	based on the user's input and output selections.

	Input and output offsets
	In all cases, the input offsets are negative and account for differences in range. As the internal
	range for these calculations is always 0-255 (the pre-calculated matrices are based on this internal
	range), the input offsets are always either 0 (for input range 0->255), -16 (for input range 16->235),
	or -48 (for input range 48->208). The same principle applies to the output ranges, but the output
	offsets based on the output range are positive rather than negative (0, +16 or +48). In addition, the
	user can specify a brightness value in the range -50 -> +50, which is added to the output offsets
	(only the Y offset in the case of YUV output).

	-------------------------------------------------------------------------------------------------------
	CASE 1) Input YUV and output YUV:

	combinedMatrix = (outputrangeScale*OUT_TRANSFER_RGB_TO_YUV)*INOUT_PRIMARIES*
						(inputrangeScale*IN_TRANSFER_YUV_TO_RGB)*CON_SAT_HUE

	An arbitrary intermediate RGB format is used to perform the primary conversion 
	(colour space=RGB, range=0-255). Note: The pregenerated primary conversion matrices are created
	based on this arbitrary intermediate format.

	So IN_TRANSFER_YUV_TO_RGB is the same as  INOUT_TRANSFER[(user defined input colour space) to RGB]
	And OUT_TRANSFER_RGB_TO_YUV is the same as  INOUT_TRANSFER[RGB to (user defined output colour space)]

	OPTIMISATION FOR CASE 1)
		If no primaries adjustment is required (i.e.:input primary = output primary), then the equation 
		simplifies to:

		combinedMatrix = CON_SAT_HUE*(outputrangeScale*inputrangeScale*INOUT_TRANSFER_YUV_TO_YUV)
	-------------------------------------------------------------------------------------------------------

	-------------------------------------------------------------------------------------------------------
	CASE 2) Input YUV and output RGB:

	combinedMatrix = INOUT_PRIMARIES*(outputrangeScale*inputrangeScale*INOUT_TRANSFER_YUV_TO_RGB)
						*CON_SAT_HUE
	-------------------------------------------------------------------------------------------------------

	-------------------------------------------------------------------------------------------------------
	CASE 3) Input RGB and output YUV
	
	combinedMatrix = CON_SAT_HUE*(outputrangeScale*inputrangeScale*INOUT_TRANSFER_RGB_TO_YUV)
						*INOUT_PRIMARIES
	-------------------------------------------------------------------------------------------------------

	-------------------------------------------------------------------------------------------------------
	CASE 4) Input RGB and output RGB:

	combinedMatrix = OUT_PRIMARY_BT709_TO_USER*(outputrangeScale*OUT_TRANSFER_YUV_TO_RGB)*
						CON_SAT_HUE*(inputrangeScale*IN_TRANSFER_RGB_TO_YUV)
							*IN_PRIMARY_USER_TO_BT709

	An arbitrary intermediate YUV format is used to perform the H/S/C operation 
	(colour space=BT709, range=0-255, primary=BT709).

	So	OUT_TRANSFER_YUV_TO_RGB is the same as INOUT_TRANSFER[BT709 to RGB]
	And IN_TRANSFER_RGB_TO_YUV is the same as INOUT_TRANSFER[RGB to BT709]
	
	In addition, the primary conversion is split into two separate matrices. This is because it was 
	found that when doing an HSC adjustment, combining the primaries conversion in to one matrix at 
	either end of the equation gives slightly different results, the correct way is to convert to 
	the  primaries used for the intermediate YUV format (BT709).

	OPTIMISATION FOR CASE 4)
		If no HSBC adjustment is required the conversion to the intermediate YUV format is not 
		required, the two primaries matrices can be combined into one, and the equation simplifies to:
		
		combinedMatrix = outputrangeScale*inputrangeScale*INOUT_PRIMARIES
	-------------------------------------------------------------------------------------------------------

*************************************************************************************************************/

static	img_bool		CSC_bAPIInitialised = IMG_FALSE;

#define	CSC_PRINT_DEBUG_INFO		0

#define CSC_MULTIPLY_MATRICES(pIn_a,pIn_b,pResult)		FIXEDPT_MatrixMultiply(3,3,3,3,3,3,(img_int32*)&((pIn_a)->ai32Coefficients),(img_int32*)&((pIn_b)->ai32Coefficients),(img_int32*)&((pResult)->ai32Coefficients),CSC_FRACTIONAL_BITS)

img_result	CSC_GenerateHSCMatrix (	CSC_psHSBCSettings	psHSBCSettings,
									CSC_ps3x3Matrix		psHSCMatrix );

#if CSC_PRINT_DEBUG_INFO
	img_void CSC_PRINT_3x3_MATRIX (	CSC_ps3x3Matrix		psMatrix )
	{
		img_uint32 ui32Row;
		img_uint32 ui32Column;

		IMG_ASSERT ( psMatrix != IMG_NULL );

		for ( ui32Row=0; ui32Row<3; ui32Row++ )
		{
			printf ( "(" );
			for ( ui32Column=0; ui32Column<3; ui32Column++ )
			{
				printf ( "%f", (((img_float) psMatrix->ai32Coefficients [ui32Row][ui32Column]) / (1 << CSC_FRACTIONAL_BITS)) );

				if ( ui32Column != 2 )
				{
					printf ( ",   " );
				}
				else
				{
					printf ( ")\n" );
				}
			}
		}
	}
#endif

img_result	CSC_GenerateMatrix	(	CSC_eColourSpace		eInputColourSpace,
									CSC_eColourSpace		eOutputColourSpace,
									CSC_eRange				eInputRange,
									CSC_eRange				eOutputRange,
									CSC_eColourPrimary		eInputPrimary,
									CSC_eColourPrimary		eOutputPrimary,
									CSC_psHSBCSettings		psHSBCSettings,
									CSC_psConfiguration		psResultantConfigurationData	)
{
	CSC_s3x3Matrix	sCombinedRangeScaleMatrix;
	CSC_s3x3Matrix	sInputRangeScaleMatrix;
	CSC_s3x3Matrix	sOutputRangeScaleMatrix;
	CSC_s3x3Matrix	sScaledMatrix;
	CSC_s3x3Matrix	sHSCMatrix;
	img_int32		i32InvertedScaleFactor;
	img_int32		i32FirstColumnValue_In;
	img_int32		i32SecondAndThirdColumnValue_In;
	img_int32		i32FirstColumnValue_Out;
	img_int32		i32SecondAndThirdColumnValue_Out;
	img_int32		i32InputYOffset;
	img_int32		i32InputUVOffset;
	img_int32		i32OutputYOffset;
	img_int32		i32OutputUVOffset;

	CSC_sColourSpaceConversionMatrix *		psThisColourSpaceConversion;
	CSC_sColourPrimaryConversionMatrix *	psThisColourPrimaryConversion;
	CSC_s3x3Matrix *						psCombinedRangeScaleMatrix;
	CSC_s3x3Matrix *						psInputRangeScaleMatrix;
	CSC_s3x3Matrix *						psOutputRangeScaleMatrix;
	CSC_s3x3Matrix *						psScaledMatrix;

	/* We need to keep two result matrices, as the matrix multiply function will not allow us to use a given	*/
	/* matrix as both an input and an output.																		*/
	CSC_s3x3Matrix	sResultMatrices [2];

	CSC_ps3x3Matrix	psLatestResult		= IMG_NULL;
	img_uint8		ui8FreeResultMatrix = 0;	/* Index to currently unused member of 'sResultMatrices' */	

	IMG_ASSERT ( psResultantConfigurationData != IMG_NULL );
	IMG_ASSERT ( eInputColourSpace < CSC_NO_OF_COLOURSPACES );
	IMG_ASSERT ( eOutputColourSpace < CSC_NO_OF_COLOURSPACES );

	if ( CSC_bAPIInitialised == IMG_FALSE )
	{
		/* Safety check static data */
		csc_StaticDataSafetyCheck ();

		CSC_bAPIInitialised = IMG_TRUE;
	}

	/* Calculate scaling matrices */

	/* For both input and output ranges, if we are dealing with RGB */
	/* then all columns of the scalar matrix are set to identical	*/
	/* values. If we are dealing with YUV, then the left hand		*/
	/* column is the Y scaling value, while the middle and right	*/
	/* hand columns are the UV scaling values.						*/

	/* Input range scaling */
	if ( asColourspaceInfo [eInputColourSpace].bIsYUV == IMG_TRUE )
	{
		i32FirstColumnValue_In = asCSC_RangeInfo [eInputRange].i32YScale;
		i32SecondAndThirdColumnValue_In = asCSC_RangeInfo [eInputRange].i32UVScale;
	}
	else
	{
		i32FirstColumnValue_In = asCSC_RangeInfo [eInputRange].i32RGBScale;
		i32SecondAndThirdColumnValue_In = i32FirstColumnValue_In;
	}

	if (( i32FirstColumnValue_In == CSC_FP(1.0) ) &&
		( i32SecondAndThirdColumnValue_In == CSC_FP(1.0) ))
	{
		psInputRangeScaleMatrix = IMG_NULL;
	}
	else
	{
		/* Complete input scaling matrix */
		sInputRangeScaleMatrix.ai32Coefficients[0][0]=i32FirstColumnValue_In;
		sInputRangeScaleMatrix.ai32Coefficients[1][0]=i32FirstColumnValue_In;
		sInputRangeScaleMatrix.ai32Coefficients[2][0]=i32FirstColumnValue_In;
		sInputRangeScaleMatrix.ai32Coefficients[0][1]=i32SecondAndThirdColumnValue_In;
		sInputRangeScaleMatrix.ai32Coefficients[1][1]=i32SecondAndThirdColumnValue_In;
		sInputRangeScaleMatrix.ai32Coefficients[2][1]=i32SecondAndThirdColumnValue_In;
		sInputRangeScaleMatrix.ai32Coefficients[0][2]=i32SecondAndThirdColumnValue_In;
		sInputRangeScaleMatrix.ai32Coefficients[1][2]=i32SecondAndThirdColumnValue_In;
		sInputRangeScaleMatrix.ai32Coefficients[2][2]=i32SecondAndThirdColumnValue_In;

		psInputRangeScaleMatrix = &sInputRangeScaleMatrix;
	}

	/* Output range scaling - output scale values are inverted (e.g.:	*/
	/* if a given range requires that RGB values be scaled by a factor	*/
	/* of 255/219 on the inputs, then the scaling range for the same	*/
	/* range selected as an output will be 219/255).					*/
	if ( asColourspaceInfo [eOutputColourSpace].bIsYUV == IMG_TRUE )
	{
		if ( asCSC_RangeInfo [eOutputRange].i32YScale == CSC_FP(1.0) )
		{
			i32InvertedScaleFactor = asCSC_RangeInfo [eOutputRange].i32YScale;
		}
		else
		{
			i32InvertedScaleFactor = FIXEDPT_64BitDivide_Signed( (1<<CSC_FRACTIONAL_BITS),
																	asCSC_RangeInfo [eOutputRange].i32YScale,
																	(32-CSC_FRACTIONAL_BITS) );
		}

		i32FirstColumnValue_Out = i32InvertedScaleFactor;

		if ( asCSC_RangeInfo [eOutputRange].i32UVScale == CSC_FP(1.0) )
		{
			i32InvertedScaleFactor = asCSC_RangeInfo [eOutputRange].i32UVScale;
		}
		else
		{
			i32InvertedScaleFactor = FIXEDPT_64BitDivide_Signed( (1<<CSC_FRACTIONAL_BITS),
																	asCSC_RangeInfo [eOutputRange].i32UVScale,
																	(32-CSC_FRACTIONAL_BITS) );
		}

		i32SecondAndThirdColumnValue_Out = i32InvertedScaleFactor;
	}
	else
	{
		if ( asCSC_RangeInfo [eOutputRange].i32RGBScale == CSC_FP(1.0) )
		{
			i32InvertedScaleFactor = asCSC_RangeInfo [eOutputRange].i32RGBScale;
		}
		else
		{
			i32InvertedScaleFactor = FIXEDPT_64BitDivide_Signed( (1<<CSC_FRACTIONAL_BITS),
																	asCSC_RangeInfo [eOutputRange].i32RGBScale,
																	(32-CSC_FRACTIONAL_BITS) );
		}

		i32FirstColumnValue_Out = i32InvertedScaleFactor;
		i32SecondAndThirdColumnValue_Out = i32FirstColumnValue_Out;
	}

	if (( i32FirstColumnValue_Out == CSC_FP(1.0) ) &&
		( i32SecondAndThirdColumnValue_Out == CSC_FP(1.0) ))
	{
		psOutputRangeScaleMatrix = IMG_NULL;
	}
	else
	{
		/* Complete output scaling matrix */
		sOutputRangeScaleMatrix.ai32Coefficients[0][0]=i32FirstColumnValue_Out;
		sOutputRangeScaleMatrix.ai32Coefficients[1][0]=i32FirstColumnValue_Out;
		sOutputRangeScaleMatrix.ai32Coefficients[2][0]=i32FirstColumnValue_Out;
		sOutputRangeScaleMatrix.ai32Coefficients[0][1]=i32SecondAndThirdColumnValue_Out;
		sOutputRangeScaleMatrix.ai32Coefficients[1][1]=i32SecondAndThirdColumnValue_Out;
		sOutputRangeScaleMatrix.ai32Coefficients[2][1]=i32SecondAndThirdColumnValue_Out;
		sOutputRangeScaleMatrix.ai32Coefficients[0][2]=i32SecondAndThirdColumnValue_Out;
		sOutputRangeScaleMatrix.ai32Coefficients[1][2]=i32SecondAndThirdColumnValue_Out;
		sOutputRangeScaleMatrix.ai32Coefficients[2][2]=i32SecondAndThirdColumnValue_Out;

		psOutputRangeScaleMatrix = &sOutputRangeScaleMatrix;
	}

	if (( psInputRangeScaleMatrix == IMG_NULL ) && ( psOutputRangeScaleMatrix == IMG_NULL ))
	{
		psCombinedRangeScaleMatrix = IMG_NULL;
	}
	else if ( psInputRangeScaleMatrix == IMG_NULL )
	{
		/* No input, just use output */
		psCombinedRangeScaleMatrix = psOutputRangeScaleMatrix;
	}
	else if ( psOutputRangeScaleMatrix == IMG_NULL )
	{
		/* No output, just use input */
		psCombinedRangeScaleMatrix = psInputRangeScaleMatrix;
	}
	else if (( eInputRange != eOutputRange )
			||
			 ( asColourspaceInfo [eInputColourSpace].bIsYUV != asColourspaceInfo [eOutputColourSpace].bIsYUV ))	/* Scale factors for YUV and RGB can be different */
	{
		/* Now combine the input and output scaling values */
		sCombinedRangeScaleMatrix.ai32Coefficients [0][0] = 
					FIXEDPT_64BitMultiply_Signed (	i32FirstColumnValue_In,
													i32FirstColumnValue_Out,
													CSC_FRACTIONAL_BITS	);

		sCombinedRangeScaleMatrix.ai32Coefficients [0][1] = 
					FIXEDPT_64BitMultiply_Signed (	i32SecondAndThirdColumnValue_In,
													i32SecondAndThirdColumnValue_Out,
													CSC_FRACTIONAL_BITS	);

		/* Duplicate values */
		sCombinedRangeScaleMatrix.ai32Coefficients [1][0] = sCombinedRangeScaleMatrix.ai32Coefficients [0][0];
		sCombinedRangeScaleMatrix.ai32Coefficients [2][0] = sCombinedRangeScaleMatrix.ai32Coefficients [0][0];
		sCombinedRangeScaleMatrix.ai32Coefficients [1][1] = sCombinedRangeScaleMatrix.ai32Coefficients [0][1];
		sCombinedRangeScaleMatrix.ai32Coefficients [2][1] = sCombinedRangeScaleMatrix.ai32Coefficients [0][1];
		sCombinedRangeScaleMatrix.ai32Coefficients [0][2] = sCombinedRangeScaleMatrix.ai32Coefficients [0][1];
		sCombinedRangeScaleMatrix.ai32Coefficients [1][2] = sCombinedRangeScaleMatrix.ai32Coefficients [0][1];
		sCombinedRangeScaleMatrix.ai32Coefficients [2][2] = sCombinedRangeScaleMatrix.ai32Coefficients [0][1];

		psCombinedRangeScaleMatrix = &sCombinedRangeScaleMatrix;
	}
	else
	{
		/* No combined range scaling required */
		psCombinedRangeScaleMatrix = IMG_NULL;
	}

	/* If HSBC information has been provided, then generate an HSBC matrix */
	if ( psHSBCSettings != IMG_NULL )
	{
		CSC_GenerateHSCMatrix (psHSBCSettings, &sHSCMatrix);

		#if CSC_PRINT_DEBUG_INFO
			printf ( "\nCSC - HSBC settings will be applied\n" );
		#endif
	}

	/* Check this primary->primary conversion is supported */
	psThisColourPrimaryConversion = &(asCSCColourPrimaryConversionMatrices[eInputPrimary][eOutputPrimary]);
	IMG_ASSERT ( psThisColourPrimaryConversion->bIsSupported == IMG_TRUE );

	if ( asColourspaceInfo [eInputColourSpace].bIsYUV == IMG_TRUE )
	{
		if ( asColourspaceInfo [eOutputColourSpace].bIsYUV == IMG_TRUE )
		{
			/****************************/
			/* CASE 1					*/
			/* Input YUV, Output YUV	*/
			/****************************/
			if ( psThisColourPrimaryConversion->bIsIdentityMatrix == IMG_TRUE )
			{
				#if CSC_PRINT_DEBUG_INFO
					printf ( "\nCSC - Optimised form of case 1, Input YUV/Output YUV\n" );
				#endif

				/* Case 1 optimisation */
				/* Can simplify equation, as we don't need to convert to RGB to do primary calculations */
				psThisColourSpaceConversion = &(asCSC_ColourSpaceConversionMatrices[eInputColourSpace][eOutputColourSpace]);
				IMG_ASSERT ( psThisColourSpaceConversion->bIsSupported == IMG_TRUE );
				if ( psThisColourSpaceConversion->bIsIdentityMatrix == IMG_FALSE )
				{
					psLatestResult = &(psThisColourSpaceConversion->sMatrix);
				}

				/* Apply scaling */
				if ( psCombinedRangeScaleMatrix != IMG_NULL )
				{
					if ( psLatestResult == IMG_NULL )
					{
						/* We need a matrix to scale - in the absence of anything else, use the identity matrix */
						psLatestResult = &sCSC_IdentityMatrix;
					}

					FIXEDPT_ScalarMatrixMultiply (	3, 3,
													(img_int32 *) &(psCombinedRangeScaleMatrix->ai32Coefficients),
													(img_int32 *) &(psLatestResult->ai32Coefficients),
													(img_int32 *) (&(sResultMatrices[ui8FreeResultMatrix].ai32Coefficients)),
													CSC_FRACTIONAL_BITS );
					psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
					ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
				}

				/* Perform con/sat/hue calculations */
				if ( psHSBCSettings != IMG_NULL )
				{
					if ( psLatestResult != IMG_NULL )
					{
						CSC_MULTIPLY_MATRICES((&sHSCMatrix),psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));
						psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
						ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
					}
					else
					{
						psLatestResult = &sHSCMatrix;
					}
				}
			}
			else
			{
				/* Non optimised form of case 1 */
				#if CSC_PRINT_DEBUG_INFO
					printf ( "\nCSC - Case 1, Input YUV/Output YUV\n" );
				#endif

				/* Perform con/sat/hue calculations */
				if ( psHSBCSettings != IMG_NULL )
				{
					psLatestResult = &sHSCMatrix;
				}

				/* We must convert to RGB prior to performing primary calculations */
				psThisColourSpaceConversion = &(asCSC_ColourSpaceConversionMatrices[eInputColourSpace][CSC_COLOURSPACE_RGB]);
				IMG_ASSERT ( psThisColourSpaceConversion->bIsSupported == IMG_TRUE );
				if (( psThisColourSpaceConversion->bIsIdentityMatrix == IMG_FALSE ) ||
					( psInputRangeScaleMatrix != IMG_NULL ))
				{
					if ( psInputRangeScaleMatrix != IMG_NULL )
					{
						/* Produce scaled version of IN_TRANSFER_YUV_TO_RGB matrix */
						FIXEDPT_ScalarMatrixMultiply (	3, 3,
														(img_int32 *) &(psThisColourSpaceConversion->sMatrix.ai32Coefficients),
														(img_int32 *) &(psInputRangeScaleMatrix->ai32Coefficients),
														(img_int32 *) &(sScaledMatrix.ai32Coefficients),
														CSC_FRACTIONAL_BITS );
						psScaledMatrix = &sScaledMatrix;
					}
					else
					{
						psScaledMatrix = &(psThisColourSpaceConversion->sMatrix);
					}

					/* Now merge latest result with scaled matrix */
					if ( psLatestResult != IMG_NULL )
					{
						CSC_MULTIPLY_MATRICES(psScaledMatrix,psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));					
						psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
						ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
					}
					else
					{
						psLatestResult = psScaledMatrix;
					}
				}

				/* Now multiply by primaries (we have already covered the identity matrix case, above) */
				if ( psLatestResult != IMG_NULL )
				{
					CSC_MULTIPLY_MATRICES((&(psThisColourPrimaryConversion->sMatrix)),psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));
					psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
					ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
				}
				else
				{
					psLatestResult = (&(psThisColourPrimaryConversion->sMatrix));
				}
 
				/* Should definitely have an intermediate matrix by now */
				IMG_ASSERT ( psLatestResult != IMG_NULL );

				/* Now convert back to YUV */
				psThisColourSpaceConversion = &(asCSC_ColourSpaceConversionMatrices[CSC_COLOURSPACE_RGB][eOutputColourSpace]);
				IMG_ASSERT ( psThisColourSpaceConversion->bIsSupported == IMG_TRUE );
				if (( psThisColourSpaceConversion->bIsIdentityMatrix == IMG_FALSE ) ||
					( psOutputRangeScaleMatrix != IMG_NULL ))
				{
					if ( psOutputRangeScaleMatrix != IMG_NULL )
					{
						/* Produce scaled version of OUT_TRANSFER_RGB_TO_YUV matrix */
						FIXEDPT_ScalarMatrixMultiply (	3, 3,
														(img_int32 *) &(psThisColourSpaceConversion->sMatrix.ai32Coefficients),
														(img_int32 *) &(psOutputRangeScaleMatrix->ai32Coefficients),
														(img_int32 *) &(sScaledMatrix.ai32Coefficients),
														CSC_FRACTIONAL_BITS );
						psScaledMatrix = &sScaledMatrix;
					}
					else
					{
						psScaledMatrix = &(psThisColourSpaceConversion->sMatrix);
					}

					/* Now merge latest result with scaled matrix */
					CSC_MULTIPLY_MATRICES(psScaledMatrix,psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));					
					psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
					ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
				}
			}
		}
		else
		{
			/****************************/
			/* CASE 2					*/
			/* Input YUV, Output RGB	*/
			/****************************/
			#if CSC_PRINT_DEBUG_INFO
				printf ( "\nCSC - Case 2, Input YUV/Output RGB\n" );
			#endif

			/* Perform con/sat/hue calculations */
			if ( psHSBCSettings != IMG_NULL )
			{
				psLatestResult = &sHSCMatrix;
			}

			/* Perform colour space conversion */
			psThisColourSpaceConversion = &(asCSC_ColourSpaceConversionMatrices[eInputColourSpace][eOutputColourSpace]);
			IMG_ASSERT ( psThisColourSpaceConversion->bIsSupported == IMG_TRUE );
			if (( psThisColourSpaceConversion->bIsIdentityMatrix == IMG_FALSE ) ||
				( psCombinedRangeScaleMatrix != IMG_NULL ))
			{
				if ( psCombinedRangeScaleMatrix != IMG_NULL )
				{
					/* Produce scaled version of IN_TRANSFER_YUV_TO_RGB matrix */
					FIXEDPT_ScalarMatrixMultiply (	3, 3,
													(img_int32 *) &(psThisColourSpaceConversion->sMatrix.ai32Coefficients),
													(img_int32 *) &(psCombinedRangeScaleMatrix->ai32Coefficients),
													(img_int32 *) &(sScaledMatrix.ai32Coefficients),
													CSC_FRACTIONAL_BITS );
					psScaledMatrix = &sScaledMatrix;
				}
				else
				{
					psScaledMatrix = &(psThisColourSpaceConversion->sMatrix);
				}

				/* Now merge latest result with scaled matrix */
				if ( psLatestResult != IMG_NULL )
				{
					CSC_MULTIPLY_MATRICES(psScaledMatrix,psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));					
					psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
					ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
				}
				else
				{
					psLatestResult = psScaledMatrix;
				}
			}

			/* Now perform colour primary conversion */
			if ( psThisColourPrimaryConversion->bIsIdentityMatrix == IMG_FALSE )
			{
				if ( psLatestResult != IMG_NULL )
				{
					CSC_MULTIPLY_MATRICES((&(psThisColourPrimaryConversion->sMatrix)),psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));
					psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
					ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
				}
				else
				{
					psLatestResult = (&(psThisColourPrimaryConversion->sMatrix));
				}
			}
		}
	}
	else
	{
		if ( asColourspaceInfo [eOutputColourSpace].bIsYUV == IMG_TRUE )
		{
			/****************************/
			/* CASE 3					*/
			/* Input RGB, Output YUV	*/
			/****************************/
			#if CSC_PRINT_DEBUG_INFO
				printf ( "\nCSC - Case 3, Input RGB/Output YUV\n" );
			#endif

			/* Perform colour primary conversion */
			if ( psThisColourPrimaryConversion->bIsIdentityMatrix == IMG_FALSE )
			{
				psLatestResult = (&(psThisColourPrimaryConversion->sMatrix));
			}

			/* Now perform colour space conversion */
			psThisColourSpaceConversion = &(asCSC_ColourSpaceConversionMatrices[eInputColourSpace][eOutputColourSpace]);
			IMG_ASSERT ( psThisColourSpaceConversion->bIsSupported == IMG_TRUE );
			if (( psThisColourSpaceConversion->bIsIdentityMatrix == IMG_FALSE ) ||
				( psCombinedRangeScaleMatrix != IMG_NULL ))
			{
				if ( psCombinedRangeScaleMatrix != IMG_NULL )
				{
					/* Produce scaled version of IN_TRANSFER_YUV_TO_RGB matrix */
					FIXEDPT_ScalarMatrixMultiply (	3, 3,
													(img_int32 *) &(psThisColourSpaceConversion->sMatrix.ai32Coefficients),
													(img_int32 *) &(psCombinedRangeScaleMatrix->ai32Coefficients),
													(img_int32 *) &(sScaledMatrix.ai32Coefficients),
													CSC_FRACTIONAL_BITS );
					psScaledMatrix = &sScaledMatrix;
				}
				else
				{
					psScaledMatrix = &(psThisColourSpaceConversion->sMatrix);
				}

				/* Now merge latest result with scaled matrix */
				if ( psLatestResult != IMG_NULL )
				{
					CSC_MULTIPLY_MATRICES(psScaledMatrix,psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));					
					psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
					ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
				}
				else
				{
					psLatestResult = psScaledMatrix;
				}
			}

			/* Perform con/sat/hue calculations */
			if ( psHSBCSettings != IMG_NULL )
			{
				if ( psLatestResult != IMG_NULL )
				{
					CSC_MULTIPLY_MATRICES((&sHSCMatrix),psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));
					psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
					ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
				}
				else
				{
					psLatestResult = &sHSCMatrix;
				}
			} 
		}
		else
		{
			/****************************/
			/* CASE 4					*/
			/* Input RGB, Output RGB	*/
			/****************************/

			/* If no HSBC modification, then the equation can be simplified */
			if ( psHSBCSettings == IMG_NULL ) 
			{
				/* Optimised case 4 */
				/* As no HSBC information has been provided, the equation can be simplified */
				#if CSC_PRINT_DEBUG_INFO
					printf ( "\nCSC - Optimised form of case 4, Input RGB/Output RGB\n" );
				#endif

				/* Perform colour primary conversion */
				if ( psThisColourPrimaryConversion->bIsIdentityMatrix == IMG_FALSE )
				{
					psLatestResult = (&(psThisColourPrimaryConversion->sMatrix));
				}

				/* Scale result */
				if ( psCombinedRangeScaleMatrix != IMG_NULL )
				{
					/* If there is no result to work with (to scale) then just use the identity matrix */
					if ( psLatestResult == IMG_NULL )
					{
						psLatestResult = &sCSC_IdentityMatrix;
					}

					FIXEDPT_ScalarMatrixMultiply (	3, 3,
													(img_int32 *) (&(psLatestResult->ai32Coefficients)),
													(img_int32 *) &(psCombinedRangeScaleMatrix->ai32Coefficients),
													(img_int32 *) &(sScaledMatrix.ai32Coefficients),
													CSC_FRACTIONAL_BITS );

					psLatestResult = &sScaledMatrix;
				}
			}
			else
			{
				/* Non optimised form of case 4 */
				#if CSC_PRINT_DEBUG_INFO
					printf ( "\nCSC - Case 4, Input RGB/Output RGB\n" );
				#endif

				/* Perform colour primary conversion - when HSBC modification is also being performed,	*/
				/* this must be performed in two steps, so that the HSBC modification is performed in	*/
				/* a fixed format (BT709 in the case of this algorithm).								*/
				psThisColourPrimaryConversion = &(asCSCColourPrimaryConversionMatrices[eInputPrimary][CSC_COLOUR_PRIMARY_BT709]);
				IMG_ASSERT ( psThisColourPrimaryConversion->bIsSupported == IMG_TRUE );

				if ( psThisColourPrimaryConversion->bIsIdentityMatrix == IMG_FALSE )
				{
					psLatestResult = &(psThisColourPrimaryConversion->sMatrix);
				}

				/* We must convert to YUV prior to performing con/sat/hue calculations */
				psThisColourSpaceConversion = &(asCSC_ColourSpaceConversionMatrices[eInputColourSpace][CSC_COLOURSPACE_YCC_BT709]);
				IMG_ASSERT ( psThisColourSpaceConversion->bIsSupported == IMG_TRUE );
				if (( psThisColourSpaceConversion->bIsIdentityMatrix == IMG_FALSE ) ||
					( psInputRangeScaleMatrix != IMG_NULL ))
				{
					if ( psInputRangeScaleMatrix != IMG_NULL )
					{
						/* Produce scaled version of IN_TRANSFER_YUV_TO_RGB matrix */
						FIXEDPT_ScalarMatrixMultiply (	3, 3,
														(img_int32 *) &(psThisColourSpaceConversion->sMatrix.ai32Coefficients),
														(img_int32 *) &(psInputRangeScaleMatrix->ai32Coefficients),
														(img_int32 *) &(sScaledMatrix.ai32Coefficients),
														CSC_FRACTIONAL_BITS );
						psScaledMatrix = &sScaledMatrix;
					}
					else
					{
						psScaledMatrix = &(psThisColourSpaceConversion->sMatrix);
					}

					/* Now merge latest result with scaled matrix */
					if ( psLatestResult != IMG_NULL )
					{
						CSC_MULTIPLY_MATRICES(psScaledMatrix,psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));					
						psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
						ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
					}
					else
					{
						psLatestResult = psScaledMatrix;
					}
				}

				/* Perform con/sat/hue calculations */
				IMG_ASSERT ( psHSBCSettings != IMG_NULL ); /* There is an optimised case for dealing with no HSBC input - see above */

				if ( psLatestResult != IMG_NULL )
				{
					CSC_MULTIPLY_MATRICES((&sHSCMatrix),psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));
					psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
					ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
				}
				else
				{
					psLatestResult = &sHSCMatrix;
				}

				/* Should definitely have an intermediate matrix by now */
				IMG_ASSERT ( psLatestResult != IMG_NULL );

				/* Now convert back to RGB */
				psThisColourSpaceConversion = &(asCSC_ColourSpaceConversionMatrices[CSC_COLOURSPACE_YCC_BT709][eOutputColourSpace]);
				IMG_ASSERT ( psThisColourSpaceConversion->bIsSupported == IMG_TRUE );
				if (( psThisColourSpaceConversion->bIsIdentityMatrix == IMG_FALSE ) ||
					( psOutputRangeScaleMatrix != IMG_NULL ))
				{
					if ( psOutputRangeScaleMatrix != IMG_NULL )
					{
						/* Produce scaled version of OUT_TRANSFER_RGB_TO_YUV matrix */
						FIXEDPT_ScalarMatrixMultiply (	3, 3,
														(img_int32 *) &(psThisColourSpaceConversion->sMatrix.ai32Coefficients),
														(img_int32 *) &(psOutputRangeScaleMatrix->ai32Coefficients),
														(img_int32 *) &(sScaledMatrix.ai32Coefficients),
														CSC_FRACTIONAL_BITS );
						psScaledMatrix = &sScaledMatrix;
					}
					else
					{
						psScaledMatrix = &(psThisColourSpaceConversion->sMatrix);
					}

					/* Now merge latest result with scaled matrix */
					CSC_MULTIPLY_MATRICES(psScaledMatrix,psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));					
					psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
					ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
				}

				/* Now convert to the requested output primary */
				psThisColourPrimaryConversion = &(asCSCColourPrimaryConversionMatrices[CSC_COLOUR_PRIMARY_BT709][eOutputPrimary]);
				IMG_ASSERT ( psThisColourPrimaryConversion->bIsSupported == IMG_TRUE );

				if ( psThisColourPrimaryConversion->bIsIdentityMatrix == IMG_FALSE )
				{
					CSC_MULTIPLY_MATRICES((&(psThisColourPrimaryConversion->sMatrix)),psLatestResult,(&(sResultMatrices[ui8FreeResultMatrix])));
					psLatestResult = &(sResultMatrices[ui8FreeResultMatrix]);
					ui8FreeResultMatrix = (1 - ui8FreeResultMatrix); /* Swap 'free result matrix' index */
				}
			}
		}
	}

	/* Entire calculation resulted in no result - use identity matrix */
	if ( psLatestResult == IMG_NULL )
	{
		#if CSC_PRINT_DEBUG_INFO
			printf ( "\nCSC final output defaulting to identity matrix\n" );
		#endif

		psLatestResult = &sCSC_IdentityMatrix;
	}

	#if CSC_PRINT_DEBUG_INFO
		printf ( "\nFinal CSC matrix :\n" );
		CSC_PRINT_3x3_MATRIX(psLatestResult);
	#endif

	/* Now copy the final results into the user supplied matrix */
	IMG_MEMCPY ( &(psResultantConfigurationData->sCoefficients),
				 psLatestResult,
				 sizeof(CSC_s3x3Matrix) );

	/* Finally, calculate input and output offsets */
	if ( asColourspaceInfo [eInputColourSpace].bIsYUV == IMG_TRUE )
	{
		i32InputYOffset = -1 * (img_int32) asCSC_RangeInfo [eInputRange].ui32YOffset;
		i32InputUVOffset = -1 * (img_int32) asCSC_RangeInfo [eInputRange].ui32UVOffset;
	}
	else
	{
		i32InputYOffset = -1 * (img_int32) asCSC_RangeInfo [eInputRange].ui32RGBOffset;
		i32InputUVOffset = i32InputYOffset;
	}

	if ( asColourspaceInfo [eOutputColourSpace].bIsYUV == IMG_TRUE )
	{
		i32OutputYOffset = (img_int32) asCSC_RangeInfo [eOutputRange].ui32YOffset;
		i32OutputUVOffset = (img_int32) asCSC_RangeInfo [eOutputRange].ui32UVOffset;
	}
	else
	{
		i32OutputYOffset = (img_int32) asCSC_RangeInfo [eOutputRange].ui32RGBOffset;
		i32OutputUVOffset = i32OutputYOffset;
	}

	/* Output offsets are specified with a fractional part */
	i32OutputYOffset = FIXEDPT_CONVERT_FRACTIONAL_BITS_NO_ROUNDING(i32OutputYOffset,0,CSC_OUTPUT_OFFSET_FRACTIONAL_BITS);
	i32OutputUVOffset = FIXEDPT_CONVERT_FRACTIONAL_BITS_NO_ROUNDING(i32OutputUVOffset,0,CSC_OUTPUT_OFFSET_FRACTIONAL_BITS);

	if ( psHSBCSettings != IMG_NULL )
	{
		i32OutputYOffset += psHSBCSettings->i32Brightness;

		/* For YUV outputs, brightness is only applied to the Y output */
		if ( asColourspaceInfo [eOutputColourSpace].bIsYUV == IMG_FALSE )
		{
			i32OutputUVOffset += psHSBCSettings->i32Brightness;
		}
	}

	/* Copy offsets into user provided structure */
	psResultantConfigurationData->ai32InputOffsets[0] = i32InputYOffset;
	psResultantConfigurationData->ai32InputOffsets[1] = i32InputUVOffset;
	psResultantConfigurationData->ai32InputOffsets[2] = i32InputUVOffset;

	psResultantConfigurationData->ai32OutputOffsets[0] = i32OutputYOffset;
	psResultantConfigurationData->ai32OutputOffsets[1] = i32OutputUVOffset;
	psResultantConfigurationData->ai32OutputOffsets[2] = i32OutputUVOffset;

	#if CSC_PRINT_DEBUG_INFO
		printf ( "\nInput offsets:\n" );
		printf ( "  [0] %03i\n", psResultantConfigurationData->ai32InputOffsets[0] );
		printf ( "  [1] %03i\n", psResultantConfigurationData->ai32InputOffsets[1] );
		printf ( "  [2] %03i\n", psResultantConfigurationData->ai32InputOffsets[2] );

		printf ( "\nOutput offsets:\n" );
		printf ( "  [0] %f\n", (((img_float) psResultantConfigurationData->ai32OutputOffsets[0]) / (1 << CSC_OUTPUT_OFFSET_FRACTIONAL_BITS)));
		printf ( "  [1] %f\n", (((img_float) psResultantConfigurationData->ai32OutputOffsets[1]) / (1 << CSC_OUTPUT_OFFSET_FRACTIONAL_BITS)));
		printf ( "  [2] %f\n", (((img_float) psResultantConfigurationData->ai32OutputOffsets[2]) / (1 << CSC_OUTPUT_OFFSET_FRACTIONAL_BITS)));
	#endif

	return IMG_SUCCESS;
};

img_result	CSC_GenerateHSCMatrix ( CSC_psHSBCSettings	psHSBCSettings,
									CSC_ps3x3Matrix		psHSCMatrix )
{
	img_int32	i32CosHue;
	img_int32	i32SinHue;
	img_int32	i32HueInRadians;
	img_int32	i32ContrastTimesSaturation;

	IMG_ASSERT ( psHSBCSettings != IMG_NULL );
	IMG_ASSERT ( psHSCMatrix != IMG_NULL );

	/* Check all supplied values are in range */
	IMG_ASSERT ( psHSBCSettings->i32Hue <= CSC_MAX_HUE );
	IMG_ASSERT ( psHSBCSettings->i32Hue >= CSC_MIN_HUE );
	IMG_ASSERT ( psHSBCSettings->i32Saturation <= CSC_MAX_SATURATION );
	IMG_ASSERT ( psHSBCSettings->i32Saturation >= CSC_MIN_SATURATION );
	IMG_ASSERT ( psHSBCSettings->i32Brightness <= CSC_MAX_BRIGHTNESS );
	IMG_ASSERT ( psHSBCSettings->i32Brightness >= CSC_MIN_BRIGHTNESS );
	IMG_ASSERT ( psHSBCSettings->i32Contrast <= CSC_MAX_CONTRAST );
	IMG_ASSERT ( psHSBCSettings->i32Contrast >= CSC_MIN_CONTRAST );

	/* conSatHue matrix is constructed as follows :						*/
	/*																	*/
	/* [ con,		0,							0					]	*/
	/* [ 0,			(con*sat*cos(hue)),			(con*sat*sin(hue))	]	*/
	/* [ 0,			-(con*sat*cos(hue)),		(con*sat*cos(hue))	]	*/

	/* Generate some useful values */
	i32HueInRadians = psHSBCSettings->i32Hue;
	if ( i32HueInRadians < 0 )
	{
		i32HueInRadians = i32HueInRadians * -1;
	}
	i32HueInRadians = FIXEDPT_64BitMultiply_Signed ( (img_int32) FIXEDPT_ONE_DEGREE_IN_RADIANS,		/* x.FIXEDPT_TRIG_INPUT_FRACTIONAL_BITS */
													 i32HueInRadians,			/* x.CSC_HSC_FRACTIONAL_BITS			*/
													 CSC_HSC_FRACTIONAL_BITS ); /* Result is in x.FIXEDPT_TRIG_INPUT_FRACTIONAL_BITS format */

	i32CosHue = FIXEDPT_Cosine( (img_uint32) i32HueInRadians );	/* Cosine is symmetrical about 0 degrees, hence cos(theta)=cos(-theta) */
	i32CosHue = FIXEDPT_CONVERT_FRACTIONAL_BITS(i32CosHue,FIXEDPT_TRIG_OUTPUT_FRACTIONAL_BITS,CSC_FRACTIONAL_BITS);

	i32SinHue = FIXEDPT_Sine( (img_uint32) i32HueInRadians ); /* Sine is not symmetrical, however sin(theta)=-1 * sin(-theta) */
	if ( psHSBCSettings->i32Hue < 0 )
	{
		i32SinHue = -1 * i32SinHue;
	}
	i32SinHue = FIXEDPT_CONVERT_FRACTIONAL_BITS(i32SinHue,FIXEDPT_TRIG_OUTPUT_FRACTIONAL_BITS,CSC_FRACTIONAL_BITS);

	i32ContrastTimesSaturation = FIXEDPT_64BitMultiply_Signed ( psHSBCSettings->i32Contrast,
																psHSBCSettings->i32Saturation,
																((2*CSC_HSC_FRACTIONAL_BITS)-CSC_FRACTIONAL_BITS) );	/* Result is in x.CSC_FRACTIONAL_BITS format */

	/* Now complete matrix */
	/* Row 0 */
	psHSCMatrix->ai32Coefficients [0][0] = FIXEDPT_CONVERT_FRACTIONAL_BITS(psHSBCSettings->i32Contrast,CSC_HSC_FRACTIONAL_BITS,CSC_FRACTIONAL_BITS);
	psHSCMatrix->ai32Coefficients [0][1] = 0;
	psHSCMatrix->ai32Coefficients [0][2] = 0;

	/* Row 1 */
	psHSCMatrix->ai32Coefficients [1][0] = 0;
	psHSCMatrix->ai32Coefficients [1][1] = FIXEDPT_64BitMultiply_Signed ( i32ContrastTimesSaturation,
																		  i32CosHue,
																		  CSC_FRACTIONAL_BITS );		/* Result is in x.CSC_FRACTIONAL_BITS format */
	psHSCMatrix->ai32Coefficients [1][2] = FIXEDPT_64BitMultiply_Signed ( i32ContrastTimesSaturation,
																		  i32SinHue,
																		  CSC_FRACTIONAL_BITS );		/* Result is in x.CSC_FRACTIONAL_BITS format */

	/* Row 2 */
	psHSCMatrix->ai32Coefficients [2][0] = 0;
	psHSCMatrix->ai32Coefficients [2][1] = -1 * psHSCMatrix->ai32Coefficients [1][2];
	psHSCMatrix->ai32Coefficients [2][2] = psHSCMatrix->ai32Coefficients [1][1];

	#if CSC_PRINT_DEBUG_INFO
		printf ( "\nHSC matrix :\n" );
		CSC_PRINT_3x3_MATRIX(psHSCMatrix);
	#endif

	return IMG_SUCCESS;
}