예제 #1
0
        /// <summary> Fills an array of pixels from the specified values.
        ///
        /// </summary>
        /// <param name="x">the x-coordinate of the upper-left corner of the region of
        /// pixels
        /// </param>
        /// <param name="y">the y-coordinate of the upper-left corner of the region of
        /// pixels
        /// </param>
        /// <param name="w">the width of the region of pixels
        /// </param>
        /// <param name="h">the height of the region of pixels
        /// </param>
        /// <param name="pixels">the array of pixels
        /// </param>
        /// <param name="off">the offset into the pixel array
        /// </param>
        /// <param name="scansize">the distance from one row of pixels to the next in the
        /// array
        /// </param>
        public void FillRgbPixels(int x, int y, int w, int h, int[] pixels, int off, int scansize)
        {
            int yrev = _parent.ImageHeight - y;

            if (!_parent.IsRampNeeded)
            {
                for (int y0 = yrev; y0-- > (yrev - h); off += scansize)
                {
                    for (int i = off, j = (_parent.RowOffset(y0) + x) * _ncolors, k = w; k > 0; k--, j += _ncolors)
                    {
                        pixels[i++] = unchecked ((int)0xff000000) | (0xff0000 & (_parent.Data[j + _redOffset] << 16)) |
                                      (0xff00 & (_parent.Data[j + _greenOffset] << 8)) |
                                      (0xff & _parent.Data[j + _blueOffset]);
                    }
                }
            }
            else
            {
                PixelReference ref_Renamed = _parent.CreateGPixelReference(0);
                for (int y0 = yrev; y0-- > (yrev - h); off += scansize)
                {
                    ref_Renamed.SetOffset(y0, x);
                    for (int i = off, k = w; k > 0; k--, ref_Renamed.IncOffset())
                    {
                        pixels[i++] = _parent.PixelRamp(ref_Renamed).GetHashCode();
                    }
                }
            }
        }
예제 #2
0
        /// <summary> Initialize this PixelMap from another image map.
        ///
        /// </summary>
        /// <param name="ref">image map to initialize from
        ///
        /// </param>
        /// <returns> the initialized PixelMap
        /// </returns>
        public virtual PixelMap Init(Map ref_Renamed)
        {
            Init(ref_Renamed.ImageHeight, ref_Renamed.ImageWidth, ((null)));

            PixelReference pixel = CreateGPixelReference(0);

            if ((ImageHeight > 0) && (ImageWidth > 0))
            {
                PixelReference refPixel = (ref_Renamed).CreateGPixelReference(0);

                for (int y = 0; y < ImageHeight; y++)
                {
                    pixel.SetOffset(y, 0);
                    refPixel.SetOffset(y, 0);

                    if (!IsRampNeeded)
                    {
                        for (int x = ImageWidth; x-- > 0; pixel.IncOffset(), refPixel.IncOffset())
                        {
                            pixel.CopyFrom(refPixel);
                        }
                    }
                    else
                    {
                        for (int x = ImageWidth; x-- > 0; pixel.IncOffset(), refPixel.IncOffset())
                        {
                            pixel.CopyFrom(ref_Renamed.PixelRamp(refPixel));
                        }
                    }
                }
            }

            return(this);
        }
예제 #3
0
        /// <summary> Insert the reference map at the specified location.
        ///
        /// </summary>
        /// <param name="ref">map to insert
        /// </param>
        /// <param name="dx">horizontal position to insert at
        /// </param>
        /// <param name="dy">vertical position to insert at
        /// </param>
        public override void Fill(Map ref_Renamed, int dx, int dy)
        {
            int x0 = (dx > 0) ? dx : 0;
            int y0 = (dy > 0) ? dy : 0;
            int x1 = (dx < 0) ? (-dx) : 0;
            int y1 = (dy < 0) ? (-dy) : 0;
            int w0 = ImageWidth - x0;
            int w1 = ref_Renamed.ImageWidth - x1;
            int w  = (w0 < w1) ? w0 : w1;
            int h0 = ImageHeight - y0;
            int h1 = ref_Renamed.ImageHeight - y1;
            int h  = (h0 < h1) ? h0 : h1;

            if ((w > 0) && (h > 0))
            {
                PixelReference pixel    = CreateGPixelReference(0);
                PixelReference refPixel = ref_Renamed.CreateGPixelReference(0);

                do
                {
                    pixel.SetOffset(y0++, x0);
                    refPixel.SetOffset(y1++, x1);

                    if (!IsRampNeeded)
                    {
                        pixel.SetPixels(refPixel, w);
                    }
                    else
                    {
                        int i = w;
                        do
                        {
                            pixel.CopyFrom(ref_Renamed.PixelRamp(refPixel));
                            pixel.IncOffset();
                            refPixel.IncOffset();
                        } while (--i > 0);
                    }
                } while (--h > 0);
            }
        }
