op_gammatable: add a new tool to generate DSS gamma tables
[pandora-misc.git] / op_gammatable.c
diff --git a/op_gammatable.c b/op_gammatable.c
new file mode 100644 (file)
index 0000000..dd321ed
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * op_gammatable - generate a gamma table for OMAP's DSS
+ *
+ * Copyright (c) 2012, GraÅžvydas Ignotas
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the organization nor the
+ *       names of its contributors may be used to endorse or promote products
+ *       derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+static void usage(const char *argv0)
+{
+       fprintf(stderr, "usage:\n");
+       fprintf(stderr, "%s [-n] {<gamma> | <gamma_r> <gamma_g> <gamma_b>}\n",
+               argv0);
+       exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+       unsigned int v, r, g, b;
+       float gamma[3] = { 0.f, };
+       int negative = 0, ptod = 0, half = 0;
+       int i, arg = 1;
+
+       if (argc < 2)
+               usage(argv[0]);
+       while (arg < argc && argv[arg][0] == '-') {
+               if (strcmp(argv[arg], "-h") == 0)
+                       usage(argv[0]);
+               else if (strcmp(argv[arg], "-n") == 0)
+                       negative = 1;
+               else if (strcmp(argv[arg], "-ptod") == 0)
+                       ptod = 1;
+               else if (strcmp(argv[arg], "-half") == 0)
+                       half = 1;
+               else
+                       fprintf(stderr, "%s: ignoring %s\n",
+                               argv[0], argv[arg]);
+               arg++;
+       }
+
+       if (argc - arg == 3) {
+               gamma[0] = atof(argv[arg++]);
+               gamma[1] = atof(argv[arg++]);
+               gamma[2] = atof(argv[arg++]);
+       }
+       else if (argc - arg == 1)
+               gamma[0] = gamma[1] = gamma[2] = atof(argv[arg++]);
+       else
+               usage(argv[0]);
+
+       if (gamma[0] <= 0.f || gamma[1] <= 0.f || gamma[2] <= 0.f) {
+               fprintf(stderr, "invalid arguments: %.3f %.3f %.3f\n",
+                       gamma[0], gamma[1], gamma[2]);
+               usage(argv[0]);
+       }
+
+       for (i = 0; i < 256; i++) {
+               // based on http://www.teamten.com/lawrence/graphics/gamma/
+               r = (unsigned int) (255.f * powf((float)i / 255.f, 1.f / gamma[0]) + 0.5f);
+               g = (unsigned int) (255.f * powf((float)i / 255.f, 1.f / gamma[1]) + 0.5f);
+               b = (unsigned int) (255.f * powf((float)i / 255.f, 1.f / gamma[2]) + 0.5f);
+
+               v = (r << 16) | (g << 8) | b;
+               if (negative)
+                       v ^= 0xffffff;
+               if (half)
+                       v = (v >> 1) & 0x7f7f7f;
+               if (ptod)
+                       v &= 0xff7fff;
+
+               printf("0x%06x ", v);
+       }
+       printf("\n");
+
+       return 0;
+}