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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <math.h>
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpmath/gimpmath.h"
#include "display-types.h"
#include "core/gimpboundary.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimp-utils.h"
#include "gimpdisplay.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-scroll.h"
#include "gimpdisplayshell-transform.h"
/* local function prototypes */
static void gimp_display_shell_transform_xy_f_noround (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gdouble *nx,
gdouble *ny);
/* public functions */
/**
* gimp_display_shell_zoom_coords:
* @shell: a #GimpDisplayShell
* @image_coords: image coordinates
* @display_coords: returns the corresponding display coordinates
*
* Zooms from image coordinates to display coordinates, so that
* objects can be rendered at the correct points on the display.
**/
void
gimp_display_shell_zoom_coords (GimpDisplayShell *shell,
const GimpCoords *image_coords,
GimpCoords *display_coords)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (image_coords != NULL);
g_return_if_fail (display_coords != NULL);
*display_coords = *image_coords;
display_coords->x = SCALEX (shell, image_coords->x);
display_coords->y = SCALEY (shell, image_coords->y);
display_coords->x -= shell->offset_x;
display_coords->y -= shell->offset_y;
}
/**
* gimp_display_shell_unzoom_coords:
* @shell: a #GimpDisplayShell
* @display_coords: display coordinates
* @image_coords: returns the corresponding image coordinates
*
* Zooms from display coordinates to image coordinates, so that
* points on the display can be mapped to points in the image.
**/
void
gimp_display_shell_unzoom_coords (GimpDisplayShell *shell,
const GimpCoords *display_coords,
GimpCoords *image_coords)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (display_coords != NULL);
g_return_if_fail (image_coords != NULL);
*image_coords = *display_coords;
image_coords->x += shell->offset_x;
image_coords->y += shell->offset_y;
image_coords->x /= shell->scale_x;
image_coords->y /= shell->scale_y;
}
/**
* gimp_display_shell_zoom_xy:
* @shell:
* @x:
* @y:
* @nx:
* @ny:
*
* Zooms an image coordinate to a shell coordinate.
**/
void
gimp_display_shell_zoom_xy (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gint *nx,
gint *ny)
{
gint64 tx;
gint64 ty;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
tx = x * shell->scale_x;
ty = y * shell->scale_y;
tx -= shell->offset_x;
ty -= shell->offset_y;
/* The projected coordinates might overflow a gint in the case of
* big images at high zoom levels, so we clamp them here to avoid
* problems.
*/
*nx = CLAMP (tx, G_MININT, G_MAXINT);
*ny = CLAMP (ty, G_MININT, G_MAXINT);
}
/**
* gimp_display_shell_unzoom_xy:
* @shell: a #GimpDisplayShell
* @x: x coordinate in display coordinates
* @y: y coordinate in display coordinates
* @nx: returns x oordinate in image coordinates
* @ny: returns y coordinate in image coordinates
* @round: if %TRUE, round the results to the nearest integer;
* if %FALSE, simply cast them to @gint.
*
* Zoom from display coordinates to image coordinates, so that
* points on the display can be mapped to the corresponding points
* in the image.
**/
void
gimp_display_shell_unzoom_xy (GimpDisplayShell *shell,
gint x,
gint y,
gint *nx,
gint *ny,
gboolean round)
{
gint64 tx;
gint64 ty;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
if (round)
{
tx = SIGNED_ROUND (((gdouble) x + shell->offset_x) / shell->scale_x);
ty = SIGNED_ROUND (((gdouble) y + shell->offset_y) / shell->scale_y);
}
else
{
tx = ((gint64) x + shell->offset_x) / shell->scale_x;
ty = ((gint64) y + shell->offset_y) / shell->scale_y;
}
*nx = CLAMP (tx, G_MININT, G_MAXINT);
*ny = CLAMP (ty, G_MININT, G_MAXINT);
}
/**
* gimp_display_shell_zoom_xy_f:
* @shell: a #GimpDisplayShell
* @x: image x coordinate of point
* @y: image y coordinate of point
* @nx: returned shell canvas x coordinate
* @ny: returned shell canvas y coordinate
*
* Zooms from image coordinates to display shell canvas
* coordinates.
**/
void
gimp_display_shell_zoom_xy_f (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gdouble *nx,
gdouble *ny)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
*nx = SCALEX (shell, x) - shell->offset_x;
*ny = SCALEY (shell, y) - shell->offset_y;
}
/**
* gimp_display_shell_unzoom_xy_f:
* @shell: a #GimpDisplayShell
* @x: x coordinate in display coordinates
* @y: y coordinate in display coordinates
* @nx: place to return x coordinate in image coordinates
* @ny: place to return y coordinate in image coordinates
*
* This function is identical to gimp_display_shell_unzoom_xy(),
* except that the input and output coordinates are doubles rather than
* ints, and consequently there is no option related to rounding.
**/
void
gimp_display_shell_unzoom_xy_f (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gdouble *nx,
gdouble *ny)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
*nx = (x + shell->offset_x) / shell->scale_x;
*ny = (y + shell->offset_y) / shell->scale_y;
}
/**
* gimp_display_shell_zoom_segments:
* @shell: a #GimpDisplayShell
* @src_segs: array of segments in image coordinates
* @dest_segs: returns the corresponding segments in display coordinates
* @n_segs: number of segments
*
* Zooms from image coordinates to display coordinates, so that
* objects can be rendered at the correct points on the display.
**/
void
gimp_display_shell_zoom_segments (GimpDisplayShell *shell,
const GimpBoundSeg *src_segs,
GimpSegment *dest_segs,
gint n_segs,
gdouble offset_x,
gdouble offset_y)
{
gint i;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
for (i = 0; i < n_segs ; i++)
{
gdouble x1, x2;
gdouble y1, y2;
x1 = src_segs[i].x1 + offset_x;
x2 = src_segs[i].x2 + offset_x;
y1 = src_segs[i].y1 + offset_y;
y2 = src_segs[i].y2 + offset_y;
dest_segs[i].x1 = SCALEX (shell, x1) - shell->offset_x;
dest_segs[i].x2 = SCALEX (shell, x2) - shell->offset_x;
dest_segs[i].y1 = SCALEY (shell, y1) - shell->offset_y;
dest_segs[i].y2 = SCALEY (shell, y2) - shell->offset_y;
}
}
/**
* gimp_display_shell_rotate_coords:
* @shell: a #GimpDisplayShell
* @image_coords: unrotated display coordinates
* @display_coords: returns the corresponding rotated display coordinates
*
* Rotates from unrotated display coordinates to rotated display
* coordinates, so that objects can be rendered at the correct points
* on the display.
**/
void
gimp_display_shell_rotate_coords (GimpDisplayShell *shell,
const GimpCoords *unrotated_coords,
GimpCoords *rotated_coords)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (unrotated_coords != NULL);
g_return_if_fail (rotated_coords != NULL);
*rotated_coords = *unrotated_coords;
if (shell->rotate_transform)
cairo_matrix_transform_point (shell->rotate_transform,
&rotated_coords->x,
&rotated_coords->y);
}
/**
* gimp_display_shell_unrotate_coords:
* @shell: a #GimpDisplayShell
* @display_coords: rotated display coordinates
* @image_coords: returns the corresponding unrotated display coordinates
*
* Rotates from rotated display coordinates to unrotated display coordinates.
**/
void
gimp_display_shell_unrotate_coords (GimpDisplayShell *shell,
const GimpCoords *rotated_coords,
GimpCoords *unrotated_coords)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (rotated_coords != NULL);
g_return_if_fail (unrotated_coords != NULL);
*unrotated_coords = *rotated_coords;
if (shell->rotate_untransform)
cairo_matrix_transform_point (shell->rotate_untransform,
&unrotated_coords->x,
&unrotated_coords->y);
}
/**
* gimp_display_shell_rotate_xy:
* @shell:
* @x:
* @y:
* @nx:
* @ny:
*
* Rotates an unrotated display coordinate to a rotated shell coordinate.
**/
void
gimp_display_shell_rotate_xy (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gint *nx,
gint *ny)
{
gint64 tx;
gint64 ty;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
if (shell->rotate_transform)
cairo_matrix_transform_point (shell->rotate_transform, &x, &y);
tx = x;
ty = y;
/* The projected coordinates might overflow a gint in the case of
* big images at high zoom levels, so we clamp them here to avoid
* problems.
*/
*nx = CLAMP (tx, G_MININT, G_MAXINT);
*ny = CLAMP (ty, G_MININT, G_MAXINT);
}
/**
* gimp_display_shell_unrotate_xy:
* @shell: a #GimpDisplayShell
* @x: x coordinate in rotated display coordinates
* @y: y coordinate in rotated display coordinates
* @nx: returns x oordinate in unrotated display coordinates
* @ny: returns y coordinate in unrotated display coordinates
*
* Rotate from rotated display coordinates to unrotated display
* coordinates.
**/
void
gimp_display_shell_unrotate_xy (GimpDisplayShell *shell,
gint x,
gint y,
gint *nx,
gint *ny)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
if (shell->rotate_untransform)
{
gdouble fx = x;
gdouble fy = y;
cairo_matrix_transform_point (shell->rotate_untransform, &fx, &fy);
*nx = CLAMP (fx, G_MININT, G_MAXINT);
*ny = CLAMP (fy, G_MININT, G_MAXINT);
}
else
{
*nx = x;
*ny = y;
}
}
/**
* gimp_display_shell_rotate_xy_f:
* @shell: a #GimpDisplayShell
* @x: image x coordinate of point
* @y: image y coordinate of point
* @nx: returned shell canvas x coordinate
* @ny: returned shell canvas y coordinate
*
* Rotates from untransformed display coordinates to rotated display
* coordinates.
**/
void
gimp_display_shell_rotate_xy_f (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gdouble *nx,
gdouble *ny)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
*nx = x;
*ny = y;
if (shell->rotate_transform)
cairo_matrix_transform_point (shell->rotate_transform, nx, ny);
}
/**
* gimp_display_shell_unrotate_xy_f:
* @shell: a #GimpDisplayShell
* @x: x coordinate in rotated display coordinates
* @y: y coordinate in rotated display coordinates
* @nx: place to return x coordinate in unrotated display coordinates
* @ny: place to return y coordinate in unrotated display coordinates
*
* This function is identical to gimp_display_shell_unrotate_xy(),
* except that the input and output coordinates are doubles rather
* than ints.
**/
void
gimp_display_shell_unrotate_xy_f (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gdouble *nx,
gdouble *ny)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
*nx = x;
*ny = y;
if (shell->rotate_untransform)
cairo_matrix_transform_point (shell->rotate_untransform, nx, ny);
}
void
gimp_display_shell_rotate_bounds (GimpDisplayShell *shell,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2,
gdouble *nx1,
gdouble *ny1,
gdouble *nx2,
gdouble *ny2)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
if (shell->rotate_transform)
{
gdouble tx1 = x1;
gdouble ty1 = y1;
gdouble tx2 = x1;
gdouble ty2 = y2;
gdouble tx3 = x2;
gdouble ty3 = y1;
gdouble tx4 = x2;
gdouble ty4 = y2;
cairo_matrix_transform_point (shell->rotate_transform, &tx1, &ty1);
cairo_matrix_transform_point (shell->rotate_transform, &tx2, &ty2);
cairo_matrix_transform_point (shell->rotate_transform, &tx3, &ty3);
cairo_matrix_transform_point (shell->rotate_transform, &tx4, &ty4);
*nx1 = MIN4 (tx1, tx2, tx3, tx4);
*ny1 = MIN4 (ty1, ty2, ty3, ty4);
*nx2 = MAX4 (tx1, tx2, tx3, tx4);
*ny2 = MAX4 (ty1, ty2, ty3, ty4);
}
else
{
*nx1 = x1;
*ny1 = y1;
*nx2 = x2;
*ny2 = y2;
}
}
void
gimp_display_shell_unrotate_bounds (GimpDisplayShell *shell,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2,
gdouble *nx1,
gdouble *ny1,
gdouble *nx2,
gdouble *ny2)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
if (shell->rotate_untransform)
{
gdouble tx1 = x1;
gdouble ty1 = y1;
gdouble tx2 = x1;
gdouble ty2 = y2;
gdouble tx3 = x2;
gdouble ty3 = y1;
gdouble tx4 = x2;
gdouble ty4 = y2;
cairo_matrix_transform_point (shell->rotate_untransform, &tx1, &ty1);
cairo_matrix_transform_point (shell->rotate_untransform, &tx2, &ty2);
cairo_matrix_transform_point (shell->rotate_untransform, &tx3, &ty3);
cairo_matrix_transform_point (shell->rotate_untransform, &tx4, &ty4);
*nx1 = MIN4 (tx1, tx2, tx3, tx4);
*ny1 = MIN4 (ty1, ty2, ty3, ty4);
*nx2 = MAX4 (tx1, tx2, tx3, tx4);
*ny2 = MAX4 (ty1, ty2, ty3, ty4);
}
else
{
*nx1 = x1;
*ny1 = y1;
*nx2 = x2;
*ny2 = y2;
}
}
/**
* gimp_display_shell_transform_coords:
* @shell: a #GimpDisplayShell
* @image_coords: image coordinates
* @display_coords: returns the corresponding display coordinates
*
* Transforms from image coordinates to display coordinates, so that
* objects can be rendered at the correct points on the display.
**/
void
gimp_display_shell_transform_coords (GimpDisplayShell *shell,
const GimpCoords *image_coords,
GimpCoords *display_coords)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (image_coords != NULL);
g_return_if_fail (display_coords != NULL);
*display_coords = *image_coords;
display_coords->x = SCALEX (shell, image_coords->x);
display_coords->y = SCALEY (shell, image_coords->y);
display_coords->x -= shell->offset_x;
display_coords->y -= shell->offset_y;
if (shell->rotate_transform)
cairo_matrix_transform_point (shell->rotate_transform,
&display_coords->x,
&display_coords->y);
}
/**
* gimp_display_shell_untransform_coords:
* @shell: a #GimpDisplayShell
* @display_coords: display coordinates
* @image_coords: returns the corresponding image coordinates
*
* Transforms from display coordinates to image coordinates, so that
* points on the display can be mapped to points in the image.
**/
void
gimp_display_shell_untransform_coords (GimpDisplayShell *shell,
const GimpCoords *display_coords,
GimpCoords *image_coords)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (display_coords != NULL);
g_return_if_fail (image_coords != NULL);
*image_coords = *display_coords;
if (shell->rotate_untransform)
cairo_matrix_transform_point (shell->rotate_untransform,
&image_coords->x,
&image_coords->y);
image_coords->x += shell->offset_x;
image_coords->y += shell->offset_y;
image_coords->x /= shell->scale_x;
image_coords->y /= shell->scale_y;
image_coords->xscale = shell->scale_x;
image_coords->yscale = shell->scale_y;
image_coords->angle = shell->rotate_angle / 360.0;
image_coords->reflect = shell->flip_horizontally ^ shell->flip_vertically;
if (shell->flip_vertically)
image_coords->angle += 0.5;
}
/**
* gimp_display_shell_transform_xy:
* @shell:
* @x:
* @y:
* @nx:
* @ny:
*
* Transforms an image coordinate to a shell coordinate.
**/
void
gimp_display_shell_transform_xy (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gint *nx,
gint *ny)
{
gint64 tx;
gint64 ty;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
tx = x * shell->scale_x;
ty = y * shell->scale_y;
tx -= shell->offset_x;
ty -= shell->offset_y;
if (shell->rotate_transform)
{
gdouble fx = tx;
gdouble fy = ty;
cairo_matrix_transform_point (shell->rotate_transform, &fx, &fy);
tx = fx;
ty = fy;
}
/* The projected coordinates might overflow a gint in the case of
* big images at high zoom levels, so we clamp them here to avoid
* problems.
*/
*nx = CLAMP (tx, G_MININT, G_MAXINT);
*ny = CLAMP (ty, G_MININT, G_MAXINT);
}
/**
* gimp_display_shell_untransform_xy:
* @shell: a #GimpDisplayShell
* @x: x coordinate in display coordinates
* @y: y coordinate in display coordinates
* @nx: returns x oordinate in image coordinates
* @ny: returns y coordinate in image coordinates
* @round: if %TRUE, round the results to the nearest integer;
* if %FALSE, simply cast them to @gint.
*
* Transform from display coordinates to image coordinates, so that
* points on the display can be mapped to the corresponding points
* in the image.
**/
void
gimp_display_shell_untransform_xy (GimpDisplayShell *shell,
gint x,
gint y,
gint *nx,
gint *ny,
gboolean round)
{
gint64 tx;
gint64 ty;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
if (shell->rotate_untransform)
{
gdouble fx = x;
gdouble fy = y;
cairo_matrix_transform_point (shell->rotate_untransform, &fx, &fy);
x = fx;
y = fy;
}
if (round)
{
tx = SIGNED_ROUND (((gdouble) x + shell->offset_x) / shell->scale_x);
ty = SIGNED_ROUND (((gdouble) y + shell->offset_y) / shell->scale_y);
}
else
{
tx = ((gint64) x + shell->offset_x) / shell->scale_x;
ty = ((gint64) y + shell->offset_y) / shell->scale_y;
}
*nx = CLAMP (tx, G_MININT, G_MAXINT);
*ny = CLAMP (ty, G_MININT, G_MAXINT);
}
/**
* gimp_display_shell_transform_xy_f:
* @shell: a #GimpDisplayShell
* @x: image x coordinate of point
* @y: image y coordinate of point
* @nx: returned shell canvas x coordinate
* @ny: returned shell canvas y coordinate
*
* Transforms from image coordinates to display shell canvas
* coordinates.
**/
void
gimp_display_shell_transform_xy_f (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gdouble *nx,
gdouble *ny)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
*nx = SCALEX (shell, x) - shell->offset_x;
*ny = SCALEY (shell, y) - shell->offset_y;
if (shell->rotate_transform)
cairo_matrix_transform_point (shell->rotate_transform, nx, ny);
}
/**
* gimp_display_shell_untransform_xy_f:
* @shell: a #GimpDisplayShell
* @x: x coordinate in display coordinates
* @y: y coordinate in display coordinates
* @nx: place to return x coordinate in image coordinates
* @ny: place to return y coordinate in image coordinates
*
* This function is identical to gimp_display_shell_untransform_xy(),
* except that the input and output coordinates are doubles rather than
* ints, and consequently there is no option related to rounding.
**/
void
gimp_display_shell_untransform_xy_f (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gdouble *nx,
gdouble *ny)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
if (shell->rotate_untransform)
cairo_matrix_transform_point (shell->rotate_untransform, &x, &y);
*nx = (x + shell->offset_x) / shell->scale_x;
*ny = (y + shell->offset_y) / shell->scale_y;
}
void
gimp_display_shell_transform_bounds (GimpDisplayShell *shell,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2,
gdouble *nx1,
gdouble *ny1,
gdouble *nx2,
gdouble *ny2)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx1 != NULL);
g_return_if_fail (ny1 != NULL);
g_return_if_fail (nx2 != NULL);
g_return_if_fail (ny2 != NULL);
if (shell->rotate_transform)
{
gdouble tx1, ty1;
gdouble tx2, ty2;
gdouble tx3, ty3;
gdouble tx4, ty4;
gimp_display_shell_transform_xy_f_noround (shell, x1, y1, &tx1, &ty1);
gimp_display_shell_transform_xy_f_noround (shell, x1, y2, &tx2, &ty2);
gimp_display_shell_transform_xy_f_noround (shell, x2, y1, &tx3, &ty3);
gimp_display_shell_transform_xy_f_noround (shell, x2, y2, &tx4, &ty4);
*nx1 = MIN4 (tx1, tx2, tx3, tx4);
*ny1 = MIN4 (ty1, ty2, ty3, ty4);
*nx2 = MAX4 (tx1, tx2, tx3, tx4);
*ny2 = MAX4 (ty1, ty2, ty3, ty4);
}
else
{
gimp_display_shell_transform_xy_f_noround (shell, x1, y1, nx1, ny1);
gimp_display_shell_transform_xy_f_noround (shell, x2, y2, nx2, ny2);
}
}
void
gimp_display_shell_untransform_bounds (GimpDisplayShell *shell,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2,
gdouble *nx1,
gdouble *ny1,
gdouble *nx2,
gdouble *ny2)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx1 != NULL);
g_return_if_fail (ny1 != NULL);
g_return_if_fail (nx2 != NULL);
g_return_if_fail (ny2 != NULL);
if (shell->rotate_untransform)
{
gdouble tx1, ty1;
gdouble tx2, ty2;
gdouble tx3, ty3;
gdouble tx4, ty4;
gimp_display_shell_untransform_xy_f (shell, x1, y1, &tx1, &ty1);
gimp_display_shell_untransform_xy_f (shell, x1, y2, &tx2, &ty2);
gimp_display_shell_untransform_xy_f (shell, x2, y1, &tx3, &ty3);
gimp_display_shell_untransform_xy_f (shell, x2, y2, &tx4, &ty4);
*nx1 = MIN4 (tx1, tx2, tx3, tx4);
*ny1 = MIN4 (ty1, ty2, ty3, ty4);
*nx2 = MAX4 (tx1, tx2, tx3, tx4);
*ny2 = MAX4 (ty1, ty2, ty3, ty4);
}
else
{
gimp_display_shell_untransform_xy_f (shell, x1, y1, nx1, ny1);
gimp_display_shell_untransform_xy_f (shell, x2, y2, nx2, ny2);
}
}
/* transforms a bounding box from image-space, uniformly scaled by a factor of
* 'scale', to display-space. this is equivalent to, but more accurate than,
* dividing the input by 'scale', and using
* gimp_display_shell_transform_bounds(), in particular, in that if 'scale'
* equals 'shell->scale_x' or 'shell->scale_y', there is no loss in accuracy
* in the corresponding dimension due to scaling (although there might be loss
* of accuracy due to rotation or translation.)
*/
void
gimp_display_shell_transform_bounds_with_scale (GimpDisplayShell *shell,
gdouble scale,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2,
gdouble *nx1,
gdouble *ny1,
gdouble *nx2,
gdouble *ny2)
{
gdouble factor_x;
gdouble factor_y;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (scale > 0.0);
g_return_if_fail (nx1 != NULL);
g_return_if_fail (ny1 != NULL);
g_return_if_fail (nx2 != NULL);
g_return_if_fail (ny2 != NULL);
factor_x = shell->scale_x / scale;
factor_y = shell->scale_y / scale;
x1 = x1 * factor_x - shell->offset_x;
y1 = y1 * factor_y - shell->offset_y;
x2 = x2 * factor_x - shell->offset_x;
y2 = y2 * factor_y - shell->offset_y;
gimp_display_shell_rotate_bounds (shell,
x1, y1, x2, y2,
nx1, ny1, nx2, ny2);
}
/* transforms a bounding box from display-space to image-space, uniformly
* scaled by a factor of 'scale'. this is equivalent to, but more accurate
* than, using gimp_display_shell_untransform_bounds(), and multiplying the
* output by 'scale', in particular, in that if 'scale' equals 'shell->scale_x'
* or 'shell->scale_y', there is no loss in accuracy in the corresponding
* dimension due to scaling (although there might be loss of accuracy due to
* rotation or translation.)
*/
void
gimp_display_shell_untransform_bounds_with_scale (GimpDisplayShell *shell,
gdouble scale,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2,
gdouble *nx1,
gdouble *ny1,
gdouble *nx2,
gdouble *ny2)
{
gdouble factor_x;
gdouble factor_y;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (scale > 0.0);
g_return_if_fail (nx1 != NULL);
g_return_if_fail (ny1 != NULL);
g_return_if_fail (nx2 != NULL);
g_return_if_fail (ny2 != NULL);
factor_x = scale / shell->scale_x;
factor_y = scale / shell->scale_y;
gimp_display_shell_unrotate_bounds (shell,
x1, y1, x2, y2,
nx1, ny1, nx2, ny2);
*nx1 = (*nx1 + shell->offset_x) * factor_x;
*ny1 = (*ny1 + shell->offset_y) * factor_y;
*nx2 = (*nx2 + shell->offset_x) * factor_x;
*ny2 = (*ny2 + shell->offset_y) * factor_y;
}
/**
* gimp_display_shell_untransform_viewport:
* @shell: a #GimpDisplayShell
* @clip: whether to clip the result to the image bounds
* @x: returns image x coordinate of display upper left corner
* @y: returns image y coordinate of display upper left corner
* @width: returns width of display measured in image coordinates
* @height: returns height of display measured in image coordinates
*
* This function calculates the part of the image, in image coordinates,
* that corresponds to the display viewport.
**/
void
gimp_display_shell_untransform_viewport (GimpDisplayShell *shell,
gboolean clip,
gint *x,
gint *y,
gint *width,
gint *height)
{
gdouble x1, y1, x2, y2;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
gimp_display_shell_untransform_bounds (shell,
0, 0,
shell->disp_width, shell->disp_height,
&x1, &y1,
&x2, &y2);
x1 = floor (x1);
y1 = floor (y1);
x2 = ceil (x2);
y2 = ceil (y2);
if (clip)
{
GimpImage *image = gimp_display_get_image (shell->display);
x1 = MAX (x1, 0);
y1 = MAX (y1, 0);
x2 = MIN (x2, gimp_image_get_width (image));
y2 = MIN (y2, gimp_image_get_height (image));
}
if (x) *x = x1;
if (y) *y = y1;
if (width) *width = x2 - x1;
if (height) *height = y2 - y1;
}
/* private functions */
/* Same as gimp_display_shell_transform_xy_f(), but doesn't do any rounding
* for the transformed coordinates.
*/
static void
gimp_display_shell_transform_xy_f_noround (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gdouble *nx,
gdouble *ny)
{
*nx = shell->scale_x * x - shell->offset_x;
*ny = shell->scale_y * y - shell->offset_y;
if (shell->rotate_transform)
cairo_matrix_transform_point (shell->rotate_transform, nx, ny);
}
|