PDA

View Full Version : cannot show image contents



saman_artorious
11th September 2013, 09:48
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.



fileContent.clear();

QFile file("://image.jpg");

if(!file.open(QIODevice::ReadOnly)) {

QMessageBox::information(0, "error", file.errorString());
}

qDebug() << file.bytesAvailable(); //119251 which is equal to file size

QTextStream in(&file);

QString wholeFile = in.readAll();

fileContent.append(wholeFile);

qDebug() << fileContent.length(); //212887 -> not equal to to file size!

// while(!in.atEnd()) {

// QString line = in.readLine();

// fileContent.append(line);
// }

file.close();

QPixmap px(fileContent.constData());

ui->label->setPixmap(px);

wysota
11th September 2013, 09:52
The image is not text.

saman_artorious
11th September 2013, 09:58
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!

wysota
11th September 2013, 10:02
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:


QPixmap px("://image.jpg");
ui->label->setPixmap(px);

saman_artorious
11th September 2013, 10:41
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?



void MainWindow::CompressImage()
{
_jpegSize = 0;

_compressedImage = NULL; //!< Memory is allocated by tjCompress2 if _jpegSize == 0

unsigned char *buffer = new unsigned char[_width*_height*COLOR_COMPONENTS]; //!< Contains the uncompressed image

buffer = (unsigned char*) fileContent.constData();

tjhandle _jpegCompressor = tjInitCompress();

tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB,
&_compressedImage, &_jpegSize, TJSAMP_444, JPEG_QUALITY,TJFLAG_FASTDCT);

tjDestroy(_jpegCompressor);

qDebug() << _jpegSize;

}

void MainWindow::DecompressImage()
{
// int _jpegSize; //!< _jpegSize from above
// unsigned char* _compressedImage; //!< _compressedImage from above

int jpegSubsamp;

unsigned char* buffer;

buffer = new unsigned char[_width*_height*COLOR_COMPONENTS]; //!< will contain the decompressed image

tjhandle _jpegDecompressor = tjInitDecompress();

tjDecompressHeader2(_jpegDecompressor, _compressedImage, _jpegSize, &_width, &_height, &jpegSubsamp);

tjDecompress2(_jpegDecompressor, _compressedImage, _jpegSize, buffer, _width, 0/*pitch*/, _height, TJPF_RGB, TJFLAG_FASTDCT);

tjDestroy(_jpegDecompressor);

qDebug() << _jpegSize;

QPixmap px((const char*)buffer);

ui->label->setPixmap(px);
}

wysota
11th September 2013, 10:55
QImage img(...);
memcpy(img.bits(), buffer, ...);
QPixmap px = QPixmap::fromImage(img);
ui->label->setPixmap(px);

saman_artorious
11th September 2013, 13:00
I used the method you noted, however I cannot recover the image from the buffer in a simple test.



//======== Load buffer from image
unsigned char buffer[_width*_height*COLOR_COMPONENTS];

QImage image;

image.load("://image.jpg", "JPEG");

memcpy(buffer, image.bits(), _width*_height*COLOR_COMPONENTS);


//========= Load image from buffer
QImage img;

img.loadFromData((const char*)buffer);

QPixmap px = QPixmap::fromImage(img);

ui->label->setPixmap(px);


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

wysota
11th September 2013, 13:31
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.

saman_artorious
12th September 2013, 07:36
thanks for the hint. I followed the doc. however, there is still something wrong with the code:



QImage(_width, _height, QImage::Format_RGB32);

image.load("://image.jpg", "JPEG");

memcpy(buffer, image.bits(), 400*300*3);



QImage img(400, 300, QImage::Format_RGB888);

img.loadFromData((const uchar*)buffer, sizeof(buffer)/sizeof(char), "JPG");

QPixmap px = QPixmap::fromImage(img);

ui->label->setPixmap(px);

wysota
12th September 2013, 07:48
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.

ChrisW67
12th September 2013, 08:15
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.