Exemple #1
0
        public static void showChunks(String file)
        {
            PngReader pngr = FileHelper.CreatePngReader(file);

            pngr.MaxTotalBytesRead = 1024 * 1024 * 1024L * 3; // 3Gb!
            pngr.ReadSkippingAllRows();
            Console.Out.WriteLine(pngr.ToString());
            Console.Out.WriteLine(pngr.GetChunksList().ToStringFull());
        }
Exemple #2
0
        static int LoadTexture_Lua(IntPtr L)
        {
            var path = Api.lua_tostring(L, 1);

            try
            {
                var           bytes = File.ReadAllBytes(Path.Combine(Application.persistentDataPath, path));
                TextureFormat fmt = TextureFormat.ARGB32;
                bool          hasOffset = false;
                long          offx = 0, offy = 0, units = 0;
                if (path.EndsWith(".jpg"))
                {
                    fmt = TextureFormat.RGB24;
                }
                else if (path.EndsWith(".png"))
                {
                    var r         = new PngReader(new MemoryStream(bytes));
                    var chunkList = r.GetChunksList();
                    var offChunk  = chunkList.GetById1(PngChunkOFFS.ID) as PngChunkOFFS;
                    if (offChunk != null)
                    {
                        hasOffset = true;
                        units     = offChunk.GetUnits();
                        offx      = offChunk.GetPosX();
                        offy      = offChunk.GetPosY();
                    }
                }
                var tex = new Texture2D(2, 2, fmt, false, true);
                if (tex.LoadImage(bytes))
                {
                    lua.Lua.PushObjectInternal(L, tex);
                    var retCount = 1;
                    if (hasOffset)
                    {
                        Api.lua_pushinteger(L, offx);
                        Api.lua_pushinteger(L, offy);
                        Api.lua_pushinteger(L, units);
                        retCount += 3;
                    }
                    return(retCount);
                }
                else
                {
                    Api.lua_pushnil(L);
                    Api.lua_pushstring(L, "err LoadImage from " + path);
                    return(2);
                }
            }
            catch (Exception e)
            {
                Api.lua_pushnil(L);
                Api.lua_pushstring(L, e.Message);
                return(2);
            }
        }
Exemple #3
0
        private static TRet ReadChunk <TRet, TChunk>(string filePath, string chunkId, Func <TChunk, TRet> valueFunc)
            where TChunk : PngChunk
        {
            PngReader pngr = null;

            try
            {
                pngr = FileHelper.CreatePngReader(filePath);

                TChunk chunk = (TChunk)pngr.GetChunksList().GetById1(chunkId);

                return(valueFunc(chunk));
            }
            finally
            {
                pngr?.End();
            }
        }
        public static void testRead(String file)
        {
            // register with factory chunk
            PngChunk.FactoryRegister(PngChunkSERI.ID, typeof(PngChunkSERI));
            // read all file
            PngReader pngr = FileHelper.CreatePngReader(file);

            pngr.ReadSkippingAllRows();
            pngr.End();
            // we assume there can be at most one chunk of this type...
            PngChunk chunk = pngr.GetChunksList().GetById1(PngChunkSERI.ID); // This would work even if not registered, but then PngChunk would be of type PngChunkUNKNOWN

            Console.Out.WriteLine(chunk);
            // the following would fail if we had not register the chunk
            PngChunkSERI chunkprop = (PngChunkSERI)chunk;
            string       name      = chunkprop.GetObj().name;
            int          age       = chunkprop.GetObj().age;

            Console.Out.WriteLine("Done. Name: " + name + " age=" + age);
        }
Exemple #5
0
        public static ResourceDictionary LoadFromPng(string FileName)
        {
            // read all file
            PngReader pngr = FileHelper.CreatePngReader(FileName);

            pngr.ReadSkippingAllRows();
            pngr.End();
            // we assume there can be at most one chunk of this type...
            PngChunk chunk = pngr.GetChunksList().GetById1(PngChunkSKIN.ID); // This would work even if not registered, but then PngChunk would be of type PngChunkUNKNOWN

            if (chunk != null)
            {
                // the following would fail if we had not register the chunk
                PngChunkSKIN  chunkprop = (PngChunkSKIN)chunk;
                ParserContext pc        = new ParserContext();
                pc.XamlTypeMapper = XamlTypeMapper.DefaultMapper;
                //  pc.XmlSpace

                //MimeObjectFactory s;

                var rd1 = (ResourceDictionary)XamlReader.Parse(chunkprop.Content);

                // Application.Current.Resources.MergedDictionaries.Add(rd1);

                //  var rd2 = (ResourceDictionary)XamlReader.Parse(chunkprop.Content);

                ////  Application.Current.Resources.MergedDictionaries.Add(rd2);

                //  if (rd1 == rd2) {
                //  }

                return(rd1);
            }
            else
            {
                return(null);
            }
        }
