Exemplo n.º 1
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="image">Source image data.</param>
        /// <param name="overlay">Overlay image data.</param>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage image, UnmanagedImage overlay)
        {
            // get image dimension
            int width  = image.Width;
            int height = image.Height;

            // width and height to process
            int widthToProcess  = width;
            int heightToProcess = height;

            // if generator was specified, then generate a texture
            // otherwise use provided texture
            if (textureGenerator != null)
            {
                texture = textureGenerator.Generate(width, height);
            }
            else
            {
                widthToProcess  = Math.Min(width, texture.GetLength(1));
                heightToProcess = Math.Min(height, texture.GetLength(0));
            }

            int pixelSize = Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int srcOffset = image.Stride - widthToProcess * pixelSize;
            int ovrOffset = overlay.Stride - widthToProcess * pixelSize;

            // do the job
            byte *ptr = (byte *)image.ImageData.ToPointer( );
            byte *ovr = (byte *)overlay.ImageData.ToPointer( );

            // for each line
            for (int y = 0; y < heightToProcess; y++)
            {
                // for each pixel
                for (int x = 0; x < widthToProcess; x++)
                {
                    double t1 = texture[y, x];
                    double t2 = 1 - t1;

                    for (int i = 0; i < pixelSize; i++, ptr++, ovr++)
                    {
                        *ptr = (byte)Math.Min(255.0f, *ptr * t1 + *ovr * t2);
                    }
                }
                ptr += srcOffset;
                ovr += ovrOffset;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="image">Source image data.</param>
        /// <param name="rect">Image rectangle for processing by the filter.</param>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int pixelSize = Image.GetPixelFormatSize(image.PixelFormat) / 8;

            // processing width and height
            int width  = rect.Width;
            int height = rect.Height;

            // processing region's dimension
            int widthToProcess  = width;
            int heightToProcess = height;

            // if generator was specified, then generate a texture
            // otherwise use provided texture
            if (textureGenerator != null)
            {
                texture = textureGenerator.Generate(width, height);
            }
            else
            {
                widthToProcess  = Math.Min(width, texture.GetLength(1));
                heightToProcess = Math.Min(height, texture.GetLength(0));
            }

            int offset = image.Stride - widthToProcess * pixelSize;

            // do the job
            byte *ptr = (byte *)image.ImageData.ToPointer( );

            // allign pointer to the first pixel to process
            ptr += (rect.Top * image.Stride + rect.Left * pixelSize);

            // texture
            for (int y = 0; y < heightToProcess; y++)
            {
                for (int x = 0; x < widthToProcess; x++)
                {
                    double t = texture[y, x];
                    // process each pixel
                    for (int i = 0; i < pixelSize; i++, ptr++)
                    {
                        *ptr = (byte)Math.Min(255.0f, (preserveLevel * (*ptr)) + (filterLevel * (*ptr)) * t);
                    }
                }
                ptr += offset;
            }
        }