Example #1
0
 static void Main()
 {
     using (OpenFileDialog ofd = new OpenFileDialog())
     {
         if (ofd.ShowDialog() == DialogResult.OK)
         {
             try
             {
                 APNGLib.APNG png = new APNGLib.APNG();
                 using (Stream s = File.OpenRead(ofd.FileName))
                 {
                     png.Load(s);
                 }
                 using (Program prog = new Program(png))
                 {
                     prog.Run();
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine("Error: " + ex.Message);
             }
         }
     }
 }
Example #2
0
        //public static APNG CreateEmpty() {
        //	return new APNG();
        //}

        //public static APNG Create(Stream s) {
        //	APNG apng = new APNG();
        //	apng.Load(s);
        //	return apng;
        //}

        public static void Add(this APNG apngBase, PNG pngFrame, int fps)
        {
            bool isFirst = apngBase.acTL == null;

            if (isFirst)
            {
                SetupAPNGChunks(apngBase, pngFrame);
            }

            if (pngFrame.IDATList.Count < 0)
            {
                return;
            }

            uint sequenceCount = 0;

            if (apngBase.FrameCount == 1)
            {
                sequenceCount = 1;
            }
            else if (apngBase.FrameCount > 1)
            {
                sequenceCount = apngBase.GetFrame(apngBase.FrameCount - 1).fdATs.Last().SequenceNumber + 1;
            }
            //Debug.WriteLine("sequenceCount:" + sequenceCount + "," + pngFrame.IDATList.Count);

            Frame frame = CreateFrame(fps, pngFrame.Height, pngFrame.Width, 0, 0, ref sequenceCount, isFirst, pngFrame.IDATList);

            apngBase.AddFrame(frame);

            apngBase.acTL.NumFrames = (uint)apngBase.FrameCount;
            //apngBase.Validate();
        }
Example #3
0
        //public static void Add(this APNG apngBase, IList<PNG> lstPngFrame) {
        //	uint sequenceCount = 0;

        //	for(int i = 0; i < lstPngFrame.Count; ++i) {
        //		bool isFirst = apngBase.acTL == null;
        //		if(isFirst) {
        //			SetupAPNGChunks(apngBase, lstPngFrame[i]);
        //		}

        //		Frame frame = CreateFrame(lstPngFrame[i].Height, lstPngFrame[i].Width, 0, 0, ref sequenceCount, true, lstPngFrame[i].IDATList);
        //		apngBase.AddFrame(frame);
        //	}

        //	//apngBase.acTL.NumFrames += (uint)lstPngFrame.Count;
        //	apngBase.acTL.NumFrames = (uint)apngBase.FrameCount;
        //	apngBase.Validate();
        //}

        public static void Save(this APNG apng, string filePath)
        {
            using (Stream pngStr = apng.ToStream()) {
                pngStr.Position = 0;

                using (FileStream fs = File.Create(filePath)) {
                    pngStr.CopyTo(fs);
                }
            }
        }
Example #4
0
        private static void SetupAPNGChunks(APNG apng, PNG png)
        {
            apng.IHDR = png.IHDR;
            apng.acTL = new acTLChunk()
            {
                NumPlays = 0
            };
            foreach (IDATChunk chunk in png.IDATList)
            {
                apng.IDATList.Add(chunk);
            }
            apng.IEND = png.IEND;
            apng.PLTE = png.PLTE;
            apng.tRNS = png.tRNS;
            apng.cHRM = png.cHRM;
            apng.gAMA = png.gAMA;
            apng.iCCP = png.iCCP;
            apng.sBIT = png.sBIT;
            apng.sRGB = png.sRGB;
            foreach (tEXtChunk chunk in png.tEXtList)
            {
                apng.tEXtList.Add(chunk);
            }
            foreach (zTXtChunk chunk in png.zTXtList)
            {
                apng.zTXtList.Add(chunk);
            }
            foreach (iTXtChunk chunk in png.iTXtList)
            {
                apng.iTXtList.Add(chunk);
            }
            apng.bKGD = png.bKGD;
            apng.hIST = png.hIST;
            apng.pHYs = png.pHYs;
            apng.sPLT = png.sPLT;

            DateTime dt = DateTime.Now;

            apng.tIME = new tIMEChunk()
            {
                Day    = (byte)dt.Day,
                Month  = (byte)dt.Month,
                Year   = (ushort)dt.Year,
                Hour   = (byte)dt.Hour,
                Minute = (byte)dt.Minute,
                Second = (byte)dt.Second
            };
        }
Example #5
0
        public static APNG AssembleAPNG(IList <string> files, bool optimize)
        {
            if (files.Count < 1)
            {
                return(null);
            }
            uint sequenceCount = 0;
            APNG apng          = new APNG();
            PNG  first         = new PNG();

            using (Stream s = File.OpenRead(files.First()))
            {
                first.Load(s);
            }
            SetupAPNGChunks(apng, first);
            Frame firstFrame = CreateFrame(first.Height, first.Width, 0, 0, ref sequenceCount, true, first.IDATList);

            apng.AddFrame(firstFrame);

            foreach (string file in files.Skip(1))
            {
                Point p   = new Point(0, 0);
                PNG   png = new PNG();
                using (Stream fileStr = File.OpenRead(file))
                {
                    if (optimize)
                    {
                        using (Stream optStr = OptimizeBitmapStream(fileStr, out p))
                        {
                            png.Load(optStr);
                        }
                    }
                    else
                    {
                        png.Load(fileStr);
                    }
                }
                Frame f = CreateFrame(png.Height, png.Width, (uint)p.X, (uint)p.Y, ref sequenceCount, false, png.IDATList);
                apng.AddFrame(f);
            }
            apng.acTL.NumFrames = (uint)apng.FrameCount;

            apng.Validate();
            return(apng);
        }