extracting mpo-files

Just starting out? Need help? Post your questions and find answers here.
high key
User
User
Posts: 23
Joined: Sun Jun 08, 2003 8:07 pm

extracting mpo-files

Post by high key »

high key wrote:mpo is an extended version of JPG to save multiple images in one file, for example for 3D Fotos

Would it be possible to extend the UseJPEGImageDecoder() for loading these additional images with

loadimage(1,mpofile$,#picindex)

?
I posted this as a feature request some time ago.

But it would be sufficient for me, if I knew how to get the index list (start addresses and bytelength of each image) in the mpo file.
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: extracting mpo-files

Post by Crusiatus Black »

There's a link to the MPO [1] and EXIF [2] specification on this page: http://rjhornby.com/geekery/82-3d-images-part1-mpofiles

A little bit of Googlin' should get you on your way.

[1] - http://www.cipa.jp/std/documents/e/DC-007_E.pdf
[2] - http://www.cipa.jp/exifprint/contents_e ... mary_E.pdf (Summary)
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: extracting mpo-files

Post by netmaestro »

Here's something to play with, it extracts images from a .mpo file. The first time you run it it will be slow because it has to download a 10m image. Subsequent runs will use the previously downloaded file. This code is extracting six images from the test mpo file.

Code: Select all

UseJPEGImageDecoder()
InitNetwork()
file$ = "DSCF0036.MPO"

If FileSize(GetTemporaryDirectory()+file$) = -1
  ReceiveHTTPFile("http://lloydsplace.com/"+file$, GetTemporaryDirectory()+file$)
EndIf

If FileSize(GetTemporaryDirectory()+file$) = 10039442
  
  ReadFile(0, GetTemporaryDirectory()+file$)
  length=Lof(0)
  *pic = AllocateMemory(length)
  ReadData(0, *pic, length)
  CloseFile(0)
  *pos=*pic
  *end=*pic+length-3
  
  nextimage=0
  
  While *pos < *end
    If CompareMemory(*pos, ?test, 3)
      If CatchImage(nextimage, *pos)
        Debug "loaded image "+Str(nextimage)
        nextimage+1
      EndIf
    EndIf
    *pos+1
  Wend
  
  ShowLibraryViewer("image", 0)
  CallDebugger
Else
  Debug "couldn't download the file"
EndIf

DataSection 
  test:
  Data.a $ff,$d8,$ff
EndDataSection
BERESHEIT
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: extracting mpo-files

Post by Thade »

I used this Blitz3D Code years ago to split MPO Files made with my Fuji W3 in a Blitz3D Anaglyph program
Not professional, but it worked ...

Code: Select all

;- SplitMPO
.SplitMPO
	If FileType(File.s)=1
		FLen=FileSize(File.s)
		Bank=CreateBank(FLen)
		fi=ReadFile(File.s)
		If fi
			Exif=Asc("f")*256*256*256 + Asc("i")*256*256 + Asc("x")*256 + Asc("E")
			ReadBytes(Bank, fi, 0, FLen)
			CloseFile(fi)
			Test=PeekInt(Bank,0)
			Gef=0
			For i=Flen/2-Flen/5 To Flen
				If PeekInt(Bank, i) = Test
					If PeekInt(Bank, i+6) =Exif
						PicL.s=GetFilenamePart(File, False)+"_l.jpg"
						fo=WriteFile(PicL.s)
						WriteBytes(Bank, fo, 0, i)
						CloseFile(fo)
						PicR.s=GetFilenamePart(File, False)+"_r.jpg"
						fo=WriteFile(PicR.s)
						WriteBytes(Bank, fo, i, FLen-i)
						CloseFile(fo)
						MerkI=i
						
						Exit ;Break in PB
					EndIf
				EndIf
			Next
		EndIf
		FreeBank(Bank)
	EndIf
Return;
;!
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
high key
User
User
Posts: 23
Joined: Sun Jun 08, 2003 8:07 pm

Re: extracting mpo-files

Post by high key »

Thade wrote:I used this Blitz3D Code years ago to split MPO Files made with my Fuji W3 in a Blitz3D Anaglyph program
Not professional, but it worked ...
Thade, I love this approach! Just searching for the "Exif"s should do the job.

I think, that's all I need :D
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: extracting mpo-files

Post by Thade »

Actually it searches for the first 4 bytes of each of the 2 main jpgs inside and then makes shure that its the beginning of the second file (and not random code inside the picture) by checking if the Exif follows at byte 6 after start as in every MPO File the Fuji shoots.
:)
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
high key
User
User
Posts: 23
Joined: Sun Jun 08, 2003 8:07 pm

Re: extracting mpo-files

Post by high key »

Now here's my code :)

Code: Select all

  pic$="E:\test.mpo"

  fsize=FileSize(pic$)
  If fsize<100
    error$="no picfile"
    Goto errortrap
  EndIf  
  
  *mpostart=AllocateMemory(fsize)
  
  If *mpostart=0
    error$="couldn't allocate memory"
    Goto errortrap
  EndIf  
  
  ok=ReadFile(1,pic$)
  If ok=0
    error$="couldn't read picdata"
    Goto errortrap
  EndIf  
  
  ReadData(1,*mpostart,fsize)
  
  CloseFile(1)
  
  
  
  phalf=*mpostart+fsize/2  ; we expect an Exif somewhere in the mid of the mpo file
  splitpoint=0
  
  testtime=GetTickCount_()
  
  For i=0 To fsize/4  ; searching from the midst forwards and backwards
      
    If PeekB(phalf+i)=$45 ;"E"
        adr=phalf+i        
        If PeekS(adr,4,#PB_Ascii )="Exif"          
          If PeekS(adr-6,4,#PB_Ascii )= "ÿØÿá"
            splitpoint=adr-6           
            Break
          EndIf  
        EndIf  
      EndIf  
      
      If PeekB(phalf-i)=$45;"E"
        adr=phalf-i        
        If PeekS(adr,4,#PB_Ascii )="Exif"
          If PeekS(adr-6,4,#PB_Ascii )="ÿØÿá"
            splitpoint=adr-6
            Break
          EndIf  
        EndIf  
      EndIf  
    
    Next i  
    
    Debug "*mpostart="+Str(*mpostart)+"    fsize="+Str(fsize)+"  phalf="+Str(phalf)+" splitpoint="+Str(splitpoint)
    Debug "Searchtime for Exif was "+Str(GetTickCount_()-testtime)+" millisecs"
    
    If splitpoint=0
      error$="splitpoint not found"
      Goto errortrap
    EndIf 
    
     size1=splitpoint-*mpostart+1
     size2=*mpostart+fsize-splitpoint
     
     
     
    ext$=GetExtensionPart(pic$)
  
    L=Len(pic$)-Len(ext$)-1
  
    outfileL$=Left(pic$,L)+"_L.jpg"
  
    outfileR$=Left(pic$,L)+"_R.jpg"
  
  
  Debug "outfileL$="+outfileL$+"     outfileR$="+outfileR$
  
  saved=0
  
   If CreateFile(1,outfileL$)  
      WriteData(1,*mpostart,size1)    ; first image is LEFT?  I'm not sure

      CloseFile(1)
      saved+1
    EndIf  
  
    
    If CreateFile(1,outfileR$)
      WriteData(1,splitpoint,size2)  ; second image is RIGHT?
      CloseFile(1)
      saved+1
    EndIf  
  
    Debug Str(saved)+" pic(s) saved"
    



progend:
;-------
End

errortrap:
;---------
  Debug error$
End
mpz
Enthusiast
Enthusiast
Posts: 497
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

Re: extracting mpo-files

Post by mpz »

Hi,

i know it is an old topic, but i have the "Fujifilm FinePix Real 3D W1" kamera with the "mpo" 3d files.

Now i am printing the following 3d Stereoskop <https://www.thingiverse.com/thing:5446241> (with Bambulabs) and need the pictures for printing as jpg.

Here comes the actualized program for PB 6.20, grettings

Code: Select all

UseJPEGImageDecoder()
UseJPEGImageEncoder()

file$ = "mpotest.mpo"

If FileSize(file$) 
  
  ReadFile(0, file$)
  length=Lof(0)
  *pic = AllocateMemory(length)
  ReadData(0, *pic, length)
  CloseFile(0)
  *pos=*pic
  *end=*pic+length-3
  
  nextimage=0
  
  While *pos < *end
    If CompareMemory(*pos, ?test, 4)
      If CatchImage(nextimage, *pos-6)
        Debug "loaded image "+Str(nextimage)
        nextimage+1
      EndIf
    EndIf
    *pos+1
  Wend
  
  CreateImage(2, ImageWidth(0)*2, ImageHeight(0))
  StartDrawing(ImageOutput(2))
    DrawImage(ImageID(0), 0, 0 ) 
    DrawImage(ImageID(1), ImageWidth(0), 0 ) 
  StopDrawing()
  SaveImage(2, "mpotest.jpg", #PB_ImagePlugin_JPEG )

  ShowLibraryViewer("image", 0)
  CallDebugger
  
Else
  Debug "couldn't download the file"
EndIf

DataSection 
  test:
  Data.a $45,$78,$69,$66
EndDataSection
Working on - MP3D Library - PB 5.73 version ready for download
Post Reply