This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
xvimage.c
3356 lines (2604 loc) · 85.4 KB
/
xvimage.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
/*
* xvimage.c - image manipulation functions (crop,resize,rotate...) for XV
*
* Contains:
* void Resize(int, int)
* void GenerateCpic()
* void GenerateEpic(w,h)
* void Crop()
* void UnCrop()
* void AutoCrop()
* void DoCrop(x,y,w,h)
* void Rotate(int)
* void RotatePic();
* void InstallNewPic(void);
* void DrawEpic(void);
* byte *FSDither()
* void CreateXImage()
* void Set824Menus( pictype );
* void Change824Mode( pictype );
* int DoPad(mode, str, wide, high, opaque, omode);
* int LoadPad(pinfo, fname);
*/
/* The following switch should better be provided at runtime for
* comparison purposes.
* At the moment it's only compile time, unfortunately.
* Who can make adaptions for use as a runtime switch by a menu option?
* [GRR 19980607: now via do_fixpix_smooth global; macro renamed to ENABLE_]
* [see http://sylvana.net/fixpix/ for home page, further info]
*/
/* #define ENABLE_FIXPIX_SMOOTH */ /* GRR 19980607: moved into xv.h */
#define NEEDSDIR /* for S_IRUSR|S_IWUSR */
#include "copyright.h"
#include "xv.h"
static void flipSel PARM((int));
static void do_zoom PARM((int, int));
static void compute_zoom_rect PARM((int, int, int*, int*, int*, int*));
static void do_unzoom PARM((void));
static void do_pan PARM((int, int));
static void do_pan_calc PARM((int, int, int *, int *));
static void crop1 PARM((int, int, int, int, int));
static int doAutoCrop24 PARM((void));
static void floydDitherize1 PARM((XImage *, byte *, int, int, int,
byte *, byte *,byte *));
#if 0 /* NOTUSED */
static int highbit PARM((unsigned long));
#endif
static int doPadSolid PARM((char *, int, int, int, int));
static int doPadBggen PARM((char *, int, int, int, int));
static int doPadLoad PARM((char *, int, int, int, int));
static int doPadPaste PARM((byte *, int, int, int, int));
static int ReadImageFile1 PARM((char *, PICINFO *));
/* The following array represents the pixel values for each shade
* of the primary color components.
* If 'p' is a pointer to a source image rgb-byte-triplet, we can
* construct the output pixel value simply by 'oring' together
* the corresponding components:
*
* unsigned char *p;
* unsigned long pixval;
*
* pixval = screen_rgb[0][*p++];
* pixval |= screen_rgb[1][*p++];
* pixval |= screen_rgb[2][*p++];
*
* This is both efficient and generic, since the only assumption
* is that the primary color components have separate bits.
* The order and distribution of bits does not matter, and we
* don't need additional variables and shifting/masking code.
* The array size is 3 KBytes total and thus very reasonable.
*/
static unsigned long screen_rgb[3][256];
/* The following array holds the exact color representations
* reported by the system.
* This is useful for less than 24 bit deep displays as a base
* for additional dithering to get smoother output.
*/
static byte screen_set[3][256];
/* The following routine initializes the screen_rgb and screen_set
* arrays.
* Since it is executed only once per program run, it does not need
* to be super-efficient.
*
* The method is to draw points in a pixmap with the specified shades
* of primary colors and then get the corresponding XImage pixel
* representation.
* Thus we can get away with any Bit-order/Byte-order dependencies.
*
* The routine uses some global X variables: theDisp, theScreen,
* and dispDEEP. Adapt these to your application as necessary.
* I've not passed them in as parameters, since for other platforms
* than X these may be different (see vfixpix.c), and so the
* screen_init() interface is unique.
*
* BUG: I've read in the "Xlib Programming Manual" from O'Reilly &
* Associates, that the DefaultColormap in TrueColor might not
* provide the full shade representation in XAllocColor.
* In this case one had to provide a 'best' colormap instead.
* However, my tests with Xaccel on a Linux-Box with a Mach64
* card were fully successful, so I leave that potential problem
* to you at the moment and would appreciate any suggestions...
*/
static void screen_init()
{
static int init_flag; /* assume auto-init as 0 */
Pixmap check_map;
GC check_gc;
XColor check_col;
XImage *check_image;
int ci, i;
if (init_flag) return;
init_flag = 1;
check_map = XCreatePixmap(theDisp, RootWindow(theDisp,theScreen),
1, 1, dispDEEP);
check_gc = XCreateGC(theDisp, check_map, 0, NULL);
for (ci = 0; ci < 3; ci++) {
for (i = 0; i < 256; i++) {
check_col.red = 0;
check_col.green = 0;
check_col.blue = 0;
/* Do proper upscaling from unsigned 8 bit (image data values)
to unsigned 16 bit (X color representation). */
((unsigned short *)&check_col.red)[ci] = (unsigned short)((i << 8) | i);
if (theVisual->class == TrueColor)
XAllocColor(theDisp, theCmap, &check_col);
else
xvAllocColor(theDisp, theCmap, &check_col);
screen_set[ci][i] =
(((unsigned short *)&check_col.red)[ci] >> 8) & 0xff;
XSetForeground(theDisp, check_gc, check_col.pixel);
XDrawPoint(theDisp, check_map, check_gc, 0, 0);
check_image = XGetImage(theDisp, check_map, 0, 0, 1, 1,
AllPlanes, ZPixmap);
if (check_image) {
switch (check_image->bits_per_pixel) {
case 8:
screen_rgb[ci][i] = *(CARD8 *)check_image->data;
break;
case 16:
screen_rgb[ci][i] = *(CARD16 *)check_image->data;
break;
case 24:
screen_rgb[ci][i] =
((unsigned long)*(CARD8 *)check_image->data << 16) |
((unsigned long)*(CARD8 *)(check_image->data + 1) << 8) |
(unsigned long)*(CARD8 *)(check_image->data + 2);
break;
case 32:
screen_rgb[ci][i] = *(CARD32 *)check_image->data;
break;
}
XDestroyImage(check_image);
}
}
}
XFreeGC(theDisp, check_gc);
XFreePixmap(theDisp, check_map);
}
#ifdef ENABLE_FIXPIX_SMOOTH
/* The following code is based in part on:
*
* jquant1.c
*
* Copyright (C) 1991-1996, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains 1-pass color quantization (color mapping) routines.
* These routines provide mapping to a fixed color map using equally spaced
* color values. Optional Floyd-Steinberg or ordered dithering is available.
*/
/* Declarations for Floyd-Steinberg dithering.
*
* Errors are accumulated into the array fserrors[], at a resolution of
* 1/16th of a pixel count. The error at a given pixel is propagated
* to its not-yet-processed neighbors using the standard F-S fractions,
* ... (here) 7/16
* 3/16 5/16 1/16
* We work left-to-right on even rows, right-to-left on odd rows.
*
* We can get away with a single array (holding one row's worth of errors)
* by using it to store the current row's errors at pixel columns not yet
* processed, but the next row's errors at columns already processed. We
* need only a few extra variables to hold the errors immediately around the
* current column. (If we are lucky, those variables are in registers, but
* even if not, they're probably cheaper to access than array elements are.)
*
* We provide (#columns + 2) entries per component; the extra entry at each
* end saves us from special-casing the first and last pixels.
*/
typedef INT16 FSERROR; /* 16 bits should be enough */
typedef int LOCFSERROR; /* use 'int' for calculation temps */
typedef struct { byte *colorset;
FSERROR *fserrors;
} FSBUF;
/* Floyd-Steinberg initialization function.
*
* It is called 'fs2_init' since it's specialized for our purpose and
* could be embedded in a more general FS-package.
*
* Returns a malloced FSBUF pointer which has to be passed as first
* parameter to subsequent 'fs2_dither' calls.
* The FSBUF structure does not need to be referenced by the calling
* application, it can be treated from the app like a void pointer.
*
* The current implementation does only require to free() this returned
* pointer after processing.
*
* Returns NULL if malloc fails.
*
* NOTE: The FSBUF structure is designed to allow the 'fs2_dither'
* function to work with an *arbitrary* number of color components
* at runtime! This is an enhancement over the IJG code base :-).
* Only fs2_init() specifies the (maximum) number of components.
*/
static FSBUF *fs2_init(width)
int width;
{
FSBUF *fs;
FSERROR *p;
fs = (FSBUF *)
malloc(sizeof(FSBUF) * 3 + ((size_t)width + 2) * sizeof(FSERROR) * 3);
if (fs == 0) return fs;
fs[0].colorset = screen_set[0];
fs[1].colorset = screen_set[1];
fs[2].colorset = screen_set[2];
p = (FSERROR *)(fs + 3);
memset(p, 0, ((size_t)width + 2) * sizeof(FSERROR) * 3);
fs[0].fserrors = p;
fs[1].fserrors = p + 1;
fs[2].fserrors = p + 2;
return fs;
}
/* Floyd-Steinberg dithering function.
*
* NOTE:
* (1) The image data referenced by 'ptr' is *overwritten* (input *and*
* output) to allow more efficient implementation.
* (2) Alternate FS dithering is provided by the sign of 'nc'. Pass in
* a negative value for right-to-left processing. The return value
* provides the right-signed value for subsequent calls!
* (3) This particular implementation assumes *no* padding between lines!
* Adapt this if necessary.
*/
static int fs2_dither(fs, ptr, nc, num_rows, num_cols)
FSBUF *fs;
byte *ptr;
int nc, num_rows, num_cols;
{
int abs_nc, ci, row, col;
LOCFSERROR delta, cur, belowerr, bpreverr;
byte *dataptr, *colsetptr;
FSERROR *errorptr;
if ((abs_nc = nc) < 0) abs_nc = -abs_nc;
for (row = 0; row < num_rows; row++) {
for (ci = 0; ci < abs_nc; ci++, ptr++) {
dataptr = ptr;
colsetptr = fs[ci].colorset;
errorptr = fs[ci].fserrors;
if (nc < 0) {
dataptr += (num_cols - 1) * abs_nc;
errorptr += (num_cols + 1) * abs_nc;
}
cur = belowerr = bpreverr = 0;
for (col = 0; col < num_cols; col++) {
cur += errorptr[nc];
cur += 8; cur >>= 4;
if ((cur += *dataptr) < 0) cur = 0;
else if (cur > 255) cur = 255;
*dataptr = cur & 0xff;
cur -= colsetptr[cur];
delta = cur << 1; cur += delta;
bpreverr += cur; cur += delta;
belowerr += cur; cur += delta;
errorptr[0] = (FSERROR)bpreverr;
bpreverr = belowerr;
belowerr = delta >> 1;
dataptr += nc;
errorptr += nc;
}
errorptr[0] = (FSERROR)bpreverr;
}
ptr += (num_cols - 1) * abs_nc;
nc = -nc;
}
return nc;
}
#endif /* ENABLE_FIXPIX_SMOOTH */
#define DO_CROP 0
#define DO_ZOOM 1
/***********************************/
void Resize(w,h)
int w,h;
{
RANGE(w,1,maxWIDE); RANGE(h,1,maxHIGH);
if (HaveSelection()) DrawSelection(0); /* turn off old rect */
if (psUp) PSResize(); /* if PSDialog is open, mention size change */
/* if same size, and Ximage created, do nothing */
if (w==eWIDE && h==eHIGH && theImage!=NULL) return;
if (DEBUG) fprintf(stderr,"Resize(%d,%d) eSIZE=%d,%d cSIZE=%d,%d\n",
w,h,eWIDE,eHIGH,cWIDE,cHIGH);
if (epicMode == EM_SMOOTH) { /* turn off smoothing */
epicMode = EM_RAW; SetEpicMode();
}
GenerateEpic(w,h);
CreateXImage();
}
/********************************************/
void GenerateCpic()
{
/* called when 'pic' has been modified (different contents, *not* different
size, orientation, etc. Rebuilds cpic. */
int i, j, bperpix;
byte *pp, *cp;
if (cpic == pic) return; /* no cropping, nothing to do */
cp = cpic;
bperpix = (picType == PIC8) ? 1 : 3;
for (i=0; i<cHIGH; i++) {
if ((i&63)==0) WaitCursor();
pp = pic + (i+cYOFF) * (pWIDE*bperpix) + (cXOFF * bperpix);
for (j=0; j<cWIDE*bperpix; j++)
*cp++ = *pp++;
}
}
/***********************************/
void GenerateEpic(w,h)
int w,h;
{
int cy,ex,ey,*cxarr, *cxarrp;
byte *clptr,*elptr,*epptr;
WaitCursor();
clptr = NULL; cxarrp = NULL; cy = 0; /* shut up compiler */
SetISTR(ISTR_EXPAND, "%.5g%% x %.5g%% (%d x %d)",
100.0 * ((float) w) / cWIDE,
100.0 * ((float) h) / cHIGH, w, h);
if (DEBUG)
fprintf(stderr,"GenerateEpic(%d,%d) eSIZE=%d,%d cSIZE=%d,%d epicode=%d\n",
w,h,eWIDE,eHIGH,cWIDE,cHIGH, epicMode);
FreeEpic(); /* get rid of the old one */
eWIDE = w; eHIGH = h;
if (epicMode == EM_SMOOTH) {
if (picType == PIC8) {
epic = SmoothResize(cpic, cWIDE, cHIGH, eWIDE, eHIGH,
rMap,gMap,bMap, rdisp,gdisp,bdisp, numcols);
}
else { /* PIC24 */
epic = Smooth24(cpic, 1, cWIDE, cHIGH, eWIDE, eHIGH, NULL, NULL, NULL);
}
if (epic) return; /* success */
else {
/* failed. Try to generate a *raw* image, at least... */
epicMode = EM_RAW; SetEpicMode();
/* fall through to rest of code */
}
}
/* generate a 'raw' epic, as we'll need it for ColorDither if EM_DITH */
if (eWIDE==cWIDE && eHIGH==cHIGH) { /* 1:1 expansion. point epic at cpic */
epic = cpic;
}
else {
/* run the rescaling algorithm */
int bperpix;
bperpix = (picType == PIC8) ? 1 : 3;
WaitCursor();
/* create a new epic of the appropriate size */
epic = (byte *) malloc((size_t) (eWIDE * eHIGH * bperpix));
if (!epic) FatalError("GenerateEpic(): unable to malloc 'epic'");
/* the scaling routine. not really all that scary after all... */
/* OPTIMIZATON: Malloc an eWIDE array of ints which will hold the
values of the equation px = (pWIDE * ex) / eWIDE. Faster than doing
a mul and a div for every point in picture */
cxarr = (int *) malloc(eWIDE * sizeof(int));
if (!cxarr) FatalError("unable to allocate cxarr");
for (ex=0; ex<eWIDE; ex++)
cxarr[ex] = bperpix * ((cWIDE * ex) / eWIDE);
elptr = epptr = epic;
for (ey=0; ey<eHIGH; ey++, elptr+=(eWIDE*bperpix)) {
ProgressMeter(0, (eHIGH)-1, ey, "Resize");
if ((ey&63) == 0) WaitCursor();
cy = (cHIGH * ey) / eHIGH;
epptr = elptr;
clptr = cpic + (cy * cWIDE * bperpix);
if (bperpix == 1) {
for (ex=0, cxarrp = cxarr; ex<eWIDE; ex++, epptr++)
*epptr = clptr[*cxarrp++];
}
else {
int j; byte *cp;
for (ex=0, cxarrp = cxarr; ex<eWIDE; ex++,cxarrp++) {
cp = clptr + *cxarrp;
for (j=0; j<bperpix; j++)
*epptr++ = *cp++;
}
}
}
free(cxarr);
}
/* at this point, we have a raw epic. Potentially dither it */
if (picType == PIC8 && epicMode == EM_DITH) {
byte *tmp;
tmp = DoColorDither(NULL, epic, eWIDE, eHIGH, rMap,gMap,bMap,
rdisp,gdisp,bdisp, numcols);
if (tmp) { /* success */
FreeEpic();
epic = tmp;
}
else { /* well... just use the raw image. */
epicMode = EM_RAW; SetEpicMode();
}
}
}
/***********************************/
void DoZoom(x,y,button)
int x, y;
unsigned int button;
{
if (button == Button1) do_zoom(x,y);
else if (button == Button2) do_pan(x,y);
else if (button == Button3) do_unzoom();
else XBell(theDisp,0);
}
/***********************************/
static void do_zoom(mx,my)
int mx,my;
{
int i;
int rx,ry,rx2,ry2, orx, ory, orw, orh;
int px,py,pw,ph,opx,opy,opw,oph,m;
Window rW, cW; unsigned int mask; int rtx, rty;
m = 0;
XSetFunction(theDisp, theGC, GXinvert);
XSetPlaneMask(theDisp, theGC, xorMasks[m]);
compute_zoom_rect(mx, my, &px, &py, &pw, &ph);
CoordP2E(px, py, &rx, &ry);
CoordP2E(px+pw, py+ph, &rx2,&ry2);
opx=px; opy=py; opw=pw; oph=ph;
orx = rx; ory = ry; orw = (rx2-rx); orh = (ry2-ry);
XDrawRectangle(theDisp,mainW,theGC,orx,ory, (u_int)orw, (u_int)orh);
/* track until Button1 is released. If ctrl is released, cancel */
while (1) {
if (!XQueryPointer(theDisp,mainW,&rW,&cW,&rtx,&rty,
&mx,&my,&mask)) continue;
if (!(mask & ControlMask)) break;
if (!(mask & Button1Mask)) break; /* button released */
compute_zoom_rect(mx, my, &px, &py, &pw, &ph);
if (px!=opx || py!=opy) {
XDrawRectangle(theDisp,mainW,theGC, orx,ory, (u_int)orw, (u_int)orh);
opx = px; opy = py; opw = pw; opy = py;
CoordP2E(opx, opy, &rx, &ry);
CoordP2E(opx+opw, opy+oph, &rx2,&ry2);
orx = rx; ory = ry; orw = (rx2-rx); orh = (ry2-ry);
XDrawRectangle(theDisp,mainW,theGC, orx,ory, (u_int)orw, (u_int)orh);
}
else {
XDrawRectangle(theDisp,mainW,theGC, orx,ory, (u_int)orw, (u_int)orh);
m = (m+1)&7;
XSetPlaneMask(theDisp, theGC, xorMasks[m]);
XDrawRectangle(theDisp,mainW,theGC, orx,ory, (u_int)orw, (u_int)orh);
XFlush(theDisp);
Timer(100);
}
}
if (!(mask & ControlMask)) { /* cancelled */
XDrawRectangle(theDisp, mainW, theGC, orx, ory, (u_int) orw, (u_int) orh);
XSetFunction(theDisp, theGC, GXcopy);
XSetPlaneMask(theDisp, theGC, AllPlanes);
return;
}
for (i=0; i<4; i++) {
XDrawRectangle(theDisp, mainW, theGC, orx, ory, (u_int) orw, (u_int) orh);
XFlush(theDisp);
Timer(100);
}
XSetFunction(theDisp, theGC, GXcopy);
XSetPlaneMask(theDisp, theGC, AllPlanes);
/* if rectangle is *completely* outside epic, don't zoom */
if (orx+orw<0 || ory+orh<0 || orx>=eWIDE || ory>=eHIGH) return;
crop1(opx, opy, opw, oph, DO_ZOOM);
}
/***********************************/
static void compute_zoom_rect(x, y, px, py, pw, ph)
int x, y, *px, *py, *pw, *ph;
{
/* given a mouse pos (in epic coords), return x,y,w,h PIC coords for
a 'zoom in by 2x' rectangle to be tracked. The rectangle stays
completely within 'pic' boundaries, and moves in 'pic' increments */
CoordE2P(x, y, px, py);
*pw = (cWIDE+1)/2;
*ph = (cHIGH+1)/2;
*px = *px - (*pw)/2;
*py = *py - (*ph)/2;
RANGE(*px, 0, pWIDE - *pw);
RANGE(*py, 0, pHIGH - *ph);
}
/***********************************/
static void do_unzoom()
{
int x,y,w,h, x2,y2, ex,ey,ew,eh;
/* compute a cropping rectangle (in pic coordinates) that's twice
the size of eWIDE,eHIGH, centered around eWIDE/2, eHIGH/2, but no
larger than pWIDE,PHIGH */
if (!but[BUNCROP].active) { /* not cropped, can't zoom out */
XBell(theDisp, 0);
return;
}
ex = -eWIDE/2; ey = -eHIGH/2;
ew = eWIDE*2; eh = eHIGH*2;
CoordE2P(ex, ey, &x, &y);
CoordE2P(ex+ew, ey+eh, &x2, &y2);
w = x2 - x; h = y2 - y;
RANGE(w, 1, pWIDE);
RANGE(h, 1, pHIGH);
if (x<0) x = 0;
if (y<0) y = 0;
if (x+w > pWIDE) x = pWIDE - w;
if (y+h > pHIGH) y = pHIGH - h;
crop1(x,y,w,h, DO_ZOOM);
}
/***********************************/
static void do_pan(mx,my)
int mx,my;
{
int i, ox,oy,offx,offy, rw,rh, px, py, dx, dy,m;
Window rW, cW; unsigned int mask; int rx, ry;
offx = ox = mx;
offy = oy = my;
rw = eWIDE-1; rh = eHIGH-1;
m = 0;
XSetFunction(theDisp, theGC, GXinvert);
XSetPlaneMask(theDisp, theGC, xorMasks[m]);
XDrawRectangle(theDisp,mainW,theGC, mx-offx, my-offy, (u_int)rw, (u_int)rh);
/* track until Button2 is released */
while (1) {
if (!XQueryPointer(theDisp, mainW, &rW, &cW, &rx, &ry,
&mx, &my, &mask)) continue;
if (!(mask & ControlMask)) break; /* cancelled */
if (!(mask & Button2Mask)) break; /* button released */
if (mask & ShiftMask) { /* constrain mx,my to horiz or vertical */
if (abs(mx-offx) > abs(my-offy)) my = offy;
else mx = offx;
}
do_pan_calc(offx, offy, &mx, &my);
if (mx!=ox || my!=oy) {
XDrawRectangle(theDisp, mainW, theGC, ox-offx, oy-offy,
(u_int) rw, (u_int) rh);
XDrawRectangle(theDisp, mainW, theGC, mx-offx, my-offy,
(u_int) rw, (u_int) rh);
ox = mx; oy = my;
}
else {
XDrawRectangle(theDisp, mainW, theGC, ox-offx, oy-offy,
(u_int) rw, (u_int) rh);
m = (m+1)&7;
XSetPlaneMask(theDisp, theGC, xorMasks[m]);
XDrawRectangle(theDisp, mainW, theGC, ox-offx, oy-offy,
(u_int) rw, (u_int) rh);
XFlush(theDisp);
Timer(100);
}
}
mx = ox; my = oy; /* in case mx,my changed on button release */
if (!(mask & ControlMask)) { /* cancelled */
XDrawRectangle(theDisp, mainW, theGC, mx-offx, my-offy,
(u_int) rw, (u_int) rh);
XSetFunction(theDisp, theGC, GXcopy);
XSetPlaneMask(theDisp, theGC, AllPlanes);
return;
}
for (i=0; i<4; i++) {
XDrawRectangle(theDisp, mainW, theGC, mx-offx, my-offy,
(u_int) rw, (u_int) rh);
XFlush(theDisp);
Timer(100);
}
/* mx-offx, my-offy is top-left corner of pan rect, in epic coords */
CoordE2P(mx-offx, my-offy, &px, &py);
dx = px - cXOFF; dy = py - cYOFF;
if (dx==0 && dy==0) { /* didn't pan anywhere */
XDrawRectangle(theDisp, mainW, theGC, mx-offx, my-offy,
(u_int) rw, (u_int) rh);
XSetFunction(theDisp, theGC, GXcopy);
XSetPlaneMask(theDisp, theGC, AllPlanes);
return;
}
XSetFunction(theDisp, theGC, GXcopy);
XSetPlaneMask(theDisp, theGC, AllPlanes);
crop1(cXOFF-dx, cYOFF-dy, cWIDE, cHIGH, DO_ZOOM);
}
/***********************************/
static void do_pan_calc(offx, offy, xp,yp)
int offx, offy, *xp, *yp;
{
/* given mouse coords (in xp,yp) and original offset, compute 'clipped'
coords (returned in xp,yp) such that the 'pan window' remains entirely
within the image boundaries */
int mx, my, eprx, epry, eprw, eprh, pprx, ppry, pprw, pprh;
mx = *xp; my = *yp;
/* compute corners of pan rect in eWIDE,eHIGH coords */
eprx = offx - mx;
epry = offy - my;
eprw = eWIDE;
eprh = eHIGH;
/* compute corners of pan rect in pWIDE,pHIGH coords */
CoordE2P(eprx, epry, &pprx, &ppry);
pprw = cWIDE;
pprh = cHIGH;
/* if pan rect (in p* coords) is outside bounds of pic, move it inside */
if (pprx<0) pprx = 0;
if (ppry<0) ppry = 0;
if (pprx + pprw > pWIDE) pprx = pWIDE-pprw;
if (ppry + pprh > pHIGH) ppry = pHIGH-pprh;
/* convert clipped pan rect back into eWIDE,eHIGH coords */
CoordP2E(pprx, ppry, &eprx, &epry);
*xp = offx - eprx;
*yp = offy - epry;
}
/***********************************/
void Crop()
{
int x, y, w, h;
if (!HaveSelection()) return;
GetSelRCoords(&x,&y,&w,&h);
EnableSelection(0);
crop1(x,y,w,h,DO_CROP);
}
/**********************************/
static void crop1(x,y,w,h,zm)
int x,y,w,h,zm;
{
int oldew,oldeh,oldcx,oldcy;
oldcx = cXOFF; oldcy = cYOFF;
oldew = eWIDE; oldeh = eHIGH;
DoCrop(x,y,w,h);
if (zm == DO_ZOOM) { eWIDE = oldew; eHIGH = oldeh; }
GenerateEpic(eWIDE, eHIGH);
if (useroot) DrawEpic();
else {
if (zm == DO_CROP) {
WCrop(eWIDE, eHIGH, cXOFF-oldcx, cYOFF-oldcy); /* shrink window */
CreateXImage();
}
else DrawEpic();
}
SetCursors(-1);
}
/***********************************/
void UnCrop()
{
int w,h;
if (cpic == pic) return; /* not cropped */
BTSetActive(&but[BUNCROP],0);
if (epicMode == EM_SMOOTH) { /* turn off smoothing */
epicMode = EM_RAW; SetEpicMode();
}
/* dispose of old cpic and epic */
FreeEpic();
if (cpic && cpic != pic) free(cpic);
cpic = NULL;
w = (pWIDE * eWIDE) / cWIDE; h = (pHIGH * eHIGH) / cHIGH;
if (w>maxWIDE || h>maxHIGH) {
/* return to 'normal' size */
if (pWIDE>maxWIDE || pHIGH>maxHIGH) {
double r,wr,hr;
wr = ((double) pWIDE) / maxWIDE;
hr = ((double) pHIGH) / maxHIGH;
r = (wr>hr) ? wr : hr; /* r is the max(wr,hr) */
w = (int) ((pWIDE / r) + 0.5);
h = (int) ((pHIGH / r) + 0.5);
}
else { w = pWIDE; h = pHIGH; }
}
cpic = pic; cXOFF = cYOFF = 0; cWIDE = pWIDE; cHIGH = pHIGH;
/* generate an appropriate 'epic' */
GenerateEpic(w,h);
CreateXImage();
WUnCrop();
SetCropString();
}
/***********************************/
void AutoCrop()
{
/* called when AutoCrop button is pressed */
int oldcx, oldcy;
oldcx = cXOFF; oldcy = cYOFF;
if (DoAutoCrop()) {
if (useroot) DrawEpic();
else {
CreateXImage();
WCrop(eWIDE, eHIGH, cXOFF-oldcx, cYOFF-oldcy);
}
}
SetCursors(-1);
}
/***********************************/
int DoAutoCrop()
{
/* returns '1' if any cropping was actually done. */
byte *cp, *cp1;
int i, ctop, cbot, cleft, cright;
byte bgcol;
ctop = cbot = cleft = cright = 0;
if (picType == PIC24) return( doAutoCrop24() );
/* crop the top */
cp = cpic;
bgcol = cp[0];
while (ctop+1 < cHIGH) {
/* see if we can delete this line */
for (i=0, cp1=cp; i<cWIDE && *cp1==bgcol; i++, cp1++);
if (i==cWIDE) { cp += cWIDE; ctop++; }
else break;
}
/* crop the bottom */
cp = cpic + (cHIGH-1) * cWIDE;
bgcol = cp[0];
while (ctop + cbot + 1 < cHIGH) {
/* see if we can delete this line */
for (i=0, cp1=cp; i<cWIDE && *cp1==bgcol; i++,cp1++);
if (i==cWIDE) { cp -= cWIDE; cbot++; }
else break;
}
/* crop the left side */
cp = cpic;
bgcol = cp[0];
while (cleft + 1 < cWIDE) {
/* see if we can delete this line */
for (i=0, cp1=cp; i<cHIGH && *cp1==bgcol; i++, cp1 += cWIDE);
if (i==cHIGH) { cp++; cleft++; }
else break;
}
/* crop the right side */
cp = cpic + cWIDE-1;
bgcol = cp[0];
while (cleft + cright + 1 < cWIDE) {
/* see if we can delete this line */
for (i=0, cp1=cp; i<cHIGH && *cp1==bgcol; i++, cp1 += cWIDE);
if (i==cHIGH) { cp--; cright++; }
else break;
}
/* do the actual cropping */
if (cleft || ctop || cbot || cright) {
DoCrop(cXOFF+cleft, cYOFF+ctop,
cWIDE-(cleft+cright), cHIGH-(ctop+cbot));
return 1;
}
return 0;
}
/***********************************/
static int doAutoCrop24()
{
/* returns '1' if any cropping was actually done */
byte *cp, *cp1;
int i, ctop, cbot, cleft, cright;
byte bgR, bgG, bgB;
int maxmiss, misses, half;
int r, g, b, R, G, B, oldr, oldg, oldb;
# define EPSILON 39 /* up to 15% (39/256ths) variance okay */
# define NEIGHBOR 16 /* within 6% of neighboring pixels */
# define MISSPCT 6 /* and up to 6% that don't match */
# define inabsrange(a,n) ( (a) < n && (a) > -n )
if (cHIGH<3 || cWIDE<3) return 0;
ctop = cbot = cleft = cright = 0;
if (picType != PIC24) FatalError("doAutoCrop24 called when pic!=PIC24");
/* crop the top */
cp = cpic;
half = cWIDE/2 * 3;
maxmiss = cWIDE * MISSPCT / 100;
bgR = cp[half+0]; bgG = cp[half+1]; bgB = cp[half+2];
while (ctop+1 < cHIGH) { /* see if we can delete this line */
oldr = bgR; oldg = bgG; oldb = bgB;
for (i=0, misses=0, cp1=cp; i<cWIDE && misses<maxmiss; i++, cp1+=3) {
r=cp1[0]-bgR; g=cp1[1]-bgG; b=cp1[2]-bgB;
R=cp1[0]-oldr; G=cp1[1]-oldg; B=cp1[2]-oldb;
if (!inabsrange(r, EPSILON) ||
!inabsrange(r, EPSILON) ||
!inabsrange(b, EPSILON) ||
!inabsrange(R, NEIGHBOR) ||
!inabsrange(R, NEIGHBOR) ||
!inabsrange(B, NEIGHBOR)) misses++;
oldr=r; oldg=g; oldb=b;
}
if (i==cWIDE) { cp += cWIDE*3; ctop++; } /* OK, we can crop this line */
else break;
}
/* crop the bottom */
cp = cpic + (cHIGH-1) * cWIDE*3;
bgR = cp[half+0]; bgG = cp[half+1]; bgB = cp[half+2];
while (ctop + cbot + 1 < cHIGH) { /* see if we can delete this line */
oldr = bgR; oldg = bgG; oldb = bgB;
for (i=0, misses=0, cp1=cp; i<cWIDE && misses<maxmiss; i++, cp1+=3) {
r=cp1[0]-bgR; g=cp1[1]-bgG; b=cp1[2]-bgB;
R=cp1[0]-oldr; G=cp1[1]-oldg; B=cp1[2]-oldb;
if (!inabsrange(r-g, EPSILON) ||
!inabsrange(r-b, EPSILON) ||
!inabsrange(b-g, EPSILON) ||
!inabsrange(R-G, NEIGHBOR) ||
!inabsrange(R-B, NEIGHBOR) ||
!inabsrange(B-G, NEIGHBOR)) misses++;
}
if (i==cWIDE) { cp -= cWIDE*3; cbot++; }