about summary refs log tree commit diff
path: root/lib/libpamn.c
blob: ae28283a26888303174db072f0f315a0e4725530 (plain) (blame)
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
/*=============================================================================
                                  libpamn.c
===============================================================================
   These are the library functions, which belong in the libnetpbm library,
   that deal with the PAM image format via maxval-normalized, floating point
   sample values.

   This file was originally written by Bryan Henderson and is contributed
   to the public domain by him and subsequent authors.
=============================================================================*/

#include <assert.h>

#include "netpbm/pm_c_util.h"
#include "netpbm/mallocvar.h"
#include "netpbm/nstring.h"

#include "pam.h"
#include "fileio.h"
#include "pm_gamma.h"

#define EPSILON 1e-7



static unsigned int
allocationDepth(const struct pam * const pamP) {

    unsigned int retval;

    if (pamP->len >= PAM_STRUCT_SIZE(allocation_depth)) {
        if (pamP->allocation_depth == 0)
            retval = pamP->depth;
        else {
            if (pamP->depth > pamP->allocation_depth)
                pm_error("'allocationDepth' (%u) is smaller than 'depth' (%u)",
                         pamP->allocation_depth, pamP->depth);
            retval = pamP->allocation_depth;
        }
    } else
        retval = pamP->depth;
    return retval;
}



tuplen
pnm_allocpamtuplen(const struct pam * const pamP) {

    tuplen retval;

    retval = malloc(allocationDepth(pamP) * sizeof(retval[0]));

    if (retval == NULL)
        pm_error("Out of memory allocating %u-plane normalized tuple",
                 allocationDepth(pamP));

    return retval;
}



static void
allocpamrown(const struct pam * const pamP,
             tuplen **          const tuplerownP,
             const char **      const errorP) {
/*----------------------------------------------------------------------------
   We assume that the dimensions of the image are such that arithmetic
   overflow will not occur in our calculations.  NOTE: pnm_readpaminit()
   ensures this assumption is valid.
-----------------------------------------------------------------------------*/
    unsigned int const bytes_per_tuple =
        allocationDepth(pamP) * sizeof(samplen);

    tuplen * tuplerown;
    const char * error;

    /* The tuple row data structure starts with 'width' pointers to
       the tuples, immediately followed by the 'width' tuples
       themselves.  Each tuple consists of 'depth' samples.
    */

    tuplerown = malloc(pamP->width * (sizeof(tuplen *) + bytes_per_tuple));
    if (tuplerown == NULL)
        pm_asprintf(&error, "Out of memory allocating space for a tuple row of"
                    "%u tuples by %u samples per tuple "
                    "by %u bytes per sample.",
                    pamP->width, allocationDepth(pamP),
                    (unsigned)sizeof(samplen));
    else {
        /* Now we initialize the pointers to the individual tuples to make this
           a regulation C two dimensional array.
        */

        unsigned char * p;
        unsigned int i;

        p = (unsigned char*) (tuplerown + pamP->width);
            /* location of Tuple 0 */
        for (i = 0; i < pamP->width; ++i) {
            tuplerown[i] = (tuplen) p;
            p += bytes_per_tuple;
        }
        *errorP = NULL;
        *tuplerownP = tuplerown;
    }
}



tuplen *
pnm_allocpamrown(const struct pam * const pamP) {
/*----------------------------------------------------------------------------
   We assume that the dimensions of the image are such that arithmetic
   overflow will not occur in our calculations.  NOTE: pnm_readpaminit()
   ensures this assumption is valid.
-----------------------------------------------------------------------------*/
    const char * error;
    tuplen * tuplerown;

    allocpamrown(pamP, &tuplerown, &error);

    if (error) {
        pm_errormsg("pnm_allocpamrown() failed.  %s", error);
        pm_strfree(error);
        pm_longjmp();
    }

    return tuplerown;
}



static void
readpbmrow(const struct pam * const pamP,
           tuplen *           const tuplenrow) {

    bit * bitrow;
    jmp_buf jmpbuf;
    jmp_buf * origJmpbufP;

    bitrow = pbm_allocrow(pamP->width);

    if (setjmp(jmpbuf) != 0) {
        pbm_freerow(bitrow);
        pm_setjmpbuf(origJmpbufP);
        pm_longjmp();
    } else {
        unsigned int col;
        pm_setjmpbufsave(&jmpbuf, &origJmpbufP);

        pbm_readpbmrow(pamP->file, bitrow, pamP->width, pamP->format);

        for (col = 0; col < pamP->width; ++col)
            tuplenrow[col][0] = bitrow[col] == PBM_BLACK ? 0.0 : 1.0;

        pm_setjmpbuf(origJmpbufP);
    }
    pbm_freerow(bitrow);
}



