summaryrefslogtreecommitdiff
path: root/Source/DirectFB/src/gfx/generic/yuvtbl-gen.c
blob: 43a487d0d0a97e1ab0d3f21eb373fd9036ae2194 (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
/*
   (c) Copyright 2001-2009  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Claudio Ciccani <klan@users.sf.net>.

   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 2 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, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* YCbCr->RGB:
 * Standard ITU Rec. BT.601 derived coefficients.
 */
#define Y_FACTOR    1.16400
#define VR_FACTOR   1.59600
#define VG_FACTOR  -0.81300
#define UG_FACTOR  -0.39100
#define UB_FACTOR   2.01800

/* RGB->YCbCr:
 * ITU Rec. BT.601 defines the following formula for EyEcbEcr:
 *   
 *   Ey  = 0.299 * R + 0.587 * G + 0.114 * B
 *   Ecb = (B - Ey) * 0.564
 *   Ecr = (R - Ey) * 0.713
 *   
 * Relationship between EyEcbEcr and YCbCr:
 *
 *   Y   = Ey  * 219 / 255 + 16
 *   Cb  = Ecb * 224 / 255 + 128
 *   Cr  = Ecr * 224 / 255 + 128
 *
 * Therefore, to convert RGB to YCbCr, we compute Ey from R G B,
 * then we derive Y Cb Cr from Ey using 3 lookup tables.
 */
#define ECB_FACTOR   0.56400
#define ECR_FACTOR   0.71300


typedef struct {
     const char *type;
     const char *name;
     int         size;
     struct {
          int    min;
          int    max;
     } range;
     struct {
          int    min;
          int    max;
     } clamp;
     float       sub;
     float       mul;
     float       add;
} GenTable;
     

#define N_PER_LINE 10

static void
dump_table( GenTable *gt )
{
     int i, n;
     
     printf( "\n%s %s[%i] = {\n", gt->type, gt->name, gt->size );

     for (i = 0, n = 0; i < gt->size; i++) {
          float num;
          
          num = (i < gt->range.min)
                ? gt->range.min
                : ((i > gt->range.max) ? gt->range.max : i);
          num = ((num - gt->sub) * gt->mul) + gt->add;
          num = (num < gt->clamp.min) 
                ? gt->clamp.min
                : ((num > gt->clamp.max) ? gt->clamp.max : num);
          
          if (n == 0)
               printf( "     " );
          
          printf( "%4i", (int) num );
          
          if (i < (gt->size-1)) {
               if (++n == N_PER_LINE) {
                    printf( ",\n" );
                    n = 0;
               } else
                    printf( ", " );
          } else
               printf( "\n" );
     }

     printf( "};\n\n" );
}

int
main( int argc, char **argv )
{
     GenTable gt = {
          .size      = 256,
          .range.min = 16,
          .clamp.min = -1000,
          .clamp.max = +1000,
     };

     /* headers */
     puts( "#ifndef __YUV_TABLES_H__" );
     puts( "#define __YUV_TABLES_H__" );
     puts( "" );
     
/* YUV->RGB */
     /* luma */
     gt.type      = "const u16";
     gt.name      = "y_for_rgb";
     gt.range.max = 235;
     gt.sub       = 16;
     gt.mul       = Y_FACTOR; 
     gt.add       = 0;
     dump_table( &gt );

     /* chroma */
     gt.type      = "const s16";
     gt.range.max = 240;
     gt.sub       = 128;

     gt.name      = "cr_for_r";
     gt.mul       = VR_FACTOR;
     dump_table( &gt );

     gt.name      = "cr_for_g";
     gt.mul       = VG_FACTOR;
     dump_table( &gt );

     gt.name      = "cb_for_g";
     gt.mul       = UG_FACTOR;
     dump_table( &gt );

     gt.name      = "cb_for_b";
     gt.mul       = UB_FACTOR;
     dump_table( &gt );

/* RGB->YUV */
     gt.size      = 256;
     gt.range.min = 0;
     gt.range.max = 255; 
     gt.clamp.min = 16;
     gt.sub       = 0;
     
     /* luma */
     gt.type      = "const u16";
     gt.name      = "y_from_ey";
     gt.clamp.max = 235;
     gt.mul       = 219.0/255.0;
     gt.add       = 16;
     dump_table( &gt );

     /* chroma */
     gt.size      = 512;
     gt.range.max = 511;
     gt.clamp.max = 240;
     gt.sub       = 255;
     gt.add       = 128;
     
     /* cb */
     gt.type = "const u16";
     gt.name = "cb_from_bey";
     gt.mul  = ECB_FACTOR * 224.0 / 255.0;
     dump_table( &gt );

     /* cr */
     gt.type = "const u16";
     gt.name = "cr_from_rey";
     gt.mul  = ECR_FACTOR * 224.0 / 255.0;
     dump_table( &gt );

     /* end headers */
     puts( "#endif /* __YUV_TABLES_H__ */" );

     return 0;
 }