How can I display 2 or more .pgm images using CImg library in C++? - cimg

Im trying to load and display 2 .pgm files in c++
So far I have:
#include <iostream>
#include <CImg.h>
using namespace std;
using namespace cimg_library;
int main()
{
CImg<unsigned char> image1("pic1.pgm");
image1.display();
CImg<unsigned char> image2("pic2.pgm");
image2.display();
}
The second image only displays after Ive closed the first one off. Is there a way I can open them both at the same time?
And for future reference, is there a way I can do this with 3 or more images.
Thank you
(sorry for being a noob)

Actually, there's an even easier way - by making a CImgList of your images. Note that I changed pgm to ppm just to get colour:
#include "CImg.h"
using namespace cimg_library;
int main(int argc, char** const argv)
{
CImg<unsigned char> image1("pic1.ppm"); // red square
CImg<unsigned char> image2("pic2.ppm"); // blue square
(image1,image2).display();
}

Related

Learning GUI programming with GTK+3

I am new to GUI programming. I recently installed Gtk+3 version on Linux. But, when I typed following code:
#include <gtk/gtk.h>
#include <stdio.h>
static int count = 0;
void button_clicked(GtkWidget *button, gpointer data)
{
printf(“%s pressed %d time(s) \n”, (char *) data, ++count);
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
button = gtk_button_new_with_label(“Hello World!”);
gtk_container_add(GTK_CONTAINER(window), button);
g_signal_connect(GTK_OBJECT (button), “clicked”,
GTK_SIGNAL_FUNC (button_clicked),
“Button 1”);
gtk_widget_show(button);
gtk_widget_show(window);
gtk_main ();
return 0;
}
To run this code I used this command: $ gcc gtk1.c –o gtk1 pkg-config --cflags --libs gtk+-3.0
but I had error like this
undefined reference to GTK_OBJECT;
undefined reference to GTK_SIGNAL_FUNC;
This is because your code sample is for an old version of GTK+ 2. GTK_OBJECT was deprecated in the late GTK+ 2.x versions, and finally removed in GTK+ 3. Same for GTK_SIGNAL_FUNC. Both have been moved to the GObject library, where they now stand as G_OBJECT and G_CALLBACK.
To avoid using outdated code, just get started with the code samples from the GTK+ 3 documentation.

Is there a way to catch a STATUS_STACK_BUFFER_OVERRUN error programmatically?

We have some C code that is throwing a STATUS_STACK_BUFFER_OVERRUN error (0xC0000409) once in a while. I can reproduce that error using the C code below. I'm using Visual Studio 2013 Update 4 on Windows 7, and I'm compiling with the /EHa and /GS flags. However, I have been unable to catch the error programmatically. The code never enters my __except block; instead, Visual Studio pops up a few dialog boxes informing me of the stack corruption. I realize that once this error occurs, the state of the program is in doubt; I'm merely trying to capture the error in hopes of locating where it is occurring in our production code. Is there a way to handle this error programmatically?
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#pragma warning(disable: 4996) // strcpy
void vulnerable(const char* str)
{
char buffer[10];
strcpy(buffer, str); // overrun the buffer
}
int main()
{
__try
{
char large_buffer[] = "This string is longer than 10 characters.";
vulnerable(large_buffer);
}
__except (GetExceptionCode() == STATUS_STACK_BUFFER_OVERRUN)
{
printf("error"); // never getting here
}
}

Draw background image in gtk - none of my attempts work

