read / write EXIF data, simple, no modifications ???

Just starting out? Need help? Post your questions and find answers here.
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

read / write EXIF data, simple, no modifications ???

Post by wichtel »

Hello,

I have searched the web and the boards to find a simple way to retain EXIF information in an image.
All tools and libs I have found so far har way too big.

I just want to load a jpeg image, and save it again without losing the exif information like:

-loadimage
-get exif block
-do something with the picture
-saveimage
-put exif block

Any ideas?
PB 5.40 LTS, W7,8,10 64bit and Mint x64
User avatar
idle
Always Here
Always Here
Posts: 5097
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
dige
Addict
Addict
Posts: 1256
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

idle wrote:Gdi+ is the thing you need.

http://www.purebasic.fr/english/viewtop ... hlight=gdi
could you give us a hint? I could'nt find any commands that read / write
exif meta data with gdi :-\
User avatar
idle
Always Here
Always Here
Posts: 5097
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

I didn't check to see if its been implemented for PB by Denis but I know that gdi+ does the business.

its as they say, google is your friend!
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

Thanks for the answers.
This gets more complicated than expected.
gdi+ is way overpowerd for what I need. Certainly worth looking into it and learn how to use it, but I am more simple minded.

The exif file format link, seemed to be pretty good.
So reading the exif block would not be a problem.
But when you save an image using SaveImage and the UseJPEGImageEncoder you get a JFIF format. I know that EXIF can be in JFIF, but the trick would be now to insert the EXIF data in the JFIF file.

Still trying to make it as simple as possible:

-check for EXIF marker in image
-load the EXIF data block to memory
-load the image using loadimage
-do whatever with the image
-save the image using saveimage
-insert the EXIF block
PB 5.40 LTS, W7,8,10 64bit and Mint x64
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

Hey, I got a simple code working.
It consists of 3 blocks.

1. I open a file taken from my camera and read out the EXIF data block.

2. I open a file that has been created from the same image using the PB functions loadimage and saveimage and the JPEGencoder. There I read the entire file.

3. I create a new file and merge der EXIF data with the rest of the file.

Not sure if that is 100% correct, but all my graphic tool still can read the created file and the EXIf info is valid.

:lol:

Code: Select all

#file=1

file$="D:\oppi\PB4\stempel\dscf0452_jfif.JPG"
If ReadFile(#file, file$) 
  length = Lof(#file)          
  If (ReadByte(#file)&$FF)*256+(ReadByte(#file)&$FF)=$FFD8 ;SOU
    Debug "JPG"
    APP=(ReadByte(#file)&$FF)*256+(ReadByte(#file)&$FF)
    If APP=$FFE1 ;APP1
      Debug "EXIF"
      ssss=(ReadByte(#file)&$FF)*256+(ReadByte(#file)&$FF) ;SSSS
      Debug Hex(ssss)
      FileSeek(#file,4)
      *memEXIF = AllocateMemory(ssss)
      If *memEXIF
        rrrr=ReadData(#file,*memEXIF,ssss)
        Debug Hex(rrrr)
      EndIf
    ElseIf APP=$FFE0 ;APP0
      Debug "JFIF"
      FileSeek(#file,20)
      If (ReadByte(#file)&$FF)*256+(ReadByte(#file)&$FF)=$FFE1 ;APP1
        Debug "EXIF in JFIF"
        ssss=(ReadByte(#file)&$FF)*256+(ReadByte(#file)&$FF) ;SSSS
        Debug Hex(ssss)
        FileSeek(#file,22)
        *memEXIF = AllocateMemory(ssss)
        If *memEXIF
          rrrr=ReadData(#file,*memEXIF,ssss)
          Debug Hex(rrrr)
        EndIf
      EndIf  
    Else   
      Debug "no EXIF, no JFIF"
    EndIf  
  Else
    Debug "no JPG"
  EndIf
  CloseFile(#file)
EndIf  

file$="D:\oppi\PB4\stempel\dscf0452_ost.JPG"
If ReadFile(#file, file$) 
  If (ReadByte(#file)&$FF)*256+(ReadByte(#file)&$FF)=$FFD8 ;SOU
    Debug "JPG"
    If (ReadByte(#file)&$FF)*256+(ReadByte(#file)&$FF)=$FFE0 ;APP0
      Debug "JFIF"
      FileSeek(#file,20)
      If (ReadByte(#file)&$FF)*256+(ReadByte(#file)&$FF)=$FFDB ;FFDB
        FileSeek(#file,0)
        Debug Hex(Lof(#file))
        *memOst=AllocateMemory(Lof(#file))
        If *memOst
          rrrr=ReadData(#file,*memOst,Lof(#file))
          Debug Hex(rrrr)
        EndIf
      EndIf
    Else
      Debug "no JFIF"
    EndIf  
  Else
    Debug "no JPG"
  EndIf
  CloseFile(#file)
EndIf  

file$="D:\oppi\PB4\stempel\dscf0452_ost_exif.JPG"
If CreateFile(#file,file$)
  WriteData(#file,*memOst,20)
  WriteByte(#file,$FF)
  WriteByte(#file,$E1)
  WriteData(#file,*memEXIF,ssss)
  WriteData(#file,*memOst+20,rrrr-20)
  CloseFile(#file)
EndIf

edit: updated code for JFIF
Last edited by wichtel on Wed Aug 13, 2008 2:43 pm, edited 1 time in total.
PB 5.40 LTS, W7,8,10 64bit and Mint x64
dige
Addict
Addict
Posts: 1256
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Hmm, does not work here, coz the App1 marker in my jpg pictures is $FFE0.
But if I use the $FFE0 marker instead, only $10 Bytes will copied..
And what about intel & motorola byte order?
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

@DIGE:

I have updated the code. It is by no means final or perfect, but a simple trick that will probably work for me.

Most pictures right out of a camera are in JPEG-EXIF.
Some pictures are in JPEG-JFIF with EXIF info.

The new code will work with those as well.

I did not care about the byte order yet.
PB 5.40 LTS, W7,8,10 64bit and Mint x64
dige
Addict
Addict
Posts: 1256
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

thx, seems to work properly now :D
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

Wichtel, don't know if you are still around but...can you decode the bits of exif data?
Amateur Radio, D-STAR/VK3HAF
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

@Fangbeast:
What do you mean with "can you decode exif"?

If you mean if I have written my own EXIF libary - No.
Way too much work. There are so many different info types in there plus vendor specific data...
I thought about extracting just the most needed values. That woul definitly be doable by having a look at the data format and the description in e.g. WiKi. But not yet...

But I could, and you as well, by using an EXIF library. There are several to be found in the net.
e.g. http://www.gimp.org/~tml/gimp/win32/libexif-0.6.16.zip (from gimp)
Last edited by wichtel on Mon Mar 09, 2009 8:52 am, edited 1 time in total.
PB 5.40 LTS, W7,8,10 64bit and Mint x64
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

wichtel wrote:@Fangbeast:
What do you mean with "can you decode exif"?

If you mean if I have written my own EXIF libary - No.
Way too much work. There are so many different info types in there plus vendor specific data...
I thought about extracting just the most needed values. That woul definitly be doable by having a look at the data format and the description in e.g. WiKi. But not yet...

But I could, and you as well, by using an EXIF library. There are several to be found in the net.
Hadn't even thought of that, thanks.
Amateur Radio, D-STAR/VK3HAF
Post Reply