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] {<gamma> | <gamma_r> <gamma_g> <gamma_b>}\n",
42 int main(int argc, char *argv[])
44 unsigned int v, r, g, b;
45 float gamma[3] = { 0.f, };
46 int negative = 0, ptod = 0, half = 0;
51 while (arg < argc && argv[arg][0] == '-') {
52 if (strcmp(argv[arg], "-h") == 0)
54 else if (strcmp(argv[arg], "-n") == 0)
56 else if (strcmp(argv[arg], "-ptod") == 0)
58 else if (strcmp(argv[arg], "-half") == 0)
61 fprintf(stderr, "%s: ignoring %s\n",
66 if (argc - arg == 3) {
67 gamma[0] = atof(argv[arg++]);
68 gamma[1] = atof(argv[arg++]);
69 gamma[2] = atof(argv[arg++]);
71 else if (argc - arg == 1)
72 gamma[0] = gamma[1] = gamma[2] = atof(argv[arg++]);
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]);
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);
88 v = (r << 16) | (g << 8) | b;
92 v = (v >> 1) & 0x7f7f7f;