1 /* --------------------------------------------------------------------------------------------------------- */
2 /* sane - Scanner Access Now Easy.
3
4 gamma4scanimage
5
6 (C) 2002 Oliver Rauch
7
8 This file is part of the SANE package.
9
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <https://www.gnu.org/licenses/>.
22
23 */
24 /* --------------------------------------------------------------------------------------------------------- */
25
26 #include <stdio.h>
27 #include <math.h>
28 #include <stdlib.h>
29
main(int argc,char ** argv)30 int main(int argc, char **argv)
31 {
32 double gamma = 1.0;
33 int maxin = 16383; /* 14 bit gamma input */
34 int maxout = 255; /* 8 bit gamma output */
35 int shadow = 0;
36 int highlight = maxin;
37 int in, out;
38 float f;
39
40 if ( (argc==1) || (argc>6) )
41 {
42 printf("Usage: %s gamma [shadow [highlight [maxin [maxout]]]]\n", argv[0]);
43 exit(-1);
44 }
45
46 if (argc>1)
47 {
48 gamma = atof(argv[1]);
49 }
50
51 if (argc>2)
52 {
53 shadow = atoi(argv[2]);
54 }
55
56 if (argc>3)
57 {
58 highlight = atoi(argv[3]);
59 }
60
61 if (argc>4)
62 {
63 maxin = atoi(argv[4]);
64 }
65
66 if (argc>5)
67 {
68 maxout = atoi(argv[5]);
69 }
70
71 if (shadow < 0)
72 {
73 printf("%s error: shadow=%d < 0\n", argv[0], shadow);
74 exit(-1);
75 }
76
77 if (highlight < 0)
78 {
79 printf("%s error: highlight=%d < 0\n", argv[0], highlight);
80 exit(-1);
81 }
82
83 if (maxin < 0)
84 {
85 printf("%s error: maxin=%d < 0\n", argv[0], maxin);
86 exit(-1);
87 }
88
89 if (maxout < 0)
90 {
91 printf("%s error: maxout=%d < 0\n", argv[0], maxout);
92 exit(-1);
93 }
94
95 if (shadow >= highlight)
96 {
97 printf("%s error: shadow=%d >= highlight=%d\n", argv[0], shadow, highlight);
98 exit(-1);
99 }
100
101 if (highlight > maxin)
102 {
103 printf("%s error: highlight=%d > maxin=%d\n", argv[0], highlight, maxin);
104 exit(-1);
105 }
106
107 if ((gamma < 0.1) || (gamma > 5))
108 {
109 printf("%s error: gamma=%f out of range [0.1;5]\n", argv[0], gamma);
110 exit(-1);
111 }
112
113 f = (highlight - shadow) / 255.0 + shadow;
114
115 printf("[%d]%d-", 0, 0);
116
117 if (shadow > 0)
118 {
119 printf("[%d]%d-", shadow, 0);
120 }
121
122 while (f < highlight)
123 {
124 in = (int) f;
125 out = maxout * pow((double) (in - shadow)/(highlight-shadow), (1.0/gamma));
126 printf("[%d]%d-", in, out);
127 f *= 1.5;
128 }
129
130 if (f > highlight)
131 {
132 printf("[%d]%d-", highlight, maxout);
133 }
134
135 printf("[%d]%d", maxin, maxout);
136
137 return 0;
138 }
139