ExifTool.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # Before "make install", this script should be runnable with "make test".
  2. # After "make install" it should work as "perl t/ExifTool.t".
  3. BEGIN { $| = 1; print "1..30\n"; $Image::ExifTool::noConfig = 1; }
  4. END {print "not ok 1\n" unless $loaded;}
  5. # test 1: Load the module(s)
  6. use Image::ExifTool 'ImageInfo';
  7. $loaded = 1;
  8. print "ok 1\n";
  9. use t::TestLib;
  10. my $testname = 'ExifTool';
  11. my $testnum = 1;
  12. # test 2: extract information from JPG file using name
  13. {
  14. ++$testnum;
  15. my $exifTool = new Image::ExifTool;
  16. my $info = $exifTool->ImageInfo('t/images/ExifTool.jpg');
  17. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  18. print "ok $testnum\n";
  19. }
  20. # test 3: TIFF file using file reference and ExifTool object with options
  21. {
  22. ++$testnum;
  23. my $exifTool = new Image::ExifTool;
  24. $exifTool->Options(Duplicates => 1, Unknown => 1);
  25. open(TESTFILE, 't/images/ExifTool.tif');
  26. my $info = $exifTool->ImageInfo(\*TESTFILE);
  27. close(TESTFILE);
  28. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  29. print "ok $testnum\n";
  30. }
  31. # test 4: test the Group option to extract EXIF info only
  32. {
  33. ++$testnum;
  34. my $info = ImageInfo('t/images/Canon.jpg', {Group0 => 'EXIF'});
  35. print 'not ' unless check($info, $testname, $testnum);
  36. print "ok $testnum\n";
  37. }
  38. # test 5: extract specified tags only
  39. {
  40. ++$testnum;
  41. my $exifTool = new Image::ExifTool;
  42. # don't test DateFormat because strftime output varies with locale
  43. # $exifTool->Options(DateFormat => '%H:%M:%S %a. %b. %e, %Y');
  44. my @tags = ('CreateDate', 'DateTimeOriginal', 'ModifyDate', 'Orientation#', '?Resolution');
  45. my $info = $exifTool->ImageInfo('t/images/Canon.jpg', \@tags);
  46. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  47. print "ok $testnum\n";
  48. }
  49. # test 6: test the 5 different ways to exclude tags...
  50. {
  51. ++$testnum;
  52. my $exifTool = new Image::ExifTool;
  53. $exifTool->Options(Exclude => 'ImageWidth');
  54. my @tagList = ( '-ImageHeight', '-Make' );
  55. my $info = $exifTool->ImageInfo('t/images/Canon.jpg', '-FileSize', '-*resolution',
  56. \@tagList, {Group0 => '-MakerNotes'});
  57. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  58. print "ok $testnum\n";
  59. }
  60. # tests 7/8: test ExtractInfo(), GetInfo(), CombineInfo()
  61. {
  62. ++$testnum;
  63. my $exifTool = new Image::ExifTool;
  64. $exifTool->Options(Duplicates => 0); # don't allow duplicates
  65. $exifTool->ExtractInfo('t/images/Canon.jpg');
  66. my $info1 = $exifTool->GetInfo({Group0 => 'MakerNotes'});
  67. my $info2 = $exifTool->GetInfo({Group0 => 'EXIF'});
  68. my $info = $exifTool->CombineInfo($info1, $info2);
  69. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  70. print "ok $testnum\n";
  71. # combine information in different order
  72. ++$testnum;
  73. $info = $exifTool->CombineInfo($info2, $info1);
  74. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  75. print "ok $testnum\n";
  76. }
  77. # test 9: test group options across different families
  78. {
  79. ++$testnum;
  80. my $exifTool = new Image::ExifTool;
  81. my $info = $exifTool->ImageInfo('t/images/Canon.jpg',
  82. { Group1 => 'Canon', Group2 => '-Camera' });
  83. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  84. print "ok $testnum\n";
  85. }
  86. # tests 10/11: test ExtractInfo() and GetInfo()
  87. # (uses output from test 5 for comparison)
  88. {
  89. ++$testnum;
  90. my $exifTool = new Image::ExifTool;
  91. # don't test DateFormat because strftime output is system dependent
  92. # $exifTool->Options(DateFormat => '%H:%M:%S %a. %b. %e, %Y');
  93. $exifTool->ExtractInfo('t/images/Canon.jpg');
  94. my @tags = ('createdate', 'datetimeoriginal', 'modifydate', 'orientation#', '?resolution');
  95. my $info = $exifTool->GetInfo(\@tags);
  96. my $good = 1;
  97. my @expectedTags = ('CreateDate', 'DateTimeOriginal', 'ModifyDate', 'Orientation',
  98. 'XResolution', 'YResolution');
  99. for (my $i=0; $i<scalar(@tags); ++$i) {
  100. $tags[$i] = $expectedTags[$i] or $good = 0;
  101. }
  102. print 'not ' unless $good;
  103. print "ok $testnum\n";
  104. ++$testnum;
  105. print 'not ' unless check($exifTool, $info, $testname, $testnum, 5);
  106. print "ok $testnum\n";
  107. }
  108. # tests 12/13: check precidence of tags extracted from groups
  109. # (Note: these tests should produce the same output as 7/8,
  110. # so the .out files from tests 7/8 are used)
  111. {
  112. ++$testnum;
  113. my $exifTool = new Image::ExifTool;
  114. $exifTool->Options(Duplicates => 0); # don't allow duplicates
  115. my $info = $exifTool->ImageInfo('t/images/Canon.jpg',{Group0=>['MakerNotes','EXIF']});
  116. print 'not ' unless check($exifTool, $info, $testname, $testnum, 7);
  117. print "ok $testnum\n";
  118. # combine information in different order
  119. ++$testnum;
  120. $info = $exifTool->ImageInfo('t/images/Canon.jpg',{Group0=>['EXIF','MakerNotes']});
  121. print 'not ' unless check($exifTool, $info, $testname, $testnum, 8);
  122. print "ok $testnum\n";
  123. }
  124. # tests 14/15/16: test GetGroups()
  125. {
  126. ++$testnum;
  127. my $exifTool = new Image::ExifTool;
  128. $exifTool->ExtractInfo('t/images/Canon.jpg');
  129. my @groups = $exifTool->GetGroups(2);
  130. my $not;
  131. foreach ('Camera','ExifTool','Image','Time') {
  132. $_ eq shift @groups or $not = 1;
  133. }
  134. @groups and $not = 1;
  135. print 'not ' if $not;
  136. print "ok $testnum\n";
  137. ++$testnum;
  138. my $info = $exifTool->GetInfo({Group0 => 'EXIF'});
  139. @groups = $exifTool->GetGroups($info,0);
  140. print 'not ' unless @groups==1 and $groups[0] eq 'EXIF';
  141. print "ok $testnum\n";
  142. ++$testnum;
  143. my $testfile = "t/ExifTool_$testnum";
  144. open(TESTFILE,">$testfile.failed");
  145. my $oldSep = $/;
  146. $/ = "\x0a"; # set input line separator
  147. $exifTool->ExtractInfo('t/images/Canon.jpg');
  148. my $family = '1:2';
  149. @groups = $exifTool->GetGroups($family);
  150. my $group;
  151. foreach $group (@groups) {
  152. next if $group eq 'ExifTool';
  153. print TESTFILE "---- $group ----\n";
  154. my $info = $exifTool->GetInfo({"Group$family" => $group});
  155. foreach (sort $exifTool->GetTagList($info)) {
  156. print TESTFILE "$_ : $$info{$_}\n";
  157. }
  158. }
  159. $/ = $oldSep; # restore input line separator
  160. close(TESTFILE);
  161. print 'not ' unless testCompare("$testfile.out","$testfile.failed",$testnum);
  162. print "ok $testnum\n";
  163. }
  164. # test 17: Test verbose output
  165. {
  166. ++$testnum;
  167. print 'not ' unless testVerbose($testname, $testnum, 't/images/Canon.jpg', 3);
  168. print "ok $testnum\n";
  169. }
  170. # tests 18/19: Test Group# option with multiple groups and no duplicates
  171. {
  172. ++$testnum;
  173. my $exifTool = new Image::ExifTool;
  174. $exifTool->Options(Duplicates => 0); # don't allow duplicates
  175. my $info = $exifTool->ImageInfo('t/images/Canon.jpg',
  176. { Group0 => ['MakerNotes','EXIF'] });
  177. print 'not ' unless check($exifTool, $info, $testname, $testnum, 7);
  178. print "ok $testnum\n";
  179. ++$testnum;
  180. $info = $exifTool->ImageInfo('t/images/Canon.jpg',
  181. { Group0 => ['EXIF','MakerNotes'] });
  182. print 'not ' unless check($exifTool, $info, $testname, $testnum, 8);
  183. print "ok $testnum\n";
  184. }
  185. # test 20: Test extracting a single, non-priority tag with duplicates set to 0
  186. {
  187. ++$testnum;
  188. my $exifTool = new Image::ExifTool;
  189. $exifTool->Options(Duplicates => 0);
  190. my $info = $exifTool->ImageInfo('t/images/Canon.jpg', 'EXIF:WhiteBalance');
  191. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  192. print "ok $testnum\n";
  193. }
  194. # test 21: Test extracting ICC_Profile as a block
  195. {
  196. ++$testnum;
  197. my $exifTool = new Image::ExifTool;
  198. my $info = $exifTool->ImageInfo('t/images/ExifTool.tif', 'ICC_Profile');
  199. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  200. print "ok $testnum\n";
  201. }
  202. # test 22: Test InsertTagValues
  203. {
  204. ++$testnum;
  205. my $exifTool = new Image::ExifTool;
  206. my @foundTags;
  207. $exifTool->ImageInfo('t/images/ExifTool.jpg', \@foundTags);
  208. my $str = $exifTool->InsertTagValues(\@foundTags, '$ifd0:model - $1ciff:3main:model');
  209. my $testfile = "t/ExifTool_$testnum";
  210. open(TESTFILE,">$testfile.failed");
  211. my $oldSep = $/;
  212. $/ = "\x0a"; # set input line separator
  213. print TESTFILE $str, "\n";
  214. $/ = $oldSep; # restore input line separator
  215. close(TESTFILE);
  216. print 'not ' unless testCompare("$testfile.out","$testfile.failed",$testnum);
  217. print "ok $testnum\n";
  218. }
  219. # test 23: Test the multi-group feature in a tag name
  220. {
  221. ++$testnum;
  222. my $exifTool = new Image::ExifTool;
  223. my $info = $exifTool->ImageInfo('t/images/ExifTool.jpg', 'main:Author:IPTC3:all');
  224. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  225. print "ok $testnum\n";
  226. }
  227. # test 24: Test a shortcut with multiple group names and a ValueConv suffix
  228. {
  229. ++$testnum;
  230. my $exifTool = new Image::ExifTool;
  231. my $info = $exifTool->ImageInfo('t/images/Canon.jpg', 'exififd:camera:common#');
  232. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  233. print "ok $testnum\n";
  234. }
  235. # test 25: Test GlobalTimeShift option
  236. {
  237. ++$testnum;
  238. my $exifTool = new Image::ExifTool;
  239. $exifTool->Options(GlobalTimeShift => '-0:1:0 0:0:0');
  240. # Note: can't extract system times because this could result in a different
  241. # calculated global time offset (since I am shifting by 1 month)
  242. my $info = $exifTool->ImageInfo('t/images/ExifTool.jpg', 'time:all', '-system:all');
  243. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  244. print "ok $testnum\n";
  245. }
  246. # test 26: Test reading with wildcards
  247. {
  248. ++$testnum;
  249. my $exifTool = new Image::ExifTool;
  250. my $info = $exifTool->ImageInfo('t/images/Canon.jpg', 'E*');
  251. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  252. print "ok $testnum\n";
  253. }
  254. # test 27: Test ListItem option
  255. {
  256. ++$testnum;
  257. my $exifTool = new Image::ExifTool;
  258. $exifTool->Options(ListItem => -3);
  259. my $info = $exifTool->ImageInfo('t/images/ExifTool.jpg', 'Subject', 'SupplementalCategories');
  260. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  261. print "ok $testnum\n";
  262. }
  263. # test 28: Test FastScan = 3
  264. {
  265. ++$testnum;
  266. my $exifTool = new Image::ExifTool;
  267. $exifTool->Options(FastScan => 3);
  268. my $info = $exifTool->ImageInfo('t/images/ExifTool.jpg');
  269. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  270. print "ok $testnum\n";
  271. }
  272. # test 29: Test Filter
  273. {
  274. ++$testnum;
  275. my $exifTool = new Image::ExifTool;
  276. $exifTool->Options(Filter => 'tr/ /_/;tr/0-9/#/');
  277. my $info = $exifTool->ImageInfo('t/images/ExifTool.jpg', '-ExifToolVersion');
  278. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  279. print "ok $testnum\n";
  280. }
  281. # test 30: Calculate JPEGDigest and JPEGQualityEstimate
  282. {
  283. ++$testnum;
  284. my $skip = '';
  285. if (eval 'require Digest::MD5') {
  286. my $exifTool = new Image::ExifTool;
  287. my $info = $exifTool->ImageInfo('t/images/Writer.jpg', 'JPEGDigest', 'JPEGQualityEstimate');
  288. print 'not ' unless check($exifTool, $info, $testname, $testnum);
  289. } else {
  290. $skip = ' # skip Requires Digest::MD5';
  291. }
  292. print "ok $testnum$skip\n";
  293. }
  294. # end