Rawzor.pm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #------------------------------------------------------------------------------
  2. # File: Rawzor.pm
  3. #
  4. # Description: Read meta information from Rawzor compressed images
  5. #
  6. # Revisions: 09/09/2008 - P. Harvey Created
  7. #
  8. # References: 1) http://www.rawzor.com/
  9. #------------------------------------------------------------------------------
  10. package Image::ExifTool::Rawzor;
  11. use strict;
  12. use vars qw($VERSION);
  13. use Image::ExifTool qw(:DataAccess :Utils);
  14. $VERSION = '1.04';
  15. # currently support this version Rawzor images
  16. my $implementedRawzorVersion = 199; # (up to version 1.99)
  17. # Rawzor-specific tags
  18. %Image::ExifTool::Rawzor::Main = (
  19. GROUPS => { 2 => 'Other' },
  20. VARS => { NO_ID => 1 },
  21. NOTES => q{
  22. Rawzor files store compressed images of other formats. As well as the
  23. information listed below, exiftool uncompresses and extracts the meta
  24. information from the original image.
  25. },
  26. OriginalFileType => { },
  27. OriginalFileSize => {
  28. PrintConv => $Image::ExifTool::Extra{FileSize}->{PrintConv},
  29. },
  30. RawzorRequiredVersion => {
  31. ValueConv => '$val / 100',
  32. PrintConv => 'sprintf("%.2f", $val)',
  33. },
  34. RawzorCreatorVersion => {
  35. ValueConv => '$val / 100',
  36. PrintConv => 'sprintf("%.2f", $val)',
  37. },
  38. # compression factor is originalSize/compressedSize (and compression
  39. # ratio is the inverse - ref "Data Compression" by David Salomon)
  40. CompressionFactor => { PrintConv => 'sprintf("%.2f", $val)' },
  41. );
  42. #------------------------------------------------------------------------------
  43. # Extract information from a Rawzor file
  44. # Inputs: 0) ExifTool object reference, 1) dirInfo reference
  45. # Returns: 1 on success, 0 if this wasn't a valid Rawzor file
  46. sub ProcessRWZ($$)
  47. {
  48. my ($et, $dirInfo) = @_;
  49. my $raf = $$dirInfo{RAF};
  50. my ($buff, $buf2);
  51. # read the Rawzor file header:
  52. # 0 string - "rawzor" signature
  53. # 6 int16u - Required SDK version
  54. # 8 int16u - Creator SDK version
  55. # 10 int64u - RWZ file size
  56. # 18 int64u - original raw file size
  57. # 26 undef[12] - reserved
  58. # 38 int64u - metadata offset
  59. $raf->Read($buff, 46) == 46 and $buff =~ /^rawzor/ or return 0;
  60. SetByteOrder('II');
  61. my $reqVers = Get16u(\$buff, 6);
  62. my $creatorVers = Get16u(\$buff, 8);
  63. my $rwzSize = Get64u(\$buff, 10);
  64. my $origSize = Get64u(\$buff, 18);
  65. my $tagTablePtr = GetTagTable('Image::ExifTool::Rawzor::Main');
  66. $et->HandleTag($tagTablePtr, RawzorRequiredVersion => $reqVers);
  67. $et->HandleTag($tagTablePtr, RawzorCreatorVersion => $creatorVers);
  68. $et->HandleTag($tagTablePtr, OriginalFileSize => $origSize);
  69. $et->HandleTag($tagTablePtr, CompressionFactor => $origSize/$rwzSize) if $rwzSize;
  70. # check version numbers
  71. if ($reqVers > $implementedRawzorVersion) {
  72. $et->Warn("Version $reqVers Rawzor images not yet supported");
  73. return 1;
  74. }
  75. my $metaOffset = Get64u(\$buff, 38);
  76. if ($metaOffset > 0x7fffffff) {
  77. $et->Warn('Bad metadata offset');
  78. return 1;
  79. }
  80. # check for the ability to uncompress the information
  81. unless (eval { require IO::Uncompress::Bunzip2 }) {
  82. $et->Warn('Install IO::Compress::Bzip2 to decode Rawzor bzip2 compression');
  83. return 1;
  84. }
  85. # read the metadata header:
  86. # 0 int64u - metadata section 0 end (offset in original file)
  87. # 8 int64u - metadata section 1 start
  88. # 16 int64u - metadata section 1 end
  89. # 24 int64u - metadata section 2 start
  90. # 32 undef[4] - reserved
  91. # 36 int32u - original metadata size
  92. # 40 int32u - compressed metadata size
  93. unless ($raf->Seek($metaOffset, 0) and $raf->Read($buff, 44) == 44) {
  94. $et->Warn('Error reading metadata header');
  95. return 1;
  96. }
  97. my $metaSize = Get32u(\$buff, 36);
  98. if ($metaSize) {
  99. # validate the metadata header and read the compressed metadata
  100. my $end0 = Get64u(\$buff, 0);
  101. my $pos1 = Get64u(\$buff, 8);
  102. my $end1 = Get64u(\$buff, 16);
  103. my $pos2 = Get64u(\$buff, 24);
  104. my $len = Get32u(\$buff, 40);
  105. unless ($raf->Read($buff, $len) == $len and
  106. $end0 + ($end1 - $pos1) + ($origSize - $pos2) == $metaSize and
  107. $end0 <= $pos1 and $pos1 <= $end1 and $end1 <= $pos2)
  108. {
  109. $et->Warn('Error reading image metadata');
  110. return 1;
  111. }
  112. # uncompress the metadata
  113. unless (IO::Uncompress::Bunzip2::bunzip2(\$buff, \$buf2) and
  114. length($buf2) eq $metaSize)
  115. {
  116. $et->Warn('Error uncompressing image metadata');
  117. return 1;
  118. }
  119. # re-assemble the original file (sans image data)
  120. undef $buff; # (can't hurt to free memory as soon as possible)
  121. $buff = substr($buf2, 0, $end0) . ("\0" x ($pos1 - $end0)) .
  122. substr($buf2, $end0, $end1 - $pos1) . ("\0" x ($pos2 - $end1)) .
  123. substr($buf2, $end0 + $end1 - $pos1, $origSize - $pos2);
  124. undef $buf2;
  125. # extract original information by calling ExtractInfo recursively
  126. $et->ExtractInfo(\$buff, { ReEntry => 1 });
  127. undef $buff;
  128. }
  129. # set OriginalFileType from FileType of original file
  130. # then change FileType and MIMEType to indicate a Rawzor image
  131. my $origFileType = $$et{VALUE}{FileType};
  132. if ($origFileType) {
  133. $et->HandleTag($tagTablePtr, OriginalFileType => $origFileType);
  134. $et->OverrideFileType('RWZ');
  135. } else {
  136. $et->HandleTag($tagTablePtr, OriginalFileType => 'Unknown');
  137. $et->SetFileType();
  138. }
  139. return 1;
  140. }
  141. 1; # end
  142. __END__
  143. =head1 NAME
  144. Image::ExifTool::Rawzor - Read meta information from Rawzor compressed images
  145. =head1 SYNOPSIS
  146. This module is used by Image::ExifTool
  147. =head1 DESCRIPTION
  148. This module contains definitions required by Image::ExifTool to extract meta
  149. information from Rawzor compressed images.
  150. =head1 AUTHOR
  151. Copyright 2003-2016, Phil Harvey (phil at owl.phy.queensu.ca)
  152. This library is free software; you can redistribute it and/or modify it
  153. under the same terms as Perl itself.
  154. =head1 REFERENCES
  155. =over 4
  156. =item L<http://www.rawzor.com/>
  157. =back
  158. =head1 SEE ALSO
  159. L<Image::ExifTool::TagNames/Rawzor Tags>,
  160. L<Image::ExifTool(3pm)|Image::ExifTool>
  161. =cut