summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/libmng/libmng_cms.c
blob: 081283fe571a2a91c78d6494b42df04adc0047b5 (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
/* ************************************************************************** */
/* *             For conditions of distribution and use,                    * */
/* *                see copyright notice in libmng.h                        * */
/* ************************************************************************** */
/* *                                                                        * */
/* * project   : libmng                                                     * */
/* * file      : libmng_cms.c              copyright (c) 2000 G.Juyn        * */
/* * version   : 1.0.1                                                      * */
/* *                                                                        * */
/* * purpose   : color management routines (implementation)                 * */
/* *                                                                        * */
/* * author    : G.Juyn                                                     * */
/* * web       : http://www.3-t.com                                         * */
/* * email     : mailto:info@3-t.com                                        * */
/* *                                                                        * */
/* * comment   : implementation of the color management routines            * */
/* *                                                                        * */
/* * changes   : 0.5.1 - 05/01/2000 - G.Juyn                                * */
/* *             - B001(105795) - fixed a typo and misconception about      * */
/* *               freeing allocated gamma-table. (reported by Marti Maria) * */
/* *             0.5.1 - 05/08/2000 - G.Juyn                                * */
/* *             - changed strict-ANSI stuff                                * */
/* *             0.5.1 - 05/09/2000 - G.Juyn                                * */
/* *             - filled application-based color-management routines       * */
/* *             0.5.1 - 05/11/2000 - G.Juyn                                * */
/* *             - added creatememprofile                                   * */
/* *             - added callback error-reporting support                   * */
/* *             0.5.1 - 05/12/2000 - G.Juyn                                * */
/* *             - changed trace to macro for callback error-reporting      * */
/* *                                                                        * */
/* *             0.5.2 - 06/10/2000 - G.Juyn                                * */
/* *             - fixed some compilation-warnings (contrib Jason Morris)   * */
/* *                                                                        * */
/* *             0.5.3 - 06/21/2000 - G.Juyn                                * */
/* *             - fixed problem with color-correction for stored images    * */
/* *             0.5.3 - 06/23/2000 - G.Juyn                                * */
/* *             - fixed problem with incorrect gamma-correction            * */
/* *                                                                        * */
/* *             0.9.2 - 08/05/2000 - G.Juyn                                * */
/* *             - changed file-prefixes                                    * */
/* *                                                                        * */
/* *             0.9.3 - 08/31/2000 - G.Juyn                                * */
/* *             - fixed sRGB precedence for gamma_only corection           * */
/* *                                                                        * */
/* *             0.9.4 - 12/16/2000 - G.Juyn                                * */
/* *             - fixed mixup of data- & function-pointers (thanks Dimitri)* */
/* *                                                                        * */
/* *             1.0.1 - 03/31/2001 - G.Juyn                                * */
/* *             - ignore gamma=0 (see png-list for more info)              * */
/* *             1.0.1 - 04/25/2001 - G.Juyn (reported by Gregg Kelly)      * */
/* *             - fixed problem with cms profile being created multiple    * */
/* *               times when both iCCP & cHRM/gAMA are present             * */
/* *             1.0.1 - 04/25/2001 - G.Juyn                                * */
/* *             - moved mng_clear_cms to libmng_cms                        * */
/* *             1.0.1 - 05/02/2001 - G.Juyn                                * */
/* *             - added "default" sRGB generation (Thanks Marti!)          * */
/* *                                                                        * */
/* ************************************************************************** */

#include "libmng.h"
#include "libmng_data.h"
#include "libmng_error.h"
#include "libmng_trace.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "libmng_objects.h"
#include "libmng_cms.h"

#if defined(__BORLANDC__) && defined(MNG_STRICT_ANSI)
#pragma option -A                      /* force ANSI-C */
#endif

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

#ifdef MNG_INCLUDE_DISPLAY_PROCS

/* ************************************************************************** */
/* *                                                                        * */
/* * Little CMS helper routines                                             * */
/* *                                                                        * */
/* ************************************************************************** */

#ifdef MNG_INCLUDE_LCMS

#define MNG_CMS_FLAGS 0

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

void mnglcms_initlibrary ()
{
  cmsErrorAction (LCMS_ERROR_IGNORE);  /* LCMS should ignore errors! */
}

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

mng_cmsprof mnglcms_createfileprofile (mng_pchar zFilename)
{
  return cmsOpenProfileFromFile (zFilename, "r");
}

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

mng_cmsprof mnglcms_creatememprofile (mng_uint32 iProfilesize,
                                      mng_ptr    pProfile)
{
  return cmsOpenProfileFromMem (pProfile, iProfilesize);
}

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

mng_cmsprof mnglcms_createsrgbprofile (void)
{
  cmsCIExyY       D65;
  cmsCIExyYTRIPLE Rec709Primaries = {
                                      {0.6400, 0.3300, 1.0},
                                      {0.3000, 0.6000, 1.0},
                                      {0.1500, 0.0600, 1.0}
                                    };
  LPGAMMATABLE    Gamma24[3];
  mng_cmsprof     hsRGB;

  cmsWhitePointFromTemp(6504, &D65);
  Gamma24[0] = Gamma24[1] = Gamma24[2] = cmsBuildGamma(256, 2.4);
  hsRGB = cmsCreateRGBProfile(&D65, &Rec709Primaries, Gamma24);
  cmsFreeGamma(Gamma24[0]);

  return hsRGB;
}

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

void mnglcms_freeprofile (mng_cmsprof hProf)
{
  cmsCloseProfile (hProf);
  return;
}

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

void mnglcms_freetransform (mng_cmstrans hTrans)
{
/* B001 start */
  cmsDeleteTransform (hTrans);
/* B001 end */
  return;
}

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

mng_retcode mng_clear_cms (mng_datap pData)
{
#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_CLEAR_CMS, MNG_LC_START)
#endif

  if (pData->hTrans)                   /* transformation still active ? */
    mnglcms_freetransform (pData->hTrans);

  pData->hTrans = 0;

  if (pData->hProf1)                   /* file profile still active ? */
    mnglcms_freeprofile (pData->hProf1);

  pData->hProf1 = 0;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_CLEAR_CMS, MNG_LC_END)
#endif

  return MNG_NOERROR;
}

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

