File: imageComp.c

package info (click to toggle)
xpaint 2.5.1-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,488 kB
  • ctags: 2,478
  • sloc: ansic: 25,980; makefile: 36; sh: 23
file content (565 lines) | stat: -rw-r--r-- 14,795 bytes parent folder | download | duplicates (4)
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* +-------------------------------------------------------------------+ */
/* | Copyright (C) 1993, David Koblas (koblas@netcom.com)	       | */
/* |								       | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.	 There is no	       | */
/* | representations about the suitability of this software for	       | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.					       | */
/* |								       | */
/* +-------------------------------------------------------------------+ */

/* $Id: imageComp.c,v 1.5 1996/06/02 08:38:35 torsten Exp $ */

/* reduce.c:

 * reduce an image's colormap usage to a set number of colors.	this also
 * translates a true color image to a TLA-style image of `n' colors.
 *
 * this uses an algorithm by Paul Heckbert discussed in `Color Image
 * Quantization for Frame Buffer Display,' _Computer Graphics_ 16(3),
 * pp 297-307.	this implementation is based on one discussed in
 * 'A Few Good Colors,' _Computer Language_, Aug. 1990, pp 32-41 by
 * Dave Pomerantz.
 *
 * this function cannot reduce to any number of colors larger than 32768.
 *
 * jim frost 04.18.91
 *
 */

/*
 * Copyright 1989, 1990, 1991 Jim Frost
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.	 The author makes no representations
 * about the suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 *
 * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
 * USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <X11/Intrinsic.h>
#include <stdio.h>

#ifndef NOSTDHDRS
#include <stdlib.h>
#include <unistd.h>
#endif

/*
**  swiped from X11/Xfuncproto.h
**   since qsort() may or may not be defined with a constant sub-function
 */
#ifndef _Xconst
#if __STDC__ || defined(__cplusplus) || defined(c_plusplus) || (FUNCPROTO&4)
#define _Xconst const
#else
#define _Xconst
#endif
#endif				/* _Xconst */

#include "image.h"
#include "hash.h"
#include "protocol.h"
#include "misc.h"

/*
** This converts an RGB pixel into a 15-bit true color value.
 */

#define TO_15BITS(r,g,b) ((r & 0xf8) << 7)|((g & 0xf8) << 2)|((b & 0xf8) >> 3)

/* 
** These macros extract color intensities, in the compressed range.
 */

#define RED_INTENSITY(P)   (((P) & 0x7c00) >> 10)
#define GREEN_INTENSITY(P) (((P) & 0x03e0) >> 5)
#define BLUE_INTENSITY(P)   ((P) & 0x001f)

/*
** These macros extract the 0..255 range value from the "true color" version
 */
#define RED_VALUE(P)   (((P) & 0x7c00) >> 7)
#define GREEN_VALUE(P) (((P) & 0x03e0) >> 2)
#define BLUE_VALUE(P)  (((P) & 0x001f) << 3)

/*
 * This structure defines a color area which is made up of an array of pixel
 * values and a count of the total number of image pixels represented by
 * the area.  Color areas are kept in a list sorted by the number of image
 * pixels they represent.
 */

typedef struct color_area {
    unsigned short *pixels;	/* array of pixel values in this area */
    unsigned short npixels;	/* size of above array */
    /* predicate func to sort with before * splitting */
    int (*func) (unsigned short *, unsigned short *);
    unsigned long count;	/* # of image pixels we represent */
    struct color_area *prev, *next;
} Area_t;

typedef struct {
    unsigned short idx;
    unsigned char r, g, b;
} color_t;

/* 
** Predicate functions for qsort
**   Generated code as you can see.
 */

