dd321ed48b86fdf83c920919b049fec25266ea71
[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] {<gamma> | <gamma_r> <gamma_g> <gamma_b>}\n",
38                 argv0);
39         exit(1);
40 }
41
42 int main(int argc, char *argv[])
43 {
44         unsigned int v, r, g, b;
45         float gamma[3] = { 0.f, };
46         int negative = 0, ptod = 0, half = 0;
47         int i, arg = 1;
48
49         if (argc < 2)
50                 usage(argv[0]);
51         while (arg < argc && argv[arg][0] == '-') {
52                 if (strcmp(argv[arg], "-h") == 0)
53                         usage(argv[0]);
54                 else if (strcmp(argv[arg], "-n") == 0)
55                         negative = 1;
56                 else if (strcmp(argv[arg], "-ptod") == 0)
57                         ptod = 1;
58                 else if (strcmp(argv[arg], "-half") == 0)
59                         half = 1;
60                 else
61                         fprintf(stderr, "%s: ignoring %s\n",
62                                 argv[0], argv[arg]);
63                 arg++;
64         }
65
66         if (argc - arg == 3) {
67                 gamma[0] = atof(argv[arg++]);
68                 gamma[1] = atof(argv[arg++]);
69                 gamma[2] = atof(argv[arg++]);
70         }
71         else if (argc - arg == 1)
72                 gamma[0] = gamma[1] = gamma[2] = atof(argv[arg++]);
73         else
74                 usage(argv[0]);
75
76         if (gamma[0] <= 0.f || gamma[1] <= 0.f || gamma[2] <= 0.f) {
77                 fprintf(stderr, "invalid arguments: %.3f %.3f %.3f\n",
78                         gamma[0], gamma[1], gamma[2]);
79                 usage(argv[0]);
80         }
81
82         for (i = 0; i < 256; i++) {
83                 // based on http://www.teamten.com/lawrence/graphics/gamma/
84                 r = (unsigned int) (255.f * powf((float)i / 255.f, 1.f / gamma[0]) + 0.5f);
85                 g = (unsigned int) (255.f * powf((float)i / 255.f, 1.f / gamma[1]) + 0.5f);
86                 b = (unsigned int) (255.f * powf((float)i / 255.f, 1.f / gamma[2]) + 0.5f);
87
88                 v = (r << 16) | (g << 8) | b;
89                 if (negative)
90                         v ^= 0xffffff;
91                 if (half)
92                         v = (v >> 1) & 0x7f7f7f;
93                 if (ptod)
94                         v &= 0xff7fff;
95
96                 printf("0x%06x ", v);
97         }
98         printf("\n");
99
100         return 0;
101 }