예제 #4
0
        /// <summary> Initialize this PixelMap from a segment of another image map.
        ///
        /// </summary>
        /// <param name="ref">image map to initialize from
        /// </param>
        /// <param name="rect">bounding rectangle to initialize from
        ///
        /// </param>
        /// <returns> the initialized PixelMap
        /// </returns>
        public virtual PixelMap Init(Map ref_Renamed, Rectangle rect)
        {
            Init(rect.Height, rect.Width, ((null)));

            Rectangle rect2 = new Rectangle(0, 0, ref_Renamed.ImageWidth, ref_Renamed.ImageHeight);

            rect2.Intersect(rect2, rect);
            rect2.Translate(-rect.Right, -rect.Bottom);

            if (!rect2.Empty)
            {
                PixelReference pixel    = CreateGPixelReference(0);
                PixelReference refPixel = ref_Renamed.CreateGPixelReference(0);

                for (int y = rect2.Bottom; y < rect2.Top; y++)
                {
                    pixel.SetOffset(y, rect2.Right);
                    refPixel.SetOffset(y + rect.Bottom, rect.Right + rect2.Right);

                    if (!IsRampNeeded)
                    {
                        for (int x = rect2.Left - rect2.Right; x-- > 0; pixel.IncOffset(), refPixel.IncOffset())
                        {
                            pixel.CopyFrom(refPixel);
                        }
                    }
                    else
                    {
                        for (int x = rect2.Left - rect2.Right; x-- > 0; pixel.IncOffset(), refPixel.IncOffset())
                        {
                            pixel.CopyFrom(ref_Renamed.PixelRamp(refPixel));
                        }
                    }
                }
            }

            return(this);
        }
예제 #5
0
        /// <summary> Fill this image from another source at reduced resolution of 4 vertical
        /// pixels to 3.  An extrapulating pixel averaging algorithm is used.
        ///
        /// </summary>
        /// <param name="src">image map to reduce
        /// </param>
        /// <param name="pdr">target bounds
        ///
        /// </param>
        /// <throws>  IllegalArgumentException if the target rectangle is out of bounds </throws>
        public virtual void Downsample43(Map src, Rectangle pdr)
        {
            int srcwidth  = src.ImageWidth;
            int srcheight = src.ImageHeight;
            //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
            int destwidth = (int)Math.Ceiling(srcwidth * 0.75D);
            //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
            int       destheight = (int)Math.Ceiling(srcheight * 0.75D);
            Rectangle rect       = new Rectangle(0, 0, destwidth, destheight);

            if (pdr != null)
            {
                if ((pdr.Right < rect.Right) || (pdr.Bottom < rect.Bottom) || (pdr.Left > rect.Left) || (pdr.Top > rect.Top))
                {
                    throw new ArgumentException("rectangle out of bounds" + "pdr=(" + pdr.Right + "," + pdr.Bottom + "," +
                                                pdr.Left + "," + pdr.Top + "),rect=(" + rect.Right + "," + rect.Bottom +
                                                "," + rect.Left + "," + rect.Top + ")");
                }

                rect       = pdr;
                destwidth  = rect.Width;
                destheight = rect.Height;
            }

            Init(destheight, destwidth, null);

            int sy = rect.Bottom / 3;
            int dy = rect.Bottom - (3 * sy);

            //    if(dy < 0)
            //    {
            //      sy--;
            //      dy += 3;
            //    }

            int sxz = rect.Right / 3;
            int dxz = rect.Right - (3 * sxz);

            if (dxz < 0)
            {
                sxz--;
                dxz += 3;
            }

            sxz *= 4;
            sy  *= 4;

            PixelReference spix0 = src.CreateGPixelReference(0);
            PixelReference spix1 = src.CreateGPixelReference(0);
            PixelReference spix2 = src.CreateGPixelReference(0);
            PixelReference spix3 = src.CreateGPixelReference(0);
            PixelReference dpix0 = CreateGPixelReference(0);
            PixelReference dpix1 = CreateGPixelReference(0);
            PixelReference dpix2 = CreateGPixelReference(0);

            while (dy < destheight)
            {
                spix0.SetOffset(sy++, sxz);

                if (sy >= srcheight)
                {
                    sy--;
                }

                spix1.SetOffset(sy++, sxz);

                if (sy >= srcheight)
                {
                    sy--;
                }

                spix2.SetOffset(sy++, sxz);

                if (sy >= srcheight)
                {
                    sy--;
                }

                spix3.SetOffset(sy++, sxz);

                dpix0.SetOffset((dy < 0) ? 0 : dy, dxz);

                if (++dy >= destheight)
                {
                    dy--;
                }

                dpix1.SetOffset((dy < 0) ? 0 : dy, dxz);

                if (++dy >= destheight)
                {
                    dy--;
                }

                dpix2.SetOffset(dy++, dxz);
                int dx = dxz;
                int sx = sxz;

                Pixel pix0 = src.PixelRamp(spix0);
                Pixel pix1 = src.PixelRamp(spix1);
                Pixel pix2 = src.PixelRamp(spix2);
                Pixel pix3 = src.PixelRamp(spix3);
                while (dx < destwidth)
                {
                    int s00b = pix0.Blue;
                    int s00g = pix0.Green;
                    int s00r = pix0.Red;
                    int s01b = pix1.Blue;
                    int s01g = pix1.Green;
                    int s01r = pix1.Red;
                    int s02b = pix2.Blue;
                    int s02g = pix2.Green;
                    int s02r = pix2.Red;
                    int s03b = pix3.Blue;
                    int s03g = pix3.Green;
                    int s03r = pix3.Red;

                    if (++sx < srcwidth)
                    {
                        spix0.IncOffset();
                        spix1.IncOffset();
                        spix2.IncOffset();
                        spix3.IncOffset();
                        pix0 = src.PixelRamp(spix0);
                        pix1 = src.PixelRamp(spix1);
                        pix2 = src.PixelRamp(spix2);
                        pix3 = src.PixelRamp(spix3);
                    }

                    int s10b = pix0.Blue;
                    int s10g = pix0.Green;
                    int s10r = pix0.Red;
                    int s11b = pix1.Blue;
                    int s11g = pix1.Green;
                    int s11r = pix1.Red;
                    int s12b = pix2.Blue;
                    int s12g = pix2.Green;
                    int s12r = pix2.Red;
                    int s13b = pix3.Blue;
                    int s13g = pix3.Green;
                    int s13r = pix3.Red;

                    if (++sx < srcwidth)
                    {
                        spix0.IncOffset();
                        spix1.IncOffset();
                        spix2.IncOffset();
                        spix3.IncOffset();
                        pix0 = src.PixelRamp(spix0);
                        pix1 = src.PixelRamp(spix1);
                        pix2 = src.PixelRamp(spix2);
                        pix3 = src.PixelRamp(spix3);
                    }

                    int s20b = pix0.Blue;
                    int s20g = pix0.Green;
                    int s20r = pix0.Red;
                    int s21b = pix1.Blue;
                    int s21g = pix1.Green;
                    int s21r = pix1.Red;
                    int s22b = pix2.Blue;
                    int s22g = pix2.Green;
                    int s22r = pix2.Red;
                    int s23b = pix3.Blue;
                    int s23g = pix3.Green;
                    int s23r = pix3.Red;

                    if (++sx < srcwidth)
                    {
                        spix0.IncOffset();
                        spix1.IncOffset();
                        spix2.IncOffset();
                        spix3.IncOffset();
                        pix0 = src.PixelRamp(spix0);
                        pix1 = src.PixelRamp(spix1);
                        pix2 = src.PixelRamp(spix2);
                        pix3 = src.PixelRamp(spix3);
                    }

                    int s30b = pix0.Blue;
                    int s30g = pix0.Green;
                    int s30r = pix0.Red;
                    int s31b = pix1.Blue;
                    int s31g = pix1.Green;
                    int s31r = pix1.Red;
                    int s32b = pix2.Blue;
                    int s32g = pix2.Green;
                    int s32r = pix2.Red;
                    int s33b = pix3.Blue;
                    int s33g = pix3.Green;
                    int s33r = pix3.Red;

                    if (++sx < srcwidth)
                    {
                        spix0.IncOffset();
                        spix1.IncOffset();
                        spix2.IncOffset();
                        spix3.IncOffset();
                        pix0 = src.PixelRamp(spix0);
                        pix1 = src.PixelRamp(spix1);
                        pix2 = src.PixelRamp(spix2);
                        pix3 = src.PixelRamp(spix3);
                    }

                    dpix0.Blue  = (sbyte)(((11 * s00b) + (2 * (s01b + s10b)) + s11b + 8) >> 4);
                    dpix0.Green = (sbyte)(((11 * s00g) + (2 * (s01g + s10g)) + s11g + 8) >> 4);
                    dpix0.Red   = (sbyte)(((11 * s00r) + (2 * (s01r + s10r)) + s11r + 8) >> 4);
                    dpix1.Blue  = (sbyte)(((7 * (s01b + s02b)) + s11b + s12b + 8) >> 4);
                    dpix1.Green = (sbyte)(((7 * (s01g + s02g)) + s11g + s12g + 8) >> 4);
                    dpix1.Red   = (sbyte)(((7 * (s01r + s02r)) + s11r + s12r + 8) >> 4);
                    dpix2.Blue  = (sbyte)(((11 * s03b) + (2 * (s02b + s13b)) + s12b + 8) >> 4);
                    dpix2.Green = (sbyte)(((11 * s03g) + (2 * (s02g + s13g)) + s12g + 8) >> 4);
                    dpix2.Red   = (sbyte)(((11 * s03r) + (2 * (s02r + s13r)) + s12r + 8) >> 4);

                    if (++dx < destwidth)
                    {
                        dpix0.IncOffset();
                        dpix1.IncOffset();
                        dpix2.IncOffset();
                    }

                    dpix0.Blue  = (sbyte)(((7 * (s10b + s20b)) + s11b + s21b + 8) >> 4);
                    dpix0.Green = (sbyte)(((7 * (s10g + s20g)) + s11g + s21g + 8) >> 4);
                    dpix0.Red   = (sbyte)(((7 * (s10r + s20r)) + s11r + s21r + 8) >> 4);
                    dpix1.Blue  = (sbyte)((s12b + s22b + s11b + s21b + 2) >> 2);
                    dpix1.Green = (sbyte)((s12g + s22g + s11g + s21g + 2) >> 2);
                    dpix1.Red   = (sbyte)((s12r + s22r + s11r + s21r + 2) >> 2);
                    dpix2.Blue  = (sbyte)(((7 * (s13b + s23b)) + s12b + s22b + 8) >> 4);
                    dpix2.Green = (sbyte)(((7 * (s13g + s23g)) + s12g + s22g + 8) >> 4);
                    dpix2.Red   = (sbyte)(((7 * (s13r + s23r)) + s12r + s22r + 8) >> 4);

                    if (++dx < destwidth)
                    {
                        dpix0.IncOffset();
                        dpix1.IncOffset();
                        dpix2.IncOffset();
                    }

                    dpix0.Blue  = (sbyte)(((11 * s30b) + (2 * (s31b + s20b)) + s21b + 8) >> 4);
                    dpix0.Green = (sbyte)(((11 * s30g) + (2 * (s31g + s20g)) + s21g + 8) >> 4);
                    dpix0.Red   = (sbyte)(((11 * s30r) + (2 * (s31r + s20r)) + s21r + 8) >> 4);
                    dpix1.Blue  = (sbyte)(((7 * (s31b + s32b)) + s21b + s22b + 8) >> 4);
                    dpix1.Green = (sbyte)(((7 * (s31g + s32g)) + s21g + s22g + 8) >> 4);
                    dpix1.Red   = (sbyte)(((7 * (s31r + s32r)) + s21r + s22r + 8) >> 4);
                    dpix2.Blue  = (sbyte)(((11 * s33b) + (2 * (s32b + s23b)) + s22b + 8) >> 4);
                    dpix2.Green = (sbyte)(((11 * s33g) + (2 * (s32g + s23g)) + s22g + 8) >> 4);
                    dpix2.Red   = (sbyte)(((11 * s33r) + (2 * (s32r + s23r)) + s22r + 8) >> 4);

                    if (++dx < destwidth)
                    {
                        dpix0.IncOffset();
                        dpix1.IncOffset();
                        dpix2.IncOffset();
                    }
                }
            }
        }