static void
readpamrow(const struct pam * const pamP,
           tuplen *           const tuplenrow) {

    jmp_buf jmpbuf;
    jmp_buf * origJmpbufP;
    tuple * tuplerow;

    tuplerow = pnm_allocpamrow(pamP);

    if (setjmp(jmpbuf) != 0) {
        pnm_freepamrow(tuplerow);
        pm_setjmpbuf(origJmpbufP);
        pm_longjmp();
    } else {
        float const scaler = 1.0 / pamP->maxval;
            /* Note: multiplication is faster than division, so we divide
               once here so we can multiply many times later.
            */

        unsigned int col;

        pm_setjmpbufsave(&jmpbuf, &origJmpbufP);

        pnm_readpamrow(pamP, tuplerow);
        for (col = 0; col < pamP->width; ++col) {
            unsigned int plane;
            for (plane = 0; plane < pamP->depth; ++plane)
                tuplenrow[col][plane] = tuplerow[col][plane] * scaler;
        }
        pm_setjmpbuf(origJmpbufP);
    }
    pnm_freepamrow(tuplerow);
}



void
pnm_readpamrown(const struct pam * const pamP,
                tuplen *           const tuplenrow) {

    /* For speed, we don't check any of the inputs for consistency
       here (unless it's necessary to avoid crashing).  Any consistency
       checking should have been done by a prior call to
       pnm_writepaminit().
    */
    assert(pamP->maxval != 0);

    /* Need a special case for raw PBM because it has multiple tuples (8)
       packed into one byte.
    */
    if (PAM_FORMAT_TYPE(pamP->format) == PBM_TYPE) {
        if (pamP->depth != 1)
            pm_error("Invalid pam structure passed to pnm_readpamrow().  "
                     "It says PBM format, but 'depth' member is not 1.");

        readpbmrow(pamP, tuplenrow);
    } else
        readpamrow(pamP, tuplenrow);
}



static void
writepbmrow(const struct pam * const pamP,
            const tuplen *     const tuplenrow) {

    jmp_buf jmpbuf;
    jmp_buf * origJmpbufP;
    bit * bitrow;

    bitrow = pbm_allocrow(pamP->width);

    if (setjmp(jmpbuf) != 0) {
        pbm_freerow(bitrow);
        pm_setjmpbuf(origJmpbufP);
        pm_longjmp();
    } else {
        unsigned int col;

        pm_setjmpbufsave(&jmpbuf, &origJmpbufP);

        for (col = 0; col < pamP->width; ++col)
            bitrow[col] = tuplenrow[col][0] < 0.5 ? PBM_BLACK : PBM_WHITE;
        pbm_writepbmrow(pamP->file, bitrow, pamP->width,
                        pamP->format == PBM_FORMAT);

        pm_setjmpbuf(origJmpbufP);
    }
    pbm_freerow(bitrow);
}



static void
writepamrow(const struct pam * const pamP,
            const tuplen *     const tuplenrow) {

    jmp_buf jmpbuf;
    jmp_buf * origJmpbufP;
    tuple * tuplerow;

    tuplerow = pnm_allocpamrow(pamP);

    if (setjmp(jmpbuf) != 0) {
        pnm_freepamrow(tuplerow);
        pm_setjmpbuf(origJmpbufP);
        pm_longjmp();
    } else {
        unsigned int col;

        pm_setjmpbufsave(&jmpbuf, &origJmpbufP);

        for (col = 0; col < pamP->width; ++col) {
            unsigned int plane;
            for (plane = 0; plane < pamP->depth; ++plane)
                tuplerow[col][plane] = (sample)
                    (tuplenrow[col][plane] * pamP->maxval + 0.5);
        }
        pnm_writepamrow(pamP, tuplerow);

        pm_setjmpbuf(origJmpbufP);
    }
    pnm_freepamrow(tuplerow);
}



void
pnm_writepamrown(const struct pam * const pamP,
                 const tuplen *     const tuplenrow) {

    /* For speed, we don't check any of the inputs for consistency
       here (unless it's necessary to avoid crashing).  Any consistency
       checking should have been done by a prior call to
       pnm_writepaminit().
    */
    assert(pamP->maxval != 0);

    /* Need a special case for raw PBM because it has multiple tuples (8)
       packed into one byte.
    */
    if (PAM_FORMAT_TYPE(pamP->format) == PBM_TYPE)
        writepbmrow(pamP, tuplenrow);
    else
        writepamrow(pamP, tuplenrow);
}



