matheus__serpa

julia_catilanha_set

Oct 10th, 2017
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. /* write_bmp_header():
  6.  *
  7.  *   In:
  8.  *      f: A file open for writing ('w')
  9.  *      (width, height): image dimensions
  10.  *  
  11.  *   Return:
  12.  *      0 on success, -1 on failure
  13.  *
  14.  */
  15.  
  16. int write_bmp_header(FILE *f, int width, int height) {
  17.  
  18.   unsigned int row_size_in_bytes = width * 3 +
  19.       ((width * 3) % 4 == 0 ? 0 : (4 - (width * 3) % 4));
  20.  
  21.   // Define all fields in the bmp header
  22.   char id[2] = "BM";
  23.   unsigned int filesize = 54 + (int)(row_size_in_bytes * height * sizeof(char));
  24.   short reserved[2] = {0,0};
  25.   unsigned int offset = 54;
  26.  
  27.   unsigned int size = 40;
  28.   unsigned short planes = 1;
  29.   unsigned short bits = 24;
  30.   unsigned int compression = 0;
  31.   unsigned int image_size = width * height * 3 * sizeof(char);
  32.   int x_res = 0;
  33.   int y_res = 0;
  34.   unsigned int ncolors = 0;
  35.   unsigned int importantcolors = 0;
  36.  
  37.   // Write the bytes to the file, keeping track of the
  38.   // number of written "objects"
  39.   size_t ret = 0;
  40.   ret += fwrite(id, sizeof(char), 2, f);
  41.   ret += fwrite(&filesize, sizeof(int), 1, f);
  42.   ret += fwrite(reserved, sizeof(short), 2, f);
  43.   ret += fwrite(&offset, sizeof(int), 1, f);
  44.   ret += fwrite(&size, sizeof(int), 1, f);
  45.   ret += fwrite(&width, sizeof(int), 1, f);
  46.   ret += fwrite(&height, sizeof(int), 1, f);
  47.   ret += fwrite(&planes, sizeof(short), 1, f);
  48.   ret += fwrite(&bits, sizeof(short), 1, f);
  49.   ret += fwrite(&compression, sizeof(int), 1, f);
  50.   ret += fwrite(&image_size, sizeof(int), 1, f);
  51.   ret += fwrite(&x_res, sizeof(int), 1, f);
  52.   ret += fwrite(&y_res, sizeof(int), 1, f);
  53.   ret += fwrite(&ncolors, sizeof(int), 1, f);
  54.   ret += fwrite(&importantcolors, sizeof(int), 1, f);
  55.  
  56.   // Success means that we wrote 17 "objects" successfully
  57.   return (ret != 17);
  58. }
  59.  
  60.  
  61. /*
  62.  * compute_julia_pixel(): compute RBG values of a pixel in a
  63.  *                        particular Julia set image.
  64.  *
  65.  *  In:
  66.  *      (x,y):            pixel coordinates
  67.  *      (width, height):  image dimensions
  68.  *      tint_bias:        a float to "tweak" the tint (1.0 is "no tint")
  69.  *  Out:
  70.  *      rgb: an already-allocated 3-byte array into which R, G, and B
  71.  *           values are written.
  72.  *
  73.  *  Return:
  74.  *      0 in success, -1 on failure
  75.  *
  76.  */
  77.  
  78. int compute_julia_pixel(int x, int y, int width, int height, float tint_bias, unsigned char *rgb) {
  79.  
  80.   // Check coordinates
  81.   if ((x < 0) || (x >= width) || (y < 0) || (y >= height)) {
  82.     fprintf(stderr,"Invalid (%d,%d) pixel coordinates in a %d x %d image\n", x, y, width, height);
  83.     return -1;
  84.   }
  85.  
  86.   // "Zoom in" to a pleasing view of the Julia set
  87.   float X_MIN = -1.6, X_MAX = 1.6, Y_MIN = -0.9, Y_MAX = +0.9;
  88.   float float_y = (Y_MAX - Y_MIN) * (float)y / height + Y_MIN ;
  89.   float float_x = (X_MAX - X_MIN) * (float)x / width  + X_MIN ;
  90.  
  91.   // Point that defines the Julia set
  92.   float julia_real = -.79;
  93.   float julia_img = .15;
  94.  
  95.   // Maximum number of iteration
  96.   int max_iter = 300;
  97.  
  98.   // Compute the complex series convergence
  99.   float real=float_y, img=float_x;
  100.   int num_iter = max_iter;
  101.   while (( img * img + real * real < 2 * 2 ) && ( num_iter > 0 )) {
  102.     float xtemp = img * img - real * real + julia_real;
  103.     real = 2 * img * real + julia_img;
  104.     img = xtemp;
  105.     num_iter--;
  106.   }
  107.  
  108.   // Paint pixel based on how many iterations were used, using some funky colors
  109.   float color_bias = (float) num_iter / max_iter;
  110.   rgb[0] = (num_iter == 0 ? 200 : - 500.0 * pow(tint_bias, 1.2) *  pow(color_bias, 1.6));
  111.   rgb[1] = (num_iter == 0 ? 100 : -255.0 *  pow(color_bias, 0.3));
  112.   rgb[2] = (num_iter == 0 ? 100 : 255 - 255.0 * pow(tint_bias, 1.2) * pow(color_bias, 3.0));
  113.  
  114.   return 0;
  115. }
  116.  
  117.  
  118. int main(int argc, char **argv){
  119.     if(argc != 2){
  120.         fprintf(stderr, "Usage: %s <N_integer_positive>\n", argv[0]);
  121.         exit(EXIT_FAILURE);
  122.     }
  123.     int n = atoi(argv[1]);
  124.     if(n % 2 == 1){
  125.         fprintf(stderr, "n (%d) should be even!\n", n);
  126.         exit(EXIT_FAILURE);
  127.     }
  128.  
  129.     unsigned char *v = (unsigned char *) malloc(n * 2 * n * 3 * sizeof(unsigned char));
  130.     int x,y ;
  131.  
  132.     for(x = 0; x < 2 * n; x++)
  133.         for(y = 0; y < n; y++)
  134.             if(compute_julia_pixel(x, y, 2 * n, n, 1.0, &v[x * n + y]) == -1)
  135.                 printf("error computing pixel (%d %d)\n", x, y);
  136.  
  137.  
  138.  
  139.     FILE *f = fopen("julia.bmp", "w");
  140.  
  141.     if(write_bmp_header(f, 2 * n, n) == -1)
  142.         printf("error writing the header \n");
  143.  
  144.     for(x = 0; x < 2 * n; x++)
  145.         for(y = 0; y < n; y++)
  146.             fwrite(&v[x * n + y], sizeof(char), 3, f);
  147.  
  148.     fclose(f);
  149.     free(v);
  150.  
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment