News:

Welcome to RetroCoders Community

Main Menu

bmp2draw

Started by mysoft, Jan 06, 2024, 04:18 PM

Previous topic - Next topic

mysoft

this is a program written in freebasic that converts a .bmp file into a .txt file that can be used along the gwbasic/qbasic/freebasic draw command.

#include "fbgfx.bi"

dim as long Wid,Hei
dim as short wHdr
var sFile = command$
if len(sFile)=0 then
  print "BMP2TXT Input.bmp"
  print "Will generate a Input.txt suitable for draw"
  end 0
end if
if open(sFile for binary access read as #1) then
  print "failed to open '"+sFile+"'"
  end 2
end if
get #1,,wHdr 
if wHdr<>cvshort("BM") then
  print "input must be a paletted .bmp file"
  end 1
end if
get #1,19,Wid : Get #1,,Hei : close #1
screenres 640,480,8,,fb.GFX_NULL
var pImg = ImageCreate(Wid,Hei)
bload sFile,pImg

var iPos = instrrev(sFile,".")
var sFileOut = left(sFile,iPos)+"txt"

open sFileOut for output as #2

var sRow = "",X=0
for Y as long = 0 to Hei-1  
  var iLastC=-1, iC=0, N=0
  for X = 0 to Wid-1
    iC = point(X,Y,pImg)
    if X=(Wid-1) orelse iC <> iLastC then
      dim as string sMore
      if N then 
        sMore = "C" & iLastC & "R" 
        if N > 1 then sMore &= N
      end if
      if (len(sRow)+len(sMore)) >= 254 then print #2,sRow : sRow=sMore else sRow += sMore
      if X=(Wid-1) andalso iC <> iLastC then 
        sMore = "C" & iC & "R0"
        if (len(sRow)+len(sMore)) >= 254 then print #2,sRow : sRow=sMore else sRow += sMore
      end if
      iLastC = iC : N=1      
    else
      N += 1
    end if
  next X  
  if Y <> Hei-1 then 
    var sMore = "BDBL" & Wid-1  
    if (len(sRow)+len(sMore)) >= 254 then print #2,sRow : sRow=sMore else sRow += sMore
  end if
next Y
if len(sRow) then print #2,sRow : sRow = ""

usage can be something like this:
10 SCREEN 8
15 DRAW "BM0,0"
20 OPEN "MyImage.txt" FOR INPUT AS #1
30 WHILE NOT EOF(1): INPUT #1, R$: DRAW R$: WEND
40 R$ = "": CLOSE #1
the sample code works in gwbasic/qbasic and freebasic (with -lang qb)

if there's a need to have the tool to be compiled with qbasic itself, let me know...

aurel

so trick is in instr() reverse ?

mysoft

Quote from: aurel on Jan 07, 2024, 07:30 AMso trick is in instr() reverse ?

not the instrrev is just to locate the last . (dot) so that when you pass file.bmp it emits file.txt
the trick is to use the draw statement and i check for pixels that have the same color so i can emit a single "R###" command instead of multiple "C#R" (so for some simple .bmp files without much detail it actually compresses a bit)

CharlieJV

That's very cool.