Recent

Author Topic: fpExif JFIF section  (Read 1073 times)

rpetges

  • New Member
  • *
  • Posts: 44
    • Attribute Changer Website
fpExif JFIF section
« on: May 03, 2020, 09:13:06 am »
Hi,

I have a question about adding a new EXIF header into a JPG file.

In fact, I created a sample JPG file in MS Paint and by default it contains basic JFIF tags. When I add a new EXIF header with a DateTimeOriginal tag to the image, the JFIF tags are deleted ( debugged with ExifTool)


[JFIF]   0x0000 JFIF Version        : 1.01
[JFIF]   0x0002 Resolution Unit     : inches
[JFIF]   0x0003 X Resolution        : 96
[JFIF]   0x0005 Y Resolution        : 96


Code: Pascal  [Select][+][-]
  1. ...
  2. L_ImageInfo.LoadFromFile(_IM_FullName);
  3. ...
  4. L_ExifData := L_ImageInfo.CreateExifData;
  5.  
  6. L_Tag := L_ImageInfo.ExifData.AddTagByName('DateTimeOriginal');
  7. TDateTimeTag(L_Tag).AsDateTime := _IM_PhotoDT;
  8.  
  9. L_ImageInfo.SaveToFile(_IM_FullName);    <- JFIF tags get lost after saving
  10. ...
  11.  

Is there a way to keep the JFIF tags when adding a new EXIF header to a file ?

Best regards,
Romain
« Last Edit: May 03, 2020, 10:17:44 am by Romain »

wp

  • Hero Member
  • *****
  • Posts: 7964
Re: fpExif JFIF section
« Reply #1 on: May 03, 2020, 10:35:01 am »
I always thought that the APP0 (JFIF) and APP1 (EXIF) segments are mutually exclusive because each one requires to be first segment after the SOI (start of image) segment (https://en.wikipedia.org/wiki/JPEG_File_Interchange_Format). Why do you need JFIF?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

rpetges

  • New Member
  • *
  • Posts: 44
    • Attribute Changer Website
Re: fpExif JFIF section
« Reply #2 on: May 03, 2020, 11:03:51 am »
It's not that I need to keep JFIF :-)

Based on customer feedback, I did some testing with File Explorer and noticed that it adds a new EXIF section to an existing JFIF to the sample file.

Your link is very informative and it is written that officially both segments are mutually exclusive but that many cameras and applications add both. So, I fully understand your decision to not keep JFIF when an EXIF segment is available or added.  It's something I was not aware.

Thank you !


wp

  • Hero Member
  • *****
  • Posts: 7964
Re: fpExif JFIF section
« Reply #3 on: May 03, 2020, 10:59:34 pm »
Seeking through my images I found that the case of having both JFIF and EXIF segments in the same JPEG file is not so rare. Therefore, I decided to add a new boolean property WriteJFIFandEXIF to TImgInfo which can enforce writing both JFIF and EXIF segments if they exist simultaneously.

Code: Pascal  [Select][+][-]
  1. var
  2.   imgInfo: TImgInfo;
  3.   tag: TTag;
  4.   exif: TExifData;
  5.  
  6. begin
  7.   imgInfo := TImgInfo.Create;
  8.   try
  9.     // Read file
  10.     imgInfo.LoadFromFile('sample.jpg');
  11.  
  12.     // Add EXIF
  13.     exif := ImgInfo.CreateExifData;
  14.  
  15.     // Add Date/Time Tag
  16.     tag := exif.AddTagByName('DateTimeOriginal');
  17.     TDateTimeTag(tag).AsDateTime := now();
  18.  
  19.     // Save to file
  20.     imgInfo.WriteJFIFandEXIF := true;    // Preserve JFIF segment in addition to EXIF
  21.     imgInfo.SaveToFile('edited_image.jpg');
  22.  
  23.   finally
  24.     imgInfo.Free;
  25.   end;  
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

rpetges

  • New Member
  • *
  • Posts: 44
    • Attribute Changer Website
Re: fpExif JFIF section
« Reply #4 on: May 04, 2020, 05:16:35 pm »
Many thanks, much appreciated :-)

