2 * op_gammatable - generate a gamma table for OMAP's DSS
4 * Copyright (c) 2012, GraÅžvydas Ignotas
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.
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.
34 static void usage(const char *argv0)
36 fprintf(stderr, "usage:\n");
37 fprintf(stderr, "%s [-n] [-b <black_level>] "
38 "{<gamma> | <gamma_r> <gamma_g> <gamma_b>}\n",
43 int main(int argc, char *argv[])
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;
53 while (arg < argc && argv[arg][0] == '-') {
54 if (strcmp(argv[arg], "-h") == 0)
56 else if (strcmp(argv[arg], "-n") == 0)
58 else if (strcmp(argv[arg], "-b") == 0) {
61 base = atof(argv[arg]);
63 else if (strcmp(argv[arg], "-ptod") == 0)
65 else if (strcmp(argv[arg], "-half") == 0)
68 fprintf(stderr, "%s: ignoring %s\n",
73 if (argc - arg == 3) {
74 gamma[0] = atof(argv[arg++]);
75 gamma[1] = atof(argv[arg++]);
76 gamma[2] = atof(argv[arg++]);
78 else if (argc - arg == 1)
79 gamma[0] = gamma[1] = gamma[2] = atof(argv[arg++]);
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]);
89 if (base < 0.f || base > 255.f) {
90 fprintf(stderr, "invalid black level: %.3f\n", base);
94 base += 0.5f; // rounding
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]));
102 v = (r << 16) | (g << 8) | b;
106 v = (v >> 1) & 0x7f7f7f;
110 printf("0x%06x ", v);