aatest2.bac
'-----------------------------------------------------------------------------------------------------------------------
CONST file$ = DIRNAME$(ME$) & "/" & "BaCon.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, 1) : ' Load the actual image in greyscale
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)
context = aa_init(&mem_d, &aa_defparams, NULL) : ' Initialize memory driver
FOR y = 0 TO Ht-2
FOR x = 0 TO Wd-2
aa_putpixel(context, x, y, PEEK(data+y*Wd+x)) : ' Put the pixels from STB loaded image into AA memory
NEXT
NEXT
aa_defrenderparams.bright = 0 : ' Add BRIGHTNESS to the resulting image (int 0=normal to 255=white)
aa_defrenderparams.contrast = 0 : ' Add CONTRAST to the resulting image (int 0=normal to 127=white)
aa_defrenderparams.gamma = 0.8 : ' 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)
awidth = aa_scrwidth(context) : ' Obtain width
aheight = aa_scrheight(context) : ' Obtain height
aa_render(context, &aa_defrenderparams, 0, 0, awidth, aheight) : ' Now let AAlib render the picture in memory
aa_flush(context) : ' Flush the result, obligatory otherwise we do not see a picture
SAVE aa_text$(context) TO "/tmp/img.txt" : ' Save ASCII art to temporary file and display in XTerm
SYSTEM "xterm -fa 'Monospace' -fs 4 -geometry " & STR$(awidth) & "x" & STR$(aheight) & " -title \"ASCII Art\" -e \"cat /tmp/img.txt; read\""
DELETE FILE "/tmp/img.txt" : ' Delete temporary file
aa_close(context) : ' Free memory from AAlib
'-----------------------------------------------------------------------------------------------------------------------
stbi_image_free(data) : ' Free memory from STB
'-----------------------------------------------------------------------------------------------------------------------
Generated by GNU Enscript 1.6.5.90.