Пример #1
0
 private void status(object sender, SignatureCanvasStatusChangedEventArgs e)
 {
     if (e.Status == SignatureCanvasStatus.Done)
     {
         if (attempt == 0)
         {
             original = canvas.Signature.Value;
         }
         else
         {
             SignatureComparisonResult result = Signature.Compare(original, canvas.Signature.Value);
             string text;
             if (result.IsMatch)
             {
                 text = "Signatures match.";
             }
             else
             {
                 text = "Signatures do not match.";
                 for (int i = 0; i < result.Failures.Count; ++i)
                 {
                     text += string.Format("Error {0}{1}: {2}", i, result.Failures[i].SegmentIndex == null ? "" : " - Part " + result.Failures[i].SegmentIndex, result.Failures[i].Code.ToString());
                 }
             }
             new Message(text).ShowDialog();
             ++attempt;
         }
     }
 }
Пример #2
0
        public PNG(string fileName)
        {
            Signature    sig        = new Signature();
            FileStream   fileStream = File.OpenRead(fileName);
            BinaryReader reader     = new BinaryReader(fileStream);

            byte[] signature = reader.ReadBytes(8);
            Console.WriteLine(sig.Compare(signature) + " - " + sig.ToString());

            DataChunks = new List <IDataChunk>();

            List <byte[]>    allDataBytes = new List <byte[]>();
            List <DataChunk> allChunks    = new List <DataChunk>();

            while ((reader.BaseStream.Position < reader.BaseStream.Length))
            {
                DataChunk chunk    = new DataChunk(reader, this);
                ulong     chunkLen = CalculateCRCPolynomial((uint)chunk.Data.Length + 4);

                if (chunk.DataChunkRepresentation is DataChunk.ImageDataChunkRepresentation)
                {
                    allDataBytes.Add(chunk.Data);
                }
                else
                {
                    Console.WriteLine($"[{chunk.Length}],[{chunk.ChunkType}],[{chunk.CRC}],[{chunk.Data.Length}]");
                    if (!string.IsNullOrEmpty(chunk.PropertyString))
                    {
                        Console.WriteLine(chunk.PropertyString);
                    }
                    DataChunks.Add(chunk);
                    allChunks.Add(chunk);
                }
            }
            reader.Close();
            fileStream.Close();
            int allBytesCount = 0;

            for (int z = 0; z < allDataBytes.Count; z++)
            {
                allBytesCount += allDataBytes[z].Length;
            }
            byte[] dataBytes = new byte[allBytesCount];
            allBytesCount = 0;
            for (int z = 0; z < allDataBytes.Count; z++)
            {
                allDataBytes[z].CopyTo(dataBytes, allBytesCount);
                allBytesCount += allDataBytes[z].Length;
            }

            MemoryStream output       = new MemoryStream();
            MemoryStream memoryStream = new MemoryStream(dataBytes);

            memoryStream.Position = 0;
            byte[] zlibHeaderBytes = new byte[2];
            memoryStream.Read(zlibHeaderBytes, 0, 2);
            DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress);

            deflateStream.CopyTo(output);
            deflateStream.Flush();
            output.Position = 0;
            byte[] outputBytes = new byte[output.Length];
            int    arrLength   = (int)output.Length;

            output.Read(outputBytes, 0, arrLength);

            MemoryStream streamReader = new MemoryStream(outputBytes);
            DataChunk    header       = allChunks.Where(x => x.ChunkType.ToLower() == "ihdr").FirstOrDefault();
            Dictionary <string, DataChunk.DataObject> headerProperties = header.DataChunkRepresentation.ToProperties(header.Data);
            uint width     = (uint)headerProperties["Width"].Value;
            uint height    = (uint)headerProperties["Height"].Value;
            byte colorType = (byte)headerProperties["ColorType"].Value;
            byte bitDepth  = (byte)headerProperties["BitDepth"].Value;

            streamReader.Position = 0;

            if (bitDepth <= 8)
            {
                RGBImage <byte> image = new RGBImage <byte>(width, height);
                for (uint y = 0; y < height; y++)
                {
                    byte filterByte = (byte)streamReader.ReadByte();
                    if (filterByte == 0)
                    {
                        if (colorType == 2)
                        {
                            for (uint x = 0; x < width; x++)
                            {
                                byte             r     = (byte)streamReader.ReadByte();
                                byte             g     = (byte)streamReader.ReadByte();
                                byte             b     = (byte)streamReader.ReadByte();
                                RGBAColor <byte> color = new RGBAColor <byte>(r, g, b, 255);
                                image.Colors[x, y] = color;
                            }
                        }
                        else if (colorType == 6)
                        {
                            for (uint x = 0; x < width; x++)
                            {
                                byte             r     = (byte)streamReader.ReadByte();
                                byte             g     = (byte)streamReader.ReadByte();
                                byte             b     = (byte)streamReader.ReadByte();
                                byte             a     = (byte)streamReader.ReadByte();
                                RGBAColor <byte> color = new RGBAColor <byte>(r, g, b, a);
                                image.Colors[x, y] = color;
                            }
                        }
                    }
                }
                deflateStream.Close();
                memoryStream.Close();
                output.Close();
                streamReader.Close();
                Image = image;
            }
            else if (bitDepth == 16)
            {
                RGBImage <uint> image = new RGBImage <uint>(width, height);
                for (uint y = 0; y < height; y++)
                {
                    byte filterByte = (byte)streamReader.ReadByte();
                    if (filterByte == 0)
                    {
                        if (colorType == 2)
                        {
                            for (uint x = 0; x < width; x++)
                            {
                                uint             r     = (uint)streamReader.ReadByte() << 8 | (uint)streamReader.ReadByte();
                                uint             g     = (uint)streamReader.ReadByte() << 8 | (uint)streamReader.ReadByte();
                                uint             b     = (uint)streamReader.ReadByte() << 8 | (uint)streamReader.ReadByte();
                                RGBAColor <uint> color = new RGBAColor <uint>(r, g, b, 255);
                                image.Colors[x, y] = color;
                            }
                        }
                        else if (colorType == 6)
                        {
                            for (uint x = 0; x < width; x++)
                            {
                                uint             r     = (uint)streamReader.ReadByte() << 8 | (uint)streamReader.ReadByte();
                                uint             g     = (uint)streamReader.ReadByte() << 8 | (uint)streamReader.ReadByte();
                                uint             b     = (uint)streamReader.ReadByte() << 8 | (uint)streamReader.ReadByte();
                                uint             a     = (uint)streamReader.ReadByte() << 8 | (uint)streamReader.ReadByte();
                                RGBAColor <uint> color = new RGBAColor <uint>(r, g, b, a);
                                image.Colors[x, y] = color;
                            }
                        }
                    }
                }
                deflateStream.Close();
                memoryStream.Close();
                output.Close();
                streamReader.Close();
                Image16Bits = image;
            }
        }