add current state
This commit is contained in:
parent
de77840760
commit
bf7a7d5069
|
@ -0,0 +1,93 @@
|
|||
/**
|
||||
* copied from
|
||||
* https://stackoverflow.com/questions/2654480/writing-bmp-image-in-pure-c-c-without-other-libraries
|
||||
**/
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
#include "bmp.h"
|
||||
|
||||
/* void generate_bitmap(unsigned char* image, int h, int w, char* file_name); */
|
||||
/* unsigned char* create_bitmap_file_header(int h, int stride); */
|
||||
/* unsigned char* create_bitmap_info_header(int h, int w); */
|
||||
|
||||
void generate_bitmap(unsigned char* image, int h, int w, char* file_name)
|
||||
{
|
||||
int width_in_bytes = w * BPP;
|
||||
|
||||
unsigned char padding[3] = {0, 0, 0};
|
||||
int padding_size = (4 - (width_in_bytes) % 4) % 4;
|
||||
|
||||
int stride = (width_in_bytes) + padding_size;
|
||||
|
||||
FILE* image_file = fopen(file_name, "wb");
|
||||
|
||||
unsigned char* file_header = create_bitmap_file_header(h, stride);
|
||||
fwrite(file_header, 1, FILE_HEADER_SIZE, image_file);
|
||||
|
||||
unsigned char* info_header = create_bitmap_info_header(h, w);
|
||||
fwrite(info_header, 1, INFO_HEADER_SIZE, image_file);
|
||||
|
||||
for(int i=0;i<h;++i)
|
||||
{
|
||||
fwrite(image + (i*width_in_bytes), BPP, w, image_file);
|
||||
fwrite(padding, 1, padding_size, image_file);
|
||||
}
|
||||
|
||||
fclose(image_file);
|
||||
}
|
||||
|
||||
unsigned char* create_bitmap_file_header(int h, int stride)
|
||||
{
|
||||
int file_size = FILE_HEADER_SIZE + INFO_HEADER_SIZE + (stride * h);
|
||||
|
||||
static unsigned char file_header[] =
|
||||
{
|
||||
0,0, /*signature*/
|
||||
0,0,0,0, /*image file size in bytes*/
|
||||
0,0,0,0, /*reserved*/
|
||||
0,0,0,0, /*start of pixel array*/
|
||||
};
|
||||
|
||||
file_header[0] = (unsigned char)('B');
|
||||
file_header[1] = (unsigned char)('M');
|
||||
file_header[2] = (unsigned char)(file_size);
|
||||
file_header[3] = (unsigned char)(file_size >> 8);
|
||||
file_header[4] = (unsigned char)(file_size >> 16);
|
||||
file_header[5] = (unsigned char)(file_size >> 24);
|
||||
file_header[10] = (unsigned char)(FILE_HEADER_SIZE + INFO_HEADER_SIZE);
|
||||
|
||||
return file_header;
|
||||
}
|
||||
|
||||
unsigned char* create_bitmap_info_header(int h, int w)
|
||||
{
|
||||
static unsigned char info_header[] =
|
||||
{
|
||||
0,0,0,0, /*header size*/
|
||||
0,0,0,0, /*image width*/
|
||||
0,0,0,0, /*image height*/
|
||||
0,0, /*number of color planes*/
|
||||
0,0, /*bits per pixel*/
|
||||
0,0,0,0, /*compression*/
|
||||
0,0,0,0, /*image size*/
|
||||
0,0,0,0, /*horizontal resolution*/
|
||||
0,0,0,0, /*vertical resolution*/
|
||||
0,0,0,0, /*colors in color table*/
|
||||
0,0,0,0, /*important color count*/
|
||||
};
|
||||
|
||||
info_header[ 0] = (unsigned char)(INFO_HEADER_SIZE);
|
||||
info_header[ 4] = (unsigned char)(w);
|
||||
info_header[ 5] = (unsigned char)(w >> 8);
|
||||
info_header[ 6] = (unsigned char)(w >> 16);
|
||||
info_header[ 7] = (unsigned char)(w >> 24);
|
||||
info_header[ 8] = (unsigned char)(h);
|
||||
info_header[ 9] = (unsigned char)(h >> 8);
|
||||
info_header[10] = (unsigned char)(h >> 16);
|
||||
info_header[11] = (unsigned char)(h >> 24);
|
||||
info_header[12] = (unsigned char)(1);
|
||||
info_header[14] = (unsigned char)(BPP*8);
|
||||
|
||||
return info_header;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
#define BPP 3 /* rgb */
|
||||
#define FILE_HEADER_SIZE 14
|
||||
#define INFO_HEADER_SIZE 40
|
||||
|
||||
void generate_bitmap(unsigned char* image, int h, int w, char* file_name);
|
||||
unsigned char* create_bitmap_file_header(int height, int stride);
|
||||
unsigned char* create_bitmap_info_header(int height, int width);
|
|
@ -0,0 +1,207 @@
|
|||
/**
|
||||
* crystal.c
|
||||
*
|
||||
* Ⓒ 2023 konomo
|
||||
* EUPL-1.2
|
||||
**/
|
||||
|
||||
#include<stdio.h> /* printf; fprintf */
|
||||
#include<stdlib.h> /* atoi; rand; srand */
|
||||
#include<time.h> /* time */
|
||||
|
||||
#include "crystal.h"
|
||||
#include "bmp.h"
|
||||
|
||||
/* how many colours, max at 256 */
|
||||
int colors = 256;
|
||||
|
||||
void paint_picture();
|
||||
char new_sprout(unsigned int id);
|
||||
void process(unsigned int id, unsigned int step);
|
||||
void debug();
|
||||
void sift_struct(struct SPROUT s);
|
||||
|
||||
unsigned char image[HEIGHT][WIDTH][BPP];
|
||||
struct SPROUT col[SPROUTS]; /* collection of all created sprouts, unallocated */
|
||||
|
||||
/* struct SPROUT *csprt = NULL; /\* pointer to current sprout for quick access *\/ */
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if(colors<3) colors = 3;
|
||||
if(colors>256) colors=256;
|
||||
|
||||
char *file_name = (char*) "out.bmp";
|
||||
srand(time(NULL));
|
||||
|
||||
/* allocate ample memory for collection */
|
||||
/* col = malloc(SPROUTS * sizeof(struct SPROUT)); */
|
||||
|
||||
int i;
|
||||
for(i=0; i<SPROUTS; ++i)
|
||||
{
|
||||
char err = new_sprout(i);
|
||||
if(err==1) fprintf(stderr, "sprouting failed for id %d\n", i);
|
||||
}
|
||||
|
||||
debug();
|
||||
|
||||
for(i=0; i<SIMLENGTH; ++i)
|
||||
{
|
||||
for(int j=0; j<SPROUTS; ++j) process(j, i);
|
||||
}
|
||||
|
||||
generate_bitmap((unsigned char*)image, HEIGHT, WIDTH, file_name);
|
||||
printf("done.\n");
|
||||
|
||||
/* free(col); */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char new_sprout(unsigned int id)
|
||||
{
|
||||
unsigned int velocity[] = {1, 1, 1, 1};
|
||||
if(id>=SPROUTS) return 1;
|
||||
|
||||
unsigned int sx = rand() % WIDTH + 1;
|
||||
unsigned int sy = rand() % HEIGHT + 1;
|
||||
unsigned int gclr = rand() % colors;
|
||||
gclr*=(256/colors); /*color limiting*/
|
||||
|
||||
unsigned int coords[] = {sx, sy};
|
||||
|
||||
struct SPROUT s;
|
||||
s.ID=id;
|
||||
s.c= gclr;
|
||||
|
||||
int i;
|
||||
for(i=0; i<4; ++i){ s.v[i]=velocity[i]; }
|
||||
for(i=0; i<2; ++i){ s.coords[i]= coords[i]; }
|
||||
|
||||
col[id]=s;
|
||||
|
||||
/* if(csprt==NULL){ fprintf(stderr, "sprout for id %d is null, check sprout array.\n", id); return 1; } */
|
||||
|
||||
for(int i=0; i<3; ++i)
|
||||
{
|
||||
printf("x: %d :: y: %d :: i: %d = %d\n", sx, sy, i, gclr);
|
||||
image[sy][sx][i]=gclr;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void process(unsigned int id, unsigned int step)
|
||||
{
|
||||
/* as long as process() is only called from within */
|
||||
/* the for loop in main(), no array-length-check */
|
||||
/* is necessary. */
|
||||
/* if(id>=SPROUTS) */
|
||||
/* { */
|
||||
/* fprintf(stderr, "Cannot process, %d is bigger than SPROUTS max %d\n", id, SPROUTS); */
|
||||
/* return; */
|
||||
/* } */
|
||||
|
||||
if(col[id].killswitch==1) { /* printf("%d skipped because of killswitch\n", col[id].ID); */ return; }
|
||||
|
||||
struct SPROUT *csprt = &col[id];
|
||||
|
||||
unsigned int bounds[4][1];
|
||||
|
||||
printf("Bounds for %d(%d|%d) are:\n", csprt->ID, csprt->coords[0], csprt->coords[1]);
|
||||
|
||||
for(int i=0; i<4; ++i) /* north east south west */
|
||||
{
|
||||
unsigned int x, y; /* these are the coordinates which should be filled into image */
|
||||
unsigned int sub;
|
||||
x = csprt->coords[0];
|
||||
y = csprt->coords[1];
|
||||
|
||||
switch(i)
|
||||
{
|
||||
case 0: /* north */
|
||||
sub = ( (csprt->v[i]) * step );
|
||||
|
||||
if(y-sub<=0)
|
||||
{
|
||||
y=0;
|
||||
col[id].killswitch=1; /* prevent this sprout from being processed again */
|
||||
}
|
||||
else y-=sub;
|
||||
|
||||
/* bounds[i][0]=x; */
|
||||
bounds[i][1]=y;
|
||||
break;
|
||||
case 1: /* east */
|
||||
x+=( (csprt->v[i]) * step );
|
||||
|
||||
if(x>WIDTH)
|
||||
{
|
||||
x=WIDTH;
|
||||
col[id].killswitch=1;
|
||||
}
|
||||
|
||||
bounds[i][0]=x;
|
||||
/* bounds[i][1]=y; */
|
||||
break;
|
||||
case 2: /* south */
|
||||
y+=( (csprt->v[i]) * step);
|
||||
|
||||
if(y>HEIGHT)
|
||||
{
|
||||
y=HEIGHT;
|
||||
col[id].killswitch=1;
|
||||
}
|
||||
|
||||
/* bounds[i][0]=x; */
|
||||
bounds[i][1]=y;
|
||||
break;
|
||||
case 3: /* west */
|
||||
sub = ( (csprt->v[i]) * step);
|
||||
|
||||
if(x-sub<=0)
|
||||
{
|
||||
x=0;
|
||||
col[id].killswitch=1;
|
||||
}
|
||||
else x-=sub;
|
||||
|
||||
bounds[i][0]=x;
|
||||
/* bounds[i][1]=y; */
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "%d in for loop up to 3\n", i);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
printf("(%d|%d)\n", bounds[0][0], bounds[3][1]);
|
||||
|
||||
/* for(int i=0; i<4; ++i) */
|
||||
/* { */
|
||||
/* printf("(%d|%d) :: ", bounds[i][0], bounds[i][1]); */
|
||||
/* image[bounds[i][1]][bounds[i][0]][0]=csprt->c; */
|
||||
/* /\* image[bounds[i][1]][bounds[i][0]][1]=csprt->c; *\/ */
|
||||
/* /\* image[bounds[i][1]][bounds[i][0]][2]=csprt->c; *\/ */
|
||||
/* } */
|
||||
printf("\n\n");
|
||||
|
||||
/* printf("%d :: %d\n", csprt->ID, csprt->v[2]); */
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void debug()
|
||||
{
|
||||
for(int i=0; i<SPROUTS; ++i)sift_struct(col[i]);
|
||||
return;
|
||||
}
|
||||
|
||||
void sift_struct(struct SPROUT s)
|
||||
{
|
||||
printf("%d '%d %d %d %d' %d '%d %d'\n", s.ID, s.v[0], s.v[1], s.v[2], s.v[3], s.c, s.coords[0], s.coords[1]);
|
||||
return;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* crystal.h
|
||||
* Ⓒ 2023 konomo
|
||||
* EUPL-1.2
|
||||
**/
|
||||
|
||||
/* for 4k */
|
||||
/* #define WIDTH 3840 */
|
||||
/* #define HEIGHT 2160 */
|
||||
|
||||
/* for fullhd */
|
||||
#define WIDTH 1920
|
||||
#define HEIGHT 1080
|
||||
|
||||
/* 360 p*/
|
||||
/* #define WIDTH 480 */
|
||||
/* #define HEIGHT 360 */
|
||||
|
||||
/* #define SPROUTS 50000 /\* recommended for 4k *\/ */
|
||||
/* #define SPROUTS 5000 /\* recommended for fullhd *\/o */
|
||||
/* #define SPROUTS 500 /\* recommended for 360p *\/ */
|
||||
#define SPROUTS 1 /* delete */
|
||||
|
||||
/* length of simulation */
|
||||
/* #define SIMLENGTH 10000 */
|
||||
#define SIMLENGTH 500
|
||||
/* #define SIMLENGTH 1 */
|
||||
|
||||
struct SPROUT
|
||||
{
|
||||
char killswitch; /* if this is set, do not bother with the sprout */
|
||||
|
||||
unsigned int ID;
|
||||
unsigned int v[4]; /* velocity in cardinal directions */
|
||||
unsigned char c; /* color */
|
||||
unsigned int coords[2]; /* initial coordinates from which to expand */
|
||||
};
|
Loading…
Reference in New Issue