예제 #6
0
        /// <summary> Fill this image from another source at reduced resolution.  Pixel
        /// averaging will be used.
        ///
        /// </summary>
        /// <param name="src">image map to reduce
        /// </param>
        /// <param name="subsample">rate to subsample
        /// </param>
        /// <param name="pdr">target bounds
        /// </param>
        public virtual void Downsample(Map src, int subsample, Rectangle pdr)
        {
            Rectangle rect = new Rectangle(0, 0, ((src.ImageWidth + subsample) - 1) / subsample,
                                           ((src.ImageHeight + subsample) - 1) / subsample);

            if (pdr != null)
            {
                if ((pdr.Right < rect.Right) || (pdr.Bottom < rect.Bottom) || (pdr.Left > rect.Left) || (pdr.Top > rect.Top))
                {
                    throw new ArgumentException("Specified rectangle overflows destination PixelMap");
                }
                rect = pdr;
            }

            Init(rect.Height, rect.Width, null);

            int sy   = rect.Bottom * subsample;
            int sxz  = rect.Right * subsample;
            int sidx = src.RowOffset(sy);
            int didx = 0;

            PixelReference sptr = src.CreateGPixelReference(0);
            PixelReference dptr = CreateGPixelReference(0);

            for (int y = 0; y < ImageHeight; y++)
            {
                int sx = sxz;

                for (int x = ImageWidth; x-- > 0; dptr.IncOffset())
                {
                    int r    = 0;
                    int g    = 0;
                    int b    = 0;
                    int s    = 0;
                    int kidx = sidx;
                    int lsy  = sy + subsample;

                    if (lsy > src.ImageHeight)
                    {
                        lsy = src.ImageHeight;
                    }

                    int lsx = sx + subsample;

                    if (lsx > src.ImageWidth)
                    {
                        lsx = src.ImageWidth;
                    }

                    for (int rsy = sy; rsy < lsy; rsy++)
                    {
                        sptr.SetOffset(kidx + sx);
                        if (!IsRampNeeded)
                        {
                            for (int rsx = lsx - sx; rsx-- > 0; sptr.IncOffset())
                            {
                                r += sptr.Red;
                                g += sptr.Green;
                                b += sptr.Blue;
                                s++;
                            }
                        }
                        else
                        {
                            for (int rsx = lsx - sx; rsx-- > 0; sptr.IncOffset())
                            {
                                Pixel pix = src.PixelRamp(sptr);
                                r += pix.Red;
                                g += pix.Green;
                                b += pix.Blue;
                                s++;
                            }
                        }

                        kidx += src.GetRowSize();
                    }

                    if (s >= _invmap.Length)
                    {
                        dptr.SetBGR(b / s, g / s, r / s);
                    }
                    else
                    {
                        dptr.SetBGR(((b * _invmap[s]) + 32768) >> 16, ((g * _invmap[s]) + 32768) >> 16,
                                    ((r * _invmap[s]) + 32768) >> 16);
                    }

                    sx += subsample;
                }

                sy   += subsample;
                sidx += src.RowOffset(subsample);
                dptr.SetOffset(didx += GetRowSize());
            }
        }
예제 #7
0
        /// <summary> Initialize this PixelMap from another image map.
        ///
        /// </summary>
        /// <param name="ref">image map to initialize from
        ///
        /// </param>
        /// <returns> the initialized PixelMap
        /// </returns>
        public virtual PixelMap Init(Map ref_Renamed)
        {
            Init(ref_Renamed.ImageHeight, ref_Renamed.ImageWidth, ((null)));

            PixelReference pixel = CreateGPixelReference(0);

            if ((ImageHeight > 0) && (ImageWidth > 0))
            {
                PixelReference refPixel = (ref_Renamed).CreateGPixelReference(0);

                for (int y = 0; y < ImageHeight; y++)
                {
                    pixel.SetOffset(y, 0);
                    refPixel.SetOffset(y, 0);

                    if (!IsRampNeeded)
                    {
                        for (int x = ImageWidth; x-- > 0; pixel.IncOffset(), refPixel.IncOffset())
                        {
                            pixel.CopyFrom(refPixel);
                        }
                    }
                    else
                    {
                        for (int x = ImageWidth; x-- > 0; pixel.IncOffset(), refPixel.IncOffset())
                        {
                            pixel.CopyFrom(ref_Renamed.PixelRamp(refPixel));
                        }
                    }
                }
            }

            return this;
        }
예제 #8
0
        /// <summary> Initialize this PixelMap from a segment of another image map.
        ///
        /// </summary>
        /// <param name="ref">image map to initialize from
        /// </param>
        /// <param name="rect">bounding rectangle to initialize from
        ///
        /// </param>
        /// <returns> the initialized PixelMap
        /// </returns>
        public virtual PixelMap Init(Map ref_Renamed, Rectangle rect)
        {
            Init(rect.Height, rect.Width, ((null)));

            Rectangle rect2 = new Rectangle(0, 0, ref_Renamed.ImageWidth, ref_Renamed.ImageHeight);
            rect2.Intersect(rect2, rect);
            rect2.Translate(-rect.Right, -rect.Bottom);

            if (!rect2.Empty)
            {
                PixelReference pixel = CreateGPixelReference(0);
                PixelReference refPixel = ref_Renamed.CreateGPixelReference(0);

                for (int y = rect2.Bottom; y < rect2.Top; y++)
                {
                    pixel.SetOffset(y, rect2.Right);
                    refPixel.SetOffset(y + rect.Bottom, rect.Right + rect2.Right);

                    if (!IsRampNeeded)
                    {
                        for (int x = rect2.Left - rect2.Right; x-- > 0; pixel.IncOffset(), refPixel.IncOffset())
                        {
                            pixel.CopyFrom(refPixel);
                        }
                    }
                    else
                    {
                        for (int x = rect2.Left - rect2.Right; x-- > 0; pixel.IncOffset(), refPixel.IncOffset())
                        {
                            pixel.CopyFrom(ref_Renamed.PixelRamp(refPixel));
                        }
                    }
                }
            }

            return this;
        }
예제 #9
0
        /// <summary> Insert the reference map at the specified location.
        ///
        /// </summary>
        /// <param name="ref">map to insert
        /// </param>
        /// <param name="dx">horizontal position to insert at
        /// </param>
        /// <param name="dy">vertical position to insert at
        /// </param>
        public override void Fill(Map ref_Renamed, int dx, int dy)
        {
            int x0 = (dx > 0) ? dx : 0;
            int y0 = (dy > 0) ? dy : 0;
            int x1 = (dx < 0) ? (-dx) : 0;
            int y1 = (dy < 0) ? (-dy) : 0;
            int w0 = ImageWidth - x0;
            int w1 = ref_Renamed.ImageWidth - x1;
            int w = (w0 < w1) ? w0 : w1;
            int h0 = ImageHeight - y0;
            int h1 = ref_Renamed.ImageHeight - y1;
            int h = (h0 < h1) ? h0 : h1;

            if ((w > 0) && (h > 0))
            {
                PixelReference pixel = CreateGPixelReference(0);
                PixelReference refPixel = ref_Renamed.CreateGPixelReference(0);

                do
                {
                    pixel.SetOffset(y0++, x0);
                    refPixel.SetOffset(y1++, x1);

                    if (!IsRampNeeded)
                    {
                        pixel.SetPixels(refPixel, w);
                    }
                    else
                    {
                        int i = w;
                        do
                        {
                            pixel.CopyFrom(ref_Renamed.PixelRamp(refPixel));
                            pixel.IncOffset();
                            refPixel.IncOffset();
                        } while (--i > 0);
                    }
                } while (--h > 0);
            }
        }
예제 #10
0
        /// <summary> Fill this image from another source at reduced resolution of 4 vertical
        /// pixels to 3.  An extrapulating pixel averaging algorithm is used.
        ///
        /// </summary>
        /// <param name="src">image map to reduce
        /// </param>
        /// <param name="pdr">target bounds
        ///
        /// </param>
        /// <throws>  IllegalArgumentException if the target rectangle is out of bounds </throws>
        public virtual void Downsample43(Map src, Rectangle pdr)
        {
            int srcwidth = src.ImageWidth;
            int srcheight = src.ImageHeight;
            //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
            int destwidth = (int)Math.Ceiling(srcwidth * 0.75D);
            //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
            int destheight = (int)Math.Ceiling(srcheight * 0.75D);
            Rectangle rect = new Rectangle(0, 0, destwidth, destheight);

            if (pdr != null)
            {
                if ((pdr.Right < rect.Right) || (pdr.Bottom < rect.Bottom) || (pdr.Left > rect.Left) || (pdr.Top > rect.Top))
                {
                    throw new ArgumentException("rectangle out of bounds" + "pdr=(" + pdr.Right + "," + pdr.Bottom + "," +
                                                pdr.Left + "," + pdr.Top + "),rect=(" + rect.Right + "," + rect.Bottom +
                                                "," + rect.Left + "," + rect.Top + ")");
                }

                rect = pdr;
                destwidth = rect.Width;
                destheight = rect.Height;
            }

            Init(destheight, destwidth, null);

            int sy = rect.Bottom / 3;
            int dy = rect.Bottom - (3 * sy);

            //    if(dy < 0)
            //    {
            //      sy--;
            //      dy += 3;
            //    }

            int sxz = rect.Right / 3;
            int dxz = rect.Right - (3 * sxz);

            if (dxz < 0)
            {
                sxz--;
                dxz += 3;
            }

            sxz *= 4;
            sy *= 4;

            PixelReference spix0 = src.CreateGPixelReference(0);
            PixelReference spix1 = src.CreateGPixelReference(0);
            PixelReference spix2 = src.CreateGPixelReference(0);
            PixelReference spix3 = src.CreateGPixelReference(0);
            PixelReference dpix0 = CreateGPixelReference(0);
            PixelReference dpix1 = CreateGPixelReference(0);
            PixelReference dpix2 = CreateGPixelReference(0);
            while (dy < destheight)
            {
                spix0.SetOffset(sy++, sxz);

                if (sy >= srcheight)
                {
                    sy--;
                }

                spix1.SetOffset(sy++, sxz);

                if (sy >= srcheight)
                {
                    sy--;
                }

                spix2.SetOffset(sy++, sxz);

                if (sy >= srcheight)
                {
                    sy--;
                }

                spix3.SetOffset(sy++, sxz);

                dpix0.SetOffset((dy < 0) ? 0 : dy, dxz);

                if (++dy >= destheight)
                {
                    dy--;
                }

                dpix1.SetOffset((dy < 0) ? 0 : dy, dxz);

                if (++dy >= destheight)
                {
                    dy--;
                }

                dpix2.SetOffset(dy++, dxz);
                int dx = dxz;
                int sx = sxz;

                Pixel pix0 = src.PixelRamp(spix0);
                Pixel pix1 = src.PixelRamp(spix1);
                Pixel pix2 = src.PixelRamp(spix2);
                Pixel pix3 = src.PixelRamp(spix3);
                while (dx < destwidth)
                {
                    int s00b = pix0.Blue;
                    int s00g = pix0.Green;
                    int s00r = pix0.Red;
                    int s01b = pix1.Blue;
                    int s01g = pix1.Green;
                    int s01r = pix1.Red;
                    int s02b = pix2.Blue;
                    int s02g = pix2.Green;
                    int s02r = pix2.Red;
                    int s03b = pix3.Blue;
                    int s03g = pix3.Green;
                    int s03r = pix3.Red;

                    if (++sx < srcwidth)
                    {
                        spix0.IncOffset();
                        spix1.IncOffset();
                        spix2.IncOffset();
                        spix3.IncOffset();
                        pix0 = src.PixelRamp(spix0);
                        pix1 = src.PixelRamp(spix1);
                        pix2 = src.PixelRamp(spix2);
                        pix3 = src.PixelRamp(spix3);
                    }

                    int s10b = pix0.Blue;
                    int s10g = pix0.Green;
                    int s10r = pix0.Red;
                    int s11b = pix1.Blue;
                    int s11g = pix1.Green;
                    int s11r = pix1.Red;
                    int s12b = pix2.Blue;
                    int s12g = pix2.Green;
                    int s12r = pix2.Red;
                    int s13b = pix3.Blue;
                    int s13g = pix3.Green;
                    int s13r = pix3.Red;

                    if (++sx < srcwidth)
                    {
                        spix0.IncOffset();
                        spix1.IncOffset();
                        spix2.IncOffset();
                        spix3.IncOffset();
                        pix0 = src.PixelRamp(spix0);
                        pix1 = src.PixelRamp(spix1);
                        pix2 = src.PixelRamp(spix2);
                        pix3 = src.PixelRamp(spix3);
                    }

                    int s20b = pix0.Blue;
                    int s20g = pix0.Green;
                    int s20r = pix0.Red;
                    int s21b = pix1.Blue;
                    int s21g = pix1.Green;
                    int s21r = pix1.Red;
                    int s22b = pix2.Blue;
                    int s22g = pix2.Green;
                    int s22r = pix2.Red;
                    int s23b = pix3.Blue;
                    int s23g = pix3.Green;
                    int s23r = pix3.Red;

                    if (++sx < srcwidth)
                    {
                        spix0.IncOffset();
                        spix1.IncOffset();
                        spix2.IncOffset();
                        spix3.IncOffset();
                        pix0 = src.PixelRamp(spix0);
                        pix1 = src.PixelRamp(spix1);
                        pix2 = src.PixelRamp(spix2);
                        pix3 = src.PixelRamp(spix3);
                    }

                    int s30b = pix0.Blue;
                    int s30g = pix0.Green;
                    int s30r = pix0.Red;
                    int s31b = pix1.Blue;
                    int s31g = pix1.Green;
                    int s31r = pix1.Red;
                    int s32b = pix2.Blue;
                    int s32g = pix2.Green;
                    int s32r = pix2.Red;
                    int s33b = pix3.Blue;
                    int s33g = pix3.Green;
                    int s33r = pix3.Red;

                    if (++sx < srcwidth)
                    {
                        spix0.IncOffset();
                        spix1.IncOffset();
                        spix2.IncOffset();
                        spix3.IncOffset();
                        pix0 = src.PixelRamp(spix0);
                        pix1 = src.PixelRamp(spix1);
                        pix2 = src.PixelRamp(spix2);
                        pix3 = src.PixelRamp(spix3);
                    }

                    dpix0.Blue = (sbyte)(((11 * s00b) + (2 * (s01b + s10b)) + s11b + 8) >> 4);
                    dpix0.Green = (sbyte)(((11 * s00g) + (2 * (s01g + s10g)) + s11g + 8) >> 4);
                    dpix0.Red = (sbyte)(((11 * s00r) + (2 * (s01r + s10r)) + s11r + 8) >> 4);
                    dpix1.Blue = (sbyte)(((7 * (s01b + s02b)) + s11b + s12b + 8) >> 4);
                    dpix1.Green = (sbyte)(((7 * (s01g + s02g)) + s11g + s12g + 8) >> 4);
                    dpix1.Red = (sbyte)(((7 * (s01r + s02r)) + s11r + s12r + 8) >> 4);
                    dpix2.Blue = (sbyte)(((11 * s03b) + (2 * (s02b + s13b)) + s12b + 8) >> 4);
                    dpix2.Green = (sbyte)(((11 * s03g) + (2 * (s02g + s13g)) + s12g + 8) >> 4);
                    dpix2.Red = (sbyte)(((11 * s03r) + (2 * (s02r + s13r)) + s12r + 8) >> 4);

                    if (++dx < destwidth)
                    {
                        dpix0.IncOffset();
                        dpix1.IncOffset();
                        dpix2.IncOffset();
                    }

                    dpix0.Blue = (sbyte)(((7 * (s10b + s20b)) + s11b + s21b + 8) >> 4);
                    dpix0.Green = (sbyte)(((7 * (s10g + s20g)) + s11g + s21g + 8) >> 4);
                    dpix0.Red = (sbyte)(((7 * (s10r + s20r)) + s11r + s21r + 8) >> 4);
                    dpix1.Blue = (sbyte)((s12b + s22b + s11b + s21b + 2) >> 2);
                    dpix1.Green = (sbyte)((s12g + s22g + s11g + s21g + 2) >> 2);
                    dpix1.Red = (sbyte)((s12r + s22r + s11r + s21r + 2) >> 2);
                    dpix2.Blue = (sbyte)(((7 * (s13b + s23b)) + s12b + s22b + 8) >> 4);
                    dpix2.Green = (sbyte)(((7 * (s13g + s23g)) + s12g + s22g + 8) >> 4);
                    dpix2.Red = (sbyte)(((7 * (s13r + s23r)) + s12r + s22r + 8) >> 4);

                    if (++dx < destwidth)
                    {
                        dpix0.IncOffset();
                        dpix1.IncOffset();
                        dpix2.IncOffset();
                    }

                    dpix0.Blue = (sbyte)(((11 * s30b) + (2 * (s31b + s20b)) + s21b + 8) >> 4);
                    dpix0.Green = (sbyte)(((11 * s30g) + (2 * (s31g + s20g)) + s21g + 8) >> 4);
                    dpix0.Red = (sbyte)(((11 * s30r) + (2 * (s31r + s20r)) + s21r + 8) >> 4);
                    dpix1.Blue = (sbyte)(((7 * (s31b + s32b)) + s21b + s22b + 8) >> 4);
                    dpix1.Green = (sbyte)(((7 * (s31g + s32g)) + s21g + s22g + 8) >> 4);
                    dpix1.Red = (sbyte)(((7 * (s31r + s32r)) + s21r + s22r + 8) >> 4);
                    dpix2.Blue = (sbyte)(((11 * s33b) + (2 * (s32b + s23b)) + s22b + 8) >> 4);
                    dpix2.Green = (sbyte)(((11 * s33g) + (2 * (s32g + s23g)) + s22g + 8) >> 4);
                    dpix2.Red = (sbyte)(((11 * s33r) + (2 * (s32r + s23r)) + s22r + 8) >> 4);

                    if (++dx < destwidth)
                    {
                        dpix0.IncOffset();
                        dpix1.IncOffset();
                        dpix2.IncOffset();
                    }
                }
            }
        }
예제 #11
0
        /// <summary> Fill this image from another source at reduced resolution.  Pixel
        /// averaging will be used.
        ///
        /// </summary>
        /// <param name="src">image map to reduce
        /// </param>
        /// <param name="subsample">rate to subsample
        /// </param>
        /// <param name="pdr">target bounds
        /// </param>
        public virtual void Downsample(Map src, int subsample, Rectangle pdr)
        {
            Rectangle rect = new Rectangle(0, 0, ((src.ImageWidth + subsample) - 1) / subsample,
                                   ((src.ImageHeight + subsample) - 1) / subsample);

            if (pdr != null)
            {
                if ((pdr.Right < rect.Right) || (pdr.Bottom < rect.Bottom) || (pdr.Left > rect.Left) || (pdr.Top > rect.Top))
                {
                    throw new ArgumentException("Specified rectangle overflows destination PixelMap");
                }
                rect = pdr;
            }

            Init(rect.Height, rect.Width, null);

            int sy = rect.Bottom * subsample;
            int sxz = rect.Right * subsample;
            int sidx = src.RowOffset(sy);
            int didx = 0;

            PixelReference sptr = src.CreateGPixelReference(0);
            PixelReference dptr = CreateGPixelReference(0);

            for (int y = 0; y < ImageHeight; y++)
            {
                int sx = sxz;

                for (int x = ImageWidth; x-- > 0; dptr.IncOffset())
                {
                    int r = 0;
                    int g = 0;
                    int b = 0;
                    int s = 0;
                    int kidx = sidx;
                    int lsy = sy + subsample;

                    if (lsy > src.ImageHeight)
                    {
                        lsy = src.ImageHeight;
                    }

                    int lsx = sx + subsample;

                    if (lsx > src.ImageWidth)
                    {
                        lsx = src.ImageWidth;
                    }

                    for (int rsy = sy; rsy < lsy; rsy++)
                    {
                        sptr.SetOffset(kidx + sx);
                        if (!IsRampNeeded)
                        {
                            for (int rsx = lsx - sx; rsx-- > 0; sptr.IncOffset())
                            {
                                r += sptr.Red;
                                g += sptr.Green;
                                b += sptr.Blue;
                                s++;
                            }
                        }
                        else
                        {
                            for (int rsx = lsx - sx; rsx-- > 0; sptr.IncOffset())
                            {
                                Pixel pix = src.PixelRamp(sptr);
                                r += pix.Red;
                                g += pix.Green;
                                b += pix.Blue;
                                s++;
                            }
                        }

                        kidx += src.GetRowSize();
                    }

                    if (s >= _invmap.Length)
                    {
                        dptr.SetBGR(b / s, g / s, r / s);
                    }
                    else
                    {
                        dptr.SetBGR(((b * _invmap[s]) + 32768) >> 16, ((g * _invmap[s]) + 32768) >> 16,
                                    ((r * _invmap[s]) + 32768) >> 16);
                    }

                    sx += subsample;
                }

                sy += subsample;
                sidx += src.RowOffset(subsample);
                dptr.SetOffset(didx += GetRowSize());
            }
        }