6ee8912db833e55d74649da9d9bb8ea343e4e3c6
[tbo.git] / src / piximage.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <math.h>
4 #include <gtk/gtk.h>
5 #include <glib/gi18n.h>
6 #include <cairo.h>
7 #include "piximage.h"
8 #include "tbo-types.h"
9
10 #define DOODLE_DIR DATA_DIR "/doodle/"
11
12
13 PIXImage *
14 tbo_piximage_new ()
15 {
16     PIXImage *image;
17     image = malloc (sizeof(PIXImage));
18     image->data = malloc(sizeof(char)*255);
19     image->free = tbo_pix_image_free;
20     image->draw = tbo_pix_image_draw;
21     image->save = tbo_pix_image_save;
22     image->clone = tbo_pix_image_clone;
23     image->type = PIXOBJ;
24     image->flipv = FALSE;
25     image->fliph = FALSE;
26     return image;
27 }
28
29 PIXImage *
30 tbo_piximage_new_width_params (int x,
31                                int y,
32                                int width,
33                                int height,
34                                const char *path)
35 {
36     PIXImage *image;
37     image = tbo_piximage_new ();
38     image->x = x;
39     image->y = y;
40     image->width = width;
41     image->height = height;
42     snprintf (image->data, 255, "%s", path);
43     image->type = PIXOBJ;
44     image->flipv = FALSE;
45     image->fliph = FALSE;
46     return image;
47 }
48
49 void
50 tbo_pix_image_free (PIXImage *self)
51 {
52     free (self->data);
53     free (self);
54 }
55
56 void
57 tbo_pix_image_draw (PIXImage *self, Frame *frame, cairo_t *cr)
58 {
59     int w, h;
60     cairo_surface_t *image;
61
62     image = cairo_image_surface_create_from_png (self->data);
63     w = cairo_image_surface_get_width (image);
64     h = cairo_image_surface_get_height (image);
65
66     if (!self->width) self->width = w;
67     if (!self->height) self->height = h;
68
69     float factorw = (float)self->width / (float)w;
70     float factorh = (float)self->height / (float)h;
71
72     cairo_matrix_t mx = {1, 0, 0, 1, 0, 0};
73     tbo_object_get_flip_matrix (self, &mx);
74
75     cairo_rectangle(cr, frame->x+2, frame->y+2, frame->width-4, frame->height-4);
76     cairo_clip (cr);
77     cairo_translate (cr, frame->x+self->x, frame->y+self->y);
78     cairo_rotate (cr, self->angle);
79     cairo_transform (cr, &mx);
80     cairo_scale (cr, factorw, factorh);
81
82     cairo_set_source_surface (cr, image, 0, 0);
83     cairo_paint (cr);
84
85     cairo_scale (cr, 1/factorw, 1/factorh);
86     cairo_transform (cr, &mx);
87     cairo_rotate (cr, -self->angle);
88     cairo_translate (cr, -(frame->x+self->x), -(frame->y+self->y));
89     cairo_reset_clip (cr);
90
91
92     cairo_surface_destroy (image);
93 }
94
95 void
96 tbo_pix_image_save (PIXImage *self, FILE *file)
97 {
98     char buffer[1024];
99
100     snprintf (buffer, 1024, "   <piximage x=\"%d\" y=\"%d\" "
101                            "width=\"%d\" height=\"%d\" "
102                            "angle=\"%f\" flipv=\"%d\" fliph=\"%d\" "
103                            "path=\"%s\">\n ",
104                            self->x, self->y, self->width, self->height,
105                            self->angle, self->flipv, self->fliph, (char*)self->data);
106     fwrite (buffer, sizeof (char), strlen (buffer), file);
107
108     snprintf (buffer, 1024, "   </piximage>\n");
109     fwrite (buffer, sizeof (char), strlen (buffer), file);
110 }
111
112 PIXImage *
113 tbo_pix_image_clone (PIXImage *self)
114 {
115     PIXImage *newimage;
116
117     newimage = tbo_piximage_new_width_params (self->x,
118                                               self->y,
119                                               self->width,
120                                               self->height,
121                                               self->data);
122     newimage->angle = self->angle;
123     newimage->flipv = self->flipv;
124     newimage->fliph = self->fliph;
125
126     return newimage;
127 }