Advertisement
palmerstone

Gtk 13

Nov 27th, 2011
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <gtk/gtk.h>
  4. #include <gdk-pixbuf/gdk-pixbuf.h>
  5. #include <gdk/gdk.h>
  6. #include <gdk/gdkkeysyms.h>
  7.  
  8. GtkContainer *box;
  9. GtkWidget *window, *vbox, *window2;
  10. GtkWidget *menubar, *filemenu, *help, *helpmenu;
  11. GtkWidget *file, *quit, *open, *save, *properties, *about, *grayscale, *brightness, *contrast, *negative;
  12. GtkWidget *img, *img2;
  13. GtkWidget *dialog;
  14. GdkPixbuf *mypic;
  15. gint offset;
  16.  
  17. void create_menu()
  18. {
  19.     /*-----------------creating vertical box-------------------------------*/
  20.     vbox = gtk_vbox_new(FALSE, 0);
  21.     box = gtk_vbox_new(FALSE, 0);
  22.     gtk_box_pack_start(GTK_BOX(box), vbox, TRUE, TRUE, 0);
  23.     menubar = gtk_menu_bar_new();
  24.     filemenu = gtk_menu_new();
  25.  
  26.     /*----------------file menu items-----------------------------------------*/
  27.     file = gtk_menu_item_new_with_label("File");
  28.     quit = gtk_menu_item_new_with_label("Quit                             Ctrl + Q");
  29.     open = gtk_menu_item_new_with_label("Open                           Ctrl + O");
  30.     save = gtk_menu_item_new_with_label("Save");
  31.     grayscale = gtk_menu_item_new_with_label("GrayScale                   Ctrl + G");
  32.     brightness = gtk_menu_item_new_with_label("Brightness                  Ctrl + B");
  33.     contrast = gtk_menu_item_new_with_label("Contrast                     Ctrl + C");
  34.     negative = gtk_menu_item_new_with_label("Negative                     Ctrl + N");
  35.     properties = gtk_menu_item_new_with_label("Properties                   Ctrl + P");
  36.  
  37.     /*----------------file menu item shells------------------------------------*/
  38.     gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
  39.     gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), open);
  40.     gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), grayscale);
  41.     gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), contrast);
  42.     gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), brightness);
  43.     gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), negative);
  44.     gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), save);
  45.  
  46.     gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), properties);
  47.     gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
  48.     gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
  49.  
  50.     /*--------------------help menu items------------------------*/
  51.     helpmenu = gtk_menu_new();
  52.     about = gtk_menu_item_new_with_label("About");
  53.     help = gtk_menu_item_new_with_label("Help");
  54.  
  55.     /*---------------help menu item shells------------------------*/
  56.     gtk_menu_item_set_submenu(GTK_MENU_ITEM(help), helpmenu);
  57.     gtk_menu_shell_append(GTK_MENU_SHELL(helpmenu), about);
  58.     gtk_menu_shell_append(GTK_MENU_SHELL(menubar), help);
  59.     gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
  60. }
  61.  
  62. void show_about()
  63. {
  64.     GtkWidget *dialog;
  65.     const char *author[] = {"None yet\n(However, Special thanks to Mehedi Hasan :-) )",NULL};
  66.     dialog = gtk_about_dialog_new();
  67.     gtk_about_dialog_set_program_name (GTK_ABOUT_DIALOG(dialog),"You just clicked about!");
  68.     gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG(dialog),author);
  69.     gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG(dialog),"Good job and go to hell");
  70.     gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_CENTER);
  71.     gtk_dialog_run(GTK_DIALOG(dialog));
  72.     gtk_widget_destroy(dialog);
  73. }
  74.  
  75. void open_file()
  76. {
  77.     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);
  78.     if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  79.     {
  80.         offset = 5;
  81.         char *filename;
  82.         filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
  83.         gtk_container_remove(GTK_CONTAINER(box), img);
  84.         img = gtk_image_new_from_file(filename);
  85.         mypic = gdk_pixbuf_new_from_file(filename, NULL);
  86.         gtk_widget_show(img);
  87.         gtk_box_pack_start(GTK_BOX(box), img, TRUE, TRUE, 0);
  88.         g_free (filename);
  89.     }
  90.     gtk_widget_destroy (dialog);
  91. }
  92.  
  93. void show_properties()
  94. {
  95.  
  96. }
  97.  
  98. void image_grayscale()
  99. {
  100.     GdkPixbuf *result_pixbuf;
  101.     char *sp, *dp;
  102.     int width, height, line, result_rowstride, mypic_rowstride, bytes_per_pixel;
  103.  
  104.     result_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (mypic),
  105.                                     gdk_pixbuf_get_has_alpha (mypic),
  106.                                     gdk_pixbuf_get_bits_per_sample (mypic),
  107.                                     gdk_pixbuf_get_width (mypic),
  108.                                     gdk_pixbuf_get_height (mypic));
  109.  
  110.     width = gdk_pixbuf_get_width (result_pixbuf);
  111.     height = gdk_pixbuf_get_height (result_pixbuf);
  112.  
  113.     result_rowstride = gdk_pixbuf_get_rowstride (result_pixbuf);
  114.     mypic_rowstride = gdk_pixbuf_get_rowstride (mypic);
  115.  
  116.     bytes_per_pixel = gdk_pixbuf_get_has_alpha (result_pixbuf) ? 4 : 3;
  117.  
  118.     sp = gdk_pixbuf_get_pixels (mypic);
  119.     dp = gdk_pixbuf_get_pixels (result_pixbuf);
  120.  
  121.     for (line = 0; line < height; line ++)
  122.     {
  123.         char *sq = sp;
  124.         char *dq = dp;
  125.         int i;
  126.  
  127.         for (i = 0; i < width; i ++)
  128.         {
  129.             dq[0] = (sq[0] + sq[1] + sq[2])/3;
  130.             dq[1] = dq[0];
  131.             dq[2] = dq[0];
  132.  
  133.             dq += bytes_per_pixel;
  134.             sq += bytes_per_pixel;
  135.         }
  136.  
  137.         sp += mypic_rowstride;
  138.         dp += result_rowstride;
  139.     }
  140.  
  141.     window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  142.     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  143.     gtk_window_set_default_size(GTK_WINDOW(window2), 600, 400);
  144.     img2 = gtk_image_new_from_pixbuf(result_pixbuf);
  145.     gtk_container_add(GTK_CONTAINER(window2), img2);
  146.     gtk_widget_show_all(window2);
  147. }
  148.  
  149.  
  150. static unsigned char apply_brightness (unsigned char u_value, float brightness)
  151. {
  152.     float  nvalue, value;
  153.     double power;
  154.  
  155.     value = (float) u_value / 255.0;
  156.  
  157.     /*--------------------------- apply brightness---------------- */
  158.     if (brightness < 0.0) value = value * (1.0 + brightness);
  159.     else value = value + ((1.0 - value) * brightness);
  160.  
  161.     return (guchar) (value * 255);
  162. }
  163.  
  164. static unsigned char apply_contrast (unsigned char u_value, float contrast)
  165. {
  166.     float  nvalue;
  167.     double power;
  168.     float  value;
  169.  
  170.     value = (float) u_value / 255.0;
  171.  
  172.     /*----------------- apply contrast -------------------------*/
  173.     if (contrast < 0.0)
  174.     {
  175.         if (value > 0.5)    nvalue = 1.0 - value;
  176.         else    nvalue = value;
  177.  
  178.         if (nvalue < 0.0) nvalue = 0.0;
  179.  
  180.         nvalue = 0.5 * pow (nvalue * 2.0 , (double) (1.0 + contrast));
  181.  
  182.         if (value > 0.5) value = 1.0 - nvalue;
  183.         else value = nvalue;
  184.     }
  185.     else
  186.     {
  187.         if (value > 0.5) nvalue = 1.0 - value;
  188.         else nvalue = value;
  189.  
  190.         if (nvalue < 0.0) nvalue = 0.0;
  191.  
  192.         power = (contrast == 1.0) ? 127 : 1.0 / (1.0 - contrast);
  193.         nvalue = 0.5 * pow (2.0 * nvalue, power);
  194.  
  195.         if (value > 0.5) value = 1.0 - nvalue;
  196.         else value = nvalue;
  197.     }
  198.  
  199.     return (guchar) (value * 255);
  200. }
  201.  
  202. void image_contrast()
  203. {
  204.     float contrast= 0.5;
  205.  
  206.     GdkPixbuf *result_pixbuf;
  207.     char *sp, *dp;
  208.     int width, height, line, result_rowstride, mypic_rowstride, bytes_per_pixel;
  209.  
  210.     g_return_val_if_fail ((contrast > -1.0 ) && (contrast < 1.0 ), NULL);
  211.  
  212.     result_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (mypic),
  213.                                     gdk_pixbuf_get_has_alpha (mypic),
  214.                                     gdk_pixbuf_get_bits_per_sample (mypic),
  215.                                     gdk_pixbuf_get_width (mypic),
  216.                                     gdk_pixbuf_get_height (mypic));
  217.  
  218.     width = gdk_pixbuf_get_width (result_pixbuf);
  219.     height = gdk_pixbuf_get_height (result_pixbuf);
  220.  
  221.     result_rowstride = gdk_pixbuf_get_rowstride (result_pixbuf);
  222.     mypic_rowstride = gdk_pixbuf_get_rowstride (mypic);
  223.  
  224.     bytes_per_pixel = gdk_pixbuf_get_has_alpha (result_pixbuf) ? 4 : 3;
  225.  
  226.     sp = gdk_pixbuf_get_pixels (mypic);
  227.     dp = gdk_pixbuf_get_pixels (result_pixbuf);
  228.  
  229.     for (line = 0; line < height; line ++)
  230.     {
  231.         char *sq = sp;
  232.         char *dq = dp;
  233.         int i;
  234.  
  235.         for (i = 0; i < width; i ++)
  236.         {
  237.             dq[0] = apply_contrast (sq[0], contrast);
  238.             dq[1] = apply_contrast (sq[1], contrast);
  239.             dq[2] = apply_contrast (sq[2], contrast);
  240.  
  241.             dq += bytes_per_pixel;
  242.             sq += bytes_per_pixel;
  243.         }
  244.  
  245.         sp += mypic_rowstride;
  246.         dp += result_rowstride;
  247.     }
  248.  
  249.     window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  250.     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  251.     gtk_window_set_default_size(GTK_WINDOW(window2), 600, 400);
  252.     img2 = gtk_image_new_from_pixbuf(result_pixbuf);
  253.     gtk_container_add(GTK_CONTAINER(window2), img2);
  254.     gtk_widget_show_all(window2);
  255. }
  256.  
  257. void image_brightness()
  258. {
  259.     float brightness=0.1;
  260.     GdkPixbuf *result_pixbuf;
  261.     char *sp, *dp;
  262.     int width, height, line, result_rowstride, mypic_rowstride, bytes_per_pixel;
  263.  
  264.     g_return_val_if_fail ((brightness > -1.0 ) && (brightness < 1.0 ), NULL);
  265.     result_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (mypic),
  266.                                     gdk_pixbuf_get_has_alpha (mypic),
  267.                                     gdk_pixbuf_get_bits_per_sample (mypic),
  268.                                     gdk_pixbuf_get_width (mypic),
  269.                                     gdk_pixbuf_get_height (mypic));
  270.  
  271.     width = gdk_pixbuf_get_width (result_pixbuf);
  272.     height = gdk_pixbuf_get_height (result_pixbuf);
  273.  
  274.     result_rowstride = gdk_pixbuf_get_rowstride (result_pixbuf);
  275.     mypic_rowstride = gdk_pixbuf_get_rowstride (mypic);
  276.  
  277.     bytes_per_pixel = gdk_pixbuf_get_has_alpha (result_pixbuf) ? 4 : 3;
  278.  
  279.     sp = gdk_pixbuf_get_pixels (mypic);
  280.     dp = gdk_pixbuf_get_pixels (result_pixbuf);
  281.  
  282.     for (line = 0; line < height; line ++)
  283.     {
  284.         char *sq = sp;
  285.         char *dq = dp;
  286.         int i;
  287.  
  288.         for (i = 0; i < width; i ++)
  289.         {
  290.             dq[0] = apply_brightness (sq[0], brightness);
  291.             dq[1] = apply_brightness (sq[1], brightness);
  292.             dq[2] = apply_brightness (sq[2], brightness);
  293.  
  294.             dq += bytes_per_pixel;
  295.             sq += bytes_per_pixel;
  296.         }
  297.  
  298.         sp += mypic_rowstride;
  299.         dp += result_rowstride;
  300.     }
  301.  
  302.     window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  303.     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  304.     gtk_window_set_default_size(GTK_WINDOW(window2), 600, 400);
  305.     img2 = gtk_image_new_from_pixbuf(result_pixbuf);
  306.     gtk_container_add(GTK_CONTAINER(window2), img2);
  307.     gtk_widget_show_all(window2);
  308. }
  309.  
  310. void image_negative()
  311. {
  312.     GdkPixbuf *result_pixbuf;
  313.     char *sp, *dp;
  314.     int width, height, line, result_rowstride, mypic_rowstride, bytes_per_pixel;
  315.  
  316.     result_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (mypic),
  317.                                     gdk_pixbuf_get_has_alpha (mypic),
  318.                                     gdk_pixbuf_get_bits_per_sample (mypic),
  319.                                     gdk_pixbuf_get_width (mypic),
  320.                                     gdk_pixbuf_get_height (mypic));
  321.  
  322.     width = gdk_pixbuf_get_width (result_pixbuf);
  323.     height = gdk_pixbuf_get_height (result_pixbuf);
  324.  
  325.     result_rowstride = gdk_pixbuf_get_rowstride (result_pixbuf);
  326.     mypic_rowstride = gdk_pixbuf_get_rowstride (mypic);
  327.  
  328.     bytes_per_pixel = gdk_pixbuf_get_has_alpha (result_pixbuf) ? 4 : 3;
  329.  
  330.     sp = gdk_pixbuf_get_pixels (mypic);
  331.     dp = gdk_pixbuf_get_pixels (result_pixbuf);
  332.  
  333.     for (line = 0; line < height; line ++)
  334.     {
  335.         char *sq = sp;
  336.         char *dq = dp;
  337.         int i;
  338.  
  339.         for (i = 0; i < width; i ++)
  340.         {
  341.             dq[0] = 255 - sq[0];
  342.             dq[1] = 255 - sq[1];
  343.             dq[2] = 255 - sq[2];
  344.  
  345.             dq += bytes_per_pixel;
  346.             sq += bytes_per_pixel;
  347.         }
  348.  
  349.         sp += mypic_rowstride;
  350.         dp += result_rowstride;
  351.     }
  352.  
  353.     window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  354.     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  355.     gtk_window_set_default_size(GTK_WINDOW(window2), 600, 400);
  356.     img2 = gtk_image_new_from_pixbuf(result_pixbuf);
  357.     gtk_container_add(GTK_CONTAINER(window2), img2);
  358.     gtk_widget_show_all(window2);
  359. }
  360.  
  361. static int keyboard_keys(GtkWidget *futile, GdkEventKey *lol, gpointer data)
  362. {
  363.     if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_q || lol->keyval == GDK_KEY_Q))
  364.         exit (0);
  365.     if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_O || lol->keyval == GDK_KEY_o))
  366.         open_file();
  367.     if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_P || lol->keyval == GDK_KEY_p))
  368.         show_properties();
  369.     if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_B || lol->keyval == GDK_KEY_b))
  370.         image_brightness();
  371.     if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_C || lol->keyval == GDK_KEY_c))
  372.         image_contrast();
  373.     if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_G || lol->keyval == GDK_KEY_g))
  374.         image_grayscale();
  375.     if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_N || lol->keyval == GDK_KEY_n))
  376.         image_negative();
  377.     return TRUE;
  378. }
  379.  
  380. int main(int argc, char*argv[])
  381. {
  382.     /*---------------------initialization-----------------------------*/
  383.     gtk_init(&argc, &argv);
  384.     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  385.     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  386.     gtk_window_set_default_size(GTK_WINDOW(window), 800, 500);
  387.     gtk_window_set_title(GTK_WINDOW(window), "Fireflight Image Viewer 1.052");
  388.  
  389.     /*---------------------go to creat menu----------------------------*/
  390.     create_menu();
  391.     /*---------------------default window-------------------------------*/
  392.     char picture_name[] = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg";
  393.     img = gtk_image_new_from_file(picture_name);
  394.     mypic = gdk_pixbuf_new_from_file(picture_name, NULL);
  395.     gtk_box_pack_start(GTK_BOX(box), img, TRUE, TRUE, 0);
  396.     gtk_container_add(GTK_CONTAINER(window), box);
  397.  
  398.     /*----------------------connecting signals----------------------------*/
  399.     g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
  400.     g_signal_connect(G_OBJECT(open), "activate", G_CALLBACK(open_file), NULL);
  401.     g_signal_connect(G_OBJECT(grayscale), "activate", G_CALLBACK(image_grayscale ), NULL);
  402.     g_signal_connect(G_OBJECT(contrast), "activate", G_CALLBACK(image_contrast), NULL);
  403.     g_signal_connect(G_OBJECT(brightness), "activate", G_CALLBACK(image_brightness), NULL);
  404.     g_signal_connect(G_OBJECT(negative), "activate", G_CALLBACK(image_negative), NULL);
  405.     g_signal_connect(G_OBJECT(properties), "activate", G_CALLBACK(show_properties), NULL);
  406.  
  407.     g_signal_connect(G_OBJECT(about), "activate", G_CALLBACK(show_about), NULL);
  408.     g_signal_connect(G_OBJECT(quit), "activate",G_CALLBACK(gtk_main_quit), NULL);
  409.     g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(keyboard_keys), window);
  410.     /*------------------------go to gtk main----------------------------------*/
  411.     gtk_widget_show_all(window);
  412.     gtk_main();
  413.  
  414.     return 0;
  415. }
  416.  
  417.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement