• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

  • tdeio
  • tdeio
kimageio.cpp
1 
11 #include"config.h"
12 
13 #include <tqdir.h>
14 #include <tdeapplication.h>
15 #include <kstandarddirs.h>
16 #include <tqstring.h>
17 #include <tqregexp.h>
18 #include <tqvaluelist.h>
19 
20 #include <ltdl.h>
21 #include "kimageio.h"
22 #include "kimageiofactory.h"
23 #include <tdelocale.h>
24 #include <klibloader.h>
25 #include <tdeglobal.h>
26 #include <kmimetype.h>
27 #include <tdesycocaentry.h>
28 #include <tdesycoca.h>
29 #include <kdebug.h>
30 #include <kstaticdeleter.h>
31 
32 #include <tqimage.h>
33 
34 KImageIOFormat::KImageIOFormat( const TQString &path)
35  : KSycocaEntry(path)
36 {
37  bLibLoaded = false;
38  mReadFunc = 0;
39  mWriteFunc = 0;
40  TDEConfig config(path, true, false);
41 
42  config.setGroup("Image Format");
43  mType = config.readEntry("Type");
44  mHeader = KURL::decode_string(config.readEntry("Header"), 4); // Latin1
45  mFlags = config.readEntry("Flags");
46  bRead = config.readBoolEntry("Read");
47  bWrite = config.readBoolEntry("Write");
48  mSuffices = config.readListEntry("Suffices");
49  mPattern = config.readEntry("Name");
50  mMimetype = config.readEntry("Mimetype");
51  mLib = config.readPathEntry("Library");
52  rPaths = config.readPathListEntry("rPaths");
53 }
54 
55 KImageIOFormat::KImageIOFormat( TQDataStream& _str, int offset) :
56  KSycocaEntry( _str, offset)
57 {
58  bLibLoaded = false;
59  mReadFunc = 0;
60  mWriteFunc = 0;
61  load( _str );
62 }
63 
64 KImageIOFormat::~KImageIOFormat()
65 {
66 }
67 
68 void
69 KImageIOFormat::load( TQDataStream& _str)
70 {
71  TQ_INT8 iRead, iWrite;
72  KSycocaEntry::read(_str, mType);
73  KSycocaEntry::read(_str, mHeader);
74  KSycocaEntry::read(_str, mFlags);
75  _str >> iRead >> iWrite;
76  KSycocaEntry::read(_str, mSuffices);
77  KSycocaEntry::read(_str, mMimetype);
78  KSycocaEntry::read(_str, mLib);
79  KSycocaEntry::read(_str, mPattern);
80  KSycocaEntry::read(_str, rPaths);
81  bRead = (iRead != 0);
82  bWrite = (iWrite != 0);
83 }
84 
85 void
86 KImageIOFormat::save( TQDataStream& _str)
87 {
88  KSycocaEntry::save( _str );
89  TQ_INT8 iRead = bRead ? 1 : 0;
90  TQ_INT8 iWrite = bWrite ? 1 : 0;
91 
92  _str << mType << mHeader << mFlags << iRead << iWrite
93  << mSuffices << mMimetype << mLib << mPattern << rPaths;
94 }
95 
96 void
97 KImageIOFormat::callLibFunc( bool read, TQImageIO *iio)
98 {
99  if (!bLibLoaded)
100  {
101  if (mLib.isEmpty())
102  {
103  iio->setStatus(1); // Error
104  return;
105  }
106  TQString libpath = KLibLoader::findLibrary(mLib.ascii());
107  if ( libpath.isEmpty())
108  {
109  iio->setStatus(1); // Error
110  return;
111  }
112  lt_dlhandle libhandle = lt_dlopen( TQFile::encodeName(libpath) );
113  if (libhandle == 0) {
114  iio->setStatus(1); // error
115  kdWarning() << "KImageIOFormat::callLibFunc: couldn't dlopen " << mLib << "(" << lt_dlerror() << ")" << endl;
116  return;
117  }
118  bLibLoaded = true;
119  TQString funcName;
120  if (bRead)
121  {
122  funcName = "kimgio_"+mType.lower()+"_read";
123  lt_ptr func = lt_dlsym(libhandle, funcName.ascii());
124 
125  if (func == NULL) {
126  iio->setStatus(1); // error
127  kdWarning() << "couln't find " << funcName << " (" << lt_dlerror() << ")" << endl;
128  }
129  mReadFunc = (void (*)(TQImageIO *))func;
130  }
131  if (bWrite)
132  {
133  funcName = "kimgio_"+mType.lower()+"_write";
134  lt_ptr func = lt_dlsym(libhandle, funcName.ascii());
135 
136  if (func == NULL) {
137  iio->setStatus(1); // error
138  kdWarning() << "couln't find " << funcName << " (" << lt_dlerror() << ")" << endl;
139  }
140  mWriteFunc = (void (*)(TQImageIO *))func;
141  }
142 
143  }
144  if (read)
145  if (mReadFunc)
146  mReadFunc(iio);
147  else
148  iio->setStatus(1); // Error
149  else
150  if (mWriteFunc)
151  mWriteFunc(iio);
152  else
153  iio->setStatus(1); // Error
154 }
155 
156 
157 KImageIOFactory *KImageIOFactory::_self = 0;
158 KImageIOFormatList *KImageIOFactory::formatList = 0;
159 
160 static KStaticDeleter<KImageIOFormatList> kiioflsd;
161 
162 KImageIOFactory::KImageIOFactory() : KSycocaFactory( KST_KImageIO )
163 {
164  _self = this;
165  if (m_str)
166  {
167  // read from database
168  KSycocaEntry::read(*m_str, mReadPattern);
169  KSycocaEntry::read(*m_str, mWritePattern);
170  KSycocaEntry::read(*m_str, rPath);
171  if (!formatList)
172  {
173  kiioflsd.setObject( formatList, new KImageIOFormatList());
174  lt_dlinit(); // Do this only once!
175  // Add rPaths.
176  for(TQStringList::Iterator it = rPath.begin();
177  it != rPath.end(); ++it)
178  lt_dladdsearchdir( TQFile::encodeName(*it) );
179  }
180  load();
181  }
182  else
183  if (KSycoca::self()->isBuilding())
184  {
185  // Build database
186  if (!formatList)
187  {
188  formatList = new KImageIOFormatList();
189  }
190  } else
191  {
192  // We have no database at all.. uh-oh
193  }
194 }
195 
196 TQString
197 KImageIOFactory::createPattern( KImageIO::Mode _mode)
198 {
199  TQStringList patterns;
200  TQString allPatterns;
201  TQString wildCard("*.");
202  TQString separator("|");
203  for( KImageIOFormatList::ConstIterator it = formatList->begin();
204  it != formatList->end();
205  ++it )
206  {
207  KImageIOFormat *format = (*it);
208  if (((_mode == KImageIO::Reading) && format->bRead) ||
209  ((_mode == KImageIO::Writing) && format->bWrite))
210  {
211  TQString pattern;
212  TQStringList suffices = format->mSuffices;
213  for( TQStringList::ConstIterator it = suffices.begin();
214  it != suffices.end();
215  ++it)
216  {
217  if (!pattern.isEmpty())
218  pattern += " ";
219  pattern = pattern + wildCard+(*it);
220  if (!allPatterns.isEmpty())
221  allPatterns += " ";
222  allPatterns = allPatterns + wildCard +(*it);
223  }
224  if (!pattern.isEmpty())
225  {
226  pattern = pattern + separator + format->mPattern;
227  patterns.append(pattern);
228  }
229  }
230  }
231  allPatterns = allPatterns + separator + i18n("All Pictures");
232  patterns.sort();
233  patterns.prepend(allPatterns);
234 
235  TQString pattern = patterns.join(TQString::fromLatin1("\n"));
236  return pattern;
237 }
238 
239 void
240 KImageIOFactory::readImage( TQImageIO *iio)
241 {
242  (void) self(); // Make sure we exist
243  const char *fm = iio->format();
244  if (!fm)
245  fm = TQImageIO::imageFormat( iio->ioDevice());
246  kdDebug() << "KImageIO: readImage() format = " << fm << endl;
247 
248  KImageIOFormat *format = 0;
249  for( KImageIOFormatList::ConstIterator it = formatList->begin();
250  it != formatList->end();
251  ++it )
252  {
253  format = (*it);
254  if (format->mType == fm)
255  break;
256  }
257  if (!format || !format->bRead)
258  {
259  iio->setStatus(1); // error
260  return;
261  }
262 
263  format->callLibFunc( true, iio);
264 }
265 
266 void
267 KImageIOFactory::writeImage( TQImageIO *iio)
268 {
269  (void) self(); // Make sure we exist
270  const char *fm = iio->format();
271  if (!fm)
272  fm = TQImageIO::imageFormat( iio->ioDevice());
273  kdDebug () << "KImageIO: writeImage() format = "<< fm << endl;
274 
275  KImageIOFormat *format = 0;
276  for( KImageIOFormatList::ConstIterator it = formatList->begin();
277  it != formatList->end();
278  ++it )
279  {
280  format = (*it);
281  if (format->mType == fm)
282  break;
283  }
284  if (!format || !format->bWrite)
285  {
286  iio->setStatus(1); // error
287  return;
288  }
289 
290  format->callLibFunc( false, iio);
291 }
292 
293 void
294 KImageIOFactory::load()
295 {
296  KSycocaEntry::List list = allEntries();
297  for( KSycocaEntry::List::Iterator it = list.begin();
298  it != list.end();
299  ++it)
300  {
301  KSycocaEntry *entry = static_cast<KSycocaEntry *>(*it);
302  KImageIOFormat *format = static_cast<KImageIOFormat *>(entry);
303 
304  // Since Qt doesn't allow us to unregister image formats
305  // we have to make sure not to add them a second time.
306  // This typically happens when the sycoca database was updated
307  // we need to reread it.
308  for( KImageIOFormatList::ConstIterator it = formatList->begin();
309  it != formatList->end();
310  ++it )
311  {
312  KImageIOFormat *_format = (*it);
313  if (format->mType == _format->mType)
314  {
315  // Already in list
316  format = 0;
317  break;
318  }
319  }
320  if (!format)
321  continue;
322  if (!format->mHeader.isEmpty() && !format->mLib.isEmpty())
323  {
324  void (*readFunc)(TQImageIO *);
325  void (*writeFunc)(TQImageIO *);
326  if (format->bRead)
327  readFunc = readImage;
328  else
329  readFunc = 0;
330  if (format->bWrite)
331  writeFunc = writeImage;
332  else
333  writeFunc = 0;
334  TQImageIO::defineIOHandler( format->mType.ascii(),
335  format->mHeader.ascii(),
336  format->mFlags.ascii(),
337  readFunc, writeFunc);
338  }
339  formatList->append( format );
340  }
341 }
342 
343 KImageIOFactory::~KImageIOFactory()
344 {
345  _self = 0;
346 
347  // We would like to:
348  // * Free all KImageIOFormats.
349  // * Unload libs
350  // * Remove Qt IO handlers.
351  // But we can't remove IO handlers, so we better keep all KImageIOFormats
352  // in memory so that we can make sure not register IO handlers again whenever
353  // the sycoca database updates (Such event deletes this factory)
354 }
355 
356 KSycocaEntry*
357 KImageIOFactory::createEntry(int offset)
358 {
359  KImageIOFormat *format = 0;
360  KSycocaType type;
361  TQDataStream *str = KSycoca::self()->findEntry(offset, type);
362  switch (type)
363  {
364  case KST_KImageIOFormat:
365  format = new KImageIOFormat(*str, offset);
366  break;
367  default:
368  return 0;
369  }
370  if (!format->isValid())
371  {
372  delete format;
373  format = 0;
374  }
375  return format;
376 }
377 
378 void KImageIO::registerFormats()
379 {
380  (void) KImageIOFactory::self();
381 }
382 
383 TQString
384 KImageIO::pattern(Mode _mode)
385 {
386  if (_mode == Reading)
387  return KImageIOFactory::self()->mReadPattern;
388  else
389  return KImageIOFactory::self()->mWritePattern;
390 }
391 
392 bool KImageIO::canWrite(const TQString& type)
393 {
394  KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
395 
396  if(formatList)
397  {
398  for( KImageIOFormatList::ConstIterator it = formatList->begin();
399  it != formatList->end();
400  ++it )
401  {
402  KImageIOFormat *format = (*it);
403  if (format->mType == type)
404  return format->bWrite;
405  }
406  }
407 
408  return false;
409 }
410 
411 bool KImageIO::canRead(const TQString& type)
412 {
413  KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
414 
415  if(formatList)
416  {
417  for( KImageIOFormatList::ConstIterator it = formatList->begin();
418  it != formatList->end();
419  ++it )
420  {
421  KImageIOFormat *format = (*it);
422  if (format->mType == type)
423  return format->bRead;
424  }
425  }
426 
427  return false;
428 }
429 
430 TQStringList KImageIO::types(Mode _mode ) {
431  KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
432  TQStringList types;
433 
434  if(formatList)
435  {
436  for( KImageIOFormatList::ConstIterator it = formatList->begin();
437  it != formatList->end();
438  ++it )
439  {
440  KImageIOFormat *format = (*it);
441  if (((_mode == Reading) && format->bRead) ||
442  ((_mode == Writing) && format->bWrite))
443  types.append(format->mType);
444  }
445  }
446 
447  return types;
448 }
449 
450 TQString KImageIO::suffix(const TQString& type)
451 {
452  KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
453 
454  if(formatList)
455  {
456  for( KImageIOFormatList::ConstIterator it = formatList->begin();
457  it != formatList->end();
458  ++it )
459  {
460  KImageIOFormat *format = (*it);
461  if (format->mType == type)
462  return format->mSuffices[0];
463  }
464  }
465 
466  return TQString::null;
467 }
468 
469 TQString KImageIO::typeForMime(const TQString& mimeType)
470 {
471  KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
472 
473  if(formatList)
474  {
475  for( KImageIOFormatList::ConstIterator it = formatList->begin();
476  it != formatList->end();
477  ++it )
478  {
479  KImageIOFormat *format = (*it);
480  if (format->mMimetype == mimeType)
481  return format->mType;
482  }
483  }
484 
485  return TQString::null;
486 }
487 
488 TQString KImageIO::type(const TQString& filename)
489 {
490  KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
491  TQString suffix = filename;
492  int dot = suffix.findRev('.');
493  if (dot >= 0)
494  suffix = suffix.mid(dot + 1);
495 
496  if(formatList)
497  {
498  for( KImageIOFormatList::ConstIterator it = formatList->begin();
499  it != formatList->end();
500  ++it )
501  {
502  KImageIOFormat *format = (*it);
503  if (format->mSuffices.contains(suffix))
504  return format->mType;
505  }
506  }
507 
508  return TQString::null;
509 }
510 
511 TQStringList KImageIO::mimeTypes( Mode _mode )
512 {
513  KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
514  TQStringList mimeList;
515 
516  if(formatList)
517  {
518  for( KImageIOFormatList::ConstIterator it = formatList->begin();
519  it != formatList->end();
520  ++it )
521  {
522  KImageIOFormat *format = (*it);
523  if (((_mode == Reading) && format->bRead) ||
524  ((_mode == Writing) && format->bWrite))
525  if ( !format->mMimetype.isEmpty() )
526  mimeList.append ( format->mMimetype );
527  }
528  }
529 
530  return mimeList;
531 }
532 
533 bool KImageIO::isSupported( const TQString& _mimeType, Mode _mode )
534 {
535  KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
536 
537  if(formatList)
538  {
539  for( KImageIOFormatList::ConstIterator it = formatList->begin();
540  it != formatList->end();
541  ++it )
542  {
543  KImageIOFormat *format = (*it);
544  if (format->mMimetype == _mimeType)
545  {
546  if (((_mode == Reading) && format->bRead) ||
547  ((_mode == Writing) && format->bWrite))
548  return true;
549  }
550  }
551  }
552 
553  return false;
554 }
555 
556 TQString KImageIO::mimeType( const TQString& _filename )
557 {
558  return KMimeType::findByURL( KURL( _filename ) )->name();
559 }
560 
561 void KImageIOFormat::virtual_hook( int id, void* data )
562 { KSycocaEntry::virtual_hook( id, data ); }
563 
564 void KImageIOFactory::virtual_hook( int id, void* data )
565 { KSycocaFactory::virtual_hook( id, data ); }
566 
KImageIO::typeForMime
static TQString typeForMime(const TQString &mimeType)
Returns the type of a MIME type.
Definition: kimageio.cpp:469
KImageIO::registerFormats
static void registerFormats()
Registers all KImageIO supported formats.
Definition: kimageio.cpp:378
KImageIO::mimeType
static TQString mimeType(const TQString &_filename)
Returns the MIME type of _filename.
Definition: kimageio.cpp:556
KImageIO::type
static TQString type(const TQString &filename)
Returns the type of given filename.
Definition: kimageio.cpp:488
KImageIO::canRead
static bool canRead(const TQString &type)
Checks if a special type is supported for reading.
Definition: kimageio.cpp:411
KImageIO::types
static TQStringList types(Mode mode=Writing)
Returns a list of all KImageIO supported formats.
Definition: kimageio.cpp:430
KImageIO::canWrite
static bool canWrite(const TQString &type)
Checks if a special type is supported for writing.
Definition: kimageio.cpp:392
KImageIO::suffix
static TQString suffix(const TQString &type)
Returns the suffix of an image type.
Definition: kimageio.cpp:450
KImageIO::isSupported
static bool isSupported(const TQString &_mimeType, Mode _mode=Writing)
Test to see whether a MIME type is supported to reading/writing.
Definition: kimageio.cpp:533
KImageIO::mimeTypes
static TQStringList mimeTypes(Mode mode=Writing)
Returns a list of MIME types for all KImageIO supported formats.
Definition: kimageio.cpp:511
KImageIO::Mode
Mode
Possible image file access modes.
Definition: kimageio.h:77
KImageIO::pattern
static TQString pattern(Mode mode=Reading)
Returns a list of patterns of all KImageIO supported formats.
Definition: kimageio.cpp:384
KMimeType::findByURL
static Ptr findByURL(const KURL &_url, mode_t _mode=0, bool _is_local_file=false, bool _fast_mode=false)
Finds a KMimeType with the given _url.
Definition: kmimetype.cpp:165

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.9.1
This website is maintained by Timothy Pearson.