Пример #1
0
 static public CodeDecoder           Decode(byte[] abImage)
 {
    PngReader xPng=new PngReader( new MemoryStream( abImage ) );
    //
    ImageLines xLines=xPng.ReadRowsByte();
    //
    byte[,] abData=new byte[xLines.ImgInfo.Cols, xLines.ImgInfo.Rows];
    //
    int iBits = xLines.ImgInfo.BitspPixel;
    int iChannels = xLines.ImgInfo.Channels;
    //
    if ( iBits==1 )
    {
       for (int y = 0; y < xLines.ImgInfo.Rows; y++)
          for (int x = 0; x < xLines.ImgInfo.Cols/8; x++)
          {
             byte b= xLines.ScanlinesB[y][x];
             //
             for (int a=0; a<8; a++)
                abData[x*8+a, y] = (byte)( ( (b & (1<<(7-a))) !=0 ) ? 255 : 0 );
          }
    }
    else if( iBits==8 )
    {
       int iC=0;
       for (int y = 0; y < xLines.ImgInfo.Rows; y++)
          for (int x = 0; x < xLines.ImgInfo.Cols; x++, iC+=iChannels)
             abData[x,y]=xLines.ScanlinesB[y][iC];
    }
    //
    return Decode( abData , xLines.ImgInfo.Cols , xLines.ImgInfo.Rows);
 }
Пример #2
0
        static void testmirror(string orig, string origni, string truecolor)
        {
            string mirror = TestsHelper.addSuffixToName(orig, "_mirror");
            string recov  = TestsHelper.addSuffixToName(orig, "_recov");
            long   crc0   = 0;
            bool   interlaced;
            bool   palete;

            {
                PngReader pngr = FileHelper.CreatePngReader(orig);
                palete = pngr.ImgInfo.Indexed;
                PngHelperInternal.InitCrcForTests(pngr);
                pngr.SetUnpackedMode(true);
                interlaced = pngr.IsInterlaced();
                PngWriter pngw = FileHelper.CreatePngWriter(mirror, pngr.ImgInfo, true);
                pngw.SetFilterType(FilterType.FILTER_CYCLIC); // just to test all filters
                pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL);
                pngw.SetUseUnPackedMode(true);
                for (int row = 0; row < pngr.ImgInfo.Rows; row++)
                {
                    ImageLine line = pngr.ReadRowInt(row);
                    mirrorLine(line);
                    pngw.WriteRow(line, row);
                }
                pngr.End();
                crc0 = PngHelperInternal.GetCrctestVal(pngr);
                pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL);
                pngw.End();
            }
            // mirror again, now with BYTE (if depth<16) and loading all rows
            {
                PngReader pngr2 = FileHelper.CreatePngReader(mirror);
                pngr2.SetUnpackedMode(true);
                PngWriter pngw = FileHelper.CreatePngWriter(recov, pngr2.ImgInfo, true);
                pngw.SetFilterType(FilterType.FILTER_AGGRESSIVE);
                pngw.CopyChunksFirst(pngr2, ChunkCopyBehaviour.COPY_ALL);
                pngw.SetUseUnPackedMode(true);
                ImageLines lines = pngr2.ImgInfo.BitDepth < 16 ? pngr2.ReadRowsByte() : pngr2
                                   .ReadRowsInt();
                for (int row = 0; row < pngr2.ImgInfo.Rows; row++)
                {
                    ImageLine line = lines.GetImageLineAtMatrixRow(row);
                    mirrorLine(line);
                    pngw.WriteRow(line, row);
                }
                pngr2.End();
                pngw.End();
            }
            // now check
            if (orig[11] != 'i')
            {
                TestsHelper.testCrcEquals(recov, crc0);
            }
            //if (interlaced)
            //    additionalTestInterlaced(orig, origni);
            //if (palete && System.IO.File.Exists(truecolor))
            //    additionalTestPalette(orig, truecolor);
        }
Пример #3
0
        /// <summary>
        /// Decodes a raw file buffer of PNG data into raw image buffer, with width and height saved.
        /// </summary>
        /// <param name="stream">Png bytes.</param>
        public ImageLines Deserialize(Stream stream)
        {
            if (stream is null)
            {
                throw new System.ArgumentNullException(nameof(stream));
            }

            using var png = new PngReader(stream);
            png.SetUnpackedMode(true);
            var image = png.ReadRowsByte();

            png.End();
            return(image);
        }
