Save QImage from BYTE buffer segfaults ?
-
And one byte per pixel ?
-
wrote on 5 Jul 2020, 23:45 last edited by
@SGaist said in Save QImage from BYTE buffer segfaults ?:
And one byte per pixel ?
That is correct yes.
-
Can you get the stack trace of the crash ?
-
wrote on 6 Jul 2020, 21:26 last edited by
@SGaist said in Save QImage from BYTE buffer segfaults ?:
Can you get the stack trace of the crash ?
I'm testing the application on another machine using the executable. I don't think I'd be able to get it without QT or another IDE right ?
-
Windows machine ?
-
wrote on 7 Jul 2020, 21:59 last edited by
@SGaist said in Save QImage from BYTE buffer segfaults ?:
Windows machine ?
Yes it is. Windows 7 and also Windows 10.
-
wrote on 8 Jul 2020, 13:44 last edited by
try:
BYTE *buf = new BYTE[imWidth * imHeight]; -
wrote on 8 Jul 2020, 16:26 last edited by
@mranger90 said in Save QImage from BYTE buffer segfaults ?:
try:
BYTE *buf = new BYTE[imWidth * imHeight];Hi, it is not crashing anymore, however the image seems to be coming out completely black. I will confirm tomorrow if the image is supposed to be completely black or not...
-
wrote on 8 Jul 2020, 16:32 last edited by
From the documentation
https://doc.qt.io/qt-5/qimage.html#QImage-4
data must be32-bit aligned
, and each scanline of data in the image must also be32-bit aligned
.So (as far as I understand) that is not enough to have memory buffer aligned to 32 bit address, also each line have to be aligned to 32 bit address. So if you have image that is 260x300 pixels, a whole line will take 288 bytes. So for whole image you should have 288*300 bytes which is more than you have provided.
-
From the documentation
https://doc.qt.io/qt-5/qimage.html#QImage-4
data must be32-bit aligned
, and each scanline of data in the image must also be32-bit aligned
.So (as far as I understand) that is not enough to have memory buffer aligned to 32 bit address, also each line have to be aligned to 32 bit address. So if you have image that is 260x300 pixels, a whole line will take 288 bytes. So for whole image you should have 288*300 bytes which is more than you have provided.
wrote on 8 Jul 2020, 16:48 last edited by@Jarek-B said in Save QImage from BYTE buffer segfaults ?:
From the documentation
https://doc.qt.io/qt-5/qimage.html#QImage-4
data must be32-bit aligned
, and each scanline of data in the image must also be32-bit aligned
.So (as far as I understand) that is not enough to have memory buffer aligned to 32 bit address, also each line have to be aligned to 32 bit address. So if you have image that is 260x300 pixels, a whole line will take 288 bytes. So for whole image you should have 288*300 bytes which is more than you have provided.
Hi, interesting...I have not seen any examples like that. How are you arriving at 288 bytes ? Are you suggesting I make the buffer 288x300 bytes instead ?
-
@Jarek-B said in Save QImage from BYTE buffer segfaults ?:
From the documentation
https://doc.qt.io/qt-5/qimage.html#QImage-4
data must be32-bit aligned
, and each scanline of data in the image must also be32-bit aligned
.So (as far as I understand) that is not enough to have memory buffer aligned to 32 bit address, also each line have to be aligned to 32 bit address. So if you have image that is 260x300 pixels, a whole line will take 288 bytes. So for whole image you should have 288*300 bytes which is more than you have provided.
Hi, interesting...I have not seen any examples like that. How are you arriving at 288 bytes ? Are you suggesting I make the buffer 288x300 bytes instead ?
wrote on 8 Jul 2020, 17:00 last edited by mranger90 7 Aug 2020, 17:01@R-P-H Of course it's black, the data never gets initialized.
Also, the reason why it doesn't crash is because:
BYTE *buf = new BYTE(x * y); // allocates a single byte, initialized to the value of (x * y)
BYTE *buf = new BYTE[x * y]; // allocates an array of bytes whose size is x * y -
@R-P-H Of course it's black, the data never gets initialized.
Also, the reason why it doesn't crash is because:
BYTE *buf = new BYTE(x * y); // allocates a single byte, initialized to the value of (x * y)
BYTE *buf = new BYTE[x * y]; // allocates an array of bytes whose size is x * ywrote on 8 Jul 2020, 17:08 last edited by@mranger90 said in Save QImage from BYTE buffer segfaults ?:
@R-P-H Of course it's black, the data never gets initialized.
Also, the reason why it doesn't crash is because:
BYTE *buf = new BYTE(x * y); // allocates a single byte, initialized to the value of (x * y)
BYTE *buf = new BYTE[x * y]; // allocates an array of bytes whose size is x * yI don't quite understand what you're saying to be honest. Where are you saying the issue is ?
-
@mranger90 said in Save QImage from BYTE buffer segfaults ?:
@R-P-H Of course it's black, the data never gets initialized.
Also, the reason why it doesn't crash is because:
BYTE *buf = new BYTE(x * y); // allocates a single byte, initialized to the value of (x * y)
BYTE *buf = new BYTE[x * y]; // allocates an array of bytes whose size is x * yI don't quite understand what you're saying to be honest. Where are you saying the issue is ?
wrote on 8 Jul 2020, 17:27 last edited by@R-P-H here is the amended code
// allocates a buffer of imWidth * imHeight bytes, which depending on the compiler // settings will either be initialized to zero, or may contain garbage BYTE *buf = new BYTE[imWidth * imHeight]; // creates a QImage based on the data in the buffer, QImage img(buf, imWidth, imHeight, QImage::Format_Grayscale8); // saves the data img.save("image.bmp", "BMP"); delete [] buf;
The point is that you never put actual image data into the buffer.
-
@R-P-H here is the amended code
// allocates a buffer of imWidth * imHeight bytes, which depending on the compiler // settings will either be initialized to zero, or may contain garbage BYTE *buf = new BYTE[imWidth * imHeight]; // creates a QImage based on the data in the buffer, QImage img(buf, imWidth, imHeight, QImage::Format_Grayscale8); // saves the data img.save("image.bmp", "BMP"); delete [] buf;
The point is that you never put actual image data into the buffer.
wrote on 8 Jul 2020, 17:56 last edited by R-P-H 7 Aug 2020, 17:59@mranger90 said in Save QImage from BYTE buffer segfaults ?:
@R-P-H here is the amended code
// allocates a buffer of imWidth * imHeight bytes, which depending on the compiler // settings will either be initialized to zero, or may contain garbage BYTE *buf = new BYTE[imWidth * imHeight]; // creates a QImage based on the data in the buffer, QImage img(buf, imWidth, imHeight, QImage::Format_Grayscale8); // saves the data img.save("image.bmp", "BMP"); delete [] buf;
The point is that you never put actual image data into the buffer.
This is already what I did according to your first post. The buffer IS populated, I just didn't show that API call. I will modify my original post to make this clear...
So the result is a completely black image. This may be normal but I will have to check with the API provider.
EDIT: I have edited my original post.
-
Is that some private library you can't share ? Or would it be possible to have more information about that API ?
-
Is that some private library you can't share ? Or would it be possible to have more information about that API ?
wrote on 9 Jul 2020, 09:46 last edited by@SGaist said in Save QImage from BYTE buffer segfaults ?:
Is that some private library you can't share ? Or would it be possible to have more information about that API ?
Your assumption is correct. There really aren't any additional details on using this API function. It simply takes as input a reference to the buffer and a unique id of the capturing device. It then populates the buffer in place. The example in the documentation initializes the buffer the same way as shown in my original post.
If we are sure the error is on the API side, then they will need to fix that.
I made the assumption that the error was in saving the image to disk.
-
Then are you sure their BYTE type is not something custom ? Do they also show how to access the data ?
Is it the Windows BYTE type ?
-
@Jarek-B said in Save QImage from BYTE buffer segfaults ?:
From the documentation
https://doc.qt.io/qt-5/qimage.html#QImage-4
data must be32-bit aligned
, and each scanline of data in the image must also be32-bit aligned
.So (as far as I understand) that is not enough to have memory buffer aligned to 32 bit address, also each line have to be aligned to 32 bit address. So if you have image that is 260x300 pixels, a whole line will take 288 bytes. So for whole image you should have 288*300 bytes which is more than you have provided.
Hi, interesting...I have not seen any examples like that. How are you arriving at 288 bytes ? Are you suggesting I make the buffer 288x300 bytes instead ?
-
wrote on 9 Jul 2020, 15:37 last edited by
@Jarek-B said in Save QImage from BYTE buffer segfaults ?:
Just round up space that you need for one line
280 / 32 = 8.75
9 * 32 = 288I tried:
BYTE *buf = new BYTE(288 * 300);
Result is exactly the same. Still crashes on occasion. -
Then are you sure their BYTE type is not something custom ? Do they also show how to access the data ?
Is it the Windows BYTE type ?
wrote on 9 Jul 2020, 15:40 last edited by@SGaist said in Save QImage from BYTE buffer segfaults ?:
Then are you sure their BYTE type is not something custom ? Do they also show how to access the data ?
Is it the Windows BYTE type ?
They do not show how to access the data afterwards.
It's defined as
typedef unsigned char BYTE;
fromwindows.h
.
21/43