aboutsummaryrefslogtreecommitdiffstats
path: root/libgnomecanvas/gnome-canvas-pixbuf.c
blob: 6a3d433c070c0ed9569523e48660eecb3dd9fcd0 (plain) (blame)
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
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
/* GNOME libraries - GdkPixbuf item for the GNOME canvas
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Author: Federico Mena-Quintero <federico@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <math.h>
#include <libgnomecanvas/gnome-canvas.h>
#include <libgnomecanvas/gnome-canvas-util.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libart_lgpl/art_rgb_affine.h>
#include <libart_lgpl/art_rgb_rgba_affine.h>
#include "gnome-canvas-pixbuf.h"

/* Private part of the GnomeCanvasPixbuf structure */
typedef struct {
    /* Our gdk-pixbuf */
    GdkPixbuf *pixbuf;

    /* Width value */
    gdouble width;

    /* Height value */
    gdouble height;

    /* X translation */
    gdouble x;

    /* Y translation */
    gdouble y;

    /* Whether dimensions are set and whether they are in pixels or units */
    guint width_set : 1;
    guint width_in_pixels : 1;
    guint height_set : 1;
    guint height_in_pixels : 1;
    guint x_in_pixels : 1;
    guint y_in_pixels : 1;

    /* Whether the pixbuf has changed */
    guint need_pixbuf_update : 1;

    /* Whether the transformation or size have changed */
    guint need_xform_update : 1;

    /* Anchor */
    GtkAnchorType anchor;
} PixbufPrivate;

/* Object argument IDs */
enum {
    PROP_0,
    PROP_PIXBUF,
    PROP_WIDTH,
    PROP_WIDTH_SET,
    PROP_WIDTH_IN_PIXELS,
    PROP_HEIGHT,
    PROP_HEIGHT_SET,
    PROP_HEIGHT_IN_PIXELS,
    PROP_X,
    PROP_X_IN_PIXELS,
    PROP_Y,
    PROP_Y_IN_PIXELS,
    PROP_ANCHOR
};

static void gnome_canvas_pixbuf_class_init (GnomeCanvasPixbufClass *class);
static void gnome_canvas_pixbuf_init (GnomeCanvasPixbuf *cpb);
static void gnome_canvas_pixbuf_destroy (GtkObject *object);
static void gnome_canvas_pixbuf_set_property (GObject *object,
                          guint param_id,
                          const GValue *value,
                          GParamSpec *pspec);
static void gnome_canvas_pixbuf_get_property (GObject *object,
                          guint param_id,
                          GValue *value,
                          GParamSpec *pspec);

static void gnome_canvas_pixbuf_update (GnomeCanvasItem *item, gdouble *affine,
                    ArtSVP *clip_path, gint flags);
static void gnome_canvas_pixbuf_draw (GnomeCanvasItem *item, GdkDrawable *drawable,
                      gint x, gint y, gint width, gint height);
static void gnome_canvas_pixbuf_render (GnomeCanvasItem *item, GnomeCanvasBuf *buf);
static gdouble gnome_canvas_pixbuf_point (GnomeCanvasItem *item,
                     gdouble x,
                     gdouble y,
                     gint cx,
                     gint cy,
                     GnomeCanvasItem **actual_item);
static void gnome_canvas_pixbuf_bounds (GnomeCanvasItem *item,
                    gdouble *x1, gdouble *y1, gdouble *x2, gdouble *y2);

static GnomeCanvasItemClass *parent_class;



/**
 * gnome_canvas_pixbuf_get_type:
 * @void:
 *
 * Registers the #GnomeCanvasPixbuf class if necessary, and returns the type ID
 * associated to it.
 *
 * Return value: The type ID of the #GnomeCanvasPixbuf class.
 **/
GType
gnome_canvas_pixbuf_get_type (void)
{
    static GType pixbuf_type;

    if (!pixbuf_type) {
        const GTypeInfo object_info = {
            sizeof (GnomeCanvasPixbufClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) gnome_canvas_pixbuf_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,           /* class_data */
            sizeof (GnomeCanvasPixbuf),
            0,          /* n_preallocs */
            (GInstanceInitFunc) gnome_canvas_pixbuf_init,
            NULL            /* value_table */
        };

        pixbuf_type = g_type_register_static (GNOME_TYPE_CANVAS_ITEM, "GnomeCanvasPixbuf",
                             &object_info, 0);
    }

    return pixbuf_type;
}

