Skip to content

Instantly share code, notes, and snippets.

@vijfhoek
Last active December 28, 2015 03:59
Show Gist options
  • Save vijfhoek/7438775 to your computer and use it in GitHub Desktop.
Save vijfhoek/7438775 to your computer and use it in GitHub Desktop.
#define _CRT_SECURE_NO_WARNINGS /* Shut up, Visual Studio */
#include <stdio.h>
#include <stdlib.h>
#include <SOIL.h>
const char *asciichars = "#*+=-:. ";
int main(int argc, char **argv) {
if (argc != 3) { printf("Syntax: %s <infile> <outfile>\n", argv[0]); return -1; }
int width, height, channels;
unsigned char *data = SOIL_load_image(argv[1], &width, &height, &channels, 1);
if (!data) { printf("Loading error: %s\n", SOIL_last_result()); return -1; }
char *output = new char[(height/2 + 1) * (width+1)];
int i = 0;
for (int y = 0; y < height; y += 2) { /* +2 to compensate for characters being higher than they're wide */
for (int x = 0; x < width; ++x)
output[i++] = asciichars[data[y * width + x] >> 5];
output[i++] = '\n';
}
FILE *out = fopen(argv[2], "w");
if (!out) return -1;
fwrite(output, 1, height/2 * (width + 1), out);
fclose(out);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment