Advertisement
palmerstone

lol

Nov 26th, 2011
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  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;
  10. GtkWidget *menubar, *filemenu, *help, *helpmenu;
  11. GtkWidget *file, *quit, *open, *save, *properties, *about, *grayscale, *brightness, *contrast, *negative;
  12. GtkWidget *img, *img2, *img3;
  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. int height, width;
  101. int i=0,j=0;
  102. int rowstride=0;
  103. int bitperpixel=0;
  104. double avg=0;
  105. gchar *pixel;
  106.  
  107.  
  108. if(gdk_pixbuf_get_bits_per_sample(mypic)!=8)
  109. return;
  110.  
  111. bitperpixel = 3;
  112. height = gdk_pixbuf_get_height(mypic);
  113. width = gdk_pixbuf_get_width(mypic);
  114. pixel = gdk_pixbuf_get_pixels(mypic);
  115. rowstride=width*bitperpixel;
  116. for(i=0; i<height; i++)
  117. for(j=0; j<rowstride; j+=bitperpixel)
  118. {
  119. /*-----------------getting grayscale value--------------------*/
  120. avg+=pixel[i*rowstride + j+0]+pixel[i*rowstride + j+1]+pixel[i*rowstride + j+2];
  121. avg/=3.00;
  122. avg=((int)(avg))%256;
  123.  
  124. pixel[i*rowstride + j+0]=(int)avg;
  125. pixel[i*rowstride + j+1]=(int)avg;
  126. pixel[i*rowstride + j+2]=(int)avg;
  127.  
  128. }
  129. return;
  130.  
  131. }
  132.  
  133. void image_contrast()
  134. {
  135.  
  136. }
  137.  
  138. void image_brightness()
  139. {
  140. int height, width;
  141. int i = 0, j = 0;
  142. int rowstride = 0;
  143. int bitperpixel = 0;
  144. gchar *pixel;
  145.  
  146.  
  147. if(gdk_pixbuf_get_bits_per_sample(mypic) != 8)
  148. return;
  149.  
  150. bitperpixel = 3;
  151. height = gdk_pixbuf_get_height(mypic);
  152. width = gdk_pixbuf_get_width(mypic);
  153. pixel = gdk_pixbuf_get_pixels(mypic);
  154. rowstride = width*bitperpixel;
  155.  
  156. for(i=0; i<height; i++)
  157. for(j=0; j<rowstride; j += bitperpixel)
  158. {
  159.  
  160. /*--------------------manipulating red-------------------------*/
  161. if((pixel[i*rowstride + j+0]+offset) >= 0xff)
  162. pixel[i*rowstride + j+0]=0xff;
  163. else if((pixel[i*rowstride + j+0]-offset) < 0)
  164. pixel[i*rowstride + j+0]=0;
  165. else
  166. pixel[i*rowstride + j+0]+=offset;
  167.  
  168.  
  169. /*----------------------manipuilating green----------------------*/
  170. if((pixel[i*rowstride + j+1]+offset) >= 0xff)
  171. pixel[i*rowstride + j+1]=0xff;
  172. else if((pixel[i*rowstride + j+1]-offset) < 0)
  173. pixel[i*rowstride + j+1]=0;
  174. else
  175. pixel[i*rowstride + j+1]+=offset;
  176.  
  177.  
  178. /*----------------------manipilating blue--------------------------*/
  179. if((pixel[i*rowstride + j+2]+offset) >= 0xff)
  180. pixel[i*rowstride + j+2]=0xff;
  181. else if((pixel[i*rowstride + j+2]-offset) < 0)
  182. pixel[i*rowstride + j+2]=0;
  183. else
  184. pixel[i*rowstride + j+2]+=offset;
  185. }
  186. offset += 5;
  187. return;
  188. }
  189.  
  190. void image_negative()
  191. {
  192. int height, width;
  193. int i = 0,j = 0;
  194. int rowstride = 0;
  195. int bitperpixel = 0;
  196. gchar *pixel;
  197.  
  198.  
  199. if(gdk_pixbuf_get_bits_per_sample(mypic) != 8)
  200. return;
  201. bitperpixel = 3;
  202. height = gdk_pixbuf_get_height(mypic);
  203. width = gdk_pixbuf_get_width(mypic);
  204. pixel = gdk_pixbuf_get_pixels(mypic);
  205. rowstride = width*bitperpixel;
  206.  
  207. for(i=0; i<height; i++)
  208. for(j=0; j<rowstride; j+= bitperpixel)
  209. {
  210. /*------------------getting negative pixel value----------------------*/
  211. pixel[i*rowstride + j+0]=255-pixel[i*rowstride + j+0];
  212. pixel[i*rowstride + j+1]=255-pixel[i*rowstride + j+1];
  213. pixel[i*rowstride + j+2]=255-pixel[i*rowstride + j+2];
  214. }
  215. return;
  216. }
  217.  
  218. static int keyboard_keys(GtkWidget *futile, GdkEventKey *lol, gpointer data)
  219. {
  220. if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_q || lol->keyval == GDK_KEY_Q))
  221. exit (0);
  222. if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_O || lol->keyval == GDK_KEY_o))
  223. open_file();
  224. if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_P || lol->keyval == GDK_KEY_p))
  225. show_properties();
  226. if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_B || lol->keyval == GDK_KEY_b))
  227. image_brightness();
  228. if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_C || lol->keyval == GDK_KEY_c))
  229. image_contrast();
  230. if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_G || lol->keyval == GDK_KEY_g))
  231. image_grayscale();
  232. if (lol->state == GDK_CONTROL_MASK && (lol->keyval == GDK_KEY_N || lol->keyval == GDK_KEY_n))
  233. image_negative();
  234. return TRUE;
  235. }
  236.  
  237. int main(int argc, char*argv[])
  238. {
  239. /*---------------------initialization-----------------------------*/
  240. gtk_init(&argc, &argv);
  241. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  242. gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  243. gtk_window_set_default_size(GTK_WINDOW(window), 800, 500);
  244. gtk_window_set_title(GTK_WINDOW(window), "Fireflight Image Viewer 1.052");
  245.  
  246. /*---------------------go to creat menu----------------------------*/
  247. create_menu();
  248. /*---------------------default window-------------------------------*/
  249. char picture_name[] = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg";
  250. img = gtk_image_new_from_file(picture_name);
  251. mypic = gdk_pixbuf_new_from_file(picture_name, NULL);
  252. gtk_box_pack_start(GTK_BOX(box), img, TRUE, TRUE, 0);
  253. gtk_container_add(GTK_CONTAINER(window), box);
  254.  
  255. /*----------------------connecting signals----------------------------*/
  256. g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
  257. g_signal_connect(G_OBJECT(open), "activate", G_CALLBACK(open_file), NULL);
  258. g_signal_connect(G_OBJECT(grayscale), "activate", G_CALLBACK(image_grayscale), NULL);
  259. g_signal_connect(G_OBJECT(contrast), "activate", G_CALLBACK(image_contrast), NULL);
  260. g_signal_connect(G_OBJECT(brightness), "activate", G_CALLBACK(image_brightness), NULL);
  261. g_signal_connect(G_OBJECT(negative), "activate", G_CALLBACK(image_negative), NULL);
  262. g_signal_connect(G_OBJECT(properties), "activate", G_CALLBACK(show_properties), NULL);
  263.  
  264. g_signal_connect(G_OBJECT(about), "activate", G_CALLBACK(show_about), NULL);
  265. g_signal_connect(G_OBJECT(quit), "activate",G_CALLBACK(gtk_main_quit), NULL);
  266. g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(keyboard_keys), window);
  267. /*------------------------go to gtk main----------------------------------*/
  268. gtk_widget_show_all(window);
  269. gtk_main();
  270.  
  271. return 0;
  272. }
  273.  
  274.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement