summaryrefslogtreecommitdiff
path: root/src/im_convertcolor.cpp
blob: e68730f896e98ceeb9d6d7f62c0246778e3743d6 (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
974
975
976
977
978
979
980
981
982
983
984
985
/** \file
 * \brief Image Conversion
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_convertcolor.cpp,v 1.3 2009/08/18 05:13:18 scuri Exp $
 */

#include "im.h"
#include "im_util.h"
#include "im_complex.h"
#include "im_image.h"
#include "im_convert.h"
#include "im_color.h"
#include "im_counter.h"

#include <stdlib.h>
#include <assert.h>
#include <memory.h>

void imConvertMapToRGB(unsigned char* data, int count, int depth, int packed, long* palette, int palette_count)
{
  int c, i, delta;
  unsigned char r[256], g[256], b[256];
  unsigned char *r_data, *g_data, *b_data;

  unsigned char* src_data = data + count-1;
  if (packed)
  {
    r_data = data + depth*(count-1);
    g_data = r_data + 1;
    b_data = r_data + 2;
    delta = depth;
  }
  else
  {
    r_data = data +   count - 1;
    g_data = data + 2*count - 1;
    b_data = data + 3*count - 1;
    delta = 1;
  }

  for (c = 0; c < palette_count; c++)
    imColorDecode(&r[c], &g[c], &b[c], palette[c]);

  for (i = 0; i < count; i++)
  {
    int index = *src_data;
    *r_data = r[index];
    *g_data = g[index];
    *b_data = b[index];

    r_data -= delta;
    g_data -= delta;
    b_data -= delta;
    src_data--;
  }
}

static void iConvertSetTranspMap(imbyte *src_map, imbyte *dst_alpha, int count, imbyte *transp_map, int transp_count)
{
  for(int i = 0; i < count; i++)
  {
    if (*src_map < transp_count)
      *dst_alpha = transp_map[*src_map];
    else
      *dst_alpha = 255;  /* opaque */

    src_map++;
    dst_alpha++;
  }
}

static void iConvertSetTranspIndex(imbyte *src_map, imbyte *dst_alpha, int count, imbyte index)
{
  for(int i = 0; i < count; i++)
  {
    if (*src_map == index)
      *dst_alpha = 0;    /* full transparent */
    else
      *dst_alpha = 255;  /* opaque */

    src_map++;
    dst_alpha++;
  }
}

static void iConvertSetTranspColor(imbyte **dst_data, int count, imbyte r, imbyte g, imbyte b)
{
  imbyte *pr = dst_data[0];
  imbyte *pg = dst_data[1];
  imbyte *pb = dst_data[2];
  imbyte *pa = dst_data[3];

  for(int i = 0; i < count; i++)
  {
    if (*pr == r &&
        *pg == g &&
        *pb == b)
      *pa = 0;    /* transparent */
    else
      *pa = 255;  /* opaque */
    
    pr++;
    pg++;
    pb++;
    pa++;
  }
}

// convert bin2gray and gray2bin
inline void iConvertBinary(imbyte* map, int count, imbyte value)
{
  imbyte thres = (value == 255)? 1: 128;

  // if gray2bin, check for invalid gray that already is binary
  if (value != 255)
  {
    imbyte vmax = 0, *pmap = map;
    for (int i = 0; i < count; i++)
    {
      if (*pmap > vmax)
        vmax = *pmap;

      pmap++;
    }

    if (vmax == 1)
      thres = 1;
    else
      thres = vmax / 2;
  }

  for (int i = 0; i < count; i++)
  {
    if (*map >= thres)
      *map = value;
    else
      *map = 0;

    map++;
  }
}

static void iConvertMap2Gray(const imbyte* src_map, imbyte* dst_map, int count, const long* palette, const int palette_count)
{
  imbyte r, g, b;
  imbyte remap[256];

  for (int c = 0; c < palette_count; c++)
  {
    imColorDecode(&r, &g, &b, palette[c]);
    remap[c] = imColorRGB2Luma(r, g, b);
  }

  for (int i = 0; i < count; i++)
  {
    *dst_map++ = remap[*src_map++];
  }
}

static void iConvertMapToRGB(const imbyte* src_map, imbyte* red, imbyte* green, imbyte* blue, int count, const long* palette, const int palette_count)
{
  imbyte r[256], g[256], b[256];
  for (int c = 0; c < palette_count; c++)
    imColorDecode(&r[c], &g[c], &b[c], palette[c]);

  for (int i = 0; i < count; i++)
  {
    int index = *src_map++;
    *red++ = r[index];
    *green++ = g[index];
    *blue++ = b[index];
  }
}

template <class T> 
int iDoConvert2Gray(int count, int data_type, 
                    const T** src_data, int src_color_space, T** dst_data, int counter)
{
  int i;
  T max;

  const T* src_map0 = src_data[0];
  const T* src_map1 = src_data[1];
  const T* src_map2 = src_data[2];
  const T* src_map3 = (src_color_space == IM_CMYK)? src_data[3]: 0;
  T* dst_map = dst_data[0];

  imCounterTotal(counter, count, "Converting To Gray...");

  switch(src_color_space)
  {
  case IM_XYZ: 
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // scale to 0-1
      float c1 = imColorReconstruct(*src_map1++, max);  // use only Y component

      // do gamma correction then scale back to 0-max
      *dst_map++ = imColorQuantize(imColorTransfer2Nonlinear(c1), max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_CMYK: 
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      T r, g, b;
      // result is still 0-max
      imColorCMYK2RGB(*src_map0++, *src_map1++, *src_map2++, *src_map3++, r, g, b, max);
      *dst_map++ = imColorRGB2Luma(r, g, b);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_RGB:
    for (i = 0; i < count; i++)
    {
      *dst_map++ = imColorRGB2Luma(*src_map0++, *src_map1++, *src_map2++);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_LUV:
  case IM_LAB:
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float
      
      float c0 = imColorReconstruct(*src_map0++, max); // scale to 0-1
      c0 = imColorLightness2Luminance(c0);             // do the convertion

      // do gamma correction then scale back to 0-max
      *dst_map++ = imColorQuantize(imColorTransfer2Nonlinear(c0), max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  default:
    return IM_ERR_DATA;
  }

  return IM_ERR_NONE;
}

template <class T> 
int iDoConvert2RGB(int count, int data_type, 
                   const T** src_data, int src_color_space, T** dst_data, int counter)
{
  int i;
  T max, zero;

  const T* src_map0 = src_data[0];
  const T* src_map1 = src_data[1];
  const T* src_map2 = src_data[2];
  const T* src_map3 = (src_color_space == IM_CMYK)? src_data[3]: 0;
  T* dst_map0 = dst_data[0];
  T* dst_map1 = dst_data[1];
  T* dst_map2 = dst_data[2];

  imCounterTotal(counter, count, "Converting To RGB...");

  switch(src_color_space)
  {
  case IM_XYZ: 
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float

      // scale to 0-1
      float c0 = imColorReconstruct(*src_map0++, max);
      float c1 = imColorReconstruct(*src_map1++, max);
      float c2 = imColorReconstruct(*src_map2++, max);

      // result is still 0-1
      imColorXYZ2RGB(c0, c1, c2, 
                     c0, c1, c2, 1.0f);

      // do gamma correction then scale back to 0-max
      *dst_map0++ = imColorQuantize(imColorTransfer2Nonlinear(c0), max);
      *dst_map1++ = imColorQuantize(imColorTransfer2Nonlinear(c1), max);
      *dst_map2++ = imColorQuantize(imColorTransfer2Nonlinear(c2), max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_YCBCR: 
    max = (T)imColorMax(data_type);
    zero = (T)imColorZero(data_type);
    for (i = 0; i < count; i++)
    {
      imColorYCbCr2RGB(*src_map0++, *src_map1++, *src_map2++, 
                       *dst_map0++, *dst_map1++, *dst_map2++, zero, max);
    }
    break;
  case IM_CMYK: 
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // result is still 0-max
      imColorCMYK2RGB(*src_map0++, *src_map1++, *src_map2++, *src_map3++, 
                      *dst_map0++, *dst_map1++, *dst_map2++, max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_LUV:
  case IM_LAB:
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float

      // scale to 0-1 and -0.5/+0.5
      float c0 = imColorReconstruct(*src_map0++, max);
      float c1 = imColorReconstruct(*src_map1++, max) - 0.5f;
      float c2 = imColorReconstruct(*src_map2++, max) - 0.5f;

      if (src_color_space == IM_LUV)
        imColorLuv2XYZ(c0, c1, c2,  // conversion in-place
                       c0, c1, c2);
      else
        imColorLab2XYZ(c0, c1, c2,  // conversion in-place
                       c0, c1, c2);

      imColorXYZ2RGB(c0, c1, c2,    // conversion in-place
                     c0, c1, c2, 1.0f);

      // do gamma correction then scale back to 0-max
      *dst_map0++ = imColorQuantize(imColorTransfer2Nonlinear(c0), max);
      *dst_map1++ = imColorQuantize(imColorTransfer2Nonlinear(c1), max);
      *dst_map2++ = imColorQuantize(imColorTransfer2Nonlinear(c2), max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  default:
    return IM_ERR_DATA;
  }

  return IM_ERR_NONE;
}

template <class T> 
int iDoConvert2YCbCr(int count, int data_type, 
                     const T** src_data, int src_color_space, T** dst_data, int counter)
{
  int i;
  T zero;

  const T* src_map0 = src_data[0];
  const T* src_map1 = src_data[1];
  const T* src_map2 = src_data[2];
  T* dst_map0 = dst_data[0];
  T* dst_map1 = dst_data[1];
  T* dst_map2 = dst_data[2];

  imCounterTotal(counter, count, "Converting To YCbCr...");

  switch(src_color_space)
  {
  case IM_RGB: 
    zero = (T)imColorZero(data_type);
    for (i = 0; i < count; i++)
    {
      imColorRGB2YCbCr(*src_map0++, *src_map1++, *src_map2++, 
                       *dst_map0++, *dst_map1++, *dst_map2++, zero);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  default:
    return IM_ERR_DATA;
  }

  return IM_ERR_NONE;
}

template <class T> 
int iDoConvert2XYZ(int count, int data_type, 
                   const T** src_data, int src_color_space, T** dst_data, int counter)
{
  int i;
  T max;

  const T* src_map0 = src_data[0];
  const T* src_map1 = (src_color_space == IM_GRAY)? 0: src_data[1];
  const T* src_map2 = (src_color_space == IM_GRAY)? 0: src_data[2];
  T* dst_map0 = dst_data[0];
  T* dst_map1 = dst_data[1];
  T* dst_map2 = dst_data[2];

  imCounterTotal(counter, count, "Converting To XYZ...");

  switch(src_color_space)
  {
  case IM_GRAY: 
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // scale to 0-1
      float c0 = imColorReconstruct(*src_map0++, max);

      // do gamma correction
      c0 = imColorTransfer2Linear(c0);

      // then scale back to 0-max
      *dst_map0++ = imColorQuantize(c0*0.9505f, max);    // Compensate D65 white point
      *dst_map1++ = imColorQuantize(c0, max);
      *dst_map2++ = imColorQuantize(c0*1.0890f, max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_RGB: 
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float

      // scale to 0-1
      float c0 = imColorReconstruct(*src_map0++, max);
      float c1 = imColorReconstruct(*src_map1++, max);
      float c2 = imColorReconstruct(*src_map2++, max);

      // do gamma correction
      c0 = imColorTransfer2Linear(c0);
      c1 = imColorTransfer2Linear(c1);
      c2 = imColorTransfer2Linear(c2);

      // result is still 0-1
      imColorRGB2XYZ(c0, c1, c2, 
                     c0, c1, c2);

      // then scale back to 0-max
      *dst_map0++ = imColorQuantize(c0, max);
      *dst_map1++ = imColorQuantize(c1, max);
      *dst_map2++ = imColorQuantize(c2, max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_LUV:
  case IM_LAB:
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float
      // scale to 0-1 and -0.5/+0.5
      float c0 = imColorReconstruct(*src_map0++, max);
      float c1 = imColorReconstruct(*src_map1++, max) - 0.5f;
      float c2 = imColorReconstruct(*src_map2++, max) - 0.5f;

      if (src_color_space == IM_LUV)
        imColorLuv2XYZ(c0, c1, c2,  // convertion in-place
                       c0, c1, c2);
      else
        imColorLab2XYZ(c0, c1, c2,  // convertion in-place
                       c0, c1, c2);

      // scale back to 0-max
      *dst_map0++ = imColorQuantize(c0, max);
      *dst_map1++ = imColorQuantize(c1, max);
      *dst_map2++ = imColorQuantize(c2, max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  default:
    return IM_ERR_DATA;
  }

  return IM_ERR_NONE;
}

template <class T> 
int iDoConvert2Lab(int count, int data_type, 
                   const T** src_data, int src_color_space, T** dst_data, int counter)
{
  int i;
  T max;

  const T* src_map0 = src_data[0];
  const T* src_map1 = (src_color_space == IM_GRAY)? 0: src_data[1];
  const T* src_map2 = (src_color_space == IM_GRAY)? 0: src_data[2];
  T* dst_map0 = dst_data[0];
  T* dst_map1 = dst_data[1];
  T* dst_map2 = dst_data[2];

  imCounterTotal(counter, count, "Converting To Lab...");

  switch(src_color_space)
  {
  case IM_GRAY: 
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // scale to 0-1
      float c0 = imColorReconstruct(*src_map0++, max);

      // do gamma correction
      c0 = imColorTransfer2Linear(c0);

      // do conversion
      c0 = imColorLuminance2Lightness(c0);

      // then scale back to 0-max
      *dst_map0++ = imColorQuantize(c0, max);  // update only the L component

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_RGB: 
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float

      // scale to 0-1
      float c0 = imColorReconstruct(*src_map0++, max);
      float c1 = imColorReconstruct(*src_map1++, max);
      float c2 = imColorReconstruct(*src_map2++, max);

      // do gamma correction
      c0 = imColorTransfer2Linear(c0);
      c1 = imColorTransfer2Linear(c1);
      c2 = imColorTransfer2Linear(c2);

      imColorRGB2XYZ(c0, c1, c2,  // convertion in-place
                     c0, c1, c2);

      imColorXYZ2Lab(c0, c1, c2,  // convertion in-place
                     c0, c1, c2);

      // then scale back to 0-max
      *dst_map0++ = imColorQuantize(c0, max);
      *dst_map1++ = imColorQuantize(c1 + 0.5f, max);
      *dst_map2++ = imColorQuantize(c2 + 0.5f, max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_XYZ:
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float
      // scale to 0-1 and -0.5/+0.5
      float c0 = imColorReconstruct(*src_map0++, max);
      float c1 = imColorReconstruct(*src_map1++, max);
      float c2 = imColorReconstruct(*src_map2++, max);

      imColorXYZ2Lab(c0, c1, c2,  // convertion in-place
                     c0, c1, c2);

      // scale back to 0-max
      *dst_map0++ = imColorQuantize(c0, max);
      *dst_map1++ = imColorQuantize(c1 + 0.5f, max);
      *dst_map2++ = imColorQuantize(c2 + 0.5f, max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_LUV:
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float
      // scale to 0-1 and -0.5/+0.5
      float c0 = imColorReconstruct(*src_map0++, max);
      float c1 = imColorReconstruct(*src_map1++, max) - 0.5f;
      float c2 = imColorReconstruct(*src_map2++, max) - 0.5f;

      imColorLuv2XYZ(c0, c1, c2,  // convertion in-place
                     c0, c1, c2);
      imColorXYZ2Lab(c0, c1, c2,  // convertion in-place
                     c0, c1, c2);

      // scale back to 0-max
      *dst_map0++ = imColorQuantize(c0, max);
      *dst_map1++ = imColorQuantize(c1 + 0.5f, max);
      *dst_map2++ = imColorQuantize(c2 + 0.5f, max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  default:
    return IM_ERR_DATA;
  }

  return IM_ERR_NONE;
}

template <class T> 
int iDoConvert2Luv(int count, int data_type, 
                   const T** src_data, int src_color_space, T** dst_data, int counter)
{
  int i;
  T max;

  const T* src_map0 = src_data[0];
  const T* src_map1 = (src_color_space == IM_GRAY)? 0: src_data[1];
  const T* src_map2 = (src_color_space == IM_GRAY)? 0: src_data[2];
  T* dst_map0 = dst_data[0];
  T* dst_map1 = dst_data[1];
  T* dst_map2 = dst_data[2];

  imCounterTotal(counter, count, "Converting To Luv...");

  switch(src_color_space)
  {
  case IM_GRAY: 
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // scale to 0-1
      float c0 = imColorReconstruct(*src_map0++, max);

      // do gamma correction
      c0 = imColorTransfer2Linear(c0);

      // do conversion
      c0 = imColorLuminance2Lightness(c0);

      // then scale back to 0-max
      *dst_map0++ = imColorQuantize(c0, max);  // update only the L component

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_RGB: 
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float

      // scale to 0-1
      float c0 = imColorReconstruct(*src_map0++, max);
      float c1 = imColorReconstruct(*src_map1++, max);
      float c2 = imColorReconstruct(*src_map2++, max);

      // do gamma correction
      c0 = imColorTransfer2Linear(c0);
      c1 = imColorTransfer2Linear(c1);
      c2 = imColorTransfer2Linear(c2);

      imColorRGB2XYZ(c0, c1, c2,  // convertion in-place
                     c0, c1, c2);

      imColorXYZ2Luv(c0, c1, c2,  // convertion in-place
                     c0, c1, c2);

      // then scale back to 0-max
      *dst_map0++ = imColorQuantize(c0, max);
      *dst_map1++ = imColorQuantize(c1 + 0.5f, max);
      *dst_map2++ = imColorQuantize(c2 + 0.5f, max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_XYZ:
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float
      // scale to 0-1 and -0.5/+0.5
      float c0 = imColorReconstruct(*src_map0++, max);
      float c1 = imColorReconstruct(*src_map1++, max);
      float c2 = imColorReconstruct(*src_map2++, max);

      imColorXYZ2Luv(c0, c1, c2,  // convertion in-place
                     c0, c1, c2);

      // scale back to 0-max
      *dst_map0++ = imColorQuantize(c0, max);
      *dst_map1++ = imColorQuantize(c1 + 0.5f, max);
      *dst_map2++ = imColorQuantize(c2 + 0.5f, max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  case IM_LAB:
    max = (T)imColorMax(data_type);
    for (i = 0; i < count; i++)
    {
      // to increase precision do intermediate conversions in float
      // scale to 0-1 and -0.5/+0.5
      float c0 = imColorReconstruct(*src_map0++, max);
      float c1 = imColorReconstruct(*src_map1++, max) - 0.5f;
      float c2 = imColorReconstruct(*src_map2++, max) - 0.5f;

      imColorLab2XYZ(c0, c1, c2,  // convertion in-place
                     c0, c1, c2);
      imColorXYZ2Luv(c0, c1, c2,  // convertion in-place
                     c0, c1, c2);

      // scale back to 0-max
      *dst_map0++ = imColorQuantize(c0, max);
      *dst_map1++ = imColorQuantize(c1 + 0.5f, max);
      *dst_map2++ = imColorQuantize(c2 + 0.5f, max);

      if (!imCounterInc(counter))
        return IM_ERR_COUNTER;
    }
    break;
  default:
    return IM_ERR_DATA;
  }

  return IM_ERR_NONE;
}

template <class T> 
int iDoConvertColorSpace(int count, int data_type, 
                                 const T** src_data, int src_color_space, 
                                       T** dst_data, int dst_color_space)
{
  int ret = IM_ERR_DATA, 
      convert2rgb = 0;

  if ((dst_color_space == IM_XYZ ||
       dst_color_space == IM_LAB ||
       dst_color_space == IM_LUV) && 
      (src_color_space == IM_CMYK ||
       src_color_space == IM_YCBCR))
  {
    convert2rgb = 1;
  }    

  if (dst_color_space == IM_YCBCR && src_color_space != IM_RGB)
    convert2rgb = 1;

  int counter = imCounterBegin("Convert Color Space");

  if (convert2rgb)
  {
    ret = iDoConvert2RGB(count, data_type, src_data, src_color_space, dst_data, counter);     

    if (ret != IM_ERR_NONE) 
    {
      imCounterEnd(counter);
      return ret;
    }

    src_data = (const T**)dst_data;
    src_color_space = IM_RGB;
  }

  switch(dst_color_space)
  {
  case IM_GRAY: 
    ret = iDoConvert2Gray(count, data_type, src_data, src_color_space, dst_data, counter);
    break;
  case IM_RGB: 
    ret = iDoConvert2RGB(count, data_type, src_data, src_color_space, dst_data, counter);
    break;
  case IM_YCBCR: 
    ret = iDoConvert2YCbCr(count, data_type, src_data, src_color_space, dst_data, counter); 
    break;
  case IM_XYZ: 
    ret = iDoConvert2XYZ(count, data_type, src_data, src_color_space, dst_data, counter);
    break;
  case IM_LAB: 
    ret = iDoConvert2Lab(count, data_type, src_data, src_color_space, dst_data, counter);
    break;
  case IM_LUV: 
    ret = iDoConvert2Luv(count, data_type, src_data, src_color_space, dst_data, counter);
    break;
  default:
    ret = IM_ERR_DATA;
    break;
  }

  imCounterEnd(counter);

  return ret;
}

static int iConvertColorSpace(const imImage* src_image, imImage* dst_image)
{
  switch(src_image->data_type)
  {
  case IM_BYTE:
    return iDoConvertColorSpace(src_image->count, src_image->data_type,
                         (const imbyte**)src_image->data, src_image->color_space, 
                               (imbyte**)dst_image->data, dst_image->color_space);
  case IM_USHORT:
    return iDoConvertColorSpace(src_image->count, src_image->data_type, 
                         (const imushort**)src_image->data, src_image->color_space, 
                               (imushort**)dst_image->data, dst_image->color_space);
  case IM_INT:
    return iDoConvertColorSpace(src_image->count, src_image->data_type,
                         (const int**)src_image->data, src_image->color_space, 
                               (int**)dst_image->data, dst_image->color_space);
  case IM_FLOAT:
    return iDoConvertColorSpace(src_image->count, src_image->data_type, 
                         (const float**)src_image->data, src_image->color_space, 
                               (float**)dst_image->data, dst_image->color_space);
  case IM_CFLOAT:
    /* treat complex as two real values */
    return iDoConvertColorSpace(2*src_image->count, src_image->data_type,
                         (const float**)src_image->data, src_image->color_space, 
                               (float**)dst_image->data, dst_image->color_space);
  }

  return IM_ERR_DATA;
}

int imConvertColorSpace(const imImage* src_image, imImage* dst_image)
{
  int ret = IM_ERR_NONE;
  assert(src_image);
  assert(dst_image);

  if (!imImageMatchDataType(src_image, dst_image))
    return IM_ERR_DATA;

  if (src_image->color_space != dst_image->color_space)
  {
    switch(dst_image->color_space)
    {
    case IM_RGB:
      switch(src_image->color_space)
      {
      case IM_BINARY:
          memcpy(dst_image->data[0], src_image->data[0], dst_image->plane_size);
          iConvertBinary((imbyte*)dst_image->data[0], dst_image->count, 255);
          memcpy(dst_image->data[1], dst_image->data[0], dst_image->plane_size);
          memcpy(dst_image->data[2], dst_image->data[0], dst_image->plane_size);
        ret = IM_ERR_NONE;
        break;
      case IM_MAP:
        iConvertMapToRGB((imbyte*)src_image->data[0], (imbyte*)dst_image->data[0], (imbyte*)dst_image->data[1], (imbyte*)dst_image->data[2], dst_image->count, src_image->palette, src_image->palette_count);
        ret = IM_ERR_NONE;
        break;
      case IM_GRAY:
          memcpy(dst_image->data[0], src_image->data[0], dst_image->plane_size);
          memcpy(dst_image->data[1], src_image->data[0], dst_image->plane_size);
          memcpy(dst_image->data[2], src_image->data[0], dst_image->plane_size);
        ret = IM_ERR_NONE;
        break;
      default: 
        ret = iConvertColorSpace(src_image, dst_image);
        break;
      }
      break;
    case IM_GRAY:  
      switch(src_image->color_space)
      {
      case IM_BINARY:
        memcpy(dst_image->data[0], src_image->data[0], dst_image->size);
        iConvertBinary((imbyte*)dst_image->data[0], dst_image->count, 255);
        ret = IM_ERR_NONE;
        break;
      case IM_MAP:
        iConvertMap2Gray((imbyte*)src_image->data[0], (imbyte*)dst_image->data[0], dst_image->count, src_image->palette, src_image->palette_count);
        ret = IM_ERR_NONE;
        break;
      case IM_YCBCR: 
        memcpy(dst_image->data[0], src_image->data[0], dst_image->plane_size);
        ret = IM_ERR_NONE;
        break;
      default:
        ret = iConvertColorSpace(src_image, dst_image);
        break;
      }
      break;
    case IM_MAP:   
      switch(src_image->color_space)
      {
      case IM_BINARY: // no break, same procedure as gray
      case IM_GRAY:
        memcpy(dst_image->data[0], src_image->data[0], dst_image->size);
        dst_image->palette_count = src_image->palette_count;
        memcpy(dst_image->palette, src_image->palette, dst_image->palette_count*sizeof(long));
        ret = IM_ERR_NONE;
        break;
      case IM_RGB:
        dst_image->palette_count = 256;
        ret = imConvertRGB2Map(src_image->width, src_image->height, 
                               (imbyte*)src_image->data[0], (imbyte*)src_image->data[1], (imbyte*)src_image->data[2], 
                               (imbyte*)dst_image->data[0], dst_image->palette, &dst_image->palette_count);
        break;
      default:
        ret = IM_ERR_DATA;
        break;
      }
      break;
    case IM_BINARY:
      switch(src_image->color_space)
      {
      case IM_GRAY:
        memcpy(dst_image->data[0], src_image->data[0], dst_image->size);
        iConvertBinary((imbyte*)dst_image->data[0], dst_image->count, 1);
        ret = IM_ERR_NONE;
        break;
      case IM_MAP:           // convert to gray, then convert to binary
        iConvertMap2Gray((imbyte*)src_image->data[0], (imbyte*)dst_image->data[0], dst_image->count, src_image->palette, src_image->palette_count);
        iConvertBinary((imbyte*)dst_image->data[0], dst_image->count, 1);
        ret = IM_ERR_NONE;
        break;
      case IM_YCBCR:         // convert to gray, then convert to binary
        memcpy(dst_image->data[0], src_image->data[0], dst_image->plane_size);
        iConvertBinary((imbyte*)dst_image->data[0], dst_image->count, 1);
        ret = IM_ERR_NONE;
        break;
      default:               // convert to gray, then convert to binary
        dst_image->color_space = IM_GRAY;
        ret = iConvertColorSpace(src_image, dst_image);
        dst_image->color_space = IM_BINARY;
        if (ret == IM_ERR_NONE)
          iConvertBinary((imbyte*)dst_image->data[0], dst_image->count, 1);
        ret = IM_ERR_NONE;
        break;
      }
      break;
    case IM_YCBCR: 
      switch(src_image->color_space)
      {
      case IM_GRAY:
        memcpy(dst_image->data[0], src_image->data[0], dst_image->plane_size);
        ret = IM_ERR_NONE;
        break;
      default:
        ret = iConvertColorSpace(src_image, dst_image);
        break;
      }
      break;
    default: 
      ret = iConvertColorSpace(src_image, dst_image);
      break;
    }
  }

  if (src_image->has_alpha && dst_image->has_alpha)
    memcpy(dst_image->data[dst_image->depth], src_image->data[src_image->depth], src_image->plane_size);
  else if (dst_image->color_space == IM_RGB && dst_image->data_type == IM_BYTE && dst_image->has_alpha)
  {
    if (src_image->color_space == IM_RGB)
    {
      imbyte* transp_color = (imbyte*)imImageGetAttribute(src_image, "TransparencyColor", NULL, NULL);
      if (transp_color)
        iConvertSetTranspColor((imbyte**)dst_image->data, dst_image->count, *(transp_color+0), *(transp_color+1), *(transp_color+2));
      else
        memset(dst_image->data[3], 255, dst_image->count);
    }
    else
    {
      int transp_count;
      imbyte* transp_index = (imbyte*)imImageGetAttribute(src_image, "TransparencyIndex", NULL, NULL);
      imbyte* transp_map = (imbyte*)imImageGetAttribute(src_image, "TransparencyMap", NULL, &transp_count);
      if (transp_map)
        iConvertSetTranspMap((imbyte*)src_image->data[0], (imbyte*)dst_image->data[3], dst_image->count, transp_map, transp_count);
      else if (transp_index)
        iConvertSetTranspIndex((imbyte*)src_image->data[0], (imbyte*)dst_image->data[3], dst_image->count, *transp_index);
      else
        memset(dst_image->data[3], 255, dst_image->count);
    }
  }

  return ret;
}