aatest2.bac
'-----------------------------------------------------------------------------------------------------------------------
CONST file$ = DIRNAME$(ME$) & "/" & "foto.png" : ' File to convert to ASCII
PROTO stbi_image_free aa_putpixel aa_render aa_flush aa_close : ' Make sure the BaCon parser accepts these
ALIAS aa_text TO aa_text$ : ' BaCon should handle this as a string function
'-----------------------------------------------------------------------------------------------------------------------
PRAGMA INCLUDE canvas/stb-master/stb_image.h : ' Include the stb_image header file
PRAGMA OPTIONS -DSTB_IMAGE_IMPLEMENTATION -DSTBI_FAILURE_USERMSG : ' Enable graphical functions and user error messages
DECLARE Wd, Ht, n TYPE int
DECLARE data TYPE unsigned char*
data = stbi_load(file$, &Wd, &Ht, &n, 4) : ' Load the actual image in full RGBA color (4 channels)
IF data = NULL THEN
PRINT stbi_failure_reason() FORMAT "===> ERROR: %s.\n" : ' Explain error if there is any
END 1
ENDIF
'-----------------------------------------------------------------------------------------------------------------------
PRAGMA INCLUDE <aalib.h> : ' Include the Ascii Art library header file
PRAGMA LDFLAGS aa : ' Link with libaa
DECLARE context TYPE aa_context* : ' This is the AALib memory pointer
aa_defparams.font = &aa_fontcourier : ' Font we're using for our image
aa_defparams.supported = AA_NORMAL_MASK : ' Supported rendering types AA_NORMAL_MASK,AA_DIM_MASK,AA_BOLD_MASK,AA_BOLDFONT_MASK,AA_REVERSE_MASK
aa_defparams.width = Wd/2 : ' Width and height of resulting ASCII Art picture.
aa_defparams.height = Ht/2 : ' -> The aalib output sizes are 1/2 image input (2x2 pixels -> 1 char)
DECLARE aa_saved TYPE aa_savedata : ' This is the AALib save indication
aa_saved.name = "/tmp/img.txt"
aa_saved.format = &aa_ansi_format
aa_saved.file = NULL
context = aa_init(&save_d, &aa_defparams, &aa_saved) : ' Initialize driver
OPTION MEMTYPE int
FOR y = 0 TO Ht-2
FOR x = 0 TO Wd-2
aa_putpixel(context, x, y, PEEK(data+(y*Wd+x)*4)/65536) : ' Put the pixels from STB loaded image into AA memory
NEXT
NEXT
aa_defrenderparams.bright = 30 : ' Add BRIGHTNESS to the resulting image (int 0=normal to 255=white)
aa_defrenderparams.contrast = 50 : ' Add CONTRAST to the resulting image (int 0=normal to 127=white)
aa_defrenderparams.gamma = 1.0 : ' Add GAMMA to the resulting image (float 0 to 1)
aa_defrenderparams.inversion = FALSE : ' Should the resulting image be inverted
aa_defrenderparams.dither = AA_FLOYD_S : ' Add DITHERING to the resulting image (AA_NONE, AA_ERRORDISTRIB, AA_FLOYD_S)
aa_defrenderparams.randomval = 0 : ' Range for random value to create dithering effect (int)
aw = aa_scrwidth(context) : ' Obtain width
ah = aa_scrheight(context) : ' Obtain height
aa_render(context, &aa_defrenderparams, 0, 0, aw, ah) : ' Now let AAlib render the picture
aa_flush(context) : ' Flush the result, obligatory otherwise we do not get a picture - then show with Xterm
SYSTEM "xterm -hold -fa 'Monospace' -fs 4 -geometry " & STR$(aw) & "x" & STR$(ah) & " -title \"ASCII Art\" -e \"echo -e '\e[38;5;155m\e[48;5;234m'; cat /tmp/img.txt\""
DELETE FILE "/tmp/img.txt" : ' Delete generated file with our ASCII art
aa_close(context) : ' Free memory from AAlib
'-----------------------------------------------------------------------------------------------------------------------
stbi_image_free(data) : ' Free memory from STB
'-----------------------------------------------------------------------------------------------------------------------
Generated by GNU Enscript 1.6.5.90.