/* Class initialization function for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_class_init (GnomeCanvasPixbufClass *class)
{
        GObjectClass *gobject_class;
    GtkObjectClass *object_class;
    GnomeCanvasItemClass *item_class;

        gobject_class = (GObjectClass *) class;
    object_class = (GtkObjectClass *) class;
    item_class = (GnomeCanvasItemClass *) class;

    parent_class = g_type_class_peek_parent (class);

    gobject_class->set_property = gnome_canvas_pixbuf_set_property;
    gobject_class->get_property = gnome_canvas_pixbuf_get_property;

        g_object_class_install_property
                (gobject_class,
                 PROP_PIXBUF,
                 g_param_spec_object ("pixbuf", NULL, NULL,
                                      GDK_TYPE_PIXBUF,
                                      (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_WIDTH,
                 g_param_spec_double ("width", NULL, NULL,
                      -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                      (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_WIDTH_SET,
                 g_param_spec_boolean ("width_set", NULL, NULL,
                       FALSE,
                       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_WIDTH_IN_PIXELS,
                 g_param_spec_boolean ("width_in_pixels", NULL, NULL,
                       FALSE,
                       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_HEIGHT,
                 g_param_spec_double ("height", NULL, NULL,
                      -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                      (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_HEIGHT_SET,
                 g_param_spec_boolean ("height_set", NULL, NULL,
                       FALSE,
                       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_HEIGHT_IN_PIXELS,
                 g_param_spec_boolean ("height_in_pixels", NULL, NULL,
                       FALSE,
                       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_X,
                 g_param_spec_double ("x", NULL, NULL,
                      -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                                    (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_X_IN_PIXELS,
                 g_param_spec_boolean ("x_in_pixels", NULL, NULL,
                       FALSE,
                       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_Y,
                 g_param_spec_double ("y", NULL, NULL,
                      -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                      (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_Y_IN_PIXELS,
                 g_param_spec_boolean ("y_in_pixels", NULL, NULL,
                       FALSE,
                       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_ANCHOR,
                 g_param_spec_enum ("anchor", NULL, NULL,
                                    GTK_TYPE_ANCHOR_TYPE,
                                    GTK_ANCHOR_NW,
                                    (G_PARAM_READABLE | G_PARAM_WRITABLE)));

    object_class->destroy = gnome_canvas_pixbuf_destroy;

    item_class->update = gnome_canvas_pixbuf_update;
    item_class->draw = gnome_canvas_pixbuf_draw;
    item_class->render = gnome_canvas_pixbuf_render;
    item_class->point = gnome_canvas_pixbuf_point;
    item_class->bounds = gnome_canvas_pixbuf_bounds;
}

/* Object initialization function for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_init (GnomeCanvasPixbuf *gcp)
{
    PixbufPrivate *priv;

    priv = g_new0 (PixbufPrivate, 1);
    gcp->priv = priv;

    priv->width = 0.0;
    priv->height = 0.0;
    priv->x = 0.0;
    priv->y = 0.0;
    priv->anchor = GTK_ANCHOR_NW;
}

/* Destroy handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_destroy (GtkObject *object)
{
    GnomeCanvasItem *item;
    GnomeCanvasPixbuf *gcp;
    PixbufPrivate *priv;

    g_return_if_fail (object != NULL);
    g_return_if_fail (GNOME_IS_CANVAS_PIXBUF (object));

    item = GNOME_CANVAS_ITEM (object);
    gcp = (GNOME_CANVAS_PIXBUF (object));
    priv = gcp->priv;

    /* remember, destroy can be run multiple times! */

    if (priv) {
        gnome_canvas_request_redraw (
        item->canvas, item->x1, item->y1, item->x2, item->y2);

        if (priv->pixbuf)
        g_object_unref (priv->pixbuf);

        g_free (priv);
        gcp->priv = NULL;
    }

    if (GTK_OBJECT_CLASS (parent_class)->destroy)
        (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}



