-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgproc.c
1138 lines (994 loc) · 34.2 KB
/
imgproc.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
/* Seven Segment Optical Character Recognition Image Processing Functions */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Copyright (C) 2004-2013 Erik Auerswald <[email protected]> */
/* ImLib2 Header */
#include <X11/Xlib.h> /* needed by Imlib2.h */
#include <Imlib2.h>
/* standard things */
#include <stdio.h> /* puts, printf, BUFSIZ, perror, FILE */
#include <stdlib.h> /* exit */
/* string manipulation */
#include <string.h> /* strcpy */
/* sin, cos */
#include <math.h>
/* my headers */
#include "defines.h" /* defines */
#include "imgproc.h" /* image processing */
#include "help.h" /* online help */
/* global variables */
extern int ssocr_foreground;
extern int ssocr_background;
/* functions */
/*** image processing ***/
/* set foreground color */
void set_fg_color(int color)
{
ssocr_foreground = color;
}
/* set background color */
void set_bg_color(int color)
{
ssocr_background = color;
}
/* set imlib color */
void ssocr_set_color(fg_bg_t color)
{
switch(color) {
case FG:
imlib_context_set_color(ssocr_foreground, ssocr_foreground,
ssocr_foreground, 255);
break;
case BG:
imlib_context_set_color(ssocr_background, ssocr_background,
ssocr_background, 255);
break;
default:
fprintf(stderr, "error: ssocr_set_color(): unknown color %d\n",
color);
exit(99);
break;
}
}
/* draw a fore- or background pixel */
void draw_pixel(Imlib_Image *image, int x, int y, fg_bg_t color)
{
Imlib_Image *current_image; /* save current image */
current_image = imlib_context_get_image();
imlib_context_set_image(image);
ssocr_set_color(color);
imlib_image_draw_pixel(x,y,0);
imlib_context_set_image(current_image);
}
/* draw a foreground pixel */
void draw_fg_pixel(Imlib_Image *image, int x, int y)
{
draw_pixel(image, x, y, FG);
}
/* draw a background pixel */
void draw_bg_pixel(Imlib_Image *image, int x, int y)
{
draw_pixel(image, x, y, BG);
}
/* check if a pixel is set regarding current foreground/background colors */
int is_pixel_set(int value, double threshold)
{
switch(ssocr_foreground) {
case SSOCR_BLACK:
if(value < threshold/100.0*MAXRGB) {
return 1;
} else {
return 0;
}
break;
case SSOCR_WHITE:
if(value >= threshold/100.0*MAXRGB) {
return 1;
} else {
return 0;
}
break;
default:
fprintf(stderr, "error: is_pixel_set(): foreground color neither black"
" nor white\n");
exit(99);
break;
}
}
/* set pixels that have at least mask pixels around it set (including the
* examined pixel itself) to black (foreground), all other pixels to white
* (background) */
Imlib_Image set_pixels_filter(Imlib_Image *source_image, double thresh,
luminance_t lt, int mask)
{
Imlib_Image new_image; /* construct filtered image here */
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int x,y,i,j; /* iteration variables */
int set_pixel; /* should pixel be set or not? */
Imlib_Color color;
int lum; /* luminance value of pixel */
/* save pointer to current image */
current_image = imlib_context_get_image();
/* create a new image */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
new_image = imlib_clone_image();
/* check for every pixel if it should be set in filtered image */
for(x=0; x<width; x++) {
for(y=0; y<height; y++) {
set_pixel=0;
for(i=x-1; i<=x+1; i++) {
for(j=y-1; j<=y+1; j++) {
if(i>=0 && i<width && j>=0 && j<height) { /* i,j inside image? */
imlib_image_query_pixel(i, j, &color);
lum = get_lum(&color, lt);
if(is_pixel_set(lum, thresh)) {
set_pixel++;
}
}
}
}
/* set pixel if at least mask pixels around it are set */
if(set_pixel >= mask) {
draw_fg_pixel(new_image, x, y);
} else {
draw_bg_pixel(new_image, x, y);
}
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
Imlib_Image dilation(Imlib_Image *source_image, double thresh, luminance_t lt)
{
return set_pixels_filter(source_image, thresh, lt, 1);
}
Imlib_Image erosion(Imlib_Image *source_image, double thresh, luminance_t lt)
{
return set_pixels_filter(source_image, thresh, lt, 9);
}
Imlib_Image closing(Imlib_Image *source_image,double thresh,luminance_t lt,int n)
{
int i;
Imlib_Image temp_image1, temp_image2;
/* dilation n times */
imlib_context_set_image(*source_image);
temp_image1 = temp_image2 = imlib_clone_image();
for(i=0; i<n; i++) {
temp_image2 = dilation(&temp_image1, thresh, lt);
imlib_context_set_image(temp_image1);
imlib_free_image();
temp_image1 = temp_image2;
}
/* erosion n times */
for(i=0; i<n; i++) {
temp_image2 = erosion(&temp_image1, thresh, lt);
imlib_context_set_image(temp_image1);
imlib_free_image();
temp_image1 = temp_image2;
}
return temp_image2;
}
Imlib_Image opening(Imlib_Image *source_image,double thresh,luminance_t lt,int n)
{
int i;
Imlib_Image temp_image1, temp_image2;
/* erosion n times */
imlib_context_set_image(*source_image);
temp_image1 = temp_image2 = imlib_clone_image();
for(i=0; i<n; i++) {
temp_image2 = erosion(&temp_image1, thresh, lt);
imlib_context_set_image(temp_image1);
imlib_free_image();
temp_image1 = temp_image2;
}
/* dilation n times */
for(i=0; i<n; i++) {
temp_image2 = dilation(&temp_image1, thresh, lt);
imlib_context_set_image(temp_image1);
imlib_free_image();
temp_image1 = temp_image2;
}
return temp_image2;
}
/* set pixels with (brightness) value lower than threshold that have more than
* mask pixels around it set (including the examined pixel itself) to black
* (foreground), set pixels with (brightness) value lower than threshold that
* less or equal pixels around it set to white (background) */
Imlib_Image keep_pixels_filter(Imlib_Image *source_image, double thresh,
luminance_t lt, int mask)
{
Imlib_Image new_image; /* construct filtered image here */
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int x,y,i,j; /* iteration variables */
int set_pixel; /* should pixel be set or not? */
Imlib_Color color;
int lum; /* luminance value of pixel */
/* save pointer to current image */
current_image = imlib_context_get_image();
/* create a new image */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
new_image = imlib_clone_image();
/* draw white (background) rectangle to clear new image */
imlib_context_set_image(new_image);
ssocr_set_color(BG);
imlib_image_draw_rectangle(0, 0, width, height);
imlib_context_set_image(*source_image);
/* check for every pixel if it should be set in filtered image */
for(x=0; x<width; x++) {
for(y=0; y<height; y++) {
set_pixel=0;
imlib_image_query_pixel(x, y, &color);
lum = get_lum(&color, lt);
if(is_pixel_set(lum, thresh)) { /* only test neighbors of set pixels */
for(i=x-1; i<=x+1; i++) {
for(j=y-1; j<=y+1; j++) {
if(i>=0 && i<width && j>=0 && j<height) { /* i,j inside image? */
imlib_image_query_pixel(i, j, &color);
lum = get_lum(&color, lt);
if(is_pixel_set(lum, thresh)) {
set_pixel++;
}
}
}
}
}
/* set pixel if at least mask pixels around it are set */
/* mask = 1 keeps all pixels */
if(set_pixel > mask) {
draw_fg_pixel(new_image, x, y);
} else {
draw_bg_pixel(new_image, x, y);
}
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
Imlib_Image remove_isolated(Imlib_Image *source_image, double thresh,
luminance_t lt)
{
return keep_pixels_filter(source_image, thresh, lt, 1);
}
/* gray stretching, i.e. lum<t1 => lum=0, lum>t2 => lum=100,
* else lum=((lum-t1)*MAXRGB)/(t2-t1) */
Imlib_Image gray_stretch(Imlib_Image *source_image, double t1, double t2,
luminance_t lt)
{
Imlib_Image new_image; /* construct filtered image here */
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int x,y; /* iteration variables */
Imlib_Color color;
int lum; /* luminance value of pixel */
/* do nothing if t1>=t2 */
if(t1 >= t2) {
fprintf(stderr, "error: gray_stretch(): t1=%.2f >= t2=%.2f\n", t1, t2);
exit(99);
}
/* check if 0 < t1,t2 < MAXRGB */
if(t1 <= 0.0) {
fprintf(stderr, "error: gray_stretch(): t1=%.2f <= 0.0\n", t1);
exit(99);
}
if(t2 >= MAXRGB) {
fprintf(stderr, "error: gray_stretch(): t2=%.2f >= %d.0\n", t2, MAXRGB);
exit(99);
}
/* save pointer to current image */
current_image = imlib_context_get_image();
/* create a new image */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
new_image = imlib_clone_image();
/* gray stretch image */
for(x=0; x<width; x++) {
for(y=0; y<height; y++) {
imlib_image_query_pixel(x, y, &color);
lum = get_lum(&color, lt);
imlib_context_set_image(new_image);
if(lum<=t1) {
imlib_context_set_color(0, 0, 0, color.alpha);
} else if(lum>=t2) {
imlib_context_set_color(MAXRGB, MAXRGB, MAXRGB, color.alpha);
} else {
imlib_context_set_color(clip(((lum-t1)*255)/(t2-t1),0,255),
clip(((lum-t1)*255)/(t2-t1),0,255),
clip(((lum-t1)*255)/(t2-t1),0,255),
color.alpha);
}
imlib_image_draw_pixel(x, y, 0);
imlib_context_set_image(*source_image);
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
/* use dynamic (aka adaptive) local thresholding to create monochrome image */
/* ww and wh are the width and height of the rectangle used to find the
* threshold value */
Imlib_Image dynamic_threshold(Imlib_Image *source_image,double t,luminance_t lt,
int ww, int wh)
{
Imlib_Image new_image; /* construct filtered image here */
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int x,y; /* iteration variables */
Imlib_Color color;
int lum;
double thresh;
/* save pointer to current image */
current_image = imlib_context_get_image();
/* create a new image */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
new_image = imlib_clone_image();
/* check for every pixel if it should be set in filtered image */
for(x=0; x<width; x++) {
for(y=0; y<height; y++) {
imlib_image_query_pixel(x, y, &color);
lum = get_lum(&color, lt);
thresh = get_threshold(source_image, t/100.0, lt, x-ww/2, y-ww/2, ww, wh);
if(is_pixel_set(lum, thresh)) {
draw_fg_pixel(new_image, x, y);
} else {
draw_bg_pixel(new_image, x, y);
}
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
/* use simple thresholding to generate monochrome image */
Imlib_Image make_mono(Imlib_Image *source_image, double thresh, luminance_t lt)
{
Imlib_Image new_image; /* construct filtered image here */
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int x,y; /* iteration variables */
Imlib_Color color;
int lum; /* pixel luminance */
/* save pointer to current image */
current_image = imlib_context_get_image();
/* create a new image */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
new_image = imlib_clone_image();
/* check for every pixel if it should be set in filtered image */
for(x=0; x<width; x++) {
for(y=0; y<height; y++) {
imlib_image_query_pixel(x, y, &color);
lum = get_lum(&color, lt);
if(is_pixel_set(lum, thresh)) {
draw_fg_pixel(new_image, x, y);
} else {
draw_bg_pixel(new_image, x, y);
}
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
/* adapt threshold to image values values */
double adapt_threshold(Imlib_Image *image, double thresh, luminance_t lt, int x,
int y, int w, int h, int flags)
{
double t = thresh;
if(!(flags & ABSOLUTE_THRESHOLD)) {
if(flags & DEBUG_OUTPUT)
fprintf(stderr, "adjusting threshold to image: %f ->", t);
t = get_threshold(image, thresh/100.0, lt, x, y, w, h);
if(flags & DEBUG_OUTPUT)
fprintf(stderr, " %f\n", t);
if(flags & DO_ITERATIVE_THRESHOLD) {
if(flags & DEBUG_OUTPUT)
fprintf(stderr, "doing iterative_thresholding: %f ->", t);
t = iterative_threshold(image, t, lt, x, y, w, h);
if(flags & DEBUG_OUTPUT)
fprintf(stderr, " %f\n", t);
}
}
if((flags & VERBOSE) || (flags & DEBUG_OUTPUT)) {
fprintf(stderr, "using threshold %.2f\n", t);
}
return t;
}
/* compute dynamic threshold value from the rectangle (x,y),(x+w,y+h) of
* source_image */
double get_threshold(Imlib_Image *source_image, double fraction, luminance_t lt,
int x, int y, int w, int h)
{
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int xi,yi; /* iteration variables */
Imlib_Color color;
int lum; /* luminance of pixel */
double minval=(double)MAXRGB, maxval=0.0;
/* save pointer to current image */
current_image = imlib_context_get_image();
/* get image dimensions */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
/* special value -1 for width or height means image width/height */
if(w == -1) w = width;
if(h == -1) h = width;
/* assure valid coordinates */
if(x+w > width) x = width-w;
if(y+h > height) y = height-h;
if(x<0) x=0;
if(y<0) y=0;
/* find the threshold value to differentiate between dark and light */
for(xi=0; (xi<w) && (xi<width); xi++) {
for(yi=0; (yi<h) && (yi<height); yi++) {
imlib_image_query_pixel(x+xi, y+yi, &color);
lum = get_lum(&color, lt);
if(lum < minval) minval = lum;
if(lum > maxval) maxval = lum;
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
return (minval + fraction * (maxval - minval)) * 100 / MAXRGB;
}
/* determine threshold by an iterative method */
double iterative_threshold(Imlib_Image *source_image, double thresh,
luminance_t lt, int x, int y, int w, int h)
{
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int xi,yi; /* iteration variables */
Imlib_Color color;
int lum; /* luminance of pixel */
unsigned int size_white, size_black; /* size of black and white groups */
unsigned long int sum_white, sum_black; /* sum of black and white groups */
unsigned int avg_white, avg_black; /* average values of black and white */
double old_thresh; /* old threshold computed by last iteration step */
double new_thresh; /* new threshold computed by current iteration step */
int thresh_lum; /* luminance value of threshold */
/* normalize threshold (was given as a percentage) */
new_thresh = thresh / 100.0;
/* save pointer to current image */
current_image = imlib_context_get_image();
/* get image dimensions */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
/* special value -1 for width or height means image width/height */
if(w == -1) w = width;
if(h == -1) h = width;
/* assure valid coordinates */
if(x+w > width) x = width-w;
if(y+h > height) y = height-h;
if(x<0) x=0;
if(y<0) y=0;
/* find the threshold value to differentiate between dark and light */
do {
thresh_lum = MAXRGB * new_thresh;
old_thresh = new_thresh;
size_black = sum_black = size_white = sum_white = 0;
for(xi=0; (xi<w) && (xi<width); xi++) {
for(yi=0; (yi<h) && (yi<height); yi++) {
imlib_image_query_pixel(xi, yi, &color);
lum = get_lum(&color, lt);
if(lum <= thresh_lum) {
size_black++;
sum_black += lum;
} else {
size_white++;
sum_white += lum;
}
}
}
if(!size_white) {
fprintf(stderr, "iterative_threshold(): error: no white pixels\n");
imlib_context_set_image(current_image);
return thresh;
}
if(!size_black) {
fprintf(stderr, "iterative_threshold(): error: no black pixels\n");
imlib_context_set_image(current_image);
return thresh;
}
avg_white = sum_white / size_white;
avg_black = sum_black / size_black;
new_thresh = (avg_white + avg_black) / (2.0 * MAXRGB);
/*fprintf(stderr, "iterative_threshold(): new_thresh = %f\n", new_thresh);*/
} while(fabs(new_thresh - old_thresh) > EPSILON);
/* restore image from before function call */
imlib_context_set_image(current_image);
return new_thresh * 100;
}
/* get minimum lum value */
double get_minval(Imlib_Image *source_image, int x, int y, int w, int h,
luminance_t lt)
{
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int xi,yi; /* iteration variables */
Imlib_Color color; /* Imlib2 RGBA color structure */
int minval = MAXRGB;
int lum = 0;
/* save pointer to current image */
current_image = imlib_context_get_image();
/* get image dimensions */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
/* special value -1 for width or height means image width/height */
if(w == -1) w = width;
if(h == -1) h = width;
/* assure valid coordinates */
if(x+w > width) x = width-w;
if(y+h > height) y = height-h;
if(x<0) x=0;
if(y<0) y=0;
/* find the minimum value in the image */
for(xi=0; (xi<w) && (xi<width); xi++) {
for(yi=0; (yi<h) && (yi<height); yi++) {
imlib_image_query_pixel(xi, yi, &color);
lum = clip(get_lum(&color, lt),0,255);
if(lum < minval) minval = lum;
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
return minval;
}
/* get maximum luminance value */
double get_maxval(Imlib_Image *source_image, int x, int y, int w, int h,
luminance_t lt)
{
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int xi,yi; /* iteration variables */
Imlib_Color color; /* Imlib2 RGBA color structure */
int lum = 0;
int maxval = 0;
/* save pointer to current image */
current_image = imlib_context_get_image();
/* get image dimensions */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
/* special value -1 for width or height means image width/height */
if(w == -1) w = width;
if(h == -1) h = width;
/* assure valid coordinates */
if(x+w > width) x = width-w;
if(y+h > height) y = height-h;
if(x<0) x=0;
if(y<0) y=0;
/* find the minimum value in the image */
for(xi=0; (xi<w) && (xi<width); xi++) {
for(yi=0; (yi<h) && (yi<height); yi++) {
imlib_image_query_pixel(xi, yi, &color);
lum = clip(get_lum(&color, lt),0,255);
if(lum > maxval) maxval = lum;
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
return maxval;
}
/* draw a white (background) border around image, overwriting image contents
* beneath border*/
Imlib_Image white_border(Imlib_Image *source_image, int bdwidth)
{
Imlib_Image new_image; /* construct filtered image here */
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int x,y; /* coordinates of upper left corner of rectangles */
/* save pointer to current image */
current_image = imlib_context_get_image();
/* create a new image */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
new_image = imlib_clone_image();
/* assure border width has a legal value */
if(bdwidth > width/2) bdwidth = width/2;
if(bdwidth > height/2) bdwidth = height/2;
/* draw white (background) rectangle around new image */
for(x=0, y=0; x<bdwidth; x++, y++) {
imlib_context_set_image(new_image);
ssocr_set_color(BG);
imlib_image_draw_rectangle(x, y, width-2*x, height-2*y);
}
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
/* shear the image
* the top line is unchanged
* the bottom line is moved offset pixels to the right
* the other lines are moved yPos*offset/(height-1) pixels to the right
* white pixels are inserted at the left side */
Imlib_Image shear(Imlib_Image *source_image, int offset)
{
Imlib_Image new_image; /* construct filtered image here */
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int x,y; /* iteration variables */
int shift; /* current shift-width */
Imlib_Color color_return; /* for imlib_query_pixel() */
/* save pointer to current image */
current_image = imlib_context_get_image();
/* create a new image */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
new_image = imlib_clone_image();
/* move every line to the right */
for(y=1; y<height; y++) {
shift = y * offset / (height-1);
/* copy pixels */
for(x=width-1; x>=shift; x--) {
imlib_image_query_pixel(x-shift, y, &color_return);
imlib_context_set_image(new_image);
imlib_context_set_color(color_return.red, color_return.green,
color_return.blue, color_return.alpha);
imlib_image_draw_pixel(x,y,0);
imlib_context_set_image(*source_image);
}
/* fill with background */
for(x=0; x<shift; x++) {
draw_bg_pixel(new_image, x, y);
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
/* rotate the image */
Imlib_Image rotate(Imlib_Image *source_image, double theta)
{
Imlib_Image new_image; /* construct filtered image here */
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int x,y; /* iteration variables / target coordinates */
int sx,sy; /* source coordinates */
Imlib_Color color_return; /* for imlib_query_pixel() */
/* save pointer to current image */
current_image = imlib_context_get_image();
/* create a new image */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
new_image = imlib_clone_image();
/* convert theta from degrees to radians */
theta = theta / 360 * 2.0 * M_PI;
/* create rotated image
* (some parts of the original image will be lost) */
for(x = 0; x < width; x++) {
for(y = 0; y < height; y++) {
sx = (x-width/2) * cos(theta) + (y-height/2) * sin(theta) + width/2;
sy = (y-height/2) * cos(theta) - (x-width/2) * sin(theta) + height/2;
if((sx >= 0) && (sx <= width) && (sy >= 0) && (sy <= height)) {
imlib_image_query_pixel(sx, sy, &color_return);
imlib_context_set_image(new_image);
imlib_context_set_color(color_return.red, color_return.green, color_return.blue, color_return.alpha);
} else {
imlib_context_set_image(new_image);
ssocr_set_color(BG);
}
imlib_image_draw_pixel(x,y,0);
imlib_context_set_image(*source_image);
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
/* turn image to grayscale */
Imlib_Image grayscale(Imlib_Image *source_image, luminance_t lt)
{
Imlib_Image new_image; /* construct grayscale image here */
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int x,y; /* iteration variables */
Imlib_Color color; /* Imlib2 color structure */
int lum=0;
/* save pointer to current image */
current_image = imlib_context_get_image();
/* create a new image */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
new_image = imlib_clone_image();
/* transform image to grayscale */
for(x=0; x<width; x++) {
for(y=0; y<height; y++) {
imlib_context_set_image(*source_image);
imlib_image_query_pixel(x, y, &color);
imlib_context_set_image(new_image);
lum = clip(get_lum(&color, lt),0,255);
imlib_context_set_color(lum, lum, lum, color.alpha);
imlib_image_draw_pixel(x, y, 0);
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
/* use simple thresholding to generate an inverted monochrome image */
Imlib_Image invert(Imlib_Image *source_image, double thresh, luminance_t lt)
{
Imlib_Image new_image; /* construct filtered image here */
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int x,y; /* iteration variables */
Imlib_Color color;
int lum; /* pixel luminance */
/* save pointer to current image */
current_image = imlib_context_get_image();
/* create a new image */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
new_image = imlib_clone_image();
/* check for every pixel if it should be set in filtered image */
for(x=0; x<width; x++) {
for(y=0; y<height; y++) {
imlib_image_query_pixel(x, y, &color);
lum = get_lum(&color, lt);
if(is_pixel_set(lum, thresh)) {
draw_bg_pixel(new_image, x, y);
} else {
draw_fg_pixel(new_image, x, y);
}
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
/* crop image */
Imlib_Image crop(Imlib_Image *source_image, int x, int y, int w, int h)
{
Imlib_Image new_image; /* construct filtered image here */
Imlib_Image current_image; /* save image pointer */
int width, height; /* source image dimensions */
/* save pointer to current image */
current_image = imlib_context_get_image();
/* get width and height of source image */
imlib_context_set_image(*source_image);
width = imlib_image_get_width();
height = imlib_image_get_height();
/* get sane values */
if(x < 0) x = 0;
if(y < 0) y = 0;
if(x >= width) x = width - 1;
if(y >= height) y = height - 1;
if(x + w > width) w = width - x;
if(y + h > height) h = height - x;
/* create the new image */
imlib_context_set_image(*source_image);
new_image = imlib_create_cropped_image(x, y, w, h);
/* restore image from before function call */
imlib_context_set_image(current_image);
/* return filtered image */
return new_image;
}
/* compute luminance from RGB values */
int get_lum(Imlib_Color *color, luminance_t lt)
{
switch(lt) {
case REC709: return get_lum_709(color);
case REC601: return get_lum_601(color);
case LINEAR: return get_lum_lin(color);
case MINIMUM: return get_lum_min(color);
case MAXIMUM: return get_lum_max(color);
case RED: return get_lum_red(color);
case GREEN: return get_lum_green(color);
case BLUE: return get_lum_blue(color);
default:
fprintf(stderr, "error: get_lum(): unknown transfer function no. %d\n",
lt);
exit(99);
}
}
/* compute luminance Y_709 from linear RGB values */
int get_lum_709(Imlib_Color *color)
{
return 0.2125*color->red + 0.7154*color->green + 0.0721*color->blue;
}
/* compute luminance Y_601 from gamma corrected (non-linear) RGB values */
int get_lum_601(Imlib_Color *color)
{
return 0.299*color->red + 0.587*color->green + 0.114*color->blue;
}
/* compute luminance Y = (R+G+B)/3 */
int get_lum_lin(Imlib_Color *color)
{
return (color->red + color->green + color->blue) / 3;
}
/* compute luminance Y = min(R,G,B) as used in GNU Ocrad 0.14 */
int get_lum_min(Imlib_Color *color)
{
return (color->red < color->green) ?
((color->red < color->blue) ? color->red : color->blue) :
((color->green < color->blue) ? color->green : color->blue);
}
/* compute luminance Y = max(R,G,B) */
int get_lum_max(Imlib_Color *color)
{
return (color->red > color->green) ?
((color->red > color->blue) ? color->red : color->blue) :
((color->green > color->blue) ? color->green : color->blue);
}
/* compute luminance Y = R */
int get_lum_red(Imlib_Color *color)
{
return color->red;
}
/* compute luminance Y = G */
int get_lum_green(Imlib_Color *color)
{
return color->green;
}