FreeCalypso > hg > freecalypso-sw
annotate miscutil/fc-rgbconv.c @ 1021:8b52011dc66a
doc/Pirelli-Howto: update for the current situation
author | Mychaela Falconia <falcon@ivan.Harhan.ORG> |
---|---|
date | Sat, 23 Apr 2016 06:19:09 +0000 |
parents | 813287f7169c |
children |
rev | line source |
---|---|
961
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * This utility is an aid for phone UI development. The color LCDs in the |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 * phones targeted by FreeCalypso implement 16-bit color in RGB 5:6:5 format, |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 * but these 16-bit color words are not particularly intuitive in raw hex form. |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 * In contrast, a 24-bit RGB 8:8:8 color given in RRGGBB hex form is quite |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 * intuitive, at least to those who understand RGB. |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 * |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 * This utility performs the conversion. It takes a single command line |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 * argument that must be either 4 or 6 hex digits. If the argument is 4 hex |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 * digits, it is interpreted as RGB 5:6:5 and converted into RGB 8:8:8. |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 * If the argument is 6 hex digits, it is interpreted as RGB 8:8:8 and |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 * converted into RGB 5:6:5. The output of the conversion is emitted on |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 * stdout as 4 or 6 hex digits. |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 */ |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 #include <stdio.h> |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 #include <stdlib.h> |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 #include <ctype.h> |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 #include <string.h> |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 is_arg_all_hex(str) |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 char *str; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 { |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 char *cp; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 int c; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 for (cp = str; c = *cp++; ) |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 if (!isxdigit(c)) |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 return(0); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 return(1); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 } |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 convert_565_to_888(in) |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 unsigned in; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 { |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 unsigned r, g, b; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 r = in >> 11; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 g = (in >> 5) & 0x3F; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 b = in & 0x1F; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 printf("%02X%02X%02X\n", r << 3, g << 2, b << 3); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 } |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 convert_888_to_565(in) |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 unsigned in; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 { |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 unsigned r, g, b; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 r = (in >> 16) >> 3; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 g = ((in >> 8) & 0xFF) >> 2; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 b = (in & 0xFF) >> 3; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 printf("%04X\n", (r << 11) | (g << 5) | b); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 } |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 main(argc, argv) |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 char **argv; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 { |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 unsigned in; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 if (argc != 2) { |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 usage: fprintf(stderr, |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 "please specify one 4-digit or 6-digit hex RGB value\n"); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 exit(1); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 } |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 if (!is_arg_all_hex(argv[1])) |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 goto usage; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 in = strtoul(argv[1], 0, 16); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 switch (strlen(argv[1])) { |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 case 4: |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 convert_565_to_888(in); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 exit(0); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 case 6: |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 convert_888_to_565(in); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 exit(0); |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 } |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 goto usage; |
813287f7169c
fc-rgbconv utility written and added
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 } |