|
| 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 calc_color(byref dat, h, y2, w, x2) |
| 23 | + local y,x,c |
| 24 | + local r = 0 |
| 25 | + local g = 0 |
| 26 | + local b = 0 |
| 27 | + local wh = w * h |
| 28 | + |
| 29 | + for y = 0 to h |
| 30 | + for x = 0 to w |
| 31 | + c = -dat[y2 + y, x2 + x] |
| 32 | + rx = ((c band 0xff0000) rshift 16) |
| 33 | + gx = ((c band 0xff00) rshift 8) |
| 34 | + bx= (c band 0xff) |
| 35 | + |
| 36 | + r += ((c band 0xff0000) rshift 16) |
| 37 | + g += ((c band 0xff00) rshift 8) |
| 38 | + b += (c band 0xff) |
| 39 | + next x |
| 40 | + next y |
| 41 | + |
| 42 | + r = r / wh |
| 43 | + g = g / wh |
| 44 | + b = b / wh |
| 45 | + |
| 46 | + local minv = min(r,g,b) |
| 47 | + local maxv = max(r,g,b) |
| 48 | + |
| 49 | + if (minv < maxv) then |
| 50 | + local diff = iff(maxv - minv == 0, 1, maxv - minv) |
| 51 | + r = int(255 * (r - minv) / diff) |
| 52 | + g = int(255 * (g - minv) / diff) |
| 53 | + b = int(255 * (b - minv) / diff) |
| 54 | + return rgb(r,g,b) |
| 55 | + else |
| 56 | + r = min(255, r) |
| 57 | + return rgb(r,r,r) |
| 58 | + endif |
| 59 | + |
| 60 | +end |
| 61 | + |
| 62 | +func get_char(byref tbl, n) |
| 63 | + local r = tbl[n] |
| 64 | + if r > 0 then return r |
| 65 | + for sn in tbl.sorted |
| 66 | + if (sn > n) then |
| 67 | + ' round up to the nearest value |
| 68 | + tbl[n] = tbl[sn] |
| 69 | + return tbl[sn] |
| 70 | + endif |
| 71 | + next n |
| 72 | + return " " |
| 73 | +end |
| 74 | + |
| 75 | +# analyze the graphic data corresponding to each character in the character set. |
| 76 | +func create_table |
| 77 | + local img,dat,i,ch,x2,n |
| 78 | + local w = txtw(chars) |
| 79 | + local h = txth(chars) |
| 80 | + local cw = txtw("1") |
| 81 | + |
| 82 | + cls: print chars |
| 83 | + img = image(0, 0, w, h, 1) |
| 84 | + img.save(dat) |
| 85 | + cls: showpage |
| 86 | + |
| 87 | + local vals = [] |
| 88 | + local minv = maxint |
| 89 | + local maxv = 0 |
| 90 | + |
| 91 | + for i = 1 to len(chars) |
| 92 | + ch = mid(chars, i, 1) |
| 93 | + x2 = ((i - 1) * cw) |
| 94 | + n = calc_block(dat, h - 1, 0, cw - 1, x2) / (w * h) |
| 95 | + minv = min(minv, n) |
| 96 | + maxv = max(maxv, n) |
| 97 | + vals << n |
| 98 | + next i |
| 99 | + |
| 100 | + ' scale the values from 0:255 |
| 101 | + local tbl = {} |
| 102 | + for i = 1 to len(chars) |
| 103 | + ch = mid(chars, i, 1) |
| 104 | + diff = iff(maxv - minv == 0, 1, maxv - minv) |
| 105 | + n = 255 * (vals[i - 1] - minv) / diff |
| 106 | + vals[i - 1] = n |
| 107 | + tbl[n] = ch |
| 108 | + next i |
| 109 | + |
| 110 | + sort vals |
| 111 | + tbl["sorted"] = vals |
| 112 | + return tbl |
| 113 | +end |
| 114 | + |
| 115 | +sub imageToAscii(path) |
| 116 | + local img,dat,pic,bly,blx,y2,x2,n,minv,maxv,row |
| 117 | + local tbl = create_table() |
| 118 | + |
| 119 | + img = image(path) |
| 120 | + img.save(dat) |
| 121 | + |
| 122 | + local w = img.width |
| 123 | + local h = img.height |
| 124 | + local blw = w / block_width |
| 125 | + local blh = h / block_height |
| 126 | + local vals = [] |
| 127 | + local minv = maxint |
| 128 | + local maxv = 0 |
| 129 | + |
| 130 | + dim pic(blh, blw) |
| 131 | + |
| 132 | + for bly = 0 to blh - 1 |
| 133 | + for blx = 0 to blw - 1 |
| 134 | + y2 = bly * block_height |
| 135 | + x2 = blx * block_width |
| 136 | + n = calc_block(dat, block_height, y2, block_width - 1, x2) / (w * h) |
| 137 | + minv = min(minv, n) |
| 138 | + maxv = max(maxv, n) |
| 139 | + pic[bly,blx] = n |
| 140 | + next blx |
| 141 | + next bly |
| 142 | + |
| 143 | + for bly = 0 to blh - 1 |
| 144 | + for blx = 0 to blw - 1 |
| 145 | + diff = iff(maxv - minv == 0, 1, maxv - minv) |
| 146 | + n = 255 * (pic[bly, blx] - minv) / diff |
| 147 | + y2 = iff(bly==0,1,bly) * block_height |
| 148 | + x2 = iff(blx==0,1,blx) * block_width |
| 149 | + color calc_color(dat, block_height, y2, block_width - 1, x2) |
| 150 | + print get_char(tbl, n); |
| 151 | + next blx |
| 152 | + print |
| 153 | + next bly |
| 154 | + showpage |
| 155 | +end |
| 156 | + |
| 157 | +imageToAscii("tree.png") |
0 commit comments