/* Set_property handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_set_property (GObject            *object,
                  guint               param_id,
                  const GValue       *value,
                  GParamSpec         *pspec)
{
    GnomeCanvasItem *item;
    GnomeCanvasPixbuf *gcp;
    PixbufPrivate *priv;
    GdkPixbuf *pixbuf;
    gdouble val;

    g_return_if_fail (object != NULL);
    g_return_if_fail (GNOME_IS_CANVAS_PIXBUF (object));

    item = GNOME_CANVAS_ITEM (object);
    gcp = GNOME_CANVAS_PIXBUF (object);
    priv = gcp->priv;

    switch (param_id) {
    case PROP_PIXBUF:
        if (g_value_get_object (value))
            pixbuf = GDK_PIXBUF (g_value_get_object (value));
        else
            pixbuf = NULL;
        if (pixbuf != priv->pixbuf) {
            if (pixbuf) {
                g_return_if_fail
                    (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
                g_return_if_fail
                    (gdk_pixbuf_get_n_channels (pixbuf) == 3
                     || gdk_pixbuf_get_n_channels (pixbuf) == 4);
                g_return_if_fail
                    (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);

                g_object_ref (pixbuf);
            }

            if (priv->pixbuf)
                g_object_unref (priv->pixbuf);

            priv->pixbuf = pixbuf;
        }

        priv->need_pixbuf_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_WIDTH:
        val = g_value_get_double (value);
        g_return_if_fail (val >= 0.0);
        priv->width = val;
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_WIDTH_SET:
        priv->width_set = g_value_get_boolean (value);
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_WIDTH_IN_PIXELS:
        priv->width_in_pixels = g_value_get_boolean (value);
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_HEIGHT:
        val = g_value_get_double (value);
        g_return_if_fail (val >= 0.0);
        priv->height = val;
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_HEIGHT_SET:
        priv->height_set = g_value_get_boolean (value);
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_HEIGHT_IN_PIXELS:
        priv->height_in_pixels = g_value_get_boolean (value);
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_X:
        priv->x = g_value_get_double (value);
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_X_IN_PIXELS:
        priv->x_in_pixels = g_value_get_boolean (value);
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_Y:
        priv->y = g_value_get_double (value);
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_Y_IN_PIXELS:
        priv->y_in_pixels = g_value_get_boolean (value);
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    case PROP_ANCHOR:
        priv->anchor = g_value_get_enum (value);
        priv->need_xform_update = TRUE;
        gnome_canvas_item_request_update (item);
        break;

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}

/* Get_property handler for the pixbuf canvasi item */
static void
gnome_canvas_pixbuf_get_property (GObject            *object,
                  guint               param_id,
                  GValue             *value,
                  GParamSpec         *pspec)
{
    GnomeCanvasPixbuf *gcp;
    PixbufPrivate *priv;

    g_return_if_fail (object != NULL);
    g_return_if_fail (GNOME_IS_CANVAS_PIXBUF (object));

    gcp = GNOME_CANVAS_PIXBUF (object);
    priv = gcp->priv;

    switch (param_id) {
    case PROP_PIXBUF:
        g_value_set_object (value, G_OBJECT (priv->pixbuf));
        break;

    case PROP_WIDTH:
        g_value_set_double (value, priv->width);
        break;

    case PROP_WIDTH_SET:
        g_value_set_boolean (value, priv->width_set);
        break;

    case PROP_WIDTH_IN_PIXELS:
        g_value_set_boolean (value, priv->width_in_pixels);
        break;

    case PROP_HEIGHT:
        g_value_set_double (value, priv->height);
        break;

    case PROP_HEIGHT_SET:
        g_value_set_boolean (value, priv->height_set);
        break;

    case PROP_HEIGHT_IN_PIXELS:
        g_value_set_boolean (value, priv->height_in_pixels);
        break;

    case PROP_X:
        g_value_set_double (value, priv->x);
        break;

    case PROP_X_IN_PIXELS:
        g_value_set_boolean (value, priv->x_in_pixels);
        break;

    case PROP_Y:
        g_value_set_double (value, priv->y);
        break;

    case PROP_Y_IN_PIXELS:
        g_value_set_boolean (value, priv->y_in_pixels);
        break;

    case PROP_ANCHOR:
        g_value_set_enum (value, priv->anchor);
        break;

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}



/* Bounds and utilities */

/* Computes the amount by which the unit horizontal and vertical vectors will be
 * scaled by an affine transformation.
 */
static void
compute_xform_scaling (gdouble *affine, ArtPoint *i_c, ArtPoint *j_c)
{
    ArtPoint orig, orig_c;
    ArtPoint i, j;

    /* Origin */

    orig.x = 0.0;
    orig.y = 0.0;
    art_affine_point (&orig_c, &orig, affine);

    /* Horizontal and vertical vectors */

    i.x = 1.0;
    i.y = 0.0;
    art_affine_point (i_c, &i, affine);
    i_c->x -= orig_c.x;
    i_c->y -= orig_c.y;

    j.x = 0.0;
    j.y = 1.0;
    art_affine_point (j_c, &j, affine);
    j_c->x -= orig_c.x;
    j_c->y -= orig_c.y;
}

/* computes the addtional resolution dependent affine needed to
 * fit the image within its viewport defined by x,y,width and height
 * args
 */
static void
compute_viewport_affine (GnomeCanvasPixbuf *gcp,
                         gdouble *viewport_affine,
                         gdouble *i2c)
{
    PixbufPrivate *priv;
    ArtPoint i_c, j_c;
    gdouble i_len, j_len;
    gdouble si_len, sj_len;
    gdouble ti_len, tj_len;
    gdouble scale[6], translate[6];
    gdouble w, h;
    gdouble x, y;

    priv = gcp->priv;

    /* Compute scaling vectors and required width/height */

    compute_xform_scaling (i2c, &i_c, &j_c);

    i_len = sqrt (i_c.x * i_c.x + i_c.y * i_c.y);
    j_len = sqrt (j_c.x * j_c.x + j_c.y * j_c.y);

    if (priv->width_set)
        w = priv->width;
    else
        w = gdk_pixbuf_get_width (priv->pixbuf);

    if (priv->height_set)
        h = priv->height;
    else
        h = gdk_pixbuf_get_height (priv->pixbuf);

    x = priv->x;
    y = priv->y;

    /* Convert i_len and j_len into scaling factors */

    if (priv->width_in_pixels) {
        if (i_len > GNOME_CANVAS_EPSILON)
            si_len = 1.0 / i_len;
        else
            si_len = 0.0;
    } else
        si_len = 1.0;

    si_len *= w / gdk_pixbuf_get_width (priv->pixbuf);

    if (priv->height_in_pixels) {
        if (j_len > GNOME_CANVAS_EPSILON)
            sj_len = 1.0 / j_len;
        else
            sj_len = 0.0;
    } else
        sj_len = 1.0;

    sj_len *= h / gdk_pixbuf_get_height (priv->pixbuf);

    /* Calculate translation offsets */

    if (priv->x_in_pixels) {
        if (i_len > GNOME_CANVAS_EPSILON)
            ti_len = 1.0 / i_len;
        else
            ti_len = 0.0;
    } else
        ti_len = 1.0;

    switch (priv->anchor) {
    case GTK_ANCHOR_NW:
    case GTK_ANCHOR_W:
    case GTK_ANCHOR_SW:
        ti_len *= x;
        break;

    case GTK_ANCHOR_N:
    case GTK_ANCHOR_CENTER:
    case GTK_ANCHOR_S:
        ti_len *= x - w * si_len / 2;
        break;

    case GTK_ANCHOR_NE:
    case GTK_ANCHOR_E:
    case GTK_ANCHOR_SE:
        ti_len *= x - w * si_len;
        break;

        default:
                break;
    }

    if (priv->y_in_pixels) {
        if (j_len > GNOME_CANVAS_EPSILON)
            tj_len = 1.0 / j_len;
        else
            tj_len = 0.0;
    } else
        tj_len = 1.0;

    switch (priv->anchor) {
    case GTK_ANCHOR_NW:
    case GTK_ANCHOR_N:
    case GTK_ANCHOR_NE:
        tj_len *= y;
        break;

    case GTK_ANCHOR_W:
    case GTK_ANCHOR_CENTER:
    case GTK_ANCHOR_E:
        tj_len *= y - h * sj_len / 2;
        break;

    case GTK_ANCHOR_SW:
    case GTK_ANCHOR_S:
    case GTK_ANCHOR_SE:
        tj_len *= y - h * sj_len;
        break;

        default:
                break;
    }

    /* Compute the final affine */

    art_affine_scale (scale, si_len, sj_len);
    art_affine_translate (translate, ti_len, tj_len);
    art_affine_multiply (viewport_affine, scale, translate);
}

/* Computes the affine transformation with which the pixbuf needs to be
 * transformed to render it on the canvas.  This is not the same as the
 * item_to_canvas transformation because we may need to scale the pixbuf
 * by some other amount.
 */
static void
compute_render_affine (GnomeCanvasPixbuf *gcp, gdouble *ra, gdouble *i2c)
{
    gdouble va[6];

    compute_viewport_affine (gcp, va, i2c);
#ifdef GNOME_CANVAS_PIXBUF_VERBOSE
    g_print ("va %g %g %g %g %g %g\n", va[0], va[1], va[2], va[3], va[4], va[5]);
#endif
    art_affine_multiply (ra, va, i2c);
#ifdef GNOME_CANVAS_PIXBUF_VERBOSE
    g_print ("ra %g %g %g %g %g %g\n", ra[0], ra[1], ra[2], ra[3], ra[4], ra[5]);
#endif
}

/* Recomputes the bounding box of a pixbuf canvas item.  The horizontal and
 * vertical dimensions may be specified in units or pixels, separately, so we
 * have to compute the components individually for each dimension.
 */
static void
recompute_bounding_box (GnomeCanvasPixbuf *gcp, gdouble *i2c)
{
    GnomeCanvasItem *item;
    PixbufPrivate *priv;
    gdouble ra[6];
    ArtDRect rect;

    item = GNOME_CANVAS_ITEM (gcp);
    priv = gcp->priv;

    if (!priv->pixbuf) {
        item->x1 = item->y1 = item->x2 = item->y2 = 0.0;
        return;
    }

    rect.x0 = 0.0;
    rect.x1 = gdk_pixbuf_get_width (priv->pixbuf);

    rect.y0 = 0.0;
    rect.y1 = gdk_pixbuf_get_height (priv->pixbuf);

#ifdef GNOME_CANVAS_PIXBUF_VERBOSE
    g_print ("i2c %g %g %g %g %g %g\n", i2c[0], i2c[1], i2c[2], i2c[3], i2c[4], i2c[5]);
#endif
    gnome_canvas_item_i2c_affine (item, i2c);
#ifdef GNOME_CANVAS_PIXBUF_VERBOSE
    g_print ("i2c %g %g %g %g %g %g\n", i2c[0], i2c[1], i2c[2], i2c[3], i2c[4], i2c[5]);
#endif
    compute_render_affine (gcp, ra, i2c);
#ifdef GNOME_CANVAS_PIXBUF_VERBOSE
    g_print ("ra %g %g %g %g %g %g\n", ra[0], ra[1], ra[2], ra[3], ra[4], ra[5]);
#endif
    art_drect_affine_transform (&rect, &rect, ra);

    item->x1 = floor (rect.x0);
    item->y1 = floor (rect.y0);
    item->x2 = ceil (rect.x1);
    item->y2 = ceil (rect.y1);
}



/* Update sequence */

/* Update handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_update (GnomeCanvasItem *item,
                            gdouble *affine,
                            ArtSVP *clip_path,
                            gint flags)
{
    GnomeCanvasPixbuf *gcp;
    PixbufPrivate *priv;

    gcp = GNOME_CANVAS_PIXBUF (item);
    priv = gcp->priv;

    if (parent_class->update)
        (* parent_class->update) (item, affine, clip_path, flags);

        /* the optimzations below cause rarely triggered redrawing bugs and
     * don't seem to actually save much performance. so it's probably
     * better to turn them off, than to chase subtle optimization bugs
     * throughgout all of gnome-canvas-pixbuf.c - TIMJ
     */
#ifdef USE_BROKEN_OPTIMIZATIONS
    if (((flags & GNOME_CANVAS_UPDATE_VISIBILITY)
         && !(GTK_OBJECT_FLAGS (item) & GNOME_CANVAS_ITEM_VISIBLE))
        || (flags & GNOME_CANVAS_UPDATE_AFFINE)
        || priv->need_pixbuf_update
        || priv->need_xform_update) {
        gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);
    }

    /* If we need a pixbuf update, or if the item changed visibility to
     * shown, recompute the bounding box.
     */
    if (priv->need_pixbuf_update
        || priv->need_xform_update
        || ((flags & GNOME_CANVAS_UPDATE_VISIBILITY)
        && (GTK_OBJECT_FLAGS (gcp) & GNOME_CANVAS_ITEM_VISIBLE))
        || (flags & GNOME_CANVAS_UPDATE_AFFINE)) {
        recompute_bounding_box (gcp, affine);
#ifdef GNOME_CANVAS_PIXBUF_VERBOSE
        g_print ("BBox is %g %g %g %g\n", item->x1, item->y1, item->x2, item->y2);
#endif
        gnome_canvas_request_redraw (item->canvas, item->x1, item->y1, item->x2, item->y2);
        priv->need_pixbuf_update = FALSE;
        priv->need_xform_update = FALSE;
    }