tuplen **
pnm_allocpamarrayn(const struct pam * const pamP) {

    tuplen ** tuplenarray;
    const char * error;

    /* If the speed of this is ever an issue, it might be sped up a little
       by allocating one large chunk.
    */

    MALLOCARRAY(tuplenarray, pamP->height);
    if (tuplenarray == NULL)
        pm_asprintf(&error,
                    "Out of memory allocating the row pointer section of "
                    "a %u row array", pamP->height);
    else {
        unsigned int rowsDone;

        rowsDone = 0;
        error = NULL;

        while (rowsDone < pamP->height && !error) {
            allocpamrown(pamP, &tuplenarray[rowsDone], &error);
            if (!error)
                ++rowsDone;
        }
        if (error) {
            unsigned int row;
            for (row = 0; row < rowsDone; ++row)
                pnm_freepamrown(tuplenarray[rowsDone]);
            free(tuplenarray);
        }
    }
    if (error) {
        pm_errormsg("pnm_allocpamarrayn() failed.  %s", error);
        pm_strfree(error);
        pm_longjmp();
    }

    return(tuplenarray);
}



void
pnm_freepamarrayn(tuplen **          const tuplenarray,
                  const struct pam * const pamP) {

    int row;
    for (row = 0; row < pamP->height; row++)
        pnm_freepamrown(tuplenarray[row]);

    free(tuplenarray);
}



tuplen**
pnm_readpamn(FILE *       const file,
             struct pam * const pamP,
             int          const size) {

    tuplen **tuplenarray;
    jmp_buf jmpbuf;
    jmp_buf * origJmpbufP;

    pnm_readpaminit(file, pamP, size);

    tuplenarray = pnm_allocpamarrayn(pamP);

    if (setjmp(jmpbuf) != 0) {
        pnm_freepamarrayn(tuplenarray, pamP);
        pm_setjmpbuf(origJmpbufP);
        pm_longjmp();
    } else {
        unsigned int row;

        pm_setjmpbufsave(&jmpbuf, &origJmpbufP);

        for (row = 0; row < pamP->height; ++row)
            pnm_readpamrown(pamP, tuplenarray[row]);

        pm_setjmpbuf(origJmpbufP);
    }
    return tuplenarray;
}



void
pnm_writepamn(struct pam * const pamP,
              tuplen **    const tuplenarray) {

    unsigned int row;

    pnm_writepaminit(pamP);

    for (row = 0; row < pamP->height; ++row)
        pnm_writepamrown(pamP, tuplenarray[row]);
}



samplen
pnm_normalized_sample(struct pam * const pamP,
                      sample       const sample) {
    return (samplen)sample/pamP->maxval;
}

sample
pnm_unnormalized_sample(struct pam * const pamP,
                        samplen      const sampleVal) {
    double const epsilon = 1e-6;
    return (sample)((sampleVal + epsilon) * pamP->maxval + 0.5);
}

void
pnm_normalizetuple(struct pam * const pamP,
                   tuple        const tuple,
                   tuplen       const tuplen) {

    unsigned int plane;

    for (plane = 0; plane < pamP->depth; ++plane)
        tuplen[plane] = pnm_normalized_sample(pamP, tuple[plane]);
}



void
pnm_unnormalizetuple(struct pam * const pamP,
                     tuplen       const tuplen,
                     tuple        const tuple) {

    unsigned int plane;

    for (plane = 0; plane < pamP->depth; ++plane)
        tuple[plane] = pnm_unnormalized_sample(pamP, tuplen[plane]);
}



void
pnm_normalizeRow(struct pam *             const pamP,
                 const tuple *            const tuplerow,
                 const pnm_transformMap * const transform,
                 tuplen *                 const tuplenrow) {

    float const scaler = 1.0 / pamP->maxval;
        /* Note: multiplication is faster than division, so we divide
           once here so we can multiply many times later.
        */
    unsigned int plane;

    for (plane = 0; plane < pamP->depth; ++plane) {
        if (transform && transform[plane]) {
            unsigned int col;
            for (col = 0; col < pamP->width; ++col) {
                sample const sample = tuplerow[col][plane];
                tuplenrow[col][plane] = transform[plane][sample];
            }
        } else {
            unsigned int col;
            for (col = 0; col < pamP->width; ++col)
                tuplenrow[col][plane] = tuplerow[col][plane] * scaler;
        }
    }
}