#endif /* MNG_INCLUDE_LCMS */

/* ************************************************************************** */
/* *                                                                        * */
/* * Color-management initialization & correction routines                  * */
/* *                                                                        * */
/* ************************************************************************** */

#ifdef MNG_INCLUDE_LCMS

mng_retcode init_full_cms (mng_datap pData)
{
  mng_cmsprof    hProf;
  mng_cmstrans   hTrans;
  mng_imagep     pImage = (mng_imagep)pData->pCurrentobj;
  mng_imagedatap pBuf;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_FULL_CMS, MNG_LC_START)
#endif

  if (!pImage)                         /* no current object? then use object 0 */
    pImage = (mng_imagep)pData->pObjzero;

  pBuf = pImage->pImgbuf;              /* address the buffer */ 

  if ((pBuf->bHasICCP) || (pData->bHasglobalICCP))
  {
    if (!pData->hProf2)                /* output profile not defined ? */
    {                                  /* then assume sRGB !! */
      pData->hProf2 = mnglcms_createsrgbprofile ();

      if (!pData->hProf2)              /* handle error ? */
        MNG_ERRORL (pData, MNG_LCMS_NOHANDLE)
    }

    if (pBuf->bHasICCP)                /* generate a profile handle */
      hProf = cmsOpenProfileFromMem (pBuf->pProfile, pBuf->iProfilesize);
    else
      hProf = cmsOpenProfileFromMem (pData->pGlobalProfile, pData->iGlobalProfilesize);

    pData->hProf1 = hProf;             /* save for future use */

    if (!hProf)                        /* handle error ? */
      MNG_ERRORL (pData, MNG_LCMS_NOHANDLE)

    if (pData->bIsRGBA16)              /* 16-bit intermediates ? */
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_16_SE,
                                   pData->hProf2, TYPE_RGBA_16_SE,
                                   INTENT_PERCEPTUAL, MNG_CMS_FLAGS);
    else
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_8,
                                   pData->hProf2, TYPE_RGBA_8,
                                   INTENT_PERCEPTUAL, MNG_CMS_FLAGS);

    pData->hTrans = hTrans;            /* save for future use */

    if (!hTrans)                       /* handle error ? */
      MNG_ERRORL (pData, MNG_LCMS_NOTRANS)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_full_cms;

    return MNG_NOERROR;                /* and done */
  }
  else
  if ((pBuf->bHasSRGB) || (pData->bHasglobalSRGB))
  {
    mng_uint8 iIntent;

    if (pData->bIssRGB)                /* sRGB system ? */
      return MNG_NOERROR;              /* no conversion retquired */

    if (!pData->hProf3)                /* sRGB profile not defined ? */
    {                                  /* then create it implicitly !! */
      pData->hProf3 = mnglcms_createsrgbprofile ();

      if (!pData->hProf3)              /* handle error ? */
        MNG_ERRORL (pData, MNG_LCMS_NOHANDLE)
    }

    hProf = pData->hProf3;             /* convert from sRGB profile */

    if (pBuf->bHasSRGB)                /* determine rendering intent */
      iIntent = pBuf->iRenderingintent;
    else
      iIntent = pData->iGlobalRendintent;

    if (pData->bIsRGBA16)              /* 16-bit intermediates ? */
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_16_SE,
                                   pData->hProf2, TYPE_RGBA_16_SE,
                                   iIntent, MNG_CMS_FLAGS);
    else
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_8,
                                   pData->hProf2, TYPE_RGBA_8,
                                   iIntent, MNG_CMS_FLAGS);

    pData->hTrans = hTrans;            /* save for future use */

    if (!hTrans)                       /* handle error ? */
      MNG_ERRORL (pData, MNG_LCMS_NOTRANS)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_full_cms;

    return MNG_NOERROR;                /* and done */
  }
  else
  if ( ((pBuf->bHasCHRM) || (pData->bHasglobalCHRM)) &&
       ( ((pBuf->bHasGAMA)        && (pBuf->iGamma        > 0)) ||
         ((pData->bHasglobalGAMA) && (pData->iGlobalGamma > 0))    ))
  {
    mng_CIExyY       sWhitepoint;
    mng_CIExyYTRIPLE sPrimaries;
    mng_gammatabp    pGammatable[3];
    mng_float        dGamma;

    if (!pData->hProf2)                /* output profile not defined ? */
    {                                  /* then assume sRGB !! */
      pData->hProf2 = mnglcms_createsrgbprofile ();

      if (!pData->hProf2)              /* handle error ? */
        MNG_ERRORL (pData, MNG_LCMS_NOHANDLE)
    }

    if (pBuf->bHasCHRM)                /* local cHRM ? */
    {
      sWhitepoint.x      = (mng_float)pBuf->iWhitepointx   / 100000;
      sWhitepoint.y      = (mng_float)pBuf->iWhitepointy   / 100000;
      sPrimaries.Red.x   = (mng_float)pBuf->iPrimaryredx   / 100000;
      sPrimaries.Red.y   = (mng_float)pBuf->iPrimaryredy   / 100000;
      sPrimaries.Green.x = (mng_float)pBuf->iPrimarygreenx / 100000;
      sPrimaries.Green.y = (mng_float)pBuf->iPrimarygreeny / 100000;
      sPrimaries.Blue.x  = (mng_float)pBuf->iPrimarybluex  / 100000;
      sPrimaries.Blue.y  = (mng_float)pBuf->iPrimarybluey  / 100000;
    }
    else
    {
      sWhitepoint.x      = (mng_float)pData->iGlobalWhitepointx   / 100000;
      sWhitepoint.y      = (mng_float)pData->iGlobalWhitepointy   / 100000;
      sPrimaries.Red.x   = (mng_float)pData->iGlobalPrimaryredx   / 100000;
      sPrimaries.Red.y   = (mng_float)pData->iGlobalPrimaryredy   / 100000;
      sPrimaries.Green.x = (mng_float)pData->iGlobalPrimarygreenx / 100000;
      sPrimaries.Green.y = (mng_float)pData->iGlobalPrimarygreeny / 100000;
      sPrimaries.Blue.x  = (mng_float)pData->iGlobalPrimarybluex  / 100000;
      sPrimaries.Blue.y  = (mng_float)pData->iGlobalPrimarybluey  / 100000;
    }

    sWhitepoint.Y      =               /* Y component is always 1.0 */
    sPrimaries.Red.Y   =
    sPrimaries.Green.Y =
    sPrimaries.Blue.Y  = 1.0;

    if (pBuf->bHasGAMA)                /* get the gamma value */
      dGamma = (mng_float)pBuf->iGamma / 100000;
    else
      dGamma = (mng_float)pData->iGlobalGamma / 100000;

/*    dGamma = pData->dViewgamma / (dGamma * pData->dDisplaygamma); ??? */
    dGamma = pData->dViewgamma / dGamma;

    pGammatable [0] =                  /* and build the lookup tables */
    pGammatable [1] =
    pGammatable [2] = cmsBuildGamma (256, dGamma);

/* B001 start */
    if (!pGammatable [0])              /* enough memory ? */
/* B001 end */
      MNG_ERRORL (pData, MNG_LCMS_NOMEM)
                                       /* create the profile */
    hProf = cmsCreateRGBProfile (&sWhitepoint, &sPrimaries, pGammatable);

/* B001 start */
    cmsFreeGamma (pGammatable [0]);    /* free the temporary gamma tables ? */
                                       /* yes! but just the one! */
/* B001 end */

    pData->hProf1 = hProf;             /* save for future use */

    if (!hProf)                        /* handle error ? */
      MNG_ERRORL (pData, MNG_LCMS_NOHANDLE)

    if (pData->bIsRGBA16)              /* 16-bit intermediates ? */
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_16_SE,
                                   pData->hProf2, TYPE_RGBA_16_SE,
                                   INTENT_PERCEPTUAL, MNG_CMS_FLAGS);
    else
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_8,
                                   pData->hProf2, TYPE_RGBA_8,
                                   INTENT_PERCEPTUAL, MNG_CMS_FLAGS);

    pData->hTrans = hTrans;            /* save for future use */

    if (!hTrans)                       /* handle error ? */
      MNG_ERRORL (pData, MNG_LCMS_NOTRANS)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_full_cms;

    return MNG_NOERROR;                /* and done */
  }

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_FULL_CMS, MNG_LC_END)
#endif

  return init_gamma_only (pData);      /* if we get here, we'll only do gamma */
}
#endif /* MNG_INCLUDE_LCMS */

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

#ifdef MNG_INCLUDE_LCMS

mng_retcode init_full_cms_object (mng_datap pData)
{
  mng_cmsprof    hProf;
  mng_cmstrans   hTrans;
  mng_imagedatap pBuf;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_FULL_CMS_OBJ, MNG_LC_START)
#endif
                                       /* address the object-buffer */
  pBuf = ((mng_imagep)pData->pRetrieveobj)->pImgbuf;

  if (pBuf->bHasICCP)
  {
    if (!pData->hProf2)                /* output profile not defined ? */
    {                                  /* then assume sRGB !! */
      pData->hProf2 = mnglcms_createsrgbprofile ();

      if (!pData->hProf2)              /* handle error ? */
        MNG_ERRORL (pData, MNG_LCMS_NOHANDLE)
    }
                                       /* generate a profile handle */
    hProf = cmsOpenProfileFromMem (pBuf->pProfile, pBuf->iProfilesize);

    pData->hProf1 = hProf;             /* save for future use */

    if (!hProf)                        /* handle error ? */
      MNG_ERRORL (pData, MNG_LCMS_NOHANDLE)

    if (pData->bIsRGBA16)              /* 16-bit intermediates ? */
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_16_SE,
                                   pData->hProf2, TYPE_RGBA_16_SE,
                                   INTENT_PERCEPTUAL, MNG_CMS_FLAGS);
    else
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_8,
                                   pData->hProf2, TYPE_RGBA_8,
                                   INTENT_PERCEPTUAL, MNG_CMS_FLAGS);

    pData->hTrans = hTrans;            /* save for future use */

    if (!hTrans)                       /* handle error ? */
      MNG_ERRORL (pData, MNG_LCMS_NOTRANS)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_full_cms;

    return MNG_NOERROR;                /* and done */
  }
  else
  if (pBuf->bHasSRGB)
  {
    if (pData->bIssRGB)                /* sRGB system ? */
      return MNG_NOERROR;              /* no conversion retquired */

    if (!pData->hProf3)                /* sRGB profile not defined ? */
    {                                  /* then create it implicitly !! */
      pData->hProf3 = mnglcms_createsrgbprofile ();

      if (!pData->hProf3)              /* handle error ? */
        MNG_ERRORL (pData, MNG_LCMS_NOHANDLE)
    }

    hProf = pData->hProf3;             /* convert from sRGB profile */

    if (pData->bIsRGBA16)              /* 16-bit intermediates ? */
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_16_SE,
                                   pData->hProf2, TYPE_RGBA_16_SE,
                                   pBuf->iRenderingintent, MNG_CMS_FLAGS);
    else
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_8,
                                   pData->hProf2, TYPE_RGBA_8,
                                   pBuf->iRenderingintent, MNG_CMS_FLAGS);

    pData->hTrans = hTrans;            /* save for future use */

    if (!hTrans)                       /* handle error ? */
      MNG_ERRORL (pData, MNG_LCMS_NOTRANS)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_full_cms;

    return MNG_NOERROR;                /* and done */
  }
  else
  if ((pBuf->bHasCHRM) && (pBuf->bHasGAMA) && (pBuf->iGamma > 0))
  {
    mng_CIExyY       sWhitepoint;
    mng_CIExyYTRIPLE sPrimaries;
    mng_gammatabp    pGammatable[3];
    mng_float        dGamma;

    if (!pData->hProf2)                /* output profile not defined ? */
    {                                  /* then assume sRGB !! */
      pData->hProf2 = mnglcms_createsrgbprofile ();

      if (!pData->hProf2)              /* handle error ? */
        MNG_ERRORL (pData, MNG_LCMS_NOHANDLE)
    }

    sWhitepoint.x      = (mng_float)pBuf->iWhitepointx   / 100000;
    sWhitepoint.y      = (mng_float)pBuf->iWhitepointy   / 100000;
    sPrimaries.Red.x   = (mng_float)pBuf->iPrimaryredx   / 100000;
    sPrimaries.Red.y   = (mng_float)pBuf->iPrimaryredy   / 100000;
    sPrimaries.Green.x = (mng_float)pBuf->iPrimarygreenx / 100000;
    sPrimaries.Green.y = (mng_float)pBuf->iPrimarygreeny / 100000;
    sPrimaries.Blue.x  = (mng_float)pBuf->iPrimarybluex  / 100000;
    sPrimaries.Blue.y  = (mng_float)pBuf->iPrimarybluey  / 100000;

    sWhitepoint.Y      =               /* Y component is always 1.0 */
    sPrimaries.Red.Y   =
    sPrimaries.Green.Y =
    sPrimaries.Blue.Y  = 1.0;

    dGamma = (mng_float)pBuf->iGamma / 100000;

/*    dGamma = pData->dViewgamma / (dGamma * pData->dDisplaygamma); ??? */
    dGamma = pData->dViewgamma / dGamma;

    pGammatable [0] =                  /* and build the lookup tables */
    pGammatable [1] =
    pGammatable [2] = cmsBuildGamma (256, dGamma);

/* B001 start */
    if (!pGammatable [0])              /* enough memory ? */
/* B001 end */
      MNG_ERRORL (pData, MNG_LCMS_NOMEM)

                                       /* create the profile */
    hProf = cmsCreateRGBProfile (&sWhitepoint, &sPrimaries, pGammatable);

/* B001 start */
    cmsFreeGamma (pGammatable [0]);    /* free the temporary gamma tables ? */
                                       /* yes! but just the one! */
/* B001 end */

    pData->hProf1 = hProf;             /* save for future use */

    if (!hProf)                        /* handle error ? */
      MNG_ERRORL (pData, MNG_LCMS_NOHANDLE)

    if (pData->bIsRGBA16)              /* 16-bit intermediates ? */
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_16_SE,
                                   pData->hProf2, TYPE_RGBA_16_SE,
                                   INTENT_PERCEPTUAL, MNG_CMS_FLAGS);
    else
      hTrans = cmsCreateTransform (hProf,         TYPE_RGBA_8,
                                   pData->hProf2, TYPE_RGBA_8,
                                   INTENT_PERCEPTUAL, MNG_CMS_FLAGS);

    pData->hTrans = hTrans;            /* save for future use */

    if (!hTrans)                       /* handle error ? */
      MNG_ERRORL (pData, MNG_LCMS_NOTRANS)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_full_cms;

    return MNG_NOERROR;                /* and done */
  }

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_FULL_CMS_OBJ, MNG_LC_END)
#endif
                                       /* if we get here, we'll only do gamma */
  return init_gamma_only_object (pData);
}
#endif /* MNG_INCLUDE_LCMS */

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

#ifdef MNG_INCLUDE_LCMS
mng_retcode correct_full_cms (mng_datap pData)
{
#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_CORRECT_FULL_CMS, MNG_LC_START)
#endif

  cmsDoTransform (pData->hTrans, pData->pRGBArow, pData->pRGBArow, pData->iRowsamples);

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_CORRECT_FULL_CMS, MNG_LC_END)
#endif

  return MNG_NOERROR;
}
#endif /* MNG_INCLUDE_LCMS */

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

#if defined(MNG_GAMMA_ONLY) || defined(MNG_FULL_CMS)
mng_retcode init_gamma_only (mng_datap pData)
{
  mng_float      dGamma;
  mng_imagep     pImage = (mng_imagep)pData->pCurrentobj;
  mng_imagedatap pBuf;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_GAMMA_ONLY, MNG_LC_START)
#endif

  if (!pImage)                         /* no current object? then use object 0 */
    pImage = (mng_imagep)pData->pObjzero;

  pBuf = pImage->pImgbuf;              /* address the buffer */

  if (pBuf->bHasSRGB)                  /* get the gamma value */
    dGamma = 0.45455;
  else
  if (pBuf->bHasGAMA)
    dGamma = (mng_float)pBuf->iGamma / 100000;
  else
  if (pData->bHasglobalSRGB)
    dGamma = 0.45455;
  else
  if (pData->bHasglobalGAMA)
    dGamma = (mng_float)pData->iGlobalGamma / 100000;
  else
    dGamma = pData->dDfltimggamma;

  if (dGamma > 0)                      /* ignore gamma=0 */
  {
    dGamma = pData->dViewgamma / (dGamma * pData->dDisplaygamma);

    if (dGamma != pData->dLastgamma)   /* lookup table needs to be computed ? */
    {
      mng_int32 iX;

      pData->aGammatab [0] = 0;

      for (iX = 1; iX <= 255; iX++)
        pData->aGammatab [iX] = (mng_uint8)(pow (iX / 255.0, dGamma) * 255 + 0.5);

      pData->dLastgamma = dGamma;      /* keep for next time */
    }
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_gamma_only;
  }

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_GAMMA_ONLY, MNG_LC_END)
#endif

  return MNG_NOERROR;
}
#endif /* MNG_GAMMA_ONLY || MNG_FULL_CMS */

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

