BMP.pm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #------------------------------------------------------------------------------
  2. # File: BMP.pm
  3. #
  4. # Description: Read BMP meta information
  5. #
  6. # Revisions: 07/16/2005 - P. Harvey Created
  7. #
  8. # References: 1) http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
  9. # 2) http://www.fourcc.org/rgb.php
  10. #------------------------------------------------------------------------------
  11. package Image::ExifTool::BMP;
  12. use strict;
  13. use vars qw($VERSION);
  14. use Image::ExifTool qw(:DataAccess :Utils);
  15. $VERSION = '1.08';
  16. # BMP chunks
  17. %Image::ExifTool::BMP::Main = (
  18. PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
  19. GROUPS => { 0 => 'File', 1 => 'File', 2 => 'Image' },
  20. NOTES => q{
  21. There really isn't much meta information in a BMP file as such, just a bit
  22. of image related information.
  23. },
  24. # 0 => size of bitmap structure:
  25. # 12 bytes => 'OS/2 V1',
  26. # 40 bytes => 'Windows V3',
  27. # 64 bytes => 'OS/2 V2',
  28. # 68 bytes => some bitmap structure in AVI videos
  29. # 108 bytes => 'Windows V4',
  30. # 124 bytes => 'Windows V5',
  31. 4 => {
  32. Name => 'ImageWidth',
  33. Format => 'int32u',
  34. },
  35. 8 => {
  36. Name => 'ImageHeight',
  37. Format => 'int32s', # (negative when stored in top-to-bottom order)
  38. ValueConv => 'abs($val)',
  39. },
  40. 12 => {
  41. Name => 'Planes',
  42. Format => 'int16u',
  43. },
  44. 14 => {
  45. Name => 'BitDepth',
  46. Format => 'int16u',
  47. },
  48. 16 => {
  49. Name => 'Compression',
  50. Format => 'int32u',
  51. # (formatted as string[4] for some values in AVI images)
  52. ValueConv => '$val > 256 ? unpack("A4",pack("V",$val)) : $val',
  53. PrintConv => {
  54. 0 => 'None',
  55. 1 => '8-Bit RLE',
  56. 2 => '4-Bit RLE',
  57. 3 => 'Bitfields',
  58. 4 => 'JPEG', #2
  59. 5 => 'PNG', #2
  60. # pass through ASCII video compression codec ID's
  61. OTHER => sub {
  62. my $val = shift;
  63. # convert non-ascii characters
  64. $val =~ s/([\0-\x1f\x7f-\xff])/sprintf('\\x%.2x',ord $1)/eg;
  65. return $val;
  66. },
  67. },
  68. },
  69. 20 => {
  70. Name => 'ImageLength',
  71. Format => 'int32u',
  72. },
  73. 24 => {
  74. Name => 'PixelsPerMeterX',
  75. Format => 'int32u',
  76. },
  77. 28 => {
  78. Name => 'PixelsPerMeterY',
  79. Format => 'int32u',
  80. },
  81. 32 => {
  82. Name => 'NumColors',
  83. Format => 'int32u',
  84. PrintConv => '$val ? $val : "Use BitDepth"',
  85. },
  86. 36 => {
  87. Name => 'NumImportantColors',
  88. Format => 'int32u',
  89. PrintConv => '$val ? $val : "All"',
  90. },
  91. );
  92. # OS/2 12-byte bitmap header (ref http://www.fileformat.info/format/bmp/egff.htm)
  93. %Image::ExifTool::BMP::OS2 = (
  94. PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
  95. GROUPS => { 0 => 'File', 1 => 'File', 2 => 'Image' },
  96. NOTES => 'Information extracted from OS/2-format BMP images.',
  97. # 0 => size of bitmap structure (12)
  98. 4 => { Name => 'ImageWidth', Format => 'int16u' },
  99. 6 => { Name => 'ImageHeight', Format => 'int16u' },
  100. 8 => { Name => 'Planes', Format => 'int16u' },
  101. 10 => { Name => 'BitDepth', Format => 'int16u' },
  102. );
  103. #------------------------------------------------------------------------------
  104. # Extract EXIF information from a BMP image
  105. # Inputs: 0) ExifTool object reference, 1) dirInfo reference
  106. # Returns: 1 on success, 0 if this wasn't a valid BMP file
  107. sub ProcessBMP($$)
  108. {
  109. my ($et, $dirInfo) = @_;
  110. my $raf = $$dirInfo{RAF};
  111. my ($buff, $tagTablePtr);
  112. # verify this is a valid BMP file
  113. return 0 unless $raf->Read($buff, 18) == 18;
  114. return 0 unless $buff =~ /^BM/;
  115. SetByteOrder('II');
  116. my $len = Get32u(\$buff, 14);
  117. return 0 unless $len == 12 or $len >= 40;
  118. return 0 unless $raf->Seek(-4, 1) and $raf->Read($buff, $len) == $len;
  119. $et->SetFileType(); # set the FileType tag
  120. my %dirInfo = (
  121. DataPt => \$buff,
  122. DirStart => 0,
  123. DirLen => length($buff),
  124. );
  125. if ($len == 12) { # old OS/2 format BMP
  126. $tagTablePtr = GetTagTable('Image::ExifTool::BMP::OS2');
  127. } else {
  128. $tagTablePtr = GetTagTable('Image::ExifTool::BMP::Main');
  129. }
  130. $et->ProcessDirectory(\%dirInfo, $tagTablePtr);
  131. return 1;
  132. }
  133. 1; # end
  134. __END__
  135. =head1 NAME
  136. Image::ExifTool::BMP - Read BMP meta information
  137. =head1 SYNOPSIS
  138. This module is used by Image::ExifTool
  139. =head1 DESCRIPTION
  140. This module contains definitions required by Image::ExifTool to read BMP
  141. (Windows Bitmap) images.
  142. =head1 AUTHOR
  143. Copyright 2003-2016, Phil Harvey (phil at owl.phy.queensu.ca)
  144. This library is free software; you can redistribute it and/or modify it
  145. under the same terms as Perl itself.
  146. =head1 REFERENCES
  147. =over 4
  148. =item L<http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html>
  149. =back
  150. =head1 SEE ALSO
  151. L<Image::ExifTool::TagNames/BMP Tags>,
  152. L<Image::ExifTool(3pm)|Image::ExifTool>
  153. =cut