Exemple #6
0
        public string GetHeader(string icon)
        {
            string ztxt = string.Empty;

            PngReader pngr = FileHelper.CreatePngReader(FileName);

            width  = pngr.ImgInfo.Cols;
            height = pngr.ImgInfo.Rows;
            ChunksList clist = pngr.GetChunksList();

            /*The File should only have one zTxt, this chunk stores our DMI information such as
             * sprite width, height, iconstates, directionals and frames among other things.
             */
            foreach (PngChunkZTXT desc in clist.GetById("zTXt"))
            {
                ztxt = desc.GetVal();
                break;
            }
            pngr.ShouldCloseStream = true;
            pngr.End();

            return(ztxt);
        }
        /// <summary>
        /// Writes a new file with several text chunks, reads it back and compares
        /// </summary>

        public static void test()
        {
            Dictionary <string, string> texts = new Dictionary <String, String>();

            texts.Add("key1", "val");
            texts.Add("empty1", "");
            texts.Add("unicode1", "Hernán");
            texts.Add("zero1", "Hola\0chau");
            texts.Add("key2", "val");
            texts.Add("empty2", "");
            texts.Add("unicode2", "Hernán");
            texts.Add("zero2", "Hola\0chau");
            texts.Add("key3", "val");
            texts.Add("empty3", "");
            texts.Add("unicode3", "Hernán");
            texts.Add("zero3", "Hola\0chau");
            texts.Add("nolatin1", "Hernán\u1230");
            String    suffix = "text";
            PngWriter png    = TestsHelper.prepareFileTmp(suffix);

            png.GetMetadata().SetText("key1", texts["key1"], false, false);
            png.GetMetadata().SetText("key2", texts["key2"], true, false);
            png.GetMetadata().SetText("key3", texts["key3"], true, true);
            png.GetMetadata().SetText("empty1", texts["empty1"], false, false);
            png.GetMetadata().SetText("empty2", texts["empty2"], true, false);
            png.GetMetadata().SetText("empty3", texts["empty3"], true, true);
            png.GetMetadata().SetText("unicode1", texts["unicode1"], false, false);
            png.GetMetadata().SetText("unicode2", texts["unicode2"], true, false);
            png.GetMetadata().SetText("unicode3", texts["unicode3"], true, true);
            png.GetMetadata().SetText("nolatin1", texts["nolatin1"], false, false);
            png.GetMetadata().SetText("zero1", texts["zero1"], false, false);
            png.GetMetadata().SetText("zero2", texts["zero2"], true, false);
            png.GetMetadata().SetText("zero3", texts["zero3"], true, true);
            TestsHelper.endFileTmp(png);
            PngReader pngr = TestsHelper.getReaderTmp(suffix);

            pngr.ReadSkippingAllRows();
            int ok = 0;

            foreach (PngChunk c in pngr.GetChunksList().GetChunks())
            {
                if (!ChunkHelper.IsText(c))
                {
                    continue;
                }
                ok++;
                PngChunkTextVar ct  = (PngChunkTextVar)c;
                String          key = ct.GetKey();
                String          val = ct.GetVal();
                Console.WriteLine(c.Id + " chunk. Key:" + key + " val='" + val + "'");
                if (!val.Equals(texts[key]))
                {
                    Console.WriteLine("ERROR: expected '" + texts[key] + "' got '" + val
                                      + "' key=" + key + " id=" + c.Id);
                }
            }
            if (ok != texts.Keys.Count)
            {
                throw new Exception("number of text chunks does not coincide");
            }
            Console.WriteLine("done");
        }
Exemple #8
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.");
        }