Пример #4
0
        private static void PngWriter(MemoryStream memoryStream, FileStream fileStream, bool modelpng = false)
        {
            PngReader          pngReader    = new PngReader(fileStream);
            EngineBinaryWriter binaryWriter = new EngineBinaryWriter(memoryStream, false);

            binaryWriter.Write(false);
            pngReader.ShouldCloseStream  = false;
            pngReader.ChunkLoadBehaviour = ChunkLoadBehaviour.LOAD_CHUNK_NEVER;
            pngReader.MaxTotalBytesRead  = 9223372036854775807L;
            ImageLines imageLines = pngReader.ReadRowsByte();

            pngReader.End();
            Image image = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows);
            int   n     = 0;

            for (int i = 0; i < image.Height; i++)
            {
                byte[] array = imageLines.ScanlinesB[i];
                int    num   = 0;
                for (int j = 0; j < image.Width; j++)
                {
                    byte r = array[num++];
                    byte g = array[num++];
                    byte b = array[num++];
                    byte a = array[num++];
                    image.Pixels[n++] = new Color(r, g, b, a);
                }
            }
            List <Image> list = new List <Image>();

            if (IsPowerOf2(image.Width) && IsPowerOf2(image.Height))
            {
                list.AddRange(Image.GenerateMipmaps(image, 2147483647));
            }
            else
            {
                list.Add(image);
                modelpng = true;
            }
            if (modelpng)
            {
                goto IL_00;
            }
            binaryWriter.Write(image.Width);
            binaryWriter.Write(image.Height);
            binaryWriter.Write(1);
            for (int i = 0; i < image.Height; i++)
            {
                byte[] array = imageLines.ScanlinesB[i];
                int    num   = 0;
                for (int j = 0; j < image.Width; j++)
                {
                    byte r = array[num++];
                    byte g = array[num++];
                    byte b = array[num++];
                    byte a = array[num++];
                    binaryWriter.Write(new Color(r, g, b, a).PackedValue);
                }
            }
            return;

IL_00:
            binaryWriter.Write(list[0].Width);
            binaryWriter.Write(list[0].Height);
            binaryWriter.Write(list.Count);
            foreach (Image current in list)
            {
                for (int j = 0; j < current.Pixels.Length; j++)
                {
                    binaryWriter.Write(current.Pixels[j].PackedValue);
                }
            }
        }
Пример #5
0
        public static Image Load(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            PngReader pngReader = new PngReader(stream);

            pngReader.ShouldCloseStream  = false;
            pngReader.ChunkLoadBehaviour = ChunkLoadBehaviour.LOAD_CHUNK_NEVER;
            pngReader.MaxTotalBytesRead  = long.MaxValue;
            ImageLines imageLines = pngReader.ReadRowsByte();

            pngReader.End();
            if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 4)
            {
                Image image = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows);
                int   i     = 0;
                int   num   = 0;
                for (; i < image.Height; i++)
                {
                    byte[] array = imageLines.ScanlinesB[i];
                    int    j     = 0;
                    int    num2  = 0;
                    for (; j < image.Width; j++)
                    {
                        byte r = array[num2++];
                        byte g = array[num2++];
                        byte b = array[num2++];
                        byte a = array[num2++];
                        image.Pixels[num++] = new Color(r, g, b, a);
                    }
                }
                return(image);
            }
            if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 3)
            {
                Image image2 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows);
                int   k      = 0;
                int   num3   = 0;
                for (; k < image2.Height; k++)
                {
                    byte[] array2 = imageLines.ScanlinesB[k];
                    int    l      = 0;
                    int    num4   = 0;
                    for (; l < image2.Width; l++)
                    {
                        byte r2 = array2[num4++];
                        byte g2 = array2[num4++];
                        byte b2 = array2[num4++];
                        image2.Pixels[num3++] = new Color(r2, g2, b2);
                    }
                }
                return(image2);
            }
            if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 2 && imageLines.ImgInfo.Greyscale)
            {
                Image image3 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows);
                int   m      = 0;
                int   num5   = 0;
                for (; m < image3.Height; m++)
                {
                    byte[] array3 = imageLines.ScanlinesB[m];
                    int    n      = 0;
                    int    num6   = 0;
                    for (; n < image3.Width; n++)
                    {
                        byte b3 = array3[num6++];
                        byte a2 = array3[num6++];
                        image3.Pixels[num5++] = new Color(b3, b3, b3, a2);
                    }
                }
                return(image3);
            }
            if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 1 && imageLines.ImgInfo.Greyscale)
            {
                Image image4 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows);
                int   num7   = 0;
                int   num8   = 0;
                for (; num7 < image4.Height; num7++)
                {
                    byte[] array4 = imageLines.ScanlinesB[num7];
                    int    num9   = 0;
                    int    num10  = 0;
                    for (; num9 < image4.Width; num9++)
                    {
                        byte b4 = array4[num10++];
                        image4.Pixels[num8++] = new Color(b4, b4, b4);
                    }
                }
                return(image4);
            }
            if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 1 && imageLines.ImgInfo.Indexed)
            {
                PngChunkPLTE pngChunkPLTE = (PngChunkPLTE)pngReader.GetChunksList().GetById1("PLTE");
                if (pngChunkPLTE == null)
                {
                    throw new InvalidOperationException("PLTE chunk not found in indexed PNG.");
                }
                Image image5 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows);
                int   num11  = 0;
                int   num12  = 0;
                for (; num11 < image5.Height; num11++)
                {
                    byte[] array5 = imageLines.ScanlinesB[num11];
                    int    num13  = 0;
                    int    num14  = 0;
                    for (; num13 < image5.Width; num13++)
                    {
                        byte n2    = array5[num14++];
                        int  entry = pngChunkPLTE.GetEntry(n2);
                        image5.Pixels[num12++] = new Color((entry >> 16) & 0xFF, (entry >> 8) & 0xFF, entry & 0xFF);
                    }
                }
                return(image5);
            }
            throw new InvalidOperationException("Unsupported PNG pixel format.");
        }