Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: rdc on November 17, 2006
-
Here is the little bitmap color extractor I used in my POM program. As I said it is a hack, but seems to work (at least with the bitmaps I tried) but I won't say it will work with all of them. May be slow on large bitmaps, as it uses the Point function. It asks for graphic screen settings; these should be the same as your target program, otherwise the colors may be off. Only tested with 32 bit color depth. Feel free to hack away on this, it is Public Domain.
'Convert.bas
'Richard Clark
'Extracts the color information from a bitmap
'to an include file for use in an FB program.
'Using a graphic screen and the Point function.
'Do not compile with -s gui, as this uses the console screen
'for input.
'Public doman, use any way you like.
option explicit
dim as integer iw, ih, sw, sh, dp
dim as string iname, aname, oname
dim as integer x, y, i, fh, cnt
Input "Enter bitmap width";iw
Input "Enter bitmap height";ih
Input "Enter bitmap filename (including extension)";iname
Input "Enter output array name";aname
Input "Enter export file name (including extension)";oname
Input "Enter graphic screen width";sw
Input "Enter graphic screen height";sh
Input "Enter graphic screen depth";dp
if iw <= 0 or ih <= 0 then
print "Width and height must be greater than zero."
sleep
end
end if
if len(iname) = 0 or len(aname) = 0 or len(oname) = 0 then
print "No values given for filename, array name or output file name."
sleep
end
end if
if sw <= 0 or sh <= 0 or dp <= 0 then
print "Screen dimensions and depth must be greater than zero."
sleep
end
end if
if len(dir(iname)) = 0 then
print "Cannot find " & iname &"."
sleep
end
end if
fh = freefile
if open(oname for output as #fh) <> 0 then
print "Cannot open output file."
sleep
end
end if
dim bmvals(iw * ih) as uinteger
screenres sw, sh, dp
if Screenptr = 0 then
print "Cannot set graphic screen."
sleep
close fh
end
end if
bload iname
'Print out header information
print #fh, "Const " & trim(aname) & "w = " & str(iw)
print #fh, "Const " & trim(aname) & "h = " & str(ih)
print #fh, "Dim Shared " & trim(aname) & "(" & str(iw * ih) & ") As UInteger = { _"
for x = 0 to iw - 1
for y = 0 to ih - 1
bmvals(x + y * iw) = point(x, y)
next
next
for i = lbound(bmvals) to ubound(bmvals)
cnt += 1
if i = ubound(bmvals) then
print #fh, "&h" & hex(bmvals(i)) & "}"
else
if cnt < 8 then
print #fh, "&h" & hex(bmvals(i)) & ",";
else
print #fh, "&h" & hex(bmvals(i)) & ", _"
cnt = 0
end if
end if
next
close fh
end
Example useage:
option explicit
#include "sand.bi"
dim as uinteger x, y
screenres 640, 480, 32
for x = 0 to sandw - 1
for y = 0 to sandh - 1
pset(x, y), sand(x + y * sandw)
next
next
sleep
end
-
This could be useful as there are times when a bitmap>256 colours is needed :) Thanks Rick.
-
Thanks, man. I don't really use anything less than 32bit, so I didn't try it, but a quick test indicates that it only appears to work with 32bit mode depths. The depth input isn't really needed, so you could remove that from the program.