Splitting a file at a marker

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Splitting a file at a marker

Post by hairydalek » Fri Jan 22, 2016 9:13 pm

Hi,
Another thing I’m thinking about is taking an MPO file and extracting the two images. An MPO file is a file that contained a number of JPEG files. In this case, it’s a Fuji stereoscopic camera where you get a left and right image in the MPO file.

I know that each JPEG starts with a marker, so I assumed that if I can somehow split the data at those points, I can get the left and right images. I’ve not been able to find any information on how to achieve what I’m thinking about. I have a little code in my stack, which looks like this:

Code: Select all

command processMPO thisFile
   # Open the file
   put URL ("binfile:" & thisFile) into theBinaryData
   
   answer length(theBinaryData) -- Returns the size of the file
   
   # look for the JPEG markers - FFD8FFE1
   put binaryEncode("H*", "FFD8FFE1") into jpegMarker

   answer jpegMarker
   
   split theBinaryData by jpegMarker
   
   answer length(theBinaryData[0]) -- Currently returns 0
   answer length(theBinaryData[1]) -- Currently returns 0
end processMPO
So the MPO file is opened, and the binary data is read in. I am assuming that I can split the binary data using the variable jpegMarker, putting each image’s binary data into an array.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Splitting a file at a marker

Post by Simon » Sat Jan 23, 2016 2:48 am

Hi hairydalek,
Check out "read from file" in the dictionary.
I don't think split is meant for binary data.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Splitting a file at a marker

Post by jacque » Sat Jan 23, 2016 8:15 pm

It's possible to use the split command on binary data. I ran this on a LC stack, opened as a binary file, and it found all the images:

Code: Select all

on mouseUp
  answer file "choose a binary file:"
  if it = "" then exit to top
  put url ("binfile:" & it) into tData
  split tData by binaryEncode("H*", "FFD8FFE1")
  breakpoint -- to see what was there
end mouseUp
I got a numerical array with five keys; the first key was not an image but simply the beginning of the file. The next four keys had elements containing the images that were in the stack, each beginning with the marker. You could try my test hander on your file and see if it works. If it doesn't then check that the file really does contain that marker. All my images had "EXIF" data too, so if yours do you might try splitting with "EXIF" to see if you get anything.

Note that LC arrays are 1-based (not zero-based) so getting theBinaryData[0] will always be empty, and theBinaryData[1] will be the first segment of the file before the first marker occurs. Check what's in theBinaryArray[2].
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Splitting a file at a marker

Post by hairydalek » Sat Jan 23, 2016 9:34 pm

jacque wrote:It's possible to use the split command on binary data. I ran this on a LC stack, opened as a binary file, and it found all the images:

Code: Select all

on mouseUp
  answer file "choose a binary file:"
  if it = "" then exit to top
  put url ("binfile:" & it) into tData
  split tData by binaryEncode("H*", "FFD8FFE1")
  breakpoint -- to see what was there
end mouseUp
I got a numerical array with five keys; the first key was not an image but simply the beginning of the file. The next four keys had elements containing the images that were in the stack, each beginning with the marker. You could try my test hander on your file and see if it works. If it doesn't then check that the file really does contain that marker. All my images had "EXIF" data too, so if yours do you might try splitting with "EXIF" to see if you get anything.

Note that LC arrays are 1-based (not zero-based) so getting theBinaryData[0] will always be empty, and theBinaryData[1] will be the first segment of the file before the first marker occurs. Check what's in theBinaryArray[2].
Thanks all. While both methods suggested offered promise, this splitting one seems to be the one that works for me. I’ve got it writing out two files - a left and right one - to confirm that what I am getting is what I need.

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Splitting a file at a marker

Post by hairydalek » Sun Jan 24, 2016 12:07 pm

Hooray! I have this (see attached screen shot). It loads an MPO, splits it into left and right images, scales and crops to fit a square frame, and places them side by side in a window. If you drag the left image, it will move horizontally, and the right image mirrors that movement. Some stereo cards have no gap between images, some do.

The eventual aim is to have the app output an image of a stereo card.
Attachments
Screen Shot 2016-01-24 at 11.03.08.jpg
Screen shot

Post Reply