Created
February 9, 2021 02:32
-
-
Save symisc/8638b3f1959874377547b89d8a8aa801 to your computer and use it in GitHub Desktop.
Introduction to the ASCII Art C/C++ Interfaces - https://pixlab.io/art
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Compile this file together with the ASCII Art source code (https://pixlab.io/art - https://github.com/symisc/ascii_art) | |
* to generate the executable. For example: | |
* | |
* gcc -W -Wall -O6 ascii_art.c sample.c -o ascii -D ART_ENABLE_STB_IMAGE | |
* ./ascii test.png | |
* | |
* ART_ENABLE_STB_IMAGE directive must be defined in order to load images from disk. | |
* Otherwise you have to rely on an external library such as OpenCV to load the target images. | |
* | |
* If you get a compile-time error, it means that the `ascii_art.hex` model is missing. | |
* Download it from: https://pixlab.io/art | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include "ascii_art.h" | |
int main(int argc, char **argv) { | |
ascii_render sRender; /* Stack allocated */ | |
unsigned char *zText, *zBlob; | |
int width, height; | |
unsigned int nBytes; | |
if (argc < 2) { | |
puts("Missing input image"); | |
return -1; | |
} | |
/* Initialize the render structure */ | |
AsciiArtInit(&sRender); | |
/* Load the target image */ | |
zBlob = AsciiArtLoadImage(argv[1], &width, &height); | |
if (zBlob == 0) { | |
puts("Cannot load image"); | |
return -1; | |
} | |
/* Allocate a buffer big enough to hold the entire text output */ | |
nBytes = AsciiArtTextBufSize(&sRender, width, height); | |
zText = malloc(nBytes); | |
/* Finally, process */ | |
AsciiArtRender(&sRender, zBlob, &width, &height, zText, 1); | |
/* Output the result */ | |
fwrite(zText, sizeof(char), nBytes, stdout); | |
/* zBlob[] hold the binary ASCII glyphs now */ | |
/* Release memory */ | |
free(zText); | |
free(zBlob); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ASCII Art is a single file, C/C++ library developed by PixLab and based on the work of Nenad Markus that let you transform an input image or video frame into printable ASCII characters at real-time using a single decision tree. Real-time performance is achieved by using pixel intensity comparison inside internal nodes of the tree.
For a live demonstration including tests on static images, Webcam/Camera stream and a general overview on how the algorithm works, please visit https://art.pixlab.io.