-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtlibmp.c
1508 lines (1228 loc) · 44.1 KB
/
tlibmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
/********************************************************************************
The MIT License
Copyright (c) 2017 Yeonji
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
********************************************************************************/
#include "tlibmp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#define uint16_LE(str) (uint16_t)((uint16_t)(str[1] << 0) + (uint16_t)(str[0] << 8))
#define uint32_LE(str) (uint32_t)((uint32_t)(str[3] << 0) +\
(uint32_t)(str[2] << 8) +\
(uint32_t)(str[1] << 16)+\
(uint32_t)(str[0] << 24))
#define uint16_BE(str) (uint16_t)((uint16_t)(str[0] << 0) + (uint16_t)(str[1] << 8))
#define uint32_BE(str) (uint32_t)((uint32_t)(str[0] << 0) +\
(uint32_t)(str[1] << 8) +\
(uint32_t)(str[2] << 16)+\
(uint32_t)(str[3] << 24))
#define str2_LE(str,content) {\
str[1] = (uint8_t)((content&0x00FF) >> 0);\
str[0] = (uint8_t)((content&0xFF00) >> 8);\
}
#define str4_LE(str,content) {\
str[3] = (uint8_t)((content&0x000000FF) >> 0);\
str[2] = (uint8_t)((content&0x0000FF00) >> 8);\
str[1] = (uint8_t)((content&0x00FF0000) >> 16);\
str[0] = (uint8_t)((content&0xFF000000) >> 24);\
}
#define str2_BE(str,content) {\
str[0] = (uint8_t)((content&0x00FF) >> 0);\
str[1] = (uint8_t)((content&0xFF00) >> 8);\
}
#define str4_BE(str,content) {\
str[0] = (uint8_t)((content&0x000000FF) >> 0);\
str[1] = (uint8_t)((content&0x0000FF00) >> 8);\
str[2] = (uint8_t)((content&0x00FF0000) >> 16);\
str[3] = (uint8_t)((content&0xFF000000) >> 24);\
}
#define s_free(ptr) if(ptr!=NULL){free(ptr);};
#define tlb_swap(x, y) {x = x^y;\
y = x^y;\
x = x^y;}
static const char * string_unknown = "Unknown";
static const char * string_windows_bmp = "Windows 3.1x bitmap (424D)";
static const char * string_os2_bitmap_array = "OS/2 struct bitmap array (4241)";
static const char * string_os2_color_icon = "OS/2 struct color icon (4349)";
static const char * string_os2_color_pointer = "OS/2 const color pointer (4350)";
static const char * string_os2_icon = "OS/2 struct icon (4943)";
static const char * string_os2_pointer = "OS/2 pointer (5054)";
static const char * string_bitmapcoreheader = "BMP core header";
static const char * string_os22xbitmapheader = "OS/2 BMP header";
static const char * string_bitmapinfoheader = "Standard BMP header";
static const char * string_bitmapv2infoheader = "Adobe Photoshop Externed BMP header ver.2";
static const char * string_bitmapv3infoheader = "Adobe Photoshop Externed BMP header ver.3";
static const char * string_os22xbitmapheader2 = "OS/2 BMP header ver.2";
static const char * string_bitmapv4header = "Standard BMP header ver.4";
static const char * string_bitmapv5header = "Standard BMP header ver.5";
static const char * string_compress_bi_rgb = "None";
static const char * string_compress_bi_rle8 = "RLE 8-bit/pixel";
static const char * string_compress_bi_rle4 = "RLE 4-bit/pixel";
static const char * string_compress_bi_bitfields = "RGBA (Perhaps Huffman 1D)";
static const char * string_compress_bi_jpeg = "JPEG image for printing";
static const char * string_compress_bi_png = "PNG image for printing";
static const char * string_compress_bi_alphabitfields = "RGBA bit field masks on Windows CE 5.0 with .NET 4.0 or later";
static const char * string_compress_bi_cmyk = "Windows Metafile CMYK";
static const char * string_compress_bi_cmykrle8 = "Windows Metafile CMYK with RLE 8-bit/pixel";
static const char * string_compress_bi_cmykrle4 = "Windows Metafile CMYK with RLE 4-bit/pixel";
static const char * get_bmp_type_string(const unsigned char * type_word);
static const char * get_bmp_dib_string(uint32_t size);
static const char * get_bmp_comp_string(uint32_t method);
int convert_from_raw(tlb_image_t * image, uint32_t depth);
uint8_t * convert_to_raw(tlb_image_t * image, uint32_t depth, uint32_t * size);
#define NULL_POINTER_WARN_UTF fprintf(stderr, "%s(): \344\274\240\345\205\245\344\270\200\344\270\252\347\251\272\346\214\207\351\222\210\344\275\240\124\115\104\345\234\250\351\200\227\346\210\221\357\274\237\n",__FUNCTION__)
tlb_image_t * tlb_load_bmp(const char *file_name)
{
/* image struct */
tlb_image_t * image = NULL;
/* image file pointer */
FILE *fp = NULL;
/* bmp header struct */
bmp_header_t header;
/* bmp information header */
bmp_stddib_t dib_info;
/* data offset */
uint32_t data_offset = 0;
/* color depth */
uint32_t color_depth = 0;
/* pixel count */
uint32_t pixel_cnt = 0;
/* open file */
fp = fopen(file_name,"rb");
/* open exceptional */
if(fp == NULL){
fprintf(stderr, "Cannot open file.\n");
return NULL;
}
/* read bmp header */
if(fread(&header, sizeof(header), 1, fp) != 1){
fprintf(stderr, "Broken File: Can not read header.\n");
goto destory;
}
/* unsupported bmp format */
if(uint16_LE(header.bfType)!=WINDOWS_BMP){
fprintf(stderr, "Unsupported Format: %s.\n", get_bmp_type_string(header.bfType));
goto destory;
}
/* transform to uint32_t */
data_offset = uint32_BE(header.bfOffBits);
/* get bmp information header */
fread(&dib_info, sizeof(dib_info), 1, fp);
/**********************************/
/* begin of file legitimacy check */
/**********************************/
/* unsupported bmp format */
if(dib_info.biSize < DIB_BITMAPINFOHEADER_SIZE){
fprintf(stderr, "Unsupported DIB Format: %s.\n", get_bmp_dib_string(dib_info.biSize));
goto destory;
}
/* set bytes_per_pixel and read image */
if(dib_info.biBitCount == 16){
/* 16-bit RGB use two byte every pixel */
color_depth=2;
}else if(dib_info.biBitCount == 24){
/* 24-bit RGB use three byte every pixel */
color_depth=3;
}else if(dib_info.biBitCount == 32){
/* 32-bit RGB use four byte every pixel */
color_depth=4;
}else{
/* unsupported number of bits per pixel */
fprintf(stderr, "Unsupported Format: Can not parse %d-bit color.\n", dib_info.biBitCount);
goto destory;
}
/* unsupported compression method */
if(dib_info.biCompression != COMPRESS_BI_RGB &&\
dib_info.biCompression != COMPRESS_BI_BITFIELDS){
fprintf(stderr, "Unsupported Format: Can not parse %s compression method.\n", get_bmp_comp_string(dib_info.biCompression));
goto destory;
}
/* unsupported compression method */
/* COMPRESS_BI_BITFIELDS marked, but color depth is not 4 */
/* this will appear when use Huffman 1D */
if(dib_info.biCompression == COMPRESS_BI_BITFIELDS && color_depth != 4){
fprintf(stderr, "Unsupported Format: Can not parse with Huffman 1D.\n");
goto destory;
}
/* get data size in theory */
pixel_cnt = dib_info.biWidth * dib_info.biHeight;
if(uint32_BE(header.bfSize)<(pixel_cnt*color_depth+data_offset)){
fprintf(stderr, "Broken File: Size in theory more then that in record.\n");
goto destory;
}
/********************************/
/* end of file legitimacy check */
/********************************/
/* locate file stream */
if(fseek(fp, data_offset, SEEK_SET)!=0){
fprintf(stderr, "Error: Can not locate file stream.\n");
goto destory;
}
/* get memory for image */
image = malloc(sizeof(tlb_image_t));
/* malloc exceptional */
if(image == NULL){
fprintf(stderr, "Error: Can not malloc for new object.\n");
goto destory;
}
/* set width of struct */
image->width = dib_info.biWidth;
/* set height of struct */
image->height = dib_info.biHeight;
/* malloc for cooked img data */
image->data = malloc((4*image->width*image->height)*sizeof(char));
if(image->data == NULL){
fprintf(stderr, "Error: Can not malloc for new object.\n");
goto destory;
}
/* set memory to zero */
memset(image->data, 0, (4*image->width*image->height));
/* read bmp data */
if(fread(image->data, (pixel_cnt*color_depth), 1, fp) != 1){
fprintf(stderr, "Broken File: Can not read data.\n");
goto destory;
}
/* convert to cooked format */
convert_from_raw(image, color_depth);
return image;
destory:
if(image != NULL){
s_free(image->data);
s_free(image);
}
fclose(fp);
return NULL;
}
/* save bmp to file by default settings */
/* bmp_format = Windows 3.1x bitmap (424D) */
/* dib_format = Standard BMP header */
/* bit_pre_pixel = 24 */
int tlb_save_bmp(tlb_image_t * image, const char *file_name)
{
/* bmp header struct */
bmp_header_t header;
/* bmp information header */
bmp_stddib_t dib_info;
uint8_t data_offset;
uint32_t data_size;
uint32_t file_size;
uint8_t *bmp_data = NULL;
FILE * fp = NULL;
if(image==NULL){
fprintf(stderr, "%s(): Cannot operation with (null) image.\n",__FUNCTION__);
return TLB_ERROR;
}
/* convert to bmp data */
bmp_data = convert_to_raw(image, 3, &data_size);
/* convert exceptional */
if(bmp_data == NULL){
fprintf(stderr, "Cannot convert this file.\n");
return TLB_ERROR;
}
data_offset = sizeof(bmp_header_t) + sizeof(bmp_stddib_t);
file_size = data_offset + data_size;
/* bmp header magic */
memset(&header, 0, sizeof(bmp_header_t));
str2_LE(header.bfType, WINDOWS_BMP);
str4_BE(header.bfSize, file_size);
str4_BE(header.bfOffBits, data_offset);
dib_info.biSize = DIB_BITMAPINFOHEADER_SIZE;
dib_info.biWidth = image->width;
dib_info.biHeight = image->height;
dib_info.biPlanes = 1; /* This shoule always be 1 */
dib_info.biBitCount = 24;
dib_info.biCompression = COMPRESS_BI_RGB;
dib_info.biSizeImage = data_size;
dib_info.biXPelsPerMeter = 72;
dib_info.biYPelsPerMeter = 72;
dib_info.biClrUsed = 0;
dib_info.biClrImportant = 0;
/* open file */
fp = fopen(file_name,"wb");
/* open exceptional */
if(fp == NULL){
fprintf(stderr, "Cannot open file.\n");
return TLB_ERROR;
}
fwrite(&header, sizeof(header), 1, fp);
fwrite(&dib_info, sizeof(dib_info), 1, fp);
fwrite(bmp_data, data_size, 1, fp);
fclose(fp);
s_free(bmp_data);
return 0;
}
int tlb_print_bmp_info(const char *file_name)
{
bmp_header_t header;
bmp_stddib_t dib_info;
const char * type_string = NULL;
FILE * fp;
uint32_t offset;
fp = fopen(file_name,"rb");
if(fp == NULL){
fprintf(stderr, "\tCannot open this file.\n");
return TLB_ERROR;
}
if( fread(&header, sizeof(header), 1, fp) == 1){
/* check file type */
type_string = get_bmp_type_string(header.bfType);
fprintf(stderr, "%s\n", type_string);
if (type_string == string_unknown) return TLB_ERROR;
fprintf(stderr, "File Size: %d Byte\n", uint32_BE(header.bfSize));
offset = uint32_BE(header.bfOffBits);
fprintf(stderr, "Data Offset: %d Byte\n", offset);
fread(&dib_info, sizeof(dib_info), 1, fp);
type_string = get_bmp_dib_string(dib_info.biSize);
fprintf(stderr, "DIB Type: %s (%d Byte)\n",type_string, dib_info.biSize);
if (type_string == string_unknown) return TLB_ERROR;
fprintf(stderr, "Image Width: %d pix\n", dib_info.biWidth);
fprintf(stderr, "Image Width: %d pix\n", dib_info.biHeight);
fprintf(stderr, "Color Planes: %d\n", dib_info.biPlanes);
fprintf(stderr, "Color Depth: %d-bit per pixel\n", dib_info.biBitCount);
type_string = get_bmp_comp_string(dib_info.biCompression);
fprintf(stderr, "Compression Type: %s (%d)\n",type_string, dib_info.biCompression);
}else{
fprintf(stderr, "Bad File\n");
}
fclose(fp);
return TLB_OK;
}
/**************/
/* conv utils */
/**************/
tlb_core_t * tlb_core_new(uint8_t size)
{
tlb_core_t * core = NULL;
int16_t real_size = 0;
if (size<=1){
fprintf(stderr, "Error: Convolution core too small.\n");
return NULL;
}
real_size = size * size;
core = malloc(sizeof(tlb_core_t));
/* malloc exceptional */
if(core == NULL){
fprintf(stderr, "Error: Can not malloc for new object.\n");
return NULL;
}
/* init */
core->size = size;
core->div = 0.0;
core->data = malloc(real_size*sizeof(double));
/* malloc exceptional */
if(core->data == NULL){
fprintf(stderr, "Error: Can not malloc for new object.\n");
return NULL;
}
for (real_size = real_size - 1; real_size >= 0; real_size--){
core->data[real_size] = 0.0;
}
return core;
}
int __cdecl tlb_core_load(tlb_core_t * core, ...)
{
va_list sp;
uint16_t cnt;
uint16_t size = core->size*core->size;
va_start(sp, core);
for (cnt = 0; cnt < size; cnt++){
core->data[cnt] = va_arg(sp, double);
}
va_end(sp);
return 0;
}
// double tlb_core_element(tlb_core_t * core, uint8_t x, uint8_t y)
// {
// uint16_t p;
// p = core->size * y + x;
// return core->data[p];
// }
int tlb_core_standard(tlb_core_t * core)
{
uint16_t real_size;
uint16_t index;
double sum = 0;
real_size = core->size * core->size;
for(index = 0; index < real_size; index++){
sum += core->data[index];
}
if(sum < -0.000001){
core->div = -sum;
core->bias = 255;
}else if(sum < 0.000001){
/* core->div == 0 */
core->div = 1.0;
core->bias = 128;
}else{
core->div = sum;
}
return TLB_OK;
}
/***************/
/* Image utils */
/***************/
tlb_image_t * tlb_img_new(uint32_t width, uint32_t height, color_t bgcolor)
{
tlb_image_t * image = NULL;
uint32_t * data;
uint32_t index;
/* get memory for image */
image = malloc(sizeof(tlb_image_t));
/* malloc exceptional */
if(image == NULL){
fprintf(stderr, "Error: Can not malloc for new object.\n");
return NULL;
}
/* set width of struct */
image->width = width;
/* set height of struct */
image->height = height;
/* malloc for cooked img data */
image->data = malloc((4*image->width*image->height)*sizeof(uint8_t));
if(image->data == NULL){
fprintf(stderr, "Error: Can not malloc for new object.\n");
return NULL;
}
data = (uint32_t*)(image->data);
for(index = 0; index < width*height; index++)
{
data[index] = bgcolor;
}
return image;
}
void tlb_img_free(tlb_image_t * image)
{
if(image != NULL){
s_free(image->data);
s_free(image);
}
}
tlb_image_t * tlb_img_copy(tlb_image_t * src_image)
{
tlb_image_t * image = NULL;
if(src_image==NULL){
fprintf(stderr, "%s(): Cannot operation with (null) image.\n",__FUNCTION__);
return NULL;
}
/* get memory for image */
image = malloc(sizeof(tlb_image_t));
/* malloc exceptional */
if(image == NULL){
fprintf(stderr, "Error: Can not malloc for new object.\n");
return NULL;
}
/* set width of struct */
image->width = src_image->width;
/* set height of struct */
image->height = src_image->height;
/* malloc for cooked img data */
image->data = malloc((4*image->width*image->height)*sizeof(char));
if(image->data == NULL){
fprintf(stderr, "Error: Can not malloc for new object.\n");
return NULL;
}
memcpy(image->data, src_image->data, (4*image->width*image->height));
return image;
}
tlb_image_t * tlb_img_chop(tlb_image_t * image, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1)
{
tlb_image_t * chop = NULL;
uint32_t x,y;
if(image==NULL){
fprintf(stderr, "%s(): Cannot operation with (null) image.\n",__FUNCTION__);
return NULL;
}
if((x0==x1)||(y0==y1)){
fprintf(stderr, "Error: Selected area too small.\n");
return NULL;
}
/* make point 1 on left-down and point 2 on right-up */
if(x0 > x1) tlb_swap(x0, x1);
if(y0 > y1) tlb_swap(y0, y1);
/* adjust for large area */
x1 = (x1>image->width) ? image->width : x1 ;
y1 = (y1>image->height) ? image->height : y1 ;
chop = tlb_img_new(x1-x0, y1-y0, 0);
for (x = x0; x < x1; x++){
for (y = y0; y < y1; y++){
*(uint32_t*)tlb_pixel(chop, x-x0, y-y0) = *(uint32_t*)tlb_pixel(image, x, y);
}
}
return chop;
}
int tlb_img_paste(tlb_image_t * src, tlb_image_t * dst, uint32_t x0, uint32_t y0){
uint32_t xs,ys;
uint32_t xd,yd;
/* check if NULL pointer */
if(dst==NULL||src==NULL){
fprintf(stderr, "%s(): Cannot operation with (null) image.\n",__FUNCTION__);
return TLB_ERROR;
}
ys = 0;
yd = y0;
while((ys<src->height)&&(yd<dst->height)){
xs = 0;
xd = x0;
while((xs<src->width)&&(xd<dst->width)){
/* copy by uint32_t */
*(uint32_t*)tlb_pixel(dst, xd, yd) = *(uint32_t*)tlb_pixel(src, xs, ys);
xs++;
xd++;
}
ys++;
yd++;
}
return TLB_OK;
}
tlb_image_t * tlb_img_make_border(tlb_image_t * image, uint32_t brsize, uint8_t type){
tlb_image_t * border = NULL;
uint32_t x,y;
/* check if NULL pointer */
if(image==NULL){
fprintf(stderr, "%s(): Cannot operation with (null) image.\n",__FUNCTION__);
return NULL;
}
/* if brsize==0 it degenerate to tlb_img_copy */
if(brsize==0){
return tlb_img_copy(image);
}
border = tlb_img_new(image->width+(2*brsize),image->height+(2*brsize),0);
tlb_img_paste(image, border, brsize, brsize);
if(type == TLB_BORDER_EMPTY){
return border;
}
if(type == TLB_BORDER_REPLICATE){
/* up and down */
for(x = brsize; x < image->width+brsize; x++){
for(y = 0; y < brsize; y++){
*(uint32_t*)tlb_pixel(border, x, y) =\
*(uint32_t*)tlb_pixel(border, x, brsize);
*(uint32_t*)tlb_pixel(border, x, border->height - y - 1) =\
*(uint32_t*)tlb_pixel(border, x, border->height - brsize - 1);
}
}
/* left and right */
for(y = brsize; y < image->height+brsize; y++){
for(x = 0; x < brsize; x++){
*(uint32_t*)tlb_pixel(border, x, y) =\
*(uint32_t*)tlb_pixel(border, brsize, y);
*(uint32_t*)tlb_pixel(border, border->width - x - 1, y) =\
*(uint32_t*)tlb_pixel(border, border->width - brsize - 1, y);
}
}
/* corner */
for(y = 0; y < brsize; y++){
for(x = 0; x < brsize; x++){
*(uint32_t*)tlb_pixel(border, x, y) =\
*(uint32_t*)tlb_pixel(border, brsize, brsize);
*(uint32_t*)tlb_pixel(border, border->width-x-1, y) =\
*(uint32_t*)tlb_pixel(border, border->width-brsize-1, brsize);
*(uint32_t*)tlb_pixel(border, x, border->height-y-1) =\
*(uint32_t*)tlb_pixel(border, brsize, border->height-brsize-1);
*(uint32_t*)tlb_pixel(border, border->width-x-1, border->height-y-1) =\
*(uint32_t*)tlb_pixel(border, border->width-brsize-1, border->height-brsize-1);
}
}
return border;
}
fprintf(stderr, "%s(): need to specific a border type, treated as TLB_BORDER_EMPTY.\n",__FUNCTION__);
return border;
}
tlb_image_t * tlb_img_conv_r(tlb_image_t * image, tlb_core_t * core, uint8_t channel){
tlb_image_t * conv = NULL;
uint32_t x0;
uint32_t y0;
uint32_t x1;
uint32_t y1;
uint32_t d;
double tmp[4];
if(image==NULL||core==NULL){
fprintf(stderr, "%s(): Cannot operation with (null) image.\n",__FUNCTION__);
return NULL;
}
d = (core->size / 2);
conv = tlb_img_copy(image);
if(channel!=CHANNEL_ALL){
for(x0 = 0; x0<image->width-core->size; x0++){
for(y0 = 0; y0<image->height-core->size; y0++){
/* ergodic pixel in image */
tmp[0] = 0;
for(x1 = 0; x1<core->size; x1++){
for(y1 = 0; y1<core->size; y1++){
tmp[0] += tlb_core_element(core, x1, y1) * (tlb_pixel(image, x0+x1, y0+y1)[channel]);
}
}
tmp[0] = tmp[0]/core->div + core->bias;
if (tmp[0]>255) tmp[0] = 255;
if (tmp[0]<0) tmp[0] = 0;
tlb_pixel(conv, x0+d, y0+d)[channel] = tmp[0];
}
}
}else{
for(x0 = 0; x0<image->width-core->size; x0++){
for(y0 = 0; y0<image->height-core->size; y0++){
/* ergodic pixel in image */
tmp[CHANNEL_R] = 0;
tmp[CHANNEL_G] = 0;
tmp[CHANNEL_B] = 0;
tmp[CHANNEL_A] = 0;
for(x1 = 0; x1<core->size; x1++){
for(y1 = 0; y1<core->size; y1++){
tmp[CHANNEL_R] += tlb_core_element(core, x1, y1) * (tlb_pixel(image, x0+x1, y0+y1)[CHANNEL_R]);
tmp[CHANNEL_G] += tlb_core_element(core, x1, y1) * (tlb_pixel(image, x0+x1, y0+y1)[CHANNEL_G]);
tmp[CHANNEL_B] += tlb_core_element(core, x1, y1) * (tlb_pixel(image, x0+x1, y0+y1)[CHANNEL_B]);
tmp[CHANNEL_A] += tlb_core_element(core, x1, y1) * (tlb_pixel(image, x0+x1, y0+y1)[CHANNEL_A]);
}
}
tmp[CHANNEL_R] = tmp[CHANNEL_R]/core->div + core->bias;
tmp[CHANNEL_G] = tmp[CHANNEL_G]/core->div + core->bias;
tmp[CHANNEL_B] = tmp[CHANNEL_B]/core->div + core->bias;
tmp[CHANNEL_A] = tmp[CHANNEL_A]/core->div + core->bias;
if (tmp[CHANNEL_R]>255) tmp[CHANNEL_R] = 255;
if (tmp[CHANNEL_G]>255) tmp[CHANNEL_G] = 255;
if (tmp[CHANNEL_B]>255) tmp[CHANNEL_B] = 255;
if (tmp[CHANNEL_A]>255) tmp[CHANNEL_A] = 255;
if (tmp[CHANNEL_R]<0) tmp[CHANNEL_R] = 0;
if (tmp[CHANNEL_G]<0) tmp[CHANNEL_G] = 0;
if (tmp[CHANNEL_B]<0) tmp[CHANNEL_B] = 0;
if (tmp[CHANNEL_A]<0) tmp[CHANNEL_A] = 0;
tlb_pixel(conv, x0+d, y0+d)[CHANNEL_R] = tmp[CHANNEL_R];
tlb_pixel(conv, x0+d, y0+d)[CHANNEL_G] = tmp[CHANNEL_G];
tlb_pixel(conv, x0+d, y0+d)[CHANNEL_B] = tmp[CHANNEL_B];
tlb_pixel(conv, x0+d, y0+d)[CHANNEL_A] = tmp[CHANNEL_A];
}
}
}
return conv;
}
tlb_image_t * tlb_img_conv(tlb_image_t * image, tlb_core_t * core, uint8_t channel){
tlb_image_t * border = NULL;
tlb_image_t * r_conv = NULL;
tlb_image_t * c_conv = NULL;
if(image==NULL){
fprintf(stderr, "%s(): Cannot operation with (null) image.\n",__FUNCTION__);
return NULL;
}
if(core==NULL){
fprintf(stderr, "%s(): Cannot operation with (null) conv core.\n",__FUNCTION__);
return NULL;
}
if(core->div==0){
fprintf(stderr, "%s(): Illegal conv core.\n",__FUNCTION__);
return NULL;
}
border = tlb_img_make_border(image, core->size, TLB_BORDER_REPLICATE);
if(border==NULL){
fprintf(stderr, "%s(): Cannot do copy and make border operation.\n",__FUNCTION__);
return NULL;
}
r_conv = tlb_img_conv_r(border, core, channel);
tlb_img_free(border);
if(r_conv==NULL){
fprintf(stderr, "%s(): Cannot do convolution operation.\n",__FUNCTION__);
return NULL;
}
c_conv = tlb_img_chop(r_conv, core->size, core->size, image->width + core->size, image->height + core->size);
tlb_img_free(r_conv);
if(c_conv==NULL){
fprintf(stderr, "%s(): Cannot do chop operation.\n",__FUNCTION__);
return NULL;
}
return c_conv;
}
/*******************/
/* pixel operation */
/*******************/
/* get a array of the pixel */
uint8_t * tlb_pixel(tlb_image_t * image, uint32_t x, uint32_t y){
/* Check the input legitimacy */
if(x > image->width){
return NULL;
}
if(y > image->height){
return NULL;
}
return (image->data + 4*(y * image->width + x));
}
/* print a pixel with specific color */
int tlb_pixel_set(tlb_image_t * image, uint32_t x, uint32_t y, color_t color){
if(x > image->width){
return TLB_ERROR;
}
if(y > image->height){
return TLB_ERROR;
}
*(uint32_t*)(image->data + 4*(y * image->width + x)) = color;
return TLB_OK;
}
/* print a pixel with specific color */
int tlb_pixel_ch_set(tlb_image_t * image, uint32_t x, uint32_t y, uint8_t channel, uint8_t val){
if(x > image->width){
return TLB_ERROR;
}
if(y > image->height){
return TLB_ERROR;
}
(image->data + 4*(y * image->width + x))[channel] = val;
return TLB_OK;
}
/*******************/
/* block operation */
/*******************/
/* get the average color of a block */
uint32_t tlb_block_average(tlb_image_t * image,\
uint32_t offset_x, uint32_t offset_y,\
uint32_t length_x, uint32_t length_y)
{
uint32_t color_sum[4] = {0};
uint8_t color_ave[4] = {0};
uint32_t pixel_x = 0;
uint32_t pixel_y = 0;
uint8_t* pixel = NULL;
uint32_t block_size = length_x * length_y;
/* select a pixel in block */
for (pixel_x = 0; pixel_x < length_x; pixel_x++){
for (pixel_y = 0; pixel_y < length_y; pixel_y++){
/* get the pointer of the pixel */
pixel = tlb_pixel(image, offset_x + pixel_x, offset_y + pixel_y);
if (pixel != NULL){
color_sum[CHANNEL_R] += pixel[CHANNEL_R];
color_sum[CHANNEL_G] += pixel[CHANNEL_G];
color_sum[CHANNEL_B] += pixel[CHANNEL_B];
color_sum[CHANNEL_A] += pixel[CHANNEL_A];
}
}
}
color_ave[CHANNEL_R] = color_sum[CHANNEL_R] / block_size;
color_ave[CHANNEL_G] = color_sum[CHANNEL_G] / block_size;
color_ave[CHANNEL_B] = color_sum[CHANNEL_B] / block_size;
color_ave[CHANNEL_A] = color_sum[CHANNEL_A] / block_size;
return tlb_rgba(color_ave[CHANNEL_R],\
color_ave[CHANNEL_G],\
color_ave[CHANNEL_B],\
color_ave[CHANNEL_A]);
}
/* fill a block with a specific color */
int tlb_block_fill(tlb_image_t * image,\
uint32_t offset_x, uint32_t offset_y,\
uint32_t length_x, uint32_t length_y,\
color_t color)
{
uint32_t pixel_x = 0;
uint32_t pixel_y = 0;
uint32_t* pixel = NULL;
/* select a pixel in block */
for (pixel_x = 0; pixel_x < length_x; pixel_x++){
for (pixel_y = 0; pixel_y < length_y; pixel_y++){
/* get the pointer of the pixel */
pixel = (uint32_t*)tlb_pixel(image, offset_x + pixel_x, offset_y + pixel_y);
if (pixel != NULL){
*pixel = color;
}
}
}
return TLB_OK;
}
tlb_image_t * tlb_block_mosaic(tlb_image_t * image,\
uint32_t offset_x, uint32_t offset_y,\
uint32_t length_x, uint32_t length_y,\
uint32_t granularity)
{
tlb_image_t * mosaic = NULL;
uint32_t color = 0;
uint32_t block_x = 0;
uint32_t block_y = 0;
mosaic = tlb_img_copy(image);
for (block_x = granularity; block_x < length_x; block_x+=granularity){
for (block_y = granularity; block_y < length_y; block_y+=granularity){
color = tlb_block_average(mosaic,\
block_x + offset_x,\
block_y + offset_y,\
granularity,\
granularity);
tlb_block_fill(mosaic,\
block_x + offset_x,\
block_y + offset_y,\
granularity,\
granularity,\
color);
}
}
return mosaic;
}
/********************/
/* drowing in image */
/********************/
int tlb_draw_line(tlb_image_t * image,\
uint32_t x0, uint32_t y0, \
uint32_t x1, uint32_t y1, \
color_t color)
{
/* I have use a lot of inline sentences in this function */
/* it was not for speed, just bucause I'm lazy */
uint8_t steep = 0;
uint32_t x;
uint32_t y;
uint32_t dx;
uint32_t dy;
float t;
/* inline abs(x0-x1) */
if(x0 > x1){
dx = x0 - x1;
}else{
dx = x1 - x0;
}
/* inline abs(y0-y1) */
if(y0 > y1){
dy = y0 - y1;
}else{
dy = y1 - y0;
}
if (dx<dy&&dx!=0&&dy!=0&&dx!=dy){
/* if the line is steep, rotate the canvas */
tlb_swap(x0, y0);
tlb_swap(x1, y1);
steep = 1;
}
if (x0>x1) {
/* make line from left to right */
tlb_swap(x0, x1);