Dear all,

I am trying to speed up the loading of a JPEG image by replacing the decode step during the loading with some libturbojpeg functions.
So I put together a minimal working example from several pieces which I found in forums etc:


.pro file:
Qt Code:
  1. QT += core gui
  2.  
  3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  4.  
  5. TARGET = Application
  6. TEMPLATE = app
  7.  
  8.  
  9. SOURCES += main.cpp\
  10. mainwindow.cpp\
  11. JpegEncoderDecoder/JpegEncoderDecoder.cpp
  12.  
  13. HEADERS += mainwindow.h\
  14. JpegEncoderDecoder/JpegEncoderDecoder.h
  15.  
  16. FORMS += mainwindow.ui
  17.  
  18.  
  19. LIBS += -L$$PWD/usr/lib/ -ljpeg -lturbojpeg
  20.  
  21. INCLUDEPATH += $$PWD/usr/include
  22. DEPENDPATH += $$PWD/usr/include
To copy to clipboard, switch view to plain text mode 


main.cpp:
Qt Code:
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. #include <QImageReader>
  4. #include <QByteArray>
  5. #include <QFile>
  6. #include <QBuffer>
  7. #include <QString>
  8.  
  9. #include <jpeglib.h>
  10.  
  11. #include <iostream>
  12. #include <fstream>
  13. #include <string>
  14.  
  15. #include <JpegEncoderDecoder/JpegEncoderDecoder.h>
  16.  
  17.  
  18. int read_jpeg_file(unsigned char *pInputDataStream, std::string file_name)
  19. {
  20. std::ifstream imageStream;
  21. imageStream.open(file_name.c_str(), std::ios_base::binary);
  22.  
  23. imageStream.seekg (0, std::ios::end);
  24. int n = imageStream.tellg();
  25. imageStream.seekg (0, std::ios::beg);
  26.  
  27. pInputDataStream = new unsigned char[n];
  28.  
  29.  
  30. if (!imageStream.is_open())
  31. {
  32. std::cout << stderr << "Can't open input file !\n";
  33. exit(1);
  34. }
  35. else
  36. {
  37. imageStream.read((char *)pInputDataStream, n);
  38. }
  39. return 0;
  40. }
  41.  
  42.  
  43.  
  44. int main(int argc, char *argv[])
  45. {
  46. QApplication a(argc, argv);
  47.  
  48. QString qPath("jpeg-100.jpg");
  49.  
  50. unsigned char *pInputDataStream;
  51. int width = 320;
  52. int height= 240;
  53. int JPEG_QUALITY=75;
  54. int COLOR_COMPONENTS=3;
  55. int in_len = width*height*COLOR_COMPONENTS;
  56. std::string strFileName=qPath.toStdString();
  57.  
  58. //Step 1: Load .jpeg in memory filebuffer
  59. int iRead = read_jpeg_file(pInputDataStream, strFileName);
  60.  
  61. //Step 2: Use turbojpeglib to decompress filebuffer to uncompressedbuffer
  62. JpegEncoderDecoder bla(width, height, JPEG_QUALITY, COLOR_COMPONENTS);
  63. unsigned char*buffer = bla.decodeJpeg(pInputDataStream);
  64.  
  65. //Step 3: Convert to QImage
  66. QImage myDecodedJPEGimage(buffer, width, height, QImage::Format_RGB888) ;
  67.  
  68. //Step 4: Show in main window
  69. MainWindow w(myDecodedJPEGimage);
  70. w.show();
  71.  
  72. return a.exec();
  73. }
To copy to clipboard, switch view to plain text mode 



JpegEncoderDecoder.h:
Qt Code:
  1. #ifndef JPEGENCODERDECODER_H
  2. #define JPEGENCODERDECODER_H
  3.  
  4. class JpegEncoderDecoder
  5. {
  6. public:
  7. JpegEncoderDecoder(int width=1920, int height=1080, int JPEG_QUALITY=75, int COLOR_COMPONENTS=3);
  8.  
  9. void encodeJpeg(unsigned char * pBuffer);
  10. unsigned char * decodeJpeg(unsigned char * pcompressedImage);
  11.  
  12. private:
  13. int m_width;
  14. int m_height;
  15. const int m_JPEG_QUALITY ;
  16. const int m_COLOR_COMPONENTS;
  17. };
  18.  
  19. #endif // JPEGENCODERDECODER_H
To copy to clipboard, switch view to plain text mode 


Qt Code:
  1. // some includes....
  2.  
  3. JpegEncoderDecoder::JpegEncoderDecoder(int width, int height, int JPEG_QUALITY, int COLOR_COMPONENTS) :
  4. m_width (width)
  5. , m_height(height)
  6. , m_JPEG_QUALITY(JPEG_QUALITY)
  7. , m_COLOR_COMPONENTS(COLOR_COMPONENTS)
  8. {
  9. }
  10.  
  11. unsigned char * JpegEncoderDecoder::decodeJpeg(unsigned char *pcompressedImage)
  12. {
  13. long unsigned int _jpegSize; //!< _jpegSize from above
  14.  
  15.  
  16. int jpegSubsamp;
  17. unsigned char buffer[m_width*m_height*m_COLOR_COMPONENTS]; //!< will contain the decompressed image
  18.  
  19. tjhandle _jpegDecompressor = tjInitDecompress();
  20.  
  21. tjDecompressHeader2(_jpegDecompressor, pcompressedImage, _jpegSize, &m_width, &m_height, &jpegSubsamp);
  22.  
  23. tjDecompress2(_jpegDecompressor, pcompressedImage, _jpegSize, buffer, m_width, 0/*pitch*/, m_height, TJPF_RGB, TJFLAG_FASTDCT);
  24.  
  25. tjDestroy(_jpegDecompressor);
  26.  
  27. return buffer;
  28. }
To copy to clipboard, switch view to plain text mode 

I am omitting the definition of JpegEncoderDecoder::encodeJpeg, as well as mainwindow.h and mainwindow.cpp here.


This is compiling and also running fine, but unfortunately it does not show the image as expected. Instead I just see a black rectangle with some colored pixels in it. What am I doing wrong?