4 // Copyright (C) 2006 Alexander Macdonald <alex@alexmac.cc>
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
33 namespace Beagle
.Filters
35 public class FilterBMP
: FilterImage
37 enum BitmapCompressionTypes
{
45 public ushort type
; // Magic identifier
46 public uint size
; // File size in bytes
47 //public ushort reserved1, reserved2; // unused
48 public uint offset
; // Offset to image data, bytes
51 struct BitmapInfoHeader
{
52 public uint size
; // Header size in bytes
53 public int width
,height
; // width and height of image
54 public ushort planes
; // Number of colour planes
55 public ushort bits
; // Bits per pixel
56 public BitmapCompressionTypes compression
; // Compression type
57 public uint imagesize
; // Image size in bytes
58 public int xresolution
,yresolution
; // Pixels per meter
59 public uint ncolors
; // Number of colours
60 public uint importantcolors
; // Important colours
63 public FilterBMP () : base ()
65 AddSupportedFlavor (FilterFlavor
.NewFromMimeType ("image/bmp"));
69 protected override void PullImageProperties ()
71 byte [] data
= new byte [54];
72 Stream
.Read (data
, 0, data
.Length
);
75 // We're not using it for anything, so we might as well not parse it.
78 bmh.type = EndianConverter.ToUInt16 (data, 0, true);
79 bmh.size = EndianConverter.ToUInt32 (data, 2, true);
80 bmh.reserved1 = EndianConverter.ToUInt16 (data, 6, true);
81 bmh.reserved2 = EndianConverter.ToUInt16 (data, 8, true);
82 bmh.offset = EndianConverter.ToUInt32 (data, 10, true);
85 /* Read the Info Header */
86 BitmapInfoHeader bmih
;
87 bmih
.size
= EndianConverter
.ToUInt32 (data
, 14, true);
88 bmih
.width
= EndianConverter
.ToInt32 (data
, 18, true);
89 bmih
.height
= EndianConverter
.ToInt32 (data
, 22, true);
90 bmih
.planes
= EndianConverter
.ToUInt16 (data
, 26, true);
91 bmih
.bits
= EndianConverter
.ToUInt16 (data
, 28, true);
92 bmih
.compression
= (BitmapCompressionTypes
) EndianConverter
.ToUInt32 (data
, 30, true);
93 bmih
.imagesize
= EndianConverter
.ToUInt32 (data
, 34, true);
94 bmih
.xresolution
= EndianConverter
.ToInt32 (data
, 38, true);
95 bmih
.yresolution
= EndianConverter
.ToInt32 (data
, 42, true);
96 bmih
.ncolors
= EndianConverter
.ToUInt32 (data
, 46, true);
97 bmih
.importantcolors
= EndianConverter
.ToUInt32 (data
, 50, true);
100 Height
= bmih
.height
;
103 AddProperty (Beagle
.Property
.NewUnsearched ("exif:Planes", bmih
.planes
));
105 switch (bmih
.compression
) {
106 case BitmapCompressionTypes
.None
:
107 AddProperty (Beagle
.Property
.NewUnsearched ("exif:Compression", "none"));
109 case BitmapCompressionTypes
.RunLength8Bit
:
110 AddProperty (Beagle
.Property
.NewUnsearched ("exif:Compression", "8bit Runlength"));
112 case BitmapCompressionTypes
.RunLength4Bit
:
113 AddProperty (Beagle
.Property
.NewUnsearched ("exif:Compression", "4bit Runlength"));
115 case BitmapCompressionTypes
.RGBBitmapWithMask
:
116 AddProperty (Beagle
.Property
.NewUnsearched ("exif:Compression", "RGB bitmap with mask"));
119 AddProperty (Beagle
.Property
.NewUnsearched ("exif:Compression", "unknown"));
123 AddProperty (Beagle
.Property
.NewUnsearched ("exif:XResolution", bmih
.xresolution
));
124 AddProperty (Beagle
.Property
.NewUnsearched ("exif:YResolution", bmih
.yresolution
));
125 AddProperty (Beagle
.Property
.NewUnsearched ("exif:NumberOfColors", bmih
.ncolors
));
126 AddProperty (Beagle
.Property
.NewUnsearched ("exif:ImportantColors", bmih
.importantcolors
));