How to get rendered pixel value of IImage?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
verilogdc
Posts: 1
Joined: Thu Nov 12, 2015 4:37 am

How to get rendered pixel value of IImage?

Post by verilogdc »

I use createScreenShot to get a IImage* screenshot and then use writeImageToFile to save it as "screenshot.png".
However, I found that screenshot->getPixel will not get the same pixel value as "screenshot.png", it will be darker than "screenshot.png". I guess it is because screenshot->getPixel gets the pixel value before rendering. Is it true? How to get rendered pixel value of IImage?

Code: Select all

virtual IImage * createScreenShot (video::ECOLOR_FORMAT format=video::ECF_UNKNOWN, video::E_RENDER_TARGET target=video::ERT_FRAME_BUFFER)=0
virtual bool writeImageToFile (IImage *image, const io::path &filename, u32 param=0)=0
virtual SColor getPixel (u32 x, u32 y) const =0
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: How to get rendered pixel value of IImage?

Post by chronologicaldot »

I can't really tell what you're doing without some code. However, I will say this: You have to render the scene to the render target (which ends by calling IVideoDriver::endScene()), which I believe must precede calling createScreenShot(). I haven't used createScreenShot(), but I do read the API.

Instead of using createScreenShot(), you can create an IRenderTarget, which is created via addRenderTargetTexture():
http://irrlicht.sourceforge.net/docu/cl ... 054b0b8797
You can set it with setRenderTarget()
http://irrlicht.sourceforge.net/docu/cl ... 6bcaaeba9c
You can even just set a texture as the render target with setRenderTarget(), but (never tried this) I believe this means it isn't also rendered to the screen.
Note, you will have to convert the texture (ITexture) to an image (IImage) via createImage():
http://irrlicht.sourceforge.net/docu/cl ... 59c3dc8fae
And from there, you can access the pixels with getPixel().
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: How to get rendered pixel value of IImage?

Post by mongoose7 »

Why not just create a screenshot from the frame buffer? I have created screenshots and written them to files and they have been pixel perfect. However, I used the BMP format. The PNG format is quite complex, I think, so perhaps there is some compression there? Anyway, you certainly must wait until the scene is rendered before taking the screenshot, perhaps after the endScene() call.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How to get rendered pixel value of IImage?

Post by CuteAlien »

Hm, can't really think of a reason why png whould get darker on saving. Can you reproduce this in a short example which we can compile and test?
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: How to get rendered pixel value of IImage?

Post by hendu »

Could be the PNG's gamma correction? Use a program that ignores it, or remove the gamma chunk with PNG tools to test.

That is, the pixel value is intact, but your viewer might convert it upon opening.
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: How to get rendered pixel value of IImage?

Post by chronologicaldot »

I wonder... It may have to do with the color format of the screenshot itself, which is then converted to png.
@verilogdc - What is your video::ECOLOR_FORMAT set to?
Post Reply