Пример #1
0
        public override ChunkRaw CreateRawChunk()
        {
            MemoryStream ba = new MemoryStream();

            ChunkHelper.WriteBytesToStream(ba, ChunkHelper.ToBytes(PalName));
            ba.WriteByte(0); // separator
            ba.WriteByte((byte)SampleDepth);
            int nentries = GetNentries();

            for (int n = 0; n < nentries; n++)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (SampleDepth == 8)
                    {
                        PngHelperInternal.WriteByte(ba, (byte)Palette[n * 5 + i]);
                    }
                    else
                    {
                        PngHelperInternal.WriteInt2(ba, Palette[n * 5 + i]);
                    }
                }
                PngHelperInternal.WriteInt2(ba, Palette[n * 5 + 4]);
            }
            byte[]   b     = ba.ToArray();
            ChunkRaw chunk = createEmptyChunk(b.Length, false);

            chunk.Data = b;
            return(chunk);
        }
Пример #2
0
        public override ChunkRaw CreateRawChunk()
        {
            ChunkRaw c;

            if (ImgInfo.Greyscale)
            {
                c = CreateEmptyChunk(2, true);
                PngHelperInternal.WriteInt2tobytes(gray, c.Data, 0);
            }
            else if (ImgInfo.Indexed)
            {
                c = CreateEmptyChunk(paletteAlpha.Length, true);
                for (var n = 0; n < c.Len; n++)
                {
                    c.Data[n] = (byte)paletteAlpha[n];
                }
            }
            else
            {
                c = CreateEmptyChunk(6, true);
                PngHelperInternal.WriteInt2tobytes(red, c.Data, 0);
                PngHelperInternal.WriteInt2tobytes(green, c.Data, 0);
                PngHelperInternal.WriteInt2tobytes(blue, c.Data, 0);
            }

            return(c);
        }
Пример #3
0
        public override ChunkRaw CreateRawChunk()
        {
            MemoryStream memoryStream = new MemoryStream();

            ChunkHelper.WriteBytesToStream(memoryStream, ChunkHelper.ToBytes(PalName));
            memoryStream.WriteByte(0);
            memoryStream.WriteByte((byte)SampleDepth);
            int nentries = GetNentries();

            for (int i = 0; i < nentries; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (SampleDepth == 8)
                    {
                        PngHelperInternal.WriteByte(memoryStream, (byte)Palette[i * 5 + j]);
                    }
                    else
                    {
                        PngHelperInternal.WriteInt2(memoryStream, Palette[i * 5 + j]);
                    }
                }
                PngHelperInternal.WriteInt2(memoryStream, Palette[i * 5 + 4]);
            }
            byte[]   array    = memoryStream.ToArray();
            ChunkRaw chunkRaw = createEmptyChunk(array.Length, alloc: false);

            chunkRaw.Data = array;
            return(chunkRaw);
        }
Пример #4
0
        public override ChunkRaw CreateRawChunk()
        {
            ChunkRaw chunkRaw = null;

            if (ImgInfo.Greyscale)
            {
                chunkRaw = createEmptyChunk(2, alloc: true);
                PngHelperInternal.WriteInt2tobytes(gray, chunkRaw.Data, 0);
            }
            else if (ImgInfo.Indexed)
            {
                chunkRaw = createEmptyChunk(paletteAlpha.Length, alloc: true);
                for (int i = 0; i < chunkRaw.Length; i++)
                {
                    chunkRaw.Data[i] = (byte)paletteAlpha[i];
                }
            }
            else
            {
                chunkRaw = createEmptyChunk(6, alloc: true);
                PngHelperInternal.WriteInt2tobytes(red, chunkRaw.Data, 0);
                PngHelperInternal.WriteInt2tobytes(green, chunkRaw.Data, 0);
                PngHelperInternal.WriteInt2tobytes(blue, chunkRaw.Data, 0);
            }
            return(chunkRaw);
        }
Пример #5
0
 public override void ParseFromRaw(ChunkRaw c)
 {
     if (c.Len != GetLen())
     {
         throw new PngjException("bad chunk length " + c);
     }
     if (ImgInfo.Greyscale)
     {
         Graysb = PngHelperInternal.ReadInt1fromByte(c.Data, 0);
         if (ImgInfo.Alpha)
         {
             Alphasb = PngHelperInternal.ReadInt1fromByte(c.Data, 1);
         }
     }
     else
     {
         Redsb   = PngHelperInternal.ReadInt1fromByte(c.Data, 0);
         Greensb = PngHelperInternal.ReadInt1fromByte(c.Data, 1);
         Bluesb  = PngHelperInternal.ReadInt1fromByte(c.Data, 2);
         if (ImgInfo.Alpha)
         {
             Alphasb = PngHelperInternal.ReadInt1fromByte(c.Data, 3);
         }
     }
 }
Пример #6
0
        public override void ParseFromRaw(ChunkRaw c)
        {
            if (c is null)
            {
                throw new ArgumentNullException(nameof(c));
            }

            if (c.Len != 7)
            {
                throw new PngjException("bad chunk " + c);
            }

            var year = PngHelperInternal.ReadInt2fromBytes(c.Data, 0);
            var mon  = PngHelperInternal.ReadInt1fromByte(c.Data, 2);
            var day  = PngHelperInternal.ReadInt1fromByte(c.Data, 3);
            var hour = PngHelperInternal.ReadInt1fromByte(c.Data, 4);
            var min  = PngHelperInternal.ReadInt1fromByte(c.Data, 5);
            var sec  = PngHelperInternal.ReadInt1fromByte(c.Data, 6);

            try
            {
                timestamp = new DateTime(year, mon, day, hour, min, sec);
            }
            catch
            {
                timestamp = DateTime.MinValue;
            }
        }
Пример #7
0
        public override ChunkRaw CreateRawChunk()
        {
            ChunkRaw chunkRaw = createEmptyChunk(4, alloc: true);

            PngHelperInternal.WriteInt4tobytes((int)(gamma * 100000.0 + 0.5), chunkRaw.Data, 0);
            return(chunkRaw);
        }
Пример #8
0
        public static void testEqual(String image1, String image2)
        {
            PngReader png1 = FileHelper.CreatePngReader(image1);

            PngHelperInternal.InitCrcForTests(png1);
            PngReader png2 = FileHelper.CreatePngReader(image2);

            PngHelperInternal.InitCrcForTests(png2);
            if (png1.IsInterlaced() != png2.IsInterlaced())
            {
                fatalError("Cannot compare, one is interlaced, the other not:" + png1 + " " + png2,
                           png1, png2);
            }
            if (!png1.ImgInfo.Equals(png2.ImgInfo))
            {
                fatalError("Image are of different type", png1, png2);
            }
            png1.ReadRow(png1.ImgInfo.Rows - 1);
            png2.ReadRow(png2.ImgInfo.Rows - 1);
            png1.End();
            png2.End();
            long crc1 = PngHelperInternal.GetCrctestVal(png1);
            long crc2 = PngHelperInternal.GetCrctestVal(png2);

            if (crc1 != crc2)
            {
                fatalError("different crcs " + image1 + "=" + crc1 + " " + image2 + "=" + crc2,
                           png1, png2);
            }
        }
Пример #9
0
        public override void ParseFromRaw(ChunkRaw c)
        {
            if (c is null)
            {
                throw new System.ArgumentNullException(nameof(c));
            }

            if (ImgInfo.Greyscale)
            {
                gray = PngHelperInternal.ReadInt2fromBytes(c.Data, 0);
            }
            else if (ImgInfo.Indexed)
            {
                var nentries = c.Data.Length;
                paletteAlpha = new int[nentries];
                for (var n = 0; n < nentries; n++)
                {
                    paletteAlpha[n] = c.Data[n] & 0xff;
                }
            }
            else
            {
                red   = PngHelperInternal.ReadInt2fromBytes(c.Data, 0);
                green = PngHelperInternal.ReadInt2fromBytes(c.Data, 2);
                blue  = PngHelperInternal.ReadInt2fromBytes(c.Data, 4);
            }
        }