#else   /* ordinary update logic */
        gnome_canvas_request_redraw (
        item->canvas, item->x1, item->y1, item->x2, item->y2);
        recompute_bounding_box (gcp, affine);
        gnome_canvas_request_redraw (
        item->canvas, item->x1, item->y1, item->x2, item->y2);
        priv->need_pixbuf_update = FALSE;
        priv->need_xform_update = FALSE;
#endif
}



/* Rendering */

/* This is private to libart, but we need it.  Sigh. */
extern void art_rgb_affine_run (gint *p_x0,
                                gint *p_x1,
                                gint y,
                                gint src_width,
                                gint src_height,
                                const gdouble affine[6]);

/* Fills the specified buffer with the transformed version of a pixbuf */
static void
transform_pixbuf (guchar *dest,
                  gint x,
                  gint y,
                  gint width,
                  gint height,
                  gint rowstride,
                  GdkPixbuf *pixbuf,
                  gdouble *affine)
{
    gint xx, yy;
    gdouble inv[6];
    guchar *src, *d;
    ArtPoint src_p, dest_p;
    gint run_x1, run_x2;
    gint src_x, src_y;
    gint i;

    art_affine_invert (inv, affine);

    for (yy = 0; yy < height; yy++) {
        dest_p.y = y + yy + 0.5;

        run_x1 = x;
        run_x2 = x + width;
        art_rgb_affine_run (&run_x1, &run_x2, yy + y,
                    gdk_pixbuf_get_width (pixbuf),
                    gdk_pixbuf_get_height (pixbuf),
                    inv);

        d = dest + yy * rowstride + (run_x1 - x) * 4;

        for (xx = run_x1; xx < run_x2; xx++) {
            dest_p.x = xx + 0.5;
            art_affine_point (&src_p, &dest_p, inv);
            src_x = floor (src_p.x);
            src_y = floor (src_p.y);

            src =
                gdk_pixbuf_get_pixels (pixbuf) + src_y *
                gdk_pixbuf_get_rowstride (pixbuf) + src_x *
                gdk_pixbuf_get_n_channels (pixbuf);

            for (i = 0; i < gdk_pixbuf_get_n_channels (pixbuf); i++)
                *d++ = *src++;

            if (!gdk_pixbuf_get_has_alpha(pixbuf))
                *d++ = 255; /* opaque */
        }
    }
}

