summaryrefslogtreecommitdiff
path: root/tests/palette.c
blob: 3fd64c4daeade233ea48c10d7dd5e8b48cb5bccf (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
/* babl - dynamically extendable universal pixel conversion library.
 * Copyright (C) 2005, Øyvind Kolås.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "babl.h"
#include "common.inc"

int
main (void)
{
  int OK = 1;
  babl_init ();
  OK = ! babl_format_is_palette (babl_format_n (babl_type ("double"), 3));
  if(1){
    unsigned char in[][1]   = {{        0},{          1},{          2},{15}};
    unsigned char out[][4]  = {{0,0,0,255},{127,0,0,255},{0,127,0,255},{255,255,255,255}};
    const Babl *palA;// = babl_new_palette (NULL, 0);
    //Babl *palB = babl_new_palette (NULL, 0);
    //
    babl_new_palette (NULL, &palA, NULL);
    assert (palA);

    CHECK_CONV("pal to rgba", unsigned char,
        palA, babl_format("R'G'B'A u8"),
        in, out);
  }
  {
    unsigned char in[][2]   = {{    0,255},{      1,255},{      2,255},{15,200}};
    unsigned char out[][4]  = {{0,0,0,255},{127,0,0,255},{0,127,0,255},{255,255,255,200}};
    const Babl *palA;
    babl_new_palette (NULL, NULL, &palA);
    assert (palA);

    CHECK_CONV("palA to rgba", unsigned char,
        palA, babl_format("R'G'B'A u8"),
        in, out);
  }
#if 1
  {
    unsigned char in[][4]  = {{0,0,0,255},{140,0,0,255},{0,127,0,255}};
    unsigned char out[][1] = {{        0},{          1},{          2}};
    const Babl *palA;
    babl_new_palette ("palC", &palA, NULL);

    CHECK_CONV("rgba to pal", unsigned char,
         babl_format("R'G'B'A u8"), palA,
         in, out);
  }

  {
    unsigned char in[][4]  = {{0,0,0,255},{140,0,0,255},{0,127,0,127}};
    unsigned char out[][2] = {{    0,255},{      1,255},{      2,127}};
    const Babl *pal;
    babl_new_palette ("palC", NULL, &pal);

    CHECK_CONV("rgba to pal+alpha", unsigned char,
         babl_format("R'G'B'A u8"), pal,
         in, out);
  }

  /* check with a custom floating point palette, _and_ alpha component  */
  {
    float palette[] = {
      0.5,  1.0,
      0.23, 0.42,
      1.0,  0.2
    };

    unsigned char in[][2]   = {{          0,255},{0,127},{       1,255},{         2,255}};
    unsigned char out[][4]  = {{128,128,128,255},{128,128,128,127},{59,59,59,107},{255,255,255,51}};

    const Babl *pal;
    babl_new_palette (NULL, NULL, &pal);

    babl_palette_set_palette (pal, babl_format ("YA float"), palette, 3);

    CHECK_CONV("rgba to YA float pal+alpha", unsigned char,
         pal, babl_format("RGBA u8"),
         in, out);
  }

  /* check with a custom floating point palette, _and_ alpha component  */
  {
    float palette[] = {
      0.5,  1.0,
      0.23, 0.42,
      1.0,  0.2
    };

    unsigned char in[][2]   = {{          0,255},{0,127},{       1,255},{         2,255}};
    unsigned char out[][4]  = {{128,128,128,255},{128,128,128,127},{59,59,59,107},{255,255,255,51}};

    const Babl *pal;
    babl_new_palette (NULL, NULL, &pal);

    babl_palette_set_palette (pal, babl_format ("YA float"), palette, 3);

    CHECK_CONV("rgba to YA float pal+alpha", unsigned char,
         pal, babl_format("RGBA u8"),
         in, out);
  }

  /* check with a custom floating point palette */
  {
    float palette[] = {
      0.5,  1.0,
      0.23, 0.42,
      1.0,  0.2
    };

    unsigned char in[][1]   = {{              0},{          1},{          2}};
    unsigned char out[][4]  = {{128,128,128,255},{59,59,59,107},{255,255,255,51}};

    const Babl *pal;
    babl_new_palette (NULL, &pal, NULL);

    babl_palette_set_palette (pal, babl_format ("YA float"), palette, 3);

    CHECK_CONV("rgba to float pal", unsigned char,
         pal, babl_format("RGBA u8"),
         in, out);

    {
      const Babl *p2;
      p2 = babl_format_with_space ((void*)pal, babl_space ("ACEScg"));


    CHECK_CONV("rgba to float pal", unsigned char,
         p2, babl_format("RGBA u8"),
         in, out);


    }

  }
#endif



  babl_exit ();
  return !OK;
}