Repository navigation
Expand file tree
/
Copy pathquant.cpp
More file actions
1782 lines (1585 loc) · 53.2 KB
/
Copy pathquant.cpp
File metadata and controls
1782 lines (1585 loc) · 53.2 KB
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
typedef unsigned char byte;
typedef unsigned int u_int;
typedef unsigned short int word;
typedef unsigned long u_long;
// 5 bits to red, 5 bits to green and 5 bits to blue.
// This macro convert RGB to a long int to create a
// otimized palette not considering very close colors.
#define RGB(r, g, b) (word)(((b) & ~7) << 7) | (((g) & ~7) << 2) | ((r) >> 3)
// this macro converts from the optimized palette to RGB
#define RED(x) (byte)(((x)&31) << 3)
#define GREEN(x) (byte)((((x) >> 5) & 255) << 3)
#define BLUE(x) (byte)((((x) >> 10) & 255) << 3)
// Returns only the most significant 5 bits and cancels the least 3
#define SIG5(x) (byte)((x)&248)
// Colors vector ******************************************************
enum
{
RED = 0, // Red color vector
GREEN = 1, // Green color vector
BLUE = 2, // Blue color vector
};
// Frequency histogram ***********************************************
struct Hist
{
byte red, green, blue;
u_long freq;
float erro;
} * histogram;
u_long hist_size;
// Image information **********************************************
int Width; // Image width
int Height; // Image height
int Type; // Image type (0=RGB, 1=MAP)
byte *Red, *Green, *Blue; // Image RGB
byte *Map; // Image Map
long *Colors; // Image color table
int err;
double quantization_error;
// Timer information ************************************************
typedef long int __time_t;
typedef __time_t time_t;
struct tm *the_time;
time_t t;
typedef struct t_dec
{
int sec;
int min;
int hour;
} tmp;
static tmp t_ini, t_fin, t_tot;
// Frequency histogram with float colors instead of byte ****************
struct Hist_Float
{
float red, green, blue;
u_long freq;
float error;
} * hist_float;
u_long new_hist_size;
// Image functions ************************************************************************
SDL_Surface *load_image(char *filename, int *Width, int *Height);
int load_image_rgb(SDL_Surface *surface, byte *Red, byte *Green, byte *Blue);
int save_image_rgb(SDL_Surface *surface, byte *Red, byte *Green, byte *Blue, char *filename);
// Functions for general use *************************************************************
void error_message(int err);
char *str_to_upper(char *s);
int check_parameters(char *Param);
void save_time(tmp &T);
void calculate_time(void);
void information(void);
void create_histgram(long size, byte *red, byte *green, byte *blue);
double euclidian_distance(Hist_Float ci, Hist_Float cj);
double euclidian_distance(Hist ci, Hist cj);
// Information for the Double Cluster Quantization Algorithm ****************
// Error matrix information - E[i][j]
float *coluna;
float **E;
// Functions for the Double Cluster Quantization Algorithm *********************
void double_cluster(int width, int height, byte *red, byte *green, byte *blue, u_long newpalsize);
void select_color_pairs(u_long &i_min, u_long &j_min);
void calculate_error_matrix(u_long tam_matriz);
void join_color_pairs(u_long ci, u_long cj, Hist_Float &cor);
void change_colors(u_long ci, u_long cj, Hist_Float cor);
void apply_quantization(u_long tam_imagem, byte *red, byte *green, byte *blue);
// Vector used in the Recursive Subdivision Algorithm *****************
struct VET
{
byte red, green, blue;
u_long bigger, smaller;
u_long freq;
} * vector;
// Functions for the Recursive Subdivision Quantization Algorithm
void change_histogram_position(byte *color, u_int pos1, u_int pos2);
void sort(byte *color, u_long esq, u_long dir);
void recursive_subdivision(int width, int height, byte *red,
byte *green, byte *blue,
u_long newpalsize);
// Information for the octree *****************************************
#define prof_max = 8; // Maximum depth to the tree
// Information of a octree node
typedef struct _Node
{
struct _Node *father, *child[8];
float r, g, b; // colors red, green and blue
byte er, eg, eb; // smaller colors of subcube
byte dr, dg, db; // bigger colors of subcube
byte level; // level where is node in the octree
u_long n_colors; // quantity of colores
} Node;
// RGB cube information
typedef struct _RGBCube
{
Node *root; // Root node
u_long idx_colors; // Size colors of RGB cube
u_long maximum_colors; // Maximun size of colors
Node **list; // Leafs list of RGB cube
} RGBCube;
static RGBCube cubo;
// Functions for the Octree Quantization Algorithm **************************
void octree(int width, int height, byte *red, byte *green, byte *blue,
u_long newpalsize);
void inicialize_octree(u_long qt_cores);
void reduces_octree(void);
void insert_node(Node *no_pai, byte index, byte red, byte green, byte blue);
void track_octree(byte red, byte green, byte blue);
void subcube_area(Node *pont);
// Main function ***************************************************************************
int main(int argc, char *argv[])
{
u_long quantcolors = 0;
if (argc < 5)
{
information();
return 0;
}
SDL_Surface *surface = load_image(argv[1], &Width, &Height);
if (surface)
{
printf("Image information %s\n", argv[1]);
printf("Width: %d, Height: %d\n", Width, Height);
Red = (byte *)malloc(Width * Height);
Green = (byte *)malloc(Width * Height);
Blue = (byte *)malloc(Width * Height);
if (!Red | !Green | !Blue)
{
printf("Alloc memory error.");
return 0;
}
err = load_image_rgb(surface, Red, Green, Blue);
if (!err)
{
if (argc > 3)
{
quantcolors = atol(argv[3]);
save_time(t_ini);
switch (atoi(argv[4]))
{
case 0:
recursive_subdivision(Width, Height, Red, Green, Blue, quantcolors);
break;
case 1:
octree(Width, Height, Red, Green, Blue, quantcolors);
break;
case 2:
double_cluster(Width, Height, Red, Green, Blue, quantcolors);
break;
default:
information();
};
save_time(t_fin);
calculate_time();
err = save_image_rgb(surface, Red, Green, Blue, argv[2]);
if (err)
return 1;
free(Red);
free(Green);
free(Blue);
}
}
else
return 2;
}
else
return 3;
return 0;
}
// Image functions **********************************************************************
SDL_Surface *load_image(char *filename, int *Width, int *Height)
{
SDL_Surface *surface = IMG_Load(filename);
if (surface)
{
*Width = surface->w;
*Height = surface->h;
// convertint to 32 bits
SDL_Surface *aux =
SDL_ConvertSurfaceFormat(
//surface, SDL_PIXELFORMAT_RGBA8888, 0);
surface, SDL_PIXELFORMAT_RGB888, 0);
SDL_FreeSurface(surface);
surface = aux;
}
return surface;
}
int load_image_rgb(SDL_Surface *surface, byte *Red, byte *Green, byte *Blue)
{
if (!surface)
{
return 1;
}
SDL_Color *pixels = (SDL_Color *)surface->pixels;
for (int i = 0; i < surface->w * surface->h; i++)
{
Red[i] = pixels[i].r;
Green[i] = pixels[i].g;
Blue[i] = pixels[i].b;
}
return 0;
}
int save_image_rgb(SDL_Surface *surface, byte *Red, byte *Green, byte *Blue, char *filename)
{
if (!surface)
{
return 1;
}
SDL_Color *pixels = (SDL_Color *)surface->pixels;
for (int i = 0; i < surface->w * surface->h; i++)
{
pixels[i].r = Red[i];
pixels[i].g = Green[i];
pixels[i].b = Blue[i];
}
if (SDL_SaveBMP(surface, filename) != 0)
{
printf("SDL_SaveBMP failed: %s\n", SDL_GetError());
}
return 0;
}
// Functions for general use ***********************************************************
// Transform a string to uppercase
char *str_to_upper(char *s)
{
int i, len = strlen(s);
for (i = 0; i < len; i++)
s[i] = toupper(s[i]);
return s;
}
void save_time(tmp &T)
{
t = time(NULL);
the_time = localtime(&t);
T.sec = the_time->tm_sec;
T.min = the_time->tm_min;
T.hour = the_time->tm_hour;
}
// Calculate execution time of an algorithm
void calculate_time(void)
{
if (t_fin.sec < t_ini.sec)
{
t_fin.min--;
t_fin.sec += 60;
}
t_tot.sec = t_fin.sec - t_ini.sec;
if (t_fin.min < t_ini.min)
{
t_fin.hour--;
t_fin.min += 60;
}
t_tot.min = t_fin.min - t_ini.min;
t_tot.hour = t_fin.hour - t_ini.hour;
printf("Elapsed time: %d:%d:%d\n", t_tot.hour, t_tot.min, t_tot.sec);
}
/*****************************************************************
* If not enter with correct parameters, shows on the screen
* information about program utilization.
******************************************************************/
void information(void)
{
printf("IMAGE QUANTIZATION \n");
printf(" \n");
printf("This program is part of a Bachelor's Degree Work in \n");
printf("Computer Science from UNOESC in 1998. \n");
printf(" \n");
printf("ACADEMIC: Saulo Popov Zambiasi. \n");
printf(" E-MAIL: saulopz@gmail.com \n");
printf(" ADVISOR: Marcos Vinicius Rayol Sobreiro \n");
printf(" \n");
printf("This program aims to transform an image from N colors\n");
printf("to M colors, where N > M, calculating the elapsed \n");
printf("time. Three color image quantization algorithms are \n");
printf("used. \n");
printf(" \n");
printf("Utilization: \n");
printf(" \n");
printf("quant <img1.ext> <img2.ext> <colors> <algorithm> \n");
printf(" \n");
printf(" img1.ext - original image with N colors. \n");
printf(" img2.ext - result image with M colors (N > M). \n");
printf(" colors - quantity of colors in img2.ext. \n");
printf(" algorithm - quantization algorithm: \n");
printf(" 0 - Recursive Subdivision. \n");
printf(" 1 - Octree. \n");
printf(" 2 - Double Clusters. \n");
printf(" \n");
printf(" Example: quant image.png result.png 256 0 \n");
}
/*****************************************************************
* Calculate the number of colors existing on image and generates
* the frequency histogram.
* Parameters:
* long size - image size (width * height
* byte red - vector for red color
* byte green - vector for green color
* byte blue - vector for blue color
* ***************************************************************/
void create_histgram(long size, byte *red, byte *green, byte *blue)
{
long pimage, phist, qt;
int pcent, old;
bool exist;
word *cor;
u_long *Freq;
cor = (word *)malloc(1000000);
Freq = (u_long *)malloc(1000000);
/******************************************************
* First quantizes the image to 15 bits:
* red - 5 bits
* green - 5 bits
* blue - 5 bits
* and calculate frequency histogram size
* ****************************************************/
pcent = old = 0;
printf("Quantizes to 15 bits. Computing colors...\n");
qt = 0;
for (pimage = 0; pimage < size; pimage++)
{
phist = 0;
exist = false;
while ((!exist) && (phist < qt))
{
exist = ((SIG5(red[pimage]) == RED(cor[phist])) &&
(SIG5(green[pimage]) == GREEN(cor[phist])) &&
(SIG5(blue[pimage]) == BLUE(cor[phist])));
phist++;
}
if (!exist)
{
cor[qt] = RGB(red[pimage], green[pimage], blue[pimage]);
Freq[qt] = 1;
qt++;
}
else
Freq[phist - 1]++;
pcent = ((100 * pimage) / size) / 2;
if (pcent > old)
{
old = pcent;
printf(".");
}
}
printf("%ld\n", qt);
hist_size = qt;
// Creating the Frequency Histogram
pcent = old = 0;
printf("Gerar Histograma");
histogram = (struct Hist *)malloc((hist_size) * sizeof(struct Hist));
if (!histogram)
{
printf("...insuficient memory...\n");
return;
}
old = 0;
for (phist = 0; phist < qt; phist++)
{
histogram[phist].red = RED(cor[phist]);
histogram[phist].green = GREEN(cor[phist]);
histogram[phist].blue = BLUE(cor[phist]);
histogram[phist].freq = Freq[phist];
histogram[phist].erro = 0;
pcent = (50 * phist) / hist_size;
if (pcent > old)
{
old = pcent;
printf(".");
}
}
printf("Ok\n");
free(cor);
free(Freq);
}
/*********************************************************************
* Calculate the distance of two colors usinng Euclidean metric square
*
* d(ci, cj) = ||ci - cj||^ 2.
*
* *******************************************************************/
double euclidian_distance(Hist_Float ci, Hist_Float cj)
{
double dist;
dist = pow(ci.red - cj.red, 2) +
pow(ci.green - cj.green, 2) +
pow(ci.blue - cj.blue, 2);
return ((double)dist);
}
double euclidian_distance(Hist ci, Hist cj)
{
double dist;
dist = pow(ci.red - cj.red, 2) +
pow(ci.green - cj.green, 2) +
pow(ci.blue - cj.blue, 2);
return ((double)dist);
}
/******************************************************************
* Double Cluster Quantization Function
* Parameters:
* int width - image width
* int height - image height
* byte red - vector for red color
* byte green - vector for green color
* byte blue - vector for blue color
* u_long newpalsize - image size of colors
* ****************************************************************/
void double_cluster(int width, int height, byte *red, byte *green, byte *blue, u_long newpalsize)
{
u_long tam_imagem, i;
tam_imagem = width * height;
printf("Quantization for Double Clusters.\n");
// Compute quantity of colors on image and generate the histogram
create_histgram(width * height, red, green, blue);
new_hist_size = hist_size;
// Create the histogram with float values (for calcuations purpose).
hist_float = (struct Hist_Float *)malloc((new_hist_size) * sizeof(struct Hist_Float));
for (i = 0; i < new_hist_size; i++)
{
hist_float[i].red = histogram[i].red;
hist_float[i].green = histogram[i].green;
hist_float[i].blue = histogram[i].blue;
hist_float[i].freq = histogram[i].freq;
}
// If new quantity of colors of new image is bigger or equals
// the quantity of original image, then is not necessary operation
if (new_hist_size <= newpalsize)
{
printf("Nao e necessario quantizar\n");
exit(1);
}
u_long ci, cj;
Hist_Float cor;
int pcent, old = 0;
// Compute the error matrix[new_hist_size][new_hist_size]
calculate_error_matrix(new_hist_size);
printf("Quantizing...");
while (new_hist_size > newpalsize)
{
select_color_pairs(ci, cj);
join_color_pairs(ci, cj, cor);
change_colors(ci, cj, cor);
pcent = (int)(((new_hist_size - newpalsize) * 50) / hist_size - newpalsize);
if (pcent < old)
{
printf(".");
old = pcent;
} /* if */
}
printf("Ok\n");
apply_quantization(tam_imagem, red, green, blue);
free(histogram);
}
// Select the pair of colors (ci, cj) with minimum error
// of quantizing Ei,j in matrix E
void select_color_pairs(u_long &i_min, u_long &j_min)
{
u_long i, j, im, jm;
float Em;
im = 0;
jm = 1;
Em = E[im][jm];
for (i = 0; i < new_hist_size; i++)
{
for (j = 0; j < new_hist_size; j++)
{
if ((i < j) && (Em > E[i][j]))
{
im = i;
jm = j;
Em = E[i][j];
}
}
}
i_min = im;
j_min = jm;
}
void join_color_pairs(u_long ci, u_long cj, Hist_Float &cor)
{
float a, h1, h2;
a = (float)hist_float[ci].freq + hist_float[cj].freq;
h1 = hist_float[ci].freq / a;
h2 = hist_float[cj].freq / a;
cor.red = ((h1 * hist_float[ci].red) + (h2 * hist_float[cj].red));
cor.green = ((h1 * hist_float[ci].green) + (h2 * hist_float[cj].green));
cor.blue = ((h1 * hist_float[ci].blue) + (h2 * hist_float[cj].blue));
cor.freq = hist_float[ci].freq + hist_float[cj].freq;
}
void change_colors(u_long ci, u_long cj, Hist_Float cor)
{
u_long i, j;
// I put the new color on the color ci place
hist_float[ci].red = cor.red;
hist_float[ci].green = cor.green;
hist_float[ci].blue = cor.blue;
hist_float[ci].freq = cor.freq;
// I decrease the size of histogram
new_hist_size--;
// I remove the color cj of histogram
for (i = cj; i < new_hist_size; i++)
{
hist_float[i] = hist_float[i + 1];
};
// I remove the color cj of error matrix, line and column
for (i = cj; i < new_hist_size; i++)
{
for (j = 0; j < new_hist_size; j++)
{
E[i][j] = E[i + 1][j];
}
}
/* free(E[tam_hist_new]); */
for (i = 0; i < new_hist_size; i++)
{
for (j = cj; j < new_hist_size; j++)
{
E[i][j] = E[i][j + 1];
}
/* E[i][tam_hist_new] = NULL; */
}
// Recalculate the error in ci, line and column
j = ci;
for (i = 0; i < new_hist_size; i++)
{
if (i < j)
{
E[i][j] = (float)(((hist_float[i].freq + pow(hist_float[j].freq, 2)) +
(hist_float[j].freq + pow(hist_float[i].freq, 2))) /
pow(hist_float[i].freq + hist_float[j].freq, 2)) *
(float)euclidian_distance(hist_float[i], hist_float[j]);
}
else
E[i][j] = 0;
if (j < i)
{
E[j][i] = (float)(((hist_float[i].freq + pow(hist_float[j].freq, 2)) +
(hist_float[j].freq + pow(hist_float[i].freq, 2))) /
pow(hist_float[i].freq + hist_float[j].freq, 2)) *
(float)euclidian_distance(hist_float[i], hist_float[j]);
}
else
E[j][i] = 0;
}
}
void calculate_error_matrix(u_long tam_matriz)
{
u_long i, j;
int pcent, old = 0;
// Alloc memory to a matrix with matrix_zise x matrix_size
printf("Matrix error");
E = (float **)malloc(sizeof(E) * tam_matriz);
if (!E)
{
printf("\nNot possible alloc memory to error matrix.");
exit(1);
}
for (i = 0; i < tam_matriz; i++)
{
E[i] = (float *)malloc(sizeof(coluna) * tam_matriz);
if (!E[i])
{
printf("\nNot possible alloc memory to error matrix.");
exit(1);
}
}
// compute the errors of matrix
for (i = 0; i < tam_matriz; i++)
{
for (j = 0; j < tam_matriz; j++)
{
if (i < j)
{
E[i][j] = (float)(((hist_float[i].freq + pow(hist_float[j].freq, 2)) +
(hist_float[j].freq + pow(hist_float[i].freq, 2))) /
pow(hist_float[i].freq + hist_float[j].freq, 2)) *
(float)euclidian_distance(hist_float[i], hist_float[j]);
}
else
E[i][j] = 0;
}
pcent = (int)((i * 50) / tam_matriz);
if (pcent > old)
{
printf(".");
old = pcent;
}
}
printf("Ok\n");
}
void apply_quantization(u_long tam_imagem, byte *red, byte *green, byte *blue)
{
u_long i, j;
Hist_Float *hist2;
int pcent, old = 0;
hist2 = (struct Hist_Float *)malloc((hist_size) * sizeof(struct Hist_Float));
for (i = 0; i < hist_size; i++)
{
hist2[i].red = histogram[i].red;
hist2[i].green = histogram[i].green;
hist2[i].blue = histogram[i].blue;
hist2[i].freq = histogram[i].freq;
}
u_long menor_dist;
double dist, dist2;
printf("Close colors");
// For each color on histogram
for (i = 0; i < hist_size; i++)
{
// Find the smaller distance
menor_dist = 0;
dist = euclidian_distance(hist2[i], hist_float[menor_dist]);
for (j = 0; j < new_hist_size; j++)
{
dist2 = euclidian_distance(hist2[i], hist_float[j]);
if (dist2 < dist)
{
dist = dist2;
menor_dist = j;
}
}
// Replaces the color by with quantized cell of smaller distance
hist2[i].red = hist_float[menor_dist].red;
hist2[i].green = hist_float[menor_dist].green;
hist2[i].blue = hist_float[menor_dist].blue;
hist2[i].freq = hist_float[menor_dist].freq;
pcent = (int)((i * 50) / hist_size);
if (pcent > old)
{
printf(".");
old = pcent;
}
}
printf("Ok\n");
// Maps the new frequency histogram to the image
bool achou;
printf("Mapping colors");
old = 0;
Hist_Float H;
for (i = 0; i < tam_imagem; i++)
{
achou = false;
j = 0;
while ((!achou) && (j < hist_size))
{
if ((SIG5(red[i]) == histogram[j].red) &&
(SIG5(green[i]) == histogram[j].green) &&
(SIG5(blue[i]) == histogram[j].blue))
{
H.red = red[i];
H.green = green[i];
H.blue = blue[i];
// Calculate the distance from the color to its quantized color
dist = (float)(euclidian_distance(H, hist2[j]));
histogram[j].erro = (float)(dist / histogram[j].freq);
red[i] = (byte)hist2[j].red;
green[i] = (byte)hist2[j].green;
blue[i] = (byte)hist2[j].blue;
achou = true;
}
j++;
}
pcent = (int)((i * 50) / tam_imagem);
if (pcent > old)
{
printf(".");
old = pcent;
} /* if */
} /* for */
printf("Ok\n");
printf("Quantization error");
quantization_error = 0;
old = 0;
for (i = 0; i < hist_size; i++)
{
quantization_error += histogram[i].erro;
pcent = (int)((i * 50) / hist_size);
if (pcent > old)
{
printf(".");
old = pcent;
} /* if */
}
quantization_error = quantization_error / hist_size;
printf("%f\n", quantization_error);
free(hist2);
}
/*****************************************************************
* Change two elements of histogram and of color vector in a
* specific position
* ***************************************************************/
void change_histogram_position(byte *color, u_int pos1, u_int pos2)
{
byte c;
byte red;
byte green;
byte blue;
float erro;
u_long freq;
c = color[pos1];
red = histogram[pos1].red;
green = histogram[pos1].green;
blue = histogram[pos1].blue;
freq = histogram[pos1].freq;
erro = histogram[pos1].erro;
color[pos1] = color[pos2];
histogram[pos1].red = histogram[pos2].red;
histogram[pos1].green = histogram[pos2].green;
histogram[pos1].blue = histogram[pos2].blue;
histogram[pos1].freq = histogram[pos2].freq;
histogram[pos1].erro = histogram[pos2].erro;
color[pos2] = c;
histogram[pos2].red = red;
histogram[pos2].green = green;
histogram[pos2].blue = blue;
histogram[pos2].freq = freq;
histogram[pos2].erro = erro;
}
/*****************************************************************
* Sort a sequence on frequency histogram, idexing by a color.
* The algorithm used is the selection ordenation.
* ***************************************************************/
void sort(byte *color, u_long esq, u_long dir)
{
u_long i, j, min;
for (i = esq; i < dir; i++)
{
min = i;
for (j = i + 1; j <= dir; j++)
{
if (color[j] < color[min])
min = j;
}
change_histogram_position(color, min, i);
}
}
/******************************************************************
* Find the bigger vector from left to right.
* returns:
* RED - if bigger vector is RED
* GREEN - if bigger vector is GREEN
* BLUE - if bigger vector is BLUE
* ****************************************************************/
int bigger_vector(u_int esq, u_int dir)
{
u_long size_red, size_green, size_blue, i;
byte min_red, min_green, min_blue;
byte max_red, max_green, max_blue;
max_red = histogram[esq].red;
max_green = histogram[esq].green;
max_blue = histogram[esq].blue;
min_red = histogram[esq].red;
min_green = histogram[esq].green;
min_blue = histogram[esq].blue;
for (i = esq; i <= dir; i++)
{
if (histogram[i].red > max_red)
max_red = histogram[i].red;
if (histogram[i].green > max_green)
max_green = histogram[i].green;
if (histogram[i].blue > max_blue)
max_blue = histogram[i].blue;
if (histogram[i].red < min_red)
min_red = histogram[i].red;
if (histogram[i].green < min_green)
min_green = histogram[i].green;
if (histogram[i].blue < min_blue)
min_blue = histogram[i].blue;
}
size_red = max_red - min_red;
size_green = max_green - min_green;
size_blue = max_blue - min_blue;
if ((size_red > size_green) && (size_red > size_blue))
return RED;
else if (size_green > size_blue)
return GREEN;
else
return BLUE;
}
/*******************************************************************
* Quantization for Recursive Subdivision
* Inputs:
* int width - image width
* int height - image height
* byte red - vector for red color
* byte gree - vector for green color
* byte blue - vector for blue color
* u_long newpalsize - colors quantity of new image
* ****************************************************************/
void recursive_subdivision(int width, int height, byte *red,
byte *green, byte *blue,
u_long newpalsize)
{
u_long ipos, iultimo, ipont, msoma, i, soma, size, image_size;
int mvec, pcent, old;
printf("Algorithm of Recursive Subdivision\n");
// Calculates the colors number on image and create histogram
image_size = width * height;
create_histgram(image_size, red, green, blue);
// If new color numbers is bigger or equals old image, exit
if (hist_size <= newpalsize)
{
printf("It is not necessary to quantize.\n");
exit(1);
}
vector = (struct VET *)malloc((newpalsize) * sizeof(struct VET));
if (!vector)
{
printf("Unable to allocate memory for quantization vector.");
exit(2);
}
ipont = iultimo = 0;
// Insert first element into the vector
vector[ipont].bigger = hist_size - 1;
vector[ipont].smaller = 0;
printf("Mid cut");
pcent = old = 0;
byte *color;
color = (byte *)malloc(hist_size);
bool inseriu = true;
while ((iultimo < newpalsize - 1) && (inseriu))
{
ipont = 0;
ipos = iultimo;
inseriu = false;
while ((ipont <= ipos) && (iultimo < newpalsize - 1))
{
// Find bigger vector on histogram
mvec = bigger_vector(vector[ipont].smaller, vector[ipont].bigger);
// Sort by bigger vector
for (i = vector[ipont].smaller; i <= vector[ipont].bigger; i++)
{
switch (mvec)
{
case RED:
color[i] = histogram[i].red;
break;
case GREEN:
color[i] = histogram[i].green;
break;
case BLUE: