static void ProcessCV2(string inputName) { string outputName = Path.ChangeExtension(inputName, "cv2"); try { CV2Reader cv2; using (FileStream ofs = new FileStream(outputName, FileMode.Open, FileAccess.Read)) { cv2 = new CV2Reader(ofs); if (cv2.Header.bpp == 8) { throw new Exception(inputName + " : do NOT support Palette !!"); } } using (FileStream ifs = new FileStream(inputName, FileMode.Open, FileAccess.Read)) { Bitmap image = cv2.Image; Bitmap inputImage = (Bitmap)Image.FromStream(ifs); using (FileStream ofs = new FileStream(outputName, FileMode.Create, FileAccess.Write)) { Binary ob = new Binary(ofs); cv2.Header.Serialize(ob); System.Console.WriteLine(inputName + " --> " + outputName); for (int x = 0; x < cv2.Header.Height; x++) for (int y = 0; y < cv2.Header.PaddedWidth; y++) { if (y < cv2.Header.Width) { Color c = inputImage.GetPixel(y, x); switch (cv2.Header.bpp) { case 8: throw new Exception(inputName + " : do NOT support Palette !!"); case 16: { short b = (short)((c.R>>3) << 11 | (c.G>>2) << 5 | (c.B>>3)); ob.WriteInt16(b); break; } case 24: { int b = 255<<24 | c.R << 16 | c.G << 8 | c.B; ob.WriteInt32(b); break; } case 32: { int b = c.A << 24 | c.R << 16 | c.G << 8 | c.B; ob.WriteInt32(b); break; } default: throw new Exception(inputName + " : unknown bpp !!"); } } else { switch (cv2.Header.bpp) { case 8: throw new Exception(inputName + " : do NOT support Palette !!"); case 16: ob.WriteInt16(0); break; case 24: ob.WriteInt32(0); break; case 32: ob.WriteInt32(0); break; default: throw new Exception(inputName + " : unknown bpp !!"); } } } } } File.Delete(inputName); } catch (Exception e) { System.Console.Error.Write(e.Message); } }
static void ProcessCV2(string inputName) { string outputName = Path.ChangeExtension(inputName, "cv2"); try { CV2Reader cv2; using (FileStream ofs = new FileStream(outputName, FileMode.Open, FileAccess.Read)) { cv2 = new CV2Reader(ofs); if (cv2.Header.bpp == 8) { throw new Exception(inputName + " : do NOT support Palette !!"); } } using (FileStream ifs = new FileStream(inputName, FileMode.Open, FileAccess.Read)) { Bitmap image = cv2.Image; Bitmap inputImage = (Bitmap)Image.FromStream(ifs); using (FileStream ofs = new FileStream(outputName, FileMode.Create, FileAccess.Write)) { Binary ob = new Binary(ofs); cv2.Header.Serialize(ob); System.Console.WriteLine(inputName + " --> " + outputName); for (int x = 0; x < cv2.Header.Height; x++) { for (int y = 0; y < cv2.Header.PaddedWidth; y++) { if (y < cv2.Header.Width) { Color c = inputImage.GetPixel(y, x); switch (cv2.Header.bpp) { case 8: throw new Exception(inputName + " : do NOT support Palette !!"); case 16: { short b = (short)((c.R >> 3) << 11 | (c.G >> 2) << 5 | (c.B >> 3)); ob.WriteInt16(b); break; } case 24: { int b = 255 << 24 | c.R << 16 | c.G << 8 | c.B; ob.WriteInt32(b); break; } case 32: { int b = c.A << 24 | c.R << 16 | c.G << 8 | c.B; ob.WriteInt32(b); break; } default: throw new Exception(inputName + " : unknown bpp !!"); } } else { switch (cv2.Header.bpp) { case 8: throw new Exception(inputName + " : do NOT support Palette !!"); case 16: ob.WriteInt16(0); break; case 24: ob.WriteInt32(0); break; case 32: ob.WriteInt32(0); break; default: throw new Exception(inputName + " : unknown bpp !!"); } } } } } } File.Delete(inputName); } catch (Exception e) { System.Console.Error.Write(e.Message); } }