public static void Run(string[] args) { if (args.Length < 2) { var appName = System.AppDomain.CurrentDomain.FriendlyName; Console.WriteLine("Invalid command. Syntax: " + appName + " <orig file> <dest file>"); } else { // Intermediate format Image image = null; string file1 = args[0]; string file2 = args[1]; // Check source and destionation formats FORMAT fromFormat; FORMAT toFormat; if (file1.ToLower().Contains(".dds")) { fromFormat = FORMAT.DDS; } else if (file1.ToLower().Contains(".bmp")) { fromFormat = FORMAT.BMP; } else { Console.WriteLine("Unknown file extension: " + file1); return; } if (file2.ToLower().Contains(".dds")) { toFormat = FORMAT.DDS; } else if (file2.ToLower().Contains(".bmp")) { toFormat = FORMAT.BMP; } else { Console.WriteLine("Unknown file extension: " + file2); return; } try { // Read into Intermediate format using (var fileStream = new FileStream(file1, FileMode.Open)) { if (fromFormat == FORMAT.DDS) { image = DDS.DDS.read(fileStream); } else if (fromFormat == FORMAT.BMP) { image = BMP.BMP.read(fileStream); } fileStream.Close(); } // Save into final format File.Delete(file2); using (var fileStream = new FileStream(file2, FileMode.OpenOrCreate)) { if (toFormat == FORMAT.DDS) { DDS.DDS dds = image; dds.write(fileStream); } else if (toFormat == FORMAT.BMP) { BMP.BMP bmp = image; bmp.write(fileStream); } fileStream.Close(); } } catch (FileNotFoundException e) { Console.WriteLine("File not found! " + file1); } } }
public static BMP read(Stream stream) { var image = new BMP(); int readIndex = 0; // Read headers readIndex += stream.ReadStruct(out image.fileHeader); readIndex += stream.ReadStruct(out image.infoHeader); if (image.infoHeader.biBitCount != 24 || image.infoHeader.biCompression != BITMAPINFOHEADER.CompressionMode.BI_RGB) { throw new NotImplementedException("Format not implemented"); } // Ensure we jump to the offset int seek = (int)image.fileHeader.bfOffBits - readIndex; stream.Seek(seek, SeekOrigin.Current); uint _height = image.uheight; uint _width = image.width; uint _size = _height * _width; // Initialize pixel matrix image.pixels = new BGR[_size]; uint imagePadding = image.padding; // Optimize loop if there is no padding if (imagePadding > 0) { uint index = 0; for (uint i = 0; i < _height; i++) { for (uint j = 0; j < _width; j++, index++) { BGR color; readIndex += stream.ReadStruct(out color); image[index] = color; } if (imagePadding > 0) { stream.Seek(imagePadding, SeekOrigin.Current); } } } else { for (uint i = 0; i < _size; i++) { BGR color; readIndex += stream.ReadStruct(out color); image[i] = color; } } return image; }
public static BMP read(Stream stream) { var image = new BMP(); int readIndex = 0; // Read headers readIndex += stream.ReadStruct(out image.fileHeader); readIndex += stream.ReadStruct(out image.infoHeader); if (image.infoHeader.biBitCount != 24 || image.infoHeader.biCompression != BITMAPINFOHEADER.CompressionMode.BI_RGB) { throw new NotImplementedException("Format not implemented"); } // Ensure we jump to the offset int seek = (int)image.fileHeader.bfOffBits - readIndex; stream.Seek(seek, SeekOrigin.Current); uint _height = image.uheight; uint _width = image.width; uint _size = _height * _width; // Initialize pixel matrix image.pixels = new BGR[_size]; uint imagePadding = image.padding; // Optimize loop if there is no padding if (imagePadding > 0) { uint index = 0; for (uint i = 0; i < _height; i++) { for (uint j = 0; j < _width; j++, index++) { BGR color; readIndex += stream.ReadStruct(out color); image[index] = color; } if (imagePadding > 0) { stream.Seek(imagePadding, SeekOrigin.Current); } } } else { for (uint i = 0; i < _size; i++) { BGR color; readIndex += stream.ReadStruct(out color); image[i] = color; } } return(image); }