image handling

This commit is contained in:
fluxgen 2003-08-22 21:25:14 +00:00
parent 7b059d2399
commit 834645f545
5 changed files with 335 additions and 0 deletions

120
src/FbTk/Image.cc Normal file
View file

@ -0,0 +1,120 @@
// Image.cc for FbTk - Fluxbox ToolKit
// Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// $Id: Image.cc,v 1.1 2003/08/22 21:25:14 fluxgen Exp $
#include "Image.hh"
#include "StringUtil.hh"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H
#ifdef HAVE_XPM
#include "ImageXPM.hh"
#endif /// HAVE_XPM
#include <list>
#include <iostream>
using namespace std;
namespace FbTk {
Image::ImageMap Image::s_image_map;
Image::StringList Image::s_search_paths;
PixmapWithMask *Image::load(const std::string &filename, int screen_num) {
#ifdef HAVE_XPM
// we must do this because static linkage with libFbTk will not init
// a static autoreg variable for it
static ImageXPM xpm;
#endif // HAVE_XPM
if (filename == "")
return false;
// determine file ending
std::string extension(StringUtil::toUpper(StringUtil::findExtension(filename)));
// valid handle?
if (s_image_map[extension] == 0)
return false;
// load file
PixmapWithMask *pm = s_image_map[extension]->load(filename, screen_num);
// failed?, try different search paths
if (pm == 0 && s_search_paths.size()) {
// first we need to get basename of current filename
std::string base_filename = StringUtil::basename(filename);
// append each search path and try to load
StringList::iterator it = s_search_paths.begin();
StringList::iterator it_end = s_search_paths.end();
for (; it != it_end && pm == 0; ++it) {
// append search path and try load it
std::string path = StringUtil::expandFilename(*it);
pm = s_image_map[extension]->load(path + "/" + base_filename, screen_num);
}
}
return pm;
}
bool Image::registerType(const std::string &type, ImageBase &base) {
string ucase_type = StringUtil::toUpper(type);
// not empty and not this base?
if (s_image_map[ucase_type] != 0 &&
s_image_map[ucase_type] != &base)
return false;
// already registered?
if (s_image_map[ucase_type] = &base)
return true;
s_image_map[ucase_type] = &base;
}
void Image::remove(ImageBase &base) {
// find and remove all referenses to base
ImageMap::iterator it = s_image_map.begin();
ImageMap::iterator it_end = s_image_map.end();
std::list<std::string> remove_list;
for (; it != it_end; ++it) {
if (it->second == &base)
remove_list.push_back(it->first);
}
while (!remove_list.empty()) {
s_image_map.erase(remove_list.back());
remove_list.pop_back();
}
}
void Image::addSearchPath(const std::string &search_path) {
s_search_paths.push_back(search_path);
}
void Image::removeAllSearchPaths() {
s_search_paths.clear();
}
}; // end namespace FbTk

70
src/FbTk/Image.hh Normal file
View file

@ -0,0 +1,70 @@
// Image.hh for FbTk - Fluxbox ToolKit
// Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// $Id: Image.hh,v 1.1 2003/08/22 21:25:14 fluxgen Exp $
#ifndef FBTK_IMAGE_HH
#define FBTK_IMAGE_HH
#include <string>
#include <list>
#include <map>
namespace FbTk {
class ImageBase;
class PixmapWithMask;
/// loads images
class Image {
public:
/// @return an instance of PixmapWithMask on success, 0 on failure
static PixmapWithMask *load(const std::string &filename, int screen_num);
/// for register file type and imagebase
/// @return false on failure
static bool registerType(const std::string &type, ImageBase &base);
/// removes a imagebase class from register
/// @return false on failure
static void remove(ImageBase &base);
/// adds a path to search images from
static void addSearchPath(const std::string &search_path);
/// adds a path to search images from
static void removeAllSearchPaths();
private:
typedef std::map<std::string, ImageBase *> ImageMap;
typedef std::list<std::string> StringList;
static ImageMap s_image_map;
static StringList s_search_paths;
};
/// common interface for all image classes
class ImageBase {
public:
virtual ~ImageBase() { Image::remove(*this); }
virtual PixmapWithMask *load(const std::string &name, int screen_num) const = 0;
};
}; // end namespace FbTk
#endif // IMAGE_HH

54
src/FbTk/ImageXPM.cc Normal file
View file

@ -0,0 +1,54 @@
// ImageXPM.cc for FbTk - Fluxbox ToolKit
// Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// $Id: ImageXPM.cc,v 1.1 2003/08/22 21:25:14 fluxgen Exp $
#include "ImageXPM.hh"
#include "App.hh"
#include "PixmapWithMask.hh"
#include <X11/xpm.h>
namespace FbTk {
ImageXPM::ImageXPM() {
Image::registerType("XPM", *this);
}
PixmapWithMask *ImageXPM::load(const std::string &filename, int screen_num) const {
XpmAttributes xpm_attr;
xpm_attr.valuemask = 0;
Display *dpy = FbTk::App::instance()->display();
Pixmap pm = 0, mask = 0;
int retvalue = XpmReadFileToPixmap(dpy,
RootWindow(dpy, screen_num),
const_cast<char *>(filename.c_str()),
&pm,
&mask, &xpm_attr);
if (retvalue == 0) // success
return new PixmapWithMask(pm, mask);
else // failure
return 0;
}
} // end namespace FbTk

38
src/FbTk/ImageXPM.hh Normal file
View file

@ -0,0 +1,38 @@
// ImageXPM.hh for FbTk - Fluxbox ToolKit
// Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// $Id: ImageXPM.hh,v 1.1 2003/08/22 21:25:14 fluxgen Exp $
#ifndef FBTK_IMAGEXPM_HH
#define FBTK_IMAGEXPM_HH
#include "Image.hh"
namespace FbTk {
class ImageXPM: public ImageBase {
public:
ImageXPM();
PixmapWithMask *load(const std::string &filename, int screen_num) const;
};
} // end namespace FbTk
#endif // FBTK_IMAGEXPM_HH

View file

@ -0,0 +1,53 @@
// PixmapWithMask.hh for FbTk - Fluxbox ToolKit
// Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// $Id: PixmapWithMask.hh,v 1.1 2003/08/22 21:25:14 fluxgen Exp $
#ifndef FBTK_PIXMAPWITHMASK_HH
#define FBTK_PIXMAPWITHMASK_HH
#include "FbPixmap.hh"
namespace FbTk {
class PixmapWithMask {
public:
PixmapWithMask() { }
PixmapWithMask(Pixmap pm, Pixmap mask):m_pixmap(pm), m_mask(mask) { }
void scale(unsigned int width, unsigned int height) {
pixmap().scale(width, height);
mask().scale(width, height);
}
FbPixmap &pixmap() { return m_pixmap; }
FbPixmap &mask() { return m_mask; }
const FbPixmap &pixmap() const { return m_pixmap; }
const FbPixmap &mask() const { return m_mask; }
private:
FbPixmap m_pixmap;
FbPixmap m_mask;
};
} // end namespace FbTk
#endif // FBTK_PIXMAPWITHMASK_HH