/// <summary> /// Determines image type via headers. /// Keeps stream position. /// </summary> /// <param name="imgData">Image data, incl header.</param> /// <returns>Type of image.</returns> public static SupportedExtensions DetermineImageType(Stream imgData) { SupportedExtensions ext = SupportedExtensions.UNKNOWN; // KFreon: Save position and go back to start long originalPos = imgData.Position; imgData.Seek(0, SeekOrigin.Begin); var bits = new byte[8]; imgData.Read(bits, 0, 8); // BMP if (BMP_Header.CheckIdentifier(bits)) { ext = SupportedExtensions.BMP; } // PNG if (PNG_Header.CheckIdentifier(bits)) { ext = SupportedExtensions.PNG; } // JPG if (JPG_Header.CheckIdentifier(bits)) { ext = SupportedExtensions.JPG; } // DDS if (DDS_Header.CheckIdentifier(bits)) { ext = SupportedExtensions.DDS; } // GIF if (GIF_Header.CheckIdentifier(bits)) { ext = SupportedExtensions.GIF; } if (TIFF_Header.CheckIdentifier(bits)) { ext = SupportedExtensions.TIF; } // TGA (assumed if no other matches if (ext == SupportedExtensions.UNKNOWN) { ext = SupportedExtensions.TGA; } // KFreon: Reset stream position imgData.Seek(originalPos, SeekOrigin.Begin); return(ext); }
internal static AbstractHeader LoadHeader(Stream stream) { stream.Seek(0, SeekOrigin.Begin); // Determine type of image ImageFormats.SupportedExtensions ext = ImageFormats.DetermineImageType(stream); // Parse header AbstractHeader header = null; switch (ext) { case ImageFormats.SupportedExtensions.BMP: header = new BMP_Header(stream); break; case ImageFormats.SupportedExtensions.DDS: header = new DDS_Header(stream); break; case ImageFormats.SupportedExtensions.JPG: header = new JPG_Header(stream); break; case ImageFormats.SupportedExtensions.PNG: header = new PNG_Header(stream); break; case ImageFormats.SupportedExtensions.TGA: header = new TGA_Header(stream); break; case ImageFormats.SupportedExtensions.GIF: header = new GIF_Header(stream); break; case ImageFormats.SupportedExtensions.TIF: header = new TIFF_Header(stream); break; default: throw new NotSupportedException("Image type unknown."); } return(header); }