private void ConvertToRGB()
        {
            int size = DstW * DstH;

            int[] newpixels = new int[size];
            if (BytePixels != null)
            {
                for (int i = 0; i < size; i++)
                {
                    newpixels[i] = ImageModel.GetRGB(BytePixels[i] & 0xff);
                }
            }
            else if (IntPixels != null)
            {
                for (int i = 0; i < size; i++)
                {
                    newpixels[i] = ImageModel.GetRGB(IntPixels[i]);
                }
            }
            BytePixels = null;
            IntPixels  = newpixels;
            DstScan    = DstW;
            DstOff     = 0;
            ImageModel = ColorModel.RGBdefault;
        }
Exemple #2
0
        private void AccumPixels(int x, int y, int w, int h, ColorModel model, Object pixels, int off, int scansize)
        {
            if (Reds == null)
            {
                MakeAccumBuffers();
            }
            int sy = y;
            int syrem = DestHeight;
            int dy, dyrem;

            if (sy == 0)
            {
                dy    = 0;
                dyrem = 0;
            }
            else
            {
                dy    = Savedy;
                dyrem = Savedyrem;
            }
            while (sy < y + h)
            {
                int amty;
                if (dyrem == 0)
                {
                    for (int i = 0; i < DestWidth; i++)
                    {
                        Alphas[i] = Reds[i] = Greens[i] = Blues[i] = 0f;
                    }
                    dyrem = SrcHeight;
                }
                if (syrem < dyrem)
                {
                    amty = syrem;
                }
                else
                {
                    amty = dyrem;
                }
                int   sx = 0;
                int   dx = 0;
                int   sxrem = 0;
                int   dxrem = SrcWidth;
                float a = 0f, r = 0f, g = 0f, b = 0f;
                while (sx < w)
                {
                    if (sxrem == 0)
                    {
                        sxrem = DestWidth;
                        int rgb;
                        if (pixels is sbyte[])
                        {
                            rgb = ((sbyte[])pixels)[off + sx] & 0xff;
                        }
                        else
                        {
                            rgb = ((int[])pixels)[off + sx];
                        }
                        // getRGB() always returns non-premultiplied components
                        rgb = model.GetRGB(rgb);
                        a   = (int)((uint)rgb >> 24);
                        r   = (rgb >> 16) & 0xff;
                        g   = (rgb >> 8) & 0xff;
                        b   = rgb & 0xff;
                        // premultiply the components if necessary
                        if (a != 255.0f)
                        {
                            float ascale = a / 255.0f;
                            r *= ascale;
                            g *= ascale;
                            b *= ascale;
                        }
                    }
                    int amtx;
                    if (sxrem < dxrem)
                    {
                        amtx = sxrem;
                    }
                    else
                    {
                        amtx = dxrem;
                    }
                    float mult = ((float)amtx) * amty;
                    Alphas[dx] += mult * a;
                    Reds[dx]   += mult * r;
                    Greens[dx] += mult * g;
                    Blues[dx]  += mult * b;
                    if ((sxrem -= amtx) == 0)
                    {
                        sx++;
                    }
                    if ((dxrem -= amtx) == 0)
                    {
                        dx++;
                        dxrem = SrcWidth;
                    }
                }
                if ((dyrem -= amty) == 0)
                {
                    int[] outpix = CalcRow();
                    do
                    {
                        Consumer.SetPixels(0, dy, DestWidth, 1, Rgbmodel, outpix, 0, DestWidth);
                        dy++;
                    } while ((syrem -= amty) >= amty && amty == SrcHeight);
                }
                else
                {
                    syrem -= amty;
                }
                if (syrem == 0)
                {
                    syrem = DestHeight;
                    sy++;
                    off += scansize;
                }
            }
            Savedyrem = dyrem;
            Savedy    = dy;
        }
        /// <summary>
        /// The setPixels method is part of the ImageConsumer API which
        /// this class must implement to retrieve the pixels.
        /// <para>
        /// Note: This method is intended to be called by the ImageProducer
        /// of the Image whose pixels are being grabbed.  Developers using
        /// this class to retrieve pixels from an image should avoid calling
        /// this method directly since that operation could result in problems
        /// with retrieving the requested pixels.
        /// </para>
        /// </summary>
        /// <param name="srcX"> the X coordinate of the upper-left corner
        ///        of the area of pixels to be set </param>
        /// <param name="srcY"> the Y coordinate of the upper-left corner
        ///        of the area of pixels to be set </param>
        /// <param name="srcW"> the width of the area of pixels </param>
        /// <param name="srcH"> the height of the area of pixels </param>
        /// <param name="model"> the specified <code>ColorModel</code> </param>
        /// <param name="pixels"> the array of pixels </param>
        /// <param name="srcOff"> the offset into the pixels array </param>
        /// <param name="srcScan"> the distance from one row of pixels to the next
        ///        in the pixels array </param>
        /// <seealso cref= #getPixels </seealso>
        public virtual void SetPixels(int srcX, int srcY, int srcW, int srcH, ColorModel model, int[] pixels, int srcOff, int srcScan)
        {
            if (srcY < DstY)
            {
                int diff = DstY - srcY;
                if (diff >= srcH)
                {
                    return;
                }
                srcOff += srcScan * diff;
                srcY   += diff;
                srcH   -= diff;
            }
            if (srcY + srcH > DstY + DstH)
            {
                srcH = (DstY + DstH) - srcY;
                if (srcH <= 0)
                {
                    return;
                }
            }
            if (srcX < DstX)
            {
                int diff = DstX - srcX;
                if (diff >= srcW)
                {
                    return;
                }
                srcOff += diff;
                srcX   += diff;
                srcW   -= diff;
            }
            if (srcX + srcW > DstX + DstW)
            {
                srcW = (DstX + DstW) - srcX;
                if (srcW <= 0)
                {
                    return;
                }
            }
            if (IntPixels == null)
            {
                if (BytePixels == null)
                {
                    IntPixels  = new int[DstW * DstH];
                    DstScan    = DstW;
                    DstOff     = 0;
                    ImageModel = model;
                }
                else
                {
                    ConvertToRGB();
                }
            }
            int dstPtr = DstOff + (srcY - DstY) * DstScan + (srcX - DstX);

            if (ImageModel == model)
            {
                for (int h = srcH; h > 0; h--)
                {
                    System.Array.Copy(pixels, srcOff, IntPixels, dstPtr, srcW);
                    srcOff += srcScan;
                    dstPtr += DstScan;
                }
            }
            else
            {
                if (ImageModel != ColorModel.RGBdefault)
                {
                    ConvertToRGB();
                }
                int dstRem = DstScan - srcW;
                int srcRem = srcScan - srcW;
                for (int h = srcH; h > 0; h--)
                {
                    for (int w = srcW; w > 0; w--)
                    {
                        IntPixels[dstPtr++] = model.GetRGB(pixels[srcOff++]);
                    }
                    srcOff += srcRem;
                    dstPtr += dstRem;
                }
            }
            Flags |= ImageObserver_Fields.SOMEBITS;
        }