/// <summary>
/// Gets an instance of an Image.
/// </summary>
/// <param name="img">a byte array</param>
/// <returns>an object of type Gif, Jpeg or Png</returns>
public static Image GetInstance(byte[] imgb, bool recoverFromImageError) {
RandomAccessSourceFactory randomAccessSourceFactory = new RandomAccessSourceFactory();
int c1 = imgb[0];
int c2 = imgb[1];
int c3 = imgb[2];
int c4 = imgb[3];
if (c1 == 'G' && c2 == 'I' && c3 == 'F') {
GifImage gif = new GifImage(imgb);
return gif.GetImage(1);
}
if (c1 == 0xFF && c2 == 0xD8) {
return new Jpeg(imgb);
}
if (c1 == 0x00 && c2 == 0x00 && c3 == 0x00 && c4 == 0x0c) {
return new Jpeg2000(imgb);
}
if (c1 == 0xff && c2 == 0x4f && c3 == 0xff && c4 == 0x51) {
return new Jpeg2000(imgb);
}
if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1]
&& c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) {
return PngImage.GetImage(imgb);
}
if (c1 == 0xD7 && c2 == 0xCD) {
return new ImgWMF(imgb);
}
if (c1 == 'B' && c2 == 'M') {
return BmpImage.GetImage(imgb);
}
if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
|| (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) {
RandomAccessFileOrArray ra = null;
try {
ra = new RandomAccessFileOrArray(randomAccessSourceFactory.CreateSource(imgb));
Image img = TiffImage.GetTiffImage(ra, 1);
if (img.OriginalData == null)
img.OriginalData = imgb;
return img;
} catch ( Exception e ) {
if ( recoverFromImageError ) {
// reruns the getTiffImage() with several error recovering workarounds in place
// not guaranteed to work with every TIFF
Image img = TiffImage.GetTiffImage(ra, recoverFromImageError, 1);
if (img.OriginalData == null)
img.OriginalData = imgb;
return img;
}
throw e;
} finally {
if (ra != null)
ra.Close();
}
}
if (c1 == 0x97 && c2 == 'J' && c3 == 'B' && c4 == '2') {
int c5 = imgb[4];
int c6 = imgb[5];
int c7 = imgb[6];
int c8 = imgb[7];
if (c5 == '\r' && c6 == '\n' && c7 == 0x1a && c8 == '\n') {
// a jbig2 file with a file header. the header is the only way we know here.
// embedded jbig2s don't have a header, have to create them by explicit use of Jbig2Image?
// nkerr, 2008-12-05 see also the getInstance(URL)
RandomAccessFileOrArray ra = null;
try {
ra = new RandomAccessFileOrArray(randomAccessSourceFactory.CreateSource(imgb));
Image img = JBIG2Image.GetJbig2Image(ra, 1);
if (img.OriginalData == null)
img.OriginalData = imgb;
return img;
}
finally {
if (ra != null)
ra.Close();
}
}
}
throw new IOException(MessageLocalization.GetComposedMessage("the.byte.array.is.not.a.recognized.imageformat"));
}