14 #include <tdeapplication.h>
15 #include <kstandarddirs.h>
18 #include <tqvaluelist.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>
30 #include <kstaticdeleter.h>
34 KImageIOFormat::KImageIOFormat(
const TQString &path)
40 TDEConfig config(path,
true,
false);
42 config.setGroup(
"Image Format");
43 mType = config.readEntry(
"Type");
44 mHeader = KURL::decode_string(config.readEntry(
"Header"), 4);
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");
55 KImageIOFormat::KImageIOFormat( TQDataStream& _str,
int offset) :
56 KSycocaEntry( _str, offset)
64 KImageIOFormat::~KImageIOFormat()
69 KImageIOFormat::load( TQDataStream& _str)
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);
82 bWrite = (iWrite != 0);
86 KImageIOFormat::save( TQDataStream& _str)
88 KSycocaEntry::save( _str );
89 TQ_INT8 iRead = bRead ? 1 : 0;
90 TQ_INT8 iWrite = bWrite ? 1 : 0;
92 _str << mType << mHeader << mFlags << iRead << iWrite
93 << mSuffices << mMimetype << mLib << mPattern << rPaths;
97 KImageIOFormat::callLibFunc(
bool read, TQImageIO *iio)
106 TQString libpath = KLibLoader::findLibrary(mLib.ascii());
107 if ( libpath.isEmpty())
112 lt_dlhandle libhandle = lt_dlopen( TQFile::encodeName(libpath) );
113 if (libhandle == 0) {
115 kdWarning() <<
"KImageIOFormat::callLibFunc: couldn't dlopen " << mLib <<
"(" << lt_dlerror() <<
")" << endl;
122 funcName =
"kimgio_"+mType.lower()+
"_read";
123 lt_ptr func = lt_dlsym(libhandle, funcName.ascii());
127 kdWarning() <<
"couln't find " << funcName <<
" (" << lt_dlerror() <<
")" << endl;
129 mReadFunc = (void (*)(TQImageIO *))func;
133 funcName =
"kimgio_"+mType.lower()+
"_write";
134 lt_ptr func = lt_dlsym(libhandle, funcName.ascii());
138 kdWarning() <<
"couln't find " << funcName <<
" (" << lt_dlerror() <<
")" << endl;
140 mWriteFunc = (void (*)(TQImageIO *))func;
157 KImageIOFactory *KImageIOFactory::_self = 0;
158 KImageIOFormatList *KImageIOFactory::formatList = 0;
160 static KStaticDeleter<KImageIOFormatList> kiioflsd;
162 KImageIOFactory::KImageIOFactory() : KSycocaFactory( KST_KImageIO )
168 KSycocaEntry::read(*m_str, mReadPattern);
169 KSycocaEntry::read(*m_str, mWritePattern);
170 KSycocaEntry::read(*m_str, rPath);
173 kiioflsd.setObject( formatList,
new KImageIOFormatList());
176 for(TQStringList::Iterator it = rPath.begin();
177 it != rPath.end(); ++it)
178 lt_dladdsearchdir( TQFile::encodeName(*it) );
183 if (KSycoca::self()->isBuilding())
188 formatList =
new KImageIOFormatList();
199 TQStringList patterns;
200 TQString allPatterns;
201 TQString wildCard(
"*.");
202 TQString separator(
"|");
203 for( KImageIOFormatList::ConstIterator it = formatList->begin();
204 it != formatList->end();
207 KImageIOFormat *format = (*it);
208 if (((_mode == KImageIO::Reading) && format->bRead) ||
209 ((_mode == KImageIO::Writing) && format->bWrite))
212 TQStringList suffices = format->mSuffices;
213 for( TQStringList::ConstIterator it = suffices.begin();
214 it != suffices.end();
217 if (!pattern.isEmpty())
219 pattern = pattern + wildCard+(*it);
220 if (!allPatterns.isEmpty())
222 allPatterns = allPatterns + wildCard +(*it);
224 if (!pattern.isEmpty())
226 pattern = pattern + separator + format->mPattern;
227 patterns.append(pattern);
231 allPatterns = allPatterns + separator + i18n(
"All Pictures");
233 patterns.prepend(allPatterns);
235 TQString pattern = patterns.join(TQString::fromLatin1(
"\n"));
240 KImageIOFactory::readImage( TQImageIO *iio)
243 const char *fm = iio->format();
245 fm = TQImageIO::imageFormat( iio->ioDevice());
246 kdDebug() <<
"KImageIO: readImage() format = " << fm << endl;
248 KImageIOFormat *format = 0;
249 for( KImageIOFormatList::ConstIterator it = formatList->begin();
250 it != formatList->end();
254 if (format->mType == fm)
257 if (!format || !format->bRead)
263 format->callLibFunc(
true, iio);
267 KImageIOFactory::writeImage( TQImageIO *iio)
270 const char *fm = iio->format();
272 fm = TQImageIO::imageFormat( iio->ioDevice());
273 kdDebug () <<
"KImageIO: writeImage() format = "<< fm << endl;
275 KImageIOFormat *format = 0;
276 for( KImageIOFormatList::ConstIterator it = formatList->begin();
277 it != formatList->end();
281 if (format->mType == fm)
284 if (!format || !format->bWrite)
290 format->callLibFunc(
false, iio);
294 KImageIOFactory::load()
296 KSycocaEntry::List list = allEntries();
297 for( KSycocaEntry::List::Iterator it = list.begin();
301 KSycocaEntry *entry =
static_cast<KSycocaEntry *
>(*it);
302 KImageIOFormat *format =
static_cast<KImageIOFormat *
>(entry);
308 for( KImageIOFormatList::ConstIterator it = formatList->begin();
309 it != formatList->end();
312 KImageIOFormat *_format = (*it);
313 if (format->mType == _format->mType)
322 if (!format->mHeader.isEmpty() && !format->mLib.isEmpty())
324 void (*readFunc)(TQImageIO *);
325 void (*writeFunc)(TQImageIO *);
327 readFunc = readImage;
331 writeFunc = writeImage;
334 TQImageIO::defineIOHandler( format->mType.ascii(),
335 format->mHeader.ascii(),
336 format->mFlags.ascii(),
337 readFunc, writeFunc);
339 formatList->append( format );
343 KImageIOFactory::~KImageIOFactory()
357 KImageIOFactory::createEntry(
int offset)
359 KImageIOFormat *format = 0;
361 TQDataStream *str = KSycoca::self()->findEntry(offset, type);
364 case KST_KImageIOFormat:
365 format =
new KImageIOFormat(*str, offset);
370 if (!format->isValid())
380 (void) KImageIOFactory::self();
386 if (_mode == Reading)
387 return KImageIOFactory::self()->mReadPattern;
389 return KImageIOFactory::self()->mWritePattern;
394 KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
398 for( KImageIOFormatList::ConstIterator it = formatList->begin();
399 it != formatList->end();
402 KImageIOFormat *format = (*it);
403 if (format->mType ==
type)
404 return format->bWrite;
413 KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
417 for( KImageIOFormatList::ConstIterator it = formatList->begin();
418 it != formatList->end();
421 KImageIOFormat *format = (*it);
422 if (format->mType ==
type)
423 return format->bRead;
431 KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
436 for( KImageIOFormatList::ConstIterator it = formatList->begin();
437 it != formatList->end();
440 KImageIOFormat *format = (*it);
441 if (((_mode == Reading) && format->bRead) ||
442 ((_mode == Writing) && format->bWrite))
443 types.append(format->mType);
452 KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
456 for( KImageIOFormatList::ConstIterator it = formatList->begin();
457 it != formatList->end();
460 KImageIOFormat *format = (*it);
461 if (format->mType ==
type)
462 return format->mSuffices[0];
466 return TQString::null;
471 KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
475 for( KImageIOFormatList::ConstIterator it = formatList->begin();
476 it != formatList->end();
479 KImageIOFormat *format = (*it);
481 return format->mType;
485 return TQString::null;
490 KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
491 TQString
suffix = filename;
492 int dot =
suffix.findRev(
'.');
498 for( KImageIOFormatList::ConstIterator it = formatList->begin();
499 it != formatList->end();
502 KImageIOFormat *format = (*it);
503 if (format->mSuffices.contains(
suffix))
504 return format->mType;
508 return TQString::null;
513 KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
514 TQStringList mimeList;
518 for( KImageIOFormatList::ConstIterator it = formatList->begin();
519 it != formatList->end();
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 );
535 KImageIOFormatList *formatList = KImageIOFactory::self()->formatList;
539 for( KImageIOFormatList::ConstIterator it = formatList->begin();
540 it != formatList->end();
543 KImageIOFormat *format = (*it);
544 if (format->mMimetype == _mimeType)
546 if (((_mode == Reading) && format->bRead) ||
547 ((_mode == Writing) && format->bWrite))
561 void KImageIOFormat::virtual_hook(
int id,
void* data )
562 { KSycocaEntry::virtual_hook(
id, data ); }
564 void KImageIOFactory::virtual_hook(
int id,
void* data )
565 { KSycocaFactory::virtual_hook(
id, data ); }
static TQString typeForMime(const TQString &mimeType)
Returns the type of a MIME type.
static void registerFormats()
Registers all KImageIO supported formats.
static TQString mimeType(const TQString &_filename)
Returns the MIME type of _filename.
static TQString type(const TQString &filename)
Returns the type of given filename.
static bool canRead(const TQString &type)
Checks if a special type is supported for reading.
static TQStringList types(Mode mode=Writing)
Returns a list of all KImageIO supported formats.
static bool canWrite(const TQString &type)
Checks if a special type is supported for writing.
static TQString suffix(const TQString &type)
Returns the suffix of an image type.
static bool isSupported(const TQString &_mimeType, Mode _mode=Writing)
Test to see whether a MIME type is supported to reading/writing.
static TQStringList mimeTypes(Mode mode=Writing)
Returns a list of MIME types for all KImageIO supported formats.
Mode
Possible image file access modes.
static TQString pattern(Mode mode=Reading)
Returns a list of patterns of all KImageIO supported formats.
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.