|
| 1 | +#http://mattmik.com/articles/ascii/ascii.html |
| 2 | +const chars = "~`!@#$%^&*()+-=[]\;',./_+{}|:" |
| 3 | +const block_width = 4 |
| 4 | +const block_height = 6 |
| 5 | + |
| 6 | +func calc_block(byref dat, h, y2, w, x2) |
| 7 | + local y,x,c,r,g,b,n |
| 8 | + |
| 9 | + n = 0 |
| 10 | + for y = 0 to h |
| 11 | + for x = 0 to w |
| 12 | + c = -dat[y2 + y, x2 + x] |
| 13 | + r = (c band 0xff0000) rshift 16 |
| 14 | + g = (c band 0xff00) rshift 8 |
| 15 | + b = (c band 0xff) |
| 16 | + n += (r + g + b) / 3 |
| 17 | + next x |
| 18 | + next y |
| 19 | + return n |
| 20 | +end |
| 21 | + |
| 22 | +func get_char(byref tbl, n) |
| 23 | + local r = tbl[n] |
| 24 | + if r > 0 then return r |
| 25 | + for sn in tbl.sorted |
| 26 | + if (sn > n) then |
| 27 | + ' round up to the nearest value |
| 28 | + tbl[n] = tbl[sn] |
| 29 | + return tbl[sn] |
| 30 | + endif |
| 31 | + next n |
| 32 | + return " " |
| 33 | +end |
| 34 | + |
| 35 | +# analyze the graphic data corresponding to each character in the character set. |
| 36 | +func create_table |
| 37 | + local img,dat,i,ch,x2,n |
| 38 | + local w = txtw(chars) |
| 39 | + local h = txth(chars) |
| 40 | + local cw = txtw("1") |
| 41 | + |
| 42 | + cls: print chars |
| 43 | + img = image(0, 0, w, h) |
| 44 | + img.save(dat) |
| 45 | + cls |
| 46 | + |
| 47 | + local vals = [] |
| 48 | + local minv = maxint |
| 49 | + local maxv = 0 |
| 50 | + |
| 51 | + for i = 1 to len(chars) |
| 52 | + ch = mid(chars, i, 1) |
| 53 | + x2 = ((i - 1) * cw) |
| 54 | + n = calc_block(dat, h - 1, 0, cw - 1, x2) / (w * h) |
| 55 | + minv = min(minv, n) |
| 56 | + maxv = max(maxv, n) |
| 57 | + vals << n |
| 58 | + next i |
| 59 | + |
| 60 | + ' scale the values from 0:255 |
| 61 | + local tbl = {} |
| 62 | + for i = 1 to len(chars) |
| 63 | + ch = mid(chars, i, 1) |
| 64 | + n = 255 * (vals[i - 1] - minv) / (maxv - minv) |
| 65 | + vals[i - 1] = n |
| 66 | + tbl[n] = ch |
| 67 | + next i |
| 68 | + |
| 69 | + sort vals |
| 70 | + tbl["sorted"] = vals |
| 71 | + return tbl |
| 72 | +end |
| 73 | + |
| 74 | +sub imageToAscii(path) |
| 75 | + local img,dat,pic,bly,blx,y2,x2,n,minv,maxv,row |
| 76 | + local tbl = create_table() |
| 77 | + |
| 78 | + img = image(path) |
| 79 | + img.save(dat) |
| 80 | + |
| 81 | + local w = img.width |
| 82 | + local h = img.height |
| 83 | + local blw = w / block_width |
| 84 | + local blh = h / block_height |
| 85 | + local vals = [] |
| 86 | + local minv = maxint |
| 87 | + local maxv = 0 |
| 88 | + |
| 89 | + dim pic(blh, blw) |
| 90 | + |
| 91 | + for bly = 0 to blh - 1 |
| 92 | + for blx = 0 to blw - 1 |
| 93 | + y2 = bly * block_height |
| 94 | + x2 = blx * block_width |
| 95 | + n = calc_block(dat, block_height, y2, block_width - 1, x2) / (w * h) |
| 96 | + minv = min(minv, n) |
| 97 | + maxv = max(maxv, n) |
| 98 | + pic[bly,blx] = n |
| 99 | + next blx |
| 100 | + next bly |
| 101 | + |
| 102 | + for bly = 0 to blh - 1 |
| 103 | + row = "" |
| 104 | + for blx = 0 to blw - 1 |
| 105 | + n = 255 * (pic[bly,blx] - minv) / (maxv - minv) |
| 106 | + row += get_char(tbl, n) |
| 107 | + next blx |
| 108 | + print row |
| 109 | + next bly |
| 110 | +end |
| 111 | + |
| 112 | +imageToAscii("tree.png") |
0 commit comments