// основная функция чтения файла static int Main(string[] args) { // Если нехватает параметров - выводим "помощь" if (args.Length < 1) { WriteHelp(); return(2); } bool debug = false; string pass = ""; bool ch_crc = true; foreach (string s in args) { if (s.ToUpper() == "-NOCRC" || s.ToUpper() == "-NCRC") { ch_crc = false; // Отключение контрольной суммы } if (s.ToUpper() == "-DEBUG" || s.ToUpper() == "-D") { debug = true; // Включение отладочной информации } if (s.ToUpper() == "-H" || s.ToUpper() == "-?" || s.ToUpper() == "/?") { WriteHelp(); return(0); } // вывод "помощи" // указание пароля if (s.Substring(0, 2).ToUpper() == "-P") { pass = s.Substring(2); } } // пробуем читать файл System.IO.FileStream fs; try { fs = new System.IO.FileStream(args[0], System.IO.FileMode.Open); } catch { return(2); } byte[] IBytes = new byte[4]; fs.Read(IBytes, 0, 4); #region V2 // для формата версии 2 if (IBytes[3] == 2) { fs.Read(IBytes, 0, 4); int chanel = BitConverter.ToInt32(IBytes, 0); fs.Read(IBytes, 0, 4); int width = BitConverter.ToInt32(IBytes, 0); fs.Read(IBytes, 0, 4); int height = BitConverter.ToInt32(IBytes, 0); fs.Read(IBytes, 0, 4); int widthR = BitConverter.ToInt32(IBytes, 0); fs.Read(IBytes, 0, 4); YCrCb q; // для наличия отсутствия фона if (IBytes[0] == 1) { q = new YCrCb(chanel, width, height, widthR, new YCrCb.YCrCbPoint(IBytes[1], IBytes[2], IBytes[3])); } else { q = new YCrCb(chanel, width, height, widthR, null); } if (debug) { Console.WriteLine("File version 2"); Console.WriteLine("W:" + widthR + " H:" + height); if (IBytes[0] == 1) { Console.WriteLine("Base color present."); } else { Console.WriteLine("Without base color."); } } int yy = 0; while (true) { try { // читаем маркер int y = fs.Read(IBytes, 0, 4); if (y == 0) { break; } int QL = BitConverter.ToInt32(IBytes, 0); if (QL > 0) { // читаем фрагмент byte[] Zcmpr = new byte[QL]; fs.Read(Zcmpr, 0, QL); if (debug) { yy++; Console.WriteLine(); Console.WriteLine("Ch#" + yy); Console.WriteLine("Size in file " + QL + " bytes"); } q.setZoneBytes(Zcmpr, debug); } } catch { exitCode = 3; break; } } q.YCrCb2RGB(); q.writeBMP(args[0] + ".BMP"); return(exitCode); } #endregion V2 #region V3-4 // для формата версии 3 if (IBytes[3] == 3 || IBytes[3] == 4) { int VV = IBytes[3]; fs.Read(IBytes, 0, 4); int chanel = BitConverter.ToInt32(IBytes, 0); fs.Read(IBytes, 0, 4); int width = BitConverter.ToInt32(IBytes, 0); fs.Read(IBytes, 0, 4); int height = BitConverter.ToInt32(IBytes, 0); fs.Read(IBytes, 0, 4); int widthR = BitConverter.ToInt32(IBytes, 0); fs.Read(IBytes, 0, 4); YCrCb q; // с наличием или отсутствием фона if (IBytes[0] == 1) { q = new YCrCb(chanel, width, height, widthR, new YCrCb.YCrCbPoint(IBytes[1], IBytes[2], IBytes[3])); } else { q = new YCrCb(chanel, width, height, widthR, null); } if (debug) { Console.WriteLine("File version " + VV); if (VV == 4) { Console.WriteLine("Crypted"); } Console.WriteLine("W:" + widthR + " H:" + height); if (IBytes[0] == 1) { Console.WriteLine("Base color present."); } else { Console.WriteLine("Without base color."); } } _Rijndael crpt = new _Rijndael(); crpt.Key = pass; int yy = 0; while (true) { try { // чтение маркера фрагмента (длины) int y = fs.Read(IBytes, 0, 4); if (y == 0) { break; } int QL = BitConverter.ToInt32(IBytes, 0); if (QL > 0) { // чтение фрагмента byte[] Zcmpr = new byte[QL]; fs.Read(Zcmpr, 0, QL); if (QL > 16) { if (debug) { yy++; Console.WriteLine(); Console.WriteLine("Ch#" + yy); Console.WriteLine("Size in file " + QL + " bytes"); } if (VV == 4) { // при включенном шифровании int yyy = q.setZoneBytesV3(crpt.Decrypt(Zcmpr), debug, VV, ch_crc); if (exitCode == 0 && yyy != 0) { exitCode = yyy; } } else { // при отключенном шифровании int yyy = q.setZoneBytesV3(Zcmpr, debug, VV, ch_crc); if (exitCode == 0 && yyy != 0) { exitCode = yyy; } } } } } catch { break; } } q.YCrCb2RGB(); q.writeBMP(args[0] + ".BMP"); return(exitCode); } #endregion V3-4 // сообщение об ошибке Console.WriteLine("The file contains the unsupported version"); return(1); }
static int Main(string[] args) { int exitCode = 0; if (args.Length < 2) { WriteHelp(); return(2); } YCrCb c = new YCrCb(args[0]); if (!c.IsCorrect()) { Console.WriteLine("File read error!"); return(2); } c.RGB2YCrCb(); bool debug = false; string pass = ""; bool uPass = false; bool crc = false; foreach (string s in args) { if (s.ToUpper() == "-CRCNO") { crc = true; } if (s.ToUpper() == "-H" || s.ToUpper() == "-?" || s.ToUpper() == "/?") { WriteHelp(); return(0); } if (s.ToUpper() == "-DEBUG" || s.ToUpper() == "-D") { debug = true; } if (s.Substring(0, 2).ToUpper() == "-P") { pass = s.Substring(2); uPass = true; } } YCrCb tmp = c.GetCopy(); if (debug) { tmp.convMono(); tmp.writeBMP("Y.bmp"); tmp = c.GetCopy(); tmp.convCr(); tmp.writeBMP("Cr.bmp"); tmp = c.GetCopy(); tmp.convCb(); tmp.writeBMP("Cb.bmp"); } //if (c.isMono()) // c.convMono(); c.getBaseColor(); c.makeBlock(); if (debug) { tmp = c.GetCopy(); tmp.drawBlock(); tmp.convMono(); tmp.writeBMP("Y_b.bmp"); tmp = c.GetCopy(); tmp.drawBlock(); tmp.convCr(); tmp.writeBMP("Cr_b.bmp"); tmp = c.GetCopy(); tmp.drawBlock(); tmp.convCb(); tmp.writeBMP("Cb_b.bmp"); tmp = c.GetCopy(); tmp.convMono(); tmp.drawZone(0); tmp.YCrCb2RGB(); tmp.writeBMP("Y_Z.bmp"); tmp = c.GetCopy(); tmp.convCr(); tmp.drawZone(1); tmp.YCrCb2RGB(); tmp.writeBMP("Cr_Z.bmp"); tmp = c.GetCopy(); tmp.convCb(); tmp.drawZone(2); tmp.YCrCb2RGB(); tmp.writeBMP("Cb_Z.bmp"); } System.IO.FileStream fs = new System.IO.FileStream(args[1] + ".YCC", System.IO.FileMode.Create); byte[] bInt; bInt = BitConverter.GetBytes('Y'); fs.Write(bInt, 0, 1); bInt = BitConverter.GetBytes('C'); fs.Write(bInt, 0, 1); bInt = BitConverter.GetBytes('C'); fs.Write(bInt, 0, 1); if (uPass) { bInt[0] = (byte)4; } else { bInt[0] = (byte)3; } fs.Write(bInt, 0, 1); bInt = BitConverter.GetBytes(c.chanel); fs.Write(bInt, 0, 4); bInt = BitConverter.GetBytes(c.width); fs.Write(bInt, 0, 4); bInt = BitConverter.GetBytes(c.height); fs.Write(bInt, 0, 4); bInt = BitConverter.GetBytes(c.widthR); fs.Write(bInt, 0, 4); byte[] bB = new byte[4]; if (c.baseColor != null) { bB[0] = (byte)1; bB[1] = c.baseColor.Y; bB[2] = c.baseColor.Cr; bB[3] = c.baseColor.Cb; } else { bB[0] = (byte)0; } fs.Write(bB, 0, 4); _Rijndael crpt = new _Rijndael(); crpt.Key = pass; for (int i = 0; i < c.ZoneQTY; i++) { byte[] bQQ = c.getZoneBytes(i, crc); byte[] b; if (bQQ.Length > 0) { if (uPass) { b = crpt.Encrypt(bQQ); } else { b = bQQ; } try { int Q = b.Length; byte[] bQ = BitConverter.GetBytes(b.Length); fs.Write(bQ, 0, bQ.Length); fs.Write(b, 0, b.Length); } catch /*(Exception ex)*/ { exitCode = 3; } finally { } } } fs.Close(); return(exitCode); }