flasher: enable tv-out
[pandora-misc.git] / op_gammatable.c
1 /*
2  * op_gammatable - generate a gamma table for OMAP's DSS
3  *
4  * Copyright (c) 2012, GraÅžvydas Ignotas
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of the organization nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 static void usage(const char *argv0)
35 {
36         fprintf(stderr, "usage:\n");
37         fprintf(stderr, "%s [-n] [-b <black_level>] "
38                         "{<gamma> | <gamma_r> <gamma_g> <gamma_b>}\n",
39                 argv0);
40         exit(1);
41 }
42
43 int main(int argc, char *argv[])
44 {
45         unsigned int v, r, g, b;
46         float gamma[3] = { 0.f, };
47         float base = 0.f, scale;
48         int negative = 0, ptod = 0, half = 0;
49         int i, arg = 1;
50
51         if (argc < 2)
52                 usage(argv[0]);
53         while (arg < argc && argv[arg][0] == '-') {
54                 if (strcmp(argv[arg], "-h") == 0)
55                         usage(argv[0]);
56                 else if (strcmp(argv[arg], "-n") == 0)
57                         negative = 1;
58                 else if (strcmp(argv[arg], "-b") == 0) {
59                         arg++;
60                         if (arg < argc)
61                                 base = atof(argv[arg]);
62                 }
63                 else if (strcmp(argv[arg], "-ptod") == 0)
64                         ptod = 1;
65                 else if (strcmp(argv[arg], "-half") == 0)
66                         half = 1;
67                 else
68                         fprintf(stderr, "%s: ignoring %s\n",
69                                 argv[0], argv[arg]);
70                 arg++;
71         }
72
73         if (argc - arg == 3) {
74                 gamma[0] = atof(argv[arg++]);
75                 gamma[1] = atof(argv[arg++]);
76                 gamma[2] = atof(argv[arg++]);
77         }
78         else if (argc - arg == 1)
79                 gamma[0] = gamma[1] = gamma[2] = atof(argv[arg++]);
80         else
81                 usage(argv[0]);
82
83         if (gamma[0] <= 0.f || gamma[1] <= 0.f || gamma[2] <= 0.f) {
84                 fprintf(stderr, "invalid arguments: %.3f %.3f %.3f\n",
85                         gamma[0], gamma[1], gamma[2]);
86                 usage(argv[0]);
87         }
88
89         if (base < 0.f || base > 255.f) {
90                 fprintf(stderr, "invalid black level: %.3f\n", base);
91                 usage(argv[0]);
92         }
93         scale = 255.f - base;
94         base += 0.5f; // rounding
95
96         for (i = 0; i < 256; i++) {
97                 // based on http://www.teamten.com/lawrence/graphics/gamma/
98                 r = (unsigned int) (base + scale * powf((float)i / 255.f, 1.f / gamma[0]));
99                 g = (unsigned int) (base + scale * powf((float)i / 255.f, 1.f / gamma[1]));
100                 b = (unsigned int) (base + scale * powf((float)i / 255.f, 1.f / gamma[2]));
101
102                 v = (r << 16) | (g << 8) | b;
103                 if (negative)
104                         v ^= 0xffffff;
105                 if (half)
106                         v = (v >> 1) & 0x7f7f7f;
107                 if (ptod)
108                         v &= 0xff7fff;
109
110                 printf("0x%06x ", v);
111         }
112         printf("\n");
113
114         return 0;
115 }