Пример #10
0
 public override void ParseFromRaw(ChunkRaw chunk)
 {
     byte[] data = chunk.Data;
     if (chunk.Len != GetLen())
     {
         throw new System.Exception($"bad chunk length {chunk}");
     }
     if (ImgInfo.Greyscale)
     {
         Graysb = PngHelperInternal.ReadInt1fromByte(data, 0);
         if (ImgInfo.Alpha)
         {
             Alphasb = PngHelperInternal.ReadInt1fromByte(data, 1);
         }
     }
     else
     {
         Redsb   = PngHelperInternal.ReadInt1fromByte(data, 0);
         Greensb = PngHelperInternal.ReadInt1fromByte(data, 1);
         Bluesb  = PngHelperInternal.ReadInt1fromByte(data, 2);
         if (ImgInfo.Alpha)
         {
             Alphasb = PngHelperInternal.ReadInt1fromByte(data, 3);
         }
     }
 }
Пример #11
0
        public override void ParseFromRaw(ChunkRaw c)
        {
            if (c is null)
            {
                throw new System.ArgumentNullException(nameof(c));
            }

            if (c.Len != 9)
            {
                throw new PngjException("bad chunk length " + c);
            }

            posX = PngHelperInternal.ReadInt4fromBytes(c.Data, 0);
            if (posX < 0)
            {
                posX += 0x100000000L;
            }

            posY = PngHelperInternal.ReadInt4fromBytes(c.Data, 4);
            if (posY < 0)
            {
                posY += 0x100000000L;
            }

            units = PngHelperInternal.ReadInt1fromByte(c.Data, 8);
        }
 public override void ParseFromRaw(ChunkRaw chunk)
 {
     byte[] data = chunk.Data;
     if (ImgInfo.Greyscale)
     {
         Gray = PngHelperInternal.ReadInt2fromBytes(data, 0);
     }
     else if (ImgInfo.Indexed)
     {
         int numEntries = data.Length;
         PaletteAlpha = new int[numEntries];
         for (int n = 0; n < numEntries; n++)
         {
             PaletteAlpha[n] = (int)(data[n] & 0xff);
         }
     }
     else
     {
         Rgb = new RGB <int> {
             R = PngHelperInternal.ReadInt2fromBytes(data, 0),
             G = PngHelperInternal.ReadInt2fromBytes(data, 2),
             B = PngHelperInternal.ReadInt2fromBytes(data, 4)
         };
     }
 }
        public override ChunkRaw CreateRawChunk()
        {
            ChunkRaw chunk = null;

            if (ImgInfo.Greyscale)
            {
                chunk = CreateEmptyChunk(2, true);
                byte[] data = chunk.Data;
                PngHelperInternal.WriteInt2tobytes(Gray, data, 0);
            }
            else if (ImgInfo.Indexed)
            {
                chunk = CreateEmptyChunk(PaletteAlpha.Length, true);
                byte[] data   = chunk.Data;
                int    length = data.Length;

                for (int n = 0; n < length; n++)
                {
                    data[n] = (byte)PaletteAlpha[n];
                }
            }
            else
            {
                chunk = CreateEmptyChunk(6, true);
                byte[] data = chunk.Data;

                PngHelperInternal.WriteInt2tobytes(Rgb.R, data, 0);
                PngHelperInternal.WriteInt2tobytes(Rgb.G, data, 0);
                PngHelperInternal.WriteInt2tobytes(Rgb.B, data, 0);
            }
            return(chunk);
        }
Пример #14
0
 public override void ParseFromRaw(ChunkRaw c)
 {
     if (c.Length != 1)
     {
         throw new PngjException("bad chunk length " + c?.ToString());
     }
     Intent = PngHelperInternal.ReadInt1fromByte(c.Data, 0);
 }
        public override ChunkRaw CreateRawChunk()
        {
            ChunkRaw chunk = CreateEmptyChunk(4, true);
            int      g     = (int)(gamma * 100000 + 0.5d);

            PngHelperInternal.WriteInt4tobytes(g, chunk.Data, 0);
            return(chunk);
        }
 public override void ParseFromRaw(ChunkRaw c)
 {
     if (c.Len != 1)
     {
         throw new System.Exception($"bad chunk length {c}");
     }
     Intent = PngHelperInternal.ReadInt1fromByte(c.Data, 0);
 }
Пример #17
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);
        }
Пример #18
0
        public override ChunkRaw CreateRawChunk()
        {
            ChunkRaw c = createEmptyChunk(9, true);

            PngHelperInternal.WriteInt4tobytes((int)posX, c.Data, 0);
            PngHelperInternal.WriteInt4tobytes((int)posY, c.Data, 4);
            c.Data[8] = (byte)units;
            return(c);
        }
