Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

c99 reverse iteration fractal cipher (RIFC) ppm renderer...

63 views
Skip to first unread message

Chris M. Thomasson

unread,
May 13, 2017, 6:07:48 PM5/13/17
to
I forgot to post the code here when I posted it over on sci.crypt:

https://groups.google.com/d/topic/sci.crypt/wJaqSHGehr8/discussion

Sorry. Anyway:

The c99 code for my first hyper crude stab at a portable PPM renderer
for my RIFC algorithm is at the end of this message. This is the cipher
that stores n-ary tokens, I call them nits, inside of roots of z^n+c.
The roots are defined as (z-c)^(1/n). The code is hardcoded at 2-ary, or
binary of you will.

Its not as easy as the other FFE PPM render I made in the visual puzzle
thread. I have to create the canvas in memory, plot to it, then finally
render it. This is the first time I used c99 for this. The code creates
a single ppm file called ct_cipher_rifc.ppm. Its not ready to be
uploaded to github yet. When you get some time, can you give it a go?

The output image should look like this:

https://o18i.imgup.net/ct_cipher_b95e.jpg

I need to be sure that others can run this crap before I go on.

Thank you all. Fwiw, take a look at:

https://groups.google.com/d/topic/comp.lang.c++/bB1wA4wvoFc/discussion

for some further information.

;^)

code:
__________________________________
/* Chris M. Thomasson RIFC Renderer ver:0.0.0.0 (pre-alpha)
5/13/2017 - Raw Hyper Experimental
_________________________________________________*/

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <complex.h>
#include <tgmath.h>
#include <stdbool.h>


#define CT_RAND() (rand() / (RAND_MAX - 0.0))


struct ct_axes
{
double xmin;
double xmax;
double ymin;
double ymax;
};


struct ct_canvas
{
unsigned long width;
unsigned long height;
unsigned char* buf;
};

bool
ct_canvas_create(
struct ct_canvas* const self,
unsigned long width,
unsigned long height
){
size_t size = width * height;

self->buf = calloc(1, size);

if (self->buf)
{
self->width = width;
self->height = height;

return true;
}

return false;
}

void
ct_canvas_destroy(
struct ct_canvas const* const self
){
free(self->buf);
}

bool
ct_canvas_save_ppm(
struct ct_canvas const* const self,
char const* fname
){
FILE* fout = fopen(fname, "w");

if (fout)
{
char const ppm_head[] =
"P3\n"
"# Chris M. Thomasson RIFC Cipher Renderer ver:0.0.0.0
(pre-alpha)";

fprintf(fout, "%s\n%lu %lu\n%u\n",
ppm_head,
self->width, self->height,
255U);

size_t size = self->width * self->height;

for (size_t i = 0; i < size; ++i)
{
unsigned int c = self->buf[i];
fprintf(fout, "%u %u %u ", c, 0U, 0U);
}

if (! fclose(fout))
{
return true;
}
}

return false;
}


struct ct_plane
{
struct ct_axes axes;
struct ct_canvas* canvas;
};

size_t
ct_plane_project(
struct ct_plane const* const self,
double complex c
){
double awidth = self->axes.xmax - self->axes.xmin;
double aheight = self->axes.ymax - self->axes.ymin;

double xstep = awidth / (self->canvas->width - 1.0);
double ystep = aheight / (self->canvas->height - 1.0);

size_t x = (creal(c) - self->axes.xmin) / xstep;
size_t y = (self->axes.ymax - cimag(c)) / ystep;

size_t i = x + y * self->canvas->height;

return i;
}

bool
ct_plane_plot(
struct ct_plane* const self,
double complex c,
unsigned char color
){
size_t cp = ct_plane_project(self, c);

if (cp < self->canvas->height * self->canvas->width)
{
self->canvas->buf[cp] = color;
return true;
}

return false;
}


void
ct_ifs(
struct ct_plane* const self,
double complex z,
double complex c,
double ratio,
unsigned long n
){
printf("ct_ifs: %lf%+lfi, ratio:%lf\n", creal(c), cimag(c), ratio);

for (unsigned long i = 0; i < n; ++i)
{
double complex d = z - c;
double complex root = csqrt(d);

z = root;

if (CT_RAND() > ratio)
{
z = -root;
}

ct_plane_plot(self, z, 255);
ct_plane_plot(self, root, 255);

if (! (i % 256))
{
printf("rendering: %lu of %lu\r", i + 1, n);
}
}

printf("rendering: %lu of %lu\n", n, n);
}


#define CT_WIDTH 1024
#define CT_HEIGHT 1024
#define CT_N 10000000

int main(void)
{
struct ct_canvas canvas;

bool status = ct_canvas_create(&canvas, CT_WIDTH, CT_HEIGHT);
assert(status);

struct ct_axes axes = { -2.0, 2.0, -2.0, 2.0 };
struct ct_plane plane = { axes, &canvas };

double complex z = 0+0*I;
//double complex c = -.75+.06*I;
double complex c = -.75+.06*I;
double ratio = .04;

ct_ifs(&plane, z, c, ratio, CT_N);

status = ct_canvas_save_ppm(&canvas, "ct_cipher_rifc.ppm");
assert(status);
printf("\ncreated: ct_cipher_rifc.ppm\n");

ct_canvas_destroy(&canvas);

return 0;
}
__________________________________


Any warnings or errors?
thanks.

Chris M. Thomasson

unread,
May 28, 2017, 7:43:56 PM5/28/17
to
On 5/13/2017 3:07 PM, Chris M. Thomasson wrote:
> I forgot to post the code here when I posted it over on sci.crypt:
>
> https://groups.google.com/d/topic/sci.crypt/wJaqSHGehr8/discussion
[...]

Here is a version that shows how to add antennae features to a circle
Julia set at point (0, 0). Usually, this point in a Julia is a
featureless circle. Check this C99 program out:

https://github.com/ChrisMThomasson/fractal_cipher/blob/master/RIFC/ct_bin_ppm.c

The output of this is a single PPM called: ct_cipher_rifc.ppm

And should look like:

https://plus.google.com/101799841244447089430/posts/JWwhaLefi3g

Can anybody else run it? Any issues with the code?

Can you spot places when it can be improved?

Thanks everybody. :^)

Chris M. Thomasson

unread,
May 30, 2017, 4:47:41 AM5/30/17
to
On 5/28/2017 4:43 PM, Chris M. Thomasson wrote:
> On 5/13/2017 3:07 PM, Chris M. Thomasson wrote:
>> I forgot to post the code here when I posted it over on sci.crypt:
>>
>> https://groups.google.com/d/topic/sci.crypt/wJaqSHGehr8/discussion
> [...]
>
> Here is a version that shows how to add antennae features to a circle
> Julia set at point (0, 0). Usually, this point in a Julia is a
> featureless circle. Check this C99 program out:

Fwiw, change line 167 in:

https://github.com/ChrisMThomasson/fractal_cipher/blob/master/RIFC/ct_bin_ppm.c

from:

-5.5 + I*.0

to:

-1.9 + I*.0

and this fractal pops out:


https://plus.google.com/101799841244447089430/posts/A8o4LMqmCfZ

:^)
0 new messages