static sample
reversemap(samplen          const samplen,
           pnm_transformMap const transformMap,
           sample           const maxval) {
/*----------------------------------------------------------------------------
   Find the integer sample value that maps to the normalized samplen value
   'samplen' through the map 'transformMap'.  We interpret the map as
   mapping the value N+1 to all the values transformMap[N] through
   transformMap[N+1], and we expect transformMap[N+1] to be greater than
   transformMap[N] for all N.
-----------------------------------------------------------------------------*/
    /* Do a binary search, since the values are in sorted (increasing)
       order
    */

    sample low, high;

    low = 0; high = maxval;  /* Consider whole range to start */

    while (low < high) {
        unsigned int const middle = (low + high) / 2;

        if (samplen < transformMap[middle])
            /* Restrict  our consideration to the lower half of the range */
            high = middle;
        else
            /* Restrict our consideration to the upper half of the range */
            low = middle + 1;
    }
    return low;
}



void
pnm_unnormalizeRow(struct pam *             const pamP,
                   const tuplen *           const tuplenrow,
                   const pnm_transformMap * const transform,
                   tuple *                  const tuplerow) {

    unsigned int plane;

    for (plane = 0; plane < pamP->depth; ++plane) {
        if (transform && transform[plane]) {
            unsigned int col;
            for (col = 0; col < pamP->width; ++col)
                tuplerow[col][plane] =
                    reversemap(tuplenrow[col][plane],
                               transform[plane], pamP->maxval);
        } else {
            unsigned int col;
            for (col = 0; col < pamP->width; ++col)
                tuplerow[col][plane] =
                    tuplenrow[col][plane] * pamP->maxval + 0.5;
        }
    }
}



typedef samplen (*gammaFunction)(samplen);

static void
gammaCommon(struct pam *  const pamP,
            tuplen *      const tuplenrow,
            gammaFunction       gammafn) {

    unsigned int plane;
    unsigned int opacityPlane;
    int haveOpacity;

    pnm_getopacity(pamP, &haveOpacity, &opacityPlane);

    for (plane = 0; plane < pamP->depth; ++plane) {
        if (haveOpacity && plane == opacityPlane) {
            /* It's an opacity (alpha) plane, which means there is
               no gamma adjustment in it.
            */
        } else {
            unsigned int col;
            for (col = 0; col < pamP->width; ++col)
                tuplenrow[col][plane] = gammafn(tuplenrow[col][plane]);
        }
    }
}



void
pnm_gammarown(struct pam * const pamP,
              tuplen *     const tuplenrow) {

    gammaCommon(pamP, tuplenrow, &pm_gamma709);
}



void
pnm_ungammarown(struct pam * const pamP,
                tuplen *     const tuplenrow) {

    gammaCommon(pamP, tuplenrow, &pm_ungamma709);
}



enum applyUnapply {OPACITY_APPLY, OPACITY_UNAPPLY};

static void
applyopacityCommon(enum applyUnapply const applyUnapply,
                   struct pam *      const pamP,
                   tuplen *          const tuplenrow) {
/*----------------------------------------------------------------------------
   Either apply or unapply opacity to the row tuplenrow[], per
   'applyUnapply'.  Apply means to multiply each foreground sample by
   the opacity value for that pixel; Unapply means to do the inverse, as
   if the foreground values had already been so multiplied.
-----------------------------------------------------------------------------*/
    unsigned int opacityPlane;
    int haveOpacity;

    pnm_getopacity(pamP, &haveOpacity, &opacityPlane);

    if (haveOpacity) {
        unsigned int plane;
        for (plane = 0; plane < pamP->depth; ++plane) {
            if (plane != opacityPlane) {
                unsigned int col;
                for (col = 0; col < pamP->width; ++col) {
                    tuplen const thisTuple = tuplenrow[col];

                    switch (applyUnapply) {
                    case OPACITY_APPLY:
                        thisTuple[plane] *= thisTuple[opacityPlane];
                        break;
                    case OPACITY_UNAPPLY:
                        if (thisTuple[opacityPlane] < EPSILON) {
                            /* There is no foreground here at all.  So
                               the color plane values must be zero and
                               as output it makes absolutely no
                               difference what they are (they must be
                               multiplied by the opacity -- zero -- to
                               be used).
                            */
                            assert(thisTuple[plane] < EPSILON);
                        } else
                            thisTuple[plane] /= thisTuple[opacityPlane];
                        break;
                    }
                }
            }
        }
    }
}


void
pnm_applyopacityrown(struct pam * const pamP,
                     tuplen *     const tuplenrow) {

    applyopacityCommon(OPACITY_APPLY, pamP, tuplenrow);

}



