public void LoadImage(string path, out bool loadSuccess, out string errorOrWarning) { loadSuccess = true; errorOrWarning = null; try { png = new PngData(path); // warning for big size int size = png.GetPixelWidth * png.GetPixelHeight; if (size >= 1048576) // 1024*1024 { errorOrWarning = "The size of the image is extreme! Next steps will be at your own risk."; } } catch (Exception ex) { loadSuccess = false; errorOrWarning = ex.Message; } }
public void Load(PngData png) { var bytelist = new List <byte>(png.originalData.ToByteArray()); var index = 0; byte[] temp_byte; temp_byte = bytelist.GetRange(index, 4).ToArray(); index += 4; if (temp_byte[0] != (byte)'P') { throw new Exception("P is error"); } if (temp_byte[1] != (byte)'D') { throw new Exception("D is error"); } if (temp_byte[2] != (byte)'C') { throw new Exception("C is error"); } if (temp_byte[3] != (byte)'A') { throw new Exception("A is error"); } temp_byte = bytelist.GetRange(index, 4).ToArray(); index += 4; if (BitConverter.ToInt32(temp_byte, 0) != 1) { throw new Exception("zip type error"); } temp_byte = bytelist.GetRange(index, 4).ToArray(); index += 4; if (ZipData.n_byte != BitConverter.ToInt32(temp_byte, 0)) { throw new Exception("n_byte is error"); } // 元のファイルの文字列の長さ 4byte temp_byte = bytelist.GetRange(index, 4).ToArray(); index += 4; int path_bytes_Length = BitConverter.ToInt32(temp_byte, 0); // 元のファイルの文字列データ path_bytes_Length byte this.path = Encoding.UTF8.GetString(bytelist.GetRange(index, path_bytes_Length).ToArray()); index += path_bytes_Length; temp_byte = bytelist.GetRange(index, 4).ToArray(); index += 4; int Code_Count = BitConverter.ToInt32(temp_byte, 0); temp_byte = bytelist.GetRange(index, 4).ToArray(); index += 4; this.byteNumber = BitConverter.ToInt32(temp_byte, 0); this.Codes = new Dictionary <BoolList, InfoSource>(); this.InvertedCodes = new Dictionary <InfoSource, BoolList>(); for (int i = 0; i < Code_Count; i++) { temp_byte = bytelist.GetRange(index, ZipData.n_byte).ToArray(); index += ZipData.n_byte; var element = new InfoSource(temp_byte); temp_byte = bytelist.GetRange(index, 4).ToArray(); index += 4; var code_length = BitConverter.ToInt32(temp_byte, 0); var code_byte_length = code_length / 8 + (code_length % 8 == 0?0:1); temp_byte = bytelist.GetRange(index, code_byte_length).ToArray(); index += code_byte_length; var boollist_temp = new BoolList(temp_byte); var code = new BoolList(); for (int j = 0; j < code_length; j++) { code.Add(boollist_temp[j]); } this.Codes[code] = element; this.InvertedCodes[element] = code; } temp_byte = bytelist.GetRange(index, 4).ToArray(); index += 4; int dataLength = BitConverter.ToInt32(temp_byte, 0); temp_byte = bytelist.GetRange(index, dataLength / 8 + (dataLength % 8 == 0?0:1)).ToArray(); index += dataLength / 8 + (dataLength % 8 == 0?0:1); var data_temp = new BoolList(temp_byte); this.data = new BoolList(); for (int i = 0; i < dataLength; i++) { this.data.Add(data_temp[i]); } }
static void Main() {/* * test(); * var t = BoolList.ToBoolArray(0x12); * for(int i = 0 ; i < 8 ; i++ ) * Console.WriteLine(t[i]); * * * var a = new BoolList(); * var b = new BoolList(); * a.Add(false); * a.Add(false); * a.Add(false); * a.Add(true); * b.Add(false); * b.Add(true); * * Console.WriteLine(a.GetHashCode()); * Console.WriteLine(b.GetHashCode()); * Console.WriteLine(a==b); * Console.WriteLine(a+b); * * var dict = new Dictionary<BoolList,int>(); * * //dict.Add(a,1); * //dict.Add(b,1); * * foreach(var k in dict.Keys) * Console.WriteLine(dict[k]); * * return; */ Console.WriteLine("1. zip"); Console.WriteLine("2. unzip"); var bdata = new BinaryData(); var zip = new ZipData(); var png = new PngData(); if (int.Parse(Console.ReadLine()) == 1) { Console.WriteLine("Input original file path >>"); // 情報源として読み取る bdata.Load(Console.ReadLine()); // ハフマン符号化して読み取る zip.Load(bdata); // PNGデータとして読みとる png.Load(zip); Console.WriteLine("Input output file path >>"); png.Save(Console.ReadLine()); } else { Console.WriteLine("Input PNG file path >>"); // PNGデータとして読みとる png.Load(Console.ReadLine()); // ハフマン符号化して読み取る zip.Load(png); // 情報源として読み取る bdata.Load(zip); bdata.Save(); } }