summaryrefslogtreecommitdiff
path: root/src/FbTk/ImageImlib2.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/ImageImlib2.cc')
-rw-r--r--src/FbTk/ImageImlib2.cc147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/FbTk/ImageImlib2.cc b/src/FbTk/ImageImlib2.cc
new file mode 100644
index 0000000..82079a3
--- /dev/null
+++ b/src/FbTk/ImageImlib2.cc
@@ -0,0 +1,147 @@
1// ImageImlib2.cc for FbTk - Fluxbox ToolKit
2// Copyright (c) 2004 Mathias Gumz <akira at fluxbox dot org>
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: $
23
24#include "ImageImlib2.hh"
25
26#include "App.hh"
27#include "PixmapWithMask.hh"
28
29#include <X11/xpm.h>
30
31#include <Imlib2.h>
32#include <map>
33
34namespace {
35
36typedef std::map<int, Imlib_Context> ScreenImlibContextContainer;
37typedef ScreenImlibContextContainer::iterator ScreenImlibContext;
38
39ScreenImlibContextContainer contexts;
40
41}; // anon namespace
42
43
44namespace FbTk {
45
46ImageImlib2::ImageImlib2() {
47
48 // lets have a 2mb cache inside imlib, holds
49 // uncompressed images
50 imlib_set_cache_size(2048 * 1024);
51
52 // TODO: this are the potential candidates,
53 // choose only sane ones. open for discussion
54 char* format_list[] = {
55 "PNG", // pngloader
56 "JPEG", "JPG", "JFI", "JFIF", // jpegloader
57// "TIFF", "TIF", // tiffloader
58 "PNM", "PPM", "PGM", "PBM", "PAM", // pnmloader
59// "TGA", // tgaloader
60// "IFF", "ILBM", "LBM", // lbmloader
61// "GIF", // gifloader
62// "ARGB", "AGR", // argbloader
63// "BMP", // bmploader
64// "BZ2", // bzloader
65// "GZ", // gzloader
66 NULL
67 };
68
69 char** format = NULL;
70 for(format = format_list; *format != NULL; format++) {
71 Image::registerType(*format, *this);
72 }
73}
74
75ImageImlib2::~ImageImlib2() {
76 ScreenImlibContext it = contexts.begin();
77 ScreenImlibContext it_end = contexts.end();
78 for (; it != it_end; it++) {
79 imlib_context_free(it->second);
80 }
81 contexts.clear();
82}
83
84PixmapWithMask *ImageImlib2::load(const std::string &filename, int screen_num) const {
85
86 Display *dpy = FbTk::App::instance()->display();
87
88 // init imlib2 if needed, the settings for each screen may differ
89 ScreenImlibContext screen_context = contexts.find(screen_num);
90 if (screen_context == contexts.end()) {
91
92 Imlib_Context new_context = imlib_context_new();
93 imlib_context_push(new_context);
94
95 imlib_context_set_display(dpy);
96 imlib_context_set_drawable(RootWindow(dpy, screen_num));
97 imlib_context_set_colormap(DefaultColormap(dpy, screen_num));
98 imlib_context_set_visual(DefaultVisual(dpy, screen_num));
99
100 imlib_context_pop();
101
102 contexts[screen_num] = new_context;
103 screen_context = contexts.find(screen_num);
104 }
105
106 if (screen_context == contexts.end()) {
107#ifdef DEBUG
108 cerr << "ImageImlib2::load: error, couldnt find a valid Imlib_Context.\n";
109#endif // DEBUG
110 return 0;
111 }
112
113 // now load the stuff
114 Imlib_Context context = screen_context->second;
115 imlib_context_push(context);
116 Imlib_Image image = imlib_load_image_immediately(filename.c_str());
117 if (image) { // loading was ok
118 imlib_context_set_image(image);
119
120 Pixmap pm = 0, mask = 0;
121 imlib_render_pixmaps_for_whole_image(&pm, &mask);
122
123 // pm and mask belong to imlib, so we have to copy them
124 FbPixmap fbpm;
125 FbPixmap fbmask;
126
127 fbpm.copy(pm);
128 fbmask.copy(mask);
129
130 // mark pm and mask as freeable in imlib
131 imlib_free_image();
132 imlib_free_pixmap_and_mask(pm);
133
134 imlib_context_pop();
135
136 PixmapWithMask* result = new PixmapWithMask();
137 result->pixmap() = fbpm;
138 result->mask() = fbmask;
139 return result;
140 }
141
142 // loading failure
143 imlib_context_pop();
144 return 0;
145}
146
147} // end namespace FbTk