C++ Image Manipulation - c++

So I have made this program for a game and need help with making it a bit more automatic.
The program takes in an image and then displays it. I'm doing this over textures in OpenGL. When I take the screenshot of the game it is usually something about 700x400. I input the height and width into my program, resize the image to 1024x1024 (making it a POT texture for better compatibility) by adding blank space (the original image stays at the top left corner and goes all the way to (700,400) and the rest is just blank; does anyone know the term for this?) and then load it into my program and adjust the corners so only the part from (0,0) to (700,400) is shown.
That's how I handle the display of the image. Now, I would like to make this automatic. So I'd take a 700x400 picture, pass it to the program which would get the image's width and height (700x400), resize it to 1024x1024 by adding blank space and then load it.
So does anyone know a C++ library capable of doing this? I would still be taking the screenshot manually though.
I am using the Simple OpenGL Image Library (SOIL) for loading the picture (.bmp) and converting it into a texture.
Thanks!

You don't really have to resize by adding blank space to display image properly. In fact, it's really unefficient way to do it, especially because you store images in .bmp format.
SOIL is able to automatically add the blank space when loading textures - maybe just try to load the file as-is, without doing any operations.
From SOIL Documentation:
Can automatically rescale the image to the next largest power-of-two
size
Can load rectangluar textures for GUI elements or splash screens
(requires GL_ARB/EXT/NV_texture_rectangle)
Anyway, you don't have to use texture to display pixels on the screen. I presume you aren't using shaders for rendering - if it all goes through fixed pipeline, there's glDrawPixels function, which will be much simpler. Just remember to change your SOIL call to SOIL_load_image.

Related

How to check if image should be flipped vertically before loading using stb_image

I'm writing a project using OpenGL, and loading the textures using stb_image.
Some of the textures are loaded flipped upside down (regarding the y-axis) so I use
"stbi flip image vertically on load" to load them properly.
The problem is that some of the textures I load require flipping, and some not,
But of course my code flipps them all.
how can I check (before loading, or at least before flipping) whether or not to flip the
image?
Short answer: always flip when loading an image from stb_image to an OpenGL texture. Longer answer: you can't know whether a user wants to flip the image themselves. As it was posed, I think your question is answered by the question Kai Burjack linked you to (Should I vertically flip the lines of an image loaded with stb_image to use in OpenGL?) because it clarifies the correct use of this feature of stb_image.
If you are going straight from an image file to an OpenGL texture, then you should always flip during import if you want the "up" of the imported texture to match what users see in their art programs. However, if you want to give users the option to load images upside down independent of how the image looks in the art program, you can totally do that, too. The catch is that the user has to tell you. There's no way to know what the user wants, and IMO artists who want their images upside-down are likely to just make them that way in their art programs anyways.

How to manipulate large images in Firemonkey?

Does Firemonkey have an image manipulation library? Right now I'm doing everything with Canvases and it sucks.
I have a program which currently loads the image into a TBitmap, performs its operations, and then saves the TBitmap object out to a file.
The TBitmap interface however is not ideal. Among the problems...
It silently scales down images which are larger than 8k pixels in any dimension, losing resolution on large .png files
It scales the image whenever there are clipping issues with DrawBitmap which is undesirable (I'd much rather it clip the output or just crash)
Rotating bitmaps is very hard to predict the output of and may scale the image.
I want a better option than canvas methods for image manipulation. I see there are units under Vcl.Imaging, but I can't find anything similar under the FMX units.
The operations I'm really looking for are just drawing lines, text, zooming, rotating, blitting to the display, saving, and loading.

Should I vertically flip the lines of an image loaded with stb_image to use in OpenGL?

I'm working on an OpenGL-powered 2d engine.
I'm using stb_image to load image data so I can create OpenGL textures. I know that the UV origin for OpenGL is bottom-left and I also intend to work in that space for my screen-space 2d vertices i.e. I'm using glm::ortho( 0, width, 0, height, -1, 1 ), not inverting 0 and height.
You probably guessed it, my texturing is vertically flipped but I'm 100% sure that my UV are specified correctly.
So: is this caused by stbi_load's storage of pixel data? I'm currently loading PNG files only so I don't know if it would cause this problem if I was using another file format. Would it? (I can't test right now, I'm not at home).
I really want to keep the screen coords in the "standard" OpenGL space... I know I could just invert the orthogonal projection to fix it but I would really rather not.
I can see two sane options:
1- If this is caused by stbi_load storage of pixel data, I could invert it at loading time. I'm a little worried about that for performance reason and because I'm using texture arrays (glTexture3d) for sprite animations meaning I would need to invert texture tiles individually which seems painful and not a general solution.
2- I could use a texture coordinate transformation to vertically flip the UVs on the GPU (in my GLSL shaders).
A possible 3rd option would be to use glPixelStore to specify the input data... but I can't find a way to tell it that the incoming pixels are vertically flipped.
What are your recommendations for handling my problem? I figured I can't be the only one using stbi_load + OpenGL and having that problem.
Finally, my target platforms are PC, Android and iOS :)
EDIT: I answered my own question... see below.
I know this question's pretty old, but it's one of the first results on google when trying to solve this problem, so I thought I'd offer an updated solution.
Sometime after this question was originally asked stb_image.h added a function called "stbi_set_flip_vertically_on_load", simply passing true to this function will cause it to output images the way OpenGL expects - thus removing the need for manual flipping/texture-coordinate flipping.
Also, for those who don't know where to get the latest version, for whatever reason, you can find it at github being actively worked on:
https://github.com/nothings/stb
It's also worth noting that in stb_image's current implementation they flip the image pixel-by-pixel, which isn't exactly performant. This may change at a later date as they've already flagged it for optimsation. Edit: It appears that they've swapped to memcpy, which should be a good bit faster.
Ok, I will answer my own question... I went thru the documentation for both libs (stb_image and OpenGL).
Here are the appropriate bits with reference:
glTexImage2D says the following about the data pointer parameter: "The first element corresponds to the lower left corner of the texture image. Subsequent elements progress left-to-right through the remaining texels in the lowest row of the texture image, and then in successively higher rows of the texture image. The final element corresponds to the upper right corner of the texture image." From http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml
The stb_image lib says this about the loaded image pixel: "The return value from an image loader is an 'unsigned char *' which points to the pixel data. The pixel data consists of *y scanlines of *x pixels, with each pixel consisting of N interleaved 8-bit components; the first pixel pointed to is top-left-most in the image." From http://nothings.org/stb_image.c‎
So, the issue is related the pixel storage difference between the image loading lib and OpenGL. It wouldn't matter if I loaded other file formats than PNG because stb_image returns the same data pointer for all formats it loads.
So I decided I'll just swap in place the pixel data returned by stb_image in my OglTextureFactory. This way, I keep my approach platform-independent. If load time becomes an issue down the road, I'll remove the flipping at load time and do something on the GPU instead.
Hope this helps someone else in the future.
Yes, you should. This can be easily accomplished by simply calling this STBI function before loading the image:
stbi_set_flip_vertically_on_load(true);
Since this is a matter of opposite assumptions between image libraries in general and OpenGL, Id say the best way is to manipulate the vertical UV-coord. This takes minimal effort and is always relevant when loading images using any image library and passing it to OpenGL.
Either feed tex-coords with 1.0f-uv.y in vertex-population OR reverse in shader.
fcol = texture2D( tex, vec2(uv.x,1.-uv.y) );

getting a subimage of a CImage

I've recently started learning how to use MFC in Visual Studio. My program uses many small 32x32 pixel images that for convenience are put together in one large .png. I've figured out how to load the big image into a CImage and display it on a CClientDC, but I can't find anything that helps me split the image into those smaller 32x32 images.
Alternatively, I can get the pixel data for the big image using DevIL, but I can't find anything that constructs a CImage from pixel data.
As I've only started learning MFC a few days ago, I don't know the API too well and I'm probably missing something obvious. Can anyone point me in the right direction?
You'll need to use the CImage::Draw() overload that lets you specify the source rectangle. So that you can clip the image that you want to paint from the original. There are two candidates, the Draw(HDC hDestDC, const RECT& rectDest, const RECT& rectSrc) for example. The other one is the first one listed on this doc page.
Usually you don't want to create a small image if you're just using the multi-icon image in a GUI.
When you come to draw the icon, pass its offset and size to the bitblt function rather than the dimensions of the whole image.
If you do want to store the sub-image in a bitmap, create a memory DC for the target bitmap and draw into that.

DirectX9 Texture of arbitrary size (non 2^n)

I'm relatively new to DirectX and have to work on an existing C++ DX9 application. The app does tracking on a camera images and displays some DirectDraw (ie. 2d) content. The camera has an aspect ratio of 4:3 (always) and the screen is undefined.
I want to load a texture and use this texture as a mask, so tracking and displaying of the content only are done within the masked area of the texture. Therefore I'd like to load a texture that has exactly the same size as the camera images.
I've done all steps to load the texture, but when I call GetDesc() the fields Width and Height of the D3DSURFACE_DESC struct are of the next bigger power-of-2 size. I do not care that the actual memory used for the texture is optimized for the graphics card but I did not find any way to get the dimensions of the original image file on the harddisk.
I do (and did, but with no success) search a possibility to load the image into the computers RAM only (graphicscard is not required) without adding a new dependency to the code. Otherwise I'd have to use OpenCV (which might anyway be a good idea when it comes to tracking), but at the moment I still try to avoid including OpenCV.
thanks for your hints,
Norbert
D3DXCreateTextureFromFileEx with parameters 3 and 4 being
D3DX_DEFAULT_NONPOW2.
After that, you can use
D3DSURFACE_DESC Desc;
m_Sprite->GetLevelDesc(0, &Desc);
to fetch the height & width.
D3DXGetImageInfoFromFile may be what you are looking for.
I'm assuming you are using D3DX because I don't think Direct3D automatically resizes any textures.