#if defined(MNG_GAMMA_ONLY) || defined(MNG_FULL_CMS)
mng_retcode init_gamma_only_object (mng_datap pData)
{
  mng_float      dGamma;
  mng_imagedatap pBuf;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_GAMMA_ONLY_OBJ, MNG_LC_START)
#endif
                                       /* address the object-buffer */
  pBuf = ((mng_imagep)pData->pRetrieveobj)->pImgbuf;

  if (pBuf->bHasSRGB)                  /* get the gamma value */
    dGamma = 0.45455;
  else
  if (pBuf->bHasGAMA)
    dGamma = (mng_float)pBuf->iGamma / 100000;
  else
    dGamma = pData->dDfltimggamma;

  if (dGamma)                          /* lets not divide by zero, shall we... */
    dGamma = pData->dViewgamma / (dGamma * pData->dDisplaygamma);

  if (dGamma != pData->dLastgamma)     /* lookup table needs to be computed ? */
  {
    mng_int32 iX;

    pData->aGammatab [0] = 0;

    for (iX = 1; iX <= 255; iX++)
      pData->aGammatab [iX] = (mng_uint8)(pow (iX / 255.0, dGamma) * 255 + 0.5);

    pData->dLastgamma = dGamma;        /* keep for next time */
  }
                                       /* load color-correction routine */
  pData->fCorrectrow = (mng_fptr)correct_gamma_only;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_GAMMA_ONLY_OBJ, MNG_LC_END)
