-
Notifications
You must be signed in to change notification settings - Fork 0
/
xvgam.c
3345 lines (2610 loc) · 86.3 KB
/
xvgam.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
/*
* xvgam.c
*
* callable functions:
* <too many to list>
*/
#include "copyright.h"
#include "xv.h"
#include "bits/h_rotl"
#include "bits/h_rotr"
#include "bits/h_flip"
#include "bits/h_sinc"
#include "bits/h_sdec"
#include "bits/h_sat"
#include "bits/h_desat"
#define GAMW 664
#define GAMH 518
#define GAMbutF 386
#define CMAPX 10
#define CMAPY 17
#define CMAPCW 12
#define CMAPCH 9
#define CMAPW (CMAPCW * 16)
#define CMAPH (CMAPCH * 16)
#define MAXUNDO 32
#define BUTTH 23
#define N_HMAP 6 /* # of Hue modification remappings */
#define N_HDBUTT 5
#define HDB_ROTL 0
#define HDB_ROTR 1
#define HDB_EXPND 2
#define HDB_SHRNK 3
#define HDB_FLIP 4
#define N_HDBUTT2 4
#define HDB_DESAT 2
#define HDB_SAT 3
#define HD_CLEAR 0x01 /* clears inside of hue dial */
#define HD_FRAME 0x02
#define HD_HANDS 0x04
#define HD_DIR 0x08
#define HD_VALS 0x10
#define HD_TITLE 0x20
#define HD_CLHNDS 0x40
#define HD_BUTTS 0x80
#define HD_ALL (HD_FRAME | HD_HANDS | HD_DIR | HD_VALS | HD_TITLE | HD_BUTTS)
#define HD_RADIUS 30 /* radius of bounding circle of HDIALs */
#define DEG2RAD (3.14159 / 180.0)
#define RAD2DEG (180.0 / 3.14159)
/* stuff for colormap Undo button */
struct cmapstate { byte rm[256], gm[256], bm[256];
int cellgroup[256];
int curgroup;
int maxgroup;
int editColor;
} prevcmap, tmpcmap;
struct hmap { int src_st;
int src_en;
int src_ccw;
int dst_st;
int dst_en;
int dst_ccw;
};
struct gamstate { struct hmap hmap[N_HMAP];
int hueRBnum; /* 1 - 6 */
int wht_stval, wht_satval, wht_enab;
int satval;
GRAF_STATE istate, rstate, gstate, bstate;
};
static struct gamstate undo[MAXUNDO], preset[4], defstate;
static struct gamstate *defLoadState;
static int uptr, uhead, utail;
typedef struct huedial {
Window win; /* window that dial exists in */
int x,y; /* coordinates of center of dial */
int range; /* 0 = single value, 1 = range */
int stval; /* start of range (ONLY val ifnot range) */
int enval; /* end of range */
int ccwise; /* 1 if range goes ccwise, 0 if cwise */
const char *str; /* title string */
u_long fg,bg; /* colors */
int satval; /* saturation value on non-range dial */
BUTT hdbutt[N_HDBUTT];
CBUTT enabCB;
void (*drawobj)PARM((void));
} HDIAL;
static BUTT hueclrB;
static CBUTT enabCB, autoCB, resetCB, dragCB;
static HDIAL srcHD, dstHD, whtHD;
static DIAL satDial, rhDial, gsDial, bvDial;
static GRAF intGraf, rGraf, gGraf, bGraf;
static RBUTT *hueRB;
static Window cmapF, hsvF, rgbF, modF, butF;
static struct hmap hmap[N_HMAP];
static int hremap[360];
static int defAutoApply;
static int hsvnonlinear = 0;
static void printUTime PARM((const char *));
static void computeHSVlinear PARM((void));
static void changedGam PARM((void));
static void drawGam PARM((int,int,int,int));
static void drawBut PARM((int,int,int,int));
static void drawArrow PARM((int, int));
static void drawCmap PARM((void));
static void clickCmap PARM((int,int,int));
static void clickGam PARM((int,int));
static void selectCell PARM((int,int));
static int deladdCell PARM((int, int));
static void doCmd PARM((int));
static void SetHSVmode PARM((void));
static void applyGamma PARM((int));
static void calcHistEQ PARM((int *, int *, int *));
static void saveGamState PARM((void));
static void gamUndo PARM((void));
static void gamRedo PARM((void));
static void ctrls2gamstate PARM((struct gamstate *));
static void gamstate2ctrls PARM((struct gamstate *));
static void rndCols PARM((void));
static void saveCMap PARM((struct cmapstate *));
static void restoreCMap PARM((struct cmapstate *));
static void parseResources PARM((void));
static void makeResources PARM((void));
static void dragGamma PARM((void));
static void dragHueDial PARM((void));
static void dragEditColor PARM((void));
static void HDCreate PARM((HDIAL *, Window, int, int, int, int,
int, int, const char *, u_long, u_long));
static void HDRedraw PARM((HDIAL *, int));
static int HDClick PARM((HDIAL *, int, int));
static int HDTrack PARM((HDIAL *, int, int));
static int hdg2xdg PARM((int));
static void pol2xy PARM((int, int, double, int, int *, int *));
static int computeHDval PARM((HDIAL *, int, int));
static void initHmap PARM((void));
static void init1hmap PARM((int));
static void dials2hmap PARM((void));
static void hmap2dials PARM((void));
static void build_hremap PARM((void));
#define CMAPF_WIDE 212
#define CMAPF_HIGH 322
#define BUTF_WIDE 212
#define BUTF_HIGH 96
#define MODF_WIDE 212
#define MODF_HIGH 70
#define HSVF_WIDE 205
#define HSVF_HIGH 500
#define RGBF_WIDE 185
#define RGBF_HIGH 500
#undef TIMING_TEST
#ifdef TIMING_TEST
#include <sys/resource.h>
#endif
/***************************/
static void printUTime(str)
const char *str;
{
#ifdef TIMING_TEST
int i;
struct rusage ru;
i = getrusage(RUSAGE_SELF, &ru);
fprintf(stderr,"%s: utime = %ld.%ld seconds\n",
str, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec);
#endif
}
/***************************************************/
void CreateGam(geom, gam, rgam, ggam, bgam, defpreset)
const char *geom;
double gam, rgam, ggam, bgam;
int defpreset;
{
XSetWindowAttributes xswa;
gamW = CreateWindow("xv color editor", "XVcedit", geom,
GAMW, GAMH, infofg,infobg, 0);
if (!gamW) FatalError("can't create cedit window!");
cmapF = XCreateSimpleWindow(theDisp,gamW, 10, 8,CMAPF_WIDE,CMAPF_HIGH,
1,infofg,infobg);
butF = XCreateSimpleWindow(theDisp,gamW, 10, 336,BUTF_WIDE,BUTF_HIGH,
1,infofg,infobg);
modF = XCreateSimpleWindow(theDisp,gamW, 10, 438,MODF_WIDE,MODF_HIGH,
1,infofg,infobg);
hsvF = XCreateSimpleWindow(theDisp,gamW, 242, 8,HSVF_WIDE,HSVF_HIGH,
1,infofg,infobg);
rgbF = XCreateSimpleWindow(theDisp,gamW, 467, 8,RGBF_WIDE,RGBF_HIGH,
1,infofg,infobg);
if (!cmapF || !butF || !modF || !hsvF || !rgbF)
FatalError("couldn't create frame windows");
#ifdef BACKING_STORE
xswa.backing_store = WhenMapped;
XChangeWindowAttributes(theDisp, cmapF, CWBackingStore, &xswa);
XChangeWindowAttributes(theDisp, butF, CWBackingStore, &xswa);
XChangeWindowAttributes(theDisp, modF, CWBackingStore, &xswa);
XChangeWindowAttributes(theDisp, hsvF, CWBackingStore, &xswa);
#endif
XSelectInput(theDisp, gamW, ExposureMask);
XSelectInput(theDisp, cmapF, ExposureMask | ButtonPressMask);
XSelectInput(theDisp, butF, ExposureMask | ButtonPressMask);
XSelectInput(theDisp, modF, ExposureMask | ButtonPressMask);
XSelectInput(theDisp, hsvF, ExposureMask | ButtonPressMask);
XSelectInput(theDisp, rgbF, ExposureMask);
if (ctrlColor) XSetWindowBackground(theDisp, gamW, locol);
else XSetWindowBackgroundPixmap(theDisp, gamW, grayTile);
/********** COLORMAP editing doo-wahs ***********/
BTCreate(&gbut[G_BCOLUNDO], cmapF, 5, 165, 66, BUTTH,
"ColUndo", infofg, infobg, hicol, locol);
BTCreate(&gbut[G_BCOLREV], cmapF, 5 + 66 + 1, 165, 67, BUTTH,
"Revert", infofg, infobg, hicol, locol);
BTCreate(&gbut[G_BHSVRGB], cmapF, 5+66+67+2, 165, 66, BUTTH,
"RGB/HSV", infofg, infobg, hicol, locol);
BTCreate(&gbut[G_BMONO], cmapF, 5, 189, 66, BUTTH,
"Grey", infofg, infobg, hicol, locol);
BTCreate(&gbut[G_BRV], cmapF, 5 + 66 + 1, 189, 67, BUTTH,
"RevVid", infofg, infobg, hicol, locol);
BTCreate(&gbut[G_BRNDCOL], cmapF, 5 + 66 + 67 + 2, 189, 66, BUTTH,
"Random", infofg, infobg, hicol, locol);
DCreate(&rhDial, cmapF, 5, 215, 66, 100, 0.0, 360.0, 180.0, 1.0, 5.0,
infofg, infobg, hicol, locol, "Hue", NULL);
DCreate(&gsDial, cmapF, 72, 215, 66, 100, 0.0, 360.0, 180.0, 1.0, 5.0,
infofg, infobg, hicol, locol, "Sat.", NULL);
DCreate(&bvDial, cmapF, 139, 215, 66, 100, 0.0, 360.0, 180.0, 1.0, 5.0,
infofg, infobg, hicol, locol, "Value", NULL);
rhDial.drawobj = gsDial.drawobj = bvDial.drawobj = dragEditColor;
/*********** CONTROL BUTTONS ***********/
/* positioning constants for buttons. (arranged as 4x4 grid...) */
#define BXSPACE 53
#define BYSPACE (BUTTH+1)
#define BX0 0
#define BX1 (BX0 + BXSPACE)
#define BX2 (BX0 + BXSPACE*2)
#define BX3 (BX0 + BXSPACE*3)
#define BY0 0
#define BY1 (BY0 + BYSPACE)
#define BY2 (BY0 + BYSPACE*2)
#define BY3 (BY0 + BYSPACE*3)
BTCreate(&gbut[G_BAPPLY], butF, BX0,BY0, 52,BUTTH,"Apply",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BNOGAM], butF, BX0,BY1, 52,BUTTH,"NoMod",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BMAXCONT],butF, BX0,BY2, 52,BUTTH,"Norm",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BHISTEQ], butF, BX0,BY3, 52,BUTTH,"HistEq",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BUP_BR],butF, BX1,BY0, 52,BUTTH,"Brite",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BDN_BR],butF, BX1,BY1, 52,BUTTH,"Dim",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BUP_CN],butF, BX1,BY2, 52,BUTTH,"Sharp",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BDN_CN],butF, BX1,BY3, 52,BUTTH,"Dull",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BRESET],butF, BX2, BY0, 52,BUTTH,"Reset",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_B1], butF, BX2, BY1, 25,BUTTH,"1",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_B2], butF, BX2+26,BY1, 26,BUTTH,"2",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_B3], butF, BX2, BY2, 25,BUTTH,"3",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_B4], butF, BX2+26,BY2, 26,BUTTH,"4",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BSET], butF, BX2, BY3, 52,BUTTH,"Set",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BUNDO], butF, BX3, BY0, 52,BUTTH,"Undo",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BREDO], butF, BX3, BY1, 52,BUTTH,"Redo",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BGETRES],butF,BX3, BY2, 52,BUTTH,"CutRes",
infofg,infobg,hicol,locol);
BTCreate(&gbut[G_BCLOSE],butF, BX3, BY3, 52,BUTTH,"Close",
infofg,infobg,hicol,locol);
gbut[G_BSET].toggle = 1;
gbut[G_BUNDO].active = 0;
gbut[G_BREDO].active = 0;
CBCreate(&enabCB, modF,2,2, "Display with HSV/RGB mods.",
infofg,infobg,hicol,locol);
CBCreate(&autoCB, modF,2,2+17, "Auto-apply HSV/RGB mods.",
infofg,infobg,hicol,locol);
CBCreate(&dragCB, modF,2,2+17*2,"Auto-apply while dragging.",
infofg,infobg,hicol,locol);
CBCreate(&resetCB,modF,2,2+17*3,"Auto-reset on new image.",
infofg,infobg,hicol,locol);
enabCB.val = autoCB.val = resetCB.val = dragCB.val = 1;
defAutoApply = autoCB.val;
/************ HSV editing doo-wahs **************/
HDCreate(&srcHD, hsvF, 52, 65, 1, 0, 30, 0, "From", infofg, infobg);
HDCreate(&dstHD, hsvF, 154, 65, 1, 0, 30, 0, "To", infofg, infobg);
HDCreate(&whtHD, hsvF, 50,243, 0, 0, 0, 0, "White",infofg, infobg);
srcHD.drawobj = dstHD.drawobj = whtHD.drawobj = dragHueDial;
DCreate(&satDial, hsvF, 100, 199, 100, 121, -100.0, 100.0, 0.0, 1.0, 5.0,
infofg, infobg,hicol,locol, "Saturation", "%");
hueRB = RBCreate(NULL, hsvF, 7, 153, "1",
infofg, infobg,hicol,locol);
RBCreate (hueRB,hsvF, 47, 153, "2",
infofg, infobg,hicol,locol);
RBCreate (hueRB,hsvF, 87, 153, "3",
infofg, infobg,hicol,locol);
RBCreate (hueRB,hsvF, 7, 170, "4",
infofg, infobg,hicol,locol);
RBCreate (hueRB,hsvF, 47, 170, "5",
infofg, infobg,hicol,locol);
RBCreate (hueRB,hsvF, 87, 170, "6",
infofg, infobg,hicol,locol);
BTCreate(&hueclrB, hsvF, 127, 158, 70, BUTTH, "Reset",
infofg, infobg,hicol,locol);
initHmap();
hmap2dials();
build_hremap();
InitGraf(&intGraf);
CreateGraf(&intGraf, hsvF, 20, 339, infofg, infobg, "Intensity");
/********* RGB color correction doo-wahs ***********/
InitGraf(&rGraf);
CreateGraf(&rGraf, rgbF, 10, 20, infofg, infobg, "Red");
InitGraf(&gGraf);
CreateGraf(&gGraf, rgbF, 10, 179, infofg, infobg, "Green");
InitGraf(&bGraf);
CreateGraf(&bGraf, rgbF, 10, 338, infofg, infobg, "Blue");
satDial.drawobj = dragGamma;
intGraf.drawobj = rGraf.drawobj = gGraf.drawobj = bGraf.drawobj = dragGamma;
SetHSVmode();
ctrls2gamstate(&defstate);
/* set up preset0 as a '2-color' preset */
ctrls2gamstate(&preset[0]);
Str2Graf(&preset[0].istate,"L 4 : 0,0 : 127,0 : 128,255 : 255,255");
/* set up preset1 as a '8-color' preset */
ctrls2gamstate(&preset[1]);
Str2Graf(&preset[1].rstate,"L 4 : 0,0 : 127,0 : 128,255 : 255,255");
Str2Graf(&preset[1].gstate,"L 4 : 0,0 : 127,0 : 128,255 : 255,255");
Str2Graf(&preset[1].bstate,"L 4 : 0,0 : 127,0 : 128,255 : 255,255");
/* set up preset2 as a 'temperature' pseudo-color preset */
ctrls2gamstate(&preset[2]);
Str2Graf(&preset[2].rstate,"S 4 : 0,0 : 105,0 : 155,140 : 255,255");
Str2Graf(&preset[2].gstate,"S 5 : 0,0 : 57,135 : 127,255 : 198,135 : 255,0");
Str2Graf(&preset[2].bstate,"S 4 : 0,255 : 100,140 : 150,0 : 255,0");
/* set up preset3 as a 'map' pseudo-color preset */
ctrls2gamstate(&preset[3]);
Str2Graf(&preset[3].rstate,"L 4 : 0,0 : 66,0 : 155,255 : 255,146");
Str2Graf(&preset[3].gstate,"L 5 : 0,0 : 28,0 : 75,255 : 162,210 : 255,46");
Str2Graf(&preset[3].bstate,"L 5 : 0,105 : 19,255 : 66,232 : 108,62:255,21");
parseResources();
CBSetActive(&dragCB, (allocMode == AM_READWRITE));
/* deal with passed in [r,g,b]gam values. If <0.0, ignore them */
if (gam>=0.0) {
char str[64];
sprintf(str, "G %f", gam);
if (Str2Graf(&defstate.istate, str)) { /* unable to parse */ }
}
if (rgam>=0.0 && ggam>=0.0 && bgam>=0.0) {
char str[64];
sprintf(str, "G %f", rgam);
Str2Graf(&defstate.rstate, str);
sprintf(str, "G %f", ggam);
Str2Graf(&defstate.gstate, str);
sprintf(str, "G %f", bgam);
Str2Graf(&defstate.bstate, str);
}
if (defpreset) defLoadState = &preset[defpreset-1];
else defLoadState = &defstate;
gamstate2ctrls(defLoadState); /* defstate may have changed */
uptr = utail = uhead = 0;
ctrls2gamstate(&undo[0]);
#ifdef BACKING_STORE
xswa.backing_store = WhenMapped;
XChangeWindowAttributes(theDisp, intGraf.win, CWBackingStore, &xswa);
XChangeWindowAttributes(theDisp, rGraf.win, CWBackingStore, &xswa);
XChangeWindowAttributes(theDisp, gGraf.win, CWBackingStore, &xswa);
XChangeWindowAttributes(theDisp, bGraf.win, CWBackingStore, &xswa);
XChangeWindowAttributes(theDisp, intGraf.gwin, CWBackingStore, &xswa);
XChangeWindowAttributes(theDisp, rGraf.gwin, CWBackingStore, &xswa);
XChangeWindowAttributes(theDisp, gGraf.gwin, CWBackingStore, &xswa);
XChangeWindowAttributes(theDisp, bGraf.gwin, CWBackingStore, &xswa);
#endif
XMapSubwindows(theDisp, cmapF);
XMapSubwindows(theDisp, hsvF);
XMapSubwindows(theDisp, rgbF);
XMapSubwindows(theDisp, gamW);
computeHSVlinear();
}
/***************************************************/
int GamCheckEvent(xev)
XEvent *xev;
{
/* check event to see if it's for one of our subwindows. If it is,
deal accordingly, and return '1'. Otherwise, return '0' */
int rv;
rv = 1;
if (xev->type == Expose) {
int x,y,w,h;
XExposeEvent *e = (XExposeEvent *) xev;
x = e->x; y = e->y; w = e->width; h = e->height;
/* throw away excess redraws for 'dumb' windows */
if (e->count > 0 &&
(e->window == satDial.win || e->window == rhDial.win ||
e->window == gsDial.win || e->window == bvDial.win ||
e->window == cmapF || e->window == modF ||
e->window == intGraf.win || e->window == rGraf.win ||
e->window == gGraf.win || e->window == bGraf.win ||
e->window == intGraf.gwin || e->window == rGraf.gwin ||
e->window == gGraf.gwin || e->window == bGraf.gwin ||
e->window == rgbF)) {}
else if (e->window == gamW) drawGam(x, y, w, h);
else if (e->window == butF) drawBut(x, y, w, h);
else if (e->window == satDial.win) DRedraw(&satDial);
else if (e->window == rhDial.win) DRedraw(&rhDial);
else if (e->window == gsDial.win) DRedraw(&gsDial);
else if (e->window == bvDial.win) DRedraw(&bvDial);
else if (e->window == intGraf.win) RedrawGraf(&intGraf, 0);
else if (e->window == rGraf.win) RedrawGraf(&rGraf, 0);
else if (e->window == gGraf.win) RedrawGraf(&gGraf, 0);
else if (e->window == bGraf.win) RedrawGraf(&bGraf, 0);
else if (e->window == intGraf.gwin) RedrawGraf(&intGraf, 1);
else if (e->window == rGraf.gwin) RedrawGraf(&rGraf, 1);
else if (e->window == gGraf.gwin) RedrawGraf(&gGraf, 1);
else if (e->window == bGraf.gwin) RedrawGraf(&bGraf, 1);
else if (e->window == cmapF) drawCmap();
else if (e->window == modF) {
Draw3dRect(modF, 0,0, MODF_WIDE-1, MODF_HIGH-1, R3D_IN, 2,
hicol, locol, infobg);
CBRedraw(&enabCB);
CBRedraw(&autoCB);
CBRedraw(&dragCB);
CBRedraw(&resetCB);
}
else if (e->window == hsvF) {
XRectangle xr;
xr.x = x; xr.y = y; xr.width = e->width; xr.height = e->height;
XSetClipRectangles(theDisp, theGC, 0,0, &xr, 1, Unsorted);
Draw3dRect(hsvF, 0,0, HSVF_WIDE-1, HSVF_HIGH-1, R3D_IN, 2,
hicol, locol, infobg);
XSetForeground(theDisp, theGC, infofg);
ULineString(hsvF, 3, 2+ASCENT, "HSV Modification");
HDRedraw(&srcHD, HD_ALL);
HDRedraw(&dstHD, HD_ALL);
HDRedraw(&whtHD, HD_ALL);
RBRedraw(hueRB, -1);
BTRedraw(&hueclrB);
XSetClipMask(theDisp, theGC, None);
}
else if (e->window == rgbF) {
Draw3dRect(rgbF, 0,0, RGBF_WIDE-1, RGBF_HIGH-1, R3D_IN, 2,
hicol, locol, infobg);
XSetForeground(theDisp, theGC, infofg);
ULineString(rgbF, 3, 2+ASCENT, "RGB Modification");
}
else rv = 0;
}
else if (xev->type == ButtonPress) {
XButtonEvent *e = (XButtonEvent *) xev;
int i,x,y;
x = e->x; y = e->y;
if (e->button == Button1) {
if (e->window == butF) clickGam(x,y);
else if (e->window == cmapF) clickCmap(x,y,1);
else if (e->window == modF) {
if (CBClick(&enabCB,x,y)) {
if (CBTrack(&enabCB)) applyGamma(0); /* enabCB changed, regen */
}
else if (CBClick(&autoCB,x,y)) {
if (CBTrack(&autoCB)) {
defAutoApply = autoCB.val;
if (autoCB.val) applyGamma(0); /* auto-apply turned on, apply */
}
}
else if (CBClick(&dragCB,x,y)) {
if (CBTrack(&dragCB)) {
if (dragCB.val) applyGamma(0); /* drag turned on, apply now */
}
}
else if (CBClick(&resetCB,x,y)) CBTrack(&resetCB);
}
else if (e->window == hsvF) {
if (HDClick(&srcHD, x,y) || HDClick(&dstHD, x,y)) {
dials2hmap();
build_hremap();
changedGam();
}
else if (HDClick(&whtHD, x,y)) changedGam();
else if (PTINRECT(x,y,hueclrB.x, hueclrB.y, hueclrB.w, hueclrB.h)) {
if (BTTrack(&hueclrB)) { /* RESET */
dstHD.stval = srcHD.stval;
dstHD.enval = srcHD.enval;
dstHD.ccwise = srcHD.ccwise;
HDRedraw(&dstHD, HD_ALL | HD_CLEAR);
dials2hmap();
build_hremap();
changedGam();
}
}
else if ((i=RBClick(hueRB,x,y)) >= 0) {
dials2hmap();
if (RBTrack(hueRB, i)) hmap2dials();
}
}
else if (e->window == intGraf.win) {
GRAF_STATE gs;
if (ClickGraf(&intGraf, e->subwindow, x,y)) {
GetGrafState(&intGraf, &gs);
changedGam();
}
}
else if (e->window == rGraf.win) {
if (ClickGraf(&rGraf, e->subwindow, x,y)) changedGam();
}
else if (e->window == gGraf.win) {
if (ClickGraf(&gGraf, e->subwindow, x,y)) changedGam();
}
else if (e->window == bGraf.win) {
if (ClickGraf(&bGraf, e->subwindow, x,y)) changedGam();
}
else if (e->window == satDial.win) {
if (DTrack(&satDial, x, y)) changedGam();
}
else if (e->window == rhDial.win ||
e->window == gsDial.win ||
e->window == bvDial.win) {
if ((e->window == rhDial.win && DTrack(&rhDial, x,y)) ||
(e->window == gsDial.win && DTrack(&gsDial, x,y)) ||
(e->window == bvDial.win && DTrack(&bvDial, x,y))) {
saveCMap(&prevcmap);
BTSetActive(&gbut[G_BCOLUNDO],1);
ApplyEditColor(0);
}
}
else rv = 0;
}
else if (e->button == Button2) {
if (e->window == cmapF) clickCmap(x, y, 2);
else rv = 0;
}
else if (e->button == Button3) {
if (e->window == cmapF) clickCmap(x, y, 3);
else rv = 0;
}
else rv = 0;
}
else if (xev->type == KeyPress) {
XKeyEvent *e = (XKeyEvent *) xev;
char buf[128]; KeySym ks;
int stlen;
stlen = XLookupString(e,buf,128,&ks,(XComposeStatus *) NULL);
buf[stlen] = '\0';
RemapKeyCheck(ks, buf, &stlen);
if (!stlen) return 0;
else if (e->window == intGraf.win) rv = GrafKey(&intGraf,buf);
else if (e->window == rGraf.win ) rv = GrafKey(&rGraf,buf);
else if (e->window == gGraf.win ) rv = GrafKey(&gGraf,buf);
else if (e->window == bGraf.win ) rv = GrafKey(&bGraf,buf);
else rv = 0;
if (rv>1) changedGam(); /* hit 'enter' in one of Graf's */
}
else rv = 0;
return rv;
}
/***************************************************/
static void computeHSVlinear()
{
/* determine linearity of HSV controls to avoid semi-expensive (and
error-inducing) HSV calculations */
int i;
hsvnonlinear = 0;
for (i=0; i<360 && hremap[i] == i; i++);
if (i!=360) hsvnonlinear++;
if (whtHD.enabCB.val && whtHD.satval) hsvnonlinear++;
if (satDial.val != 0.0) hsvnonlinear++;
/* check intensity graf */
for (i=0; i<256 && intGraf.func[i]==i; i++);
if (i<256) hsvnonlinear++;
}
/***************************************************/
static void changedGam()
{
/* called whenever an HSV/RGB gamma ctrl has changed
applies change to image if autoCB.val is set */
computeHSVlinear();
saveGamState();
if (autoCB.val) applyGamma(0);
}
/***************************************************/
void GamBox(vis)
int vis;
{
if (vis) XMapRaised(theDisp, gamW);
else XUnmapWindow(theDisp, gamW);
gamUp = vis;
}
/***************************************************/
static void drawGam(x,y,w,h)
int x,y,w,h;
{
XRectangle xr;
xr.x = x; xr.y = y; xr.width = w; xr.height = h;
XSetClipRectangles(theDisp, theGC, 0,0, &xr, 1, Unsorted);
drawArrow(232,178);
drawArrow(457,178);
XSetClipMask(theDisp, theGC, None);
}
/***************************************************/
static void drawBut(x,y,w,h)
int x,y,w,h;
{
int i;
XRectangle xr;
xr.x = x; xr.y = y; xr.width = w; xr.height = h;
XSetClipRectangles(theDisp, theGC, 0,0, &xr, 1, Unsorted);
for (i=0; i<G_NBUTTS; i++) {
if (gbut[i].win == butF) BTRedraw(&gbut[i]);
}
XSetClipMask(theDisp, theGC, None);
}
/***************************************************/
static void drawArrow(x,y)
int x,y;
{
XPoint pts[8];
pts[0].x = x+10; pts[0].y = y;
pts[1].x = x-4; pts[1].y = y-100;
pts[2].x = x-4; pts[2].y = y-40;
pts[3].x = x-10; pts[3].y = y-40;
pts[4].x = x-10; pts[4].y = y+40;
pts[5].x = x-4; pts[5].y = y+40;
pts[6].x = x-4; pts[6].y = y+100;
pts[7].x = pts[0].x; pts[7].y = pts[0].y;
XSetForeground(theDisp, theGC, infobg);
XFillPolygon(theDisp, gamW, theGC, pts, 8, Convex, CoordModeOrigin);
XSetForeground(theDisp, theGC, infofg);
XDrawLines(theDisp, gamW, theGC, pts, 8, CoordModeOrigin);
}
/***************************************************/
static void drawCmap()
{
int i;
Draw3dRect(cmapF, 0,0, CMAPF_WIDE-1, CMAPF_HIGH-1, R3D_IN, 2,
hicol, locol, infobg);
XSetForeground(theDisp, theGC, infofg);
ULineString(cmapF, 3, 2+ASCENT, "Colormap Editing");
for (i=0; i<G_NBUTTS; i++) {
if (gbut[i].win == cmapF) BTRedraw(&gbut[i]);
}
RedrawCMap();
}
/***************************************************/
void NewCMap()
{
/* called when we've loaded a new picture */
int i;
XClearArea(theDisp, cmapF, CMAPX, CMAPY, CMAPW+1, CMAPH+1, False);
for (i=0; i<256; i++) cellgroup[i] = 0;
curgroup = maxgroup = 0;
BTSetActive(&gbut[G_BCOLUNDO],0);
if (resetCB.val) { /* auto-reset gamma controls */
i = autoCB.val;
if (i) autoCB.val = 0; /* must NOT apply changes! */
gamstate2ctrls(defLoadState);
autoCB.val = i;
}
/* disable/enable things if we're in PIC24 or PIC8 mode */
BTSetActive(&gbut[G_BCOLREV], (picType == PIC8) ? 1 : 0);
BTSetActive(&gbut[G_BHSVRGB], (picType == PIC8) ? 1 : 0);
BTSetActive(&gbut[G_BMONO], (picType == PIC8) ? 1 : 0);
BTSetActive(&gbut[G_BRV], (picType == PIC8) ? 1 : 0);
BTSetActive(&gbut[G_BRNDCOL], (picType == PIC8) ? 1 : 0);
DSetActive(&rhDial, (picType == PIC8) ? 1 : 0);
DSetActive(&gsDial, (picType == PIC8) ? 1 : 0);
DSetActive(&bvDial, (picType == PIC8) ? 1 : 0);
}
/***************************************************/
void RedrawCMap()
{
int i;
CBSetActive(&dragCB, (allocMode == AM_READWRITE));
XSetLineAttributes(theDisp, theGC, 0, LineSolid, CapButt, JoinMiter);
XSetForeground(theDisp, theGC, infofg);
if (picType != PIC8) {
CenterString(cmapF, CMAPX + CMAPW/2, CMAPY + CMAPH/2,
"No colormap in 24-bit mode.");
return;
}
for (i=0; i<numcols; i++) {
int x,y;
x = CMAPX + (i%16)*CMAPCW;
y = CMAPY + (i/16)*CMAPCH;
XDrawRectangle(theDisp, cmapF, theGC, x, y, CMAPCW,CMAPCH);
}
for (i=0; i<numcols; i++) {
int x,y;
x = CMAPX + (i%16)*CMAPCW;
y = CMAPY + (i/16)*CMAPCH;
XSetForeground(theDisp, theGC, cols[i]);
XFillRectangle(theDisp, cmapF, theGC, x+1, y+1, CMAPCW-1,CMAPCH-1);
if (i == editColor || (curgroup && (cellgroup[i]==curgroup))) {
XSetForeground(theDisp, theGC, infobg);
XDrawRectangle(theDisp, cmapF, theGC, x+1, y+1, CMAPCW-2,CMAPCH-2);
XSetForeground(theDisp, theGC, infofg);
XDrawRectangle(theDisp, cmapF, theGC, x+2, y+2, CMAPCW-4,CMAPCH-4);
}
}
}
/***************************************************/
static void selectCell(cellno, sel)
int cellno, sel;
{
int x,y;
if (cellno >= numcols) return;
x = CMAPX + (cellno%16)*CMAPCW;
y = CMAPY + (cellno/16)*CMAPCH;
if (!sel) { /* unhighlight a cell */
XSetForeground(theDisp, theGC, cols[cellno]);
XFillRectangle(theDisp, cmapF, theGC, x+1, y+1, CMAPCW-1,CMAPCH-1);
}
else { /* highlight a cell */
XSetForeground(theDisp, theGC, infobg);
XDrawRectangle(theDisp, cmapF, theGC, x+1, y+1, CMAPCW-2,CMAPCH-2);
XSetForeground(theDisp, theGC, infofg);
XDrawRectangle(theDisp, cmapF, theGC, x+2, y+2, CMAPCW-4,CMAPCH-4);
}
}
/***************************************************/
static void clickGam(x,y)
int x,y;
{
int i;
BUTT *bp;
for (i=0; i<G_NBUTTS; i++) {
bp = &gbut[i];
if (bp->win == butF && PTINRECT(x, y, bp->x, bp->y, bp->w, bp->h)) break;
}
/* if 'Set' is lit, and we didn't click 'set' or 'Reset' or '1'..'4',
turn it off */
if (i!=G_BSET && i!=G_B1 && i!=G_B2 && i!=G_B3 && i!=G_B4 && i!=G_BRESET
&& gbut[G_BSET].lit) {
gbut[G_BSET].lit = 0;
BTRedraw(&gbut[G_BSET]);
}
if (i<G_NBUTTS) { /* found one */
if (BTTrack(bp)) doCmd(i);
}
}
/***************************************************/
static void clickCmap(x,y,but)
int x,y,but;
{
int i, recolor;
BUTT *bp;
if (but==1) { /* if left click, check the cmap controls */
for (i=0; i<G_NBUTTS; i++) {
bp = &gbut[i];
if (bp->win == cmapF && PTINRECT(x,y,bp->x, bp->y, bp->w, bp->h)) break;
}
if (i<G_NBUTTS) { /* found one */
if (BTTrack(bp)) doCmd(i);
return;
}
}
/* see if we're anywhere in the colormap area */
if (picType == PIC8 && PTINRECT(x,y,CMAPX,CMAPY,CMAPW,CMAPH)) {
if (but==1) { /* select different cell/group for editing */
/* compute colorcell # */
i = ((x-CMAPX)/CMAPCW) + ((y-CMAPY)/CMAPCH)*16;
if (i<numcols) { /* clicked in colormap. track til mouseup */
if (i!=editColor) ChangeEC(i);
while (1) {
Window rW,cW;
int rx,ry,x,y;
unsigned int mask;
if (XQueryPointer(theDisp,cmapF,&rW,&cW,&rx,&ry,&x,&y,&mask)) {
if (!(mask & Button1Mask)) break; /* button released */
RANGE(x, CMAPX, CMAPX+CMAPW-1);
RANGE(y, CMAPY, CMAPY+CMAPH-1);
i = ((x-CMAPX)/CMAPCW) + ((y-CMAPY)/CMAPCH)*16;