-
Notifications
You must be signed in to change notification settings - Fork 0
/
xvalg.c
1674 lines (1245 loc) · 44.3 KB
/
xvalg.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
/*
* xvalg.c - image manipulation algorithms (Blur, etc.)
*
* Contains:
* void AlgInit();
* void DoAlg(int algnum);
*/
#include "copyright.h"
#include "xv.h"
#ifndef M_PI
# define M_PI (3.1415926535897932385)
#endif
static void NoAlg PARM((void));
static void Blur PARM((void));
static void Sharpen PARM((void));
static void EdgeDetect PARM((void));
static void TinFoil PARM((void));
static void OilPaint PARM((void));
static void Blend PARM((void));
static void FineRotate PARM((int));
static void Pixelize PARM((void));
static void Spread PARM((void));
static void MedianFilter PARM((void));
static void saveOrigPic PARM((void));
static void doBlurConvolv PARM((byte *,int,int,byte *, int,int,int,int, int));
static void doSharpConvolv PARM((byte *,int,int,byte *, int,int,int,int, int));
static void doEdgeConvolv PARM((byte *,int,int,byte *, int,int,int,int));
static void doAngleConvolv PARM((byte *,int,int,byte *, int,int,int,int));
static void doOilPaint PARM((byte *,int,int,byte *, int,int,int,int, int));
static void doBlend PARM((byte *,int,int,byte *, int,int,int,int));
static void doRotate PARM((byte *,int,int,byte *, int,int,int,int,
double, int));
static void doPixel PARM((byte *,int,int,byte *, int,int,int,int,
int, int));
static void doSpread PARM((byte *,int,int,byte *, int,int,int,int,
int, int));
static void doMedianFilter PARM((byte *,int,int,byte *, int,int,int,int, int));
static void add2bb PARM((int *, int *, int *, int *, int, int));
static void rotXfer PARM((int, int, double *,double *,
double,double, double));
#ifdef FOO
static void intsort PARM((int *, int));
#endif
static int start24bitAlg PARM((byte **, byte **));
static void end24bitAlg PARM((byte *, byte *));
static void printUTime PARM((char *));
static byte *origPic = (byte *) NULL;
static int origPicType;
static byte origrmap[256], origgmap[256], origbmap[256];
#undef TIMING_TEST
#ifdef TIMING_TEST
#include <sys/time.h>
#include <sys/resource.h>
#endif
/***************************/
static void printUTime(str)
char *str;
{
#ifdef TIMING_TEST
int i; struct rusage ru;
i = getrusage(RUSAGE_SELF, &ru);
fprintf(stderr,"%s: utime = %d.%d seconds\n",
str, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec);
#endif
}
/************************************************************/
void AlgInit()
{
/* called whenver an image file is loaded. disposes of origPic
if neccessary, and points it to null */
if (origPic) free(origPic);
origPic = (byte *) NULL;
algMB.dim[ALG_NONE] = 1; /* can't undo when init'ed already */
}
/************************/
void DoAlg(anum)
int anum;
{
/* called with a value from the algMB button. Executes the specified
algorithm */
switch (anum) {
case ALG_NONE: NoAlg(); break;
case ALG_BLUR: Blur(); break;
case ALG_SHARPEN: Sharpen(); break;
case ALG_EDGE: EdgeDetect(); break;
case ALG_TINF: TinFoil(); break;
case ALG_OIL: OilPaint(); break;
case ALG_BLEND: Blend(); break;
case ALG_ROTATE: FineRotate(0); break;
case ALG_ROTATECLR: FineRotate(1); break;
case ALG_PIXEL: Pixelize(); break;
case ALG_SPREAD: Spread(); break;
case ALG_MEDIAN: MedianFilter(); break;
}
algMB.dim[ALG_NONE] = (origPic == (byte *) NULL);
}
/************************/
static void NoAlg()
{
int i;
/* restore original picture */
if (!origPic) return; /* none to restore */
WaitCursor();
KillOldPics(); /* toss the old pic/cpic/epic/theImage away */
picType = origPicType;
Set824Menus(picType);
pic = origPic; origPic = NULL;
if (picType == PIC8) {
for (i=0; i<256; i++) {
rMap[i] = origrmap[i];
gMap[i] = origgmap[i];
bMap[i] = origbmap[i];
}
}
InstallNewPic();
}
/************************/
static void Blur()
{
/* runs a n*n convolution mask (all 1's) over 'pic',
producing a 24-bit version. Then calls 24to8 to generate a new 8-bit
image, and installs it.
Note that 'n' must be odd for things to work properly */
byte *pic24, *tmpPic;
int i, sx,sy,sw,sh, n;
static char *labels[] = { "\nOk", "\033Cancel" };
char txt[256];
static char buf[64] = { '3', '\0' };
sprintf(txt, "Blur: \n\n%s",
"Enter mask size (ex. 3, 5, 7, ...)");
i = GetStrPopUp(txt, labels, 2, buf, 64, "0123456789", 1);
if (i==1 || strlen(buf)==0) return;
n = atoi(buf);
if (n < 1 || (n&1)!=1) {
ErrPopUp("Error: The value entered must be odd and greater than zero.",
"\nOh!");
return;
}
WaitCursor();
if (HaveSelection()) GetSelRCoords(&sx,&sy,&sw,&sh);
else { sx = 0; sy = 0; sw = pWIDE; sh = pHIGH; }
CropRect2Rect(&sx,&sy,&sw,&sh, 0,0,pWIDE,pHIGH);
SetISTR(ISTR_INFO, "Blurring %s with %dx%d convolution mask...",
(HaveSelection() ? "selection" : "image"), n,n);
if (start24bitAlg(&pic24, &tmpPic)) return;
xvbcopy((char *) pic24, (char *) tmpPic, (size_t) (pWIDE*pHIGH*3));
doBlurConvolv(pic24, pWIDE,pHIGH, tmpPic, sx,sy,sw,sh, n);
end24bitAlg(pic24, tmpPic);
}
/************************/
static void Sharpen()
{
/* runs an edge-enhancment algorithm */
byte *pic24, *tmpPic;
int i, sx,sy,sw,sh, n;
static char *labels[] = { "\nOk", "\033Cancel" };
char txt[256];
static char buf[64] = { '7', '5', '\0' };
sprintf(txt, "Sharpen: \n\n%s",
"Enter enhancement factor (0-99%)");
i = GetStrPopUp(txt, labels, 2, buf, 64, "0123456789", 1);
if (i==1 || strlen(buf)==0) return;
n = atoi(buf);
if (n>=100) {
ErrPopUp("Error: The value entered must be less than 100%.", "\nOh!");
return;
}
WaitCursor();
if (HaveSelection()) GetSelRCoords(&sx,&sy,&sw,&sh);
else { sx = 0; sy = 0; sw = pWIDE; sh = pHIGH; }
CropRect2Rect(&sx,&sy,&sw,&sh, 0,0,pWIDE,pHIGH);
SetISTR(ISTR_INFO, "Sharpening %s by factor of %d%%...",
(HaveSelection() ? "selection" : "image"), n);
if (start24bitAlg(&pic24, &tmpPic)) return;
xvbcopy((char *) pic24, (char *) tmpPic, (size_t) (pWIDE*pHIGH*3));
doSharpConvolv(pic24, pWIDE,pHIGH, tmpPic, sx,sy,sw,sh, n);
end24bitAlg(pic24, tmpPic);
}
/************************/
static void EdgeDetect()
{
byte *pic24, *p24, *tmpPic, *tlp;
char *str;
int i, j, v, maxv, sx,sy,sw,sh;
WaitCursor();
if (HaveSelection()) GetSelRCoords(&sx,&sy,&sw,&sh);
else { sx = 0; sy = 0; sw = pWIDE; sh = pHIGH; }
CropRect2Rect(&sx,&sy,&sw,&sh, 0,0,pWIDE,pHIGH);
str = "Doing edge detection...";
SetISTR(ISTR_INFO, str);
if (start24bitAlg(&pic24, &tmpPic)) return;
xvbcopy((char *) pic24, (char *) tmpPic, (size_t) (pWIDE*pHIGH*3));
doEdgeConvolv(pic24, pWIDE, pHIGH, tmpPic, sx,sy,sw,sh);
SetISTR(ISTR_INFO, "%snormalizing...", str);
/* Normalize results */
for (i=sy, maxv=0; i<sy+sh; i++) { /* compute max value */
p24 = tmpPic + (i*pWIDE + sx) * 3;
for (j=sx; j<sx+sw; j++, p24+=3) {
v = MONO(p24[0], p24[1], p24[2]);
if (v>maxv) maxv = v;
}
}
for (i=sy; maxv>0 && i<sy+sh; i++) { /* normalize */
p24 = tlp = tmpPic + (i*pWIDE + sx) * 3;
for (j=0; j<sw*3; j++) {
v = (((int) *p24) * 255) / maxv;
RANGE(v,0,255);
*p24++ = (byte) v;
}
}
end24bitAlg(pic24, tmpPic);
}
/************************/
static void TinFoil()
{
byte *pic24, *tmpPic, *tp, *tlp;
char *str;
int i, j, v, sx,sy,sw,sh;
WaitCursor();
str = "Doing cheesy embossing effect...";
SetISTR(ISTR_INFO, str);
if (HaveSelection()) GetSelRCoords(&sx,&sy,&sw,&sh);
else { sx = 0; sy = 0; sw = pWIDE; sh = pHIGH; }
CropRect2Rect(&sx,&sy,&sw,&sh, 0,0,pWIDE,pHIGH);
if (start24bitAlg(&pic24, &tmpPic)) return;
xvbcopy((char *) pic24, (char *) tmpPic, (size_t) (pWIDE*pHIGH*3));
/* fill selected area of tmpPic with gray128 */
for (i=sy; i<sy+sh; i++) {
tp = tlp = tmpPic + (i*pWIDE + sx) * 3;
for (j=sx; j<sx+sw; j++) {
*tp++ = 128; *tp++ = 128; *tp++ = 128;
}
}
doAngleConvolv(pic24, pWIDE, pHIGH, tmpPic, sx,sy,sw,sh);
/* mono-ify selected area of tmpPic */
for (i=sy; i<sy+sh; i++) {
tp = tlp = tmpPic + (i*pWIDE + sx) * 3;
for (j=sx; j<sx+sw; j++, tp+=3) {
v = MONO(tp[0], tp[1], tp[2]);
RANGE(v,0,255);
tp[0] = tp[1] = tp[2] = (byte) v;
}
}
end24bitAlg(pic24, tmpPic);
}
/************************/
static void OilPaint()
{
byte *pic24, *tmpPic;
int sx,sy,sw,sh;
WaitCursor();
SetISTR(ISTR_INFO, "Doing oilpaint effect...");
if (HaveSelection()) GetSelRCoords(&sx,&sy,&sw,&sh);
else { sx = 0; sy = 0; sw = pWIDE; sh = pHIGH; }
CropRect2Rect(&sx,&sy,&sw,&sh, 0,0,pWIDE,pHIGH);
if (start24bitAlg(&pic24, &tmpPic)) return;
xvbcopy((char *) pic24, (char *) tmpPic, (size_t) (pWIDE*pHIGH*3));
doOilPaint(pic24, pWIDE, pHIGH, tmpPic, sx,sy,sw,sh, 3);
end24bitAlg(pic24, tmpPic);
}
/************************/
static void Blend()
{
byte *pic24, *tmpPic;
int sx,sy,sw,sh;
if (HaveSelection()) GetSelRCoords(&sx,&sy,&sw,&sh);
else { sx = 0; sy = 0; sw = pWIDE; sh = pHIGH; }
CropRect2Rect(&sx,&sy,&sw,&sh, 0,0,pWIDE,pHIGH);
WaitCursor();
if (start24bitAlg(&pic24, &tmpPic)) return;
xvbcopy((char *) pic24, (char *) tmpPic, (size_t) (pWIDE*pHIGH*3));
doBlend(pic24, pWIDE, pHIGH, tmpPic, sx,sy,sw,sh);
end24bitAlg(pic24, tmpPic);
}
/************************/
static void FineRotate(clr)
int clr;
{
byte *pic24, *tmpPic;
int i,sx,sy,sw,sh;
double rotval;
static char *labels[] = { "\nOk", "\033Cancel" };
char txt[256];
static char buf[64] = { '\0' };
sprintf(txt, "Rotate (%s):\n\nEnter rotation angle, in degrees: (>0 = CCW)",
(clr ? "Clear" : "Copy"));
i = GetStrPopUp(txt, labels, 2, buf, 64, "0123456789.-", 1);
if (i==1 || strlen(buf)==0) return;
rotval = atof(buf);
if (rotval == 0.0) return;
if (HaveSelection()) GetSelRCoords(&sx,&sy,&sw,&sh);
else { sx = 0; sy = 0; sw = pWIDE; sh = pHIGH; }
CropRect2Rect(&sx,&sy,&sw,&sh, 0,0,pWIDE,pHIGH);
WaitCursor();
if (start24bitAlg(&pic24, &tmpPic)) return;
xvbcopy((char *) pic24, (char *) tmpPic, (size_t) (pWIDE*pHIGH*3));
doRotate(pic24, pWIDE, pHIGH, tmpPic, sx,sy,sw,sh, rotval, clr);
end24bitAlg(pic24, tmpPic);
}
/************************/
static void Pixelize()
{
byte *pic24, *tmpPic;
int i,sx,sy,sw,sh, pixX,pixY,err;
static char *labels[] = { "\nOk", "\033Cancel" };
char txt[256];
static char buf[64] = { '4', '\0' };
sprintf(txt, "Pixelize:\n\nEnter new pixel size, in image pixels: %s",
"(ex. '3', '5x8')");
i = GetStrPopUp(txt, labels, 2, buf, 64, "0123456789x", 1);
if (i==1 || strlen(buf)==0) return;
pixX = pixY = err = 0;
if (index(buf, 'x')) {
if (sscanf(buf, "%d x %d", &pixX, &pixY) != 2) err++;
}
else {
if (sscanf(buf, "%d", &pixX) != 1) err++;
pixY = pixX;
}
if (pixX<1 || pixY<1 || err) {
ErrPopUp("Error: The entered string is not valid.", "\nWho Cares!");
return;
}
if (HaveSelection()) GetSelRCoords(&sx,&sy,&sw,&sh);
else { sx = 0; sy = 0; sw = pWIDE; sh = pHIGH; }
CropRect2Rect(&sx,&sy,&sw,&sh, 0,0,pWIDE,pHIGH);
WaitCursor();
if (start24bitAlg(&pic24, &tmpPic)) return;
xvbcopy((char *) pic24, (char *) tmpPic, (size_t) (pWIDE*pHIGH*3));
doPixel(pic24, pWIDE, pHIGH, tmpPic, sx,sy,sw,sh, pixX, pixY);
end24bitAlg(pic24, tmpPic);
}
/************************/
static void Spread()
{
byte *pic24, *tmpPic;
int i,sx,sy,sw,sh, pixX,pixY,err;
static char *labels[] = { "\nOk", "\033Cancel" };
char txt[256];
static char buf[64] = { '5', '\0' };
sprintf(txt, "Spread:\n\nEnter spread factor (or x,y factors): %s",
"(ex. '10', '1x5')");
i = GetStrPopUp(txt, labels, 2, buf, 64, "0123456789x", 1);
if (i==1 || strlen(buf)==0) return;
pixX = pixY = err = 0;
if (index(buf, 'x')) {
if (sscanf(buf, "%d x %d", &pixX, &pixY) != 2) err++;
if (pixX<0 || pixY<0) err++;
}
else {
if (sscanf(buf, "%d", &pixX) != 1) err++;
pixX = -pixX;
}
if (!pixX && !pixY) return; /* 0x0 has no effect */
if (err) {
ErrPopUp("Error: The entered string is not valid.", "\nBite Me!");
return;
}
if (HaveSelection()) GetSelRCoords(&sx,&sy,&sw,&sh);
else { sx = 0; sy = 0; sw = pWIDE; sh = pHIGH; }
CropRect2Rect(&sx,&sy,&sw,&sh, 0,0,pWIDE,pHIGH);
WaitCursor();
if (start24bitAlg(&pic24, &tmpPic)) return;
xvbcopy((char *) pic24, (char *) tmpPic, (size_t) (pWIDE*pHIGH*3));
doSpread(pic24, pWIDE, pHIGH, tmpPic, sx,sy,sw,sh, pixX, pixY);
end24bitAlg(pic24, tmpPic);
}
/************************/
static void MedianFilter()
{
/* runs median filter algorithm (for n*n rect centered around each pixel,
replace with median value */
byte *pic24, *tmpPic;
int i, sx,sy,sw,sh, n;
static char *labels[] = { "\nOk", "\033Cancel" };
char txt[256];
static char buf[64] = { '3', '\0' };
sprintf(txt, "DeSpeckle (median filter): \n\n%s",
"Enter mask size (ex. 3, 5, 7, ...)");
i = GetStrPopUp(txt, labels, 2, buf, 64, "0123456789", 1);
if (i==1 || strlen(buf)==0) return;
n = atoi(buf);
if (n < 1 || (n&1)!=1) {
ErrPopUp("Error: The value entered must be odd and greater than zero.",
"\nOh!");
return;
}
WaitCursor();
if (HaveSelection()) GetSelRCoords(&sx,&sy,&sw,&sh);
else { sx = 0; sy = 0; sw = pWIDE; sh = pHIGH; }
CropRect2Rect(&sx,&sy,&sw,&sh, 0,0,pWIDE,pHIGH);
SetISTR(ISTR_INFO, "DeSpeckling %s with %dx%d convolution mask...",
(HaveSelection() ? "selection" : "image"), n,n);
if (start24bitAlg(&pic24, &tmpPic)) return;
xvbcopy((char *) pic24, (char *) tmpPic, (size_t) (pWIDE*pHIGH*3));
doMedianFilter(pic24, pWIDE,pHIGH, tmpPic, sx,sy,sw,sh, n);
end24bitAlg(pic24, tmpPic);
}
/************************/
static void doBlurConvolv(pic24, w, h, results, selx,sely,selw,selh, n)
byte *pic24, *results;
int w,h, selx,sely,selw,selh, n;
{
/* convolves with an n*n array, consisting of only 1's.
Operates on rectangular region 'selx,sely,selw,selh' (in pic coords)
Region is guaranteed to be completely within pic boundaries
'n' must be odd */
register byte *p24;
register int rsum,gsum,bsum;
byte *rp;
int x,y,x1,y1,count,n2;
printUTime("start of blurConvolv");
n2 = n/2;
for (y=sely; y<sely+selh; y++) {
ProgressMeter(sely, (sely+selh)-1, y, "Blur");
if ((y & 15) == 0) WaitCursor();
p24 = pic24 + y*w*3 + selx*3;
rp = results + y*w*3 + selx*3;
for (x=selx; x<selx+selw; x++) {
rsum = gsum = bsum = 0; count = 0;
for (y1=y-n2; y1<=y+n2; y1++) {
if (y1>=sely && y1<sely+selh) {
p24 = pic24 + y1*w*3 +(x-n2)*3;
for (x1=x-n2; x1<=x+n2; x1++) {
if (x1>=selx && x1<selx+selw) {
rsum += *p24++;
gsum += *p24++;
bsum += *p24++;
count++;
}
else p24 += 3;
}
}
}
rsum = rsum / count;
gsum = gsum / count;
bsum = bsum / count;
RANGE(rsum,0,255);
RANGE(gsum,0,255);
RANGE(bsum,0,255);
*rp++ = (byte) rsum;
*rp++ = (byte) gsum;
*rp++ = (byte) bsum;
}
}
printUTime("end of blurConvolv");
}
/************************/
static void doSharpConvolv(pic24, w, h, results, selx,sely,selw,selh, n)
byte *pic24, *results;
int w,h, selx,sely,selw,selh, n;
{
byte *p24;
int rv, gv, bv;
byte *rp;
int i,x,y;
double fact, ifact, hue,sat,val, vsum;
double *linem1, *line0, *linep1, *tmpptr;
printUTime("start of sharpConvolv");
fact = n / 100.0;
ifact = 1.0 - fact;
if (selw<3 || selh<3) return; /* too small */
linem1 = (double *) malloc(selw * sizeof(double));
line0 = (double *) malloc(selw * sizeof(double));
linep1 = (double *) malloc(selw * sizeof(double));
if (!linem1 || !line0 || !linep1) {
ErrPopUp("Malloc() error in doSharpConvov().", "\nDoh!");
if (linem1) free(linem1);
if (line0) free(line0);
if (linep1) free(linep1);
return;
}
/* load up line arrays */
p24 = pic24 + (((sely+1)-1)*w + selx) * 3;
for (x=selx, tmpptr=line0; x<selx+selw; x++, p24+=3, tmpptr++) {
rgb2hsv((int) p24[0], (int) p24[1], (int) p24[2], &hue,&sat,&val);
*tmpptr = val;
}
p24 = pic24 + (((sely+1)+0)*w + selx) * 3;
for (x=selx, tmpptr=linep1; x<selx+selw; x++, p24+=3, tmpptr++) {
rgb2hsv((int) p24[0], (int) p24[1], (int) p24[2], &hue,&sat,&val);
*tmpptr = val;
}
for (y=sely+1; y<(sely+selh)-1; y++) {
ProgressMeter(sely+1, (sely+selh)-2, y, "Sharpen");
if ((y & 15) == 0) WaitCursor();
tmpptr = linem1; linem1 = line0; line0 = linep1; linep1 = tmpptr;
/* get next line */
p24 = pic24 + ((y+1)*w + selx) * 3;
for (x=selx, tmpptr=linep1; x<selx+selw; x++, p24+=3, tmpptr++) {
rgb2hsv((int) p24[0], (int) p24[1], (int) p24[2], &hue,&sat,&val);
*tmpptr = val;
}
p24 = pic24 + (y*w + selx+1) * 3;
rp = results + (y*w + selx+1) * 3;
for (x=selx+1; x<selx+selw-1; x++, p24+=3) {
vsum = 0.0; i = x-selx;
vsum = linem1[i-1] + linem1[i] + linem1[i+1] +
line0 [i-1] + line0 [i] + line0 [i+1] +
linep1[i-1] + linep1[i] + linep1[i+1];
rgb2hsv((int) p24[0], (int) p24[1], (int) p24[2], &hue, &sat, &val);
val = ((val - (fact * vsum) / 9) / ifact);
RANGE(val, 0.0, 1.0);
hsv2rgb(hue,sat,val, &rv, &gv, &bv);
RANGE(rv,0,255);
RANGE(gv,0,255);
RANGE(bv,0,255);
*rp++ = (byte) rv;
*rp++ = (byte) gv;
*rp++ = (byte) bv;
}
}
free(linem1); free(line0); free(linep1);
printUTime("end of sharpConvolv");
}
/************************/
static void doEdgeConvolv(pic24, w, h, results, selx,sely,selw,selh)
byte *pic24, *results;
int w, h, selx,sely,selw,selh;
{
/* convolves with two edge detection masks (vertical and horizontal)
simultaneously, taking Max(abs(results))
The two masks are (hard coded):
-1 0 1 -1 -1 -1
H = -1 0 1 and V = 0 0 0
-1 0 1 1 1 1
divided into
-1 0 0 0 0 0 0 0 1 0 1 0
a = 0 0 0 , b = -1 0 1 , c = 0 0 0 , d = 0 0 0 .
0 0 1 0 0 0 -1 0 0 0 -1 0
So H = a + b + c, V = a - c - d.
gradient = max(abs(H),abs(V)).
Also, only does pixels in which the masks fit fully onto the picture
(no pesky boundary conditionals) */
register byte *p24;
register int bperlin, a, b, c, d, rsum, gsum, bsum;
byte *rp;
int x, y;
printUTime("start of edgeConvolv");
bperlin = w * 3;
for (y=sely+1; y<sely+selh-1; y++) {
ProgressMeter(sely+1, (sely+selh-1)-1, y, "Edge Detect");
if ((y & 63) == 0) WaitCursor();
rp = results + (y*w + selx+1)*3;
p24 = pic24 + (y*w + selx+1)*3;
for (x=selx+1; x<selx+selw-1; x++, p24+=3) {
/* RED PLANE */
a = p24[bperlin+3] - p24[-bperlin-3]; /* bottom right - top left */
b = p24[3] - p24[-3]; /* mid right - mid left */
c = p24[-bperlin+3] - p24[bperlin-3]; /* top right - bottom left */
d = p24[-bperlin] - p24[bperlin]; /* top mid - bottom mid */
rsum = a + b + c; /* horizontal gradient */
if (rsum < 0) rsum = -rsum;
a = a - c - d; /* vertical gradient */
if (a < 0) a = -a;
if (a > rsum) rsum = a;
rsum /= 3;
/* GREEN PLANE */
a = p24[bperlin+4] - p24[-bperlin-2]; /* bottom right - top left */
b = p24[4] - p24[-2]; /* mid right - mid left */
c = p24[-bperlin+4] - p24[bperlin-2]; /* top right - bottom left */
d = p24[-bperlin+1] - p24[bperlin+1]; /* top mid - bottom mid */
gsum = a + b + c; /* horizontal gradient */
if (gsum < 0) gsum = -gsum;
a = a - c - d; /* vertical gradient */
if (a < 0) a = -a;
if (a > gsum) gsum = a;
gsum /= 3;
/* BLUE PLANE */
a = p24[bperlin+5] - p24[-bperlin-1]; /* bottom right - top left */
b = p24[5] - p24[-1]; /* mid right - mid left */
c = p24[-bperlin+5] - p24[bperlin-1]; /* top right - bottom left */
d = p24[-bperlin+2] - p24[bperlin+2]; /* top mid - bottom mid */
bsum = a + b + c; /* horizontal gradient */
if (bsum < 0) bsum = -bsum;
a = a - c - d; /* vertical gradient */
if (a < 0) a = -a;
if (a > bsum) bsum = a;
bsum /= 3;
*rp++ = (byte) rsum;
*rp++ = (byte) gsum;
*rp++ = (byte) bsum;
}
}
printUTime("end of edgeConvolv");
}
/************************/
static void doAngleConvolv(pic24, w, h, results, selx,sely,selw,selh)
byte *pic24, *results;
int w, h, selx,sely,selw,selh;
{
/* convolves with edge detection mask, at 45 degrees to horizontal.
The mask is (hard coded):
-2 -1 0
-1 0 1
0 1 2
Also, only does pixels in which the masks fit fully onto the picture
(no pesky boundary conditionals)
Adds value of rsum,gsum,bsum to results pic */
register byte *p24;
register int bperlin,rsum,gsum,bsum;
byte *rp;
int x,y;
printUTime("start of doAngleConvolv");
bperlin = w * 3;
for (y=sely+1; y<sely+selh-1; y++) {
ProgressMeter(sely+1, (sely+selh-1)-1, y, "Convolve");
if ((y & 63) == 0) WaitCursor();
rp = results + (y*w + selx+1)*3;
p24 = pic24 + (y*w + selx+1)*3;
for (x=selx+1; x<selx+selw-1; x++, p24+=3) {
/* compute weighted average for *p24 (pic24[x,y]) */
rsum = gsum = bsum = 0;
rsum -= (p24[-bperlin-3] * 2); /* top left */
gsum -= (p24[-bperlin-2] * 2);
bsum -= (p24[-bperlin-1] * 2);
rsum -= p24[-bperlin]; /* top mid */
gsum -= p24[-bperlin+1];
bsum -= p24[-bperlin+2];
rsum -= p24[-3]; /* mid left */
gsum -= p24[-2];
bsum -= p24[-1];
rsum += p24[3]; /* mid right */
gsum += p24[4];
bsum += p24[5];
rsum += p24[bperlin]; /* bottom mid */
gsum += p24[bperlin+1];
bsum += p24[bperlin+2];
rsum += (p24[bperlin+3] * 2); /* bottom right */
gsum += (p24[bperlin+4] * 2);
bsum += (p24[bperlin+5] * 2);
rsum = rsum / 8;
gsum = gsum / 8;
bsum = bsum / 8;
rsum += rp[0]; RANGE(rsum,0,255);
gsum += rp[1]; RANGE(gsum,0,255);
bsum += rp[2]; RANGE(bsum,0,255);
*rp++ = (byte) rsum;
*rp++ = (byte) gsum;
*rp++ = (byte) bsum;
}
}
printUTime("end of edgeConvolv");
}
/************************/
static void doOilPaint(pic24, w, h, results, selx,sely,selw,selh, n)
byte *pic24, *results;
int w, h, selx,sely,selw,selh, n;
{
/* does an 'oil transfer', as described in the book "Beyond Photography",
by Holzmann, chapter 4, photo 7. It is a sort of localized smearing.
The following algorithm (but no actual code) was borrowed from
'pgmoil.c', written by Wilson Bent (whb@hoh-2.att.com),
and distributed as part of the PBMPLUS package.
for each pixel in the image (assume, for a second, a grayscale image),
compute a histogram of the n*n rectangle centered on the pixel.
replace the pixel with the color that had the greatest # of hits in
the histogram. Note that 'n' should be odd.
I've modified the algorithm to do the *right* thing for RGB images.
(jhb, 6/94) */
register byte *pp;
register int bperlin;
byte *rp, *p24, *plin;
int i,j,k,x,y,n2,col,cnt,maxcnt;
int *nnrect;
printUTime("start of doOilPaint");
if (n & 1) n++; /* n must be odd */
bperlin = w * 3;
n2 = n/2;
/* nnrect[] is an n*n array of ints, with '-1' meaning 'outside the region'
otherwise they'll have 24-bit RGB values */
nnrect = (int *) malloc(n * n * sizeof(int));
if (!nnrect) FatalError("can't malloc nnrect[] in doOilPaint()\n");
for (y=sely; y<sely+selh; y++) {
if ((y & 15) == 0) WaitCursor();
ProgressMeter(sely, (sely+selh)-1, y, "Oil Paint");
p24 = pic24 + ((y-n2)*w + selx-n2)*3; /* pts to top-left of mask */
rp = results + (y*w + selx)*3;
for (x=selx; x<selx+selw; x++) {
/* fill 'nnrect' with valid pixels from n*n region centered round x,y */
pp = plin = p24;
for (i=y-n2, k=0; i<y+n2; i++) {
for (j=x-n2; j<x+n2; j++, k++, pp+=3) {
if (i>=sely && i<sely+selh && j>=selx && j<selx+selw) {
nnrect[k] = (((int) pp[0])<<16) | (((int) pp[1])<<8) | pp[2];
}
else nnrect[k] = -1;
}
plin += bperlin; pp = plin;
}
/* find 'most popular color' in nnrect, not counting '-1' */
maxcnt = cnt = col = 0;
for (i=0; i<n*n; i++) {
if (nnrect[i] != -1) {
cnt = 1;
for (j=i+1; j<n*n; j++) { /* count it */
if (nnrect[i] == nnrect[j]) cnt++;
}
if (cnt>maxcnt) { col = nnrect[i]; maxcnt = cnt; }
}
}
*rp++ = (byte) ((col>>16) & 0xff);
*rp++ = (byte) ((col>>8) & 0xff);
*rp++ = (byte) ((col) & 0xff);
p24 += 3;
}
}
free(nnrect);
printUTime("end of doOilPaint");
}
/************************/
static void doBlend(pic24, w, h, results, selx,sely,selw,selh)
byte *pic24, *results;
int w, h, selx,sely,selw,selh;
{
/* 'blends' a rectangular region out of existence. computes the average
color of all the pixels on the edge of the rect, stores this in the
center, and for each pixel in the rect, replaces it with a weighted
average of the center color, and the color on the edge that intersects
a line drawn from the center to the point */
byte *p24;
int i,x,y;
int cx,cy,cR,cG,cB;
int ex,ey,eR,eG,eB;