File: extract.c

package info (click to toggle)
babl 0.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,912 kB
  • sloc: ansic: 15,162; sh: 11,131; makefile: 362; ruby: 90
file content (144 lines) | stat: -rw-r--r-- 4,152 bytes parent folder | download | duplicates (2)
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
/* 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
 * <http://www.gnu.org/licenses/>.
 */

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


#define CHECK_CONV(test_name, componenttype, src_fmt, dst_fmt, src_pix, expected_pix) \
  {       \
  const Babl *fish;       \
  int i;       \
  fish = babl_fish (src_fmt, dst_fmt);       \
  if (!fish)       \
    {       \
      printf ("  %s failed to make fish\n", test_name);       \
      OK = 0;       \
    }       \
  for (i = 0; i < sizeof(src_pix)/sizeof(src_pix[0]); i ++)       \
    {       \
      int c;\
      componenttype result[10];       \
      babl_process (fish, src_pix[i], result, 1);       \
      for (c = 0; c < sizeof(expected_pix[i])/sizeof(expected_pix[i][0]); c++) \
      if (result[c] != expected_pix[i][c])       \
        {       \
          printf (" %s failed #%i[%i]   %i\n", test_name, i, c, result[c]);       \
          OK = 0;          \
        }       \
    }       \
  }

int
main (int    argc,
      char **argv)
{
  int OK = 1;
  babl_init ();

  {
    char in[][4]  = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
    char out[][1] = {{      3},{      7},{       11}};

    babl_format_new ("name", "A u8",
         babl_model ("YA"), babl_type ("u8"),
          babl_component ("A"), NULL);

    CHECK_CONV("extract alpha", char,
        babl_format("RGBA u8"), babl_format("A u8"),
        in, out);
  }

  {
    char in[][4]  = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
    char out[][1] = {{  1    },{  5    },{  9      }};

    babl_format_new ("name", "G u8",
         babl_model ("RGBA"), babl_type ("u8"),
          babl_component ("G"), NULL);

    CHECK_CONV("extract green", char,
         babl_format("RGBA u8"), babl_format("G u8"),
         in, out);
  }


  {
    char in[][4]  = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
    char out[][2] = {{  2,1  },{  6,5  },{  10,9   }};

    babl_format_new ("name", "BG u8",
         babl_model ("RGBA"), babl_type ("u8"),
          babl_component ("B"),
          babl_component ("G"), NULL);

    CHECK_CONV("extract green", char,
         babl_format("RGBA u8"), babl_format("BG u8"),
         in, out);
  }



  {
    float in[][4]  = {{0,1,2,3/255.0},{4,5,6,277/255.0},{8,9,10,101/255.0}};
    char out[][1] = {{       3},      {      255},{             101}};

    CHECK_CONV("extract alpha from float", char,
         babl_format("RGBA float"), babl_format("A u8"),
         in, out);
  }

  {
    char in[][4]   = {{1,2,3,4},{4,5,6,7},{8,9,10,11}};
    char out[][4]  = {{4,3,2,1},{7,6,5,4},{11,10,9,8}};

    babl_format_new ("name", "abgr",
         babl_model ("RGBA"), babl_type ("u8"),
          babl_component ("A"),
          babl_component ("B"),
          babl_component ("G"),
          babl_component ("R"),
          NULL);

    CHECK_CONV("bgra", char,
         babl_format("RGBA u8"), babl_format("abgr"),
         in, out);
  }

  {
    char in[][4]   = {{1,2,3,4},{4,5,6,7},{8,9,10,11}};
    //char out[][4]= {{3,0,0,4},{6,0,0,7},{10,0,0,11}};
    char out[][4]  = {{3,3,3,4},{6,6,6,7},{10,10,10,11}};

    CHECK_CONV("bPADa", char,
         babl_format("RGBA u8"),
         babl_format_new (babl_model ("RGBA"), babl_type ("u8"),
               babl_component ("B"),
               babl_component ("PAD"),
               babl_component ("PAD"),
               babl_component ("A"),
               NULL),
         in, out);
  }

  babl_exit ();
  return !OK;
}