Lock mutex earlier when accessing ImlibImage data
[qcomicbook-oblomov] / src / imlibimage.cpp
1 /*
2  * This file is a part of QComicBook.
3  *
4  * Copyright (C) 2005-2006 Pawel Stolowski <yogin@linux.bydg.org>
5  *
6  * QComicBook is free software; you can redestribute it and/or modify it
7  * under terms of GNU General Public License by Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY. See GPL for more details.
11  */
12
13 #include "imlibimage.h"
14 #include <qpaintdevice.h>
15 #include <qfile.h>
16 #include <X11/Xlib.h>
17 #include <Imlib2.h>
18
19 using namespace QComicBook;
20
21 QMutex ImlibImage::mutex;
22
23 ImlibImage::ImlibImage(): data(NULL), w(0), h(0)
24 {
25         mutex.lock();
26         context = imlib_context_new();
27         mutex.unlock();
28 }
29
30 ImlibImage::~ImlibImage()
31 {
32         mutex.lock();
33         if (data)
34         {
35                 imlib_context_push(context);
36                 imlib_context_set_image(data);
37                 imlib_free_image();
38                 imlib_context_pop(); //??
39         }
40         imlib_context_free(context);
41         mutex.unlock();
42 }
43
44 int ImlibImage::load(const QString &path)
45 {
46         Imlib_Load_Error error;
47
48         mutex.lock();
49
50         imlib_context_push(context);
51         data = imlib_load_image_with_error_return(QFile::encodeName(path), &error);
52         if (error == IMLIB_LOAD_ERROR_NONE)
53         {
54                 imlib_context_set_image(data);
55                 w = imlib_image_get_width();
56                 h = imlib_image_get_height();
57         }
58         imlib_context_pop();
59
60         mutex.unlock();
61         return error == IMLIB_LOAD_ERROR_NONE;
62 }
63
64 void ImlibImage::draw(QPaintDevice *p, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh)
65 {
66         mutex.lock();
67         if (data)
68         {
69                 imlib_context_push(context);
70
71                 imlib_context_set_image(data);
72
73                 imlib_context_set_display(p->x11Display());
74                 imlib_context_set_visual((Visual *)p->x11Visual());
75                 imlib_context_set_colormap(p->x11Colormap());
76
77                 imlib_context_set_drawable(p->handle());
78                 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
79                 
80                 imlib_context_pop();
81         }
82         mutex.unlock();
83 }
84
85 int ImlibImage::width() const
86 {
87         return w;
88 }
89
90 int ImlibImage::height() const
91 {
92         return h;
93 }
94
95 void ImlibImage::rotate(int orient)
96 {
97         mutex.lock();
98         if (data)
99         {
100                 imlib_context_push(context);
101                 imlib_context_set_image(data);
102                 imlib_image_orientate(orient);
103                 w = imlib_image_get_width();
104                 h = imlib_image_get_height();
105                 imlib_context_pop();
106         }
107         mutex.unlock();
108 }
109
110 ImlibImage* ImlibImage::rotateClone(int orient)
111 {
112         return NULL;
113         /*
114            ImlibImage *img = new ImlibImage();
115            imlib_context_push(context);
116            imlib_context_set_image(data);
117            imlib_image_orientate(orient);
118            img->data = imlib_clone_image();
119
120         imlib_context_set_image(img->data);
121         imlib_image_orientate(0);
122         img->w = h; //imlib_image_get_width();
123         img->h = w; //imlib_image_get_height();
124         imlib_context_pop();
125         return img;
126         */
127 }
128
129 void ImlibImage::reset()
130 {
131         mutex.lock();
132         if (data)
133         {
134                 imlib_context_push(context);
135                 imlib_context_set_image(data);
136                 imlib_free_image();
137                 data = NULL;
138                 w = h = 0;
139                 imlib_context_pop();
140         }
141         mutex.unlock();
142 }
143
144 void ImlibImage::setCacheSize(int bytes)
145 {
146         mutex.lock();
147         imlib_set_cache_size(bytes);
148         mutex.unlock();
149 }
150