- Main Page
- Related Pages
- Files

- File List
- Globals

# random_phase_noise.c

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 2010, Bruno Galerne <bruno.galerne@cmla.ens-cachan.fr>
    00003  * All rights reserved.
    00004  *
    00005  * This program is free software: you can use, modify and/or
    00006  * redistribute it under the terms of the GNU General Public
    00007  * License as published by the Free Software Foundation, either
    00008  * version 3 of the License, or (at your option) any later
    00009  * version. You should have received a copy of this license along
    00010  * this program. If not, see <http://www.gnu.org/licenses/>.
    00011  */
    00012
    00128 #include <stdio.h>
    00129 #include <stdlib.h>
    00130 #include <string.h>
    00131 #include <time.h>
    00132 #include <getopt.h>
    00133 #include <fftw3.h>
    00134
    00135 #include "mt.h"
    00136 #include "io_png.h"
    00137 #include "random_phase_noise_lib.h"
    00138
    00140 #define FATAL(MSG)                      \
    00141 do {                            \
    00142         fprintf(stderr, MSG "\n");      \
    00143         abort();                        \
    00144         } while (0);
    00145
    00146 /*
    00147 * Main function section
    00148 */
    00149
    00154 static int display_usage()
    00155 {
    00156     printf("\nrandom_phase_noise -r ratio -x nxout -y nyout "
    00157            "-a alpha -s seed image_in.png image_out.png\n\n");
    00158     printf("Required parameters:\n");
    00159     printf("   image_in.png   :   name of the input PNG image\n");
    00160     printf("   image_out.png  :   name of the output PNG image\n\n");
    00161     printf("Optionnal parameters:\n");
    00162     printf("   ratio           :   float > 1 that specifies the increase "
    00163            "of size of image_out.png relatively to the size "
    00164            "of image_in.png\n");
    00165     printf("   nxout           :   int to specify the width "
    00166            "of image_out.png. Must be greater than the width "
    00167            "of image_in.png\n");
    00168     printf("   nyout           :   int to specify the height "
    00169            "of image_out.png. Must be greater than the height "
    00170            "of image_in.png\n");
    00171     printf("   alpha           :   float in (0,0.5) that specifies "
    00172            "the proportion of the smoothing zone for "
    00173            "the spot extension (alpha = 0.1 by default)\n");
    00174     printf("   seed            :   unsigned int to specify the seed "
    00175            "for the random number generator "
    00176            "(seed = time(NULL) by default)\n");
    00177
    00178     return (1);
    00179 }
    00180
    00184 int main(int argc, char **argv)
    00185 {
    00186     char *fname_in, *fname_out; /* input/output file names */
    00187     float *data_in;             /*input data */
    00188     float *data_in_rgb[3];
    00189     float *data_out;            /* output data */
    00190     float *data_out_rgb[3];
    00191     size_t nxin, nyin;          /* input size */
    00192     size_t nxout, nyout;        /* output size */
    00193     float alpha;
    00194     int r_flag = 0;
    00195     float ratio;                /* increase ratio for the
    00196                                  * output size (must be >1) */
    00197     unsigned long seed = 0;     /* seed for the random number
    00198                                  * generator */
    00199     int x_flag = 0;
    00200     int y_flag = 0;
    00201     int a_flag = 0;
    00202     int s_flag = 0;
    00203     int c;                      /* getopt flag */
    00204
    00205     /* "default" value initialization */
    00206     alpha = 0.1;
    00207     nxout = 0;
    00208     nyout = 0;
    00209     ratio = 1.;
    00210
    00211     /* process the options and parameters */
    00212
    00213     while ((c = getopt(argc, argv, "r:x:y:a:s:hv")) != -1) {
    00214         switch (c) {
    00215         case 'r':
    00216             /* increase ratio specified */
    00217             r_flag = 1;
    00218             ratio = atof(optarg);
    00219             if (ratio <= 1)
    00220                 FATAL("Option usage: The ratio "
    00221                       "specified by -r must be " "greater than 1.")
    00222                     break;
    00223         case 'x':
    00224             /* output width specified */
    00225             x_flag = 1;
    00226             nxout = atoi(optarg);
    00227             break;
    00228         case 'y':
    00229             /* output height specified */
    00230             y_flag = 1;
    00231             nyout = atoi(optarg);
    00232             break;
    00233         case 'a':
    00234             /* alpha parameter specified */
    00235             a_flag = 1;
    00236             alpha = atof(optarg);
    00237             break;
    00238         case 's':
    00239             /* seed specified */
    00240             s_flag = 1;
    00241             seed = (unsigned long) atol(optarg);
    00242             break;
    00243         case 'h':
    00244             /* display usage */
    00245             display_usage();
    00246             return (-1);
    00247         case 'v':
    00248             /* display version */
    00249             fprintf(stdout, "%s version " __DATE__ "\n", argv[0]);
    00250             return (-1);
    00251         default:
    00252             abort();
    00253         }
    00254     }
    00255
    00256     /* process the non-option parameters */
    00257     if (2 > (argc - optind)) {
    00258         printf("The image file names are missing\n\n");
    00259         display_usage();
    00260         return (-1);
    00261     }
    00262     fname_in = argv[optind++];
    00263     fname_out = argv[optind++];
    00264
    00265     /* initilization of the random number generator */
    00266     if (s_flag == 0) {
    00267         mt_init((unsigned long) time(NULL));
    00268     }
    00269     else {
    00270         mt_init(seed);
    00271     }
    00272
    00273     /* read the PNG image and set pointers for data_in */
    00274     if (NULL == (data_in = io_png_read_f32_rgb(fname_in, &nxin, &nyin)))
    00275         FATAL("error while reading the PNG image");
    00276     data_in_rgb[0] = data_in;
    00277     data_in_rgb[1] = data_in + nxin * nyin;
    00278     data_in_rgb[2] = data_in + 2 * nxin * nyin;
    00279
    00280     /* deal with options conflict/inconsistance and set the output size */
    00281     if ((r_flag == 1) && ((x_flag == 1) || (y_flag == 1))) {
    00282         printf("Option usage: Conflicting options to specify "
    00283                "the output size: Use either the -r option "
    00284                "or both -x and -y options\n\n");
    00285         display_usage();
    00286         return (-1);
    00287     }
    00288     if (x_flag == 0) {
    00289         nxout = (int) (ratio * ((float) nxin));
    00290     }
    00291     else if (nxin > nxout) {
    00292         printf("Option usage: the width specified by "
    00293                "the -x option must be greater than the one "
    00294                "of the input image\n\n");
    00295         display_usage();
    00296         return (-1);
    00297     }
    00298     if (y_flag == 0) {
    00299         nyout = (int) (ratio * ((float) nyin));
    00300     }
    00301     else if (nyin > nyout) {
    00302         printf("Option usage: the height specified by "
    00303                "the -y option must be greater than the one "
    00304                "of the input image\n\n");
    00305         display_usage();
    00306         return (-1);
    00307     }
    00308
    00309     if ((a_flag == 1) && (nxin == nxout) && (nyin == nyout)) {
    00310         printf("INFO: The -a option was ignored "
    00311                "since the output size is the same as " "the input size.\n");
    00312     }
    00313
    00314     /* Two modes depending whether the input size and the output
    00315      * size are the same */
    00316     if ((nxin == nxout) && (nyin == nyout)) {   /* Same size */
    00317         /* RPN computation */
    00318         if (NULL == rpn_color_same_size(data_in_rgb, nxin, nyin))
    00319             FATAL("error while running rpn_color_same_size");
    00320
    00321         /* Write result in PNG file */
    00322         if (0 != io_png_write_f32(fname_out, data_in, nxin, nyin, 3))
    00323             FATAL("error while writing output PNG file");
    00324
    00325         /* Free memory */
    00326         free(data_in);
    00327     }
    00328     else {                      /* Different sizes */
    00329
    00330         /* Memory allocation for data_out */
    00331         if (NULL == (data_out = (float *)
    00332                      malloc(3 * nxout * nyout * sizeof(float))))
    00333             FATAL("allocation error");
    00334         data_out_rgb[0] = data_out;
    00335         data_out_rgb[1] = data_out + nxout * nyout;
    00336         data_out_rgb[2] = data_out + 2 * nxout * nyout;
    00337
    00338         /* RPN computation */
    00339         if (NULL == rpn_color_different_size(data_out_rgb, data_in_rgb,
    00340                                              nxout, nyout, nxin, nyin, alpha))
    00341             FATAL("error while running rpn_color_different_size");
    00342
    00343         /* Write result in PNG file */
    00344         if (0 != io_png_write_f32(fname_out, data_out, nxout, nyout, 3))
    00345             FATAL("error while writing output PNG file");
    00346
    00347         /* Free memory */
    00348         free(data_in);
    00349         free(data_out);
    00350     }
    00351
    00352     return (0);
    00353 }