1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
|
/*
* This file is part of Siril, an astronomy image processor.
* Copyright (C) 2005-2011 Francois Meyer (dulle at free.fr)
* Copyright (C) 2012-2019 team free-astro (see more in AUTHORS file)
* Reference site is https://free-astro.org/index.php/Siril
*
* Siril is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Siril is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Siril. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include "core/siril.h"
#include "core/proto.h"
#include "core/processing.h"
#include "io/conversion.h"
#include "io/films.h"
#include "io/sequence.h"
#include "io/ser.h"
#include "gui/callbacks.h"
#include "gui/message_dialog.h"
#include "gui/progress_and_log.h"
#include "algos/demosaicing.h"
#define MAX_OF_EXTENSIONS 50 // actual size of supported_extensions
static gchar *destroot = NULL;
static unsigned int convflags = CONV1X3; // default
static unsigned int supported_filetypes = 0; // initialized by initialize_converters()
// NULL-terminated array, initialized by initialize_converters(), used only by stat_file
char **supported_extensions;
supported_raw_list supported_raw[] = {
{"dng", "Adobe", BAYER_FILTER_RGGB},
{"mos", "Aptus", BAYER_FILTER_RGGB},
{"cr2", "Canon", BAYER_FILTER_RGGB},
{"crw", "Canon", BAYER_FILTER_RGGB},
{"bay", "Casio", BAYER_FILTER_NONE}, // Not tested
{"erf", "Epson", BAYER_FILTER_RGGB},
{"raf", "Fuji", BAYER_FILTER_GBRG}, // Not really supported, specially XTRANS
{"3fr", "Hasselblad", BAYER_FILTER_GRBG}, // GRBG, RGGB
{"kdc", "Kodak", BAYER_FILTER_GRBG},
{"dcr", "Kodak", BAYER_FILTER_GRBG},
{"mef", "Mamiya", BAYER_FILTER_RGGB},
{"mrw", "Minolta", BAYER_FILTER_RGGB},
{"nef", "Nikon", BAYER_FILTER_RGGB},
{"nrw", "Nikon", BAYER_FILTER_RGGB},
{"orf", "Olympus", BAYER_FILTER_GRBG},
{"raw", "Leica", BAYER_FILTER_RGGB},
{"rw2", "Panasonic", BAYER_FILTER_BGGR},
{"pef", "Pentax", BAYER_FILTER_BGGR},
{"ptx", "Pentax", BAYER_FILTER_NONE}, // Not tested
{"x3f", "Sigma", BAYER_FILTER_NONE}, // Not supported yet
{"srw", "Samsung", BAYER_FILTER_BGGR},
{"arw", "Sony", BAYER_FILTER_RGGB}
};
char *filter_pattern[] = {
"RGGB",
"BGGR",
"GBRG",
"GRBG",
"RBGBRGGGRGGBGGBGGRBRGRBGGGBGGRGGRGGB", /* XTRANS */
"GBGGRGRGRBGBGBGGRGGRGGBGBGBRGRGRGGBG",
"GGRGGBGGBGGRBRGRBGGGBGGRGGRGGBRBGBRG"
};
enum {
COLUMN_CONV_FILENAME, // gchar[]
COLUMN_CONV_DATE, // gchar[]
N_COLUMNS
};
static gboolean end_convert_idle(gpointer p);
int get_nb_raw_supported() {
return sizeof(supported_raw) / sizeof(supported_raw_list);
}
/* This function is used with command line only */
void list_format_available() {
puts("=======================================================");
puts("[ Supported image file formats ]");
puts("=======================================================");
puts("FITS\t(*.fit, *.fits, *.fts)");
puts("BMP\t(*.bmp)");
puts("NetPBM\t(*.ppm, *.pgm, *.pnm)");
puts("PIC\t(*.pic)");
#ifdef HAVE_LIBRAW
printf("RAW\t(");
int i, nb_raw;
nb_raw = get_nb_raw_supported();
for (i = 0; i < nb_raw; i++) {
printf("*.%s",supported_raw[i].extension);
if (i != nb_raw - 1) printf(", ");
}
printf(")\n");
#endif
#ifdef HAVE_LIBTIFF
puts("TIFF\t(*.tif, *.tiff)");
#endif
#ifdef HAVE_LIBJPEG
puts("JPEG\t(*.jpg, *.jpeg)");
#endif
#ifdef HAVE_LIBPNG
puts("PNG\t(*.png)");
#endif
}
static gint sort_conv_tree(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
gpointer user_data) {
gchar *name_a, *name_b;
gchar *collate_key1, *collate_key2;
gint ret;
gtk_tree_model_get(model, a, 0, &name_a, -1);
gtk_tree_model_get(model, b, 0, &name_b, -1);
collate_key1 = g_utf8_collate_key_for_filename(name_a, strlen(name_a));
collate_key2 = g_utf8_collate_key_for_filename(name_b, strlen(name_b));
ret = g_strcmp0(collate_key1, collate_key2);
g_free(collate_key1);
g_free(collate_key2);
g_free(name_a);
g_free(name_b);
return ret;
}
// input is destroot
static char *create_sequence_filename(int counter, char *output, int outsize) {
const char *ext = get_filename_ext(destroot);
if (ext) {
/* we need to insert a number before the extension */
gchar *the_ext = g_strdup(ext);
gchar *the_root = g_strdup(destroot);
if (the_ext == the_root) {
g_snprintf(output, outsize, "%s", destroot);
g_free(the_ext);
g_free(the_root);
return output;
}
the_root[ext-destroot-1] = '\0';
gchar last_char = the_root[strlen(the_root)-1];
if (last_char == '-' || last_char == '_')
g_snprintf(output, outsize, "%s%05d.%s", the_root, counter, the_ext);
else g_snprintf(output, outsize, "%s_%05d.%s", the_root, counter, the_ext);
g_free(the_ext);
g_free(the_root);
} else {
/* create the file name with destroot_number */
g_snprintf(output, outsize, "%s%05d", destroot, counter);
}
return output;
}
/* This function sets all default values of libraw settings in the com.raw_set
* struct, as defined in the glade file.
* When the ini file is read, the values of com.raw_set are overwritten, but if the
* file is missing, like the first time Siril is launched, we don't want to have the
* GUI states reset to zero by set_GUI_LIBRAW() because the data in com.raw_set had
* not been initialized with the default GUI values (= initialized to 0).
*/
static void initialize_libraw_settings() {
com.raw_set.bright = 1.0; // brightness
com.raw_set.mul[0] = 1.0; // multipliers: red
com.raw_set.mul[1] = 1.0; // multipliers: green, not used because always equal to 1
com.raw_set.mul[2] = 1.0; // multipliers: blue
com.raw_set.auto_mul = 1; // multipliers are Either read from file, or calculated on the basis of file data, or taken from hardcoded constants
com.raw_set.user_black = 0; // black point correction
com.raw_set.use_camera_wb = 0; // if possible, use the white balance from the camera.
com.raw_set.use_auto_wb = 0; // use automatic white balance obtained after averaging over the entire image
com.raw_set.user_qual = 1; // type of interpolation. AHD by default
com.raw_set.gamm[0] = 1.0; // gamma curve: linear by default
com.raw_set.gamm[1] = 1.0;
}
static void initialize_ser_debayer_settings() {
com.debayer.open_debayer = FALSE;
com.debayer.use_bayer_header = TRUE;
com.debayer.compatibility = FALSE;
com.debayer.bayer_pattern = BAYER_FILTER_RGGB;
com.debayer.bayer_inter = BAYER_VNG;
}
static gboolean end_convert_idle(gpointer p) {
struct _convert_data *args = (struct _convert_data *) p;
struct timeval t_end;
if (get_thread_run() && args->nb_converted > 1) {
// load the sequence
char *ppseqname = malloc(strlen(args->destroot) + 5);
sprintf(ppseqname, "%s.seq", args->destroot);
check_seq(0);
update_sequences_list(ppseqname);
free(ppseqname);
}
update_used_memory();
set_progress_bar_data(PROGRESS_TEXT_RESET, PROGRESS_DONE);
set_cursor_waiting(FALSE);
gettimeofday(&t_end, NULL);
show_time(args->t_start, t_end);
stop_processing_thread();
g_free(args->destroot);
free(args);
return FALSE;
}
/* from a fits object, save to file or files, based on the channel policy from convflags */
static int save_to_target_fits(fits *fit, const char *dest_filename) {
if (convflags & CONV3X1) { // an RGB image to 3 fits, one for each channel
char filename[130];
if (fit->naxis != 3) {
siril_log_message(_("Saving to 3 FITS files cannot be done because the source image does not have three channels\n"));
return 1;
}
sprintf(filename, "r_%s", dest_filename);
if (save1fits16(filename, fit, RLAYER)) {
printf("tofits: save1fit8 error, CONV3X1\n");
return 1;
}
sprintf(filename, "g_%s", dest_filename);
if (save1fits16(filename, fit, GLAYER)) {
printf("tofits: save1fit8 error, CONV3X1\n");
return 1;
}
sprintf(filename, "b_%s", dest_filename);
if (save1fits16(filename, fit, BLAYER)) {
printf("tofits: save1fit8 error, CONV3X1\n");
return 1;
}
} else if (convflags & CONV1X1) { // a single FITS to convert from an RGB grey image
if (save1fits16(dest_filename, fit, RLAYER)) {
printf("tofits: save1fit8 error, CONV1X1\n");
return 1;
}
} else { // normal FITS save, any format
if (savefits(dest_filename, fit)) {
printf("tofits: savefit error, CONV1X3\n");
return 1;
}
}
return 0;
}
/* open the file with path source from any image type and load it into a new FITS object */
static fits *any_to_new_fits(image_type imagetype, const char *source, gboolean compatibility) {
int retval = 0;
fits *tmpfit = calloc(1, sizeof(fits));
retval = any_to_fits(imagetype, source, tmpfit);
if (!retval)
retval = debayer_if_needed(imagetype, tmpfit, compatibility, FALSE);
if (retval) {
clearfits(tmpfit);
free(tmpfit);
return NULL;
}
return tmpfit;
}
static int retrieveBayerPattern(char *bayer) {
int i;
for (i = 0; i < (sizeof(filter_pattern) / sizeof(char *)); i++) {
if (g_ascii_strcasecmp(bayer, filter_pattern[i]) == 0) {
return i;
}
}
return BAYER_FILTER_NONE;
}
/**************************Public functions***********************************************************/
void check_for_conversion_form_completeness() {
static GtkTreeView *tree_convert = NULL;
GtkTreeIter iter;
GtkTreeModel *model = NULL;
gboolean valid;
GtkWidget *go_button = lookup_widget("convert_button");
if (tree_convert == NULL)
tree_convert = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview_convert"));
model = gtk_tree_view_get_model(tree_convert);
valid = gtk_tree_model_get_iter_first(model, &iter);
gtk_widget_set_sensitive (go_button, destroot && destroot[0] != '\0' && valid);
/* we override the sort function in order to provide natural sort order */
gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(model),
COLUMN_CONV_FILENAME, (GtkTreeIterCompareFunc) sort_conv_tree, NULL,
NULL);
update_statusbar_convert();
}
/* initialize converters (utilities used for different image types importing) *
* updates the label listing the supported input file formats, and modifies the
* list of file types used in convflags */
void initialize_converters() {
GtkLabel *label_supported;
GString *string;
gchar *text;
int count_ext = 0;
string = g_string_new("\t");
/* internal converters */
supported_filetypes |= TYPEBMP;
g_string_append(string, _("BMP images, "));
supported_filetypes |= TYPEPIC;
g_string_append(string, _("PIC images (IRIS), "));
supported_filetypes |= TYPEPNM;
g_string_append(string, _("PGM and PPM binary images"));
supported_extensions = malloc(MAX_OF_EXTENSIONS * sizeof(char*));
/* internal extensions */
if (supported_extensions == NULL) {
fprintf(stderr, "initialize_converters: error allocating data\n");
return;
}
supported_extensions[count_ext++] = ".fit";
supported_extensions[count_ext++] = ".fits";
supported_extensions[count_ext++] = ".fts";
supported_extensions[count_ext++] = ".bmp";
supported_extensions[count_ext++] = ".ppm";
supported_extensions[count_ext++] = ".pgm";
supported_extensions[count_ext++] = ".pnm";
supported_extensions[count_ext++] = ".pic";
initialize_ser_debayer_settings(); // below in the file
#ifdef HAVE_LIBRAW
int i, nb_raw;
supported_filetypes |= TYPERAW;
g_string_append(string, ", ");
g_string_append(string, _("RAW images"));
if (!com.script) set_libraw_settings_menu_available(TRUE); // enable libraw settings
initialize_libraw_settings(); // below in the file
nb_raw = get_nb_raw_supported();
for (i = 0; i < nb_raw; i++) {
supported_extensions[count_ext+i] = malloc(strlen(supported_raw[i].extension) + 2 * sizeof (char));
strcpy(supported_extensions[count_ext+i], ".");
strcat(supported_extensions[count_ext+i], supported_raw[i].extension);
}
count_ext += nb_raw;
#else
if (!com.script) set_libraw_settings_menu_available(FALSE); // disable libraw settings
#endif
g_string_append(string, ", ");
g_string_append(string, _("FITS-CFA images"));
#if defined(HAVE_FFMS2_1) || defined(HAVE_FFMS2_2)
supported_filetypes |= TYPEAVI;
g_string_append(string, ", ");
g_string_append(string, _("Films"));
#endif
supported_filetypes |= TYPESER;
g_string_append(string, ", ");
g_string_append(string, _("SER sequences"));
/* library converters (detected by configure) */
#ifdef HAVE_LIBTIFF
supported_filetypes |= TYPETIFF;
g_string_append(string, ", ");
g_string_append(string, _("TIFF images"));
supported_extensions[count_ext++] = ".tif";
supported_extensions[count_ext++] = ".tiff";
#endif
#ifdef HAVE_LIBJPEG
supported_filetypes |= TYPEJPG;
g_string_append(string, ", ");
g_string_append(string, _("JPG images"));
supported_extensions[count_ext++] = ".jpg";
supported_extensions[count_ext++] = ".jpeg";
#endif
#ifdef HAVE_LIBPNG
supported_filetypes |= TYPEPNG;
g_string_append(string, ", ");
g_string_append(string, _("PNG images"));
supported_extensions[count_ext++] = ".png";
#endif
supported_extensions[count_ext++] = NULL; // NULL-terminated array
g_string_append(string, ".");
text = g_string_free(string, FALSE);
if (!com.script) {
label_supported = GTK_LABEL(gtk_builder_get_object(builder, "label_supported_types"));
gtk_label_set_text(label_supported, text);
}
siril_log_message(_("Supported file types: %s\n"), text + 1);
g_free(text);
}
int check_for_raw_extensions(const char *extension) {
int i, nb_raw;
nb_raw = get_nb_raw_supported();
for (i = 0; i < nb_raw; i++) {
if (!g_ascii_strcasecmp(extension, supported_raw[i].extension))
return 0;
}
return 1;
}
/* returns the image_type for the extension without the dot, only if it is supported by
* the current instance of Siril. */
image_type get_type_for_extension(const char *extension) {
if ((supported_filetypes & TYPEBMP) && !g_ascii_strcasecmp(extension, "bmp")) {
return TYPEBMP;
} else if ((supported_filetypes & TYPEJPG) &&
(!g_ascii_strcasecmp(extension, "jpg") || !g_ascii_strcasecmp(extension, "jpeg"))) {
return TYPEJPG;
} else if ((supported_filetypes & TYPETIFF) &&
(!g_ascii_strcasecmp(extension, "tif") || !g_ascii_strcasecmp(extension, "tiff"))) {
return TYPETIFF;
} else if ((supported_filetypes & TYPEPNG) && !g_ascii_strcasecmp(extension, "png")) {
return TYPEPNG;
} else if ((supported_filetypes & TYPEPNM) &&
(!g_ascii_strcasecmp(extension, "pnm") || !g_ascii_strcasecmp(extension, "ppm") ||
!g_ascii_strcasecmp(extension, "pgm"))) {
return TYPEPNM;
} else if ((supported_filetypes & TYPEPIC) && !g_ascii_strcasecmp(extension, "pic")){
return TYPEPIC;
} else if ((supported_filetypes & TYPERAW) && !check_for_raw_extensions(extension)) {
return TYPERAW;
#if defined(HAVE_FFMS2_1) || defined(HAVE_FFMS2_2)
// check_for_film_extensions is undefined without FFMS2
} else if ((supported_filetypes & TYPEAVI) && !check_for_film_extensions(extension)) {
return TYPEAVI;
#endif
} else if ((supported_filetypes & TYPESER) && !g_ascii_strcasecmp(extension, "ser")) {
return TYPESER;
} else if (!g_ascii_strcasecmp(extension, "fit") || !g_ascii_strcasecmp(extension, "fits") ||
!g_ascii_strcasecmp(extension, "fts")) {
return TYPEFITS;
}
return TYPEUNDEF; // not recognized or not supported
}
int count_selected_files() {
static GtkTreeView *tree_convert = NULL;
GtkTreeModel *model = NULL;
GtkTreeIter iter;
gboolean valid;
int count = 0;
if (tree_convert == NULL)
tree_convert = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview_convert"));
model = gtk_tree_view_get_model(tree_convert);
valid = gtk_tree_model_get_iter_first(model, &iter);
while (valid) {
gchar *file_name, *file_date;
gtk_tree_model_get(model, &iter, COLUMN_FILENAME, &file_name,
COLUMN_DATE, &file_date, -1);
valid = gtk_tree_model_iter_next (model, &iter);
count ++;
}
return count;
}
static void initialize_convert() {
GDir *dir;
GError *error = NULL;
gchar *file_data, *file_date;
const gchar *indice;
static GtkTreeView *tree_convert = NULL;
static GtkEntry *startEntry = NULL;
GtkTreeModel *model = NULL;
GtkTreeIter iter;
gboolean valid, several_type_of_files = FALSE;
image_type imagetype = TYPEUNDEF;
GList *list = NULL;
int count = 0;
if (tree_convert == NULL) {
tree_convert = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview_convert"));
startEntry = GTK_ENTRY(gtk_builder_get_object(builder, "startIndiceEntry"));
}
struct timeval t_start;
if (get_thread_run()) {
siril_log_message(_("Another task is already in progress, ignoring new request.\n"));
return;
}
/* test if forbidden chars exist */
char *forbid_char = strchr(destroot, '/');
if (forbid_char == NULL) {
forbid_char = strchr(destroot, '\\');
}
if (forbid_char != NULL) {
siril_message_dialog(GTK_MESSAGE_ERROR, _("Invalid char"), _("Please remove invalid char in the sequence name "
"before trying to convert images into a new sequence again."));
return;
}
if (g_file_test(destroot, G_FILE_TEST_EXISTS)) {
char *title = siril_log_message(_("A file named %s already exists. "
"Do you want to replace it?\n"), destroot);
gboolean replace = siril_confirm_dialog(title, _("The file already exists. "
"Replacing it will overwrite its contents."), FALSE);
if (!replace) return;
}
model = gtk_tree_view_get_model(tree_convert);
valid = gtk_tree_model_get_iter_first(model, &iter);
if (valid == FALSE) return; //The tree is empty
while (valid) {
gtk_tree_model_get(model, &iter, COLUMN_FILENAME, &file_data,
COLUMN_DATE, &file_date, -1);
list = g_list_append (list, file_data);
const char *src_ext = get_filename_ext(file_data);
if (count != 0) {
if (imagetype != get_type_for_extension(src_ext)) {
several_type_of_files = TRUE;
}
}
imagetype = get_type_for_extension(src_ext);
valid = gtk_tree_model_iter_next (model, &iter);
count ++;
}
if ((convflags & CONVDEBAYER) && (imagetype == TYPESER) && (several_type_of_files == FALSE)) {
siril_message_dialog(GTK_MESSAGE_WARNING, _("A conflict has been detected."),
_("The Debayer option is not allowed in SER conversion, please uncheck the option."));
set_cursor_waiting(FALSE);
return;
} else if ((convflags & CONVMULTIPLE) && (imagetype == TYPESER) && (several_type_of_files == FALSE)) {
siril_message_dialog(GTK_MESSAGE_WARNING, _("A conflict has been detected."),
_("The Multiple SER option is not allowed in SER conversion, please uncheck the option."));
set_cursor_waiting(FALSE);
return;
}
indice = gtk_entry_get_text(startEntry);
siril_log_color_message(_("Conversion: processing...\n"), "red");
gettimeofday(&t_start, NULL);
set_cursor_waiting(TRUE);
control_window_switch_to_tab(OUTPUT_LOGS);
/* then, convert files to Siril's FITS format */
struct _convert_data *args;
set_cursor_waiting(TRUE);
char *tmpmsg;
if (!com.wd) {
tmpmsg = siril_log_message(_("Conversion: no working directory set.\n"));
siril_message_dialog(GTK_MESSAGE_WARNING, _("Warning"), tmpmsg);
set_cursor_waiting(FALSE);
return;
}
if((dir = g_dir_open(com.wd, 0, &error)) == NULL){
tmpmsg = siril_log_message(_("Conversion: error opening working directory %s.\n"), com.wd);
siril_message_dialog(GTK_MESSAGE_ERROR, _("Error"), tmpmsg);
fprintf (stderr, "Conversion: %s\n", error->message);
set_cursor_waiting(FALSE);
return ;
}
args = malloc(sizeof(struct _convert_data));
args->start = (atof(indice) == 0 || atof(indice) > USHRT_MAX) ? 1 : atof(indice);
args->dir = dir;
args->list = list;
args->total = count;
args->nb_converted = 0;
args->t_start.tv_sec = t_start.tv_sec;
args->t_start.tv_usec = t_start.tv_usec;
args->compatibility = com.debayer.compatibility;
args->command_line = FALSE;
args->several_type_of_files = several_type_of_files;
args->destroot = g_strdup(destroot);
start_in_new_thread(convert_thread_worker, args);
return;
}
void on_entry2_activate(GtkEntry *entry, gpointer user_data) {
initialize_convert();
}
void on_convert_button_clicked(GtkButton *button, gpointer user_data) {
initialize_convert();
}
gpointer convert_thread_worker(gpointer p) {
char dest_filename[128], msg_bar[256];
int indice;
int ser_frames = 0;
double progress = 0.0;
struct ser_struct *ser_file = NULL;
struct _convert_data *args = (struct _convert_data *) p;
GList *list;
list = g_list_first(args->list);
indice = args->start;
if (convflags & CONVDSTSER) {
if (convflags & CONV3X1) {
siril_log_color_message(_("SER output will take precedence over the one-channel per image creation option.\n"), "salmon");
convflags &= ~CONV3X1;
} else {
ser_file = malloc(sizeof(struct ser_struct));
if (!(convflags & CONVMULTIPLE)) {
if (ser_create_file(args->destroot, ser_file, TRUE, NULL)) {
siril_log_message(_("Creating the SER file failed, aborting.\n"));
goto clean_exit;
}
}
}
}
while (list) {
gchar *src_filename = (gchar *)list->data;
const char *src_ext = get_filename_ext(src_filename);
image_type imagetype;
if (!get_thread_run()) {
break;
}
gchar *name = g_utf8_strrchr(src_filename, strlen(src_filename), G_DIR_SEPARATOR);
if (name)
g_snprintf(msg_bar, 256, _("Converting %s..."), name + 1);
else g_snprintf(msg_bar, 256, _("Converting %s..."), src_filename);
imagetype = get_type_for_extension(src_ext);
com.filter = (int) imagetype;
if (imagetype == TYPEUNDEF) {
char msg[512];
char *title = siril_log_message(_("Filetype is not supported, cannot convert: %s\n"), src_ext);
g_snprintf(msg, 512, _("File extension '%s' is not supported.\n"
"Verify that you typed the extension correctly.\n"
"If so, you may need to install third-party software to enable "
"this file type conversion, look at the README file.\n"
"If the file type you are trying to load is listed in supported "
"formats, you may notify the developpers that the extension you are "
"trying to use should be recognized for this type."), src_ext);
siril_message_dialog(GTK_MESSAGE_ERROR, title, msg);
break; // avoid 100 error popups
}
if (imagetype == TYPEAVI) {
// we need to do a semi-recursive thing here,
// thankfully it's only one level deep
#if defined(HAVE_FFMS2_1) || defined(HAVE_FFMS2_2)
int frame;
fits *fit = calloc(1, sizeof(fits));
struct film_struct film_file;
if (film_open_file(src_filename, &film_file) != FILM_SUCCESS) {
siril_log_message(_("Error while opening film %s, aborting.\n"), src_filename);
clearfits(fit);
free(fit);
break;
}
if (convflags & CONVMULTIPLE) {
if (ser_create_file(create_sequence_filename(indice++, dest_filename, 128),
ser_file, TRUE, NULL)) {
siril_log_message(_("Creating the SER file failed, aborting.\n"));
clearfits(fit);
free(fit);
goto clean_exit;
}
}
for (frame = 0; frame < film_file.frame_count; frame++) {
if (!get_thread_run()) {
break;
}
// read frame from the film
if (film_read_frame(&film_file, frame, fit) != FILM_SUCCESS) {
siril_log_message(_("Error while reading frame %d from %s, aborting.\n"),
frame, src_filename);
clearfits(fit);
free(fit);
goto clean_exit;
}
// save to the destination file
if (convflags & CONVDSTSER) {
if (convflags & CONV1X1)
keep_first_channel_from_fits(fit);
if (ser_write_frame_from_fit(ser_file, fit, frame)) {
siril_log_message(_("Error while converting to SER (no space left?)\n"));
clearfits(fit);
free(fit);
goto clean_exit;
}
} else {
g_snprintf(dest_filename, 128, "%s%05d", args->destroot, indice++);
if (save_to_target_fits(fit, dest_filename)) {
siril_log_message(_("Error while converting to FITS (no space left?)\n"));
clearfits(fit);
free(fit);
goto clean_exit;
}
}
clearfits(fit);
}
if (convflags & CONVMULTIPLE) {
ser_write_and_close(ser_file);
}
free(fit);
#endif
}
else if (imagetype == TYPESER) {
if (args->several_type_of_files) {
siril_log_message(_("Joining SER files is only possible with a list "
"only containing SER files. Please, remove non SER files.\n"));
break;
}
int frame;
fits *fit = calloc(1, sizeof(fits));
struct ser_struct tmp_ser;
ser_init_struct(&tmp_ser);
if (ser_open_file(src_filename, &tmp_ser)) {
siril_log_message(_("Error while opening ser file %s, aborting.\n"), src_filename);
clearfits(fit);
free(fit);
break;
}
if (args->nb_converted > 0 && (convflags & CONVDSTSER)) {
if (tmp_ser.image_height != ser_file->image_height
|| tmp_ser.image_width != ser_file->image_width) {
siril_log_color_message(_("Input SER files must have the same size to be joined.\n"), "red");
clearfits(fit);
free(fit);
break;
}
}
set_progress_bar_data(msg_bar, PROGRESS_PULSATE);
for (frame = 0; frame < tmp_ser.frame_count; frame++) {
if (!get_thread_run()) {
break;
}
// read frame from the film
if (ser_read_frame(&tmp_ser, frame, fit)) {
siril_log_message(_("Error while reading frame %d from %s, aborting.\n"),
frame, src_filename);
clearfits(fit);
free(fit);
goto clean_exit;
}
// save to the destination file
if (convflags & CONVDSTSER) {
if (convflags & CONV1X1)
keep_first_channel_from_fits(fit);
if (ser_write_frame_from_fit(ser_file, fit, frame + ser_frames)) {
siril_log_message(_("Error while converting to SER (no space left?)\n"));
clearfits(fit);
free(fit);
goto clean_exit;
}
} else {
g_snprintf(dest_filename, 128, "%s%05d", args->destroot, indice++);
if (save_to_target_fits(fit, dest_filename)) {
siril_log_message(_("Error while converting to FITS (no space left?)\n"));
clearfits(fit);
free(fit);
goto clean_exit;
}
}
clearfits(fit);
}
ser_frames += frame;
ser_close_file(&tmp_ser);
free(fit);
}
else { // single image
fits *fit = any_to_new_fits(imagetype, src_filename, args->compatibility);
if (fit) {
if (convflags & CONVDSTSER) {
if (convflags & CONV1X1)
keep_first_channel_from_fits(fit);
if (ser_write_frame_from_fit(ser_file, fit, args->nb_converted)) {
siril_log_message(_("Error while converting to SER (no space left?)\n"));
break;
}
} else {
g_snprintf(dest_filename, 128, "%s%05d", args->destroot, indice++);
if (save_to_target_fits(fit, dest_filename)) {
siril_log_message(_("Error while converting to FITS (no space left?)\n"));
break;
}
}
clearfits(fit);
free(fit);
}
}
set_progress_bar_data(msg_bar, progress/((double)args->total));
progress += 1.0;
args->nb_converted++;
list = g_list_next(list);
}
clean_exit:
if (convflags & CONVDSTSER) {
if (!(convflags & CONVMULTIPLE))
ser_write_and_close(ser_file);
free(ser_file);
}
if (args->command_line) {
unset_debayer_in_convflags();
}
g_list_free_full(args->list, g_free);
g_dir_close(args->dir);
siril_add_idle(end_convert_idle, args);
return NULL;
}
int debayer_if_needed(image_type imagetype, fits *fit, gboolean compatibility, gboolean force_debayer) {
int retval = 0;
sensor_pattern tmp;
/* What the hell?
* Siril's FITS are stored bottom to top, debayering will throw
* wrong results. So before demosacaing we need to transforme the image
* with fits_flip_top_to_bottom() function */
if (imagetype == TYPEFITS && (((convflags & CONVDEBAYER) && !force_debayer) || force_debayer)) {
tmp = com.debayer.bayer_pattern;
if (fit->naxes[2] != 1) {
siril_log_message(_("Cannot perform debayering on image with more than one channel\n"));
return retval;
}
if (!compatibility)
fits_flip_top_to_bottom(fit);
/* Get Bayer informations from header if available */
if (com.debayer.use_bayer_header) {
sensor_pattern bayer;
bayer = retrieveBayerPattern(fit->bayer_pattern);
if (bayer <= BAYER_FILTER_MAX) {
if (bayer != com.debayer.bayer_pattern) {
if (bayer == BAYER_FILTER_NONE) {
siril_log_color_message(_("No Bayer pattern found in the header file.\n"), "red");
}
else {
siril_log_color_message(_("Bayer pattern found in header (%s) is different"
" from Bayer pattern in settings (%s). Overriding settings.\n"),
"red", filter_pattern[bayer], filter_pattern[com.debayer.bayer_pattern]);
com.debayer.bayer_pattern = bayer;
}
}
} else { /* FIXME: XTRANS CASE. TESTED FOR ONE FILE */
com.debayer.bayer_pattern = XTRANS_FILTER;
com.debayer.bayer_inter = XTRANS;
siril_log_color_message(_("XTRANS Sensor detected. Using special algorithm.\n"), "red");
}
}
if (com.debayer.bayer_pattern >= BAYER_FILTER_MIN
&& com.debayer.bayer_pattern <= BAYER_FILTER_MAX) {
siril_log_message(_("Filter Pattern: %s\n"), filter_pattern[com.debayer.bayer_pattern]);
}
if (debayer(fit, com.debayer.bayer_inter)) {
siril_log_message(_("Cannot perform debayering\n"));
retval = -1;
} else {
if (!compatibility)
fits_flip_top_to_bottom(fit);
}
com.debayer.bayer_pattern = tmp;
}
return retval;
}
/* open the file with path source from any image type and load it into the given FITS object */
int any_to_fits(image_type imagetype, const char *source, fits *dest) {
int retval = 0;
switch (imagetype) {
case TYPEFITS:
retval = (readfits(source, dest, NULL) != 0);
break;
case TYPEBMP:
retval = (readbmp(source, dest) < 0);
break;
case TYPEPIC:
retval = (readpic(source, dest) < 0);
break;
#ifdef HAVE_LIBTIFF
case TYPETIFF:
retval = (readtif(source, dest) < 0);
break;
#endif
case TYPEPNM:
retval = (import_pnm_to_fits(source, dest) < 0);
break;
#ifdef HAVE_LIBJPEG
case TYPEJPG:
retval = (readjpg(source, dest) < 0);
break;
#endif
#ifdef HAVE_LIBPNG
case TYPEPNG:
retval = (readpng(source, dest) < 0);
break;
#endif
#ifdef HAVE_LIBRAW
case TYPERAW:
retval = (open_raw_files(source, dest, !(convflags & CONVDEBAYER)) < 0);
break;
#endif
case TYPESER:
case TYPEAVI:
siril_log_message(_("Requested converting a sequence file to single FITS image, should not happen\n"));
retval = 1;
break;
case TYPEUNDEF:
default: // when the ifdefs are not compiled, default happens!
siril_log_message(_("Error opening %s: file type not supported.\n"), source);
retval = 1;
}
return retval;
}
void set_debayer_in_convflags() {
convflags |= CONVDEBAYER;
}
void unset_debayer_in_convflags() {
convflags &= ~CONVDEBAYER;
}
/******************Callback functions*******************************************************************/
static gchar forbidden_char[] = { '/', '\\' };
static int get_nb_forbidden_char() {
return sizeof(forbidden_char) / sizeof(gchar);
}
static gboolean is_forbiden(gchar c) {
int i, n;
n = get_nb_forbidden_char();
for (i = 0; i < n; i++) {
if (c == forbidden_char[i]) {
return TRUE;
}
}
return FALSE;
}
void insert_text_handler(GtkEntry *entry, const gchar *text, gint length,
gint *position, gpointer data) {
GtkEditable *editable = GTK_EDITABLE(entry);
int i, count = 0;
gchar *result = g_new(gchar, length);
for (i = 0; i < length; i++) {
if (is_forbiden(text[i]))
continue;
result[count++] = text[i];
}
if (count > 0) {
g_signal_handlers_block_by_func(G_OBJECT (editable),
G_CALLBACK (insert_text_handler), data);
gtk_editable_insert_text(editable, result, count, position);
g_signal_handlers_unblock_by_func(G_OBJECT (editable),
G_CALLBACK (insert_text_handler), data);
}
g_signal_stop_emission_by_name(G_OBJECT(editable), "insert_text");
g_free(result);
}
// truncates destroot if it's more than 120 characters, append a '_' if it
// doesn't end with one or a '-'. SER extensions are accepted and unmodified.
void on_convtoroot_changed (GtkEditable *editable, gpointer user_data){
static GtkWidget *multiple_ser = NULL;
const gchar *name = gtk_entry_get_text(GTK_ENTRY(editable));
if (*name != 0) {
if (!multiple_ser)
multiple_ser = lookup_widget("multipleSER");
if (destroot)
g_free(destroot);
destroot = g_str_to_ascii(name, NULL); // we want to avoid special char
const char *ext = get_filename_ext(destroot);
if (ext && !g_ascii_strcasecmp(ext, "ser")) {
convflags |= CONVDSTSER;
gtk_widget_set_visible(multiple_ser, TRUE);
} else {
convflags &= ~CONVDSTSER;
gtk_widget_set_visible(multiple_ser, FALSE);
destroot = format_basename(destroot);
}
check_for_conversion_form_completeness();
}
}
void on_demosaicing_toggled (GtkToggleButton *togglebutton, gpointer user_data) {
static GtkToggleButton *but = NULL;
if (!but) but = GTK_TOGGLE_BUTTON(lookup_widget("radiobutton1"));
if (gtk_toggle_button_get_active(togglebutton)) {
set_debayer_in_convflags();
gtk_toggle_button_set_active(but, TRUE);
com.debayer.open_debayer = TRUE;
}
else {
unset_debayer_in_convflags(); // used for conversion
com.debayer.open_debayer = FALSE; // used for image opening
}
}
void on_multipleSER_toggled (GtkToggleButton *togglebutton, gpointer user_data) {
if (gtk_toggle_button_get_active(togglebutton))
convflags |= CONVMULTIPLE;
else convflags &= ~CONVMULTIPLE;
}
void on_conv3planefit_toggled (GtkToggleButton *togglebutton, gpointer user_data){
convflags |= CONV1X3;
convflags &= ~(CONV3X1|CONV1X1);
}
void on_conv3_1plane_toggled (GtkToggleButton *togglebutton, gpointer user_data) {
convflags |= CONV3X1;
convflags &= ~(CONV1X1|CONV1X3);
}
void on_conv1_1plane_toggled (GtkToggleButton *togglebutton, gpointer user_data) {
convflags |= CONV1X1;
convflags &= ~(CONV3X1|CONV1X3);
}
|