public static void doit(String orig)
        {
            string copy = TestsHelper.addSuffixToName(orig, "_tc");

            PngReader pngr = FileHelper.CreatePngReader(orig);

            if (!pngr.ImgInfo.Indexed)
            {
                throw new Exception("Not indexed image");
            }
            PngChunkPLTE plte  = pngr.GetMetadata().GetPLTE();
            PngChunkTRNS trns  = pngr.GetMetadata().GetTRNS(); // transparency metadata, can be null
            bool         alpha = trns != null;
            ImageInfo    im2   = new ImageInfo(pngr.ImgInfo.Cols, pngr.ImgInfo.Rows, 8, alpha);
            PngWriter    pngw  = FileHelper.CreatePngWriter(copy, im2, true);

            pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE);
            int[] buf = null;
            for (int row = 0; row < pngr.ImgInfo.Rows; row++)
            {
                ImageLine line = pngr.ReadRowInt(row);
                buf = ImageLineHelper.Palette2rgb(line, plte, trns, buf);
                pngw.WriteRowInt(buf, row);
            }
            pngw.CopyChunksLast(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE);
            pngr.End();
            pngw.End();
            Console.WriteLine("True color: " + copy);
        }
Ejemplo n.º 2
0
        private static void additionalTestPalette(string orig, string truecolor)
        {
            // covnert to true color 8 bits and check equality
            PngReader    pngr  = FileHelper.CreatePngReader(orig);
            PngChunkPLTE plte  = pngr.GetMetadata().GetPLTE();
            PngChunkTRNS trns  = pngr.GetMetadata().GetTRNS();
            string       copy  = TestsHelper.addSuffixToName(orig, "_tccopy");
            bool         alpha = trns != null;
            ImageInfo    im2   = new ImageInfo(pngr.ImgInfo.Cols, pngr.ImgInfo.Rows, 8, alpha);
            PngWriter    pngw  = FileHelper.CreatePngWriter(copy, im2, true);

            pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE);
            int[] buf = null;
            for (int row = 0; row < pngr.ImgInfo.Rows; row++)
            {
                ImageLine line = pngr.ReadRowInt(row);
                buf = ImageLineHelper.Palette2rgb(line, plte, trns, buf);
                pngw.WriteRowInt(buf, row);
            }
            pngr.End();
            pngw.End();
            TestsHelper.testEqual(copy, truecolor);
            System.IO.File.Delete(copy);
        }
Ejemplo n.º 3
0
        public IEnumerable <RGBA> EnRGBA()
        {
            PngChunkPLTE oplte = null;
            PngChunkTRNS otrns = null; // transparency metadata, can be null

            if (pngr.ImgInfo.Indexed)
            {
                oplte = pngr.GetMetadata().GetPLTE();
                otrns = pngr.GetMetadata().GetTRNS();
            }

            for (var row = 0; row < pngr.ImgInfo.Rows; row++)
            {
                var line = pngr.ReadRowInt(row);
                if (pngr.ImgInfo.Indexed)
                {
                    var rgrgba = ImageLineHelper.Palette2rgb(line, oplte, otrns, null);
                    for (var irgba = 0; irgba < rgrgba.Length;)
                    {
                        yield return(new RGBA
                        {
                            r = rgrgba[irgba++],
                            g = rgrgba[irgba++],
                            b = rgrgba[irgba++],
                            a = (otrns != null ? rgrgba[irgba++] : 255),
                        });
                    }
                }
                else
                {
                    if (pngr.ImgInfo.Packed)
                    {
                        line = line.unpackToNewImageLine();
                    }
                    for (var col = 0; col < pngr.ImgInfo.Cols; col++)
                    {
                        switch (pngr.ImgInfo.Channels)
                        {
                        case 1:
                            yield return(new RGBA
                            {
                                r = Read8(col, line),
                                g = Read8(col, line),
                                b = Read8(col, line),
                            });

                            break;

                        case 2:
                            yield return(new RGBA
                            {
                                r = Read8(col * 2, line),
                                g = Read8(col * 2, line),
                                b = Read8(col * 2, line),
                                a = Read8(col * 2 + 1, line),
                            });

                            break;

                        case 3:
                            yield return(new RGBA
                            {
                                r = Read8(col * 3, line),
                                g = Read8(col * 3 + 1, line),
                                b = Read8(col * 3 + 2, line),
                            });

                            break;

                        case 4:
                            yield return(new RGBA
                            {
                                r = Read8(col * 4, line),
                                g = Read8(col * 4 + 1, line),
                                b = Read8(col * 4 + 2, line),
                                a = Read8(col * 4 + 3, line),
                            });

                            break;
                        }
                    }
                }
            }
        }