]> mj.ucw.cz Git - libucw.git/blob - images/color-tool.c
Merge with git+ssh://cvs.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / images / color-tool.c
1 /*
2  *      Color spaces tool
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "images/images.h"
12 #include "images/color.h"
13
14 #include <getopt.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17 #include <stdio.h>
18 #include <string.h>
19
20 static void NONRET
21 usage(void)
22 {
23   fputs("\
24 Usage: color-tool input-color-space output-color-space\n\
25 ", stderr);
26   exit(1);
27 }
28
29 static char *shortopts = "";
30 static struct option longopts[] =
31 {
32   { NULL,                       0, 0, 0 }
33 };
34
35 static const struct color_space_info *
36 parse_color_space(byte *s)
37 {
38   if (!strcasecmp(s, "sRGB"))
39     return &color_srgb_info;
40   else if (!strcasecmp(s, "AdobeRGB") || !strcasecmp(s, "Adobe RGB"))
41     return &color_adobe_rgb_info;
42   else if (!strcasecmp(s, "CIERGB") || strcasecmp(s, "CIE RGB"))
43     return &color_cie_rgb_info;
44   else
45     die("Unknown color space");
46 }
47
48 static void
49 print_matrix(double m[9])
50 {
51   for (uns j = 0; j < 3; j++)
52     {
53       for (uns i = 0; i < 3; i++)
54         printf(" %12.8f", m[i + j * 3]);
55       printf("\n");
56     }
57 }
58
59 int
60 main(int argc, char **argv)
61 {
62   log_init(argv[0]);
63   int opt;
64   while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) >= 0)
65     switch (opt)
66       {
67         default:
68           usage();
69       }
70
71   if (argc == optind + 1)
72     {
73       const struct color_space_info *a = parse_color_space(argv[optind]);
74       double a_to_xyz[9], xyz_to_a[9];
75       color_compute_color_space_to_xyz_matrix(a_to_xyz, &a->chromacity);
76       color_invert_matrix(xyz_to_a, a_to_xyz);
77       printf("linear %s -> XYZ:\n", a->name);
78       print_matrix(a_to_xyz);
79       printf("XYZ -> linear %s:\n", a->name);
80       print_matrix(xyz_to_a);
81       printf("Simple gamma: %.8f\n", a->gamma.simple_gamma);
82       printf("Detailed gamma: g=%.8f o=%.8f t=%.8f s=%.8f\n", a->gamma.detailed_gamma, a->gamma.offset, a->gamma.transition, a->gamma.slope);
83     }
84   else if (argc == optind + 2)
85     {
86       const struct color_space_info *a = parse_color_space(argv[optind++]);
87       const struct color_space_info *b = parse_color_space(argv[optind]);
88       double a_to_b[9];
89       color_compute_color_spaces_conversion_matrix(a_to_b, &a->chromacity, &b->chromacity);
90       printf("linear %s -> linear %s:\n", a->name, b->name);
91       print_matrix(a_to_b);
92     }
93   else
94     usage();
95
96   return 0;
97 }