Post by jstone on Sept 8, 2010 9:32:43 GMT -5
The problem I had was how to read hex values from a binary TarGA image file given that RB uses UTF-8 character encoding rather than ASCII. Trying to read hex value 0x80 (decimal value 128) from a binary file is a problem because RB's UTF-8 encoding interprets this character as the Euro symbol with a hex value of 0x20ac (decimal value 8364). The same thing occurs when reading/writing most characters between 128-159 in the ASCII table: the equivalent UTF-8 characters and corresponding hex/decimal values are completely different. Consequently, running the file's contents through RB's native hex functions returns an Unsupported Character error.
The below code demonstrates how RB can read a TarGA image file as hex and correct for the errors introduced by UTF-8 character encoding. The code uses the MID$ function as a substitute for the unsupported SEEK statement.
The attached zip file contains the full source code for my work-in-progress TarGA viewer, the text file used to correct the character encoding issues caused by UTF-8, and some sample flight simulator terrain textures as TarGA images. If you run the full source code then you'll need to amend the DefaultDir$ path to whatever folder you've stored the hexdec.txt file in.
The below code demonstrates how RB can read a TarGA image file as hex and correct for the errors introduced by UTF-8 character encoding. The code uses the MID$ function as a substitute for the unsupported SEEK statement.
The attached zip file contains the full source code for my work-in-progress TarGA viewer, the text file used to correct the character encoding issues caused by UTF-8, and some sample flight simulator terrain textures as TarGA images. If you run the full source code then you'll need to amend the DefaultDir$ path to whatever folder you've stored the hexdec.txt file in.
dim hex2dec(32, 2)
dim tgaBytes$(fLen)
fileName$ = "A TarGA file"
[readDecimalFile]
'hexdec.txt is a list of the offending 32 utf-8 decimals and ASCII counterparts
open "hexdec.txt" for input as #hex2dec
for x = 1 to 32
input #hex2dec, hex2dec(x, 1)
input #hex2dec, hex2dec(x, 2)
next x
close #hex2dec
[readTargaFile]
'Get contents of the TarGA file
open fileName$ for input as #tgaFile
fLen = lof(#tgaFile)
redim tgaBytes$(fLen)
fileText$ = input$(#tgaFile, fLen)
close #tgaFile
'Convert bytes to decimal, convert incorrect decimal values, convert to hex
for aLoop = 1 to fLen
tgaBytes$(aLoop) = mid$(fileText$, aLoop, 1)
aByte = hexdec(dechex$(asc(tgaBytes$(aLoop))))
if aByte > 255 then
tgaBytes$(aLoop) = dechex$(decConv(aByte))
else
tgaBytes$(aLoop) = dechex$(asc(tgaBytes$(aLoop)))
end if
if len(tgaBytes$(aLoop)) = 1 then
tgaBytes$(aLoop) = "0" + tgaBytes$(aLoop)
end if
next aLoop
wait ' insert code to process/render the image
'Function to convert UTF-8 decimals to ASCII decimals
function decConv(bByte)
for x = 1 to 32
if bByte = hex2dec(x, 1) then
decConv = hex2dec(x, 2)
exit for
end if
next x
end function