#endif

  return MNG_NOERROR;
}
#endif /* MNG_GAMMA_ONLY || MNG_FULL_CMS */

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

#if defined(MNG_GAMMA_ONLY) || defined(MNG_FULL_CMS)
mng_retcode correct_gamma_only (mng_datap pData)
{
  mng_uint8p pWork;
  mng_int32  iX;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_CORRECT_GAMMA_ONLY, MNG_LC_START)
#endif

  pWork = pData->pRGBArow;             /* address intermediate row */

  if (pData->bIsRGBA16)                /* 16-bit intermediate row ? */
  {

  
     /* TODO: 16-bit precision gamma processing */
     /* we'll just do the high-order byte for now */

     
                                       /* convert all samples in the row */
     for (iX = 0; iX < pData->iRowsamples; iX++)
     {                                 /* using the precalculated gamma lookup table */
       *pWork     = pData->aGammatab [*pWork];
       *(pWork+2) = pData->aGammatab [*(pWork+2)];
       *(pWork+4) = pData->aGammatab [*(pWork+4)];

       pWork += 8;
     }
  }
  else
  {                                    /* convert all samples in the row */
     for (iX = 0; iX < pData->iRowsamples; iX++)
     {                                 /* using the precalculated gamma lookup table */
       *pWork     = pData->aGammatab [*pWork];
       *(pWork+1) = pData->aGammatab [*(pWork+1)];
       *(pWork+2) = pData->aGammatab [*(pWork+2)];

       pWork += 4;
     }
  }

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_CORRECT_GAMMA_ONLY, MNG_LC_END)
#endif

  return MNG_NOERROR;
}
#endif /* MNG_GAMMA_ONLY || MNG_FULL_CMS */

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

