How to resize an image using SDL2 - c++

I have been looking around on many links to find out how to resize an image on SDL2, like in pygame, the "pygame.transform.scale()" function, and I have tried to look to see how to implement one but all of the links are unique to the question or are very unclear to me. How can I implement a function that returns a surface/texture, whatever is used for SDL_RenderCopy() of a newly-resized image, so when I want to use the rectangle it has, it's rectangle is now resized to what i tell it to do. As in:
SDL_Surface/SDL_Texture ResizeImage(int width, int height){
//Process code here to effectively do the equivalent in python of:
newSurface/Texture = pygame.transform.scale(img, width, height);
return newSurface/Texture;
}

Related

How to use DrawTransparentBitmap() in C++ Builder

Just trying to figure out how to use DrawTransparent in C++ Builder to draw a bitmap or tbitmap with an alpha channel so that the drawn image is semi-transparent with the background image.
I looked all over the place and on this site but can't find anything other than a note that this as well as DrawTransparentBitmap exists..
In the help it is listed as follows:
virtual void __fastcall DrawTransparent(TCanvas* ACanvas, const
System::Types::TRect &Rect, System::Byte Opacity);
However there are no code examples. The compiler doesn't recognize the procedure name and it does not appear as a method of tbitmap...
I am still new to C++ and I could really use some help with this...
DrawTransparent is a protected method so you can't call it directly, but this should work:
// opacity 50
destination_bitmap->Canvas->Draw(0, 0, source_bitmap->Picture->Graphic, 50);
You may need to do source_bitmap->Transparent = true; too.

GTKMM: Take a screen shot of an DrawingArea?

I Have a Drawing Area that I draw some figures or images inside, so I have this callback to take screenshots:
void CanvasToolBox::actionCanvasCamera()
{
auto root = Gdk::Window::get_default_root_window();
int height = root->get_height();
int width = root->get_width();
auto pixels = Gdk::Pixbuf::create(root, 0, 0, width, height);
pixels->save("s.png", "png");
}
That code takes a screenshot of the whole screen..
But I need to take the screenshot of a especific window a Gtk::DrawingArea in my case.. so the function create needs a Gdk::Window as a parameter.
Is there any way to take a screenshot of a Gtk especific window?
Thanks
I'm not used to GTKmm (I use C and Python APIs instead), but with your example, that looks pretty straightforward. You just want to use your Gtk::DrawingArea instead of the root window. So you search in the documentation of Gdk::Window::get_default_root_window and find that it returns a Gdk::Window.
Now you have to find how to get a Gdk::Window out of a Gtk::DrawingArea. So you go to the documentation of Gtk::DrawingArea and search for a function that returns a Gdk::Window, and then find this is Gtk::Widget::get_window, which is inherited from Gtk::Widget. BTW, you may prefer the const version, as you don't modify the window, just read it.
You can then replace code that gets the root windows with a call to get the Gdk::Window of the widget you care about, and you're done. If you want to go further, instead of using Gdk::Pixbuf, there may be a way to use a cairo context.

How do I create a border around a rectangle in an image(PIX) in Leptonica?

I have an image in PIX format, and I have a BOX[with the necessary coordinates] too, I would like to draw a border around the content that's in the box, like for example -
I've looked around in the documentation, but so far I've only found out how to border the whole image, not just a rectangle inside it.
Your method works only for images depth of 3 bpp. Use the pixPaintBoxa or pixDrawBoxa method. See https://tpgit.github.io/Leptonica/boxfunc3_8c.html#a2dca217227f62a99f6003910420b47ba for more details. These methods work for any bpp.
However to use these functions, you have to build leptonica again using static makefiles. See http://www.leptonica.com/source/README.html#DEVELOP for more details.

is it possible to define 'plot panels' in opengl and directly draw to them instead of the window?

I was wondering whether the following can be easily implemented in OpenGL:
divide window into multiple panels
put each panel into place using normalized coordinates (0-1) so that if the window gets re-sized, the panel will stay in the correct position
draw directly into one of the previously defined panels
The result should look something like the following picture:
So far I've been drawing my objects directly into the window, offsetting the coordinates with the respective value for each panel. However this doesn't feel like the right way of doing things. Any suggestions and example code from experienced developers is highly appreciated!
EDIT: What I've read so far, using glScissor or glViewport might be two ways of accomplishing what I want, however I don't know what the pros and cons are for going either route (your insights are very welcome!). Additionally, I would really prefer to define some kind of panel, return its handle and just draw into that.
If you use glScissor() you just define the clipping rectangle (i.e. where to draw). This might be everything you need.
With glViewport() you essentially call glScissor() behind the scenes as well as changing how coordinates are mapped to screen space.
If you want to limit drawing to one of the panels (without using local coordinates), use glScissor(). Otherwise, use glViewport().
As for the panel, I'd probably use some abstract base class:
class Panel {
// ...
virtual void OnDraw(void) = 0;
void Draw(void) {
glViewport(x, y, w, h);
OnDraw();
glViewport(0, 0, parent_w, parent_h);
}
}

glutGetWindow - Expression must have class type

Good Afternoon,
So I'm working with C++ (Visual Studios C++ 2010 to be exact) and am working on a seemingly easy task;
Draw a picture onto the middle of the window. If you readjust the window size, the picture/bitmap will be redrawn into the middle of the newly sized window.
I figure to get the middle of the window, I should find its rightmost and bottommost bits, then divide each by 2, but I don't know how to get the window length and height.
I currently have
centrewidth = glutGetWindow().Size.Width;
centreheight = glutGetWindow().Size.Height;
Yet for both I'm getting errors on glutGetWindow, saying "Error: Expression must have class type". I'm certain once I get this, it will be no problem, but this is causing a lot of trouble for me. Any advice is greatly appreciated. Thanks!
glutGetWindow doesn't return object of any class type. It's signature is this:
int glutGetWindow(void);
And you need is this (see doc):
int glutGet(GLenum eWhat);
Example,
int width = glutGet(GLUT_WINDOW_WIDTH); //Width in pixels of the current window.
int height = glutGet(GLUT_WINDOW_HEIGHT); //Height in pixels of the current window.
Have a look at the doc to know what states you can get using this function.