예제 #1
0
        public void SetNeighbors(DMSImage Source, int resolution)
        {
            if (resolution >= m_savedResolution)
                return;
            m_savedResolution = resolution;

            if (_V != 0 && (Up == 0 || Up > resolution))
            {
                for (Up = 1; Up < resolution; Up++)
                {
                    if (_V - Up < 0)
                    {
                        Up = 0;
                        break;
                    }
                    if (Source.GetPixel(_U, _V - Up).A != 0)
                        break;
                }
            }

            if (_V != Source.Height - 1 && (Down == 0 || Down > resolution))
            {
                for (Down = 1; Down < resolution; Down++)
                {
                    if (_V + Down >= Source.Height)
                    {
                        Down = 0;
                        break;
                    }
                    if (Source.GetPixel(_U, _V + Down).A != 0)
                        break;
                }
            }

            if (_U != 0 && (Left == 0 || Left > resolution))
            {
                for (Left = 1; Left < resolution; Left++)
                {
                    if (_U - Left < 0)
                    {
                        Left = 0;
                        break;
                    }
                    if (Source.GetPixel(_U - Left, _V).A != 0)
                        break;
                }
            }

            if (_U != Source.Width - 1 && (Right == 0 || Right > resolution))
            {
                for (Right = 1; Right < resolution; Right++)
                {
                    if (_U + Right >= Source.Width)
                    {
                        Right = 0;
                        break;
                    }
                    if (Source.GetPixel(_U + Right, _V).A != 0)
                        break;
                }
            }
        }
예제 #2
0
        //constructors;
        public Palette(DMSImage SourcePixels, DMSImage Target)
            : base(new Size(Target.Width, Target.Height))
        {
            Random rnd = new Random();
            int u, v, i;

            //set up histogram tables.
            m_sourceTable = new double[256];
            m_targetTable = new double[256];
            for (i = 0; i < 256; i++)
            {
                m_sourceTable[i] = 0;
                m_targetTable[i] = 0;
            }

            m_totalSourcePixels = 0.0;
            for (u = 0; u < SourcePixels.Width; u++)
            {
                for (v = 0; v < SourcePixels.Height; v++)
                {
                    Color pixel = SourcePixels.GetPixel(u, v);

                    m_sourceTable[pixel.R] += (double)pixel.A / 255.0;
                    m_totalSourcePixels += (double)pixel.A / 255.0;
                }
            }

            for( u = 0; u < Target.Width; u++ )
            {
                for (v = 0; v < Target.Height; v++)
                {
                    m_targetTable[Target.GetPixel(u, v).R]++;
                }
            }

            //scale source pixels to equal total number of target pixels.
            double factor = (double)Target.Height * Target.Width / m_totalSourcePixels;
            for ( i = 0; i < 256; i++)
                m_sourceTable[i] *= factor;

            //set targetTable such that targetTable[i-1] .. targetTable[i] is the range of pixel indices of value i;
            for (i = 1; i < 256; i++)
                m_targetTable[i] += m_targetTable[i - 1];

            //okay now set each pixel
            for (u = 0; u < Target.Width; u++)
            {
                for (v = 0; v < Target.Height; v++)
                {
                    int pixelValue = Target.GetPixel(u, v).R;
            #if false
                    //find out how far we are into the histogram.
                    double index = (pixelValue == 0) ? 0 : m_targetTable[pixelValue - 1];
                    index += rnd.NextDouble() * (m_targetTable[pixelValue] - index);  //this randomization effects a dithering
            #else
                    m_targetTable[pixelValue]--;
                    if (m_targetTable[pixelValue] < 0) m_targetTable[pixelValue] = 0;
                    double index = m_targetTable[pixelValue];
            #endif

                    //now which value in source has this pixel?
                    double sum=0;
                    for( i=0; i<256; i++ )
                    {
                        if( sum + m_sourceTable[i] >= index )
                            break;
                        sum += m_sourceTable[i];
                    }

                    SetPixel(u,v,Color.FromArgb(i,i,i));
                }
            }
        }