Write an Image from raw pixel data

Hey Guys :slight_smile:

i want to write an Image from my ethernet stream.
i get a pointer to the raw pixel data and now i want to write this data into a picture file and save it.
have qnx some functions for my problem?
i found that in the “pxImage.h” are some functions for that, is it right?
now how can i make it ;)

i hope somebody could give me a hint :slight_smile:

with best regards nudels :slight_smile:

As far as I know there are only fonctions to load images (png, jpg, bmp etc) but for some reasons not to save them.

Check this out:
error = PxWriteImage( fname, image, NULL,PX_IMAGE_JPG,0 );

where:
char *fname, file name prefixed with the absolute path.
PhImage_t *image,
PX_IMAGE_JPG —> if you need the file to be saved in JPG format
PX_IMAGE_BMP for BMP format.
PNP does not work.
I use it in my applications successfully. It is not documented but works fine.
Your application needs to be linked with libphexlib,
#add libphexlib for PxWriteImage in your common.mk
LIBS+=phexlib

thanks for replies :slight_smile:

@koko:
i have seen this function, but my problem is, that i have an const unsigned int Pointer and not a PhImage_t Pointer and i cant cast from const unsigned to PhImage_t.
Or is there any function or structdata that i have to reference with the const unsigned int Pointer?

EDIT 13.09.2011:

okay i tried with PxWriteImage(). My code is the following:

int Draw::saveImg(const unsigned int* img)
{
	std::cout << "Draw::saveImg()" << std::endl;
	m_img = img;

	int error;
	if((error = PxWriteImage("/home/write.bmp",(PhImage_t*) m_img, NULL ,PX_IMAGE_BMP,0)) == 1)
	{
		perror("ERROR in PxWriteImage");
		exit(1);
	}
	std::cout << "finished" << std::endl;

	return 0;
}

But i get the error, that my core is dumped (“Memory fault (core dumped)”)
And sometimes i get the error: “PxWriteImage: No such Process”.

Thanks for your interest to help me :slight_smile:

with best regards

Hi nudels,
Your problem is that you try to cast to PhImage_t something what is not.
if((error = PxWriteImage("/home/write.bmp",(PhImage_t*) m_img, NULL ,PX_IMAGE_BMP,0)) == 1)
{
perror(“ERROR in PxWriteImage”);
exit(1);
}

You should read QNX documentation( Chapter Raw Drawing and Animation ) or look at /usr/include/photon/Pg.h for PhImage_t.
You will see that the structure from type PtImage_t has a field image( char *image ). This is the pointer which has to point to your frame.
I recommend you to read carefully the mentioned chapter - there are some tricks you need to understand before using the function.
Look the section for Images into this chapter. Pay attention to the format of your frame - incoming and the one you need to draw.
Cheers

hi koko,

also i have tried it like u described.
my pics are 16bit bmp files.

int Draw::saveImg(const unsigned int* img)
{
	std::cout << "Draw::saveImg()" << std::endl;
	m_img = img;

	PhImage_t my;

	memset(&my, 0, sizeof(PhImage_t));

	my.image = (char*) m_img;
	my.type = 33;
	my.size.h = 240;
	my.size.w = 320;
	my.alpha = 0;
	my.bpl = 5120;
	my.colors = 0;


	int error;
	if((error = PxWriteImage("/home/write.bmp", &my, NULL ,PX_IMAGE_BMP,0)) == 1)
	{
		perror("ERROR in PxWriteImage");
		exit(1);
	}
	std::cout << "finished()" << std::endl;

	return 0;
}

but i get the same error. “Memory fault (core dumped)”.

it creates a file, but the filesize is 0 bytes and i cant open it :confused:

EDIT:

okay now it works ;)
it was a little mistake ^^

but now i have the problem that the bmp picture has no color :confused: even when i set “my.color = 32768”.

Congratulations,
You are step away from the solution.
Where did you get type=33?
I believe your incoming frame format is RGB565, and not RGB888( which is 33 ).
Use defines instead —> type=Pg_IMAGE_DIRECT_565. They are all defined in Pg.h.
Cheers,
koko

huhu :slight_smile:

okay now i have a bitmpa with color.
i had to set the type to Pg_IMAGE_DIRECT_8888.
The other one didn’t worked.
Now i have an Bitmpa, but its some kind of distorted :confused:
i attach the picture and maybe has somebody an idea how to fix it :slight_smile:

When you get a distorted image, it probably has to do with the difference between bpl (bytes per line) and the pixel width.

Let’s say you try to load pixel data in 888 format into an existing image. The first pixel goes in the first three bytes, the next pixel in the next three bytes, and so forth. Let’s say you have a width of 100. So the first 100 pixels goes in the first 300 bytes.
But where does the first pixel in the 2nd row go? It’s not necessarily in the next byte. You have to look at the image->bpl value.