/* Draw handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_draw (GnomeCanvasItem *item, GdkDrawable *drawable,
              gint x, gint y, gint width, gint height)
{
    GnomeCanvasPixbuf *gcp;
    PixbufPrivate *priv;
    gdouble i2c[6], render_affine[6];
    guchar *buf;
    GdkPixbuf *pixbuf;
    ArtIRect p_rect, a_rect, d_rect;
    gint w, h;

    gcp = GNOME_CANVAS_PIXBUF (item);
    priv = gcp->priv;

    if (!priv->pixbuf)
        return;

    gnome_canvas_item_i2c_affine (item, i2c);
    compute_render_affine (gcp, render_affine, i2c);

    /* Compute the area we need to repaint */

    p_rect.x0 = item->x1;
    p_rect.y0 = item->y1;
    p_rect.x1 = item->x2;
    p_rect.y1 = item->y2;

    a_rect.x0 = x;
    a_rect.y0 = y;
    a_rect.x1 = x + width;
    a_rect.y1 = y + height;

    art_irect_intersect (&d_rect, &p_rect, &a_rect);
    if (art_irect_empty (&d_rect))
        return;

    /* Create a temporary buffer and transform the pixbuf there */

    w = d_rect.x1 - d_rect.x0;
    h = d_rect.y1 - d_rect.y0;

    buf = g_new0 (guchar, w * h * 4);
    transform_pixbuf (buf,
              d_rect.x0, d_rect.y0,
              w, h,
              w * 4,
              priv->pixbuf, render_affine);

    pixbuf = gdk_pixbuf_new_from_data (buf, GDK_COLORSPACE_RGB,
                       TRUE,
                       8, w, h,
                       w * 4,
                       NULL, NULL);

    gdk_draw_pixbuf (drawable, NULL, pixbuf,
             0, 0,
             d_rect.x0 - x, d_rect.y0 - y,
             w, h,
             GDK_RGB_DITHER_MAX,
             d_rect.x0, d_rect.y0);

    g_object_unref (pixbuf);
    g_free (buf);
}

