コード例 #1
0
        /// <summary>
        /// Given an indexed line with a palette, unpacks as a RGB array
        /// </summary>
        /// <param name="line">ImageLine as returned from PngReader</param>
        /// <param name="pal">Palette chunk</param>
        /// <param name="trns">TRNS chunk (optional)</param>
        /// <param name="buf">Preallocated array, optional</param>
        /// <returns>R G B (one byte per sample)</returns>
        public static int[] Palette2rgb(ImageLine line, PngChunkPLTE pal, PngChunkTRNS trns, int[] buf)
        {
            bool isalpha  = trns != null;
            int  channels = isalpha ? 4 : 3;
            int  nsamples = line.ImgInfo.Cols * channels;

            if (buf == null || buf.Length < nsamples)
            {
                buf = new int[nsamples];
            }
            if (!line.SamplesUnpacked)
            {
                line = line.unpackToNewImageLine();
            }
            bool isbyte            = line.SampleType == Hjg.Pngcs.ImageLine.ESampleType.BYTE;
            int  nindexesWithAlpha = trns != null?trns.GetPalletteAlpha().Length : 0;

            for (int c = 0; c < line.ImgInfo.Cols; c++)
            {
                int index = isbyte ? (line.ScanlineB[c] & 0xFF) : line.Scanline[c];
                pal.GetEntryRgb(index, buf, c * channels);
                if (isalpha)
                {
                    int alpha = index < nindexesWithAlpha?trns.GetPalletteAlpha()[index] : 255;

                    buf[c * channels + 3] = alpha;
                }
            }
            return(buf);
        }
コード例 #2
0
        public static int[] Palette2rgb(ImageLine line, PngChunkPLTE pal, PngChunkTRNS trns, int[] buf)
        {
            bool flag = trns != null;
            int  num  = flag ? 4 : 3;
            int  num2 = line.ImgInfo.Cols * num;

            if (buf == null || buf.Length < num2)
            {
                buf = new int[num2];
            }
            if (!line.SamplesUnpacked)
            {
                line = line.unpackToNewImageLine();
            }
            bool flag2 = line.SampleType == ImageLine.ESampleType.BYTE;
            int  num3  = (trns != null) ? trns.GetPalletteAlpha().Length : 0;

            for (int i = 0; i < line.ImgInfo.Cols; i++)
            {
                int num4 = flag2 ? (line.ScanlineB[i] & 0xFF) : line.Scanline[i];
                pal.GetEntryRgb(num4, buf, i * num);
                if (flag)
                {
                    int num5 = buf[i * num + 3] = ((num4 < num3) ? trns.GetPalletteAlpha()[num4] : 255);
                }
            }
            return(buf);
        }
コード例 #3
0
        private static unsafe Bitmap ReadBitmap(int bitmapIndex, byte[][] buf, PngChunkPLTE pal)
        {
            var bmp = new Bitmap(32, 32, PixelFormat.Format32bppArgb);

            var data = bmp.LockBits(
                new Rectangle(0, 0, 32, 32),
                ImageLockMode.WriteOnly,
                PixelFormat.Format32bppArgb);

            try
            {
                var ptr = (int *)data.Scan0;

                for (var y = 0; y < 32; y++)
                {
                    var line = buf[y];

                    for (var x = 0; x < 32; x++)
                    {
                        var offset    = (bitmapIndex * 32) + x;
                        var bmpOffset = (y * 32) + x;
                        int idx       = line[offset];

                        var rgb = new int[3];
                        pal.GetEntryRgb(idx, rgb);

                        var c = Color.FromArgb(rgb[0], rgb[1], rgb[2]);

                        if (!PaletteFactory.TAPalette.Contains(c))
                        {
                            throw new ArgumentException("Image contains colors not in the TA palette.");
                        }

                        ptr[bmpOffset] = c.ToArgb();
                    }
                }
            }
            finally
            {
                bmp.UnlockBits(data);
            }

            return(bmp);
        }
コード例 #4
0
    bool ReadPalette(PngChunkPLTE _palette)
    {
        int[] rgb = new int[4];
        int   c;

        for (c = 0; c < m_coloursInPalette; c++)
        {
            int c2 = c;
            if (m_config.m_colorRemapSourceToDest.ContainsKey(c))
            {
                c2 = m_config.m_colorRemapSourceToDest[c];
            }

            _palette.GetEntryRgb(c2, rgb);
            float fr = ((float)rgb[0]) / 255.0f;
            float fg = ((float)rgb[1]) / 255.0f;
            float fb = ((float)rgb[2]) / 255.0f;
            //float fa = ((float)a) / 255.0f;
            float fa = 1.0f;

            /*
             * if((c%16) == 0 )
             *      fa = 0.0f;
             * else
             *      fa = 1.0f;
             */

            Color col = new Color(fr, fg, fb, fa);
            m_palette.Add(col);
            m_colorUsed.Add(false);
        }

        while (c < 16)
        {
            m_palette.Add(new Color(1.0f, 0.0f, 1.0f, 1.0f));
            m_colorUsed.Add(false);
            c++;
        }

        return(true);
    }
コード例 #5
0
        /// <summary>
        /// Given an indexed line with a palette, unpacks as a RGB array
        /// </summary>
        /// <param name="line">ImageLine as returned from PngReader</param>
        /// <param name="pal">Palette chunk</param>
        /// <param name="trns">TRNS chunk (optional)</param>
        /// <param name="buf">Preallocated array, optional</param>
        /// <returns>R G B (one byte per sample)</returns>
        public static int[] Palette2rgb(ImageLine line, PngChunkPLTE pal, PngChunkTRNS trns, int[] buf)
        {
            if (line is null)
            {
                throw new ArgumentNullException(nameof(line));
            }

            if (pal is null)
            {
                throw new ArgumentNullException(nameof(pal));
            }

            var isalpha  = trns is object;
            var channels = isalpha ? 4 : 3;
            var nsamples = line.ImgInfo.Cols * channels;

            if (buf is null || buf.Length < nsamples)
            {
                buf = new int[nsamples];
            }

            if (!line.SamplesUnpacked)
            {
                line = line.UnpackToNewImageLine();
            }

            var isbyte            = line.SampleType == Hjg.Pngcs.ImageLine.ESampleType.BYTE;
            var nindexesWithAlpha = trns?.GetPalletteAlpha().Length ?? 0;

            for (var c = 0; c < line.ImgInfo.Cols; c++)
            {
                var index = isbyte ? (line.ScanlineB[c] & 0xFF) : line.Scanline[c];
                pal.GetEntryRgb(index, buf, c * channels);
                if (isalpha)
                {
                    buf[(c * channels) + 3] = index < nindexesWithAlpha?trns.GetPalletteAlpha()[index] : 255;
                }
            }

            return(buf);
        }