-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathegi_image.c
7041 lines (5919 loc) · 217 KB
/
egi_image.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
/*------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
NOTE: Try not to use EGI_PDEBUG() here!
EGI_IMGBUF functions
Jurnal
2021-02-22:
1. Modify egi_imgbuf_resetColorAlpha().
2021-04-20:
1. Add param keep_ratio for egi_imgbuf_resize() AND egi_imgbuf_resize_update().
2021-06-05:
1. Modify egi_imgbuf_resize(): if ineimg and outeimg is same size, then goto CREATE_OUTEIMG.
2. Add egi_imgbuf_resize_nolock().
3. Add egi_imgbuf_scale(), egi_imgbuf_scale_update().
2021-08-21/22:
1. Add egi_imgbuf_mapTriWriteFB()
2. Add egi_imgbuf_mapTriWriteFB2()
2021-08-25:
1. Add egi_imgbuf_uvToPixel();
2021-08-27:
XXX 1. egi_imgbuf_mapTriWriteFB2():
XXX 1.1 Check/make barycentric factor a/b/r within [0.0 1.0].
XXX 1.2 Check/make u/v within [0.0 1.0]
XXX 2. egi_imgbuf_uvToPixel(): Check/make u/v within [0.0 1.0].
2021-09-08:
1. Add egi_imgbuf_mapTriWriteFB3().
2021-09-09:
1. Improve egi_imgbuf_mapTriWriteFB3(): interpolation u/v for line pixel.
2. Add egi_subimg_serialWriteFB().
2021-09-10:
1. Add egi_imgmotion_saveHeader()
2. Add egi_imgmotion_saveFrame()
3. Add egi_imgmotion_playFile()
2021-09-13
1. Zlib compress/uncompress IMGMOTION,
and update egi_imgmotion_xxxx() functions accordingly.
2021-09-24:
1. egi_imgbuf_mapTriWriteFB3(): Apply fb_dev->pixalpha.
2021-09-27:
1. egi_imgbuf_mapTriWriteFB3(): If u/v<0, make it positive u/v=1.0+u/v. ?????
2021-12-06:
1. Add egi_imgbuf_get_fitsize()
2022-02-10:
1. egi_imgbuf_mapTriWriteFB3(): with input params of float z0,z1,z2 to compute pixZ
for each pixel.
2022-03-22:
1. egi_imgbuf_readfile(): Call egi_simpleCheck_jpgfile() to identify JPEG or PNG.
2022-04-05:
1. egi_imgbuf_resize_nolock(): Create a transition imgbuf to improve scaledown precision.
2022-04-06:
1. Add egi_imgbuf_rotBlockCopy2(): float type angle and 4p interpolation.
2022-04-08:
1. Add egi_imgbuf_createLinkFBDEV() and egi_imgbuf_freeLinkFBDEV().
2022-05-05:
1. Add egi_imgbuf_getColor().
2022-05-08:
1. Add egi_imgbuf_blockResetColorAlpha().
2023-03-06:
1. Add egi_imgmotion_mergeFiles()
2023-04-24:
1. Add egi_imgbuf_gammaCorrect()
Midas Zhou
[email protected](Not in use since 2022_03_01)
-------------------------------------------------------------------*/
#include <pthread.h>
#include <math.h>
#include <sys/time.h>
#include "egi_image.h"
#include "egi_bjp.h"
#include "egi_utils.h"
#include "egi_log.h"
#include "egi_math.h"
#include "egi_log.h"
#include "egi_timer.h"
#include "egi_debug.h"
#include "egi_matrix.h"
typedef struct fbdev FBDEV; /* Just a declaration, referring to definition in egi_fbdev.h */
/*--------------------------------------------
Allocate an EGI_IMGBUF
Allocate an EGI_IMGBUF and init img_mutex.
---------------------------------------------*/
EGI_IMGBUF *egi_imgbuf_alloc(void) //new(void)
{
EGI_IMGBUF *eimg;
eimg=calloc(1, sizeof(EGI_IMGBUF));
if(eimg==NULL) {
printf("%s: Fail to calloc EGI_IMGBUF.\n",__func__);
return NULL;
}
/* init imgbuf mutex */
if(pthread_mutex_init(&eimg->img_mutex,NULL) != 0)
{
printf("%s: fail to initiate img_mutex.\n",__func__);
free(eimg);
return NULL;
}
return eimg;
}
/*-----------------------------------------
Allocate an array of EGI_BOX
@n: Size of EGI_BOX array.
Return:
Pointer to an array of EGI_BOX OK
NULL Fails
-----------------------------------------*/
EGI_IMGBOX *egi_imgboxes_alloc(int n)
{
EGI_IMGBOX* boxes;
if(n<=0)
return NULL;
boxes=calloc(n, sizeof(EGI_IMGBOX));
return boxes;
}
/*-----------------------------------------
Free an array of EGI_BOX
@n: Size of EGI_BOX array.
-----------------------------------------*/
void egi_imgboxes_free(EGI_IMGBOX *imboxes)
{
free(imboxes);
}
/*------------------------------------------------------------
Free data in EGI_IMGBUF, but NOT itself!
NOTE:
1. WARNING!!!: No mutex operation here, the caller shall take
care of imgbuf mutex lock.
-------------------------------------------------------------*/
void egi_imgbuf_cleardata(EGI_IMGBUF *egi_imgbuf)
{
if(egi_imgbuf != NULL) {
if(egi_imgbuf->imgbuf != NULL) {
free(egi_imgbuf->imgbuf);
egi_imgbuf->imgbuf=NULL;
}
if(egi_imgbuf->alpha != NULL) {
free(egi_imgbuf->alpha);
egi_imgbuf->alpha=NULL;
}
if(egi_imgbuf->data != NULL) {
free(egi_imgbuf->data);
egi_imgbuf->data=NULL;
}
if(egi_imgbuf->subimgs != NULL) {
free(egi_imgbuf->subimgs);
egi_imgbuf->subimgs=NULL;
}
if(egi_imgbuf->pcolors !=NULL) {
egi_free_buff2D((unsigned char **)egi_imgbuf->pcolors, egi_imgbuf->height);
egi_imgbuf->pcolors=NULL;
}
if(egi_imgbuf->palphas !=NULL) {
egi_free_buff2D(egi_imgbuf->palphas, egi_imgbuf->height);
egi_imgbuf->palphas=NULL;
}
/* reset size and submax */
egi_imgbuf->height=0;
egi_imgbuf->width=0;
egi_imgbuf->submax=0;
}
/* Only clear data !!!!DO NOT free egi_imgbuf itself ; */
}
/*------------------------------------------
Free EGI_IMGBUF and its data
The caller MUST reset egi_imgbuf to NULL!!!
-------------------------------------------*/
void egi_imgbuf_free(EGI_IMGBUF *egi_imgbuf)
{
if(egi_imgbuf == NULL)
return;
/* Hope there is no other user */
if(pthread_mutex_lock(&egi_imgbuf->img_mutex) !=0 )
EGI_PLOG(LOGLV_TEST,"%s:Fail to lock img_mutex!\n",__func__);
/* !MOVE TO egi_imgbuf_cleardata(), free 2D array data if any */
//egi_free_buff2D((unsigned char **)egi_imgbuf->pcolors, egi_imgbuf->height);
//egi_free_buff2D(egi_imgbuf->palphas, egi_imgbuf->height);
/* free data inside */
egi_imgbuf_cleardata(egi_imgbuf);
#if 1 /* ??????? necesssary ????? */
/* "It shall be safe to destroy an initialized mutex that is unlocked. Attempting to
* destroy a locked mutex results in undefined behavior" --- POSIX man/Linux man 3
*/
if( pthread_mutex_unlock(&egi_imgbuf->img_mutex) != 0)
EGI_PLOG(LOGLV_TEST,"%s:Fail to unlock img_mutex!\n",__func__);
#endif
/* Destroy thread mutex lock for page resource access */
if(pthread_mutex_destroy(&egi_imgbuf->img_mutex) !=0 )
EGI_PLOG(LOGLV_TEST,"%s:Fail to destroy img_mutex!. Err'%s'. \n",__func__, strerror(errno));
/* Err'No message of desired type'
Fail to destroy img_mutex!. Err'File exists'.??? */
/* NOTE: It seems a previous ERROR/errno will affect pthread_mutex_destroy()!? */
free(egi_imgbuf);
// egi_imgbuf=NULL; /* ineffective though...*/
}
/*------------------------------------------
Free EGI_IMGBUF and itsef
-------------------------------------------*/
void egi_imgbuf_free2(EGI_IMGBUF **pimg)
{
if(pimg==NULL || *pimg==NULL)
return;
egi_imgbuf_free(*pimg);
*pimg=NULL;
}
/*-----------------------------------------------------------------------------
Initiate/alloc imgbuf as an image canvas, with all alpha=0!!!
If egi_imgbuf has old data, then it will be cleared/freed first.
NOTE:
1. !!!!WARNING!!!: No mutex operation here, the caller shall take
care of imgbuf mutex lock.
@height height of image
@width width of image
@AlphaON If ture, egi_imgbuf->alpha will be allocated.
Return:
0 OK
<0 Fails
-----------------------------------------------------------------------------*/
int egi_imgbuf_init(EGI_IMGBUF *egi_imgbuf, int height, int width, bool AlphaON)
{
if(egi_imgbuf==NULL)
return -1;
if(height<=0 || width<=0)
return -2;
/* Empty old data if any */
egi_imgbuf_cleardata(egi_imgbuf);
/* Calloc imgbuf->imgbuf */
egi_imgbuf->imgbuf = calloc(1,height*width*sizeof(EGI_16BIT_COLOR));
if(egi_imgbuf->imgbuf == NULL) {
egi_dperr("Fail to calloc egi_imgbuf->imgbuf.");
return -2;
}
/* Calloc imgbuf->alpha, alpha=0, 100% canvas color. */
if(AlphaON) {
egi_imgbuf->alpha= calloc(1, height*width*sizeof(unsigned char)); /* alpha value 8bpp */
if(egi_imgbuf->alpha == NULL) {
egi_dperr("Fail to calloc egi_imgbuf->alpha.");
free(egi_imgbuf->imgbuf);
return -3;
}
}
/* Retset height and width for imgbuf */
egi_imgbuf->height=height;
egi_imgbuf->width=width;
return 0;
}
/*---------------------------------------------------------
Gamma correction for imgbuf RGB values.
@imgbuf: An pointer to EGI_IMGBUF
@gampow: Gamma pow value used for correction.
as vo=vi^(gampow) (vo/vi: normalized value)
Normally 2.2 OR 1.0/2.2.
Return:
0 OK
<0 Fails
----------------------------------------------------------*/
int egi_imgbuf_gammaCorrect(EGI_IMGBUF *imgbuf, float gampow)
{
if(imgbuf==NULL || imgbuf->imgbuf==NULL)
return -1;
static unsigned char LookUpTab[256]; /* Map from RGB value 'index' to Gammacorrected value 'LookUpTab[index]' */
int i,j;
float fn; /* Normalized value */
EGI_16BIT_COLOR color;
unsigned char R,G,B;
int pos;
/* Build LookUpTable */
for(i=0; i<256; i++) {
fn=(i+0.5)/256; /* Normalize */
fn=powf(fn, gampow);
LookUpTab[i]=fn*256-0.5;
}
/* Make Gamma Correction for each RGB */
for(i=0; i< imgbuf->height; i++) {
for(j=0; j< imgbuf->width; j++) {
pos=i*imgbuf->height+j;
color=imgbuf->imgbuf[pos];
R=COLOR_R8_IN16BITS(color);
G=COLOR_G8_IN16BITS(color);
B=COLOR_B8_IN16BITS(color);
R=LookUpTab[R];
G=LookUpTab[G];
B=LookUpTab[B];
imgbuf->imgbuf[pos]=COLOR_RGB_TO16BITS(R,G,B);
}
}
return 0;
}
/*-----------------------------------------------
To get color/alpha value for the specified point.
@imgbuf: An pointer to EGI_IMGBUF
@px,py: Point in the imgbuf.
@color,alpha Pointers to pass out values.
Return:
0 OK
<0 Fails
-----------------------------------------------*/
int egi_imgbuf_getColor(EGI_IMGBUF *imgbuf, int px, int py, EGI_16BIT_COLOR *color, EGI_8BIT_ALPHA *alpha)
{
if(imgbuf==NULL || imgbuf->imgbuf==NULL)
return -1;
if(px<0 || px>imgbuf->width-1 || py<0 || py>imgbuf->height-1)
return -1;
if(color)
*color=imgbuf->imgbuf[py*imgbuf->width+px];
if(alpha && imgbuf->alpha)
*alpha=imgbuf->alpha[py*imgbuf->width+px];
return 0;
}
/*--------------------------------------------------------------------------------
Modify color/alpha data of an EGI_IMGBUF to set an bounding rectangle(limit box)
with given color and width.
@ineimg: pointer to an EGI_IMGBUF.
@color: Color value for boundary lines.
@lw: width of boundary lines, in pixels.
Return:
0 OK
<0 Fails
--------------------------------------------------------------------------------*/
int egi_imgbuf_addBoundaryBox(EGI_IMGBUF *ineimg, EGI_16BIT_COLOR color, int lw)
{
int i,j;
if(ineimg==NULL || ineimg->imgbuf==NULL)
return -1;
/* limit wl */
if(lw <= 0 )
lw=1;
if(lw > ineimg->width/2 )
lw=ineimg->width/2;
if(lw > ineimg->height/2 )
lw=ineimg->height/2;
/* set upper boundary */
for(i=0; i < lw*(ineimg->width); i++) {
ineimg->imgbuf[i]=color;
if(ineimg->alpha)
ineimg->alpha[i]=255;
}
/* set bottom boundary */
for( i=(ineimg->height-lw)*(ineimg->width); i < (ineimg->height)*(ineimg->width); i++ ) {
ineimg->imgbuf[i]=color;
if(ineimg->alpha)
ineimg->alpha[i]=255;
}
/* set left and right boundary */
for(i=0; i < ineimg->height; i++) {
for(j=0; j<lw; j++) {
ineimg->imgbuf[i*(ineimg->width)+j]=color; /* left */
ineimg->imgbuf[(i+1)*(ineimg->width)-1-j]=color; /* right */
if(ineimg->alpha) {
ineimg->alpha[i*(ineimg->width)+j]=255; /* left */
ineimg->alpha[(i+1)*(ineimg->width)-1-j]=255; /* right */
}
}
}
return 0;
}
/* ------------------------------------------------------
Create an EGI_IMGBUF, set color and alpha value.
@height,width: height and width of the imgbuf.
@alpha: >0, alpha value for all pixels.
(else, default 0)
@color: >0 basic color of the imgbuf.
(else, default 0)
Return:
A pointer to EGI_IMGBUF OK
FULL Fails
-------------------------------------------------------*/
EGI_IMGBUF *egi_imgbuf_create( int height, int width,
unsigned char alpha, EGI_16BIT_COLOR color )
{
int i;
EGI_IMGBUF *imgbuf=egi_imgbuf_alloc();
if(imgbuf==NULL)
return NULL;
/* init the struct, initial color and alpha all to be 0! */
if ( egi_imgbuf_init(imgbuf, height, width, true) !=0 )
return NULL;
/* set alpha and color */
if(alpha>0) /* alpha default calloced as 0 already */
memset( imgbuf->alpha, alpha, height*width );
if(color>0) { /* color default calloced as 0 already */
for(i=0; i< height*width; i++)
*(imgbuf->imgbuf+i)=color;
}
return imgbuf;
}
/* -----------------------------------------------------------
Create an EGI_IMGBUF without alpha space.
@height,width: height and width of the imgbuf.
@color: >0 basic color of the imgbuf.
(else, default 0)
Return:
A pointer to EGI_IMGBUF OK
FULL Fails
---------------------------------------------------------------*/
EGI_IMGBUF *egi_imgbuf_createWithoutAlpha(int height, int width, EGI_16BIT_COLOR color)
{
int i;
EGI_IMGBUF *imgbuf=egi_imgbuf_alloc();
if(imgbuf==NULL)
return NULL;
/* init the struct, initial color and alpha all to be 0! */
if ( egi_imgbuf_init(imgbuf, height, width, false) !=0 )
return NULL;
/* set alpha and color */
if(color>0) { /* color default calloced as 0 already */
for(i=0; i< height*width; i++)
*(imgbuf->imgbuf+i)=color;
}
return imgbuf;
}
/*--------------------------------------------------------
Allocate eimg->subimgs and set eimg->submax. and it will
NOT check eimg->imgbuf.
All eimg->subimgs[] shall be assigned properly after this.
@eimg: Pointer to an EGI_IMGBUF
@num: Total number of subimgs
Return:
0 OK
<0 Fail
-------------------------------------------------------*/
int egi_imgbuf_setSubImgs(EGI_IMGBUF *eimg, int num)
{
if(eimg==NULL)
return -1;
/* Allocate sumbimgs */
eimg->subimgs=egi_imgboxes_alloc(num);
if(eimg->subimgs==NULL)
return -2;
/* Assign submax */
eimg->submax=num-1;
return 0;
}
/*-------------------------------------------------------
Create an EGI_IMGBUF whose imgbuf refers/links to FBDEV buffer.
Call egi_imgbuf_freeLinkFBDEV() to free it!
Note:
1. Only a pointer reference to FBDEV image data! NO synch mechanism!
@fbdev: An pointer to FBDEV
Return:
!NULL OK
NULL Fails
-------------------------------------------------------*/
EGI_IMGBUF *egi_imgbuf_createLinkFBDEV(const FBDEV *fbdev)
{
EGI_IMGBUF *fbimg=egi_imgbuf_alloc();
if(fbimg==NULL)
return NULL;
fbimg->width=fbdev->pos_xres;
fbimg->height=fbdev->pos_yres;
/* Link imgbuf data */
fbimg->imgbuf=(EGI_16BIT_COLOR*)fbdev->map_fb;
return fbimg;
}
/*------------------------------------------------------
Free an imgbuf whose imgbuf refers/links to FBDEV buffer.
MUST NOT to free linked data memory!
------------------------------------------------------*/
void egi_imgbuf_freeLinkFBDEV(EGI_IMGBUF **imgbuf)
{
if(imgbuf==NULL || *imgbuf==NULL)
return;
/* Hope there is no other user */
if(pthread_mutex_lock(&(*imgbuf)->img_mutex) !=0 ) {
EGI_PLOG(LOGLV_TEST,"%s:Fail to lock img_mutex!\n",__func__);
return;
}
/* Destroy thread mutex lock */
if(pthread_mutex_destroy(&(*imgbuf)->img_mutex) !=0 ) {
EGI_PLOG(LOGLV_TEST,"%s:Fail to destroy img_mutex!. Err'%s'. \n",__func__, strerror(errno));
return;
}
free(*imgbuf);
*imgbuf=NULL;
}
/*--------------------------------------------------------------
Read an image file and load data to an EGI_IMGBUF as for return.
@fpath: Full path to an image file.
Supports only JPG and PNG currently.
Return:
A pointer to EGI_IMGBUF Ok
NULL Fails
----------------------------------------------------------------*/
EGI_IMGBUF *egi_imgbuf_readfile(const char* fpath)
{
//if(fpath==NULL) /* check fpath in egi_imgbuf_loadjpg() */
// return NULL;
EGI_IMGBUF *eimg=egi_imgbuf_alloc();
if(eimg==NULL)
return NULL;
#if 0
if( egi_imgbuf_loadjpg(fpath, eimg)!=0 ) {
if ( egi_imgbuf_loadpng(fpath, eimg)!=0 ) {
egi_imgbuf_free2(&eimg);
return NULL;
}
}
#else
/* PNG File */
if(egi_simpleCheck_jpgfile(fpath)==JPEGFILE_INVALID) {
if ( egi_imgbuf_loadpng(fpath, eimg)!=0 ) {
egi_imgbuf_free2(&eimg);
return NULL;
}
}
/* JPEG File */
else {
if ( egi_imgbuf_loadjpg(fpath, eimg)!=0 ) {
egi_imgbuf_free2(&eimg);
return NULL;
}
}
#endif
/* TODO: some png file with 0x0 image size HK2023-03-01 */
if( eimg->width<1 || eimg->height<1) {
egi_dpstd(DBG_RED"Imgbuf size error W*H=%d*%d\n"DBG_RESET, eimg->width, eimg->height);
egi_imgbuf_free2(&eimg);
return NULL;
}
return eimg;
}
/*----------------------------------------------------------------
Copy a block of image from input EGI_IMGBUF, and create
a new EGI_IMGBUF to hold the data.
Only color/alpha of ineimg will be copied to outeimg, other members
such as subimg will be ignored.
@ineimg: The original image from which we'll copy an image block.
@px,py: Coordinates of the origin for the wanted image block,
relative to ineimg image coords.
@height: Height of the image block
@width: Width of the image block
Return:
A pointer to EGI_IMGBUF Ok
NULL Fails
-----------------------------------------------------------------*/
EGI_IMGBUF *egi_imgbuf_blockCopy( const EGI_IMGBUF *ineimg,
int px, int py, int height, int width )
{
int i,j;
unsigned int indx,outdx;
EGI_IMGBUF *outeimg=NULL;
bool alpha_on;
if( ineimg==NULL || ineimg->imgbuf==NULL )
return NULL;
/* create a new imgbuf, h/w check inside. */
outeimg=egi_imgbuf_create( height, width, 255, 0); /* default alpha 255 */
if(outeimg==NULL) {
printf("%s: Fail to create outeimg!\n",__func__);
return NULL;
}
/* alpha ON/OFF */
if( ineimg->alpha != NULL )
alpha_on=true;
else {
alpha_on=false;
/* free it */
free(outeimg->alpha);
outeimg->alpha=NULL;
}
/* Copy color/alpha data */
for(i=0; i<height; i++) {
for(j=0; j<width; j++) {
/* Range check */
if( py+i < 0 || px+j < 0 ||
py+i >= ineimg->height || px+j >= ineimg->width ) {
continue;
}
outdx=i*width+j; /* data index for outeimg*/
indx=(py+i)*ineimg->width+(px+j); /* data index for ineimg */
/* copy data */
outeimg->imgbuf[outdx]=ineimg->imgbuf[indx];
if(alpha_on) {
outeimg->alpha[outdx]=ineimg->alpha[indx];
}
}
}
return outeimg;
}
/*---------------------------------------------------------------------
Copy a block from srcimg and paste it to destimg.
Note:
1. If destimg has alpha values,then copy it also. while if original
destimg dose not has alpha space, then allocate it first.
!!! WARNING !!!
It MAY allocate memory space for destimg->alpha!
2. The destination image and srouce image may be the same.
@destimg: The destination EGI_IMGBUF.
@srcimg: The source EGI_IMGBUF.
@blendON: True: Applicable only when srcimg has alpha values.
Pixels of two images will be blended together.
The copied pixels will be merged to destimg by callying
egi_16bitColor_blend(), where only destimg->alpha
takes effect, and alpha value of srcimg (if it has)
will not be changed.
False: Copied pixels will replace old pixels, with colors
and alphas (if srcimg has alphas.)
@xd, yd: Block left top point coord relative to destimg.
@xs, ys: Block left top point coord relative to srcimg.
@bw: Width of the copying block
@bh: Height of the copying block
Return:
0 Ok
<0 Fails
-----------------------------------------------------------------------*/
int egi_imgbuf_copyBlock( EGI_IMGBUF *destimg, const EGI_IMGBUF *srcimg, bool blendON,
int bw, int bh,
int xd, int yd, int xs, int ys )
{
int i,j;
int destimg_size; /* image size, in pixels */
//int srcimg_size;
int pos_dest; /* offset position */
int pos_src;
#if 0 /* TEST: ----- */
egi_dpstd("destimg(W%dxH%d), srcimg(W%dxH%d), bw*bh(%dx%d) (xd,yd)(%d,%d), (xs,ys)(%d,%d) \n",
destimg->width, destimg->height, srcimg->width, srcimg->height, bw, bh, xd,yd, xs,ys);
#endif
/* Check input */
if( destimg==NULL || destimg->imgbuf==NULL || srcimg==NULL || srcimg->imgbuf==NULL )
return -1;
/* check block size */
if( bw <=0 || bh <=0 )
return -1;
/* Assume width/heigth of EGI_IMGBUF are sane! */
/* Check (xd,yd) and (xs,ys), rule out situations that block does NOT cover any part of the image. */
if( xd+bw-1<0 || yd+bh-1<0 || xd > destimg->width-1 || yd > destimg->height-1 ) {
printf("%s: Block covers no part of destimg! please check positon xd,yd. \n",__func__);
return -2;
}
if( xs+bw-1<0 || ys+bh-1<0 || xs > srcimg->width-1 || ys > srcimg->height-1 ) {
printf("%s: Block covers no part of srcimg! please check postion xs,ys. \n",__func__);
return -2;
}
/* Get image size, in pixels */
//srcimg_size=srcimg->height*srcimg->width;
destimg_size=destimg->height*destimg->width;
#if 1 /* If srcimg has alpha values while destimg dose not, then allocate and memset with 255 */
if( srcimg->alpha != NULL && destimg->alpha == NULL ) {
egi_dpstd("Allocate alpha for destimg!\n");
destimg->alpha=calloc(1, destimg_size*sizeof(EGI_8BIT_ALPHA));
if(destimg->alpha==NULL) {
printf("%s: Fail to calloc destimg->alpha!\n",__func__);
return -3;
}
memset(destimg->alpha, 255, destimg_size*sizeof(EGI_8BIT_ALPHA));
}
#endif
/* Copy pixels */
for(i=0; i<bh; i++) {
//printf("i=%d dw=%d,dh=%d\n",i, destimg->width, destimg->height);
for(j=0; j<bw; j++) {
/* If pixel coord. out of srcimg */
if( xs+j < 0 || ys+i <0 )
continue;
else if( xs+j > srcimg->width-1 || ys+i > srcimg->height-1 )
continue;
/* If pixel coord. out of destimg */
if( xd+j < 0 || yd+i <0 )
continue;
else if( xd+j >destimg->width-1 || yd+i > destimg->height-1 )
continue;
//printf("i=%d, j=%d\n",i,j);
/* Get offset position of data */
pos_src=(ys+i)*srcimg->width+xs +j;
pos_dest=(yd+i)*destimg->width+xd +j;
/* copy color and alpha data */
if( blendON && srcimg->alpha && destimg->alpha ) { /* If need to blend together */
destimg->imgbuf[pos_dest]=egi_16bitColor_blend( srcimg->imgbuf[pos_src],
destimg->imgbuf[pos_dest], srcimg->alpha[pos_src]);
/* Alpha values of destimg NOT changes */
}
else {
destimg->imgbuf[pos_dest]=srcimg->imgbuf[pos_src];
if(srcimg->alpha)
destimg->alpha[pos_dest]=srcimg->alpha[pos_src];
}
}
}
return 0;
}
/*-------------------------------------------------------------------
Copy a subimage from input EGI_IMGBUF, and create
a new EGI_IMGBUF to hold the data.
@eimg: An EGI_IMGBUF with subimges inside.
@index: Index of subimage, as of eimg->subimg[x]
Return:
A pointer to EGI_IMGBUF Ok
NULL Fails
--------------------------------------------------------------------*/
EGI_IMGBUF *egi_imgbuf_subImgCopy( const EGI_IMGBUF *eimg, int index )
{
/* Check input data */
if( eimg==NULL || eimg->subimgs==NULL || index<0 || index > eimg->submax ) {
printf("%s:Input eimg or subimage index is invalid!\n",__func__);
return NULL;
}
return egi_imgbuf_blockCopy( eimg, eimg->subimgs[index].x0, eimg->subimgs[index].y0,
eimg->subimgs[index].h, eimg->subimgs[index].w );
}
/*-----------------------------------------------------------
Set/create a frame for an EGI_IMGBUF.
The frame are formed by different patterns of alpha values.
Note:
1. Usually the frame is to be used as cutout_shape of an image.
2. If input eimg has alpha value:
2.1 If input alpha >=0, then modify eimg->alpha all to alpah.
2.2 If input alpha <0, no change for eimg->alpha.
3. If input eimg has no alpha value, then allocate it and:
3.1 If input alpha >=0, modify eimg->alpha all to be alpha
2.2 If input alpha <0, modify eimg->alpha all to be 255.
@eimg: An EGI_IMGBUF holding pixel colors.
@type: type/shape of the frame.
@alpha: >=0: Rset all eimg->alpha to be alpha before cutting frame.
<0: If input eimg has alpha value. ignore/no change.
If input eimg has no alpha value, allocate
and initiate with 255 before cutting frame.
@pn: numbers of paramters
@param: array of params
Return:
0 OK
<0 Fail
---------------------------------------------------------*/
int egi_imgbuf_setFrame( EGI_IMGBUF *eimg, enum imgframe_type type,
int alpha, int pn, const int *param )
{
int i,j;
if( pn<1 || param==NULL ) {
printf("%s: Input param invalid!\n",__func__);
return -1;
}
if( eimg==NULL || eimg->imgbuf==NULL ) {
printf("%s: Invali input eimg!\n",__func__);
return -1;
}
int height=eimg->height;
int width=eimg->width;
/* allocate alpha if NULL, and set alpha value */
if( eimg->alpha == NULL ) {
eimg->alpha=calloc(1, height*width*sizeof(unsigned char));
if(eimg->alpha==NULL) {
printf("%s: Fail to calloc eimg->alpha!\n",__func__);
return -2;
}
/* set alpha value */
if( alpha<0 ) /* all set to 255 */
memset( eimg->alpha, 255, height*width );
else
memset( eimg->alpha, alpha, height*width );
}
else { /* reset alpha, if input alpha >=0 */
if ( alpha >=0 )
memset( eimg->alpha, alpha, height*width );
}
/* --- edit alpha value to create a frame --- */
/* 1. Rectangle with 4 round corners */
if(type==frame_round_rect)
{
int rad=param[0];
int ir;
/* adjust radius */
if( rad < 0 )rad=0;
if( rad > height/2 ) rad=height/2;
if( rad > width/2 ) rad=width/2;
/* cut out 4 round corners */
for(i=0; i<rad; i++) {
ir=rad-round(sqrt(rad*rad*1.0-(rad-i)*(rad-i)*1.0));
for(j=0; j<ir; j++) {
/* upp. left corner */
*(eimg->alpha+i*width+j)=0; /* set alpha 0 at corner */
/* upp. right corner */
*(eimg->alpha+((i+1)*width-1)-j)=0;
/* down. left corner */
*(eimg->alpha+(height-1-i)*width+j)=0;
/* down. right corner */
*(eimg->alpha+(height-i)*width-1-j)=0;
}
}
}
/* TODO: other types */
else {
/* Original rectangle shape */
}
return 0;
}
/*---------------------------------------------------------------------------------
Get alpha value on a certain X-Alpha mapping curve.
Note:
1. Somthness of result alpha values depends not only on fading
'type'(algorithm), but also on 'range' value, the bigger the better--xxxNOPE!!!
2. In respect of transition smothness, linear algorithm is better
than nonlinear one? Abrupt change of alpha values will cause
some crease lines in final image.
3. !!! WARING !!! Be careful of calculation overflow. Change to float type
if necessary.
@max_alpha: Max alpha value as transition position [0 range] maps to [0 max_alpha]
@range: The range of image transition band width, corresponding to
alpha value range of [0 max_alpha]. (in pixels)
when x=range, alpha to be max_alpha.
@type: Mapping curve type, see in function codes.
Recommand type 0, type 2, and type 4.
@x: Input X, distance from image side, in pixels.
The range of input X, [0 range] (in pixels)
Return: Mapped alpha value. [0 max_alpha] <-- [0 range-1]
or 255 if no match.
------------------------------------------------------------------------------------*/
unsigned char get_alpha_mapCurve( EGI_8BIT_ALPHA max_alpha, int range, int type, int x)
{
unsigned char alpha; /* mapped alpha value */
long tx;
/* check input range */
if(range<=0) return max_alpha;
/* check input x */
if(x<0)
x=0;
else if(x>range)
x=range;
switch(type)
{
/* 0. Linear curve: Linear transition */
case 0:
alpha=max_alpha*x/range;
break;
/* 1. Cubic curve: GOOD_Smooth transition at start, Fast transition at end
but rather bigger invisible edge areas.
*/
case 1:
#if 1