#!/usr/bin/perl # the line above could be the first line for a typical UNIX systems # you can find perl on your system by using "which perl" in the shell use strict; use warnings; use File::Basename; use Image::MetaData::JPEG; my $EvilOS = 0; # boolean, if we run on Windows this is 1 if ($^O =~ m/win/i) { $EvilOS = 1; } if ($^O =~ m/darwin/i) { # Mac OS X is not evil, but unfortunately contains the string "win"! $EvilOS = 0; } my $prog = 'copyNikonISOtoEXIF'; ### the main body starts here @ARGV = map { glob } @ARGV if $EvilOS; if ($#ARGV < 0) { print <get_Exif_data('ALL', 'TEXTUAL'); # er = exif hash ref unless (defined $er) { print " no EXIF data! Skipping\n"; return ($count); } if (defined $er->{'APP1@IFD0@SubIFD'}->{'ISOSpeedRatings'}) { my $iso = ${$er->{'APP1@IFD0@SubIFD'}->{'ISOSpeedRatings'}}[0]; print " EXIF ISO already defined: ISO$iso\n"; return ($count); } #else{ # Same as ISOSpeedRatings. Only Kodak's camera uses this tag instead of ISOSpeedRating #if (defined $er->{'APP1@IFD0@SubIFD'}->{'ExposureIndex'}) { # my $iso = calc($er->{'APP1@IFD0@SubIFD'}->{'ExposureIndex'}); # $exif .= "ISO$iso "; #} # Nikon hides the ISO settings in the Makernotes (at least the Nikon D70) my $seg = $meta->retrieve_app1_Exif_segment(); unless ($seg) { print " no EXIF segment! Skipping\n"; return ($count); } my $iso = $seg->search_record('IFD0@SubIFD@MakerNote_Nikon_3@2'); unless ($iso) { print " no Nikon MakerNote! Skipping\n"; return ($count); } my $iso_value = $iso->get_value(1); unless (($iso_value > 1) and ($iso_value < 30000)) { print " ISO$iso_value too small or too big! Skipping\n"; return ($count); } print "+++ adding ISO$iso_value to EXIF ISO: "; my $hash = $meta->set_Exif_data({'ISOSpeedRatings' => $iso_value}, 'IMAGE_DATA', 'ADD'); if (%$hash) { print "ISO record rejected\n"; } else { if ($meta->save()) { print "Saving .. OK\n"; $changed++; } else { print "Saving .. failed!!!\n"; } } return ($count); } ############################################################## # is_a_JPEG - returns true (1) if the given file is a JPEG/JFIF ############################################################## sub is_a_JPEG($) { my $dpic = shift; return 0 unless ($dpic); return 0 unless (-f $dpic); my @c; # open file and read the first 3 bytes return 0 unless (open FILE,"<$dpic"); for my $i (0 .. 2) { read(FILE, $c[$i], 1); } close FILE; # JPEG JFIF files start with 0xFF 0xD8 0xFF # todo: this check is necessary but not sufficent if ( (ord($c[0]) == 0xFF) && (ord($c[1]) == 0xD8) && (ord($c[2]) == 0xFF) ) { return 1; } else { return 0; } }