Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <gtk/gtk.h>
- #include <gdk-pixbuf/gdk-pixbuf.h>
- #include <gdk/gdk.h>
- #include <gdk/gdkkeysyms.h>
- GtkContainer *box;
- GtkWidget *window, *vbox, *window2;
- GtkWidget *menubar, *filemenu, *help, *helpmenu;
- GtkWidget *file, *quit, *open, *save, *properties, *about, *grayscale, *brightness, *contrast, *negative;
- GtkWidget *img, *img2;
- GtkWidget *dialog;
- GdkPixbuf *mypic;
- gint offset;
- void create_menu()
- {
- /*-----------------creating vertical box-------------------------------*/
- vbox = gtk_vbox_new(FALSE, 0);
- box = gtk_vbox_new(FALSE, 0);
- gtk_box_pack_start(GTK_BOX(box), vbox, TRUE, TRUE, 0);
- menubar = gtk_menu_bar_new();
- filemenu = gtk_menu_new();
- /*----------------file menu items-----------------------------------------*/
- file = gtk_menu_item_new_with_label("File");
- quit = gtk_menu_item_new_with_label("Quit Ctrl + Q");
- open = gtk_menu_item_new_with_label("Open Ctrl + O");
- save = gtk_menu_item_new_with_label("Save");
- grayscale = gtk_menu_item_new_with_label("GrayScale Ctrl + G");
- brightness = gtk_menu_item_new_with_label("Brightness Ctrl + B");
- contrast = gtk_menu_item_new_with_label("Contrast Ctrl + C");
- negative = gtk_menu_item_new_with_label("Negative Ctrl + N");
- properties = gtk_menu_item_new_with_label("Properties Ctrl + P");
- /*----------------file menu item shells------------------------------------*/
- gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
- gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), open);
- gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), grayscale);
- gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), contrast);
- gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), brightness);
- gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), negative);
- gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), save);
- gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), properties);
- gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
- gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
- /*--------------------help menu items------------------------*/
- helpmenu = gtk_menu_new();
- about = gtk_menu_item_new_with_label("About");
- help = gtk_menu_item_new_with_label("Help");
- /*---------------help menu item shells------------------------*/
- gtk_menu_item_set_submenu(GTK_MENU_ITEM(help), helpmenu);
- gtk_menu_shell_append(GTK_MENU_SHELL(helpmenu), about);
- gtk_menu_shell_append(GTK_MENU_SHELL(menubar), help);
- gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
- }
- void show_about()
- {
- GtkWidget *dialog;
- const char *author[] = {"None yet\n(However, Special thanks to Mehedi Hasan :-) )",NULL};
- dialog = gtk_about_dialog_new();
- gtk_about_dialog_set_program_name (GTK_ABOUT_DIALOG(dialog),"You just clicked about!");
- gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG(dialog),author);
- gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG(dialog),"Good job and go to hell");
- gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_CENTER);
- gtk_dialog_run(GTK_DIALOG(dialog));
- gtk_widget_destroy(dialog);
- }
- void open_file()
- {
- dialog = gtk_file_chooser_dialog_new ("Open File",window,GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,NULL);
- if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
- {
- offset = 5;
- char *filename;
- filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
- gtk_container_remove(GTK_CONTAINER(box), img);
- img = gtk_image_new_from_file(filename);
- mypic = gdk_pixbuf_new_from_file(filename, NULL);
- gtk_widget_show(img);
- gtk_box_pack_start(GTK_BOX(box), img, TRUE, TRUE, 0);
- g_free (filename);
- }
- gtk_widget_destroy (dialog);
- }
- void show_properties()
- {
- }
- void image_grayscale()
- {
- GdkPixbuf *result_pixbuf;
- char *sp, *dp;
- int width, height, line, result_rowstride, mypic_rowstride, bytes_per_pixel;
- result_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (mypic),
- gdk_pixbuf_get_has_alpha (mypic),
- gdk_pixbuf_get_bits_per_sample (mypic),
- gdk_pixbuf_get_width (mypic),
- gdk_pixbuf_get_height (mypic));
- width = gdk_pixbuf_get_width (result_pixbuf);
- height = gdk_pixbuf_get_height (result_pixbuf);
- result_rowstride = gdk_pixbuf_get_rowstride (result_pixbuf);
- mypic_rowstride = gdk_pixbuf_get_rowstride (mypic);
- bytes_per_pixel = gdk_pixbuf_get_has_alpha (result_pixbuf) ? 4 : 3;
- sp = gdk_pixbuf_get_pixels (mypic);
- dp = gdk_pixbuf_get_pixels (result_pixbuf);
- for (line = 0; line < height; line ++)
- {
- char *sq = sp;
- char *dq = dp;
- int i;
- for (i = 0; i < width; i ++)
- {
- dq[0] = (sq[0] + sq[1] + sq[2])/3;
- dq[1] = dq[0];
- dq[2] = dq[0];
- dq += bytes_per_pixel;
- sq += bytes_per_pixel;
- }
- sp += mypic_rowstride;
- dp += result_rowstride;
- }
- window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
- gtk_window_set_default_size(GTK_WINDOW(window2), 600, 400);
- img2 = gtk_image_new_from_pixbuf(result_pixbuf);
- gtk_container_add(GTK_CONTAINER(window2), img2);
- gtk_widget_show_all(window2);
- }
- static unsigned char apply_brightness (unsigned char u_value, float brightness)
- {
- float nvalue, value;
- double power;
- value = (float) u_value / 255.0;
- /*--------------------------- apply brightness---------------- */
- if (brightness < 0.0) value = value * (1.0 + brightness);
- else value = value + ((1.0 - value) * brightness);
- return (guchar) (value * 255);
- }
- static unsigned char apply_contrast (unsigned char u_value, float contrast)
- {
- float nvalue;
- double power;
- float value;
- value = (float) u_value / 255.0;
- /*----------------- apply contrast -------------------------*/
- if (contrast < 0.0)
- {
- if (value > 0.5) nvalue = 1.0 - value;
- else nvalue = value;
- if (nvalue < 0.0) nvalue = 0.0;
- nvalue = 0.5 * pow (nvalue * 2.0 , (double) (1.0 + contrast));
- if (value > 0.5) value = 1.0 - nvalue;
- else value = nvalue;
- }
- else
- {
- if (value > 0.5) nvalue = 1.0 - value;
- else nvalue = value;
- if (nvalue < 0.0) nvalue = 0.0;
- power = (contrast == 1.0) ? 127 : 1.0 / (1.0 - contrast);
- nvalue = 0.5 * pow (2.0 * nvalue, power);
- if (value > 0.5) value = 1.0 - nvalue;
- else value = nvalue;
- }
- return (guchar) (value * 255);
- }
- void image_contrast()
- {
- float contrast= 0.5;
- GdkPixbuf *result_pixbuf;
- char *sp, *dp;
- int width, height, line, result_rowstride, mypic_rowstride, bytes_per_pixel;
- g_return_val_if_fail ((contrast > -1.0 ) && (contrast < 1.0 ), NULL);
- result_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (mypic),
- gdk_pixbuf_get_has_alpha (mypic),
- gdk_pixbuf_get_bits_per_sample (mypic),
- gdk_pixbuf_get_width (mypic),
- gdk_pixbuf_get_height (mypic));
- width = gdk_pixbuf_get_width (result_pixbuf);
- height = gdk_pixbuf_get_height (result_pixbuf);
- result_rowstride = gdk_pixbuf_get_rowstride (result_pixbuf);
- mypic_rowstride = gdk_pixbuf_get_rowstride (mypic);
- bytes_per_pixel = gdk_pixbuf_get_has_alpha (result_pixbuf) ? 4 : 3;
- sp = gdk_pixbuf_get_pixels (mypic);
- dp = gdk_pixbuf_get_pixels (result_pixbuf);
- for (line = 0; line < height; line ++)
- {
- char *sq = sp;
- char *dq = dp;
- int i;
- for (i = 0; i < width; i ++)
- {
- dq[0] = apply_contrast (sq[0], contrast);
- dq[1] = apply_contrast (sq[1], contrast);
- dq[2] = apply_contrast (sq[2], contrast);
- dq += bytes_per_pixel;
- sq += bytes_per_pixel;
- }
- sp += mypic_rowstride;
- dp += result_rowstride;
- }
- window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
- gtk_window_set_default_size(GTK_WINDOW(window2), 600, 400);
- img2 = gtk_image_new_from_pixbuf(result_pixbuf);
- gtk_container_add(GTK_CONTAINER(window2), img2);
- gtk_widget_show_all(window2);
- }
- void image_brightness()
- {
- float brightness=0.1;
- GdkPixbuf *result_pixbuf;
- char *sp, *dp;
- int width, height, line, result_rowstride, mypic_rowstride, bytes_per_pixel;
- g_return_val_if_fail ((brightness > -1.0 ) && (brightness < 1.0 ), NULL);
- result_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (mypic),
- gdk_pixbuf_get_has_alpha (mypic),
- gdk_pixbuf_get_bits_per_sample (mypic),
- gdk_pixbuf_get_width (mypic),
- gdk_pixbuf_get_height (mypic));
- width = gdk_pixbuf_get_width (result_pixbuf);
- height = gdk_pixbuf_get_height (result_pixbuf);
- result_rowstride = gdk_pixbuf_get_rowstride (result_pixbuf);
- mypic_rowstride = gdk_pixbuf_get_rowstride (mypic);
- bytes_per_pixel = gdk_pixbuf_get_has_alpha (result_pixbuf) ? 4 : 3;
- sp = gdk_pixbuf_get_pixels (mypic);
- dp = gdk_pixbuf_get_pixels (result_pixbuf);
- for (line = 0; line < height; line ++)
- {
- char *sq = sp;
- char *dq = dp;
- int i;
- for (i = 0; i < width; i ++)
- {
- dq[0] = apply_brightness (sq[0], brightness);
- dq[1] = apply_brightness (sq[1], brightness);
- dq[2] = apply_brightness (sq[2], brightness);
- dq += bytes_per_pixel;
- sq += bytes_per_pixel;
- }
- sp += mypic_rowstride;
- dp += result_rowstride;
- }
- window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
- gtk_window_set_default_size(GTK_WINDOW(window2), 600, 400);
- img2 = gtk_image_new_from_pixbuf(result_pixbuf);
- gtk_container_add(GTK_CONTAINER(window2), img2);
- gtk_widget_show_all(window2);
- }
- void image_negative()
- {
- GdkPixbuf *result_pixbuf;
- char *sp, *dp;
- int width, height, line, result_rowstride, mypic_rowstride, bytes_per_pixel;
- result_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (mypic),
- gdk_pixbuf_get_has_alpha (mypic),
- gdk_pixbuf_get_bits_per_sample (mypic),
- gdk_pixbuf_get_width (mypic),
- gdk_pixbuf_get_height (mypic));
- width = gdk_pixbuf_get_width (result_pixbuf);
- height = gdk_pixbuf_get_height (result_pixbuf);
- result_rowstride = gdk_pixbuf_get_rowstride (result_pixbuf);
- mypic_rowstride = gdk_pixbuf_get_rowstride (mypic);
- bytes_per_pixel = gdk_pixbuf_get_has_alpha (result_pixbuf) ? 4 : 3;
- sp = gdk_pixbuf_get_pixels (mypic);
- dp = gdk_pixbuf_get_pixels (result_pixbuf);
- for (line = 0; line < height; line ++)
- {
- char *sq = sp;
- char *dq = dp;
- int i;
- for (i = 0; i < width; i ++)
- {
- dq[0] = 255 - sq[0];
- dq[1] = 255 - sq[1];
- dq[2] = 255 - sq[2];
- dq += bytes_per_pixel;
- sq += bytes_per_pixel;
- }
- sp += mypic_rowstride;
- dp += result_rowstride;
- }
- window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
- gtk_window_set_default_size(GTK_WINDOW(window2), 600, 400);
- img2 = gtk_image_new_from_pixbuf(result_pixbuf);
- gtk_container_add(GTK_CONTAINER(window2), img2);
- gtk_widget_show_all(window2);
- }
- static int keyboard_keys(GtkWidget *futile, GdkEventKey *lol, gpointer data)
- {
- if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_q || lol->keyval == GDK_KEY_Q))
- exit (0);
- if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_O || lol->keyval == GDK_KEY_o))
- open_file();
- if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_P || lol->keyval == GDK_KEY_p))
- show_properties();
- if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_B || lol->keyval == GDK_KEY_b))
- image_brightness();
- if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_C || lol->keyval == GDK_KEY_c))
- image_contrast();
- if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_G || lol->keyval == GDK_KEY_g))
- image_grayscale();
- if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_N || lol->keyval == GDK_KEY_n))
- image_negative();
- return TRUE;
- }
- int main(int argc, char*argv[])
- {
- /*---------------------initialization-----------------------------*/
- gtk_init(&argc, &argv);
- window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
- gtk_window_set_default_size(GTK_WINDOW(window), 800, 500);
- gtk_window_set_title(GTK_WINDOW(window), "Fireflight Image Viewer 1.052");
- /*---------------------go to creat menu----------------------------*/
- create_menu();
- /*---------------------default window-------------------------------*/
- char picture_name[] = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg";
- img = gtk_image_new_from_file(picture_name);
- mypic = gdk_pixbuf_new_from_file(picture_name, NULL);
- gtk_box_pack_start(GTK_BOX(box), img, TRUE, TRUE, 0);
- gtk_container_add(GTK_CONTAINER(window), box);
- /*----------------------connecting signals----------------------------*/
- g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
- g_signal_connect(G_OBJECT(open), "activate", G_CALLBACK(open_file), NULL);
- g_signal_connect(G_OBJECT(grayscale), "activate", G_CALLBACK(image_grayscale ), NULL);
- g_signal_connect(G_OBJECT(contrast), "activate", G_CALLBACK(image_contrast), NULL);
- g_signal_connect(G_OBJECT(brightness), "activate", G_CALLBACK(image_brightness), NULL);
- g_signal_connect(G_OBJECT(negative), "activate", G_CALLBACK(image_negative), NULL);
- g_signal_connect(G_OBJECT(properties), "activate", G_CALLBACK(show_properties), NULL);
- g_signal_connect(G_OBJECT(about), "activate", G_CALLBACK(show_about), NULL);
- g_signal_connect(G_OBJECT(quit), "activate",G_CALLBACK(gtk_main_quit), NULL);
- g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(keyboard_keys), window);
- /*------------------------go to gtk main----------------------------------*/
- gtk_widget_show_all(window);
- gtk_main();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement