-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.c
More file actions
1537 lines (1234 loc) · 41.4 KB
/
image.c
File metadata and controls
1537 lines (1234 loc) · 41.4 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
/**
* @file image.c Image: operações imagens em formato TGA.
*
* @author
* - Maira Noronha
* - Fabiola Maffra
* - Marcelo Gattass
*
* @date
* Última Modificação: 23 de Abril de 2003
*
* @version 2.0
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include "color.h"
#include "image.h"
/* Compiler dependent definitions */
typedef unsigned char BYTE;
typedef unsigned short int USHORT;
typedef unsigned short int WORD;
typedef long int LONG;
typedef unsigned long int DWORD;
#define ROUND(_) (int)floor( (_) + 0.5 )
struct Image_imp {
int width; /* numero de pixels na direcao horizontal da imagem */
int height; /* numero de pixels na direcao vertical da imagem */
float *buf; /* vetor de dimensao 3*width*height que armazena consecutivamente as 3 componentes de cor de cada pixel a */
/* partir do canto inferior esquerdo da imagem. A posicao das 3 componentes de cor do pixel (x,y) */
/* fica armazenada a partir da posicao (y*width*3) + (x*3) */
};
typedef struct
{
Color max; /* maximos R's, G's e B's */
Color min; /* minimos R's, G's e B's */
float var; /* variacao em cada cubo */
int ini; /* final do cubo */
int fim; /* inicio do cubo */
int maiorDim; /* R=0, G=1, B=2 */
int checked;
} colorCube;
/************************************************************************/
/* Definicao das Funcoes Privadas */
/************************************************************************/
/* getuint e putuint:
* Funcoes auxiliares para ler e escrever inteiros na ordem (lo-hi)
* Note que no Windows as variaveis tipo "unsigned short int" sao
* armazenadas no disco em dois bytes na ordem inversa. Ou seja, o
* numero 400, por exemplo, que pode ser escrito como 0x190, fica
* armazenado em dois bytes consecutivos 0x90 e 0x01. Nos sistemas
* UNIX e Mac este mesmo inteiro seria armazenado na ordem 0x01 e
* 0x90. O armazenamento do Windows e' chamado de "little endian"
* (i.e., lowest-order byte stored first), e no sitemas Unix sao
* "big-endian" (i.e., highest-order byte stored first).
*/
/***************************************************************************
* Reads an unsigned integer from input *
***************************************************************************/
static int getuint(unsigned short *_uint, FILE *input)
{
int got;
unsigned char temp[2];
unsigned short tempuint;
got = (int) fread(&temp, 1, 2, input);
if (got != 2) return 0;
tempuint = ((unsigned short)(temp[1])<<8) | ((unsigned short)(temp[0]));
*_uint = tempuint;
return 1;
}
/***************************************************************************
* Writes an unsigned integer in output *
***************************************************************************/
static int putuint(unsigned short _uint, FILE *output)
{
int put;
unsigned char temp[2];
temp[0] = _uint & 0xff;
temp[1] = (_uint >> 8) & 0xff;
put = (int) fwrite(&temp, 1, 2, output);
if (put != 2) return 0;
return 1;
}
/***************************************************************************
* Reads a long integer from input *
***************************************************************************/
static int getlong(FILE *input, long int *longint)
{
int got;
unsigned char temp[4];
long int templongint;
got = (int)fread(&temp, 1, 4, input);
if (got != 4) return 0;
templongint = ((long int)(temp[3])<<24) | ((long int)(temp[2])<<16)
| ((long int)(temp[1])<<8) | ((long int)(temp[0]));
*longint = templongint;
return 1;
}
/***************************************************************************
* Writes a long integer in output *
***************************************************************************/
static int putlong(FILE *output, long int longint)
{
int put;
unsigned char temp[4];
temp[0] = (unsigned char)longint & 0xff;
temp[1] = (unsigned char)(longint >> 8) & 0xff;
temp[2] = (unsigned char)(longint >> 16) & 0xff;
temp[3] = (unsigned char)(longint >> 24) & 0xff;
put = (int)fwrite(&temp, 1, 4, output);
if (put != 4) return 0;
return 1;
}
/***************************************************************************
* Reads a word from input *
***************************************************************************/
static int getword(FILE *input, unsigned short int *word)
{
int got;
unsigned char temp[2];
unsigned short int tempword;
got = (int)fread(&temp, 1, 2, input);
if (got != 2) return 0;
tempword = ((unsigned short int)(temp[1])<<8) | ((unsigned short int)(temp[0]));
*word = tempword;
return 1;
}
/***************************************************************************
* Writes a word in output *
***************************************************************************/
static int putword(FILE *output, unsigned short int word)
{
int put;
unsigned char temp[2];
temp[0] = word & 0xff;
temp[1] = (word >> 8) & 0xff;
put = (int)fwrite(&temp, 1, 2, output);
if (put != 2) return 0;
return 1;
}
/***************************************************************************
* Reads a double word from input *
***************************************************************************/
static int getdword(FILE *input, unsigned long int *dword)
{
int got;
unsigned char temp[4];
unsigned long int tempdword;
got = (int)fread(&temp, 1, 4, input);
if (got != 4) return 0;
tempdword = ((unsigned long int)(temp[3])<<24) | ((unsigned long int)(temp[2])<<16)
| ((unsigned long int)(temp[1])<<8) | ((unsigned long int)(temp[0]));
*dword = tempdword;
return 1;
}
/***************************************************************************
* Writes a double word in output *
***************************************************************************/
static int putdword(FILE *output, unsigned long int dword)
{
int put;
unsigned char temp[4];
temp[0] = (unsigned char) (dword & 0xff);
temp[1] = (unsigned char) ((dword >> 8) & 0xff);
temp[2] = (unsigned char) ((dword >> 16) & 0xff);
temp[3] = (unsigned char) ((dword >> 24) & 0xff);
put = (int)fwrite(&temp, 1, 4, output);
if (put != 4) return 0;
return 1;
}
/************************************************************************/
/* Definicao das Funcoes Exportadas */
/************************************************************************/
Image * imgCreate(int w, int h)
{
Image * image = (Image*) malloc (sizeof(Image));
assert(image);
image->width =(unsigned int) w;
image->height =(unsigned int) h;
image->buf = (float *) malloc (w * h * 3*sizeof(float));
assert(image->buf);
return image;
}
void imgDestroy (Image *image)
{
if (image)
{
if (image->buf) free (image->buf);
free(image);
}
}
Image * imgCopy(Image * image)
{
int w = imgGetWidth(image);
int h = imgGetHeight(image);
Image * img1=imgCreate(w,h);
int x,y;
float rgb[3];
for (y=0;y<h;y++){
for (x=0;x<w;x++) {
imgGetPixel3fv(image,x,y,rgb);
imgSetPixel3fv(img1,x,y,rgb);
}
}
return img1;
}
Image * imgGrey(Image * image)
{
int w = imgGetWidth(image);
int h = imgGetHeight(image);
Image * img1=imgCreate(w,h);
int x,y;
float rgb[3],grey[3];
for (y=0;y<h;y++){
for (x=0;x<w;x++) {
imgGetPixel3fv(image,x,y,rgb);
grey[0]=0.2126f*rgb[0]+0.7152f*rgb[1]+0.0722f*rgb[2];
grey[1]=grey[0];
grey[2]=grey[0];
imgSetPixel3fv(img1,x,y,grey);
}
}
return img1;
}
Image * imgResize(Image * img0, int w1, int h1)
{
Image *img1 = imgCreate(w1,h1);
int w0, h0;
int x0,y0,x1,y1;
Color color;
w0 = imgGetWidth(img0);
h0 = imgGetHeight(img0);
for (y1=0;y1<h1;y1++)
for (x1=0;x1<w1;x1++)
{
x0=ROUND(w0*x1/w1); /* pega a cor do pixel mais proxima */
y0=ROUND(h0*y1/h1);
color = imageGetPixel(img0,x0,y0);
imageSetPixel(img1,x1,y1,color);
}
return img1;
}
Image * imgAdjust2eN(Image *img0)
{
Image * img1;
int i=0,valid[14]={1,2,4,8,16,32,64,128,512,1024,2048,4096,8192,16384};
int w0=img0->width;
int h0=img0->height;
int w1,h1;
int x,y;
float rgb[3],black[3]={0.f,0.f,0.f};
for (i=0;i<14&&valid[i]<w0;i++);
w1=valid[i];
for (i=0;i<14&&valid[i]<h0;i++);
h1=valid[i];
img1=imgCreate(w1,h1);
for (y=0;y<h1;y++){
for (x=0;x<w1;x++) {
if (x<w0&&y<h0) {
imgGetPixel3fv(img0,x,y,rgb);
imgSetPixel3fv(img1,x,y,rgb);
}
else
imgSetPixel3fv(img1,x,y,black);
}
}
return img1;
}
int imgGetWidth(Image * image)
{
return image->width;
}
int imgGetHeight(Image * image)
{
return image->height;
}
float *imgGetRGBData(Image * image)
{
return image->buf;
}
void imgSetPixel3fv(Image * image, int x, int y, float * color)
{
int pos = (y*image->width*3) + (x*3);
image->buf[pos ] = color[0];
image->buf[pos+1] = color[1];
image->buf[pos+2] = color[2];
}
void imgGetPixel3fv(Image * image, int x, int y, float *color)
{
int pos = (y*image->width*3) + (x*3);
color[0] = image->buf[pos ];
color[1] = image->buf[pos+1];
color[2] = image->buf[pos+2];
}
void imgSetPixel3ubv(Image * image, int x, int y, unsigned char * color)
{
int pos = (y*image->width*3) + (x*3);
image->buf[pos ] = (float)(color[0]/255.);
image->buf[pos+1] = (float)(color[1]/255.);
image->buf[pos+2] = (float)(color[2]/255.);
}
void imgGetPixel3ubv(Image * image, int x, int y, unsigned char *color)
{
int pos = (y*image->width*3) + (x*3);
int r= ROUND(255*image->buf[pos]);
int g= ROUND (255*image->buf[pos+1]);
int b= ROUND (255*image->buf[pos+2]);
color[0] = (unsigned char)(r<256) ? r : 255 ;
color[1] = (unsigned char)(g<256) ? g : 255 ;
color[2] = (unsigned char)(b<256) ? b : 255 ;
}
void imageSetPixel(Image *image, int x, int y, Color color)
{
int pos = (y*image->width*3) + (x*3);
image->buf[pos ] = (float)(color.red);
image->buf[pos+1] = (float)(color.green);
image->buf[pos+2] = (float)(color.blue);
}
Color imageGetPixel(Image *image, int x, int y)
{
Color color;
int pos = (y*image->width*3) + (x*3);
color.red = (image->buf[pos]);
color.green = (image->buf[pos+1]);
color.blue = (image->buf[pos+2]);
return color;
}
Image * imageLoad(char *filename)
{
FILE *filePtr;
Image *image; /* imagem a ser criada */
unsigned char *buffer; /* buffer para ler o vetor de rgb da imagem */
unsigned char imageType; /* 2 para imagens RGB */
unsigned short int imageWidth; /* largura da imagem */
unsigned short int imageHeight; /* altura da imagem */
unsigned char bitCount; /* numero de bits por pixel */
int x,y; /* posicao de um pixel */
unsigned char ucharSkip; /* dado lixo unsigned char */
short int sintSkip; /* dado lixo short int */
/* abre o arquivo com a imagem TGA */
filePtr = fopen(filename, "rb");
assert(filePtr);
/* pula os primeiros dois bytes que devem ter valor zero */
ucharSkip = getc(filePtr); /* tamanho do descritor da imagem (0) */
if (ucharSkip != 0) printf("erro na leitura de %s: imagem com descritor\n", filename);
ucharSkip = getc(filePtr);
if (ucharSkip != 0) printf("erro na leitura de %s: imagem com tabela de cores\n", filename);
/* le o tipo de imagem (que deve ser obrigatoriamente 2).
nao estamos tratando dos outros tipos */
imageType=getc(filePtr);
assert(imageType == 2);
/* pula 9 bytes relacionados com a tabela de cores
(que nao existe quando a imagem e' RGB, imageType=2) */
getuint((short unsigned int *)&sintSkip,filePtr);
getuint((short unsigned int *)&sintSkip,filePtr);
ucharSkip = getc(filePtr);
/* especificacao da imagem */
getuint((short unsigned int *)&sintSkip,filePtr); /* origem em x (por default = 0) */
getuint((short unsigned int *)&sintSkip,filePtr); /* origem em y (por default = 0) */
getuint(&imageWidth,filePtr); /* largura */
getuint(&imageHeight,filePtr); /* altura */
/* read image bit depth */
bitCount=getc(filePtr);
assert(bitCount == 24); /* colorMode -> 3 = BGR (24 bits) */
/* read 1 byte of garbage data */
ucharSkip = getc(filePtr);
/* cria uma instancia do tipo Imagem */
image = imgCreate(imageWidth,imageHeight);
buffer = (unsigned char *) malloc(3*imageWidth*imageHeight*sizeof(unsigned char));
assert(image);
assert(buffer);
/* read in image data */
fread(buffer, sizeof(unsigned char), 3*imageWidth*imageHeight, filePtr);
/* copia e troca as compontes de BGR para RGB */
for (y=0;y<imageHeight;y++) {
for (x=0;x<imageWidth;x++) {
int pos = (y*imageWidth*3) + (x*3);
image->buf[pos ] = (float)(buffer[pos+2]/255.);
image->buf[pos+1] = (float)(buffer[pos+1]/255.);
image->buf[pos+2] = (float)(buffer[pos ]/255.);
}
}
free(buffer);
fclose(filePtr);
return image;
}
int imageWriteTGA(char *filename, Image * image)
{
unsigned char imageType=2; /* RGB(A) sem compressão */
unsigned char bitDepth=24; /* 24 bits por pixel */
FILE *filePtr; /* ponteiro do arquivo */
unsigned char * buffer; /* buffer de bytes */
int x,y;
unsigned char byteZero=0; /* usado para escrever um byte zero no arquivo */
short int shortZero=0; /* usado para escrever um short int zero no arquivo */
if (!image) return 0;
/* cria um arquivo binario novo */
filePtr = fopen(filename, "wb");
assert(filePtr);
/* cria o buffer */
buffer = (unsigned char *) malloc(3*image->width*image->height*sizeof(unsigned char));
assert(buffer);
/* copia e troca as compontes de BGR para RGB */
for (y=0;y<image->height;y++) {
for (x=0;x<image->width;x++) {
int pos = (y*image->width*3) + (x*3);
int r= ROUND (255*image->buf[pos ]);
int g= ROUND (255*image->buf[pos+1]);
int b= ROUND (255*image->buf[pos+2]);
buffer[pos+2] = (unsigned char)(r<256) ? r : 255 ;
buffer[pos+1] = (unsigned char)(g<256) ? g : 255 ;
buffer[pos ] = (unsigned char)(b<256) ? b : 255 ;
}
}
/* escreve o cabecalho */
putc(byteZero,filePtr); /* 0, no. de caracteres no campo de id da imagem */
putc(byteZero,filePtr); /* = 0, imagem nao tem palheta de cores */
putc(imageType,filePtr); /* = 2 -> imagem "true color" (RGB) */
putuint(shortZero,filePtr); /* info sobre a tabela de cores (inexistente) */
putuint(shortZero,filePtr); /* idem */
putc(byteZero,filePtr); /* idem */
putuint(shortZero,filePtr); /* =0 origem em x */
putuint(shortZero,filePtr); /* =0 origem em y */
putuint(image->width,filePtr); /* largura da imagem em pixels */
putuint(image->height,filePtr); /* altura da imagem em pixels */
putc(bitDepth,filePtr); /* numero de bits de um pixel */
putc(byteZero, filePtr); /* =0 origem no canto inf esquedo sem entrelacamento */
/* escreve o buf de cores da imagem */
fwrite(buffer, sizeof(unsigned char), 3*image->width*image->height, filePtr);
free(buffer);
fclose(filePtr);
return 1;
}
Image * imgReadBMP(char *filename)
{
FILE *filePtr; /* ponteiro do arquivo */
Image *image; /* imagem a ser criada */
BYTE *linedata;
USHORT bfType; /* "BM" = 19788 */
LONG biWidth; /* image width in pixels */
LONG biHeight; /* image height in pixels */
WORD biBitCount; /* bitmap color depth */
DWORD bfSize;
USHORT ushortSkip; /* dado lixo USHORT */
DWORD dwordSkip; /* dado lixo DWORD */
LONG longSkip; /* dado lixo LONG */
WORD wordSkip; /* dado lixo WORD */
LONG i, j, k, l, linesize, got;
/* abre o arquivo com a imagem BMP */
filePtr = fopen(filename, "rb");
assert(filePtr);
/* verifica se eh uma imagem bmp */
getuint(&bfType, filePtr);
assert(bfType == 19778);
/* pula os 12 bytes correspondentes a bfSize, Reserved1 e Reserved2 */
getdword(filePtr, &bfSize);
getuint(&ushortSkip, filePtr); /* Reserved1, deve ter valor 0 */
assert(ushortSkip == 0);
getuint(&ushortSkip, filePtr); /* Reserved2, deve ter valor 0 */
assert(ushortSkip == 0);
/* pula os 4 bytes correspondentes a bfOffBits, que deve ter valor 54 */
getdword(filePtr, &dwordSkip);
assert(dwordSkip == 54);
/* pula os 4 bytes correspondentes a biSize, que deve ter valor 40 */
getdword(filePtr, &dwordSkip);
assert(dwordSkip == 40);
/* pega largura e altura da imagem */
getlong(filePtr, &biWidth);
getlong(filePtr, &biHeight);
/* verifica que o numero de quadros eh igual a 1 */
getword(filePtr, &wordSkip);
assert(wordSkip == 1);
/* Verifica se a imagem eh de 24 bits */
getword(filePtr, &biBitCount);
if(biBitCount != 24)
{
fprintf(stderr, "imgReadBMP: Not a bitmap 24 bits file.\n");
fclose(filePtr);
return (NULL);
}
/* pula os demais bytes do infoheader */
getdword(filePtr, &dwordSkip);
assert(dwordSkip == 0);
getdword(filePtr, &dwordSkip);
getlong(filePtr, &longSkip);
getlong(filePtr, &longSkip);
getdword(filePtr, &dwordSkip);
getdword(filePtr, &dwordSkip);
image = imgCreate(biWidth, biHeight);
/* a linha deve terminar em uma fronteira de dword */
linesize = 3*image->width;
if (linesize & 3) {
linesize |= 3;
linesize++;
}
/* aloca espaco para a area de trabalho */
linedata = (BYTE *) malloc(linesize);
if (linedata == NULL) {
fprintf(stderr, "get24bits: Not enough memory.\n");
return 0;
}
/* pega as componentes de cada pixel */
for (k=0, i=0; i<image->height; i++) {
got = (unsigned long int)fread(linedata, linesize, 1, filePtr);
if (got != 1) {
free(linedata);
fprintf(stderr, "get24bits: Unexpected end of file.\n");
}
for (l=1, j=0; j<image->width; j++, l=l+3) {
image->buf[k++] = (float)(linedata[l+1]/255.);
image->buf[k++] = (float)(linedata[l ]/255.);
image->buf[k++] = (float)(linedata[l-1]/255.);
}
}
free(linedata);
fclose(filePtr);
return image;
}
int imgWriteBMP(char *filename, Image * bmp)
{
FILE *filePtr; /* ponteiro do arquivo */
unsigned char *filedata;
DWORD bfSize;
int i, j, k, l;
int linesize, put;
if (!bmp) return 0;
/* cria um novo arquivo binario */
filePtr = fopen(filename, "wb");
assert(filePtr);
/* a linha deve terminar em uma double word boundary */
linesize = bmp->width * 3;
if (linesize & 3) {
linesize |= 3;
linesize ++;
}
/* calcula o tamanho do arquivo em bytes */
bfSize = 14 + /* file header size */
40 + /* info header size */
bmp->height * linesize; /* image data size */
/* Preenche o cabeçalho -> FileHeader e InfoHeader */
putuint(19778, filePtr); /* type = "BM" = 19788 */
putdword(filePtr, bfSize); /* bfSize -> file size in bytes */
putuint(0, filePtr); /* bfReserved1, must be zero */
putuint(0, filePtr); /* bfReserved2, must be zero */
putdword(filePtr, 54); /* bfOffBits -> offset in bits to data */
putdword(filePtr, 40); /* biSize -> structure size in bytes */
putlong(filePtr, bmp->width); /* biWidth -> image width in pixels */
putlong(filePtr, bmp->height); /* biHeight -> image height in pixels */
putword(filePtr, 1); /* biPlanes, must be 1 */
putword(filePtr, 24); /* biBitCount, 24 para 24 bits -> bitmap color depth */
putdword(filePtr, 0); /* biCompression, compression type -> no compression */
putdword(filePtr, 0); /* biSizeImage, nao eh usado sem compressao */
putlong(filePtr, 0); /* biXPelsPerMeter */
putlong(filePtr, 0); /* biYPelsPerMeter */
putdword(filePtr, 0); /* biClrUsed, numero de cores na palheta */
putdword(filePtr, 0); /* biClrImportant, 0 pq todas sao importantes */
/* aloca espacco para a area de trabalho */
filedata = (unsigned char *) malloc(linesize);
assert(filedata);
/* a linha deve ser zero padded */
for (i=0; i<(linesize-(3*bmp->width)); i++)
filedata[linesize-1-i] = 0;
j = 1;
for (k=0; k<bmp->height;k++)
{
l = 1;
/* coloca as componentes BGR no buffer */
for (i=0; i<bmp->width; i++) {
int b= ROUND (255*bmp->buf[j+1]);
int g= ROUND (255*bmp->buf[j ]);
int r= ROUND (255*bmp->buf[j-1]);
filedata[l-1] = (unsigned char)(b<256) ? b : 255 ;
filedata[l ] = (unsigned char)(g<256) ? g : 255 ;
filedata[l+1] = (unsigned char)(r<256) ? r : 255 ;
j+=3; l+=3;
}
/* joga para o arquivo */
put = (int)fwrite(filedata, linesize, 1, filePtr);
if (put != 1) {
fprintf(stderr, "put24bits: Disk full.");
free(filedata);
return 0;
}
}
/* operacao executada com sucesso */
fprintf(stdout,"imgWriteBMP: %s successfuly generated\n",filename);
free(filedata);
fclose(filePtr);
return 1;
}
static int comparaCor(const void * p1, const void * p2)
{
float *c1 = (float *) p1; /* aponta para o byte red da cor 1 */
float *c2 = (float *) p2; /* aponta para o byte red da cor 2 */
/* compara o canal vermelho */
if (*c1 < *c2) return -1;
if (*c1 > *c2) return 1;
/* compara o canal verde, uma vez que o vermelho e' igual */
c1++; c2++;
if (*c1 < *c2) return -1;
if (*c1 > *c2) return 1;
/* compara o canal azul, uma vez que o vermelho e o azul sao iguais */
c1++; c2++;
if (*c1 < *c2) return -1;
if (*c1 > *c2) return 1;
/* sao iguais */
return 0;
}
unsigned int imgCountColors(Image * img)
{
unsigned int numCor = 1;
int w = imgGetWidth(img);
int h = imgGetHeight(img);
float* buf=imgGetRGBData(img);
float *vet=(float*) malloc(3*w*h*sizeof(float));
int i;
/* copia o buffer da imagem no vetor de floats fazendo uma quantizacao para 256 tons de cada componente de cor */
for (i=0;i<3*w*h;i++) {
float val = (float)(255*buf[i]);
vet[i] = (float)(val<256) ? val : 255;
}
/* ordena o vetor */
qsort(vet,w*h,3*sizeof(float),comparaCor);
/* conta o numero de cores diferentes */
for (i=3; i<3*w*h; i+=3)
if (comparaCor(&vet[i-3],&vet[i])!=0) numCor++;
free(vet);
return numCor;
}
Image * imgNormalizeColors(Image * img0)
{
int w = imgGetWidth(img0);
int h = imgGetHeight(img0);
Image* img1 = imgCreate(w,h);
int x,y;
float rgb[3];
for (y=0;y<h;y++){
for (x=0;x<w;x++) {
imgGetPixel3fv(img0,x,y,rgb);
imgSetPixel3fv(img1,x,y,rgb);
}
}
return img1;
}
static int comparaR(const void * p1, const void * p2)
{
Color *color1 = (Color *) p1;
Color *color2 = (Color *) p2;
Color c1 = *color1;
Color c2 = *color2;
if (c1.red < c2.red) return -1;
if (c1.red > c2.red) return 1;
return 0;
}
static int comparaG(const void * p1, const void * p2)
{
Color *color1 = (Color *) p1;
Color *color2 = (Color *) p2;
Color c1 = *color1;
Color c2 = *color2;
if (c1.green < c2.green) return -1;
if (c1.green > c2.green) return 1;
return 0;
}
static int comparaB(const void * p1, const void * p2)
{
Color *color1 = (Color *) p1;
Color *color2 = (Color *) p2;
Color c1 = *color1;
Color c2 = *color2;
if (c1.blue < c2.blue) return -1;
if (c1.blue > c2.blue) return 1;
return 0;
}
void caixaEnvolvente(colorCube* cube, Color* colorVec)
{
float r,g,b;
int i;
/* inicializa com o pior caso */
cube->min.red = cube->min.green = cube->min.blue = 1.0;
cube->max.red = cube->max.green = cube->max.blue = 0.0;
/* percorre o cubo ajustando o domínio das cores */
for (i=cube->ini;i<=cube->fim;i++){
r = colorVec[i].red;
if (r > cube->max.red) cube->max.red = r;
if (r < cube->min.red) cube->min.red = r;
g = colorVec[i].green;
if (g > cube->max.green) cube->max.green = g;
if (g < cube->min.green) cube->min.green = g;
b = colorVec[i].blue;
if (b > cube->max.blue) cube->max.blue = b;
if (b < cube->min.blue) cube->min.blue = b;
}
}
Image* bestColor(Image *img0, Color* pal, Image* img1, int pal_size)
{
int w = imgGetWidth(img0);
int h = imgGetHeight(img0);
float rgb[3];
int x,y,i;
for (y=0;y<h;y++){
for (x=0;x<w;x++){
int i_menor;
float m_menor;
float rgb1[3];
imgGetPixel3fv(img0,x,y,rgb);
m_menor = FLT_MAX;
i_menor = 0;
for(i=0;i<pal_size;i++)
{
float dif[3];
float m;
dif[0] = rgb[0] - pal[i].red;
dif[1] = rgb[1] - pal[i].green;
dif[2] = rgb[2] - pal[i].blue;
m = (float)((dif[0]*dif[0]) + (dif[1]*dif[1]) + (dif[2]*dif[2]));
if (m < m_menor)
{
i_menor = i;
m_menor = m;
rgb1[0]=pal[i_menor].red;
rgb1[1]=pal[i_menor].green;
rgb1[2]=pal[i_menor].blue;
}
}
imgSetPixel3fv(img1,x,y,rgb1);
}
}
return img1;
}
void paleta(Color* pal, colorCube* cubeVec, Color* colorVec, int numCubos)
{
float sumvar[3];
int count;
int i,j;
for(i=0;i<numCubos;i++)
{
sumvar[0] = sumvar[1] = sumvar[2] = 0.0;
count = 0;
for(j=cubeVec[i].ini;j<=cubeVec[i].fim;j++)
{
sumvar[0] += colorVec[j].red;
sumvar[1] += colorVec[j].green;
sumvar[2] += colorVec[j].blue;
count++;
}
pal[i].red = sumvar[0]/count;
pal[i].green = sumvar[1]/count;
pal[i].blue = sumvar[2]/count;
}
}
void ordenaMaiorDim(colorCube* cubeVec, Color* colorVec, int i)
{
size_t numElem;
size_t tamElem;
float var[3];
numElem = (size_t)(cubeVec[i].fim - cubeVec[i].ini + 1);
tamElem = (size_t)(sizeof(Color));
var[0] = cubeVec[i].max.red - cubeVec[i].min.red;
var[1] = cubeVec[i].max.green - cubeVec[i].min.green;
var[2] = cubeVec[i].max.blue - cubeVec[i].min.blue;
/* procura a maior dimensao em um cubo */
if((var[0]>=var[1])&&(var[0]>=var[2])) cubeVec[i].maiorDim = 0;
else
if((var[1]>=var[0])&&(var[1]>=var[2])) cubeVec[i].maiorDim = 1;
else
if((var[2]>var[0]) && (var[2]>var[1])) cubeVec[i].maiorDim = 2;
/* ordena de acordo com a maior dimensao da caixa */
if(cubeVec[i].maiorDim == 0)
{
qsort(&colorVec[cubeVec[i].ini],numElem,tamElem,comparaR);
cubeVec[i].var = var[0];
}
if(cubeVec[i].maiorDim == 1)
{
qsort(&colorVec[cubeVec[i].ini],numElem,tamElem,comparaG);
cubeVec[i].var = var[1];
}
if(cubeVec[i].maiorDim == 2)
{
qsort(&colorVec[cubeVec[i].ini],numElem,tamElem,comparaB);
cubeVec[i].var = var[2];
}
}
void cortaCubo(colorCube* cubeVec, int posCorte, int numCubos)
{
/* divide o cubo */
int ini = cubeVec[posCorte].ini;
int fim = cubeVec[posCorte].fim;
if((fim - ini)%2 != 0) /* numero par de elementos */
{
cubeVec[numCubos].ini = ini + (fim - ini + 1)/2;
cubeVec[numCubos].fim = fim;
cubeVec[posCorte].fim = cubeVec[numCubos].ini - 1;
}
else /* numero impar de elementos */
{
cubeVec[numCubos].ini = ini + (fim - ini)/2;
cubeVec[numCubos].fim = fim;
cubeVec[posCorte].fim = cubeVec[numCubos].ini - 1;
}
}
int cuboCorte(colorCube* cubeVec, int numCubos)
{
/* escolhe o cubo a ser cortado */
float maiorVar = -1;
int posCorte = -1;
int k;
for(k=0;k<numCubos;k++)
{
/* cubo com uma unica cor */