Page 1 of 1

Splitting a file at a marker

Posted: Fri Jan 22, 2016 9:13 pm
by hairydalek
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.

Re: Splitting a file at a marker

Posted: Sat Jan 23, 2016 2:48 am
by Simon
Hi hairydalek,
Check out "read from file" in the dictionary.
I don't think split is meant for binary data.

Simon

Re: Splitting a file at a marker

Posted: Sat Jan 23, 2016 8:15 pm
by jacque
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].

Re: Splitting a file at a marker

Posted: Sat Jan 23, 2016 9:34 pm
by hairydalek
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.

Re: Splitting a file at a marker

Posted: Sun Jan 24, 2016 12:07 pm
by hairydalek
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.