Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerating moderately interesting images
    primarykey
    data
    text
    <p>Abstract: Can you propose a mathematical-ish algorithm over a plane of pixels that will generate a moderately interesting image, preferably one that on the whole resembles something?</p> <p><strong>The story thus far:</strong></p> <p>Once upon a time I decided in an effort to reduce cycle waste on my (admittedly too) numerous computers, and set out to generate images in a <em>moderately</em> interesting fashion; using a PRNG and some clever math to create images that would, on the whole, <em>resemble</em> something.</p> <p>Or at least, that was the plan. As it turns out, clever math requires being a clever mathematician; this I am not.</p> <p>At some length I arrived at a method that preferred straight lines (as these are generally the components of which our world is made), perhaps too strongly. The result is <em>mildly</em> interesting; resembling, perhaps, city grids as such:</p> <p><a href="http://totlandweb.info/imggen.out.png" rel="noreferrer">City grids, maybe? http://totlandweb.info/imggen.out.png</a></p> <p>Now for the question proper: Given the source code of this little program; can you improve upon it and propose a method that gives somewhat more interesting results? (e.g. not city grids, but perhaps faces, animals, geography, what have you)</p> <p>This is also meant as a sort of challenge; I suppose and as such I've set down some completely arbitrary and equally optional rules:</p> <ol> <li><p>The comments in the code says it all really. Suggestions and "solutions" should edit the algorithm itself, not the surrounding framework, except as for to fix errors that prevents the sample from compiling.</p></li> <li><p>The code should compile cleanly with a standard issue C compiler. (If the example provided doesn't, oops! Tell me, and I'll fix. :)</p></li> <li><p>The method should, though again, this is optional, not need to elicit help from your friendly neighborhood math library, and on the whole employ a (P)RNG as its primary data input channel.</p></li> <li><p>Solutions should probably be deliverable by simply yanking out whatever is between the snip lines (the ones that say you should not edit above and below, respectively), with a statement to the effect of what you need to add to the preamble in particular.</p></li> <li><p><strong>Edit:</strong> It is sometimes easy to forget that people on the internet cannot read my mind; but there you go. The program should require a minimum of human intervention in the generation of the images, except for to evaluate the results and chose the best ones.</p></li> </ol> <p>The code requires a C compiler and libpng to build; I'm not entirely confident that the MinGW compiler provides the necessities, but I would be surprised if it didn't. For Debian you'll want the libpng-dev package, and for Mac OS X you'll want the XCode tools..</p> <p>The source code can be <a href="http://totlandweb.info/imggen.c" rel="noreferrer">downloaded here</a>.</p> <p><strong>Warning:</strong> Massive code splurge incoming!</p> <pre><code>// compile with gcc -o imggen -lpng imggen.c // optionally with -DITERATIONS=x, where x is an appropriate integer // If you're on a Mac or using MinGW, you may have to fiddle with the linker flags to find the library and includes. #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;png.h&gt; #ifdef ITERATIONS #define REPEAT #endif // ITERATIONS // YOU MAY CHANGE THE FOLLOWING DEFINES #define WIDTH 320 #define HEIGHT 240 // YOU MAY REPLACE THE FOLLOWING DEFINES AS APPROPRIATE #define INK 16384 void writePNG (png_bytepp imageBuffer, png_uint_32 width, png_uint_32 height, int iteration) { char *fname; asprintf(&amp;fname, "out.%d.png", iteration); FILE *fp = fopen(fname, "wb"); if (!fp) return; png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_infop info_ptr = png_create_info_struct(png_ptr); png_init_io(png_ptr, fp); png_set_filter(png_ptr, PNG_FILTER_TYPE_DEFAULT, PNG_FILTER_NONE); png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_set_rows(png_ptr, info_ptr, imageBuffer); png_set_invert_mono(png_ptr); /// YOU MAY COMMENT OUT THIS LINE png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); png_destroy_write_struct(&amp;png_ptr, &amp;info_ptr); fclose(fp); free(fname); } int main (int argc, const char * argv[]) { png_uint_32 height = HEIGHT, width = WIDTH; int iteration = 1; #ifdef REPEAT for (iteration = 1; iteration &lt;= ITERATIONS; iteration++) { #endif // REPEAT png_bytepp imageBuffer = malloc(sizeof(png_bytep) * height); for (png_uint_32 i = 0; i &lt; height; i++) { imageBuffer[i] = malloc(sizeof(png_byte) * width); for (png_uint_32 j = 0; j &lt; width; j++) { imageBuffer[i][j] = 0; } } /// CUT ACROSS THE DASHED LINES /// ------------------------------------------- /// NO EDITING ABOVE THIS LINE; EXCEPT AS NOTED int ink = INK; int x = rand() % width, y = rand() % height; int xdir = (rand() % 2)?1:-1; int ydir = (rand() % 2)?1:-1; while (ink) { imageBuffer[y][x] = 255; --ink; xdir += (rand() % 2)?(1):(-1); ydir += (rand() % 2)?(1):(-1); if (ydir &gt; 0) { ++y; } else if (ydir &lt; 0) { --y; } if (xdir &gt; 0) { ++x; } else if (xdir &lt; 0) { --x; } if (x == -1 || y == -1 || x == width || y == height || x == y &amp;&amp; x == 0) { x = rand() % width; y = rand() % height; xdir = (rand() % 2)?1:-1; ydir = (rand() % 2)?1:-1; } } /// NO EDITING BELOW THIS LINE /// ------------------------------------------- writePNG(imageBuffer, width, height, iteration); for (png_uint_32 i = 0; i &lt; height; i++) { free(imageBuffer[i]); } free(imageBuffer); #ifdef REPEAT } #endif // REPEAT return 0; } </code></pre> <p><strong>Note:</strong> While this question doesn't strictly speaking seem "answerable" as such; I still believe that it can give rise to some manner of "right" answer. Maybe.</p> <p>Happy hunting.</p> <p><strong>Edit (again):</strong> The source code for the simplistic bezier paths used in my answer (read down) can be found <a href="http://totlandweb.info/bezier.h" rel="noreferrer">here</a> and <a href="http://totlandweb.info/bezier.c" rel="noreferrer">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload