[pdftex] Embedding Exif-Style JPEG graphics

Hartmut Henkel hartmut_henkel at gmx.de
Thu May 9 12:40:07 CEST 2002


Dear pdftex friends,

there is some problem of embedding JPEG/Exif images into pdftex, as
pdftex tells `reading JPEG image failed.' In the
teTeX-texmfsrc-beta-20020207, file texk/web2c/pdftexdir/writejpg.c there
is in line 107 a strict check for JFIF=APP0 markers, which blocks
reading Exif=APP1 files; these don't contain APP0 markers.

Such JPEG/Exif images are nowadays increasingly often produced by
digital cameras.

This can be solved without modifying pdftex, using the nice jhead tool
from Matthias Wandel:

http://www.sentex.net/~mwandel/jhead/jhead-1.6.tar.gz

Calling e. g. jhead -de image.jpg throws out Exif parts from the JPEG
file and puts a JFIF marker in.

With Matthias' help (big thanks!) I tried to patch writejpg.c, and now
apparently pdftex also digests the JPEG/Exif images, at least from the
CASIO camera here (just as disclaimer :-).

The patch should not change the original JFIF reading behaviour, but it
allows some JPEG/Exif file reading, just by less fussy header check
(with all risks...).

I don't know whether in the long run one can keep ignoring new
application headers (only using the JPEG essence), or whether writejpg.c
would require an extension, might be by using some libjpeg. E. g. the
img_xres(img) parameter could probably be extracted also from APP1/Exif
data...

Standards are available in the Net for Exif (exifStandard.pdf), JPEG
(itu-t81.pdf, from www.w3.org), and JFIF (jfif3.pdf).

Best Regards

Hartmut


Here is the patch:
------------------------------------------------------------------------
--- writejpg.c	Fri Oct 26 22:58:15 2001
+++ writejpg_patched.c	Thu May  9 11:17:53 2002
@@ -97,27 +97,30 @@
 {
     int i;
     char jpg_id[]="JFIF";
-    int units;
+    int units=0;
+    img_xres(img)=0;
+    img_yres(img)=0;
     jpg_ptr(img)->file = xfopen(img_name(img), FOPEN_RBIN_MODE);
     xfseek(jpg_ptr(img)->file,0,SEEK_END,cur_file_name);
     jpg_ptr(img)->length=xftell(jpg_ptr(img)->file,cur_file_name);
     xfseek(jpg_ptr(img)->file,0,SEEK_SET,cur_file_name);
     if(read2bytes(jpg_ptr(img)->file)!=0xFFD8)
         pdftex_fail("reading JPEG image failed");
-    if(read2bytes(jpg_ptr(img)->file)!=0xFFE0)
+    if(read2bytes(jpg_ptr(img)->file)==0xFFE0) /* JFIF APP0 */
+    {
+      (void)read2bytes(jpg_ptr(img)->file);
+      for(i=0;i<5;i++) if(xgetc(jpg_ptr(img)->file)!=jpg_id[i])
         pdftex_fail("reading JPEG image failed");
-    (void)read2bytes(jpg_ptr(img)->file);
-    for(i=0;i<5;i++) if(xgetc(jpg_ptr(img)->file)!=jpg_id[i])
-        pdftex_fail("reading JPEG image failed");
-    (void)read2bytes(jpg_ptr(img)->file);
-    units=xgetc(jpg_ptr(img)->file);
-    img_xres(img)=read2bytes(jpg_ptr(img)->file);
-    img_yres(img)=read2bytes(jpg_ptr(img)->file);
-    switch(units) {
+      (void)read2bytes(jpg_ptr(img)->file);
+      units=xgetc(jpg_ptr(img)->file);
+      img_xres(img)=read2bytes(jpg_ptr(img)->file);
+      img_yres(img)=read2bytes(jpg_ptr(img)->file);
+      switch(units) {
         case 1: break; /* pixels per inch */
         case 2: img_xres(img)*=2.54; img_yres(img)*=2.54;
                 break; /* pixels per cm */
         default:img_xres(img)=img_yres(img)=0; break;
+      }
     }
     xfseek(jpg_ptr(img)->file,0,SEEK_SET,cur_file_name);
     while(1) {
------------------------------------------------------------------------


------------------------------------------------------------------------
Dr.-Ing. Hartmut Henkel, Oftersheim, Germany
------------------------------------------------------------------------





More information about the pdftex mailing list