forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathImageDecoder.cs
193 lines (166 loc) · 7.03 KB
/
ImageDecoder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// AForge Image Formats Library
// AForge.NET framework
//
// Copyright © Andrew Kirillov, 2005-2008
// andrew.kirillov@gmail.com
//
namespace AForge.Imaging.Formats
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
/// <summary>
/// Image decoder to decode different custom image file formats.
/// </summary>
///
/// <remarks><para>The class represent a help class, which simplifies decoding of image
/// files finding appropriate image decoder automatically (using list of registered
/// image decoders). Instead of using required image decoder directly, users may use this
/// class, which will find required decoder by file's extension.</para>
///
/// <para>By default the class registers on its own all decoders, which are available in
/// AForge.Imaging.Formats library. If user has implementation of his own image decoders, he
/// needs to register them using <see cref="RegisterDecoder"/> method to be able to use them through
/// the <see cref="ImageDecoder"/> class.</para>
///
/// <para><note>If the class can not find appropriate decode in the list of registered
/// decoders, it passes file to .NET's image decoder for decoding.</note></para>
///
/// <para>Sample usage:</para>
/// <code>
/// // sample file name
/// string fileName = "myFile.pnm";
/// // decode image file
/// Bitmap = ImageDecoder.DecodeFromFile( fileName );
/// </code>
/// </remarks>
///
/// <seealso cref="PNMCodec"/>
/// <seealso cref="FITSCodec"/>
///
public class ImageDecoder
{
private static Dictionary< string, IImageDecoder > decoders = new Dictionary<string, IImageDecoder>( );
static ImageDecoder( )
{
// register PNM file format
IImageDecoder decoder = new PNMCodec( );
RegisterDecoder( "pbm", decoder );
RegisterDecoder( "pgm", decoder );
RegisterDecoder( "pnm", decoder );
RegisterDecoder( "ppm", decoder );
// register FITS file format
decoder = new FITSCodec( );
RegisterDecoder( "fit", decoder );
RegisterDecoder( "fits", decoder );
}
/// <summary>
/// Register image decoder for a specified file extension.
/// </summary>
///
/// <param name="fileExtension">File extension to register decoder for ("bmp", for example).</param>
/// <param name="decoder">Image decoder to use for the specified file extension.</param>
///
/// <remarks><para>The method allows to register image decoder object, which should be used
/// to decode images from files with the specified extension.</para></remarks>
///
public static void RegisterDecoder( string fileExtension, IImageDecoder decoder )
{
System.Diagnostics.Debug.WriteLine( "Registering decoder: " + fileExtension );
decoders.Add( fileExtension.ToLower( ), decoder );
}
/// <summary>
/// Decode first frame for the specified file.
/// </summary>
///
/// <param name="fileName">File name to read image from.</param>
///
/// <returns>Return decoded image. In the case if file format support multiple
/// frames, the method return the first frame.</returns>
///
/// <remarks><para>The method uses table of registered image decoders to find the one,
/// which should be used for the specified file. If there is not appropriate decoder
/// found, the method uses default .NET's image decoding routine (see
/// <see cref="System.Drawing.Image.FromFile( string )"/>).</para></remarks>
///
public static Bitmap DecodeFromFile( string fileName )
{
ImageInfo imageInfo = null;
return DecodeFromFile( fileName, out imageInfo );
}
/// <summary>
/// Decode first frame for the specified file.
/// </summary>
///
/// <param name="fileName">File name to read image from.</param>
/// <param name="imageInfo">Information about the decoded image.</param>
///
/// <returns>Return decoded image. In the case if file format support multiple
/// frames, the method return the first frame.</returns>
///
/// <remarks><para>The method uses table of registered image decoders to find the one,
/// which should be used for the specified file. If there is not appropriate decoder
/// found, the method uses default .NET's image decoding routine (see
/// <see cref="System.Drawing.Image.FromFile( string )"/>).</para></remarks>
///
public static Bitmap DecodeFromFile( string fileName, out ImageInfo imageInfo )
{
Bitmap bitmap = null;
string fileExtension = Path.GetExtension( fileName ).ToLower( );
if ( ( fileExtension != string.Empty ) && ( fileExtension.Length != 0 ) )
{
fileExtension = fileExtension.Substring( 1 );
if ( decoders.ContainsKey( fileExtension ) )
{
IImageDecoder decoder = decoders[fileExtension];
// open stream
FileStream stream = new FileStream( fileName, FileMode.Open );
// open decoder
decoder.Open( stream );
// read the first frame
bitmap = decoder.DecodeFrame( 0, out imageInfo );
// close decoder and stream
decoder.Close( );
stream.Close( );
stream.Dispose( );
return bitmap;
}
}
// use default .NET's image decoding routine
bitmap = FromFile( fileName );
imageInfo = new ImageInfo( bitmap.Width, bitmap.Height, Image.GetPixelFormatSize( bitmap.PixelFormat ), 0, 1 );
return bitmap;
}
private static System.Drawing.Bitmap FromFile( string fileName )
{
Bitmap loadedImage = null;
FileStream stream = null;
try
{
// read image to temporary memory stream
stream = File.OpenRead( fileName );
MemoryStream memoryStream = new MemoryStream( );
byte[] buffer = new byte[10000];
while ( true )
{
int read = stream.Read( buffer, 0, 10000 );
if ( read == 0 )
break;
memoryStream.Write( buffer, 0, read );
}
loadedImage = (Bitmap) Bitmap.FromStream( memoryStream );
}
finally
{
if ( stream != null )
{
stream.Close( );
stream.Dispose( );
}
}
return loadedImage;
}
}
}