Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Custom image plugin testing difficulties
QtWS25 Last Chance

Custom image plugin testing difficulties

Scheduled Pinned Locked Moved Installation and Deployment
17 Posts 2 Posters 3.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChrisM87
    wrote on last edited by
    #1

    I just made a custom image plugin to read Playstation texture format files (*.tim) into Qt programs that use QImage. I made sure to try and follow the details when implementing and it compiles successfully without any warnings. However, I made a simple tim2png program that's literally just this, with some qDebug() output I used omitted:

    #include <QImage>
    
    int main()
    {
    	QImage img;
    	img.load("0025.tim", "tim");
    	img.save("0025.png");
    	return 0;
    }
    

    However, when running the test program, the program exits immediately. A bit of qDebug() statements show that the image is not loaded nor saved successfully and I wouldn't know what exception class to usew to diagnose this. I've tried puting my plugin library in the same directory and then in an "imageformats" subdirectory with no luck. As for the plugin itself, this is a bit lengthy, but please bear with me:

    // timplugin.h
    #ifndef _TIMPLUGIN_H
    #define _TIMPLUGIN_H
    
    #include <QByteArray>
    #include <QImageIOPlugin>
    
    class QStringList;
    class QImageIOHandler;
    class QIODevice;
    
    class TimPlugin final: public QImageIOPlugin
    {
    	Q_OBJECT
    	Q_PLUGIN_METADATA(IID "TimPlugin" FILE "tim.json")
    public:
    	QStringList keys() const;
    	Capabilities capabilities(QIODevice *dev, const QByteArray &fmt) const;
    	QImageIOHandler* create(QIODevice *dev, const QByteArray &fmt = QByteArray()) const;
    };
    
    #endif // _TIMPLUGIN_H
    
    // timplugin.cpp
    
    #include <QIODevice>
    #include <QStringList>
    
    #include "timhandler.hpp"
    #include "timplugin.hpp"
    
    QStringList TimPlugin::keys() const
    {
    	return QStringList() << "tim";
    }
    
    QImageIOPlugin::Capabilities TimPlugin::capabilities(QIODevice *dev, const QByteArray &fmt) const
    {
    	if (fmt == "tim") return Capabilities(CanRead);
    	if (!(fmt.isEmpty() && dev->isOpen())) return 0;
    	
    	Capabilities cap;
    	if (dev->isReadable()) cap |= CanRead;
    	
    	return cap;
    }
    
    QImageIOHandler* TimPlugin::create(QIODevice *dev, const QByteArray &fmt) const
    {
    	QImageIOHandler *hnd = new TimHandler;
    	hnd->setDevice(dev);
    	hnd->setFormat(fmt);
    	return hnd;
    }
    
    // timhandler.h
    #ifndef _TIMHANDLER_H
    #define _TIMHANDLER_H
    
    #include <QImageIOHandler>
    
    class TimHandler final: public QImageIOHandler
    {
    public:
    	bool canRead() const;
    	bool read(QImage *img);
    	QVariant option(ImageOption op) const override;
    	bool supportsOption(ImageOption op) const override;
    };
    
    #endif // _TIMHANDLER_H
    
    // timhandler.cpp
    #include <QDataStream>
    #include <QImage>
    #include <QIODevice>
    #include <QtGlobal>
    #include <QtMath>
    #include <QVariant>
    #include <QVector>
    
    #include "timhandler.hpp"
    
    #define BIT16 0.121568627451
    
    bool TimHandler::canRead() const
    {
    	return device()->peek(4) == "\x10\x0\x0\x0";
    }
    
    static inline quint32 rgba5551ToArgb32(quint16 c)
    {
    	quint32 r = ((c & 0xf800) >> 11) / BIT16;
    	quint32 g = ((c & 0x7c0) >> 6) / BIT16;
    	quint32 b = ((c & 0x3e) >> 1) / BIT16;
    	quint32 a = c & 1 ? 0 : 0xff;
    	
    	return ((a << 24) | (r << 16) | (g << 8) | b);
    }
    
    bool TimHandler::read(QImage *img)
    {
    	QDataStream ds(device());
    	ds.setByteOrder(QDataStream::LittleEndian);
    	quint32 magic, flags;
    	
    	ds >> magic >> flags;
    	if (ds.status() != QDataStream::Ok || magic != 0x10)
    		return false;
    	bool hasClut = flags & 0x8 ? true : false;
    	quint8 pxlMode = flags & 0x7;
    	
    	quint8 bpp;
    	switch (pxlMode)
    	{
    		case 0: bpp = 4; break;
    		case 1: bpp = 8; break;
    		case 2: bpp = 16; break;
    		case 3: bpp = 24; break;
    		default: bpp = 4;
    	}
    	
    	QVector<QRgb> clut;
    	if (hasClut)
    	{
    		quint32 colors, npals;
    		ds.skipRawData(12);
    		ds >> colors >> npals;
    		
    		for (quint16 i = 0; i < (colors*npals); i++)
    		{
    			quint16 c;
    			ds >> c;
    			clut.append(rgba5551ToArgb32(c));
    		}
    	}
    	
    	ds.skipRawData(8);
    	quint16 w, h;
    	ds >> w >> h;
    	w = (w*16)/bpp;
    	
    	if (img) delete img;
    	img = new QImage(w, h, QImage::Format_ARGB32);
    	if (hasClut) img->setColorTable(clut);
    	
    	for (quint16 y = 0; y < h; y++)
    	{
    		for (quint16 x = 0; x < w; x++)
    		{
    			switch (bpp)
    			{
    				case 4:
    				{
    					quint8 p;
    					ds >> p;
    					img->setPixel(x, y, p & 0xf0);
    					img->setPixel(x, y, p & 0xf);
    				}
    					break;
    				case 8:
    				{
    					quint8 p;
    					ds >> p;
    					img->setPixel(x, y, p);
    				}
    					break;
    				case 16:
    				{
    					quint16 p;
    					ds >> p;
    					img->setPixel(x, y, rgba5551ToArgb32(p));
    				}
    					break;
    			}
    		}
    	}
    	
    	return true;
    }
    
    bool TimHandler::supportsOption(ImageOption op) const
    {
    	switch (op)
    	{
    		case Size:
    			return true;
    		default:
    			return false;
    	}
    }
    
    QVariant TimHandler::option(ImageOption op) const
    {
    	switch (op)
    	{
    		case Size:
    		{
    			quint32 size;
    			quint16 w, h;
    			QByteArray ba = device()->peek(8);
    			QDataStream ds(&ba, QIODevice::ReadOnly);
    			ds >> size;
    			size -= 4; // 12 - TIM header + image size/palette x/y
    			ds.skipRawData(size);
    			ds >> w >> h;
    			return QSize(w, h);
    		}
    		default:
    			return QVariant();
    	}
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You don't instantiate a QApplication so there's not way for QImage to know where to load your plugin from

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisM87
        wrote on last edited by ChrisM87
        #3

        Oops, instantiate... okay, my bad. Thanks

        EDIT: Still doesn't work, and QT_DEBUG_PLUGINS shows normal results:

        Found metadata in lib /home/admin/Documents/Qt-extra/imageformats/tim/timtest/imageformats/libtim.so, metadata=
        {
        "IID": "TimPlugin",
        "MetaData": {
        "Keys": [
        "tim"
        ]
        },
        "className": "TimPlugin",
        "debug": true,
        "version": 328705
        }

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The most simple is to copy your plugin in Qt's plugin folder or you can tell your application to search in the folder containing your plugin with QCoreApplication::addLibraryPath

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisM87
            wrote on last edited by
            #5

            Well as I showed above, with the QT_DEBUG_PLUGINS variable set, the program is picking it up. However, it still refuses to load it. Is the IID or keys array invalid? I figured if it was choking on the plugin code itself, it would either error, segfault, or spit out a corrupt PNG.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Without seeing your code, it's pretty much Crystal Ball Debugging (™) I can't say

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisM87
                wrote on last edited by ChrisM87
                #7

                Well, I had posted my code in the original post. However, my handler class was tweaked a bit after spotting some errors, so here's the updated class code:

                EDIT: I did a few tests on QImageReader that should fail with unsupported format error, but I keep getting "unknown error." I attempted to load my ".pro" qmake file to see which error it would get but still unknown error. However, I compiled qtraw from Github, dropped it into an imageformats subdirectory and it ran fine.

                #include <QDataStream>
                #include <QImage>
                #include <QIODevice>
                #include <QtGlobal>
                #include <QtMath>
                #include <QVariant>
                #include <QVector>
                #include <QImageIOHandler>
                
                class TimHandler final: public QImageIOHandler
                {
                public:
                	bool canRead() const;
                	bool read(QImage *img);
                	QVariant option(ImageOption op) const override;
                	bool supportsOption(ImageOption op) const override;
                };
                
                #define BIT16 0.121568627451
                
                bool TimHandler::canRead() const
                {
                	return device()->peek(4) == "\x10\x0\x0\x0";
                }
                
                static inline quint32 rgba5551ToArgb32(quint16 c)
                {
                	quint32 r = ((c & 0xf800) >> 11) / BIT16;
                	quint32 g = ((c & 0x7c0) >> 6) / BIT16;
                	quint32 b = ((c & 0x3e) >> 1) / BIT16;
                	quint32 a = c & 1 ? 0 : 0xff;
                	
                	return ((a << 24) | (r << 16) | (g << 8) | b);
                }
                
                bool TimHandler::read(QImage *img)
                {
                	QDataStream ds(device());
                	ds.setByteOrder(QDataStream::LittleEndian);
                	quint32 magic, flags;
                	
                	ds >> magic >> flags;
                	if (ds.status() != QDataStream::Ok || magic != 0x10)
                		return false;
                	bool hasClut = flags & 0x8 ? true : false;
                	quint8 pxlMode = flags & 0x7;
                	
                	quint8 bpp;
                	switch (pxlMode)
                	{
                		case 0: bpp = 4; break;
                		case 1: bpp = 8; break;
                		case 2: bpp = 16; break;
                		case 3: bpp = 24; break;
                		default: bpp = 4;
                	}
                	
                	QVector<QRgb> clut;
                	if (hasClut)
                	{
                		quint32 colors, npals;
                		ds.skipRawData(12);
                		ds >> colors >> npals;
                		
                		for (quint16 i = 0; i < (colors*npals); i++)
                		{
                			quint16 c;
                			ds >> c;
                			clut.append(rgba5551ToArgb32(c));
                		}
                	}
                	
                	ds.skipRawData(8);
                	quint16 w, h;
                	ds >> w >> h;
                	w = (w*16)/bpp;
                	
                	if (img) delete img;
                	img = new QImage(w, h, QImage::Format_ARGB32);
                	if (hasClut) img->setColorTable(clut);
                	
                	for (quint16 y = 0; y < h; y++)
                	{
                		for (quint16 x = 0; x < w; x++)
                		{
                			switch (bpp)
                			{
                				case 4:
                				{
                					quint8 p;
                					ds >> p;
                					img->setPixel(x++, y, p & 0xf0);
                					img->setPixel(x, y, p & 0xf);
                				}
                					break;
                				case 8:
                				{
                					quint8 p;
                					ds >> p;
                					img->setPixel(x, y, p);
                				}
                					break;
                				case 16:
                				{
                					quint16 p;
                					ds >> p;
                					img->setPixel(x, y, rgba5551ToArgb32(p));
                				}
                					break;
                			}
                		}
                	}
                	
                	return true;
                }
                
                bool TimHandler::supportsOption(ImageOption op) const
                {
                	switch (op)
                	{
                		case Size:
                			return true;
                		default:
                			return false;
                	}
                }
                
                QVariant TimHandler::option(ImageOption op) const
                {
                	switch (op)
                	{
                		case Size:
                		{
                			quint32 size;
                			quint16 w, h;
                			QDataStream ds(device());
                			ds.skipRawData(8);
                			ds >> size;
                			size -= 4; // 12 - image size - palette x - palette y
                			ds.skipRawData(size);
                			ds >> w >> h;
                			return QSize(w, h);
                		}
                		default:
                			return QVariant();
                	}
                }
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Sorry about the code part, I had a problem when I read it first and only saw the end of it.
                  What about a more simple json ?

                  {
                      "Keys": [ "tim" ],
                      "MimeTypes": [ "image/x-tim" ]
                  }
                  
                  

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    ChrisM87
                    wrote on last edited by ChrisM87
                    #9

                    Well, while I could try that, I don't know if that's the mime type for TIM files. A Google search doesn't help either. My examination of qtraw shows that it too lacks a mime type entry. Thus, I thought it was unnecessary.

                    EDIT: tried it anyway, still nothing

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Just thought of something… Your plugin code is correct but:

                      img.load("0025.tim", "tim");

                      "0025.tim" is a relative path, is that image in the same folder as your executable ? You should rather give the full path to it.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        ChrisM87
                        wrote on last edited by ChrisM87
                        #11

                        Same directory, yep. Though of course in a real world scenario this would be different.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          I forgot a correction I made:

                          Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "tim.json")

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            ChrisM87
                            wrote on last edited by
                            #13

                            Yeah, I changed it to that in my progress trying to fix it, but no go. I booted up QtCreator and created a new project from Qt plugin template but I see nothing unusual in comparison with my code, aside from the constructor with QObject* parameter, which I skipped in my code because I saw no point for an empty constructor/destructor.

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              ChrisM87
                              wrote on last edited by
                              #14

                              So after a little tinkering, I got qmake to generate main.moc which I thought was needed, but I'm still having issues with the plugin not being discovered. Additionally, the RGBA5551 conversion function was incorrect, which I fixed, but that shouldn't be the make-or-break factor.

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                Not being discovered or not loaded ? Did you put your plugin in a imageformats subfolder ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  ChrisM87
                                  wrote on last edited by ChrisM87
                                  #16

                                  Yep. Even set target.path in my .pro file thinking that was the issue. Qt seems very nitpicky.

                                  EDIT: Just debugged again, found this gem of an error:

                                  QLibraryPrivate::loadPlugin failed on "/home/admin/Documents/Qt-extra/imageformats/qtim/timtest/imageformats/libqtim.so" : "Cannot load library /home/admin/Documents/Qt-extra/imageformats/qtim/timtest/imageformats/libqtim.so: (/home/admin/Documents/Qt-extra/imageformats/qtim/timtest/imageformats/libqtim.so: undefined symbol: _ZN11QTimHandler9setOptionEN15QImageIOHandler11ImageOptionERK8QVariant)"
                                  
                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    Which version of Qt are you using ?
                                    What happens if you try to load it by hand using QLibrary ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved