imageloader.cpp 3.86 KB
Newer Older
zmoelnig's avatar
zmoelnig committed
1
2
3
4
5
6
7
8
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.kug.ac.at
//
// Implementation file
//
zmoelnig's avatar
zmoelnig committed
9
//    Copyright (c) 2011-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
zmoelnig's avatar
zmoelnig committed
10
11
12
13
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
14

15
16
#include "imageloader.h"
#include "plugins/PluginFactory.h"
zmoelnig's avatar
zmoelnig committed
17

18
#include "Gem/RTE.h"
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
19
#include "Gem/Exception.h"
20

21
22
#include <algorithm>

23
gem::plugins::imageloader :: ~imageloader(void) {}
zmoelnig's avatar
zmoelnig committed
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
static gem::PluginFactoryRegistrar::dummy<gem::plugins::imageloader> fac_imageloaderdummy;

namespace gem { namespace plugins {
  class imageloaderMeta : public gem::plugins::imageloader {
  private:
    static imageloaderMeta*s_instance;
    std::vector<gem::plugins::imageloader*>m_loaders;
    std::vector<std::string>m_ids;
    bool m_canThread;
  public:
    imageloaderMeta(void) : m_canThread(true) {
      gem::PluginFactory<gem::plugins::imageloader>::loadPlugins("image");
      std::vector<std::string>available_ids=gem::PluginFactory<gem::plugins::imageloader>::getIDs();

      addLoader(available_ids, "magick");
      addLoader(available_ids);

42
43
44
      static bool firsttime=true;
      if(firsttime && m_ids.size()>0) {
        startpost("GEM: image loading support:");
45
46
47
48
49
50
        unsigned int i;
        for(i=0; i<m_ids.size(); i++) {
          startpost(" %s", m_ids[i].c_str());
        }
        endpost();
      }
51
      firsttime=false;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

      m_canThread=true;
      unsigned int i;
      for(i=0; i<m_loaders.size(); i++) {
        if(!m_loaders[i]->isThreadable()) {
          m_canThread=false;
          break;
        }
      }
    }
    bool addLoader( std::vector<std::string>available, std::string ID=std::string("")) {
      int count=0;

      std::vector<std::string>id;
      if(!ID.empty()) {
        // if requested 'cid' is in 'available' add it to the list of 'id's
        if(std::find(available.begin(), available.end(), ID)!=available.end()) {
          id.push_back(ID);
        } else {
          // request for an unavailable ID
          verbose(2, "backend '%s' unavailable", ID.c_str());
          return false;
        }
      } else {
        // no 'ID' given: add all available IDs
        id=available;
      }

      unsigned int i=0;
      for(i=0; i<id.size(); i++) {
        std::string key=id[i];
        verbose(2, "trying to add '%s' as backend", key.c_str());
        if(std::find(m_ids.begin(), m_ids.end(), key)==m_ids.end()) {
          // not yet added, do so now!
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
86
87
          gem::plugins::imageloader*loader=NULL;
          try {
88
            loader=gem::PluginFactory<gem::plugins::imageloader>::getInstance(key);
89
          } catch(GemException&x) {
IOhannes m zmölnig's avatar
IOhannes m zmölnig committed
90
91
92
93
            loader=NULL;
            verbose(1, "cannot use image loader plugin '%s': %s", key.c_str(), x.what());
          }
          if(NULL==loader)continue;
94
95
96
          m_ids.push_back(key);
          m_loaders.push_back(loader);
          count++;
97
          verbose(2, "added backend#%d '%s' @ %p", (int)(m_loaders.size()-1), key.c_str(), loader);
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        }
      }
      return (count>0);
    }

  public:
    virtual ~imageloaderMeta(void) {
      unsigned int i;
      for(i=0; i<m_loaders.size(); i++) {
        delete m_loaders[i];
        m_loaders[i]=NULL;
      }
    }

    virtual bool load(std::string filename, imageStruct&result, gem::Properties&props) {
      unsigned int i;
      for(i=0; i<m_loaders.size(); i++) {
        if(m_loaders[i]->load(filename, result, props))
          return true;
      }
      return false;
    }

    virtual bool isThreadable(void) {
      return m_canThread;
    }
  };  }; };


127
gem::plugins::imageloader*gem::plugins::imageloader::getInstance(void) {
128
129
  gem::plugins::imageloader*result=new imageloaderMeta();
 return result;
zmoelnig's avatar
zmoelnig committed
130
}