Import of original sources
[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         if (data)
67         {
68                 mutex.lock();
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                 mutex.unlock();
82         }
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         if (data)
98         {
99                 mutex.lock();
100
101                 imlib_context_push(context);
102                 imlib_context_set_image(data);
103                 imlib_image_orientate(orient);
104                 w = imlib_image_get_width();
105                 h = imlib_image_get_height();
106                 imlib_context_pop();
107
108                 mutex.unlock();
109         }
110 }
111
112 ImlibImage* ImlibImage::rotateClone(int orient)
113 {
114         return NULL;
115         /*
116            ImlibImage *img = new ImlibImage();
117            imlib_context_push(context);
118            imlib_context_set_image(data);
119            imlib_image_orientate(orient);
120            img->data = imlib_clone_image();
121
122         imlib_context_set_image(img->data);
123         imlib_image_orientate(0);
124         img->w = h; //imlib_image_get_width();
125         img->h = w; //imlib_image_get_height();
126         imlib_context_pop();
127         return img;
128         */
129 }
130
131 void ImlibImage::reset()
132 {
133         if (data)
134         {
135                 mutex.lock();
136
137                 imlib_context_push(context);
138                 imlib_context_set_image(data);
139                 imlib_free_image();
140                 data = NULL;
141                 w = h = 0;
142                 imlib_context_pop();
143
144                 mutex.unlock();
145         }
146 }
147
148 void ImlibImage::setCacheSize(int bytes)
149 {
150         mutex.lock();
151         imlib_set_cache_size(bytes);
152         mutex.unlock();
153 }
154