Skip to content

Instantly share code, notes, and snippets.

@stolk
Created June 21, 2023 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stolk/ea29bc05fe2d1b9e25748b2d856eea37 to your computer and use it in GitHub Desktop.
Save stolk/ea29bc05fe2d1b9e25748b2d856eea37 to your computer and use it in GitHub Desktop.
Blue Paint
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <assert.h>
int blw;
int blh;
uint8_t* blu;
int imw;
int imh;
uint8_t* img;
const char* sample(int x, int y)
{
// Randomly offset sample, differently for both samples.
int sx0 = 13 + x;
int sy0 = 31 + y;
int sx1 = 43248 - y;
int sy1 = 11033 - x;
// Wrap around in noise field.
sx0 &= (blw-1);
sy0 &= (blh-1);
sx1 &= (blw-1);
sy1 &= (blh-1);
int ix = x/2;
int iy = y/2;
uint8_t v0 = img[(iy * imw + ix) * 3 + 0]; // red value of pixel.
uint8_t v1 = img[(iy * imw + ix) * 3 + 1]; // grn value of pixel.
v0 = v0/2;
v1 = v1/2;
const uint8_t nv0 = blu[sy0 * blw + sx0];
const uint8_t nv1 = blu[sy1 * blw + sx1];
if (nv0 < v0)
return "🌵";
if (nv1 < v1)
return "🪨";
return "..";
}
int main(int argc, char* argv[])
{
if (argc!=3)
{
fprintf(stderr, "Usage: %s design.ppm noisefield.pgm\n", argv[0]);
exit(1);
}
const char* dname = argv[1];
const char* nname = argv[2];
// Read in design.
FILE* f = fopen(dname, "rb");
assert(f);
const int numr = fscanf(f, "P6 %d %d 255\n", &imw, &imh);
assert(numr==2);
img = (uint8_t*) malloc(imw*imh*3);
const int numl = fread(img, imw*3, imh, f);
fprintf(stderr,"read %d lines\n", numl);
assert(numl == imh);
fclose(f);
fprintf(stderr, "Read design of size %dx%d\n", imw, imh);
// Read in noise field.
f = fopen(nname, "rb");
assert(f);
const int nums = fscanf(f, "P5 %d %d 255\n", &blw, &blh);
assert(nums==2);
blu = (uint8_t*) malloc(blw*blh);
const int numl2 = fread(blu, blh, blw, f);
assert(numl2 == blh);
fclose(f);
fprintf(stderr, "Read noise field of size %dx%d\n", blw, blh);
for (int y=0; y<imh*2; ++y)
{
for (int x=0; x<imw*2; ++x)
printf("%s", sample(x,y));
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment