Author Topic: BMP to ASCII  (Read 3445 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
BMP to ASCII
« on: February 02, 2019, 03:42:57 pm »
Code: QB64: [Select]
  1. _TITLE "Heather Thomas by rfrost@mail.com"
  2. DEFINT A-Z
  3. DIM valid(190), pixel(20, 20), CharDef(127, 15)
  4. q = 32
  5. PALETTE 15, q * 65536 + q * 256 + q
  6. FOR a = 0 TO 127 ' read/store/evaluate charset
  7.     READ CharDef$
  8.     OkUse = -1 ' assume can use the char, for now
  9.     c$ = CHR$(a)
  10.     IF INSTR("~+/\", c$) THEN OkUse = 0
  11.     IF a < 65 THEN OkUse = 0
  12.     IF UCASE$(c$) = LCASE$(c$) THEN OkUse = 0
  13.     IF OkUse THEN
  14.         FOR byte = 0 TO 15
  15.             pat = VAL("&H" + MID$(CharDef$, byte * 2 + 1, 2))
  16.             CharDef(a, byte) = pat
  17.             IF a < 33 THEN OkUse = 0 ' unprintable
  18.             IF (pat AND 1) THEN OkUse = 0 ' no far right column
  19.             IF (pat > 0) AND (byte < 2) THEN OkUse = 0 ' no top 2 rows
  20.             IF (pat > 0) AND (byte > 11) THEN OkUse = 0 ' no bottom 4 rows
  21.         NEXT byte
  22.     END IF
  23.     IF OkUse THEN valid = valid + 1: valid(valid) = a
  24. xb = 20: xe = 350 ' x begin and end
  25. yb = 0: ye = 430 ' y begin and end
  26. OPEN "heath.bmp" FOR RANDOM AS #1 LEN = 1: FIELD #1, 1 AS t$
  27. OPEN "heather.txt" FOR OUTPUT AS #2
  28. PRINT #2, CHR$(27); "A"; CHR$(8);
  29. FOR y1 = yb TO ye STEP 14
  30.     OutLine$ = ""
  31.     FOR x1 = xb TO xe STEP 8
  32.         OutChar$ = " "
  33.         PixelLit = 0
  34.         FOR y2 = 0 TO 7
  35.             FOR x2 = 0 TO 6
  36.                 x3 = x1 + x2
  37.                 y3 = y1 + y2
  38.                 r& = CDBL(479 - y3) * 640 + x3 + 441
  39.                 GET #1, r&
  40.                 d = ASC(t$) \ 15
  41.                 IF d > 15 THEN d = 15
  42.                 pixel(x2, y2) = -(d > 9) ' reverse for printer
  43.                 PixelLit = PixelLit + pixel(x2, y2)
  44.             NEXT x2
  45.         NEXT y2
  46.         IF PixelLit THEN
  47.             LINE (x, y)-(x + 6, y + 9), 0, BF
  48.             BestScore = -999
  49.             SaveASCII = 32
  50.             FOR a = 1 TO valid
  51.                 s = 0
  52.                 FOR byte = 0 TO 9
  53.                     n = CharDef(valid(a), byte + 2)
  54.                     FOR bit = 0 TO 6
  55.                         p = pixel(bit, byte)
  56.                         b = SGN(n AND 128) ' XOR 1
  57.                         n = (n AND 127) * 2
  58.                         s = s - (p = b)
  59.                     NEXT bit
  60.                 NEXT byte
  61.                 IF s > BestScore THEN
  62.                     BestScore = s
  63.                     SaveASCII = valid(a)
  64.                 END IF
  65.             NEXT a
  66.             IF SaveASCII <> 32 THEN
  67.                 OutChar$ = CHR$(SaveASCII)
  68.                 FOR byte = 0 TO 9
  69.                     p = CharDef(SaveASCII, byte + 2)
  70.                     FOR bit = 0 TO 6
  71.                         IF (p AND 128) THEN PSET (120 + x1 + bit, 8 + y1 + byte), 15
  72.                         p = (p AND 127) * 2
  73.                     NEXT bit
  74.                 NEXT byte
  75.             END IF
  76.         END IF
  77.         OutLine$ = OutLine$ + OutChar$
  78.     NEXT x1
  79.     IF INKEY$ = CHR$(27) THEN CLOSE: SYSTEM
  80.     PRINT #2, "."; OutLine$; CHR$(13); CHR$(10);
  81. NEXT y1
  82. t$ = "output file heather.txt created"
  83. LOCATE 29, 40 - LEN(t$) \ 2: PRINT t$;
  84.  
  85. DATA 00000000000000000000000000000000
  86. DATA 00007E81A58181BD9981817E00000000
  87. DATA 00007EFFDBFFFFC3E7FFFF7E00000000
  88. DATA 000000006CFEFEFEFE7C381000000000
  89. DATA 0000000010387CFE7C38100000000000
  90. DATA 000000183C3CE7E7E718183C00000000
  91. DATA 000000183C7EFFFF7E18183C00000000
  92. DATA 000000000000183C3C18000000000000
  93. DATA FFFFFFFFFFFFE7C3C3E7FFFFFFFFFFFF
  94. DATA 00000000003C664242663C0000000000
  95. DATA FFFFFFFFFFC399BDBD99C3FFFFFFFFFF
  96. DATA 00001E0E1A3278CCCCCCCC7800000000
  97. DATA 00003C666666663C187E181800000000
  98. DATA 00003F333F3030303070F0E000000000
  99. DATA 00007F637F6363636367E7E6C0000000
  100. DATA 0000001818DB3CE73CDB181800000000
  101. DATA 0080C0E0F0F8FEF8F0E0C08000000000
  102. DATA 0002060E1E3EFE3E1E0E060200000000
  103. DATA 0000183C7E1818187E3C180000000000
  104. DATA 00006666666666666600666600000000
  105. DATA 00007FDBDBDB7B1B1B1B1B1B00000000
  106. DATA 007CC660386CC6C66C380CC67C000000
  107. DATA 0000000000000000FEFEFEFE00000000
  108. DATA 0000183C7E1818187E3C187E00000000
  109. DATA 0000183C7E1818181818181800000000
  110. DATA 0000181818181818187E3C1800000000
  111. DATA 0000000000180CFE0C18000000000000
  112. DATA 00000000003060FE6030000000000000
  113. DATA 000000000000C0C0C0FE000000000000
  114. DATA 0000000000286CFE6C28000000000000
  115. DATA 000000001038387C7CFEFE0000000000
  116. DATA 00000000FEFE7C7C3838100000000000
  117. DATA 00000000000000000000000000000000
  118. DATA 0000183C3C3C18181800181800000000
  119. DATA 00666666240000000000000000000000
  120. DATA 0000006C6CFE6C6C6CFE6C6C00000000
  121. DATA 18187CC6C2C07C060686C67C18180000
  122. DATA 00000000C2C60C183060C68600000000
  123. DATA 0000386C6C3876DCCCCCCC7600000000
  124. DATA 00303030600000000000000000000000
  125. DATA 00000C18303030303030180C00000000
  126. DATA 000030180C0C0C0C0C0C183000000000
  127. DATA 0000000000663CFF3C66000000000000
  128. DATA 000000000018187E1818000000000000
  129. DATA 00000000000000000018181830000000
  130. DATA 00000000000000FE0000000000000000
  131. DATA 00000000000000000000181800000000
  132. DATA 0000000002060C183060C08000000000
  133. DATA 0000386CC6C6D6D6C6C66C3800000000
  134. DATA 00001838781818181818187E00000000
  135. DATA 00007CC6060C183060C0C6FE00000000
  136. DATA 00007CC606063C060606C67C00000000
  137. DATA 00000C1C3C6CCCFE0C0C0C1E00000000
  138. DATA 0000FEC0C0C0FC060606C67C00000000
  139. DATA 00003860C0C0FCC6C6C6C67C00000000
  140. DATA 0000FEC606060C183030303000000000
  141. DATA 00007CC6C6C67CC6C6C6C67C00000000
  142. DATA 00007CC6C6C67E0606060C7800000000
  143. DATA 00000000181800000018180000000000
  144. DATA 00000000181800000018183000000000
  145. DATA 000000060C18306030180C0600000000
  146. DATA 00000000007E00007E00000000000000
  147. DATA 0000006030180C060C18306000000000
  148. DATA 00007CC6C60C18181800181800000000
  149. DATA 0000007CC6C6DEDEDEDCC07C00000000
  150. DATA 000010386CC6C6FEC6C6C6C600000000
  151. DATA 0000FC6666667C66666666FC00000000
  152. DATA 00003C66C2C0C0C0C0C2663C00000000
  153. DATA 0000F86C6666666666666CF800000000
  154. DATA 0000FE6662687868606266FE00000000
  155. DATA 0000FE6662687868606060F000000000
  156. DATA 00003C66C2C0C0DEC6C6663A00000000
  157. DATA 0000C6C6C6C6FEC6C6C6C6C600000000
  158. DATA 00003C18181818181818183C00000000
  159. DATA 00001E0C0C0C0C0CCCCCCC7800000000
  160. DATA 0000E666666C78786C6666E600000000
  161. DATA 0000F06060606060606266FE00000000
  162. DATA 0000C6EEFEFED6C6C6C6C6C600000000
  163. DATA 0000C6E6F6FEDECEC6C6C6C600000000
  164. DATA 00007CC6C6C6C6C6C6C6C67C00000000
  165. DATA 0000FC6666667C60606060F000000000
  166. DATA 00007CC6C6C6C6C6C6D6DE7C0C0E0000
  167. DATA 0000FC6666667C6C666666E600000000
  168. DATA 00007CC6C660380C06C6C67C00000000
  169. DATA 00007E7E5A1818181818183C00000000
  170. DATA 0000C6C6C6C6C6C6C6C6C67C00000000
  171. DATA 0000C6C6C6C6C6C6C66C381000000000
  172. DATA 0000C6C6C6C6D6D6D6FEEE6C00000000
  173. DATA 0000C6C66C7C38387C6CC6C600000000
  174. DATA 0000666666663C181818183C00000000
  175. DATA 0000FEC6860C183060C2C6FE00000000
  176. DATA 00003C30303030303030303C00000000
  177. DATA 00000080C0E070381C0E060200000000
  178. DATA 00003C0C0C0C0C0C0C0C0C3C00000000
  179. DATA 10386CC6000000000000000000000000
  180. DATA 00000000000000000000000000FF0000
  181. DATA 30301800000000000000000000000000
  182. DATA 0000000000780C7CCCCCCC7600000000
  183. DATA 0000E06060786C666666667C00000000
  184. DATA 00000000007CC6C0C0C0C67C00000000
  185. DATA 00001C0C0C3C6CCCCCCCCC7600000000
  186. DATA 00000000007CC6FEC0C0C67C00000000
  187. DATA 0000386C6460F060606060F000000000
  188. DATA 000000000076CCCCCCCCCC7C0CCC7800
  189. DATA 0000E060606C7666666666E600000000
  190. DATA 00001818003818181818183C00000000
  191. DATA 00000606000E06060606060666663C00
  192. DATA 0000E06060666C78786C66E600000000
  193. DATA 00003818181818181818183C00000000
  194. DATA 0000000000ECFED6D6D6D6C600000000
  195. DATA 0000000000DC66666666666600000000
  196. DATA 00000000007CC6C6C6C6C67C00000000
  197. DATA 0000000000DC66666666667C6060F000
  198. DATA 000000000076CCCCCCCCCC7C0C0C1E00
  199. DATA 0000000000DC7666606060F000000000
  200. DATA 00000000007CC660380CC67C00000000
  201. DATA 0000103030FC30303030361C00000000
  202. DATA 0000000000CCCCCCCCCCCC7600000000
  203. DATA 000000000066666666663C1800000000
  204. DATA 0000000000C6C6D6D6D6FE6C00000000
  205. DATA 0000000000C66C3838386CC600000000
  206. DATA 0000000000C6C6C6C6C6C67E060CF800
  207. DATA 0000000000FECC183060C6FE00000000
  208. DATA 00000E18181870181818180E00000000
  209. DATA 00001818181800181818181800000000
  210. DATA 0000701818180E181818187000000000
  211. DATA 000076DC000000000000000000000000
  212. DATA 0000000082986CC6C6C6FE0000000000
  213.  
  214.  
HEATH.BMP
* HEATH.BMP (Filesize: 301.05 KB, Dimensions: 640x480, Views: 326)
It works better if you plug it in.