/// <summary> /// Gets the image format from the leading byte identifiers. /// </summary> /// <param name="path">The path to the image file.</param> /// <returns>The image format.</returns> public static ImgFormat GetImageFormat(string path) { if (string.IsNullOrEmpty(path) || !File.Exists(path)) { return(ImgFormat.nil); } try { using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) using (BinaryReader binaryReader = new BinaryReader(fileStream)) { byte[] magicBytes = new byte[MAX_MAGIC_BYTE_LENGTH]; for (int i = 0; i < MAX_MAGIC_BYTE_LENGTH; i += 1) { magicBytes[i] = binaryReader.ReadByte(); foreach (KeyValuePair <byte[], ImgFormat> kvPair in Image_Byte_Identifiers) { if (ByteHelper.StartsWith(magicBytes, kvPair.Key)) { return(kvPair.Value); } } } return(ImgFormat.nil); } } catch { return(ImgFormat.nil); } }
/// <summary> /// Reads the first 5 bytes from the reader to identify a WORM image. /// </summary> /// <param name="binaryReader">The reader.</param> /// <returns>The format of a worm image. nil for invalid</returns> public static WormFormat GetWormFormat(BinaryReader binaryReader) { byte[] identifier = binaryReader.ReadBytes(5); if (ByteHelper.StartsWith(identifier, IdentifierBytes_1)) { return(WormFormat.wrm); } if (ByteHelper.StartsWith(identifier, IdentifierBytes_2)) { return(WormFormat.dwrm); } return(WormFormat.nil); }
/// <summary> /// Gets the dimensions of an image. /// </summary> /// <param name="path">The path of the image to get the dimensions of.</param> /// <returns>The dimensions of the specified image.</returns> /// <exception cref="ArgumentException">The image was of an unrecognised format.</exception> public static Size GetDimensions(BinaryReader binaryReader) { byte[] magicBytes = new byte[MAX_MAGIC_BYTE_LENGTH]; for (int i = 0; i < MAX_MAGIC_BYTE_LENGTH; i += 1) { magicBytes[i] = binaryReader.ReadByte(); foreach (KeyValuePair <byte[], Func <BinaryReader, Size> > kvPair in Image_Format_Decoders) { if (ByteHelper.StartsWith(magicBytes, kvPair.Key)) { return(kvPair.Value(binaryReader)); } } } return(Size.Empty); }
/// <summary> /// Checks if param 2 starts with param 1. /// </summary> /// <returns>True if param 2 starts with param 1.</returns> public static bool StartsWith(this byte[] thisBytes, byte[] thatBytes) { return(ByteHelper.StartsWith(thatBytes, thisBytes)); }