示例#1
0
        private PngChunk GenerateIhdrChunk(Image.Image image)
        {
            PngChunk chunk = new PngChunk();

            chunk.PngChunkType = PngChunkType.IHDR;
            chunk.ChunkBody    = new byte[13];

            byte[] widthBytes  = BitConverter.GetBytes(image.Width);
            byte[] heightBytes = BitConverter.GetBytes(image.Height);

            chunk.ChunkBody[0] = widthBytes[3];
            chunk.ChunkBody[1] = widthBytes[2];
            chunk.ChunkBody[2] = widthBytes[1];
            chunk.ChunkBody[3] = widthBytes[0];

            chunk.ChunkBody[4] = heightBytes[3];
            chunk.ChunkBody[5] = heightBytes[2];
            chunk.ChunkBody[6] = heightBytes[1];
            chunk.ChunkBody[7] = heightBytes[0];

            chunk.ChunkBody[8]  = 8;
            chunk.ChunkBody[9]  = 2;
            chunk.ChunkBody[10] = 0;
            chunk.ChunkBody[11] = 0;
            chunk.ChunkBody[12] = 0;

            return(chunk);
        }
示例#2
0
        public void WriteImage(Image.Image format, string path = "test.ppm")
        {
            var width  = format.Width;
            var height = format.Height;

            var bytes   = new byte[width * height * 3];
            var bytePos = 0;

            for (int row = 0; row < height; row++)
            {
                for (int col = 0; col < width; col++, bytePos += 3)
                {
                    var _pixel = format.Pixels[row][col];
                    if (_pixel is null)
                    {
                        continue;
                    }
                    bytes[bytePos]     = (byte)_pixel.Red;
                    bytes[bytePos + 1] = (byte)_pixel.Green;
                    bytes[bytePos + 2] = (byte)_pixel.Blue;
                }
            }

            File.WriteAllText(path, $"P6\n{width} {height}\n255\n");
            using var stream = new FileStream(path, FileMode.Append);
            stream.Write(bytes, 0, bytes.Length);
        }
示例#3
0
        private PngChunk GenerateIdatChunk(Image.Image image)
        {
            PngChunk chunk = new PngChunk();

            chunk.PngChunkType = PngChunkType.IDAT;
            chunk.ChunkBody    = Compressing(Filtering(image));
            return(chunk);
        }
示例#4
0
        public void WriteImage(Image.Image image, String path)
        {
            byte[] widthBytes           = BitConverter.GetBytes(image.Width);
            byte[] heightBytes          = BitConverter.GetBytes(image.Height);
            byte[] imageSizeBytes       = BitConverter.GetBytes(image.Width * image.Height * 4);
            byte[] fileSizeBytes        = BitConverter.GetBytes(HeaderSize + HeaderInfoSize + (image.Width * image.Height * 4));
            byte[] pixelDataOffsetBytes = BitConverter.GetBytes(HeaderSize + HeaderInfoSize);

            byte[] imageBytes = new byte[HeaderSize + HeaderInfoSize + (image.Width * image.Height * 4)];

            imageBytes[0]  = 66;
            imageBytes[1]  = 77;
            imageBytes[26] = 1;
            imageBytes[28] = 32;
            for (int i = 0; i < fileSizeBytes.Length; i++)
            {
                imageBytes[i + 2] = fileSizeBytes[i];
            }
            for (int i = 0; i < pixelDataOffsetBytes.Length; i++)
            {
                imageBytes[i + 10] = pixelDataOffsetBytes[i];
            }
            for (int i = 0; i < HeaderSizeBytse.Length; i++)
            {
                imageBytes[i + 14] = HeaderSizeBytse[i];
            }
            for (int i = 0; i < widthBytes.Length; i++)
            {
                imageBytes[i + 18] = widthBytes[i];
            }
            for (int i = 0; i < heightBytes.Length; i++)
            {
                imageBytes[i + 22] = heightBytes[i];
            }
            for (int i = 0; i < imageSizeBytes.Length; i++)
            {
                imageBytes[i + 34] = imageSizeBytes[i];
            }
            for (int i = 0; i < image.Pixels.Length; i++)
            {
                int reversedI = image.Height - 1 - i;
                for (int l = 0; l < image.Pixels[i].Length; l++)
                {
                    imageBytes[i * image.Width * 4 + HeaderSize + HeaderInfoSize + l * 4]     = image.Pixels[reversedI][l].Blue;
                    imageBytes[i * image.Width * 4 + HeaderSize + HeaderInfoSize + l * 4 + 1] = image.Pixels[reversedI][l].Green;
                    imageBytes[i * image.Width * 4 + HeaderSize + HeaderInfoSize + l * 4 + 2] = image.Pixels[reversedI][l].Red;
                    imageBytes[i * image.Width * 4 + HeaderSize + HeaderInfoSize + l * 4 + 3] = 0;
                }
            }
            File.WriteAllBytes(path, imageBytes);
        }