Пример #19
0
        public override void ParseFromRaw(ChunkRaw c)
        {
            int num = -1;

            for (int i = 0; i < c.Data.Length; i++)
            {
                if (c.Data[i] == 0)
                {
                    num = i;
                    break;
                }
            }
            if (num <= 0 || num > c.Data.Length - 2)
            {
                throw new PngjException("bad sPLT chunk: no separator found");
            }
            PalName     = ChunkHelper.ToString(c.Data, 0, num);
            SampleDepth = PngHelperInternal.ReadInt1fromByte(c.Data, num + 1);
            num        += 2;
            int num2 = (c.Data.Length - num) / ((SampleDepth == 8) ? 6 : 10);

            Palette = new int[num2 * 5];
            int num3 = 0;

            for (int j = 0; j < num2; j++)
            {
                int num4;
                int num5;
                int num6;
                int num7;
                if (SampleDepth == 8)
                {
                    num4 = PngHelperInternal.ReadInt1fromByte(c.Data, num++);
                    num5 = PngHelperInternal.ReadInt1fromByte(c.Data, num++);
                    num6 = PngHelperInternal.ReadInt1fromByte(c.Data, num++);
                    num7 = PngHelperInternal.ReadInt1fromByte(c.Data, num++);
                }
                else
                {
                    num4 = PngHelperInternal.ReadInt2fromBytes(c.Data, num);
                    num += 2;
                    num5 = PngHelperInternal.ReadInt2fromBytes(c.Data, num);
                    num += 2;
                    num6 = PngHelperInternal.ReadInt2fromBytes(c.Data, num);
                    num += 2;
                    num7 = PngHelperInternal.ReadInt2fromBytes(c.Data, num);
                    num += 2;
                }
                int num8 = PngHelperInternal.ReadInt2fromBytes(c.Data, num);
                num            += 2;
                Palette[num3++] = num4;
                Palette[num3++] = num5;
                Palette[num3++] = num6;
                Palette[num3++] = num7;
                Palette[num3++] = num8;
            }
        }
Пример #20
0
        public override ChunkRaw CreateRawChunk()
        {
            ChunkRaw chunkRaw = createEmptyChunk(9, alloc: true);

            PngHelperInternal.WriteInt4tobytes((int)PixelsxUnitX, chunkRaw.Data, 0);
            PngHelperInternal.WriteInt4tobytes((int)PixelsxUnitY, chunkRaw.Data, 4);
            chunkRaw.Data[8] = (byte)Units;
            return(chunkRaw);
        }
Пример #21
0
        public override void ParseFromRaw(ChunkRaw chunk)
        {
            byte[] data   = chunk.Data;
            int    length = data.Length;
            int    t      = -1;

            for (int i = 0; i < length; i++)           // look for first zero
            {
                if (data[i] == 0)
                {
                    t = i;
                    break;
                }
            }
            if (t <= 0 || t > length - 2)
            {
                throw new System.Exception("bad sPLT chunk: no separator found");
            }
            PalName     = ChunkHelper.ToString(data, 0, t);
            SampleDepth = PngHelperInternal.ReadInt1fromByte(data, t + 1);
            t          += 2;
            int nentries = (length - t) / (SampleDepth == 8 ? 6 : 10);

            Palette = new int[nentries * 5];
            int r, g, b, a, f, ne;

            ne = 0;
            for (int i = 0; i < nentries; i++)
            {
                if (SampleDepth == 8)
                {
                    r = PngHelperInternal.ReadInt1fromByte(data, t++);
                    g = PngHelperInternal.ReadInt1fromByte(data, t++);
                    b = PngHelperInternal.ReadInt1fromByte(data, t++);
                    a = PngHelperInternal.ReadInt1fromByte(data, t++);
                }
                else
                {
                    r  = PngHelperInternal.ReadInt2fromBytes(data, t);
                    t += 2;
                    g  = PngHelperInternal.ReadInt2fromBytes(data, t);
                    t += 2;
                    b  = PngHelperInternal.ReadInt2fromBytes(data, t);
                    t += 2;
                    a  = PngHelperInternal.ReadInt2fromBytes(data, t);
                    t += 2;
                }
                f             = PngHelperInternal.ReadInt2fromBytes(data, t);
                t            += 2;
                Palette[ne++] = r;
                Palette[ne++] = g;
                Palette[ne++] = b;
                Palette[ne++] = a;
                Palette[ne++] = f;
            }
        }
        public override void ParseFromRaw(ChunkRaw chunk)
        {
            if (chunk.Len != 4)
            {
                throw new System.Exception($"bad chunk {chunk}");
            }
            int g = PngHelperInternal.ReadInt4fromBytes(chunk.Data, 0);

            gamma = ((double)g) / 100000.0d;
        }
