forked from alandipert/ncsa-mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
medcut.c
510 lines (466 loc) · 12.4 KB
/
medcut.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/****************************************************************************
* NCSA Mosaic for the X Window System *
* Software Development Group *
* National Center for Supercomputing Applications *
* University of Illinois at Urbana-Champaign *
* 605 E. Springfield, Champaign IL 61820 *
* *
* Copyright (C) 1993, Board of Trustees of the University of Illinois *
* *
* NCSA Mosaic software, both binary and source (hereafter, Software) is *
* copyrighted by The Board of Trustees of the University of Illinois *
* (UI), and ownership remains with the UI. *
* *
* The UI grants you (hereafter, Licensee) a license to use the Software *
* for academic, research and internal business purposes only, without a *
* fee. Licensee may distribute the binary and source code (if released) *
* to third parties provided that the copyright notice and this statement *
* appears on all copies and that no charge is associated with such *
* copies. *
* *
* Licensee may make derivative works. However, if Licensee distributes *
* any derivative work based on or derived from the Software, then *
* Licensee will (1) notify NCSA regarding its distribution of the *
* derivative work, and (2) clearly notify users that such derivative *
* work is a modified version and not the original NCSA Mosaic *
* distributed by the UI. *
* *
* Any Licensee wishing to make commercial use of the Software should *
* contact the UI, c/o NCSA, to negotiate an appropriate license for such *
* commercial use. Commercial use includes (1) integration of all or *
* part of the source code into a product for sale or license by or on *
* behalf of Licensee to third parties, or (2) distribution of the binary *
* code or source code to third parties that need it to utilize a *
* commercial product sold or licensed by or on behalf of Licensee. *
* *
* UI MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR *
* ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED *
* WARRANTY. THE UI SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY THE *
* USERS OF THIS SOFTWARE. *
* *
* By using or copying this Software, Licensee agrees to abide by the *
* copyright law and all other applicable laws of the U.S. including, but *
* not limited to, export control laws, and the terms of this license. *
* UI shall have the right to terminate this license immediately by *
* written notice upon Licensee's breach of, or non-compliance with, any *
* of its terms. Licensee may be held legally responsible for any *
* copyright infringement that is caused or encouraged by Licensee's *
* failure to abide by the terms of this license. *
* *
* Comments and questions are welcome and can be sent to *
* [email protected]. *
****************************************************************************/
#include "../config.h"
#include <X11/Intrinsic.h>
#include <stdio.h>
#define RED 0
#define GREEN 1
#define BLUE 2
#ifndef DISABLE_TRACE
extern int srcTrace;
#endif
#if 0
#define FindHash(red, green, blue, h_ptr) \
h_ptr = Hash[((red * 299) + (green * 587) + (blue * 114)) / 1000 * NCells / 65536]; \
while(h_ptr != NULL) \
{ \
if ((h_ptr->pixel[RED] == red)&& \
(h_ptr->pixel[GREEN] == green)&& \
(h_ptr->pixel[BLUE] == blue)) \
{ \
break; \
} \
h_ptr = h_ptr->hash_next; \
}
#endif
#define FindHash(red, green, blue, h_ptr) \
h_ptr = Hash[((((red * 306) + (green * 601) + (blue * 117)) >> 10) * NCells) >> 16]; \
while(h_ptr != NULL) \
{ \
if ((h_ptr->pixel[RED] == red)&& \
(h_ptr->pixel[GREEN] == green)&& \
(h_ptr->pixel[BLUE] == blue)) \
{ \
break; \
} \
h_ptr = h_ptr->hash_next; \
}
static struct color_rec {
int pixel[3];
int box_num;
struct color_rec *hash_next;
struct color_rec *next;
} *Hash[256];
static struct c_box_rec {
int min_pix[3];
int max_pix[3];
int count;
struct color_rec *c_data;
} C_boxes[256];
static int BoxCount;
static struct color_rec *hash_ptr;
static struct color_rec *free_hash = (struct color_rec *)NULL;
static struct color_rec *tptr;
static int Width, Height;
static int ColorCnt;
static int NCells;
void InitMinMax(boxnum)
int boxnum;
{
C_boxes[boxnum].min_pix[RED] = 65536;
C_boxes[boxnum].max_pix[RED] = 0;
C_boxes[boxnum].min_pix[GREEN] = 65536;
C_boxes[boxnum].max_pix[GREEN] = 0;
C_boxes[boxnum].min_pix[BLUE] = 65536;
C_boxes[boxnum].max_pix[BLUE] = 0;
}
struct color_rec *AddHash(red, green, blue)
int red, green, blue;
{
int lum;
#if 0
lum = ((red * 299) + (green * 587) + (blue * 114)) / 1000 * NCells / 65536;;
#endif
lum = ((((red * 306) + (green * 601) + (blue * 117)) >> 10) * NCells) >> 16;;
if (free_hash != NULL)
{
hash_ptr = free_hash;
free_hash = free_hash->hash_next;
}
else
{
hash_ptr = (struct color_rec *)
XtMalloc(sizeof(struct color_rec));
}
if (hash_ptr == NULL)
{
fprintf(stderr, "Cannot malloc %dth color\n", ColorCnt);
exit(1);
}
hash_ptr->pixel[RED] = red;
hash_ptr->pixel[GREEN] = green;
hash_ptr->pixel[BLUE] = blue;
hash_ptr->box_num = 0;
hash_ptr->next = NULL;
hash_ptr->hash_next = Hash[lum];
Hash[lum] = hash_ptr;
return(hash_ptr);
}
void AddColor(cptr, boxnum)
struct color_rec *cptr;
int boxnum;
{
struct color_rec *ptr;
while (cptr != NULL)
{
ptr = cptr;
cptr = cptr->next;
ptr->box_num = boxnum;
ptr->next = C_boxes[boxnum].c_data;
C_boxes[boxnum].c_data = ptr;
if (ptr->pixel[RED] < C_boxes[boxnum].min_pix[RED])
C_boxes[boxnum].min_pix[RED] = ptr->pixel[RED];
if (ptr->pixel[RED] > C_boxes[boxnum].max_pix[RED])
C_boxes[boxnum].max_pix[RED] = ptr->pixel[RED];
if (ptr->pixel[GREEN] < C_boxes[boxnum].min_pix[GREEN])
C_boxes[boxnum].min_pix[GREEN] = ptr->pixel[GREEN];
if (ptr->pixel[GREEN] > C_boxes[boxnum].max_pix[GREEN])
C_boxes[boxnum].max_pix[GREEN] = ptr->pixel[GREEN];
if (ptr->pixel[BLUE] < C_boxes[boxnum].min_pix[BLUE])
C_boxes[boxnum].min_pix[BLUE] = ptr->pixel[BLUE];
if (ptr->pixel[BLUE] > C_boxes[boxnum].max_pix[BLUE])
C_boxes[boxnum].max_pix[BLUE] = ptr->pixel[BLUE];
}
}
void CountColors(data, colrs, color_used)
unsigned char *data;
XColor *colrs;
int *color_used;
{
unsigned char *dptr;
register int i;
int red, green, blue;
register struct color_rec *tptr;
InitMinMax(0);
C_boxes[0].c_data = NULL;
tptr = C_boxes[0].c_data;
ColorCnt = 0;
for (i=0; i<256; i++)
{
color_used[i] = 0;
}
dptr = data;
for (i=(Width * Height); i>0; i--)
{
color_used[(int)(*dptr)] = 1;
dptr++;
}
for (i=0; i<256; i++)
{
if (!color_used[i])
{
continue;
}
red = colrs[i].red;
green = colrs[i].green;
blue = colrs[i].blue;
FindHash(red, green, blue, tptr);
if (tptr == NULL)
{
tptr = AddHash(red, green, blue);
tptr->next = NULL;
AddColor(tptr, 0);
ColorCnt++;
}
}
}
int FindTarget(tptr)
int *tptr;
{
int range, i, indx;
range = 0;
for (i=0; i<BoxCount; i++)
{
int rr, gr, br;
rr = C_boxes[i].max_pix[RED] - C_boxes[i].min_pix[RED];
gr = C_boxes[i].max_pix[GREEN] - C_boxes[i].min_pix[GREEN];
br = C_boxes[i].max_pix[BLUE] - C_boxes[i].min_pix[BLUE];
if (rr > range)
{
range = rr;
*tptr = i;
indx = RED;
}
if (gr > range)
{
range = gr;
*tptr = i;
indx = GREEN;
}
if (br > range)
{
range = br;
*tptr = i;
indx = BLUE;
}
}
return(indx);
}
void SplitBox(boxnum, color_indx)
int boxnum, color_indx;
{
struct color_rec *low, *high;
struct color_rec *data;
int med_cnt, split_val;
int low_cnt, high_cnt;
int Low_cnt, High_cnt;
int Greater, Lesser;
Greater = BoxCount++;
Lesser = boxnum;
InitMinMax(Lesser);
InitMinMax(Greater);
data = C_boxes[boxnum].c_data;
med_cnt = C_boxes[boxnum].count / 2;
C_boxes[Lesser].c_data = NULL;
C_boxes[Greater].c_data = NULL;
Low_cnt = 0;
High_cnt = 0;
while(med_cnt > 0)
{
if (data->pixel[color_indx] < data->next->pixel[color_indx])
{
low = data;
high = data->next;
data = high->next;
}
else
{
high = data;
low = data->next;
data = low->next;
}
low->next = NULL;
high->next = NULL;
low_cnt = 1;
high_cnt = 1;
split_val = low->pixel[color_indx];
while(data != NULL)
{
tptr = data;
data = data->next;
if (tptr->pixel[color_indx] > split_val)
{
tptr->next = high;
high = tptr;
high_cnt++;
}
else
{
tptr->next = low;
low = tptr;
low_cnt++;
}
} /* end while data->next != NULL */
if (low_cnt <= med_cnt)
{
AddColor(low, Lesser);
Low_cnt += low_cnt;
med_cnt -= low_cnt;
if (med_cnt == 0)
{
AddColor(high, Greater);
High_cnt += high_cnt;
}
data = high;
}
else
{
AddColor(high, Greater);
High_cnt += high_cnt;
data = low;
}
} /* end while med_cnt */
C_boxes[Lesser].count = Low_cnt;
C_boxes[Greater].count = High_cnt;
}
void SplitColors(e_cnt)
int e_cnt;
{
if (ColorCnt < e_cnt)
{
int i;
tptr = C_boxes[0].c_data;
for (i=0; i<ColorCnt; i++)
{
hash_ptr = tptr;
tptr = tptr->next;
C_boxes[i].c_data = hash_ptr;
C_boxes[i].count = 1;
hash_ptr->box_num = i;
hash_ptr->next = NULL;
}
BoxCount = ColorCnt;
}
else
{
BoxCount = 1;
while (BoxCount < e_cnt)
{
int target, color_indx;
target = 0;
color_indx = 0;
color_indx = FindTarget(&target);
SplitBox(target, color_indx);
}
}
}
void ConvertData(data, colrs, colors_used)
unsigned char *data;
XColor *colrs;
int *colors_used;
{
unsigned char *dptr;
register int i/*, j*/;
int red, green, blue;
register struct color_rec *hash_ptr;
int pixel_map[256];
/*
* Generate translation map.
*/
for (i=0; i<256; i++)
{
if (!colors_used[i])
{
continue;
}
red = colrs[i].red;
green = colrs[i].green;
blue = colrs[i].blue;
FindHash(red, green, blue, hash_ptr);
if (hash_ptr == NULL)
{
#ifndef DISABLE_TRACE
if (srcTrace) {
fprintf(stderr, "Unknown color (%d,%d,%d)\n",
red, green, blue);
}
#endif
hash_ptr = Hash[0];
}
pixel_map[i] = hash_ptr->box_num;
}
dptr = data;
for (i=(Width*Height); i>0; i--)
{
*dptr = (unsigned char)pixel_map[(int)*dptr];
dptr++;
}
}
void PrintColormap(e_cnt, colrs)
int e_cnt;
XColor *colrs;
{
int i;
for(i=0; i<BoxCount; i++)
{
int Tred, Tgreen, Tblue;
int c_cnt;
c_cnt = 0;
Tred = Tgreen = Tblue = 0;
tptr = C_boxes[i].c_data;
while (tptr != NULL)
{
Tred += tptr->pixel[RED];
Tgreen += tptr->pixel[GREEN];
Tblue += tptr->pixel[BLUE];
c_cnt++;
tptr = tptr->next;
}
colrs[i].red = Tred / c_cnt;
colrs[i].green = Tgreen / c_cnt;
colrs[i].blue = Tblue / c_cnt;
}
for(i=BoxCount; i<e_cnt; i++)
{
colrs[i].red = 0;
colrs[i].green = 0;
colrs[i].blue = 0;
}
}
void MedianCut(data, w, h, colrs, start_cnt, end_cnt)
unsigned char *data;
int *w, *h;
XColor *colrs;
int start_cnt, end_cnt;
{
int i;
int colors_used[256];
Width = *w;
Height = *h;
NCells = start_cnt;
BoxCount = 0;
ColorCnt = 0;
for (i=0; i<256; i++)
{
Hash[i] = NULL;
C_boxes[i].c_data = NULL;
C_boxes[i].count = 0;
}
CountColors(data, colrs, colors_used);
C_boxes[0].count = ColorCnt;
SplitColors(end_cnt);
ConvertData(data, colrs, colors_used);
PrintColormap(end_cnt, colrs);
for (i=0; i<256; i++)
{
hash_ptr = Hash[i];
while (hash_ptr != NULL)
{
tptr = hash_ptr;
hash_ptr = hash_ptr->hash_next;
tptr->hash_next = free_hash;
free_hash = tptr;
}
}
}