#ifdef MNG_APP_CMS
mng_retcode init_app_cms (mng_datap pData)
{
  mng_imagedatap pBuf = ((mng_imagep)pData->pObjzero)->pImgbuf;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_APP_CMS, MNG_LC_START)
#endif

  if ( (pData->fProcessiccp) &&
       ((pBuf->bHasICCP) || (pData->bHasglobalICCP)) )
  {
    mng_uint32 iProfilesize;
    mng_ptr    pProfile;

    if (pBuf->bHasICCP)                /* get the right profile */
    {
      iProfilesize = pBuf->iProfilesize;
      pProfile     = pBuf->pProfile;
    }
    else
    {
      iProfilesize = pData->iGlobalProfilesize;
      pProfile     = pData->pGlobalProfile;
    }
                                       /* inform the app */
    if (!pData->fProcessiccp ((mng_handle)pData, iProfilesize, pProfile))
      MNG_ERROR (pData, MNG_APPCMSERROR)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_app_cms;
  }

  if ( (pData->fProcesssrgb) &&
       ((pBuf->bHasSRGB) || (pData->bHasglobalSRGB)) )
  {
    mng_uint8 iIntent;

    if (pBuf->bHasSRGB)                /* determine rendering intent */
      iIntent = pBuf->iRenderingintent;
    else
      iIntent = pData->iGlobalRendintent;
                                       /* inform the app */
    if (!pData->fProcesssrgb ((mng_handle)pData, iIntent))
      MNG_ERROR (pData, MNG_APPCMSERROR)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_app_cms;
  }

  if ( (pData->fProcesschroma) &&
       ( ((pBuf->bHasCHRM) || (pData->bHasglobalCHRM)) ) )
  {
    mng_uint32 iWhitepointx,   iWhitepointy;
    mng_uint32 iPrimaryredx,   iPrimaryredy;
    mng_uint32 iPrimarygreenx, iPrimarygreeny;
    mng_uint32 iPrimarybluex,  iPrimarybluey;

    if (pBuf->bHasCHRM)                /* local cHRM ? */
    {
      iWhitepointx   = pBuf->iWhitepointx;
      iWhitepointy   = pBuf->iWhitepointy;
      iPrimaryredx   = pBuf->iPrimaryredx;
      iPrimaryredy   = pBuf->iPrimaryredy;
      iPrimarygreenx = pBuf->iPrimarygreenx;
      iPrimarygreeny = pBuf->iPrimarygreeny;
      iPrimarybluex  = pBuf->iPrimarybluex;
      iPrimarybluey  = pBuf->iPrimarybluey;  
    }
    else
    {
      iWhitepointx   = pData->iGlobalWhitepointx;
      iWhitepointy   = pData->iGlobalWhitepointy;
      iPrimaryredx   = pData->iGlobalPrimaryredx;
      iPrimaryredy   = pData->iGlobalPrimaryredy;
      iPrimarygreenx = pData->iGlobalPrimarygreenx;
      iPrimarygreeny = pData->iGlobalPrimarygreeny;
      iPrimarybluex  = pData->iGlobalPrimarybluex;
      iPrimarybluey  = pData->iGlobalPrimarybluey;
    }
                                       /* inform the app */
    if (!pData->fProcesschroma ((mng_handle)pData, iWhitepointx,   iWhitepointy,
                                                   iPrimaryredx,   iPrimaryredy,
                                                   iPrimarygreenx, iPrimarygreeny,
                                                   iPrimarybluex,  iPrimarybluey))
      MNG_ERROR (pData, MNG_APPCMSERROR)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_app_cms;
  }

  if ( (pData->fProcessgamma) &&
       ((pBuf->bHasGAMA) || (pData->bHasglobalGAMA)) )
  {
    mng_uint32 iGamma;

    if (pBuf->bHasGAMA)                /* get the gamma value */
      iGamma = pBuf->iGamma;
    else
      iGamma = pData->iGlobalGamma;
                                       /* inform the app */
    if (!pData->fProcessgamma ((mng_handle)pData, iGamma))
      MNG_ERROR (pData, MNG_APPCMSERROR)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_app_cms;
  }

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_APP_CMS, MNG_LC_END)
#endif

  return MNG_NOERROR;
}
#endif /* MNG_APP_CMS */

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

