-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_util.c
214 lines (161 loc) · 5.25 KB
/
cf_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "config.h"
#include "checkfocus.h"
int debug=0;
int verbose=0;
/* we need to mess around with fixed and floating point math */
Cf_stat adjust(Cf_stat input, int shiftr, Cf_fudge fudge_factor)
{
Cf_stat output;
if (debug)
fprintf(stderr, "adjust given args input=%llu, shift=%d,fudge_factor=%1.3g\n",
input, shiftr, fudge_factor);
output=input; /* unless we change something */
if (shiftr) /* just scale all the numbers down ,, in case other tools can't cope */
output>>=shiftr;
if (debug)
fprintf(stderr, "adjust following shift; output=%llu\n",
output);
if (fudge_factor != 0.0 && fudge_factor != 1.0) /* we lose precision doing floating point, so only get into it if necessary */
output = (Cf_stat) (fudge_factor * (Cf_fudge)output ); /* fixed to float, float multiply, then back to fixed */
if (debug)
fprintf(stderr, "adjust following fudge_factoring; output=%llu\n",
output);
return (output);
}
CF_BOOL inbox(struct box * p, Tcol column, Trow row)
{
return ( row >= p->first_row && row <= p->last_row && column >= p->first_column && column <= p->last_column);
}
CF_BOOL box_defined(struct box *p)
{
return (p->first_row < p->last_row && p->first_column < p->last_column );
}
void copy_box(struct box *to, struct box *from)
{
to->first_row = from->first_row;
to->last_row = from->last_row;
to->first_column = from->first_column;
to->last_column = from->last_column;
return;
}
/* the "meat" of the whole roadshow:
*
* A contrastly(?) image might have values 0, 0, 0,16,16,16,16,16,16
* A blury one 0, 2, 4, 6, 8,10,12,14,16
*
* If we sum the differences "cell to out right" we get (0-0)+(0-0)+(0-16)+(16-16)+(16-16)+(16-16)+(16-16)+(16-16) = -16
* verses (0-2)+(2-4)+(4-6)+(6-8)+(8-10)+(10-12)+(12-14)+(14-16) = -16
* If however we square them before adding, we get -16*-16 = 256 vs -2*-2 =4 (8 times) = 32
*
* So squaring makes the sharp transition much more noticable (and has the added advantage of making it +ve)
*/
Cf_stat diff(unsigned char one, unsigned char other )
{
int x = one;
int y = other; /* we want signed arithmetic) */
Cf_stat result;
result = (Cf_stat)((x-y)*(x-y)); /* result will always be +ve, so we lose nothing ...doing unsigned subtraction would cause big numbers */
return (result);
}
char *colorspace_string(J_COLOR_SPACE space)
{
char *name;
switch(space)
{
case (JCS_UNKNOWN):
name="JCS_UNKNOWN"; /* error/unspecified */
break;
case (JCS_GRAYSCALE):
name="JCS_GRAYSCALE"; /* monochrome */
break;
case (JCS_RGB):
name="JCS_RGB"; /* red/green/blue as specified by the RGB_RED, RGB_GREEN, RGB_BLUE, and RGB_PIXELSIZE macros */
break;
case (JCS_YCbCr):
name="JCS_YCbCr"; /* Y/Cb/Cr (also known as YUV) */
break;
case (JCS_CMYK):
name="JCS_CMYK"; /* C/M/Y/K */
break;
case (JCS_YCCK):
name="JCS_YCCK"; /* Y/Cb/Cr/K */
break;
#if defined(HAVE_DECL_JCS_EXT_RGB) && (HAVE_DECL_JCS_EXT_RGB==1)
case (JCS_EXT_RGB):
name="JCS_EXT_RGB"; /* red/green/blue */
break;
case (JCS_EXT_RGBX):
name="JCS_EXT_RGBX"; /* red/green/blue/x */
break;
case (JCS_EXT_BGR):
name="JCS_EXT_BGR"; /* blue/green/red */
break;
case (JCS_EXT_BGRX):
name="JCS_EXT_BGRX"; /* blue/green/red/x */
break;
case (JCS_EXT_XBGR):
name="JCS_EXT_XBGR"; /* x/blue/green/red */
break;
case (JCS_EXT_XRGB):
name="JCS_EXT_XRGB"; /* x/red/green/blue */
break;
case (JCS_EXT_RGBA):
name="JCS_EXT_RGBA"; /* red/green/blue/alpha */
break;
case (JCS_EXT_BGRA):
name="JCS_EXT_BGRA"; /* blue/green/red/alpha */
break;
case (JCS_EXT_ABGR):
name="JCS_EXT_ABGR"; /* alpha/blue/green/red */
break;
case (JCS_EXT_ARGB):
name="JCS_EXT_ARGB"; /* alpha/red/green/blue */
break;
case (JCS_RGB565): /* 5-bit red/6-bit green/5-bit blue */
name="JCS_RGB565";
break;
#endif
default:
name="unknown";
break;
}
return (name);
};
void dump_3component(unsigned char comp[])
{
fprintf(stderr, "[%d,%d,%d]", comp[0], comp[1], comp[2]);
}
void dump_1component(unsigned char comp[])
{
fprintf(stderr, "[%d]", comp[0]);
}
/* Note output width is in terms of components */
void dump_1row(int output_components, JSAMPARRAY buffer, Tcol output_width)
{
// NB
// typedef JSAMPLE *JSAMPROW; /* ptr to one image row of pixel samples. */
// typedef JSAMPROW *JSAMPARRAY; /* ptr to some rows (a 2-D sample array) */
// typedef JSAMPARRAY *JSAMPIMAGE; /* a 3-D sample array: top index is color */
int i;
unsigned char *p;
p = *buffer;
fprintf(stderr, "row:{");
for (i=0; i<output_width; ++i)
{
if (output_components == 3)
{
dump_3component(p);
p+=3;
}
else if (output_components == 1)
{
dump_1component(p);
p+=1;
}
else
{
fprintf(stderr, "unable to dump components of size=%d", output_components);
}
}
fprintf(stderr, "}\n");
}