Ejemplo n.º 1
0
        // Maps image colors to the color map
        protected void AnalyzePixels()
        {
            int len  = m_Pixels.Length;
            int nPix = len / 3;

            m_IndexedPixels = new byte[nPix];

            // Analyze image colors and create color map (original, expensive, Moments Recorder behaviour)
            if (m_FramesPerColorSample == 0)
            {
                nq         = new NeuQuant(m_Pixels, len, (int)m_SampleInterval);
                m_ColorTab = nq.Process();                 // Create reduced palette
            }

            // Map image pixels to new palette
            int k = 0;

            for (int i = 0; i < nPix; i++)
            {
                int index = nq.Map(m_Pixels[k++] & 0xff, m_Pixels[k++] & 0xff, m_Pixels[k++] & 0xff);
                m_UsedEntry[index] = true;
                m_IndexedPixels[i] = (byte)index;
            }

            m_Pixels      = null;
            m_ColorDepth  = 8;
            m_PaletteSize = 7;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Builds a colour map out of the combined colours from several frames.
        /// </summary>
        /// <param name="frames">List of frames to sample colour palette from</param>
        public void BuildPalette(ref List <GifFrame> frames)
        {
            // Do not build the color palette here if user wants separate palettes created per frame
            if (m_FramesPerColorSample == 0)
            {
                return;
            }

            // Initialize a large image
            Byte[] combinedPixels = new Byte[3 * frames[0].Width * frames[0].Height * (1 + frames.Count / m_FramesPerColorSample)];

            int count = 0;

            // Stich the large image together out of pixels from several frames
            for (int i = 0; i < frames.Count; i += m_FramesPerColorSample)
            {
                Color32[] p = frames[i].Data;
                // Texture data is layered down-top, so flip it
                for (int th = frames[i].Height - 1; th >= 0; th--)
                {
                    for (int tw = 0; tw < frames[i].Width; tw++)
                    {
                        Color32 color = p[th * frames[i].Width + tw];
                        combinedPixels[count] = color.r; count++;
                        combinedPixels[count] = color.g; count++;
                        combinedPixels[count] = color.b; count++;
                    }
                }
            }

            // Run the quantizer over our stitched together image and create reduced palette
            nq         = new NeuQuant(combinedPixels, combinedPixels.Length, (int)m_SampleInterval);
            m_ColorTab = nq.Process();
        }
Ejemplo n.º 3
0
        // Analyzes image colors and creates color map.
        protected void AnalyzePixels()
        {
            int len  = m_Pixels.Length;
            int nPix = len / 3;

            m_IndexedPixels = new byte[nPix];
            NeuQuant nq = new NeuQuant(m_Pixels, len, (int)m_SampleInterval);

            m_ColorTab = nq.Process();             // Create reduced palette

            // Map image pixels to new palette
            int k = 0;

            for (int i = 0; i < nPix; i++)
            {
                int index = nq.Map(m_Pixels[k++] & 0xff, m_Pixels[k++] & 0xff, m_Pixels[k++] & 0xff);
                m_UsedEntry[index] = true;
                m_IndexedPixels[i] = (byte)index;
            }

            m_Pixels      = null;
            m_ColorDepth  = 8;
            m_PaletteSize = 7;
        }
Ejemplo n.º 4
0
        // Analyzes image colors and creates color map.
        protected void AnalyzePixels()
        {
            int len  = m_Pixels.Length;
            int nPix = len / 3;

            m_IndexedPixels = new byte[nPix];
            bool reuseTab = false;

            if (m_PrevPixels != null)
            {
                var delta = 0;
                for (int i = 0; i < len; i += 3)
                {
                    if (m_Pixels[i] != m_PrevPixels[i] ||
                        m_Pixels[i + 1] != m_PrevPixels[i + 1] ||
                        m_Pixels[i + 2] != m_PrevPixels[i + 2])
                    {
                        delta++;
                    }
                }

                var match = 100 - Mathf.Ceil(delta / nPix * 100);
                reuseTab = match >= 98;
            }


            if (!reuseTab)
            {
                nq = new NeuQuant(m_Pixels, len, (int)m_SampleInterval);

                m_ColorTab = nq.Process(); // Create reduced palette
            }

            if (m_PrevIndices == null)
            {
                m_PrevIndices = new int[nPix];
            }

            // Map image pixels to new palette
            for (int i = 0; i < nPix; i++)
            {
                int index = 0;
                if (reuseTab &&
                    m_PrevPixels != null &&
                    m_PrevPixels[3 * i + 0] == m_Pixels[3 * i + 0] &&
                    m_PrevPixels[3 * i + 1] == m_Pixels[3 * i + 1] &&
                    m_PrevPixels[3 * i + 2] == m_Pixels[3 * i + 2])
                {
                    index = m_PrevIndices[i];
                }
                else
                {
                    index            = nq.Map(m_Pixels[3 * i + 0] & 0xff, m_Pixels[3 * i + 1] & 0xff, m_Pixels[3 * i + 2] & 0xff);
                    m_PrevIndices[i] = index;
                }


                m_UsedEntry[index] = true;
                m_IndexedPixels[i] = (byte)index;
            }

            this.m_PrevPixels = m_Pixels;
            m_Pixels          = null;
            m_ColorDepth      = 8;
            m_PaletteSize     = 7;
        }
Ejemplo n.º 5
0
        // Analyzes image colors and creates color map.
        protected void AnalyzePixels()
        {
            int len = m_Pixels.Length;
            int nPix = len / 3;
            m_IndexedPixels = new byte[nPix];
            NeuQuant nq = new NeuQuant(m_Pixels, len, (int)m_SampleInterval);
            m_ColorTab = nq.Process(); // Create reduced palette

            // Map image pixels to new palette
            int k = 0;
            for (int i = 0; i < nPix; i++)
            {
                int index = nq.Map(m_Pixels[k++] & 0xff, m_Pixels[k++] & 0xff, m_Pixels[k++] & 0xff);
                m_UsedEntry[index] = true;
                m_IndexedPixels[i] = (byte)index;
            }

            m_Pixels = null;
            m_ColorDepth = 8;
            m_PaletteSize = 7;
        }