Chris_M_Thomasson

Reverse Julia Hybrid Test:0

Jun 4th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.58 KB | None | 0 0
  1. /*
  2.     Chris M. Thomasson's Hybrid Reverse 2-ary Julia
  3.     Copyright (c) 6/4/2017
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. *_____________________________________________________________*/
  18.  
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <assert.h>
  23. #include <complex.h>
  24. #include <tgmath.h>
  25. #include <stdbool.h>
  26.  
  27.  
  28. #define CT_RAND() (rand() / (RAND_MAX - 0.0))
  29.  
  30.  
  31. struct ct_axes
  32. {
  33.     double xmin;
  34.     double xmax;
  35.     double ymin;
  36.     double ymax;
  37. };
  38.  
  39.  
  40. struct ct_color
  41. {
  42.     unsigned char r;
  43.     unsigned char g;
  44.     unsigned char b;
  45.     unsigned char a;
  46. };
  47.  
  48.  
  49. struct ct_canvas
  50. {
  51.     unsigned long width;
  52.     unsigned long height;
  53.     struct ct_color* buf; // gxxx
  54. };
  55.  
  56. bool
  57. ct_canvas_create(
  58.     struct ct_canvas* const self,
  59.     unsigned long width,
  60.     unsigned long height
  61. ){
  62.     size_t size = width * height * sizeof(*self->buf);
  63.  
  64.     self->buf = calloc(1, size);
  65.  
  66.     if (self->buf)
  67.     {
  68.         self->width = width;
  69.         self->height = height;
  70.  
  71.         return true;
  72.     }
  73.  
  74.     return false;
  75. }
  76.  
  77. void
  78. ct_canvas_destroy(
  79.     struct ct_canvas const* const self
  80. ){
  81.     free(self->buf);
  82. }
  83.  
  84. bool
  85. ct_canvas_save_ppm(
  86.     struct ct_canvas const* const self,
  87.     char const* fname
  88. ){
  89.     FILE* fout = fopen(fname, "w");
  90.  
  91.     if (fout)
  92.     {
  93.         char const ppm_head[] =
  94.             "P3\n"
  95.             "# Chris M. Thomasson RIFC Cipher Renderer ver:0.0.0.0 (pre-alpha)";
  96.  
  97.         fprintf(fout, "%s\n%lu %lu\n%u\n",
  98.                 ppm_head,
  99.                 self->width, self->height,
  100.                 255U);
  101.  
  102.         size_t size = self->width * self->height;
  103.  
  104.         for (size_t i = 0; i < size; ++i)
  105.         {
  106.             struct ct_color* c = &self->buf[i];
  107.             fprintf(fout, "%u %u %u  ", c->r, c->g, c->b);
  108.         }
  109.  
  110.         if (! fclose(fout))
  111.         {
  112.             return true;
  113.         }
  114.     }
  115.  
  116.     return false;
  117. }
  118.  
  119.  
  120. struct ct_plane
  121. {
  122.     struct ct_axes axes;
  123.     struct ct_canvas* canvas;
  124. };
  125.  
  126. size_t
  127. ct_plane_project(
  128.     struct ct_plane const* const self,
  129.     double complex c
  130. ){
  131.     double awidth = self->axes.xmax - self->axes.xmin;
  132.     double aheight = self->axes.ymax - self->axes.ymin;
  133.  
  134.     // assert for avoiding div-by-zero
  135.     assert(self->canvas->width > 1 &&
  136.            self->canvas->height > 1);
  137.  
  138.     double xstep = awidth / (self->canvas->width - 1.0);
  139.     double ystep = aheight / (self->canvas->height - 1.0);
  140.  
  141.     size_t x = (creal(c) - self->axes.xmin) / xstep;
  142.     size_t y = (self->axes.ymax - cimag(c)) / ystep;
  143.  
  144.     size_t i = x + y * self->canvas->height;
  145.  
  146.     return i;
  147. }
  148.  
  149. bool
  150. ct_plane_plot(
  151.     struct ct_plane* const self,
  152.     double complex c,
  153.     struct ct_color color
  154. ){
  155.     size_t cp = ct_plane_project(self, c);
  156.  
  157.     if (cp < self->canvas->height * self->canvas->width)
  158.     {
  159.         self->canvas->buf[cp] = color;
  160.         return true;
  161.     }
  162.  
  163.     return false;
  164. }
  165.  
  166.  
  167. bool
  168. ct_plane_plot_gradient(
  169.     struct ct_plane* const self,
  170.     double complex c,
  171.     struct ct_color color
  172. ){
  173.     size_t cp = ct_plane_project(self, c);
  174.  
  175.     if (cp < self->canvas->height * self->canvas->width)
  176.     {
  177.         struct ct_color temp = self->canvas->buf[cp];
  178.  
  179.         color.r = temp.r + color.r;
  180.         color.g = temp.g + color.g;
  181.         color.b = temp.b + color.b;
  182.         color.a = temp.a + color.a;
  183.  
  184.         if (color.r < temp.r) color.r = temp.r;
  185.         if (color.g < temp.g) color.g = temp.g;
  186.         if (color.b < temp.b) color.b = temp.b;
  187.         if (color.a < temp.a) color.a = temp.a;
  188.  
  189.         self->canvas->buf[cp] = color;
  190.  
  191.  
  192.         return true;
  193.     }
  194.  
  195.     return false;
  196. }
  197.  
  198.  
  199. // selective light
  200. void
  201. ct_slight(
  202.     struct ct_plane* const self
  203. ){
  204.     size_t size = self->canvas->width * self->canvas->height;
  205.  
  206.     for (size_t i = 0; i < size; ++i)
  207.     {
  208.         struct ct_color* color = &self->canvas->buf[i];
  209.  
  210.         if (color->r > 0 && color->r < 10)
  211.         {
  212.             color->r = color->r * 123;
  213.             color->g = color->g * 123;
  214.         }
  215.  
  216.         /*
  217.         if (color->r > 0 && color->r < 10)
  218.         {
  219.             color->r = (color->r + 13) * 13;
  220.         }
  221.  
  222.         if (color->g > 0 && color->g < 10)
  223.         {
  224.             color->g = (color->g + 13) * 13;
  225.         }
  226.         */
  227.     }
  228. }
  229.  
  230.  
  231. // Compute the forward fractal
  232. void
  233. ct_forward(
  234.     struct ct_plane* const self,
  235.     double complex z,
  236.     double complex c,
  237.     double ratio,
  238.     unsigned long n
  239. ){
  240.     for (unsigned long i = 0; i < n; ++i)
  241.     {
  242.         z = z*z + c;
  243.         z = z * ratio;
  244.  
  245.         //struct ct_color c0 = { (i + 1) * 12345, i,  i * 4, 0 };
  246.         //ct_plane_plot(self, z, c0);
  247.  
  248.         struct ct_color c0 = { 2, i % 2, 2, 0 };
  249.         ct_plane_plot_gradient(self, z, c0);
  250.     }
  251. }
  252.  
  253.  
  254. // Compute the reverse fractal
  255. void
  256. ct_ifs(
  257.     struct ct_plane* const self,
  258.     double complex z,
  259.     double complex c,
  260.     double ratio,
  261.     unsigned long n
  262. ){
  263.     // 2 sets
  264.     double complex jp[] = {
  265.         .26 + I*0,
  266.         -1.9 + I*.0
  267.     };
  268.  
  269.     ratio = .95;
  270.  
  271.     printf("ct_ifs: %lf%+lfi, %lf%+lfi, ratio:%lf\n",
  272.            creal(jp[0]), cimag(jp[0]),
  273.            creal(jp[1]), cimag(jp[1]),
  274.            ratio);
  275.  
  276.     for (unsigned long i = 0; i < n; ++i)
  277.     {
  278.         //double ir = i / (n - 1.);
  279.         double rn0 = rand() / (RAND_MAX - .0);
  280.         double rn1 = rand() / (RAND_MAX - .0);
  281.  
  282.         if (rn0 > -5)
  283.         {
  284.             c = jp[0];
  285.         }
  286.  
  287.         else
  288.         {
  289.             c = jp[1];
  290.         }
  291.  
  292.  
  293.         double complex d = z - c;
  294.         double complex root = csqrt(d);
  295.  
  296.         z = root;
  297.  
  298.         if (rn1 > ratio)
  299.         {
  300.             z = -root;
  301.         }
  302.  
  303.         ct_forward(self, z, z, .7, 5);
  304.  
  305.         struct ct_color c0 = { 2, 0, 0, 0 };
  306.         struct ct_color c1 = { 0, 0, 2, 0 };
  307.  
  308.  
  309.         /*
  310.         double esc = .9;
  311.         if (creal(z) > fabs(cos(ir * creal(z))) * esc)
  312.         {
  313.             z = -creal(z) * 1.4 + cimag(z) * I;
  314.         }
  315.         */
  316.  
  317.  
  318.         ct_plane_plot_gradient(self, z, c0);
  319.         ct_plane_plot_gradient(self, root, c1);
  320.  
  321.         if (! (i % 256))
  322.         {
  323.             printf("rendering: %lu of %lu\r", i + 1, n);
  324.         }
  325.     }
  326.  
  327.     printf("rendering: %lu of %lu\n", n, n);
  328. }
  329.  
  330.  
  331. #define CT_WIDTH 1024
  332. #define CT_HEIGHT 1024
  333. #define CT_N 20000000
  334.  
  335. int main(void)
  336. {
  337.     struct ct_canvas canvas;
  338.  
  339.     bool status = ct_canvas_create(&canvas, CT_WIDTH, CT_HEIGHT);
  340.     assert(status);
  341.  
  342.     double radius = 2.25;
  343.     struct ct_axes axes = { -radius, radius, -radius, radius };
  344.     struct ct_plane plane = { axes, &canvas };
  345.  
  346.     // Julia circle at origin z and Julia point c = 0+0i
  347.     double complex z = 0+0*I;
  348.     double complex c = 0+0*I; // Julia
  349.     double ratio = .5;
  350.  
  351.     ct_ifs(&plane, z, c, ratio, CT_N);
  352.  
  353.     ct_slight(&plane);
  354.  
  355.     status = ct_canvas_save_ppm(&canvas, "ct_cipher_rifc.ppm");
  356.     assert(status);
  357.     printf("\ncreated: ct_cipher_rifc.ppm\n");
  358.  
  359.     ct_canvas_destroy(&canvas);
  360.  
  361.     return 0;
  362. }
Advertisement
Add Comment
Please, Sign In to add comment