#ifndef __STDC__
#define SORT(ca, cb, cc)							\
static int sort/**/ca/**/cb/**/cc(unsigned short *p1, unsigned short *p2)	\
{										\
	unsigned int	R1,R2,G1,G2,B1,B2;					\
	R1 = RED_INTENSITY(*p1);						\
	G1 = GREEN_INTENSITY(*p1);						\
	B1 = BLUE_INTENSITY(*p1);						\
	R2 = RED_INTENSITY(*p2);						\
	G2 = GREEN_INTENSITY(*p2);						\
	B2 = BLUE_INTENSITY(*p2);						\
	if (ca/**/1 == ca/**/2)							\
		if (cb/**/1 == cb/**/2)						\
			return (cc/**/1 < cc/**/2) ? -1 : 1;			\
		else								\
			return (cb/**/1 < cb/**/2) ? -1 : 1;			\
	else									\
		return (ca/**/1 < ca/**/2) ? -1 : 1;				\
	}
#else
#define SORT(ca, cb, cc)							\
    static int sort ## ca ## cb ## cc(unsigned short *p1, unsigned short *p2)	\
	{									\
		unsigned int	R1,R2,G1,G2,B1,B2;				\
		R1 = RED_INTENSITY(*p1);					\
		G1 = GREEN_INTENSITY(*p1);					\
		B1 = BLUE_INTENSITY(*p1);					\
		R2 = RED_INTENSITY(*p2);					\
		G2 = GREEN_INTENSITY(*p2);					\
		B2 = BLUE_INTENSITY(*p2);					\
		if (ca ## 1 == ca ## 2)						\
			if (cb ## 1 == cb ## 2)					\
				return (cc ## 1 < cc ## 2) ? -1 : 1;		\
			else							\
				return (cb ## 1 < cb ## 2) ? -1 : 1;		\
		else								\
			return (ca ## 1 < ca ## 2) ? -1 : 1;			\
	}
#endif

SORT(R, G, B)
SORT(R, B, G)
SORT(G, R, B)
SORT(G, B, R)
SORT(B, G, R)
SORT(B, R, G)
#undef SORT

/*
 * this does calculations on a color area following a split and inserts
 * the color area in the list of color areas.
 */

static void 
insertColorArea(unsigned long *counts,
		Area_t ** rlargest, Area_t ** rsmallest, Area_t * area)
{
    int i;
    unsigned int red, green, blue;
    unsigned int min_red, min_green, min_blue;
    unsigned int max_red, max_green, max_blue = 0;
    Area_t *largest, *smallest;

    /*
    ** update pixel count for this area and find RGB intensity widths
     */
    min_red = max_red = RED_INTENSITY(area->pixels[0]);
    min_green = max_green = GREEN_INTENSITY(area->pixels[0]);
    min_blue = max_blue = BLUE_INTENSITY(area->pixels[0]);

    area->count = 0;
    for (i = 1; i < area->npixels; i++) {
	area->count += counts[area->pixels[i]];
	red = RED_INTENSITY(area->pixels[i]);
	green = GREEN_INTENSITY(area->pixels[i]);
	blue = BLUE_INTENSITY(area->pixels[i]);

	if (red < min_red)
	    min_red = red;
	if (red > max_red)
	    max_red = red;
	if (green < min_green)
	    min_green = green;
	if (green > max_green)
	    max_green = green;
	if (blue < min_blue)
	    min_blue = blue;
	if (blue > max_blue)
	    max_blue = blue;
    }

    /*
    ** calculate widths and determine which predicate function to use based
    ** on the result
     */

    red = max_red - min_red;
    green = max_green - min_green;
    blue = max_blue - min_blue;

    if (red > green)
	if (green > blue)
	    area->func = sortRGB;
	else if (red > blue)
	    area->func = sortRBG;
	else
	    area->func = sortBRG;
    else if (green > blue)
	if (red > blue)
	    area->func = sortGRB;
	else
	    area->func = sortGBR;
    else
	area->func = sortBGR;

    /*
     * insert color area in color area list sorted by number of pixels that
     * the area represents
     */

    largest = *rlargest;
    smallest = *rsmallest;

    if (largest == NULL) {
	largest = smallest = area;
	area->prev = area->next = (Area_t *) NULL;
    } else if (area->npixels < 2) {
	/*
	**  if we only have one element, our pixel count is immaterial so we get
	**  stuck on the end of the list.
	 */
	smallest->next = area;
	area->prev = smallest;
	area->next = (Area_t *) NULL;
	smallest = area;
    } else {
	/*
	**  insert node into list
	 */
	Area_t *cur;

	for (cur = largest; cur != NULL; cur = cur->next) {
	    if ((area->count > cur->count) || (cur->npixels < 2)) {
		area->prev = cur->prev;
		area->next = cur;
		cur->prev = area;
		if (area->prev != NULL)
		    area->prev->next = area;
		else
		    largest = area;
		break;
	    }
	}
	if (cur == NULL) {
	    area->prev = smallest;
	    area->next = (Area_t *) NULL;
	    smallest->next = area;
	    smallest = area;
	}
    }
    *rlargest = largest;
    *rsmallest = smallest;
}

/*
**  hash table support functions.
 */
static int 
cmpColor(void *a, void *b)
{
    color_t *ca = (color_t *) a;
    color_t *cb = (color_t *) b;

    if (ca->r != cb->r)
	return ca->r < cb->r ? -1 : 1;
    if (ca->g != cb->g)
	return ca->g < cb->g ? -1 : 1;
    if (ca->b != cb->b)
	return ca->b < cb->b ? -1 : 1;
    return 0;
}

static void 
freeColor(void *junk)
{
    /*
    **	Don't apply a free function to the hashtable items.
     */
}

Image *
ImageCompress(Image * input, int ncolors, int noforce)
{
    unsigned long counts[32768];
    unsigned short array[XtNumber(counts)];
    unsigned char *ocp;
    unsigned short *osp;
    int x, y, i, count, nuniq;
    int allocated;
    Area_t *areas, *largest, *smallest;
    Image *output;
    void *htable;
    color_t *ctable;

    /*
    **	make sure we have array space...
     */
    if (ncolors > XtNumber(counts))
	ncolors = XtNumber(counts);

    /*
    **	Why are we trying to compress a b&w image?
    **	  or an image that already fits
     */
    if (input->isBW)
	return input;
    if (input->cmapSize != 0 && input->cmapSize < ncolors)
	return input;

    /*
    **	Create a histogram of 15-bit color values.
    **	  also, save the "real" color values.
    **	  or at least enough of them so that we know
    **	  that compression is really necessary.
     */
    htable = HashCreate(cmpColor, freeColor, 256);
    ctable = (color_t *) XtCalloc(sizeof(color_t), ncolors + 1);
    nuniq = 0;

    /* GRR:  use memset() instead? */
    for (i = 0; i < XtNumber(counts); i++)
	counts[i] = 0;

    for (y = 0; y < input->height; y++) {
	for (x = 0; x < input->width; x++) {
	    unsigned char *dp;

	    dp = ImagePixel(input, x, y);

	    if (nuniq <= ncolors && htable != NULL) {
		color_t *cptr = &ctable[nuniq];
		cptr->r = dp[0];
		cptr->g = dp[1];
		cptr->b = dp[2];
		if (HashFind(htable, dp[0], cptr) == NULL) {
		    cptr->idx = nuniq;
		    HashAdd(htable, dp[0], cptr);
		    nuniq++;
		}
	    }
	    counts[TO_15BITS(dp[0], dp[1], dp[2])]++;
	}

	if (y % 256 == 0)
	    StateTimeStep();
    }

    if (nuniq <= ncolors) {
	/*
	**  Wow, this has few enough colors to fit in the requested colormap.
	**    This should be able to renumber (compress) an existing
	**    colormap, rather than creating a new image.
	 */
	unsigned short *osp;
	unsigned char *ocp;

	output = ImageNewCmap(input->width, input->height, nuniq);
	for (y = 0; y < nuniq; y++)
	    ImageSetCmap(output, y,
			 ctable[y].r, ctable[y].g, ctable[y].b);

	osp = (unsigned short *) output->data;
	ocp = output->data;

	for (y = 0; y < input->height; y++) {
	    for (x = 0; x < input->width; x++, osp++, ocp++) {
		unsigned char *dp;
		color_t *cptr, col;

		dp = ImagePixel(input, x, y);

		col.r = dp[0];
		col.g = dp[1];
		col.b = dp[2];
		cptr = HashFind(htable, dp[0], &col);

		if (nuniq > 256)
		    *osp = cptr->idx;
		else
		    *ocp = cptr->idx;
	    }

	    if (y % 256 == 0)
		StateTimeStep();
	}

	output->cmapPacked = True;

	HashDestroy(htable);
	XtFree((XtPointer) ctable);
	output->maskData = input->maskData;
	input->maskData = NULL;
	ImageDelete(input);
	return output;
    }
    HashDestroy(htable);
    XtFree((XtPointer) ctable);

    /* GRR 960525:  if not forcing compression to ncolors, return now */
    if (noforce)
        return NULL;

    /*
    **	Create an array of 15-bit pixel values that actually occur 
    **	 in the image.
     */
    count = 0;
    for (i = 0; i < XtNumber(counts); i++) {
	if (counts[i] != 0)
	    array[count++] = i;
    }

    /*
    **	Create the color areas and initialize the first element
     */
    areas = (Area_t *) XtCalloc(sizeof(Area_t), ncolors);
    areas[0].pixels = array;
    areas[0].npixels = count;

    largest = smallest = NULL;
    insertColorArea(counts, &largest, &smallest, areas);

    allocated = 1;

    /*
    **	While there are areas still to be broken down 
    **	  or the largest cannot be subdivided.
     */
    while (allocated < ncolors && largest->npixels >= 2) {
	Area_t *newArea, *oldArea;
	int i, midpoint;

	qsort(largest->pixels, largest->npixels, sizeof(short),
	       (int (*)(_Xconst void *, _Xconst void *)) largest->func);

	/*
	**  Find the midpoint of the largest area an split
	 */
	midpoint = largest->count / 2;
	for (i = 0, count = 0; i < largest->npixels && count < midpoint; i++)
	    count += counts[largest->pixels[i]];

	if (i == largest->npixels)
	    i--;
	if (i == 0)
	    i = 1;
	newArea = areas + allocated;
	newArea->pixels = largest->pixels + i;
	newArea->npixels = largest->npixels - i;
	largest->npixels = i;
	oldArea = largest;
	largest = largest->next;
	if (largest != NULL)
	    largest->prev = NULL;
	else
	    smallest = NULL;

	/* 
	**  recalculate for each area of split and insert in the area list
	 */
	insertColorArea(counts, &largest, &smallest, newArea);
	insertColorArea(counts, &largest, &smallest, oldArea);

	allocated++;

	if (allocated % 64 == 0)
	    StateTimeStep();
    }

    output = ImageNewCmap(input->width, input->height, allocated);
    output->maskData = input->maskData;
    input->maskData = NULL;

    for (i = 0; i < allocated; i++) {
	long red, green, blue;
	int j;

	/* 
	** Calculate RGB table from each color area.  This should really calculate
	** a new color by weighting the intensities by the number of pixels, but
	** it's a pain to scale so this just averages all the intensities.  It
	** works pretty well regardless.
	 */
	red = green = blue = 0;
	for (j = 0; j < areas[i].npixels; j++) {
	    unsigned short pixel = areas[i].pixels[j];

	    red += RED_VALUE(pixel) & 0xff;
	    green += GREEN_VALUE(pixel) & 0xff;
	    blue += BLUE_VALUE(pixel) & 0xff;

	    counts[pixel] = i;
	}
	red /= areas[i].npixels;
	green /= areas[i].npixels;
	blue /= areas[i].npixels;

	ImageSetCmap(output, i, red, green, blue);
    }

    /*
    **	Done with the areas information.
     */
    XtFree((XtPointer) areas);

    /*
    **	Copy input to output
     */

    ocp = output->data;
    osp = (unsigned short *) output->data;

    for (y = 0; y < input->height; y++) {
	for (x = 0; x < input->width; x++) {
	    unsigned char *dp;

	    dp = ImagePixel(input, x, y);

	    if (output->cmapSize > 256) {
		*osp++ = counts[TO_15BITS(dp[0], dp[1], dp[2])];
	    } else {
		*ocp++ = counts[TO_15BITS(dp[0], dp[1], dp[2])];
	    }
	}
    }

    output->cmapPacked = True;
    ImageDelete(input);
    return output;
}