示例#5
0
        private byte[] Filtering(Image.Image image)
        {
            byte[] filteredData = new byte[image.Height * ((image.Width * 3) + 1)];
            int    iteratorI    = 0;

            for (int i = 0; i < filteredData.Length;)
            {
                filteredData[i] = 0;
                i++;
                for (int l = 0; l < image.Width; l++)
                {
                    filteredData[i + l * 3]     = image.Pixels[iteratorI][l].Red;
                    filteredData[i + l * 3 + 1] = image.Pixels[iteratorI][l].Green;
                    filteredData[i + l * 3 + 2] = image.Pixels[iteratorI][l].Blue;
                }
                iteratorI++;
                i += image.Width * 3;
            }
            return(filteredData);
        }
示例#6
0
        public Image.Image ReadImage(String fileName)
        {
            byte[] array = File.ReadAllBytes(fileName);

            List <PngChunk> chunks = ReadChunks(array);
            PngHeader       header = GetPngHeader(chunks);

            // Console.WriteLine(array.Length);
            // Console.WriteLine(header);
            // for (int i = 0; i < array.Length; i++)
            // {
            //     Console.WriteLine("I: " + i + ", B: " + array[i] + ", C: " + (char) array[i]);
            //     // if (i > 50) break;
            // }

            byte[] decompressedData = Decompressing(GetCompressedDataFromAllIdatChunks(chunks));
            byte[] unfilteredData   = Unfiltering(decompressedData, header);

            Image.Image image = new Image.Image(header.Width, header.Height, GetPixelMatrix(unfilteredData, header));
            // image.PrintPixels();
            return(image);
        }
示例#7
0
        public Image.Image ReadImage(String fileName)
        {
            byte[] array = File.ReadAllBytes(fileName);

            byte[] fileSizeBytes = { array[2], array[3], array[4], array[5] };
            int    fileSize      = BitConverter.ToInt32(fileSizeBytes, 0);

            byte[] sizeHeaderBytes = { array[14], array[15], array[16], array[17] };
            int    headerSize      = BitConverter.ToInt32(sizeHeaderBytes, 0);

            byte[] widthBytes = { array[18], array[19], array[20], array[21] };
            int    width      = BitConverter.ToInt32(widthBytes, 0);

            byte[] heightBytes = { array[22], array[23], array[24], array[25] };
            int    height      = BitConverter.ToInt32(heightBytes, 0);

            byte[] imageSizeBytes = { array[34], array[35], array[36], array[37] };
            int    imageSize      = BitConverter.ToInt32(imageSizeBytes, 0);

            Image.Image image = new Image.Image();
            image.Width  = width;
            image.Height = height;
            Pixel[][] pixels = new Pixel[height][];
            for (int i = 0; i < height; i++)
            {
                int rowStart = 14 + headerSize + (image.Height - 1 - i) * width * 4;
                pixels[i] = new Pixel[width];
                for (int l = 0; l < width; l++)
                {
                    pixels[i][l] = new Pixel(array[rowStart + l * 4 + 2],
                                             array[rowStart + l * 4 + 1],
                                             array[rowStart + l * 4]);
                }
            }
            image.Pixels = pixels;
            return(image);
        }
示例#8
0
        public void WriteImage(Image.Image image, String path)
        {
            PngChunk ihdr = GenerateIhdrChunk(image);
            PngChunk idat = GenerateIdatChunk(image);

            byte[] file = new byte[57 + idat.ChunkBody.Length];

            file[0] = 137;
            file[1] = 80;
            file[2] = 78;
            file[3] = 71;
            file[4] = 13;
            file[5] = 10;
            file[6] = 26;
            file[7] = 10;

            file[8]  = 0;
            file[9]  = 0;
            file[10] = 0;
            file[11] = 13;

            file[12] = 73;
            file[13] = 72;
            file[14] = 68;
            file[15] = 82;

            for (int i = 0; i < ihdr.ChunkBody.Length; i++)
            {
                file[16 + i] = ihdr.ChunkBody[i];
            }

            file[29] = 141;
            file[30] = 111;
            file[31] = 38;
            file[32] = 229;

            byte[] idatSizeBytes = BitConverter.GetBytes(idat.ChunkBody.Length);

            file[33] = idatSizeBytes[3];
            file[34] = idatSizeBytes[2];
            file[35] = idatSizeBytes[1];
            file[36] = idatSizeBytes[0];

            file[37] = 73;
            file[38] = 68;
            file[39] = 65;
            file[40] = 84;

            for (int i = 0; i < idat.ChunkBody.Length; i++)
            {
                file[41 + i] = idat.ChunkBody[i];
            }

            int iterator = 41 + idat.ChunkBody.Length;

            file[iterator]     = 45;
            file[iterator + 1] = 27;
            file[iterator + 2] = 23;
            file[iterator + 3] = 249;

            file[iterator + 4] = 0;
            file[iterator + 5] = 0;
            file[iterator + 6] = 0;
            file[iterator + 7] = 0;

            file[iterator + 8]  = 73;
            file[iterator + 9]  = 69;
            file[iterator + 10] = 78;
            file[iterator + 11] = 68;

            file[iterator + 12] = 174;
            file[iterator + 13] = 66;
            file[iterator + 14] = 96;
            file[iterator + 15] = 130;

            File.WriteAllBytes(path, file);
        }