#ifdef MNG_APP_CMS
mng_retcode init_app_cms_object (mng_datap pData)
{
  mng_imagedatap pBuf = ((mng_imagep)pData->pCurrentobj)->pImgbuf;

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_APP_CMS_OBJ, MNG_LC_START)
#endif

  if ((pData->fProcessiccp) && (pBuf->bHasICCP))
  {                                    /* inform the app */
    if (!pData->fProcessiccp ((mng_handle)pData, pBuf->iProfilesize, pBuf->pProfile))
      MNG_ERROR (pData, MNG_APPCMSERROR)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_app_cms;
  }

  if ((pData->fProcesssrgb) && (pBuf->bHasSRGB))
  {                                    /* inform the app */
    if (!pData->fProcesssrgb ((mng_handle)pData, pBuf->iRenderingintent))
      MNG_ERROR (pData, MNG_APPCMSERROR)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_app_cms;
  }

  if ((pData->fProcesschroma) && (pBuf->bHasCHRM))
  {                                    /* inform the app */
    if (!pData->fProcesschroma ((mng_handle)pData, pBuf->iWhitepointx,   pBuf->iWhitepointy,
                                                   pBuf->iPrimaryredx,   pBuf->iPrimaryredy,
                                                   pBuf->iPrimarygreenx, pBuf->iPrimarygreeny,
                                                   pBuf->iPrimarybluex,  pBuf->iPrimarybluey))
      MNG_ERROR (pData, MNG_APPCMSERROR)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_app_cms;
  }

  if ((pData->fProcessgamma) && (pBuf->bHasGAMA))
  {                                    /* inform the app */
    if (!pData->fProcessgamma ((mng_handle)pData, pBuf->iGamma))
      MNG_ERROR (pData, MNG_APPCMSERROR)
                                       /* load color-correction routine */
    pData->fCorrectrow = (mng_fptr)correct_app_cms;
  }

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_INIT_APP_CMS_OBJ, MNG_LC_END)
#endif

  return MNG_NOERROR;
}
#endif /* MNG_APP_CMS */

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

#ifdef MNG_APP_CMS
mng_retcode correct_app_cms (mng_datap pData)
{
#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_CORRECT_APP_CMS, MNG_LC_START)
#endif

  if (pData->fProcessarow)             /* let the app do something with our row */
    if (!pData->fProcessarow ((mng_handle)pData, pData->iRowsamples,
                              pData->bIsRGBA16, pData->pRGBArow))
      MNG_ERROR (pData, MNG_APPCMSERROR)

#ifdef MNG_SUPPORT_TRACE
  MNG_TRACE (pData, MNG_FN_CORRECT_APP_CMS, MNG_LC_END)
#endif

  return MNG_NOERROR;
}
#endif /* MNG_APP_CMS */

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

#endif /* MNG_INCLUDE_DISPLAY_PROCS */

/* ************************************************************************** */
/* * end of file                                                            * */
/* ************************************************************************** */