/* Render handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_render (GnomeCanvasItem *item, GnomeCanvasBuf *buf)
{
    GnomeCanvasPixbuf *gcp;
    PixbufPrivate *priv;
    gdouble i2c[6], render_affine[6];

    gcp = GNOME_CANVAS_PIXBUF (item);
    priv = gcp->priv;

    if (!priv->pixbuf)
        return;

    gnome_canvas_item_i2c_affine (item, i2c);
    compute_render_affine (gcp, render_affine, i2c);
        gnome_canvas_buf_ensure_buf (buf);

    if ((fabs (render_affine[1]) < GNOME_CANVAS_EPSILON) &&
        (fabs (render_affine[2]) < GNOME_CANVAS_EPSILON) &&
        render_affine[0] > 0.0 &&
        render_affine[3] > 0.0)
      {
        GdkPixbuf *dest_pixbuf;
        gint x0, y0, x1, y1;

        dest_pixbuf = gdk_pixbuf_new_from_data (buf->buf,
                            GDK_COLORSPACE_RGB,
                            FALSE,
                            8,
                            buf->rect.x1 - buf->rect.x0,
                            buf->rect.y1 - buf->rect.y0,
                            buf->buf_rowstride,
                            NULL, NULL);

        x0 = floor (render_affine[4] - buf->rect.x0 + 0.5);
        y0 = floor (render_affine[5] - buf->rect.y0 + 0.5);

        x1 = x0 + floor (gdk_pixbuf_get_width (priv->pixbuf) * render_affine[0] + 0.5);
        y1 = y0 + floor (gdk_pixbuf_get_height (priv->pixbuf) * render_affine[3] + 0.5);

        x0 = MAX (x0, 0);
        x0 = MIN (x0, buf->rect.x1 - buf->rect.x0);
        y0 = MAX (y0, 0);
        y0 = MIN (y0, buf->rect.y1 - buf->rect.y0);

        x1 = MAX (x1, 0);
        x1 = MIN (x1, buf->rect.x1 - buf->rect.x0);
        y1 = MAX (y1, 0);
        y1 = MIN (y1, buf->rect.y1 - buf->rect.y0);

        gdk_pixbuf_composite  (priv->pixbuf,
                   dest_pixbuf,
                   x0, y0,
                   x1 - x0, y1 - y0,
                   render_affine[4] - buf->rect.x0,
                   render_affine[5] - buf->rect.y0,
                   render_affine[0],
                   render_affine[3],
                   GDK_INTERP_BILINEAR,
                   255);

        g_object_unref (dest_pixbuf);
      }
    else if (gdk_pixbuf_get_has_alpha(priv->pixbuf))
        art_rgb_rgba_affine (buf->buf,
                     buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1,
                     buf->buf_rowstride,
                     gdk_pixbuf_get_pixels(priv->pixbuf),
                     gdk_pixbuf_get_width(priv->pixbuf),
                     gdk_pixbuf_get_height(priv->pixbuf),
                     gdk_pixbuf_get_rowstride(priv->pixbuf),
                     render_affine,
                     ART_FILTER_NEAREST,
                     NULL);
    else
        art_rgb_affine (buf->buf,
                buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1,
                buf->buf_rowstride,
                gdk_pixbuf_get_pixels(priv->pixbuf),
                gdk_pixbuf_get_width(priv->pixbuf),
                gdk_pixbuf_get_height(priv->pixbuf),
                gdk_pixbuf_get_rowstride(priv->pixbuf),
                render_affine,
                ART_FILTER_NEAREST,
                NULL);

    buf->is_bg = 0;
}



/* Point handler for the pixbuf canvas item */
static double
gnome_canvas_pixbuf_point (GnomeCanvasItem *item,
                           gdouble x,
                           gdouble y,
                           gint cx,
                           gint cy,
                           GnomeCanvasItem **actual_item)
{
    GnomeCanvasPixbuf *gcp;
    PixbufPrivate *priv;
    gdouble i2c[6], render_affine[6], inv[6];
    ArtPoint c, p;
    gint px, py;
    gdouble no_hit;
    guchar *src;
    GdkPixbuf *pixbuf;

    gcp = GNOME_CANVAS_PIXBUF (item);
    priv = gcp->priv;
    pixbuf = priv->pixbuf;

    *actual_item = item;

    no_hit = item->canvas->pixels_per_unit * 2 + 10;

    if (!priv->pixbuf)
        return no_hit;

    gnome_canvas_item_i2c_affine (item, i2c);
    compute_render_affine (gcp, render_affine, i2c);
    art_affine_invert (inv, render_affine);

    c.x = cx;
    c.y = cy;
    art_affine_point (&p, &c, inv);
    px = p.x;
    py = p.y;

    if (px < 0 || px >= gdk_pixbuf_get_width (pixbuf) ||
        py < 0 || py >= gdk_pixbuf_get_height (pixbuf))
        return no_hit;

    if (!gdk_pixbuf_get_has_alpha (pixbuf))
        return 0.0;

    src = gdk_pixbuf_get_pixels (pixbuf) +
        py * gdk_pixbuf_get_rowstride (pixbuf) +
        px * gdk_pixbuf_get_n_channels (pixbuf);

    if (src[3] < 128)
        return no_hit;
    else
        return 0.0;
}



/* Bounds handler for the pixbuf canvas item */
static void
gnome_canvas_pixbuf_bounds (GnomeCanvasItem *item,
                            gdouble *x1,
                            gdouble *y1,
                            gdouble *x2,
                            gdouble *y2)
{
    GnomeCanvasPixbuf *gcp;
    PixbufPrivate *priv;
    gdouble i2c[6], viewport_affine[6];
    ArtDRect rect;

    gcp = GNOME_CANVAS_PIXBUF (item);
    priv = gcp->priv;

    if (!priv->pixbuf) {
        *x1 = *y1 = *x2 = *y2 = 0.0;
        return;
    }

    rect.x0 = 0.0;
    rect.x1 = gdk_pixbuf_get_width (priv->pixbuf);

    rect.y0 = 0.0;
    rect.y1 = gdk_pixbuf_get_height (priv->pixbuf);

    gnome_canvas_item_i2c_affine (item, i2c);
    compute_viewport_affine (gcp, viewport_affine, i2c);
    art_drect_affine_transform (&rect, &rect, viewport_affine);

    *x1 = rect.x0;
    *y1 = rect.y0;
    *x2 = rect.x1;
    *y2 = rect.y1;
}