Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "wave-image.h"
00036
00037 #include "common/wave_ex.h"
00038 #include "nstream/nstream.h"
00039 #include "perf/perf.h"
00040 #include "threadsafe/smart_ptr.h"
00041 #include "wave-jpeg/wave-jpeg.h"
00042 #include "wave-png/wave-png.h"
00043
00044
00045 namespace media {
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 class Registry {
00062 public:
00063
00064 void addLoader(IN smart_ptr<ImageLoader>& loader);
00065 bool load(IN nstream::Manager * mgr, IN const char * name,
00066 OUT image_t& image);
00067
00068 private:
00069
00070 typedef std::vector<smart_ptr<ImageLoader> > vec_loader_t;
00071
00072
00073 vec_loader_t m_loaders;
00074 };
00075
00076
00077
00078 void
00079 Registry::addLoader
00080 (
00081 IN smart_ptr<ImageLoader>& loader
00082 )
00083 {
00084 ASSERT(loader, "null");
00085
00086 m_loaders.push_back(loader);
00087 }
00088
00089
00090
00091 bool
00092 Registry::load
00093 (
00094 IN nstream::Manager * mgr,
00095 IN const char * name,
00096 OUT image_t& image
00097 )
00098 {
00099 ASSERT(mgr, "null");
00100 ASSERT(name, "null");
00101
00102 DPRINTF("Looking for a loader for image: %s", name);
00103
00104
00105 for (vec_loader_t::iterator i = m_loaders.begin(); i != m_loaders.end();
00106 ++i) {
00107 ImageLoader * loader = *i;
00108 ASSERT(loader, "null");
00109
00110 DPRINTF("Checking loader '%s'...", loader->getLoaderName());
00111
00112 if (loader->canLoadImage(mgr, name)) {
00113 DPRINTF("Found loader! '%s'", loader->getLoaderName());
00114
00115
00116 smart_ptr<nstream::File> file = mgr->getEntry(name);
00117 ASSERT_THROW(file,
00118 "image path is not a file: " << name);
00119 smart_ptr<nstream::Stream> stream = file->openStream();
00120 ASSERT_THROW(stream,
00121 "could not open image file: " << name);
00122
00123
00124 image.clear();
00125
00126
00127 std::string timerName = "loadImage:";
00128 timerName += loader->getLoaderName();
00129 {
00130 perf::Timer timer(timerName.c_str());
00131 loader->load(stream, image);
00132 }
00133
00134 ASSERT_THROW(image.isValid(),
00135 "loader did not return a valid image for file: "
00136 << name);
00137
00138 return true;
00139 }
00140 }
00141
00142
00143 return false;
00144 }
00145
00146
00147
00148 #define ADD_IMAGE_LOADER( reg , type ) \
00149 { \
00150 DPRINTF("Adding image loader: '%s'...", #type ); \
00151 smart_ptr<ImageLoader> loader = \
00152 create ##type ##ImageLoader(); \
00153 ASSERT(loader, "Failed to create loader of type: %s", \
00154 #type ); \
00155 reg->addLoader(loader); \
00156 }
00157
00158
00159 static Registry *
00160 getRegistry
00161 (
00162 void
00163 )
00164 {
00165
00166
00167 static smart_mutex s_mutex;
00168 mlock l(s_mutex);
00169
00170 static Registry * s_reg = NULL;
00171 if (!s_reg) {
00172 s_reg = new Registry;
00173
00174
00175
00176
00177
00178 ADD_IMAGE_LOADER(s_reg, Png);
00179 ADD_IMAGE_LOADER(s_reg, Jpeg);
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 }
00194 return s_reg;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 bool
00206 loadImage
00207 (
00208 IN nstream::Manager * mgr,
00209 IN const char * name,
00210 OUT image_t& image
00211 )
00212 {
00213 perf::Timer timer("loadImage");
00214 ASSERT(mgr, "null");
00215 ASSERT(name, "null");
00216
00217
00218 Registry * reg = getRegistry();
00219 ASSERT(reg, "null");
00220 return reg->load(mgr, name, image);
00221 }
00222
00223
00224
00225 };
00226