PDA

View Full Version : how to use loadFromData in painter



sar_van81
22nd January 2007, 14:52
hi everyone,

i have a RGB values in an character array. can anyone say me to use loadFromData function in QPixmap inorder to convert the RGB datas to a pixmap...?
i did as follows:


void paintEvent(QPaintEvent *)
{
unsigned char buffer[20]={0,0,0,0,0,255,255,255,255,255,0,0,0,0,0};
QPixmap pixmap;
if(pixmap.loadFromData(buffer,20,0,QPixmap::Auto);
QPainter p(this);
p.drawPIxmap(0,0,pixmap,0,0,-1,-1);
}
. the code gets compiled without error. but nothing is coming on the screen.can anyone provide suggestions for this...?
are these steps correct...? if no can any correct me...?

thanks in advance,

saravanan

wysota
22nd January 2007, 15:25
You need more than just the data:

The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.

You have to provide a valid header for the format you wish to use.

In your situation it might be better to create an empty QImage and then use setPixel() or bits() to set the data.

sar_van81
23rd January 2007, 11:14
hi,

i tried with setpixel.but still not got anything. i tried as follows:

QImage img;
if(img.valid(10,10))
img.setPixel(10,10,200);
else printf("\n coordinate not valid..");

like this repeated for about 10 point from x=10,y=0 to x=20,y=0.
but when i compiled and executed the code, there were following errors:

coordinate not valid..
QImage::setPixel: x=10 out of range

.since i'm using depth as 32bit i gave 200 value to index_or_rgb. i gave 0 also.did not work out.

Also i tried the following:
QImage img;
img.setColor(19,qRgb(255,255,0));
*(img.scanLine(20)+20)=19;
.but same error came.

QImage::setColor: Index 19 out of range
QImage::scanLine: Index 20 out of range
Segmentation fault
.

Am i missing something...?Is this corect ...?can you say where am going wrong...?

sar_van81
23rd January 2007, 12:38
hi...

thank you for the suggestion of setPixel. i got it working.

i did as follows:
QImage image(x,y,32);
image.setAlphaBuffer( true );
for ( uint j = 0; j <x; j++ )
{
for ( uint i = 0; i <y; i++ )
{
QRgb pixel;
pixel = qRgb(0,0,0);
image.setPixel( i, j, pixel );
}
}
. this worked perfectly. earlier i did not create QImage and set the pixel value. now i created QImage and then set the pixel value.

saravanan.

sar_van81
23rd January 2007, 19:43
hi..

not over yet...i tried specifying the format in loadFromData as "BMP"as follows:

unsigned char buffer[20]={255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255};
QImage img(100,100,32);
img.setAlphaBuffer( true );

if(img.loadFromData(buffer,20,"BMP"))
printf("\n the image is loaded...\n");
else printf("\n image not loaded...\n");

but image is not loaded.. is this correct...?

any suggestions for this...?

wysota
23rd January 2007, 20:53
I don't see a valid BMP header here...

http://www.fastgraph.com/help/bmp_header_format.html

sar_van81
25th January 2007, 12:44
hi...

thanks for the response.but still a bit clarification.can you say me how to specify the BMP header in this ...?

wysota
25th January 2007, 13:11
You can't just pass the pixel data, you need to pass the data in a format of your choice. For example if you want to feed the data in BMP format, the data has to contain the BMP header. Check out the link I gave you, you have to construct a block of data according to the rules mentioned there. If you just want to set some pixels though, I suggest you use QImage::setPixel() instead.

sar_van81
25th January 2007, 13:56
hi..

yeah i saw the link . i had programmed like this: i'l open a bmp image, read the file header information and will allocate the temporary buffer with size given in the file header information. then feed this temproray buffer to the QImage::loadFromData ( const uchar * buf, uint len, const char * format = 0 ).

but still could not get it.i had attached the code.

is this correct way....?

wysota
25th January 2007, 14:50
If the file is a valid BMP file, then just create the QImage from the file...


QImage imag("/home/qtprograms/test8.bmp");

sar_van81
30th January 2007, 06:39
hi....

if we use like this directly then is there any need for loadFromData ... i.e. is this following correct..?

QImage img("/home/qtprograms/text8.bmp");
QPainter p;
p.drawImage ( QPoint(0,0),img,0);

..
sorry for the late reply. went out-of-station.

wysota
30th January 2007, 09:02
It is correct, you don't need loadFromData at all.

sar_van81
30th January 2007, 09:36
hi..

this working fine. but my requirement if i have a file which contains only RGB data, i need to construct an image from that and need to display in a widget.

is there any suggestions for this...?

wysota
30th January 2007, 13:41
It contains data in some format. If it conforms to what the QImage object requires, just use QImage::bits() and assign the data there.

sar_van81
30th January 2007, 17:13
hi...

i'm working in qt-3.3.5. i think there is no function as you said. also in the function loadFromData i gave the buffer size and specified the format as BMP. but still it did not work.

i will attach the code.


can you say me where i went wrong...?

wysota
30th January 2007, 17:19
Let me put it this way - loadFromData is of not much use for you.

uchar * QImage::bits () const
Returns a pointer to the first pixel data. This is equivalent to scanLine(0).
See also numBytes(), scanLine(), and jumpTable().
Example: opengl/texture/gltexobj.cpp.

You can assign data to the address pointed by the result of this method if it conforms to QImage's format specified during construction of the object.

sar_van81
30th January 2007, 18:03
hi...

sorry to ask you the same thing repeatedly.can you say me whats the use of loadFrom Data...?

its given as

"QImage::loadFromData ( const uchar * buf, uint len, const char * format = 0 ):
Loads an image from the first len bytes of binary data in buf. Returns TRUE if the image was successfully loaded; otherwise returns FALSE.

If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader reads a few bytes from the header to guess the file format. ".

so which means that if i have a buffer of RGB datas and if i specify the format as "BMP" or any format it will construct an image.is my undersanding correct ...?


Also i think the function you said is similar to QImage::setPixel wherein we can set each pixel's value usong RGB.
or is there any other information i'm missing...?

my ultimate requirement is that i have two applications.one application collects an RGB data from an external interface and stores in a buffer. another applicaiton will read the data from the buffer and should display it on the widget with a specified format. i'm working on the second application to display RGB data from buffer. for this only i'm insisting on loadFromData.

is there any suggestions or solutions for displaying image from RGB datas...?

wysota
31st January 2007, 00:00
sorry to ask you the same thing repeatedly.can you say me whats the use of loadFrom Data...?

To load an image from a block of data which is not contained in a file on disk. QImage constructors take a filename as an argument. loadFromData() is just another way of constructing a QImage. It's exactly equivalent to the following constructor:

QImage ( const uchar * data, int width, int height, Format format )


so which means that if i have a buffer of RGB datas and if i specify the format as "BMP" or any format it will construct an image.is my undersanding correct ...?
No. You have to have the data in the format specified by the format you pass - it has to be a proper BMP image in this case. Maybe this example will help:

QFile bmpfile("someimage.bmp");
bmpfile.open(QFile::ReadOnly);
QByteArray contents = bmpfile.readAll();

QImage image;
image.loadFromData(contents, "BMP");

As you see you need something more than an actual pixel data - you need everything which is contained in a BMP file (the header containing width, height, colour depth and a colour map in case of indexed images in addition to the pixel data in a proper format (upside down BGR in case of a BMP file) ).


Also i think the function you said is similar to QImage::setPixel wherein we can set each pixel's value usong RGB.

No. setPixel sets a pixel according to the format of an already existing QImage object, it's width and height whereas loadFromData() creates an image from scratch (reading its dimensions and colour depth from the blob).


for this only i'm insisting on loadFromData.
Then don't as it's of no use to you. Use QImage::bits() instead.


is there any suggestions or solutions for displaying image from RGB datas...?
Use QLabel to display your image.

Understand this - "RGB data" doesn't mean anything by itself. The machine has to know how many bytes correspond to a single pixel, what is the order of colour components, the length of the scanline (width of the picture) and what colours should be associated with colour numbers for indexed images. All this data has to be provided in addition to the actual blob of data containing the pixels themselves (which you call "RGB data"). So summarising loadFromData() doesn't only take the blob ("RGB data") itself as an input but also all those other values I mentioned and they have to be provided according to some format understood by QImage.

sar_van81
31st January 2007, 05:09
hi...

thank you very much for the explanation. i did as you said and got the output. now its working perfectly.