1 /* NeuQuant Neural-Net Quantization Algorithm
2  * ------------------------------------------
3  *
4  * Copyright (c) 1994 Anthony Dekker
5  *
6  * NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994.
7  * See "Kohonen neural networks for optimal colour quantization"
8  * in "Network: Computation in Neural Systems" Vol. 5 (1994) pp 351-367.
9  * for a discussion of the algorithm.
10  * See also  http://members.ozemail.com.au/~dekker/NEUQUANT.HTML
11  *
12  * Any party obtaining a copy of these files from the author, directly or
13  * indirectly, is granted, free of charge, a full and unrestricted irrevocable,
14  * world-wide, paid up, royalty-free, nonexclusive right and license to deal
15  * in this software and documentation files (the "Software"), including without
16  * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons who receive
18  * copies from any such party to do so, with the only requirement being
19  * that this copyright notice remain intact.
20  *
21  *
22  * Modified to process 32bit RGBA images.
23  * Stuart Coyle 2004-2007
24  * From: http://pngnq.sourceforge.net/
25  *
26  * Ported to libgd by Pierre A. Joye
27  * (and make it thread safety by droping static and  global variables)
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif /* HAVE_CONFIG_H */
33 
34 #include <stdlib.h>
35 #include <string.h>
36 #include "gd.h"
37 #include "gdhelpers.h"
38 #include "gd_errors.h"
39 
40 #include "gd_nnquant.h"
41 
42 /* Network Definitions
43    ------------------- */
44 
45 #define maxnetpos	(MAXNETSIZE-1)
46 #define netbiasshift	4			/* bias for colour values */
47 #define ncycles		100			/* no. of learning cycles */
48 
49 /* defs for freq and bias */
50 #define intbiasshift    16			/* bias for fractions */
51 #define intbias		(((int) 1)<<intbiasshift)
52 #define gammashift  	10			/* gamma = 1024 */
53 #define gamma   	(((int) 1)<<gammashift)
54 #define betashift  	10
55 #define beta		(intbias>>betashift)	/* beta = 1/1024 */
56 #define betagamma	(intbias<<(gammashift-betashift))
57 
58 /* defs for decreasing radius factor */
59 #define initrad		(MAXNETSIZE>>3)		/* for 256 cols, radius starts */
60 #define radiusbiasshift	6			/* at 32.0 biased by 6 bits */
61 #define radiusbias	(((int) 1)<<radiusbiasshift)
62 #define initradius	(initrad*radiusbias)	/* and decreases by a */
63 #define radiusdec	30			/* factor of 1/30 each cycle */
64 
65 /* defs for decreasing alpha factor */
66 #define alphabiasshift	10			/* alpha starts at 1.0 */
67 #define initalpha	(((int) 1)<<alphabiasshift)
68 
69 /* radbias and alpharadbias used for radpower calculation */
70 #define radbiasshift	8
71 #define radbias		(((int) 1)<<radbiasshift)
72 #define alpharadbshift  (alphabiasshift+radbiasshift)
73 #define alpharadbias    (((int) 1)<<alpharadbshift)
74 
75 #define ALPHA 0
76 #define RED 1
77 #define BLUE 2
78 #define GREEN 3
79 
80 typedef int nq_pixel[5];
81 
82 typedef struct {
83 	/* biased by 10 bits */
84 	int alphadec;
85 
86 	/* lengthcount = H*W*3 */
87 	int lengthcount;
88 
89 	/* sampling factor 1..30 */
90 	int samplefac;
91 
92 	/* Number of colours to use. Made a global instead of #define */
93 	int netsize;
94 
95 	/* for network lookup - really 256 */
96 	int netindex[256];
97 
98 	/* ABGRc */
99 	/* the network itself */
100 	nq_pixel network[MAXNETSIZE];
101 
102 	/* bias and freq arrays for learning */
103 	int bias[MAXNETSIZE];
104 	int freq[MAXNETSIZE];
105 
106 	/* radpower for precomputation */
107 	int radpower[initrad];
108 
109 	/* the input image itself */
110 	unsigned char *thepicture;
111 } nn_quant;
112 
113 /* Initialise network in range (0,0,0,0) to (255,255,255,255) and set parameters
114    ----------------------------------------------------------------------- */
initnet(nnq,thepic,len,sample,colours)115 static void initnet(nnq, thepic, len, sample, colours)
116 nn_quant *nnq;
117 unsigned char *thepic;
118 int len;
119 int sample;
120 int colours;
121 {
122 	register int i;
123 	register int *p;
124 
125 	/* Clear out network from previous runs */
126 	/* thanks to Chen Bin for this fix */
127 	memset((void*)nnq->network, 0, sizeof(nq_pixel)*MAXNETSIZE);
128 
129 	nnq->thepicture = thepic;
130 	nnq->lengthcount = len;
131 	nnq->samplefac = sample;
132 	nnq->netsize = colours;
133 
134 	for (i=0; i < nnq->netsize; i++) {
135 		p = nnq->network[i];
136 		p[0] = p[1] = p[2] = p[3] = (i << (netbiasshift+8)) / nnq->netsize;
137 		nnq->freq[i] = intbias / nnq->netsize;	/* 1/netsize */
138 		nnq->bias[i] = 0;
139 	}
140 }
141 
142 /* -------------------------- */
143 
144 /* Unbias network to give byte values 0..255 and record
145  * position i to prepare for sort
146  */
147 /* -------------------------- */
148 
unbiasnet(nn_quant * nnq)149 static void unbiasnet(nn_quant *nnq)
150 {
151 	int i,j,temp;
152 
153 	for (i=0; i < nnq->netsize; i++) {
154 		for (j=0; j<4; j++) {
155 			/* OLD CODE: network[i][j] >>= netbiasshift; */
156 			/* Fix based on bug report by Juergen Weigert jw@suse.de */
157 			temp = (nnq->network[i][j] + (1 << (netbiasshift - 1))) >> netbiasshift;
158 			if (temp > 255) temp = 255;
159 			nnq->network[i][j] = temp;
160 		}
161 		nnq->network[i][4] = i;			/* record colour no */
162 	}
163 }
164 
165 /* Output colormap to unsigned char ptr in RGBA format */
getcolormap(nnq,map)166 static void getcolormap(nnq, map)
167 nn_quant *nnq;
168 unsigned char *map;
169 {
170 	int i,j;
171 	for(j=0; j < nnq->netsize; j++) {
172 		for (i=3; i>=0; i--) {
173 			*map = nnq->network[j][i];
174 			map++;
175 		}
176 	}
177 }
178 
179 /* Insertion sort of network and building of netindex[0..255] (to do after unbias)
180    ------------------------------------------------------------------------------- */
inxbuild(nn_quant * nnq)181 static void inxbuild(nn_quant *nnq)
182 {
183 	register int i,j,smallpos,smallval;
184 	register int *p,*q;
185 	int previouscol,startpos;
186 
187 	previouscol = 0;
188 	startpos = 0;
189 	for (i=0; i < nnq->netsize; i++) {
190 		p = nnq->network[i];
191 		smallpos = i;
192 		smallval = p[2];			/* index on g */
193 		/* find smallest in i..netsize-1 */
194 		for (j=i+1; j < nnq->netsize; j++) {
195 			q = nnq->network[j];
196 			if (q[2] < smallval) {		/* index on g */
197 				smallpos = j;
198 				smallval = q[2];	/* index on g */
199 			}
200 		}
201 		q = nnq->network[smallpos];
202 		/* swap p (i) and q (smallpos) entries */
203 		if (i != smallpos) {
204 			j = q[0];
205 			q[0] = p[0];
206 			p[0] = j;
207 			j = q[1];
208 			q[1] = p[1];
209 			p[1] = j;
210 			j = q[2];
211 			q[2] = p[2];
212 			p[2] = j;
213 			j = q[3];
214 			q[3] = p[3];
215 			p[3] = j;
216 			j = q[4];
217 			q[4] = p[4];
218 			p[4] = j;
219 		}
220 		/* smallval entry is now in position i */
221 		if (smallval != previouscol) {
222 			nnq->netindex[previouscol] = (startpos+i)>>1;
223 			for (j=previouscol+1; j<smallval; j++) nnq->netindex[j] = i;
224 			previouscol = smallval;
225 			startpos = i;
226 		}
227 	}
228 	nnq->netindex[previouscol] = (startpos+maxnetpos)>>1;
229 	for (j=previouscol+1; j<256; j++) nnq->netindex[j] = maxnetpos; /* really 256 */
230 }
231 
232 
233 /* Search for ABGR values 0..255 (after net is unbiased) and return colour index
234 	 ---------------------------------------------------------------------------- */
inxsearch(nnq,al,b,g,r)235 static unsigned int inxsearch(nnq, al,b,g,r)
236 nn_quant *nnq;
237 register int al, b, g, r;
238 {
239 	register int i, j, dist, a, bestd;
240 	register int *p;
241 	unsigned int best;
242 
243 	bestd = 1000;		/* biggest possible dist is 256*3 */
244 	best = 0;
245 	i = nnq->netindex[g];	/* index on g */
246 	j = i-1;		/* start at netindex[g] and work outwards */
247 
248 	while ((i<nnq->netsize) || (j>=0)) {
249 		if (i< nnq->netsize) {
250 			p = nnq->network[i];
251 			dist = p[2] - g;		/* inx key */
252 			if (dist >= bestd) i = nnq->netsize;	/* stop iter */
253 			else {
254 				i++;
255 				if (dist<0) dist = -dist;
256 				a = p[1] - b;
257 				if (a<0) a = -a;
258 				dist += a;
259 				if (dist<bestd) {
260 					a = p[3] - r;
261 					if (a<0) a = -a;
262 					dist += a;
263 				}
264 				if(dist<bestd) {
265 					a = p[0] - al;
266 					if (a<0) a = -a;
267 					dist += a;
268 				}
269 				if (dist<bestd) {
270 					bestd=dist;
271 					best=p[4];
272 				}
273 			}
274 		}
275 
276 		if (j>=0) {
277 			p = nnq->network[j];
278 			dist = g - p[2]; /* inx key - reverse dif */
279 			if (dist >= bestd) j = -1; /* stop iter */
280 			else {
281 				j--;
282 				if (dist<0) dist = -dist;
283 				a = p[1] - b;
284 				if (a<0) a = -a;
285 				dist += a;
286 				if (dist<bestd) {
287 					a = p[3] - r;
288 					if (a<0) a = -a;
289 					dist += a;
290 				}
291 				if(dist<bestd) {
292 					a = p[0] - al;
293 					if (a<0) a = -a;
294 					dist += a;
295 				}
296 				if (dist<bestd) {
297 					bestd=dist;
298 					best=p[4];
299 				}
300 			}
301 		}
302 	}
303 
304 	return(best);
305 }
306 
307 /* Search for biased ABGR values
308    ---------------------------- */
contest(nnq,al,b,g,r)309 static int contest(nnq, al,b,g,r)
310 nn_quant *nnq;
311 register int al,b,g,r;
312 {
313 	/* finds closest neuron (min dist) and updates freq */
314 	/* finds best neuron (min dist-bias) and returns position */
315 	/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */
316 	/* bias[i] = gamma*((1/netsize)-freq[i]) */
317 
318 	register int i,dist,a,biasdist,betafreq;
319 	unsigned int bestpos,bestbiaspos;
320 	double bestd,bestbiasd;
321 	register int *p,*f, *n;
322 
323 	bestd = ~(((int) 1)<<31);
324 	bestbiasd = bestd;
325 	bestpos = 0;
326 	bestbiaspos = bestpos;
327 	p = nnq->bias;
328 	f = nnq->freq;
329 
330 	for (i=0; i< nnq->netsize; i++) {
331 		n = nnq->network[i];
332 		dist = n[0] - al;
333 		if (dist<0) dist = -dist;
334 		a = n[1] - b;
335 		if (a<0) a = -a;
336 		dist += a;
337 		a = n[2] - g;
338 		if (a<0) a = -a;
339 		dist += a;
340 		a = n[3] - r;
341 		if (a<0) a = -a;
342 		dist += a;
343 		if (dist<bestd) {
344 			bestd=dist;
345 			bestpos=i;
346 		}
347 		biasdist = dist - ((*p)>>(intbiasshift-netbiasshift));
348 		if (biasdist<bestbiasd) {
349 			bestbiasd=biasdist;
350 			bestbiaspos=i;
351 		}
352 		betafreq = (*f >> betashift);
353 		*f++ -= betafreq;
354 		*p++ += (betafreq<<gammashift);
355 	}
356 	nnq->freq[bestpos] += beta;
357 	nnq->bias[bestpos] -= betagamma;
358 	return(bestbiaspos);
359 }
360 
361 
362 /* Move neuron i towards biased (a,b,g,r) by factor alpha
363 	 ---------------------------------------------------- */
364 
altersingle(nnq,alpha,i,al,b,g,r)365 static void altersingle(nnq, alpha,i,al,b,g,r)
366 nn_quant *nnq;
367 register int alpha,i,al,b,g,r;
368 {
369 	register int *n;
370 
371 	n = nnq->network[i];	/* alter hit neuron */
372 	*n -= (alpha*(*n - al)) / initalpha;
373 	n++;
374 	*n -= (alpha*(*n - b)) / initalpha;
375 	n++;
376 	*n -= (alpha*(*n - g)) / initalpha;
377 	n++;
378 	*n -= (alpha*(*n - r)) / initalpha;
379 }
380 
381 
382 /* Move adjacent neurons by precomputed alpha*(1-((i-j)^2/[r]^2)) in radpower[|i-j|]
383 	 --------------------------------------------------------------------------------- */
384 
alterneigh(nnq,rad,i,al,b,g,r)385 static void alterneigh(nnq, rad,i,al,b,g,r)
386 nn_quant *nnq;
387 int rad,i;
388 register int al,b,g,r;
389 {
390 	register int j,k,lo,hi,a;
391 	register int *p, *q;
392 
393 	lo = i-rad;
394 	if (lo<-1) lo=-1;
395 	hi = i+rad;
396 	if (hi>nnq->netsize) hi=nnq->netsize;
397 
398 	j = i+1;
399 	k = i-1;
400 	q = nnq->radpower;
401 	while ((j<hi) || (k>lo)) {
402 		a = (*(++q));
403 		if (j<hi) {
404 			p = nnq->network[j];
405 			*p -= (a*(*p - al)) / alpharadbias;
406 			p++;
407 			*p -= (a*(*p - b)) / alpharadbias;
408 			p++;
409 			*p -= (a*(*p - g)) / alpharadbias;
410 			p++;
411 			*p -= (a*(*p - r)) / alpharadbias;
412 			j++;
413 		}
414 		if (k>lo) {
415 			p = nnq->network[k];
416 			*p -= (a*(*p - al)) / alpharadbias;
417 			p++;
418 			*p -= (a*(*p - b)) / alpharadbias;
419 			p++;
420 			*p -= (a*(*p - g)) / alpharadbias;
421 			p++;
422 			*p -= (a*(*p - r)) / alpharadbias;
423 			k--;
424 		}
425 	}
426 }
427 
428 
429 /* Main Learning Loop
430    ------------------ */
431 
learn(nnq,verbose)432 static void learn(nnq, verbose) /* Stu: N.B. added parameter so that main() could control verbosity. */
433 nn_quant *nnq;
434 int verbose;
435 {
436 	register int i,j,al,b,g,r;
437 	int radius,rad,alpha,step,delta,samplepixels;
438 	register unsigned char *p;
439 	unsigned char *lim;
440 
441 	nnq->alphadec = 30 + ((nnq->samplefac-1)/3);
442 	p = nnq->thepicture;
443 	lim = nnq->thepicture + nnq->lengthcount;
444 	samplepixels = nnq->lengthcount/(4 * nnq->samplefac);
445 	/* here's a problem with small images: samplepixels < ncycles => delta = 0 */
446 	delta = samplepixels/ncycles;
447 	/* kludge to fix */
448 	if(delta==0) delta = 1;
449 	alpha = initalpha;
450 	radius = initradius;
451 
452 	rad = radius >> radiusbiasshift;
453 
454 	for (i=0; i<rad; i++)
455 		nnq->radpower[i] = alpha*(((rad*rad - i*i)*radbias)/(rad*rad));
456 
457 	if (verbose) gd_error_ex(GD_NOTICE, "beginning 1D learning: initial radius=%d\n", rad);
458 
459 	if ((nnq->lengthcount%prime1) != 0) step = 4*prime1;
460 	else {
461 		if ((nnq->lengthcount%prime2) !=0) step = 4*prime2;
462 		else {
463 			if ((nnq->lengthcount%prime3) !=0) step = 4*prime3;
464 			else step = 4*prime4;
465 		}
466 	}
467 
468 	i = 0;
469 	while (i < samplepixels) {
470 		al = p[ALPHA] << netbiasshift;
471 		b = p[BLUE] << netbiasshift;
472 		g = p[GREEN] << netbiasshift;
473 		r = p[RED] << netbiasshift;
474 		j = contest(nnq, al,b,g,r);
475 
476 		altersingle(nnq, alpha,j,al,b,g,r);
477 		if (rad) alterneigh(nnq, rad,j,al,b,g,r);   /* alter neighbours */
478 
479 		p += step;
480 		while (p >= lim) p -= nnq->lengthcount;
481 
482 		i++;
483 		if (i%delta == 0) {                    /* FPE here if delta=0*/
484 			alpha -= alpha / nnq->alphadec;
485 			radius -= radius / radiusdec;
486 			rad = radius >> radiusbiasshift;
487 			if (rad <= 1) rad = 0;
488 			for (j=0; j<rad; j++)
489 				nnq->radpower[j] = alpha*(((rad*rad - j*j)*radbias)/(rad*rad));
490 		}
491 	}
492 	if (verbose) gd_error_ex(GD_NOTICE, "finished 1D learning: final alpha=%f !\n",((float)alpha)/initalpha);
493 }
494 
495 /**
496  * Function: gdImageNeuQuant
497  *
498  * Creates a new palette image from a truecolor image
499  *
500  * This is the same as calling <gdImageCreatePaletteFromTrueColor> with the
501  * quantization method <GD_QUANT_NEUQUANT>.
502  *
503  * Parameters:
504  *   im            - The image.
505  *   max_color     - The number of desired palette entries.
506  *   sample_factor - The quantization precision between 1 (highest quality) and
507  *                   10 (fastest).
508  *
509  * Returns:
510  *   A newly create palette image; NULL on failure.
511  */
gdImageNeuQuant(gdImagePtr im,const int max_color,int sample_factor)512 BGD_DECLARE(gdImagePtr) gdImageNeuQuant(gdImagePtr im, const int max_color, int sample_factor)
513 {
514 	const int newcolors = max_color;
515 	const int verbose = 1;
516 
517 	int bot_idx, top_idx; /* for remapping of indices */
518 	int remap[MAXNETSIZE];
519 	int i,x;
520 
521 	unsigned char map[MAXNETSIZE][4];
522 	unsigned char *d;
523 
524 	nn_quant *nnq = NULL;
525 
526 	int row;
527 	unsigned char *rgba = NULL;
528 	gdImagePtr dst = NULL;
529 
530 	/* Default it to 3 */
531 	if (sample_factor < 1) {
532 		sample_factor = 3;
533 	}
534 	/* Start neuquant */
535 	/* Pierre:
536 	 * This implementation works with aligned contiguous buffer only
537 	 * Upcoming new buffers are contiguous and will be much faster.
538 	 * let don't bloat this code to support our good "old" 31bit format.
539 	 * It alos lets us convert palette image, if one likes to reduce
540 	 * a palette
541 	 */
542 	if (overflow2(gdImageSX(im), gdImageSY(im))
543 	        || overflow2(gdImageSX(im) * gdImageSY(im), 4)) {
544 		goto done;
545 	}
546 	rgba = (unsigned char *) gdMalloc(gdImageSX(im) * gdImageSY(im) * 4);
547 	if (!rgba) {
548 		goto done;
549 	}
550 
551 	d = rgba;
552 	for (row = 0; row < gdImageSY(im); row++) {
553 		int *p = im->tpixels[row];
554 		register int c;
555 
556 		for (i = 0; i < gdImageSX(im); i++) {
557 			c = *p;
558 			*d++ = gdImageAlpha(im, c);
559 			*d++ = gdImageRed(im, c);
560 			*d++ = gdImageBlue(im, c);
561 			*d++ = gdImageGreen(im, c);
562 			p++;
563 		}
564 	}
565 
566 	nnq = (nn_quant *) gdMalloc(sizeof(nn_quant));
567 	if (!nnq) {
568 		goto done;
569 	}
570 
571 	initnet(nnq, rgba, gdImageSY(im) * gdImageSX(im) * 4, sample_factor, newcolors);
572 
573 	learn(nnq, verbose);
574 	unbiasnet(nnq);
575 	getcolormap(nnq, (unsigned char*)map);
576 	inxbuild(nnq);
577 	/* remapping colormap to eliminate opaque tRNS-chunk entries... */
578 	for (top_idx = newcolors-1, bot_idx = x = 0;  x < newcolors;  ++x) {
579 		if (map[x][3] == 255) { /* maxval */
580 			remap[x] = top_idx--;
581 		} else {
582 			remap[x] = bot_idx++;
583 		}
584 	}
585 	if (bot_idx != top_idx + 1) {
586 		gd_error("  internal logic error: remapped bot_idx = %d, top_idx = %d\n",
587 			 bot_idx, top_idx);
588 		goto done;
589 	}
590 
591 	dst = gdImageCreate(gdImageSX(im), gdImageSY(im));
592 	if (!dst) {
593 		goto done;
594 	}
595 
596 	for (x = 0; x < newcolors; ++x) {
597 		dst->red[remap[x]] = map[x][0];
598 		dst->green[remap[x]] = map[x][1];
599 		dst->blue[remap[x]] = map[x][2];
600 		dst->alpha[remap[x]] = map[x][3];
601 		dst->open[remap[x]] = 0;
602 		dst->colorsTotal++;
603 	}
604 
605 	/* Do each image row */
606 	for ( row = 0; row < gdImageSY(im); ++row ) {
607 		int offset;
608 		unsigned char *p = dst->pixels[row];
609 
610 		/* Assign the new colors */
611 		offset = row * gdImageSX(im) * 4;
612 		for(i=0; i < gdImageSX(im); i++) {
613 			p[i] = remap[
614 			           inxsearch(nnq, rgba[i * 4 + offset + ALPHA],
615 			                     rgba[i * 4 + offset + BLUE],
616 			                     rgba[i * 4 + offset + GREEN],
617 			                     rgba[i * 4 + offset + RED])
618 			       ];
619 		}
620 	}
621 
622 done:
623 	if (rgba) {
624 		gdFree(rgba);
625 	}
626 
627 	if (nnq) {
628 		gdFree(nnq);
629 	}
630 	return dst;
631 }
632