However, do you have a link to get the latest snapshot. I'm unable to find the latest snapshot on Sourceforge.

wp

  • Hero Member
  • *****
  • Posts: 7964
Re: fpExif JFIF section
« Reply #5 on: May 04, 2020, 05:53:42 pm »
Use the "Download snapshot" button on https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fpexif/. Or use svn.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

rpetges

  • New Member
  • *
  • Posts: 44
    • Attribute Changer Website
Re: fpExif JFIF section
« Reply #6 on: May 04, 2020, 06:05:29 pm »
New version works perfect.

Thank you again.



speter

  • Full Member
  • ***
  • Posts: 102
Re: fpExif JFIF section
« Reply #7 on: November 02, 2020, 07:59:34 am »
G'Day Folks,

I downloaded the EXIF package and wrote a simple program to read the exif data from a JPG.
It worked fine for most of my images. but when I tried it on a long-exposure image it gave an error.

If I run my program outside the IDE the error is
Quote
Floating point overflow
inside the IDE the error is
Quote
Project read_exif raised exception class 'External: SIGPE'. At address 100005260

I resized the image (to make it small :)) and the error still happens.
The image is attached.

Can someone please check whether the image causes an error for them as well, please!!

FYI, I "installed" the package (simply) by copying the files into a folder, then adding my source to that same folder; so it is quite possible that my (incorrect?) installation of the code could be causing the error...

I have also attached my project source.

cheers
S.
--
Lazarus 2.09; FPC 3.0.4; Win 10.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.8 / FPC 3.0.4 / Windows 10 (64bit)

wp

  • Hero Member
  • *****
  • Posts: 7964
Re: fpExif JFIF section
« Reply #8 on: November 02, 2020, 10:50:26 am »
Shutterspeed is stored as the log of a rational number (numerator divided by denominator). It seems to be that in case of manual exposure your Canon writes the numerator $8000000 (=2147483648) and the denominator 1. Their ratio is way to much for the following exponential function (to invert the log). Since this tag is in the general EXIF section I suppose that this kind of storage is a convention although I did not find an explicit reference anywhere.

The code now detects whether the numerator is $80000000 and sets the shutterspeed to 0 in this case. The string conversion routine in this case returns the string '(manual)'.

Fixed in r7837.

As for copying the fpexif sources into your project: You do not need to do this when you add the fpexif_pkg to the requirements of your project: Open the project inspector, right-click on "Required packages", click "Add" and select fpexif_pkg in the list. If fpexif_pkg is not listed you open "Package" > "Open Package File (*.lpk)", navigate to the folder in which you downloaded the fpexif sources. Load fpexif_pkg.lpk - now the IDE knows where the fpexif units are found. Not absolutely required, but you can click "Compile" to check that everything is correct with the package. Note that it is NOT needed to install ("Use" > "Install") fpexif because it contains only runtime code.

If you want to use fpexif in this "official" way do not forget to remove all fpexif units as well as their compiled units from your project folder.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

speter

  • Full Member
  • ***
  • Posts: 102
Re: fpExif JFIF section
« Reply #9 on: November 02, 2020, 12:39:40 pm »
@WP; thanks very much.

cheers
S.
PS: In case anyone is interested the image is of the Australian War Memorial (and Mount Ainsley behind). Just before a fireworks display (in March 2108). :)
PPS: According to Irfranview the exposure time was 16 seconds. So it's not unreasonable that the "shutter speed" value was outrageous!!
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.8 / FPC 3.0.4 / Windows 10 (64bit)

wp

  • Hero Member
  • *****
  • Posts: 7964
Re: fpExif JFIF section
« Reply #10 on: November 02, 2020, 03:34:49 pm »
According to Irfranview the exposure time was 16 seconds.
There is also a tag "ExposureTime" which is evaluated by fpExif, too, in addition to ShutterSpeed, and this tell that in fact the exposure time was 16 sec (Load your image into the MetaDataViewer demo which comes with fpExif and displays all tags supported).
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018