void
pnm_unapplyopacityrown(struct pam * const pamP,
                       tuplen *     const tuplenrow) {

    applyopacityCommon(OPACITY_UNAPPLY, pamP, tuplenrow);
}



void
pnm_maketuplergbn(const struct pam * const pamP,
                  tuplen             const tuple) {

    if (allocationDepth(pamP) < 3)
        pm_error("allocation depth %u passed to pnm_maketuplergb().  "
                 "Must be at least 3.", allocationDepth(pamP));

    if (pamP->depth < 3)
        tuple[2] = tuple[1] = tuple[0];
}



void
pnm_makerowrgbn(const struct pam * const pamP,
                tuplen *           const tuplerow) {

    if (pamP->depth < 3) {
        unsigned int col;

        if (allocationDepth(pamP) < 3)
            pm_error("allocation depth %u passed to pnm_makerowrgb().  "
                     "Must be at least 3.", allocationDepth(pamP));

        for (col = 0; col < pamP->width; ++col) {
            tuplen const thisTuple = tuplerow[col];
            thisTuple[2] = thisTuple[1] = thisTuple[0];
        }
    }
}



void
pnm_makearrayrgbn(const struct pam * const pamP,
                  tuplen **          const tuples) {

    if (pamP->depth < 3) {
        unsigned int row;
        if (allocationDepth(pamP) < 3)
            pm_error("allocation depth %u passed to pnm_makearrayrgb().  "
                     "Must be at least 3.", allocationDepth(pamP));

        for (row = 0; row < pamP->height; ++row) {
            tuplen * const tuplerow = tuples[row];
            unsigned int col;
            for (col = 0; col < pamP->width; ++col) {
                tuplen const thisTuple = tuplerow[col];
                thisTuple[2] = thisTuple[1] = thisTuple[0];
            }
        }
    }
}



static void
fillInMap(pnm_transformMap const ungammaTransformMap,
          sample           const maxval,
          float            const offset) {

    float const scaler = 1.0/maxval;  /* divide only once, it's slow */

    sample sample;

    /* Fill in the map */
    for (sample = 0; sample <= maxval; ++sample) {
        samplen const samplen = (sample + offset) * scaler;
        ungammaTransformMap[sample] = pm_ungamma709(samplen);
    }
}



static pnm_transformMap *
createUngammaMapOffset(const struct pam * const pamP,
                       float              const offset) {
/*----------------------------------------------------------------------------
   Create a transform table that computes ungamma(arg+offset) for arg
   in [0..maxval]; So with offset == 0, you get a function that can be
   used in converting integer sample values to normalized ungamma'ed
   samplen values.  But with offset == 0.5, you get a function that
   can be used in a reverse lookup to convert normalized ungamma'ed
   samplen values to integer sample values.  The 0.5 effectively does
   the rounding.

   This never throws an error.  Return value NULL means failed.
-----------------------------------------------------------------------------*/
    pnm_transformMap * retval;
    pnm_transformMap ungammaTransformMap;

    MALLOCARRAY(retval, pamP->depth);

    if (retval != NULL) {
        MALLOCARRAY(ungammaTransformMap, pamP->maxval+1);

        if (ungammaTransformMap != NULL) {
            int haveOpacity;
            unsigned int opacityPlane;
            unsigned int plane;

            pnm_getopacity(pamP, &haveOpacity, &opacityPlane);

            for (plane = 0; plane < pamP->depth; ++plane) {
                if (haveOpacity && plane == opacityPlane)
                    retval[plane] = NULL;
                else
                    retval[plane] = ungammaTransformMap;
            }
            fillInMap(ungammaTransformMap, pamP->maxval, offset);
        } else {
            free(retval);
            retval = NULL;
        }
    }
    return retval;
}



pnm_transformMap *
pnm_createungammatransform(const struct pam * const pamP) {

    return createUngammaMapOffset(pamP, 0.0);
}



pnm_transformMap *
pnm_creategammatransform(const struct pam * const pamP) {

    /* Since we're creating a map to be used backwards (you search for
       the normalized value in the array, and the result is the array
       index at which you found it), the gamma transform map is almost
       identical to the ungamma transform map -- just with a 0.5 offset
       to effect rounding.
    */
    return createUngammaMapOffset(pamP, 0.5);
}



void
pnm_freegammatransform(const pnm_transformMap * const transform,
                       const struct pam *       const pamP) {

    unsigned int plane;

    for (plane = 0; plane < pamP->depth; ++plane)
        if (transform[plane])
            free(transform[plane]);

    free((void*)transform);
}