-
Notifications
You must be signed in to change notification settings - Fork 0
/
xvcut.c
1864 lines (1424 loc) · 46.5 KB
/
xvcut.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
/*
* xvcut.c - xv cut-n-paste (and eventually drag-n-drop) related functions
*
* Contains:
* int CutAllowed();
* int PasteAllowed();
* void DoImgCopy();
* void DoImgCut();
* void DoImgClear();
* void DoImgPaste();
*
* static byte *getSelection ();
* static byte *getFromClip ();
* void SaveToClip (data);
* static void clearSelectedArea();
* static void makeClipFName ();
* static int countcols24 (byte *, int,int, int,int,int,int));
* static int countNewCols (byte*, int, int, byte*, int,
* int, int, int, int);
*
* void InitSelection ();
* int HaveSelection ();
* int GetSelType ();
* void GetSelRCoords (&x, &y, &w, &h);
* void EnableSelection(onoff);
* void DrawSelection (onoff);
* int DoSelection (XButtonEvent *);
* static int dragHandle (XButtonEvent *);
* static void dragSelection (XButtonEvent *, u_int bmask, dndrop);
* static void rectSelection (XButtonEvent *);
* void MoveGrowSelection(dx,dy,dw,dh);
*
* void BlinkSelection (cnt);
* void FlashSelection (cnt);
*
* static void makePasteSel (data);
*
* void CropRect2Rect (int*,int*,int*,int*, int,int,int,int);
*
* coordinate system transforms between pic, cpic, and epic coords
* void CoordE2C(ex, ey, &cx, &cy);
* void CoordC2E(cx, cy, &ex, &ey);
* void CoordP2C(px, py, &cx, &cy);
* void CoordC2P(cx, cy, &px, &py);
* void CoordE2P(ex, ey, &px, &py);
* void CoordP2E(px, py, &ex, &ey);
*/
#define NEEDSDIR /* for stat() */
#include "copyright.h"
#include "xv.h"
#include "bits/cut"
#include "bits/cutm"
#include "bits/copy"
#include "bits/copym"
#define DBLCLKTIME 500
#define CLIPPROP "XV_CLIPBOARD"
/***
*** local functions
***/
static void doPaste PARM((byte *));
static void buildCursors PARM((void));
static byte *getSelection PARM((void));
static byte *getFromClip PARM((void));
static void clearSelectedArea PARM((void));
static void makeClipFName PARM((void));
static int countcols24 PARM((byte *, int, int, int, int, int, int));
static int countNewCols PARM((byte *, int, int, byte *, int,
int, int, int, int));
static int dragHandle PARM((XButtonEvent *));
static void dragSelection PARM((XButtonEvent *, u_int, int));
static void rectSelection PARM((XButtonEvent *));
static void makePasteSel PARM((byte *));
static void CoordE2Prnd PARM((int, int, int *, int *));
/***
*** local data
***/
static char *clipfname = (char *) NULL;
static char clipfnstr[MAXPATHLEN];
static int selrx, selry, selrw, selrh; /* PIC coords of sel. rect */
static int haveSel; /* have a selection? */
static int selType; /* type of sel. (RECT, etc) */
static int selFilled; /* if true flash whole sel */
static int selColor; /* selection 'mask' 0-7 */
static int selTracking; /* in a 'tracking' loop */
static Cursor dragcurs = (Cursor) 0;
static Cursor copycurs = (Cursor) 0;
static Cursor cutcurs = (Cursor) 0;
/********************************************/
int CutAllowed()
{
/* returns '1' if cut/copy/clear commands should be enabled (ie, if
there's a selection of some sort */
return (but[BCROP].active);
}
/********************************************/
int PasteAllowed()
{
Atom clipAtom;
struct stat st;
/* returns '1' if there's something on the clipboard to be pasted */
/* also, can't paste if we're in a root mode */
if (useroot) return 0;
/* see if there's a CLIPPROP associated with the root window,
if there is, we'll use that as the clipboard: return '1' */
clipAtom = XInternAtom(theDisp, CLIPPROP, True);
if (clipAtom != None) return 1; /* clipboard property exists: can paste */
/* barring that, see if the CLIPFILE exists. if so, we can paste */
if (!clipfname) makeClipFName();
if (stat(clipfname, &st)==0) return 1; /* file exists: can paste */
return 0;
}
/********************************************/
void DoImgCopy()
{
/*
* called when 'Copy' command is issued. does entire user interface,
* such as it is, and puts appropriate data onto the clipboard.
* Does all complaining on any errors
*/
byte *cimg;
if (!HaveSelection()) return;
cimg = getSelection();
if (!cimg) return;
SaveToClip(cimg);
free(cimg);
FlashSelection(2);
SetCursors(-1);
}
/********************************************/
void DoImgCut()
{
/*
* called when 'Cut' command is issued. does entire user interface,
* such as it is, and puts appropriate data onto the clipboard.
* Does all complaining on any errors
*/
byte *cimg;
if (!HaveSelection()) return;
FlashSelection(2);
cimg = getSelection();
if (!cimg) return;
SaveToClip(cimg);
free(cimg);
clearSelectedArea();
SetCursors(-1);
}
/********************************************/
void DoImgClear()
{
/* called when 'Clear' command is issued */
if (!HaveSelection()) return;
FlashSelection(2);
clearSelectedArea();
SetCursors(-1);
}
/********************************************/
void DoImgPaste()
{
byte *cimg;
if (!PasteAllowed()) { XBell(theDisp, 0); return; }
cimg = getFromClip();
if (!cimg) return;
/* if there's no selection, make one! */
if (!HaveSelection()) makePasteSel(cimg);
else doPaste(cimg);
free(cimg);
SetCursors(-1);
}
/********************************************/
static void doPaste(cimg)
byte *cimg;
{
/*
* This is fairly hairy.
*/
byte *dp, *dpic, *clippic, *clipcmap;
int clipw, cliph, clipis24, len, istran, trval;
int i, j, sx,sy,sw,sh, cx,cy,cw,ch, dx,dy,dw,dh;
/*
* verify clipboard data
*/
if (!cimg) return;
len = ((int) cimg[CIMG_LEN + 0]) |
((int) (cimg[CIMG_LEN + 1]<<8)) |
((int) (cimg[CIMG_LEN + 2]<<16)) |
((int) (cimg[CIMG_LEN + 3]<<24));
if (len < CIMG_PIC24) return;
istran = cimg[CIMG_TRANS];
trval = cimg[CIMG_TRVAL];
clipis24 = cimg[CIMG_24];
clipw = cimg[CIMG_W] | ((int) (cimg[CIMG_W+1]<<8));
cliph = cimg[CIMG_H] | ((int) (cimg[CIMG_H+1]<<8));
if (clipw<1 || cliph<1) return;
if (( clipis24 && len != CIMG_PIC24 + clipw * cliph * 3) ||
(!clipis24 && len != CIMG_PIC8 + clipw * cliph) ) {
ErrPopUp("The clipboard data is not in a recognized format!", "\nMoof!");
goto exit;
}
clippic = cimg + ((clipis24) ? CIMG_PIC24 : CIMG_PIC8);
clipcmap = cimg + CIMG_CMAP;
/* determine if we're going to promote 'pic' to 24-bits (if it isn't
* already, because if we *are*, we'd prefer to do any clipboard rescaling
* in 24-bit space for the obvious reasons.
*
* possibilities:
* PIC24 - easy, do clipboard rescale in 24-bit space
* PIC8, and clipboard is 8 bits, (or 24-bits, but with <=256 colors)
* and total unique colors < 256:
* do 8-bit rescale & paste, don't ask anything
* PIC8, and clipboard is 24 bits, or 8-bits but total # of cols >256:
* *ask* if they want to promote pic. if so, do it, otherwise
* do clipboard rescale in 8-bits, and do the best we can...
*/
GetSelRCoords(&sx, &sy, &sw, &sh); /* sel rect in pic coords */
/* dx,dy,dw,dh is the rectangle (in PIC coords) where the paste will occur
(cropped to be entirely within PIC */
dx = sx; dy = sy; dw = sw; dh = sh;
CropRect2Rect(&dx, &dy, &dw, &dh, 0, 0, pWIDE, pHIGH);
/* cx,cy,cw,ch is the rectangle of the clipboard data (in clipboard coords)
that will actually be used in the paste operation */
cx = (sx>=0) ? 0 : ((-sx) * clipw) / sw;
cy = (sy>=0) ? 0 : ((-sy) * cliph) / sh;
cw = (dw * clipw) / sw;
ch = (dh * cliph) / sh;
/* see if we have to ask any questions */
if (picType == PIC8) {
int ncc, keep8;
char buf[512];
if (clipis24) { /* pasting in a 24-bit image that *requires* promotion */
static const char *labels[] = { "\nOkay", "\033Cancel" };
strcpy(buf, "Warning: Pasting this 24-bit image will require ");
strcat(buf, "promoting the current image to 24 bits.");
if (PopUp(buf, labels, 2)) goto exit; /* Cancelled */
else Change824Mode(PIC24); /* promote pic to 24 bits */
}
else { /* clip is 8 bits */
ncc = countNewCols(clippic,clipw,cliph,clipcmap,clipis24,cx,cy,cw,ch);
if (ncc + numcols > 256) {
static const char *labels[] = { "\nPromote", "8Keep 8-bit", "\033Cancel" };
strcpy(buf,"Warning: The image and the clipboard combine to have ");
strcat(buf,"more than 256 unique colors. Promoting the ");
strcat(buf,"image to 24 bits is recommended, otherwise the contents ");
strcat(buf,"of the clipboard will probably lose some colors.");
keep8 = PopUp(buf, labels, 3);
if (keep8==2) goto exit; /* Cancel */
else if (keep8==0) Change824Mode(PIC24); /* promote pic to 24 bits */
}
}
}
/* legal possibilities at this point:
* pic is PIC24: clip is 8 or 24
* pic is PIC8: clip is 8, or clip is 24 but has 256 or fewer colors
*/
if (picType == PIC8) {
int clx, cly, r,g,b,k,mind,close,newcols;
byte *cp, *clp, *pp, newr[256], newg[256], newb[256], remap[256];
byte order[256], trans[256];
int bperpix, dpncols;
dpic = (byte *) malloc((size_t) dw * dh);
if (!dpic) FatalError("Out of memory in DoImgPaste()\n");
bperpix = (clipis24) ? 3 : 1;
newcols = 0;
/* dpic = a scaled, 8-bit representation of clippic[cx,cy,cw,ch] */
if (!clipis24) { /* copy colormap from clip data into newr,g,b[] */
for (i=0; i<256; i++) {
newr[i] = clipcmap[i*3];
newg[i] = clipcmap[i*3 + 1];
newb[i] = clipcmap[i*3 + 2];
}
}
for (i=0; i<dh; i++) { /* un-smooth 8-bit resize */
dp = dpic + i*dw;
cly = cy + (i * ch) / dh;
clp = clippic + (cly*clipw * bperpix);
for (j=0; j<dw; j++, dp++) {
/* get appropriate pixel from clippic */
clx = cx + (j * cw) / dw;
cp = clp + (clx * bperpix);
if (!clipis24) *dp = *cp;
else { /* build colormap as we go... */
r = *cp++; g = *cp++; b = *cp++;
/* look it up in new colormap, add if not there */
for (k=0; k<newcols && (r!=newr[k] || g!=newg[k] ||b!=newb[k]); k++);
if (k==newcols && k<256) {
newr[k]=r; newg[k]=g; newb[k]=b; newcols++;
}
*dp = (byte) (k & 0xff);
}
}
}
SortColormap(dpic, dw, dh, &dpncols, newr,newg,newb, order, trans);
for (i=0, dp=dpic; i<dw*dh; i++, dp++) *dp = trans[*dp];
if (istran) {
if (!clipis24) trval = trans[trval];
else {
for (i=0; i<dpncols; i++) {
if (cimg[CIMG_TRR] == newr[i] &&
cimg[CIMG_TRG] == newg[i] &&
cimg[CIMG_TRB] == newb[i]) { trval = i; break; }
}
}
}
/* COLORMAP MERGING */
newcols = 0;
for (i=0; i<dpncols; i++) {
if (istran && i==trval) continue;
for (j=0; j<numcols; j++) { /* look for an exact match */
if (rMap[j]==newr[i] && gMap[j]==newg[i] && bMap[j]==newb[i]) break;
}
if (j<numcols) remap[i] = j;
else { /* no exact match */
newcols++;
if (numcols < 256) {
rMap[numcols] = newr[i];
gMap[numcols] = newg[i];
bMap[numcols] = newb[i];
remap[i] = numcols;
numcols++;
}
else { /* map to closest in image colormap */
r = newr[i]; g=newg[i]; b=newb[i];
mind = 256*256 + 256*256 + 256*256;
for (j=close=0; j<numcols; j++) {
k = ((rMap[j]-r) * (rMap[j]-r)) +
((gMap[j]-g) * (gMap[j]-g)) +
((bMap[j]-b) * (bMap[j]-b));
if (k<mind) { mind = k; close = j; }
}
remap[i] = (byte) close;
}
}
}
/* copy the data into PIC */
dp = dpic;
for (i=dy; i<dy+dh; i++) {
pp = pic + (i*pWIDE) + dx;
for (j=dx; j<dx+dw; j++) {
if (istran && *dp==trval) { pp++; dp++; }
else { *pp++ = remap[*dp++]; }
}
}
free(dpic);
if (newcols) InstallNewPic(); /* does color reallocation, etc. */
else {
GenerateCpic();
GenerateEpic(eWIDE, eHIGH);
DrawEpic();
}
}
/******************** PIC24 handling **********************/
else {
byte *tmppic, *cp, *pp, *clp;
int bperpix;
int trr, trg, trb, clx, cly;
trr = trg = trb = 0;
if (istran) {
if (clipis24) {
trr = cimg[CIMG_TRR];
trg = cimg[CIMG_TRG];
trb = cimg[CIMG_TRB];
}
else {
trr = clipcmap[trval*3];
trg = clipcmap[trval*3+1];
trb = clipcmap[trval*3+2];
}
}
bperpix = (clipis24) ? 3 : 1;
if (!istran && (cw != dw || ch != dh)) { /* need to resize, can smooth */
byte rmap[256], gmap[256], bmap[256];
tmppic = (byte *) malloc((size_t) cw * ch * bperpix);
if (!tmppic) FatalError("Out of memory in DoImgPaste()\n");
/* copy relevant hunk of clippic into tmppic (Smooth24 only works on
complete images */
for (i=0; i<ch; i++) {
dp = tmppic + i*cw*bperpix;
cp = clippic + ((i+cy)*clipw + cx) * bperpix;
for (j=0; j<cw*bperpix; j++) *dp++ = *cp++;
}
if (!clipis24) {
for (i=0; i<256; i++) {
rmap[i] = clipcmap[i*3];
gmap[i] = clipcmap[i*3+1];
bmap[i] = clipcmap[i*3+2];
}
}
dpic = Smooth24(tmppic, clipis24, cw,ch, dw,dh, rmap,gmap,bmap);
if (!dpic) FatalError("Out of memory (2) in DoImgPaste()\n");
free(tmppic);
/* copy the resized, smoothed, 24-bit data into 'pic' */
/* XXX: (deal with smooth-resized transparent imgs) */
dp = dpic;
for (i=dy; i<dy+dh; i++) {
pp = pic + (i*pWIDE + dx) * 3;
for (j=0; j<dw; j++) {
if (istran && dp[0]==trr && dp[1]==trg && dp[2]==trb) {
pp +=3; dp += 3;
} else {
*pp++ = *dp++; *pp++ = *dp++; *pp++ = *dp++;
}
}
}
free(dpic);
}
else { /* can't do smooth resize. Do non-smooth resize (if any!) */
for (i=0; i<dh; i++) {
pp = pic + ((i+dy)*pWIDE + dx) * 3;
cly = cy + (i * ch) / dh;
clp = clippic + (cly*clipw * bperpix);
for (j=0; j<dw; j++, pp+=3) {
clx = cx + (j * cw) / dw;
cp = clp + (clx * bperpix);
if (clipis24) {
if (!istran || cp[0]!=trr || cp[1]!=trg || cp[2]==trb) {
pp[0] = *cp++; pp[1] = *cp++; pp[2] = *cp++;
}
}
else { /* clip is 8 bit */
if (!istran || *cp != trval) {
pp[0] = clipcmap[*cp * 3];
pp[1] = clipcmap[*cp * 3 + 1];
pp[2] = clipcmap[*cp * 3 + 2];
}
}
}
}
}
GenerateCpic();
GenerateEpic(eWIDE, eHIGH);
DrawEpic();
}
exit:
SetCursors(-1);
}
/********************************************/
static void buildCursors()
{
Pixmap p1,p2,p3,p4;
XColor cfg, cbg;
dragcurs = XCreateFontCursor(theDisp, XC_fleur);
p1 = XCreatePixmapFromBitmapData(theDisp, rootW, (char *) cut_bits,
cut_width, cut_height, 1L, 0L, 1);
p2 = XCreatePixmapFromBitmapData(theDisp, rootW, (char *) cutm_bits,
cutm_width, cutm_height, 1L, 0L, 1);
p3 = XCreatePixmapFromBitmapData(theDisp, rootW, (char *) copy_bits,
copy_width, copy_height, 1L, 0L, 1);
p4 = XCreatePixmapFromBitmapData(theDisp, rootW, (char *) copym_bits,
copym_width, copym_height, 1L, 0L, 1);
if (p1 && p2 && p3 && p4) {
cfg.red = cfg.green = cfg.blue = 0;
cbg.red = cbg.green = cbg.blue = 0xffff;
cutcurs = XCreatePixmapCursor(theDisp, p1,p2, &cfg, &cbg,
cut_x_hot, cut_y_hot);
copycurs = XCreatePixmapCursor(theDisp, p3,p4, &cfg, &cbg,
copy_x_hot, copy_y_hot);
if (!cutcurs || !copycurs) FatalError("can't create cut/copy cursors...");
}
if (p1) XFreePixmap(theDisp, p1);
if (p2) XFreePixmap(theDisp, p2);
if (p3) XFreePixmap(theDisp, p3);
if (p4) XFreePixmap(theDisp, p4);
}
/********************************************/
static byte *getSelection()
{
/* alloc's and builds image with values based on currently selected
* portion of the image. Returns NULL on failure
*
* also note: getSelection will always fill trans,r,g,b with 0, for now
* as you can't 'select' transparent regions. Other code (TextPaste()),
* *can* generate semi-transparent objects to be pasted
*/
byte *pp, *dp, *cimg;
int i, j, k, x, y, w, h, do24, len;
if (!CutAllowed()) { XBell(theDisp, 0); return (byte *) NULL; }
if (!HaveSelection()) return (byte *) NULL;
GetSelRCoords(&x,&y,&w,&h);
CropRect2Rect(&x,&y,&w,&h, 0,0,pWIDE,pHIGH);
/* make selection be entirely within image */
EnableSelection(0);
selrx = x; selry = y; selrw = w; selrh = h;
EnableSelection(1);
if (picType == PIC24 && countcols24(pic,pWIDE,pHIGH, x,y,w,h)>256) {
do24=1;
len = CIMG_PIC24 + w*h*3;
}
else {
do24=0;
len = CIMG_PIC8 + w*h;
}
cimg = (byte *) malloc((size_t) len);
if (!cimg) {
ErrPopUp("Unable to malloc() temporary space for the selection.",
"\nByte Me!");
return (byte *) NULL;
}
cimg[CIMG_LEN ] = len & 0xff;
cimg[CIMG_LEN+1] = (len>> 8) & 0xff;
cimg[CIMG_LEN+2] = (len>>16) & 0xff;
cimg[CIMG_LEN+3] = (len>>24) & 0xff;
cimg[CIMG_W ] = w & 0xff;
cimg[CIMG_W+1] = (w>>8) & 0xff;
cimg[CIMG_H ] = h & 0xff;
cimg[CIMG_H+1] = (h>>8) & 0xff;
cimg[CIMG_24] = do24;
cimg[CIMG_TRANS] = 0;
if (picType == PIC24 && !do24) { /* 24-bit data as 8-bit */
int nc,pr,pg,pb;
byte *cm;
nc = 0;
dp = cimg + CIMG_PIC8;
for (i=y; i<y+h; i++) {
pp = pic + i*pWIDE*3 + x*3;
for (j=x; j<x+w; j++, pp+=3) {
pr = pp[0]; pg = pp[1]; pb = pp[2];
cm = cimg + CIMG_CMAP;
for (k=0; k<nc; k++,cm+=3) {
if (pr==cm[0] && pg==cm[1] && pb==cm[2]) break;
}
if (k==nc) {
nc++;
cimg[CIMG_CMAP + nc*3 ] = pr;
cimg[CIMG_CMAP + nc*3 + 1] = pg;
cimg[CIMG_CMAP + nc*3 + 2] = pb;
}
*dp++ = (byte) k;
}
}
}
else if (picType == PIC24) { /* 24-bit data as 24-bit */
dp = cimg + CIMG_PIC24;
for (i=y; i<y+h; i++) {
pp = pic + i*pWIDE*3 + x*3;
for (j=x; j<x+w; j++) {
*dp++ = *pp++;
*dp++ = *pp++;
*dp++ = *pp++;
}
}
}
else if (picType == PIC8) { /* 8-bit selection */
byte *cm = cimg + CIMG_CMAP;
for (i=0; i<256; i++) { /* copy colormap */
if (i<numcols) {
*cm++ = rMap[i];
*cm++ = gMap[i];
*cm++ = bMap[i];
}
}
dp = cimg + CIMG_PIC8;
for (i=y; i<y+h; i++) { /* copy image */
pp = pic + i*pWIDE + x;
for (j=x; j<x+w; j++) *dp++ = *pp++;
}
}
return cimg;
}
/********************************************/
static byte *getFromClip()
{
/* gets whatever data is on the clipboard, in CIMG_* format */
Atom clipAtom, actType;
int i, actFormat, len;
unsigned long nitems, nleft;
byte *data, *data1, lbuf[4];
FILE *fp;
char str[512];
if (forceClipFile) { /* remove property, if any */
clipAtom = XInternAtom(theDisp, CLIPPROP, True);
if (clipAtom != None) XDeleteProperty(theDisp, rootW, clipAtom);
}
clipAtom = XInternAtom(theDisp, CLIPPROP, True); /* find prop */
if (clipAtom != None) {
/* try to retrieve the length of the data in the property */
i = XGetWindowProperty(theDisp, rootW, clipAtom, 0L, 1L, False, XA_STRING,
&actType, &actFormat, &nitems, &nleft,
(unsigned char **) &data);
if (i==Success && actType==XA_STRING && actFormat==8 && nleft>0) {
/* got some useful data */
len = data[0];
len |= ((int) data[1])<<8;
len |= ((int) data[2])<<16;
len |= ((int) data[3])<<24;
XFree((void *) data);
/* read the rest of the data (len bytes) */
i = XGetWindowProperty(theDisp, rootW, clipAtom, 1L,
(long) ((len-4)+3)/4,
False, XA_STRING, &actType, &actFormat, &nitems,
&nleft, (unsigned char **) &data);
if (i==Success) {
/* copy data into regular 'malloc'd space, so we won't have to use
XFree() to get rid of it in calling procs */
data1 = (byte *) malloc((size_t) len);
if (!data1) {
XFree((void *) data);
ErrPopUp("Insufficient memory to retrieve clipboard!", "\nShucks!");
return (byte *) NULL;
}
data1[0] = len & 0xff;
data1[1] = (len>> 8) & 0xff;
data1[2] = (len>>16) & 0xff;
data1[3] = (len>>24) & 0xff;
xvbcopy((char *) data, (char *) data1+4, (size_t) len-4);
XFree((void *) data);
return data1;
}
}
}
/* if we're still here, then the prop method was less than successful.
use the file method, instead */
if (!clipfname) makeClipFName();
fp = fopen(clipfname, "r");
if (!fp) {
unlink(clipfname);
sprintf(str, "Can't read clipboard file '%s'\n\n %s.",
clipfname, ERRSTR(errno));
ErrPopUp(str,"\nBletch!");
return (byte *) NULL;
}
/* get data length */
if (fread((char *) lbuf, (size_t) 1, (size_t) 4, fp) != 4) {
fclose(fp);
unlink(clipfname);
sprintf(str, "Error occurred while reading clipboard file.\n\n %s.",
ERRSTR(errno));
ErrPopUp(str,"\nGlork!");
return (byte *) NULL;
}
len = lbuf[0];
len |= ((int) lbuf[1])<<8;
len |= ((int) lbuf[2])<<16;
len |= ((int) lbuf[3])<<24;
data = (byte *) malloc((size_t) len);
if (!data) {
ErrPopUp("Insufficient memory to retrieve clipboard!", "\nShucks!");
return (byte *) NULL;
}
data[0] = len & 0xff;
data[1] = (len>> 8) & 0xff;
data[2] = (len>>16) & 0xff;
data[3] = (len>>24) & 0xff;
/* get data */
if (fread((char *) data+4, (size_t) 1, (size_t) len-4, fp) != len-4) {
fclose(fp);
free(data);
unlink(clipfname);
sprintf(str, "Error occurred while reading clipboard file.\n\n %s.",
ERRSTR(errno));
ErrPopUp(str,"\nNertz!");
return (byte *) NULL;
}
fclose(fp);
return data;
}
/********************************************/
void SaveToClip(cimg)
byte *cimg;
{
/* takes the 'thing' pointed to by data and sticks it on the clipboard.
always tries to use the property method. If it gets a BadAlloc
error (the X server ran out of memory (ie, probably an X terminal)),
it deletes the property, and falls back to using the file method */
Atom clipAtom;
FILE *fp;
char str[512];
int len;
if (!cimg) return;
len = ((int) cimg[CIMG_LEN + 0]) |
((int) (cimg[CIMG_LEN + 1]<<8)) |
((int) (cimg[CIMG_LEN + 2]<<16)) |
((int) (cimg[CIMG_LEN + 3]<<24));
if (forceClipFile) { /* remove property, if any */
clipAtom = XInternAtom(theDisp, CLIPPROP, True);
if (clipAtom != None) XDeleteProperty(theDisp, rootW, clipAtom);
}
if (!forceClipFile) {
clipAtom = XInternAtom(theDisp, CLIPPROP, False); /* find or make prop */
if (clipAtom != None) {
/* try to store the data in the property */
xerrcode = 0;
XChangeProperty(theDisp, rootW, clipAtom, XA_STRING, 8, PropModeReplace,
cimg, len);
XSync(theDisp, False); /* make it happen *now* */
if (!xerrcode) return; /* success! */
/* failed, use file method */
XDeleteProperty(theDisp, rootW, clipAtom);
}
}
/* if we're still here, try the file method */
if (!clipfname) makeClipFName();
fp = fopen(clipfname, "w");
if (!fp) {
unlink(clipfname);
sprintf(str, "Can't write clipboard file '%s'\n\n %s.",
clipfname, ERRSTR(errno));
ErrPopUp(str,"\nBletch!");
return;
}
if (fwrite((char *) cimg, (size_t) 1, (size_t) len, fp) != len) {
fclose(fp);
unlink(clipfname);
sprintf(str, "Error occurred while writing to clipboard file.\n\n %s.",
ERRSTR(errno));
ErrPopUp(str,"\nGlork!");
return;
}
fclose(fp);
}
/********************************************/
static void clearSelectedArea()
{
/* called by 'Cut' or 'Clear' functions. fills the selected area of the
image with either clearR,clearG,clearB (in PIC24 mode), or editColor
(in PIC8 mode). Regens and redraws the image */
int i,j,x,y,w,h;
byte *pp;
if (!HaveSelection()) return;
GetSelRCoords(&x,&y,&w,&h);
CropRect2Rect(&x,&y,&w,&h, 0,0,pWIDE,pHIGH);
if (picType == PIC24) {
for (i=y; i<y+h && i<pHIGH; i++) {
pp = pic + i*pWIDE*3 + x*3;
for (j=x; j<x+w && j<pWIDE; j++) {
*pp++ = clearR;
*pp++ = clearG;
*pp++ = clearB;
}
}
}
else { /* PIC8 */
for (i=y; i<y+h && i<pHIGH; i++) {
pp = pic + i*pWIDE + x;
for (j=x; j<x+w && j<pWIDE; j++) *pp++ = editColor;
}
}
GenerateCpic();
GenerateEpic(eWIDE,eHIGH);
DrawEpic(); /* redraws selection, also */
}
/********************************************/
static void makeClipFName()
{
const char *homedir;
if (clipfname) return;
#ifdef VMS
sprintf(clipfnstr, "SYS$LOGIN:%s", CLIPFILE);
#else
homedir = (char *) getenv("HOME");
if (!homedir) homedir = ".";
sprintf(clipfnstr, "%s/%s", homedir, CLIPFILE);
#endif
clipfname = clipfnstr;
}
/********************************************/
static int countcols24(pic, pwide, phigh, x, y, w, h)
byte *pic;
int pwide, phigh, x,y,w,h;
{
/* counts the # of unique colors in a selected rect of a 24-bit image
returns '0-256' or >256 */
int i, j, k, nc;
u_int rgb[257], col;
byte *pp;
nc = 0;