Loading PNGs with CImg - png

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.

Related

Why can I not print non-ASCII characters to the console in curses mode?

Please consider this simple snippet:
#include <stdio.h>
#include <curses.h>
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
void cleanup(int signal)
{
endwin();
exit(0);
}
int main()
{
initscr();
struct sigaction cleanup_action = { .sa_handler = cleanup, .sa_flags = 0 };
sigfillset(&cleanup_action.sa_mask);
sigaction(SIGINT, &cleanup_action, NULL);
cbreak();
keypad(stdscr, TRUE);
noecho();
mvaddstr(2, 2, "🧱");
mvaddstr(2, 3, "🧱");
mvaddstr(2, 4, "🧱");
mvaddstr(3, 2, "⬜");
mvaddstr(3, 2, "⚾");
mvaddstr(3, 4, "⬜");
refresh();
while(true) getch();
return 0;
}
(No, I'm not certain that my clean-up on exit is correct, but that's not the point.)
Why are the emojis not being printed out?
When I run this program this is what I see:
���~_��
�~��~\
I don't understand this because according to POSIX specification:
addnstr, addstr, mvaddnstr, mvaddstr, mvwaddnstr, mvwaddstr waddnstr, waddstr - add a string of multi-byte characters without rendition to a window and advance cursor
"MULTI-BYTE" they say! So I guess this should print out correctly! I'm not limited to ASCII!
Also, I guess my terminal can handle these characters. This is because as opposed to curses.h, stdio.h is able to print them correctly:
#include <stdio.h>
int main()
{
printf("🧱⬜⚾\n");
return 0;
}
This prints out:
🧱⬜⚾
How can I print emojis with curses.h?

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.

Timer0 as counter with Microchip PIC10F202

I developing RC switch for my RC glider.
I have microkontroller Microchip PIC10F202 and I have problem with ON/OFF LED Lights.
I want triggering input PPM signal from RC receiver, I setted GP2 as input (by T0CKI and count for every rise edge - from low to high), but LED is still ON, not reacting on input signal from RC receiver.
I post my source code in C language, I can not ASM language.
// this is header file with some macros and function prototypes
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H
#include <xc.h>
#define _XTAL_FREQ 4000000
#define KONST_ON 50
#define KONST_OFF 1000
#define STROBO_LED GP0
#define NAVI_LED GP1
#define RC_SIGNAL GP2
#define STAV_ON 1
#define STAV_OFF 0
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
void setup ();
void flashing ();
#endif
// In this function is setup for microkontroller
#include <xc.h>
#include "prototypy.h"
void setup ()
{
OSCCAL = 0b0111111;
//STATUS = 0b00111000;
OPTION = 0b11100111;
TRISGPIO = 0b0000;
};
// This is function for LED flashing
#include <xc.h>
#include "prototypy.h"
void flashing ()
{
__delay_ms (KONST_OFF);
STROBO_LED = STAV_OFF;
__delay_ms (KONST_ON);
STROBO_LED = STAV_ON;
};
// in this main function I do logical product, where I have logical state from pin GP2 and number 1
#include <xc.h>
#include "prototypy.h"
// Code protection bit
#pragma config CP = OFF
// Watchdog timer
#pragma config WDTE = OFF
// Pin function
#pragma config MCLRE = OFF
void main ()
{
setup ();
while (1)
{
if (RC_SIGNAL & 1)
{
flashing ();
}
}
}
Please, can me someone help and found error in my source code?
I'am not really familar with that devive, but I guess you Pin RC_SIGNAL is not configured as an Input. Please try
TRISGPIO = 0b0100;

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

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();
}

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
}
}