Added FilterDeb from Kevin Kubasik.
[beagle.git] / Filters / FilterBmp.cs
blobe116bc9b3e850c7d3287158ea18f3e93b04b311c
1 //
2 // FilterBmp.cs
3 //
4 // Copyright (C) 2006 Alexander Macdonald <alex@alexmac.cc>
5 //
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.
26 using System;
27 using System.IO;
28 using System.Text;
30 using Beagle.Util;
31 using Beagle.Daemon;
33 namespace Beagle.Filters
35 public class FilterBMP : FilterImage
37 enum BitmapCompressionTypes {
38 None,
39 RunLength8Bit,
40 RunLength4Bit,
41 RGBBitmapWithMask
44 struct BitmapHeader {
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"));
66 PreLoad = false;
69 protected override void PullImageProperties ()
71 byte [] data = new byte [54];
72 Stream.Read (data, 0, data.Length);
74 // Read the Header
75 // We're not using it for anything, so we might as well not parse it.
77 BitmapHeader bmh;
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);
99 Width = bmih.width;
100 Height = bmih.height;
101 Depth = bmih.bits;
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"));
108 break;
109 case BitmapCompressionTypes.RunLength8Bit:
110 AddProperty (Beagle.Property.NewUnsearched ("exif:Compression", "8bit Runlength"));
111 break;
112 case BitmapCompressionTypes.RunLength4Bit:
113 AddProperty (Beagle.Property.NewUnsearched ("exif:Compression", "4bit Runlength"));
114 break;
115 case BitmapCompressionTypes.RGBBitmapWithMask:
116 AddProperty (Beagle.Property.NewUnsearched ("exif:Compression", "RGB bitmap with mask"));
117 break;
118 default:
119 AddProperty (Beagle.Property.NewUnsearched ("exif:Compression", "unknown"));
120 break;
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));