Пример #23
0
        public override ChunkRaw CreateRawChunk()
        {
            ChunkRaw chunk = CreateEmptyChunk(9, true);

            byte[] data = chunk.Data;
            PngHelperInternal.WriteInt4tobytes((int)posX, data, 0);
            PngHelperInternal.WriteInt4tobytes((int)posY, data, 4);
            data[8] = (byte)units;
            return(chunk);
        }
Пример #24
0
        public override void ParseFromRaw(ChunkRaw chunk)
        {
            if (chunk.Length != 4)
            {
                throw new PngjException("bad chunk " + chunk?.ToString());
            }
            int num = PngHelperInternal.ReadInt4fromBytes(chunk.Data, 0);

            gamma = (double)num / 100000.0;
        }
Пример #25
0
 /// <summary> Called after setting data, before writing to os </summary>
 int ComputeCrc()
 {
     Zlib.CRC32 crcengine = PngHelperInternal.GetCRC();
     crcengine.Reset();
     crcengine.Update(IdBytes, 0, 4);
     if (Len > 0)
     {
         crcengine.Update(Data, 0, Len);
     }
     return((int)crcengine.GetValue());
 }
Пример #26
0
        public override ChunkRaw CreateRawChunk()
        {
            ChunkRaw chunkRaw = createEmptyChunk(7, alloc: true);

            PngHelperInternal.WriteInt2tobytes(year, chunkRaw.Data, 0);
            chunkRaw.Data[2] = (byte)mon;
            chunkRaw.Data[3] = (byte)day;
            chunkRaw.Data[4] = (byte)hour;
            chunkRaw.Data[5] = (byte)min;
            chunkRaw.Data[6] = (byte)sec;
            return(chunkRaw);
        }
Пример #27
0
        public int ComputeCrc()
        {
            CRC32 cRC = PngHelperInternal.GetCRC();

            cRC.Reset();
            cRC.Update(IdBytes, 0, 4);
            if (Length > 0)
            {
                cRC.Update(Data, 0, Length);
            }
            return((int)cRC.GetValue());
        }
Пример #28
0
        public override ChunkRaw CreateRawChunk()
        {
            var c = CreateEmptyChunk(7, true);

            PngHelperInternal.WriteInt2tobytes(timestamp.Year, c.Data, 0);
            c.Data[2] = (byte)timestamp.Month;
            c.Data[3] = (byte)timestamp.Day;
            c.Data[4] = (byte)timestamp.Hour;
            c.Data[5] = (byte)timestamp.Minute;
            c.Data[6] = (byte)timestamp.Second;
            return(c);
        }
        public override ChunkRaw CreateRawChunk()
        {
            ChunkRaw c = CreateEmptyChunk(7, true);

            byte[] data = c.Data;
            PngHelperInternal.WriteInt2tobytes(year, data, 0);
            data[2] = (byte)mon;
            data[3] = (byte)day;
            data[4] = (byte)hour;
            data[5] = (byte)min;
            data[6] = (byte)sec;
            return(c);
        }
Пример #30
0
 public override void ParseFromRaw(ChunkRaw chunk)
 {
     if (chunk.Length != 7)
     {
         throw new PngjException("bad chunk " + chunk?.ToString());
     }
     year = PngHelperInternal.ReadInt2fromBytes(chunk.Data, 0);
     mon  = PngHelperInternal.ReadInt1fromByte(chunk.Data, 2);
     day  = PngHelperInternal.ReadInt1fromByte(chunk.Data, 3);
     hour = PngHelperInternal.ReadInt1fromByte(chunk.Data, 4);
     min  = PngHelperInternal.ReadInt1fromByte(chunk.Data, 5);
     sec  = PngHelperInternal.ReadInt1fromByte(chunk.Data, 6);
 }