Confusion over "Open File for binary read" and Two's Complement

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Confusion over "Open File for binary read" and Two's Complement

Post by Simon Knight » Wed Aug 26, 2020 12:30 pm

Hi,

I am struggling to read data from a binary file. The file is a Tiff or RAW or JPEG image file. I have a working stack that uses Open URL commands but its is quite slow when dealing with multiple files on a SD card so I'm rewriting some handlers to only read in meta data from the image file.

My handler for reading selected bytes is here.

Code: Select all

function ReadFileBytes pfileName, pStartByte, pByteCount
   /* pfileName is the full path to a file
   LowByte and HighByte are integers specifying bytes requied
   Note: codes adds a byte offset to account for JPEG files 
   */
   put 0 into tOffset
   set itemdel to "."
   put the last item of pFileName into tExtn
   If tExtn is "jpg" then put 12 into tOffset
   
   Open File pfileName for binary read
   read from file pfileName at (pStartByte+tOffset) for pByteCount int1
   Close File pfileName
   return it
end ReadFileBytes
I should say that I have read the dictionary entry for Open File and I find it confusing and pretty unhelpful. It is lacking examples and I have been unable to use the enums it defines because I am unable to determine the correct syntax.

The read operation returns a comma separated list of decimal values which is good because it save me the effort of doing conversions. However the problem which originates with the line
Open File pfileName for binary read
is that it seems that binary read returns two's compliment eight bit binary.

An example from the file is 0x87 (hex), I want the 255 bit decode of 135 decimal, but read from file returns -121 decimal. Now I could just add 256 to all negative values but that is a bit of a cop out as I'm sure there should be a way of specifying a non two's complement read.

From the dictionary :
open file filePath [for [ {[encoding] text | binary}] {update | read | write | append}]
I take this to mean the the list of encoding enumerations are only applicable to text files but when I try to use them I just get an unsupported error:

Code: Select all

 Open File pfileName for "ASCII" text read
stack "Read-Image-Meta-Data": execution error at line 367 (open: unsupported encoding), char 1
Any ideas ?
best wishes
Skids

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Confusion over "Open File for binary read" and Two's Complement

Post by Simon Knight » Wed Aug 26, 2020 12:39 pm

Ha,

The power of writing a problem down!

The line thats reads from the file is asking for a chunktype of int1, it should be uInt1 i.e. unsigned integer.

Code: Select all

read from file pfileName at (pStartByte+tOffset) for pByteCount uint1
Works!

Doh!
best wishes
Skids

Post Reply