static void node_composit_exec_view_levels(void *data, bNode *node, bNodeStack **in, bNodeStack **out) { CompBuf* cbuf; CompBuf* histogram; RenderData *rd=data; float mean, std_dev; int bins[256]; int x; if(in[0]->hasinput==0) return; if(in[0]->data==NULL) return; histogram=alloc_compbuf(256, 256, CB_RGBA, 1); cbuf=typecheck_compbuf(in[0]->data, CB_RGBA); /*initalize bins*/ for(x=0; x<256; x++) { bins[x]=0; } /*fill bins */ fill_bins(node, in[0]->data, bins, rd->color_mgt_flag & R_COLOR_MANAGEMENT); /* draw the histogram chart */ draw_histogram(node, histogram, bins); /* calculate the average brightness and contrast */ mean=brightness_mean(node, in[0]->data); std_dev=brightness_standard_deviation(node, in[0]->data, mean); /* Printf debuging ;) printf("Mean: %f\n", mean); printf("Std Dev: %f\n", std_dev); */ if(out[0]->hasoutput) out[0]->vec[0]= mean; if(out[1]->hasoutput) out[1]->vec[0]= std_dev; generate_preview(data, node, histogram); if(cbuf!=in[0]->data) free_compbuf(cbuf); free_compbuf(histogram); }
int main(int argc, char *argv[]) { struct GModule *module; struct { struct Option *quant, *perc, *slots, *basemap, *covermap, *output; } opt; struct { struct Flag *r, *p; } flag; const char *basemap, *covermap; char **outputs; int reclass, print; int cover_fd, base_fd; struct Range range; struct FPRange fprange; int i; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("statistics")); module->description = _("Compute category quantiles using two passes."); opt.basemap = G_define_standard_option(G_OPT_R_BASE); opt.covermap = G_define_standard_option(G_OPT_R_COVER); opt.quant = G_define_option(); opt.quant->key = "quantiles"; opt.quant->type = TYPE_INTEGER; opt.quant->required = NO; opt.quant->description = _("Number of quantiles"); opt.perc = G_define_option(); opt.perc->key = "percentiles"; opt.perc->type = TYPE_DOUBLE; opt.perc->multiple = YES; opt.perc->description = _("List of percentiles"); opt.perc->answer = "50"; opt.slots = G_define_option(); opt.slots->key = "bins"; opt.slots->type = TYPE_INTEGER; opt.slots->required = NO; opt.slots->description = _("Number of bins to use"); opt.slots->answer = "1000"; opt.output = G_define_standard_option(G_OPT_R_OUTPUT); opt.output->description = _("Resultant raster map(s)"); opt.output->required = NO; opt.output->multiple = YES; flag.r = G_define_flag(); flag.r->key = 'r'; flag.r->description = _("Create reclass map with statistics as category labels"); flag.p = G_define_flag(); flag.p->key = 'p'; flag.p->description = _("Do not create output maps; just print statistics"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); basemap = opt.basemap->answer; covermap = opt.covermap->answer; outputs = opt.output->answers; reclass = flag.r->answer; print = flag.p->answer; if (!print && !opt.output->answers) G_fatal_error(_("Either -%c or %s= must be given"), flag.p->key, opt.output->key); if (print && opt.output->answers) G_fatal_error(_("-%c and %s= are mutually exclusive"), flag.p->key, opt.output->key); num_slots = atoi(opt.slots->answer); if (opt.quant->answer) { num_quants = atoi(opt.quant->answer) - 1; quants = G_calloc(num_quants, sizeof(DCELL)); for (i = 0; i < num_quants; i++) quants[i] = 1.0 * (i + 1) / (num_quants + 1); } else { for (i = 0; opt.perc->answers[i]; i++) ; num_quants = i; quants = G_calloc(num_quants, sizeof(DCELL)); for (i = 0; i < num_quants; i++) quants[i] = atof(opt.perc->answers[i]) / 100; qsort(quants, num_quants, sizeof(DCELL), compare_dcell); } if (opt.output->answer) { for (i = 0; opt.output->answers[i]; i++) ; if (i != num_quants) G_fatal_error(_("Number of quantiles (%d) does not match number of output maps (%d)"), num_quants, i); } base_fd = Rast_open_old(basemap, ""); cover_fd = Rast_open_old(covermap, ""); if (Rast_map_is_fp(basemap, "") != 0) G_fatal_error(_("The base map must be an integer (CELL) map")); if (Rast_read_range(basemap, "", &range) < 0) G_fatal_error(_("Unable to read range of base map <%s>"), basemap); Rast_get_range_min_max(&range, &min, &max); num_cats = max - min + 1; if (num_cats > MAX_CATS) G_fatal_error(_("Base map <%s> has too many categories (max: %d)"), basemap, MAX_CATS); Rast_read_fp_range(covermap, "", &fprange); Rast_get_fp_range_min_max(&fprange, &f_min, &f_max); slot_size = (f_max - f_min) / num_slots; basecats = G_calloc(num_cats, sizeof(struct basecat)); for (i = 0; i < num_cats; i++) basecats[i].slots = G_calloc(num_slots, sizeof(unsigned int)); rows = Rast_window_rows(); cols = Rast_window_cols(); get_slot_counts(base_fd, cover_fd); initialize_bins(); fill_bins(base_fd, cover_fd); sort_bins(); compute_quantiles(); if (print) print_quantiles(); else if (reclass) do_reclass(basemap, outputs); else do_output(base_fd, outputs, covermap); Rast_close(cover_fd); Rast_close(base_fd); return (EXIT_SUCCESS); }
/** * Função principal do programa, responsável por executar funções * que definem o comportamento do algoritmo. * * \param argc Quantidade de argumentos passados ao programa. * \param argv Vetor que contem os argumentos passados para o programa. * \return Retorna zero caso tenha sucesso na execução. * \see create_empty_bin_list * \see sort_numbers_array * \see print_numbers * \see fill_bins * \see print_list_bins * \see free_bins * \see NUMBERS_QUANTITY * \see NUMBERS_MINIMUM * \see NUMBERS_MAXIMUM * \see BIN_SIZE */ int main(int argc, char **argv) { unsigned short int i; unsigned short int *values; /** Usa-se ponteiro para armazenar a lista de números. */ bin_list *bins; /** Usa-se ponteiro contem a lsita de BINs gerados dinamicamente. */ /** * Verifica-se os argumentos passados, ou seja, se foi passado a quantidade necessária. * Se não, informa o que é preciso para execução e encerra o programa. */ if (argc < 5) { printf("Passar os argumentos do programa.\n"); printf("1 - Quantidade de números para empacotar \n"); printf("2 - Tamanhos dos BINs \n"); printf("3 - Valor mínimo dos números \n"); printf("4 - Valor máximo dos números \n"); printf("5 - Valores a serem empacotados (Opcional) \n"); exit(1); } /** * Atribui os argumentos as variavéis globais do programa, para * que seja utilizado no restante do rpograma. */ NUMBERS_QUANTITY = atoi(argv[1]); BIN_SIZE = atoi(argv[2]); NUMBERS_MINIMUM = atoi(argv[3]); NUMBERS_MAXIMUM = atoi(argv[4]); /** * Caso tenha sido passado mais de cinco argumentos para o programa, * significa que a lista de números foi informada pelo usuário e, * portanto, não será gerada aleatoriamente. */ if (argc > 5) { values = malloc(sizeof(int)*argc-5); NUMBERS_QUANTITY = argc -5; for (i = 0; i < argc-5; i++) values[i] = atoi( argv[i+5]); } else { values = malloc(sizeof(int)*NUMBERS_QUANTITY); create_numbers_array (values); } /** * \attention * Caso não tenha sido possível alocar memória para os números gerados ou * informados o programa é encerrado devido a indisponibilidade de memória. */ if (values == NULL) exit(1); /** Inicialisa a lista de BINs.*/ bins = create_empty_bin_list(); /** Ordena de forma descrescente os números para empacotar. */ sort_numbers_array (values); /** Imprime os números gerados e devidamente ordenados. */ print_numbers(values); /** Preenche os BINS, ou seja, ler a lista de números e gera os BINs necessários. */ fill_bins (values, bins); /** Imprime os BINs que foram gerados. */ print_list_bins (bins); /** Por fim, libera todos os recursos que foram utilizados. */ free_bins (bins); free (values); return 0; }