I've been trying to set a background image on a gtk widget without success, even after trying 4 different approaches.
The following program contains 3 approaches (the 4th approach involves no code). I compiled it using MinGW (g++ 4.5.0) and gtkmm 2.4. The APPROACH macro can be set to 1, 2 or 3 in order to choose which approach to compile. I've also added references in the comments, so you can find out where I got the ideas from.
#include <iostream>
#include <gtkmm/main.h>
#include <gtkmm/alignment.h>
#include <gtkmm/box.h>
#include <gtkmm/entry.h>
#include <gtkmm/eventbox.h>
#include <gtkmm/frame.h>
#include <gtkmm/image.h>
#include <gtkmm/label.h>
#include <gtkmm/table.h>
#include <gtkmm/window.h>
// Set this to 1, 2 or 3 to try different ways of drawing the background
// Set to 0 to load no background at all
#define APPROACH (0)
// Making this alignment global in order to modify it from drawBackground
Gtk::Alignment* alignment;
bool drawBackground(GdkEventExpose* event) {
std::cout << "Draw background" << std::endl;
// Load background image
Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_file("background.jpg");
Glib::RefPtr<Gdk::Pixmap> pixmap;
Glib::RefPtr<Gdk::Bitmap> mask;
pixbuf->render_pixmap_and_mask(pixmap, mask,0);
{
// Test that pixbuf was created correctly
Glib::RefPtr<Gdk::Pixbuf> back_to_pixbuf = Gdk::Pixbuf::create((Glib::RefPtr<Gdk::Drawable>)pixmap, 0, 0, pixbuf->get_width(), pixbuf->get_height());
back_to_pixbuf->save("back_to_pixbuf.png", "png");
}
#if APPROACH == 1
// Approach 1: draw_pixbuf
// Ref: http://islascruz.org/html/index.php/blog/show/Image-as-background-in-a-Gtk-Application..html
Glib::RefPtr<Gtk::Style> style = alignment->get_style();
alignment->get_window()->draw_pixbuf(style->get_bg_gc(Gtk::STATE_NORMAL), pixbuf, 0, 0, 0, 200, pixbuf->get_width(), pixbuf->get_height(), Gdk::RGB_DITHER_NONE, 0, 0);
#endif
#if APPROACH == 2
// Approach 2: set_back_pixmap
// Ref: http://www.gtkforums.com/viewtopic.php?t=446
// http://stackoverflow.com/questions/3150706/gtk-drawing-set-background-image
alignment->get_window()->set_back_pixmap(pixmap);
#endif
}
int main (int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window w;
Gtk::VBox mainBox;
// Top image
Gtk::Image topImage("header.jpg");
mainBox.pack_start(topImage,false,false,0);
// Middle alignment
alignment = Gtk::manage(new Gtk::Alignment);
mainBox.pack_start(*alignment,true,true,0);
// Create widget
Gtk::Alignment mywidget(0.5, 0.5, 0.1, 0.9);
Gtk::Table table;
Gtk::Label label1("Username"); table.attach(label1,0,1,0,1);
Gtk::Label label2("Password"); table.attach(label2,0,1,1,2);
Gtk::Entry entry1; table.attach(entry1,1,2,0,1);
Gtk::Entry entry2; table.attach(entry2,1,2,1,2);
Gtk::Button button("Login"); table.attach(button,1,2,2,3);
mywidget.add(table);
// Put widget in middle alignment
alignment->add(mywidget);
// Try to change the background
#if APPROACH == 1 || APPROACH == 2
alignment->signal_expose_event().connect(sigc::ptr_fun(&drawBackground), true);
#endif
#if APPROACH == 3
// Approach 3: modify the style using code
// Ref: http://www.gtkforums.com/viewtopic.php?t=446
// Load background image
Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_file("background.jpg");
Glib::RefPtr<Gdk::Pixmap> pixmap;
Glib::RefPtr<Gdk::Bitmap> mask;
pixbuf->render_pixmap_and_mask(pixmap, mask,0);
Glib::RefPtr<Gtk::Style> style = alignment->get_style()->copy();
style->set_bg_pixmap(Gtk::STATE_NORMAL,pixmap);
style->set_bg_pixmap(Gtk::STATE_ACTIVE,pixmap);
style->set_bg_pixmap(Gtk::STATE_PRELIGHT,pixmap);
style->set_bg_pixmap(Gtk::STATE_SELECTED,pixmap);
style->set_bg_pixmap(Gtk::STATE_INSENSITIVE,pixmap);
alignment->set_style(style);
#endif
// Approach 4: modify share\themes\MS-Windows\gtk-2.0
// adding the following line
// bg_pixmap[NORMAL] = "D:\\path\\to\\file\\background.jpg"
// in the style "msw-default" section
// Ref: http://lists.ximian.com/pipermail/gtk-sharp-list/2005-August/006324.html
// Show the window
w.add(mainBox);
w.show_all();
kit.run(w);
return 0;
}
Links to images I used: header.jpg background.jpg
The layout mimics that of my actual program. The main window contains a Gtk::VBox with a header image on top and an Gtk::Alignment at the bottom. The contents of this alignment will change over time but I want it to have a background image always visible.
When loading no background at all, the header image loads correctly and the window looks like this:
Approach 1 is the one that is closer to work, though it hides the labels and the buttons:
Approaches 2 and 3 look the same as loading no background. Besides, approach 2 gives me the following error message:
(test-img-fondo.exe:1752): Gdk-CRITICAL **: gdk_window_set_back_pixmap: assertion `pixmap == NULL || !parent_relative' failed
Finally, in approach 4, I attempt to modify share\themes\MS-Windows\gtk-2.0 by adding the following line
bg_pixmap[NORMAL] = "D:\\path\\to\\file\\background.jpg"
in the style "msw-default" section. It doesn't work either.
So, has anyone succesfully drawn a background image on a Gtk widget? Is this possible at all? Any changes in my code that would make this work? Any workarounds?
All help is greatly appreciated.
I think I've solved it myself. Use approach 1 but change this line
alignment->signal_expose_event().connect(sigc::ptr_fun(&drawBackground), true);
for this:
alignment->signal_expose_event().connect(sigc::ptr_fun(&drawBackground), false);
This way, the call to drawBackground occurs before gtk calls its own handlers.
I should also point out that, in a real program, the images should be loaded once outside of drawBackground.

How to create a GtkImage from a Cairo context?

I got a paint function that works using a Cairo context and the end result should be a GtkImage (without intermediate image creation). I tried to use the gdk_cairo_create function but this code:
...
GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 22, 22);
GtkWidget *image = gtk_image_new_from_pixbuf (pixbuf);
GdkDrawable *drawable = image->window;
cairo_t *ctx = gdk_cairo_create (drawable);
my_cairo_paint_function (ctx);
...
fails with:
Gdk-CRITICAL **: IA__gdk_cairo_create: assertion `GDK_IS_DRAWABLE (drawable)' failed
Same with a simple:
#include <gtk/gtk.h>
#include <cairo.h>
int
main (int argc, char *argv[])
{
gtk_init(&argc, &argv);
cairo_t *ctx = gdk_cairo_create (gtk_widget_get_window (gtk_image_new_from_file ("foobar.png")));
gtk_main();
return 0;
}
I don't understand why this fails. Any help is appreciated!
GtkImage doesn't have a GdkWindow, so the call to gtk_widget_get_window() or the access of widget->window returns NULL. You can put the GtkImage in a GtkEventBox and draw on the event box's GdkWindow.
Although, it looks like you're trying (with gdk_pixbuf_new) to create an empty space to draw on. In that case, GtkImage is not the widget you want -- use GtkDrawingArea. And don't forget to call your paint function in the handler for the event-expose signal!

Loading PNGs with CImg

I am unable to load PNGs with CImg. I've heard you need to get libpng / zlib to get to work first but I am unsure how to set this up. I am on Ubuntu. My source:
#include <cmath>
#include <cstdio>
#include <string>
#include <assert.h>
#include <stdarg.h>
#define cimg_using_png
#include "CImg.h"
using namespace cimg_library;
#include "png.h"
int main(int argc, char** argv)
{
CImg<unsigned char> img2("test.png");
img2.display();
return 0;
}
Close, but you need #define cimg_use_png
and add -lpng to your linker flags.