Results 1 to 11 of 11

Thread: cannot show image contents

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default cannot show image contents

    I can setPixmap the contents of an image file opened via Open. file.BytesAvailable gives me a different value than the number of characters I read from file. Finally, nothing is displayed in the label background.

    Qt Code:
    1. fileContent.clear();
    2.  
    3. QFile file("://image.jpg");
    4.  
    5. if(!file.open(QIODevice::ReadOnly)) {
    6.  
    7. QMessageBox::information(0, "error", file.errorString());
    8. }
    9.  
    10. qDebug() << file.bytesAvailable(); //119251 which is equal to file size
    11.  
    12. QTextStream in(&file);
    13.  
    14. QString wholeFile = in.readAll();
    15.  
    16. fileContent.append(wholeFile);
    17.  
    18. qDebug() << fileContent.length(); //212887 -> not equal to to file size!
    19.  
    20. // while(!in.atEnd()) {
    21.  
    22. // QString line = in.readLine();
    23.  
    24. // fileContent.append(line);
    25. // }
    26.  
    27. file.close();
    28.  
    29. QPixmap px(fileContent.constData());
    30.  
    31. ui->label->setPixmap(px);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: cannot show image contents

    The image is not text.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: cannot show image contents

    Quote Originally Posted by wysota View Post
    The image is not text.
    but I am using its character contents and set those chars to setpixmap which is defined as a parameter for it!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: cannot show image contents

    Quote Originally Posted by saman_artorious View Post
    but I am using its character contents and set those chars to setpixmap which is defined as a parameter for it!
    But the image is not text. If you treat it as text, it goes through a number of conversions which modify file contents.

    Your code can be substituted with:

    Qt Code:
    1. QPixmap px("://image.jpg");
    2. ui->label->setPixmap(px);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: cannot show image contents

    I don't think I can use your option. I am com/decom-pressing an image with libjpeg-turbo. I received the compressed and decompressed image as a character buffer. Finally, I want to check if the decompressed image is shown the same as the original image. So, that's why I was trying to pixmap an image using characters.

    What is your opinion?

    Qt Code:
    1. void MainWindow::CompressImage()
    2. {
    3. _jpegSize = 0;
    4.  
    5. _compressedImage = NULL; //!< Memory is allocated by tjCompress2 if _jpegSize == 0
    6.  
    7. unsigned char *buffer = new unsigned char[_width*_height*COLOR_COMPONENTS]; //!< Contains the uncompressed image
    8.  
    9. buffer = (unsigned char*) fileContent.constData();
    10.  
    11. tjhandle _jpegCompressor = tjInitCompress();
    12.  
    13. tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB,
    14. &_compressedImage, &_jpegSize, TJSAMP_444, JPEG_QUALITY,TJFLAG_FASTDCT);
    15.  
    16. tjDestroy(_jpegCompressor);
    17.  
    18. qDebug() << _jpegSize;
    19.  
    20. }
    21.  
    22. void MainWindow::DecompressImage()
    23. {
    24. // int _jpegSize; //!< _jpegSize from above
    25. // unsigned char* _compressedImage; //!< _compressedImage from above
    26.  
    27. int jpegSubsamp;
    28.  
    29. unsigned char* buffer;
    30.  
    31. buffer = new unsigned char[_width*_height*COLOR_COMPONENTS]; //!< will contain the decompressed image
    32.  
    33. tjhandle _jpegDecompressor = tjInitDecompress();
    34.  
    35. tjDecompressHeader2(_jpegDecompressor, _compressedImage, _jpegSize, &_width, &_height, &jpegSubsamp);
    36.  
    37. tjDecompress2(_jpegDecompressor, _compressedImage, _jpegSize, buffer, _width, 0/*pitch*/, _height, TJPF_RGB, TJFLAG_FASTDCT);
    38.  
    39. tjDestroy(_jpegDecompressor);
    40.  
    41. qDebug() << _jpegSize;
    42.  
    43. QPixmap px((const char*)buffer);
    44.  
    45. ui->label->setPixmap(px);
    46. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: cannot show image contents

    Qt Code:
    1. QImage img(...);
    2. memcpy(img.bits(), buffer, ...);
    3. QPixmap px = QPixmap::fromImage(img);
    4. ui->label->setPixmap(px);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: cannot show image contents

    I used the method you noted, however I cannot recover the image from the buffer in a simple test.

    Qt Code:
    1. //======== Load buffer from image
    2. unsigned char buffer[_width*_height*COLOR_COMPONENTS];
    3.  
    4. QImage image;
    5.  
    6. image.load("://image.jpg", "JPEG");
    7.  
    8. memcpy(buffer, image.bits(), _width*_height*COLOR_COMPONENTS);
    9.  
    10.  
    11. //========= Load image from buffer
    12. QImage img;
    13.  
    14. img.loadFromData((const char*)buffer);
    15.  
    16. QPixmap px = QPixmap::fromImage(img);
    17.  
    18. ui->label->setPixmap(px);
    To copy to clipboard, switch view to plain text mode 

    I took _width & _height 1024 * 768 and the other arguments as RGB 3;

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: cannot show image contents

    Your constructor is invalid. You have to initialize it with the width, height and format of the data. Also loadFromData() does not do what you think it does. RTFM.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    saman_artorious (12th September 2013)

  10. #9
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: cannot show image contents

    thanks for the hint. I followed the doc. however, there is still something wrong with the code:

    Qt Code:
    1. QImage(_width, _height, QImage::Format_RGB32);
    2.  
    3. image.load("://image.jpg", "JPEG");
    4.  
    5. memcpy(buffer, image.bits(), 400*300*3);
    6.  
    7.  
    8.  
    9. QImage img(400, 300, QImage::Format_RGB888);
    10.  
    11. img.loadFromData((const uchar*)buffer, sizeof(buffer)/sizeof(char), "JPG");
    12.  
    13. QPixmap px = QPixmap::fromImage(img);
    14.  
    15. ui->label->setPixmap(px);
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: cannot show image contents

    Because you still need to RTFM. As I said loadFromData does not do what you want. Start reading and thinking instead of blindly trying things, for God's sake... It seems as if you had totally no idea what you were doing.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: cannot show image contents

    I am com/decom-pressing an image with libjpeg-turbo. I received the compressed and decompressed image as a character buffer. Finally, I want to check if the decompressed image is shown the same as the original image. So, that's why I was trying to pixmap an image using characters.

    What is your opinion?
    If you are compressing an image using JPEG and decompressing the result then what you have is not equal to the original. JPEG is a lossy compression algorithm in all typical uses, and the atypical lossless JPEG form is, as they advertise, lossless.

    Now to your code. Lines 1 through 5: no idea what you think you are doing here. Asking Qt to load a "JPEG" [sic] file and copying a fixed number of bytes of it: I can only hope the source image and the buffer are big enough or you are heading to crash city.

    You tell us you are getting a decompressed image from a third-party library (not related to the earlier lines). You have a buffer that contains raw pixel data for that image, 3-bytes-per-pixel if TJPF_RGB is the format. It is not a JPEG image any more, so trying to treat it as such at line 11 makes little sense. Wysota already told you how to copy the raw pixel data into the internal buffer of a correctly initialised QImage. You must at least idiot check that the byteCount() of the QImage and the size of the raw buffer are equal.

Similar Threads

  1. Making a QDropBox show a list contents
    By alitoh in forum Newbie
    Replies: 1
    Last Post: 26th April 2011, 13:23
  2. How to show moving image
    By nrabara in forum Newbie
    Replies: 1
    Last Post: 30th December 2009, 08:24
  3. QAbstractItemView - Show all contents all the time...
    By youkai in forum Qt Programming
    Replies: 5
    Last Post: 31st August 2008, 16:54
  4. Replies: 3
    Last Post: 30th May 2008, 07:37
  5. Show Image on a QLabel
    By ^NyAw^ in forum Newbie
    Replies: 11
    Last Post: 15th April 2008, 15:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.