GdkPixbuf * get_pixbuf_from_data(unsigned char * data, int scale) { unsigned char * tmpdata = malloc(128 * 128 * 3); int i; for(i = 0; i < 128 * 128; i++) { if(data[i] > 3) { tmpdata[i * 3] = colors[data[i]].r; tmpdata[i * 3 + 1] = colors[data[i]].g; tmpdata[i * 3 + 2] = colors[data[i]].b; } else { int x = i % 128, y = i / 128; x /= 4; y /= 4; tmpdata[i * 3] = ((x + (y % 2)) % 2) ? 0xFF : 0xAA; tmpdata[i * 3 + 1] = ((x + (y % 2)) % 2) ? 0xFF : 0xAA; tmpdata[i * 3 + 2] = ((x + (y % 2)) % 2) ? 0xFF : 0xAA; } } GdkPixbuf * fdata = image_from_data(tmpdata, scale); free(tmpdata); return fdata; }
static void button_click(gpointer data) { if(strcmp("button.open", (char *)data) == 0) { GtkWidget * dialog; dialog = gtk_file_chooser_dialog_new("Open file", GTK_WINDOW(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) { char * file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); if(srecmpend(".dat", file) == 0) { add_buffer(); image_load_map(file); } else if(srecmpend(".bmp", file) == 0 || srecmpend(".png", file) == 0 || srecmpend(".jpg", file) == 0 || srecmpend(".jpeg", file) == 0 || srecmpend(".gif", file) == 0) { GError * err = NULL; add_buffer(); if(gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(FSD_checkbox))) generate_image_dithered(mdata[current_buffer], file, colors, &err); else generate_image(mdata[current_buffer], file, colors, &err); if(err != NULL) { information("Error while loading image file!"); printf("%s\n", err->message); g_error_free(err); } set_image(); } else if(srecmpend(".imtm", file) == 0) { add_buffer(); load_raw_map(file, mdata[current_buffer]); set_image(); } else information("File format not supported!"); } gtk_widget_destroy(dialog); } else if(strcmp("button.save", (char *)data) == 0) { if(mdata[current_buffer] == NULL) return; GtkWidget * dialog; dialog = gtk_file_chooser_dialog_new ("Save Map", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER (dialog), "map_0.dat"); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { char * file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); printf("%s\n", file); if(srecmpend(".dat", file) == 0) save_map(file); } gtk_widget_destroy(dialog); printf("bracket cleared\n"); } else if(strcmp("button.exp_img", (char *)data) == 0) { if(mdata[current_buffer] == NULL) return; GtkWidget * dialog; dialog = gtk_file_chooser_dialog_new ("Export Image of Map", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER (dialog), "map.png"); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { char * file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); printf("%s\n", file); unsigned char * data = malloc(128 * 128 * 3); int i; for(i = 0; i < 128 * 128; i++) { if(mdata[current_buffer][i] > 3) { data[i * 3] = colors[mdata[current_buffer][i]].r; data[i * 3 + 1] = colors[mdata[current_buffer][i]].g; data[i * 3 + 2] = colors[mdata[current_buffer][i]].b; } else { int x = i % 128, y = i / 128; x /= 4; y /= 4; data[i * 3] = ((x + (y % 2)) % 2) ? 0xFF : 0xAA; data[i * 3 + 1] = ((x + (y % 2)) % 2) ? 0xFF : 0xAA; data[i * 3 + 2] = ((x + (y % 2)) % 2) ? 0xFF : 0xAA; } } GdkPixbuf * spixbuf = image_from_data(data, 0); free(data); GError * err = NULL; gdk_pixbuf_save(spixbuf, file, "png", &err, "compression", "9", NULL); if (err != NULL) { /* Report error to user, and free error */ printf("Error while saving: %s\n", err->message); g_error_free(err); } g_object_unref(spixbuf); } gtk_widget_destroy(dialog); } else if(strcmp("button.save_rm", (char *)data) == 0) { if(mdata == NULL) return; GtkWidget * dialog; dialog = gtk_file_chooser_dialog_new("Save Map", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER (dialog), TRUE); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER (dialog), "map.imtm"); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { char * file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); printf("%s\n", file); save_raw_map(file, mdata[current_buffer]); } gtk_widget_destroy(dialog); } else if(strcmp("button.palette", (char *)data) == 0) { add_buffer(); generate_palette(mdata[current_buffer]); set_image(); } else if(strcmp("button.random_noise", (char *)data) == 0) { add_buffer(); generate_random_noise(mdata[current_buffer]); set_image(); } else if(strcmp("button.mandelbrot", (char *)data) == 0) { add_buffer(); generate_mandelbrot(mdata[current_buffer]); set_image(); } else if(strcmp("button.julia", (char *)data) == 0) { add_buffer(); generate_julia(mdata[current_buffer], 0.5, 0.5); set_image(); } else if(strcmp("button.from_clipboard", (char *)data) == 0) { GtkClipboard * clipboard; clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); gtk_clipboard_request_image(clipboard, clipboard_callback, NULL); } else printf("Unhandeled button press: %s\n", (char *)data); }
static void button_click(gpointer data) { if((size_t)data == ITEM_SIGNAL_OPEN) { GtkWidget * dialog; dialog = gtk_file_chooser_dialog_new("Open file", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_OPEN, _("_Cancel"), GTK_RESPONSE_CANCEL, _("_Open"), GTK_RESPONSE_ACCEPT, NULL); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { char * file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); if(srecmpend(".dat", file) == 0) { add_buffer(); image_load_map(file); } else if(srecmpend(".bmp", file) == 0 || srecmpend(".png", file) == 0 || srecmpend(".jpg", file) == 0 || srecmpend(".jpeg", file) == 0 || srecmpend(".gif", file) == 0) { GError * err = NULL; add_buffer(); if(gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(FSD_checkbox))) generate_image_dithered(mdata[current_buffer], 128, 128,gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(YUV_checkbox)), file, colors, &err); else generate_image(mdata[current_buffer], 128, 128, gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(YUV_checkbox)), file, colors, &err); if(err != NULL) { information("Error while loading image file!"); printf("%s\n", err->message); g_error_free(err); } set_image(); } else if(srecmpend(".imtm", file) == 0) { add_buffer(); load_raw_map(file, mdata[current_buffer]); set_image(); } else information("File format not supported!"); } gtk_widget_destroy(dialog); } if((size_t)data == ITEM_SIGNAL_OPEN_GRID_IMAGE) { GtkWidget * dialog; dialog = gtk_file_chooser_dialog_new("Open file", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_OPEN, _("_Cancel"), GTK_RESPONSE_CANCEL, _("_Open"), GTK_RESPONSE_ACCEPT, NULL); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { char * file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); if(srecmpend(".bmp", file) == 0 || srecmpend(".png", file) == 0 || srecmpend(".jpg", file) == 0 || srecmpend(".jpeg", file) == 0 || srecmpend(".gif", file) == 0) { GError * err = NULL; int width = 1, height = 1; int i, j; int pi, pj; { GtkWidget * dialog = gtk_dialog_new_with_buttons("Split Image", GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, _("_OK"), GTK_RESPONSE_ACCEPT, NULL); GtkWidget * content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); GtkWidget * hbox; GtkWidget * label; #ifdef GTK2 hbox = gtk_hbox_new(FALSE, 0); #else hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); #endif label = gtk_label_new("width"); GtkWidget * width_entry = gtk_entry_new(); gtk_entry_set_text(GTK_ENTRY(width_entry), "1"); gtk_container_add(GTK_CONTAINER(hbox), width_entry); gtk_container_add(GTK_CONTAINER(hbox), label); gtk_container_add(GTK_CONTAINER(content_area), hbox); #ifdef GTK2 hbox = gtk_hbox_new(FALSE, 0); #else hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); #endif label = gtk_label_new("height"); GtkWidget * height_entry = gtk_entry_new(); gtk_entry_set_text(GTK_ENTRY(height_entry), "1"); gtk_container_add(GTK_CONTAINER(hbox), height_entry); gtk_container_add(GTK_CONTAINER(hbox), label); gtk_container_add(GTK_CONTAINER(content_area), hbox); gtk_widget_show_all(dialog); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { width = atoi((char *)gtk_entry_get_text(GTK_ENTRY(width_entry))); height = atoi((char *)gtk_entry_get_text(GTK_ENTRY(height_entry))); } gtk_widget_destroy(dialog); } unsigned char tmp_buffer[width * height * 128 * 128]; if(gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(FSD_checkbox))) generate_image_dithered(tmp_buffer, width * 128, height * 128, gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(YUV_checkbox)), file, colors, &err); else generate_image(tmp_buffer, width * 128, height * 128, gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(YUV_checkbox)), file, colors, &err); if(err != NULL) { information("Error while loading image file!"); printf("%s\n", err->message); g_error_free(err); } for(i = 0; i < width; i++) for(j = 0; j < height; j++) { add_buffer(); for(pi = 0; pi < 128; pi++) for(pj = 0; pj < 128; pj++) { int buffer_i, buffer_j; buffer_i = i * 128 + pi; buffer_j = j * 128 + pj; mdata[current_buffer][pi + pj * 128] = tmp_buffer[buffer_i + (width * 128) * buffer_j]; } } set_image(); } else information("File format not supported!"); } gtk_widget_destroy(dialog); } else if((size_t)data == ITEM_SIGNAL_SAVE) { if(mdata[current_buffer] == NULL) return; GtkWidget * dialog; dialog = gtk_file_chooser_dialog_new ("Save Map", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_SAVE, _("_Cancel"), GTK_RESPONSE_CANCEL, _("_Save"), GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER (dialog), "map_0.dat"); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { char * file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); printf("%s\n", file); if(srecmpend(".dat", file) == 0) { save_map(file); sprintf(last_file, "%s", file); } } gtk_widget_destroy(dialog); printf("bracket cleared\n"); } else if((size_t)data == ITEM_SIGNAL_SAVE_INCREMENT) { int i = 0; char * tmp = last_file; char * basename_s, * dirname_s; if(mdata[current_buffer] == NULL) return; basename_s = custom_basename(tmp); dirname_s = last_file; if(basename_s != NULL) { tmp = basename_s; if(strncmp("map_", tmp, 4) == 0) { tmp += 4; i = strtol(tmp, &tmp, 10) + 1; if(strcmp(".dat", tmp) == 0) { #ifdef OS_LINUX sprintf(last_file, "%s/map_%i.dat", dirname_s, i); #else sprintf(last_file, "%s\\map_%i.dat", dirname_s, i); #endif save_map(last_file); /* save_map(last_file); */ } } } } else if((size_t)data == ITEM_SIGNAL_SAVE_ALL) { int old_current_buffer = current_buffer; GtkWidget * dialog; int i = 0; char * tmp; char * basename_s, * dirname_s; /* Gets first file name */ dialog = gtk_file_chooser_dialog_new ("Save Map", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_SAVE, _("_Cancel"), GTK_RESPONSE_CANCEL, _("_Save"), GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER (dialog), "map_0.dat"); if(gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT) { gtk_widget_destroy(dialog); return; } char * file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); if(srecmpend(".dat", file) != 0) { gtk_widget_destroy(dialog); return; } sprintf(last_file, "%s", file); gtk_widget_destroy(dialog); /* Gets the directory and the file name */ basename_s = custom_basename(last_file); dirname_s = malloc(strlen(last_file)+8); /* 8 is a magic number! Naa jk, it is just to make sure we don't overflow */ if (dirname_s == NULL) return; strcpy(dirname_s, last_file); memset(dirname_s+(strlen(dirname_s)-strlen(basename_s)), strlen(basename_s), '\0'); if(basename_s == NULL) return; tmp = basename_s; /* Gets the number in the file name, 10 for 'map_10.dat' */ if(strncmp("map_", tmp, 4) != 0) return; tmp += 4; i = strtol(tmp, &tmp, 10); /* Saves all the buffers */ for (current_buffer = 0; current_buffer < BUFFER_COUNT; current_buffer++) { if(mdata[current_buffer] == NULL) break; #ifdef OS_LINUX sprintf(last_file, "%s/map_%i.dat", dirname_s, i); #else sprintf(last_file, "%s\\map_%i.dat", dirname_s, i); #endif save_map(last_file); i++; } /* Restores the selection to the right buffer and frees the allocated memory */ current_buffer = old_current_buffer; free(dirname_s); } else if((size_t)data == ITEM_SIGNAL_EXPORT_IMAGE) { if(mdata[current_buffer] == NULL) return; GtkWidget * dialog; dialog = gtk_file_chooser_dialog_new ("Export Image of Map", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_SAVE, _("_Cancel"), GTK_RESPONSE_CANCEL, _("_Save"), GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER (dialog), "map.png"); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { char * file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); printf("%s\n", file); unsigned char * data = malloc(128 * 128 * 3); int i; for(i = 0; i < 128 * 128; i++) { if(mdata[current_buffer][i] > 3) { data[i * 3] = colors[mdata[current_buffer][i]].r; data[i * 3 + 1] = colors[mdata[current_buffer][i]].g; data[i * 3 + 2] = colors[mdata[current_buffer][i]].b; } else { int x = i % 128, y = i / 128; x /= 4; y /= 4; data[i * 3] = ((x + (y % 2)) % 2) ? 0xFF : 0xAA; data[i * 3 + 1] = ((x + (y % 2)) % 2) ? 0xFF : 0xAA; data[i * 3 + 2] = ((x + (y % 2)) % 2) ? 0xFF : 0xAA; } } GdkPixbuf * spixbuf = image_from_data(data, 0); free(data); GError * err = NULL; gdk_pixbuf_save(spixbuf, file, "png", &err, "compression", "9", NULL); if (err != NULL) { /* Report error to user, and free error */ printf("Error while saving: %s\n", err->message); g_error_free(err); } g_object_unref(spixbuf); } gtk_widget_destroy(dialog); } else if((size_t)data == ITEM_SIGNAL_SAVE_RM) { if(mdata == NULL) return; GtkWidget * dialog; dialog = gtk_file_chooser_dialog_new("Save Map", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_SAVE, _("_Cancel"), GTK_RESPONSE_CANCEL, _("_Save"), GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER (dialog), TRUE); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER (dialog), "map.imtm"); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { char * file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); printf("%s\n", file); save_raw_map(file, mdata[current_buffer]); } gtk_widget_destroy(dialog); } else if((size_t)data == ITEM_SIGNAL_GENERATE_PALETTE) { add_buffer(); generate_palette(mdata[current_buffer]); set_image(); } else if((size_t)data == ITEM_SIGNAL_GENERATE_RANDOM_NOISE) { add_buffer(); generate_random_noise(mdata[current_buffer]); set_image(); } else if((size_t)data == ITEM_SIGNAL_GENERATE_MANDELBROT) { add_buffer(); generate_mandelbrot(mdata[current_buffer]); set_image(); } else if((size_t)data == ITEM_SIGNAL_GENERATE_JULIA) { add_buffer(); generate_julia(mdata[current_buffer], 0.5, 0.5); set_image(); } else if((size_t)data == ITEM_SIGNAL_GENERATE_FROM_CLIPBOARD) { GtkClipboard * clipboard; clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); gtk_clipboard_request_image(clipboard, clipboard_callback, NULL); } else if((size_t)data == ITEM_SIGNAL_WORLD_RENDER_ITEM) { GtkWidget * dialog = gtk_dialog_new_with_buttons("Render World Map", GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, _("_OK"), GTK_RESPONSE_ACCEPT, _("_Cancel"), GTK_RESPONSE_REJECT, NULL); GtkWidget * content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); GtkWidget * scale_entry = gtk_entry_new(); gtk_entry_set_text(GTK_ENTRY(scale_entry), "3"); gtk_container_add(GTK_CONTAINER(content_area), scale_entry); GtkWidget * xpos_entry = gtk_entry_new(); gtk_entry_set_text(GTK_ENTRY(xpos_entry), "0"); gtk_container_add(GTK_CONTAINER(content_area), xpos_entry); GtkWidget * zpos_entry = gtk_entry_new(); gtk_entry_set_text(GTK_ENTRY(zpos_entry), "0"); gtk_container_add(GTK_CONTAINER(content_area), zpos_entry); GtkWidget * directory_entry = gtk_entry_new(); gtk_entry_set_text(GTK_ENTRY(directory_entry), MINECRAFT_PATH); gtk_container_add(GTK_CONTAINER(content_area), directory_entry); gtk_widget_show_all(dialog); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { int scale = atoi((char *)gtk_entry_get_text(GTK_ENTRY(scale_entry))); char * path = (char *)gtk_entry_get_text(GTK_ENTRY(directory_entry)); int x = atoi((char *)gtk_entry_get_text(GTK_ENTRY(xpos_entry))); int z = atoi((char *)gtk_entry_get_text(GTK_ENTRY(zpos_entry))); int rs = pow(2, scale) * 128; block_info_t * blocks = read_region_files(path, x - (rs / 2), z - (rs / 2), rs, rs); add_buffer(); render_map(blocks, mdata[current_buffer], scale); free(blocks); mdata_info[current_buffer].scale = scale; mdata_info[current_buffer].xpos = x; mdata_info[current_buffer].zpos = z; mdata_info[current_buffer].dimension = 0; set_image(); } gtk_widget_destroy(dialog); } else if((size_t)data == ITEM_SIGNAL_CLEAN) { while(mdata[1] != NULL) { remove_buffer(0); } /* set_image(); */ } else if((size_t)data == ITEM_SIGNAL_QUIT) { kill_window(NULL, NULL, NULL); } else printf("Unhandeled button press: %i\n", (int)(size_t)data); }