Strip Mode is now optional
[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), frmt(OTHER_FORMAT)
24 {
25         mutex.lock();
26         context = imlib_context_new();
27         mutex.unlock();
28 }
29
30 ImlibImage::ImlibImage(int width, int height): data(NULL), w(width), h(height), frmt(OTHER_FORMAT)
31 {
32         mutex.lock();
33         context = imlib_context_new();
34         imlib_context_push(context);
35         data = imlib_create_image(width, height);
36         imlib_context_pop();
37         mutex.unlock();
38 }
39
40 ImlibImage::~ImlibImage()
41 {
42         mutex.lock();
43         if (data)
44         {
45                 imlib_context_push(context);
46                 imlib_context_set_image(data);
47                 imlib_free_image();
48                 imlib_context_pop(); //??
49         }
50         imlib_context_free(context);
51         mutex.unlock();
52 }
53
54 int ImlibImage::load(const QString &path)
55 {
56         Imlib_Load_Error error;
57
58         mutex.lock();
59
60         imlib_context_push(context);
61         data = imlib_load_image_with_error_return(QFile::encodeName(path), &error);
62         if (error == IMLIB_LOAD_ERROR_NONE)
63         {
64                 imlib_context_set_image(data);
65                 w = imlib_image_get_width();
66                 h = imlib_image_get_height();
67         }
68         imlib_context_pop();
69
70         if (w == 0 || h == 0) {
71                 frmt = OTHER_FORMAT;
72         } else {
73                 double r = (double)w/h;
74
75                 if (r > 2)
76                         frmt = STRIP_FORMAT;
77                 else if (1.0/r > 1.14)
78                         frmt = PAGE_FORMAT;
79                 else
80                         frmt = SPREAD_FORMAT;
81         }
82
83
84         mutex.unlock();
85         return error == IMLIB_LOAD_ERROR_NONE;
86 }
87
88 void ImlibImage::addImage(ImlibImage *source, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh)
89 {
90         mutex.lock();
91         if (data && source->data)
92         {
93                 imlib_context_push(context);
94                 imlib_context_set_image(data);
95                 imlib_blend_image_onto_image(source->data, 1,
96                                 sx, sy, sw, sh,
97                                 dx, dy, dw, dh);
98                 imlib_context_pop();
99         }
100         mutex.unlock();
101 }
102
103 void ImlibImage::draw(QPaintDevice *p, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh)
104 {
105         mutex.lock();
106         if (data)
107         {
108                 imlib_context_push(context);
109
110                 imlib_context_set_image(data);
111
112                 imlib_context_set_display(p->x11Display());
113                 imlib_context_set_visual((Visual *)p->x11Visual());
114                 imlib_context_set_colormap(p->x11Colormap());
115
116                 imlib_context_set_drawable(p->handle());
117                 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
118                 
119                 imlib_context_pop();
120         }
121         mutex.unlock();
122 }
123
124 int ImlibImage::width() const
125 {
126         return w;
127 }
128
129 int ImlibImage::height() const
130 {
131         return h;
132 }
133
134 ImageFormat ImlibImage::getFormat() const
135 {
136         return frmt;
137 }
138
139 void ImlibImage::setFormat(ImageFormat format)
140 {
141         frmt = format;
142 }
143
144 void ImlibImage::rotate(int orient)
145 {
146         mutex.lock();
147         if (data)
148         {
149                 imlib_context_push(context);
150                 imlib_context_set_image(data);
151                 imlib_image_orientate(orient);
152                 w = imlib_image_get_width();
153                 h = imlib_image_get_height();
154                 imlib_context_pop();
155         }
156         mutex.unlock();
157 }
158
159 ImlibImage* ImlibImage::rotateClone(int orient)
160 {
161         return NULL;
162         /*
163            ImlibImage *img = new ImlibImage();
164            imlib_context_push(context);
165            imlib_context_set_image(data);
166            imlib_image_orientate(orient);
167            img->data = imlib_clone_image();
168
169         imlib_context_set_image(img->data);
170         imlib_image_orientate(0);
171         img->w = h; //imlib_image_get_width();
172         img->h = w; //imlib_image_get_height();
173         imlib_context_pop();
174         return img;
175         */
176 }
177
178 void ImlibImage::reset()
179 {
180         mutex.lock();
181         if (data)
182         {
183                 imlib_context_push(context);
184                 imlib_context_set_image(data);
185                 imlib_free_image();
186                 data = NULL;
187                 w = h = 0;
188                 frmt = OTHER_FORMAT;
189                 imlib_context_pop();
190         }
191         mutex.unlock();
192 }
193
194 void ImlibImage::setCacheSize(int bytes)
195 {
196         mutex.lock();
197         imlib_set_cache_size(bytes);
198         mutex.unlock();
199 }
200