예제 #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;
            }
        }
예제 #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;
            }
        }
예제 #3
0
        public Image process(Image imageIn)
        {
            // get source image size
            int width  = imageIn.getWidth();
            int height = imageIn.getHeight();

            // 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 r, g, b;

            // texture
            for (int y = 0; y < heightToProcess; y++)
            {
                for (int x = 0; x < widthToProcess; x++)
                {
                    double t = texture[y, x];
                    r = imageIn.getRComponent(x, y);
                    g = imageIn.getGComponent(x, y);
                    b = imageIn.getBComponent(x, y);
                    // process each pixel

                    r = (byte)Math.Min(255.0f, (preserveLevel * r) + (filterLevel * r) * t);
                    g = (byte)Math.Min(255.0f, (preserveLevel * g) + (filterLevel * g) * t);
                    b = (byte)Math.Min(255.0f, (preserveLevel * b) + (filterLevel * b) * t);
                    imageIn.setPixelColor(x, y, r, g, b);
                }
            }
            return(imageIn);
        }
예제 #4
0
        protected unsafe override void ProcessFilter(UnmanagedImage image, UnmanagedImage overlay)
        {
            int width  = image.Width;
            int height = image.Height;
            int num    = width;
            int num2   = height;

            if (textureGenerator != null)
            {
                texture = textureGenerator.Generate(width, height);
            }
            else
            {
                num  = System.Math.Min(width, texture.GetLength(1));
                num2 = System.Math.Min(height, texture.GetLength(0));
            }
            int   num3 = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int   num4 = image.Stride - num * num3;
            int   num5 = overlay.Stride - num * num3;
            byte *ptr  = (byte *)image.ImageData.ToPointer();
            byte *ptr2 = (byte *)overlay.ImageData.ToPointer();

            for (int i = 0; i < num2; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    double num6 = texture[i, j];
                    double num7 = 1.0 - num6;
                    int    num8 = 0;
                    while (num8 < num3)
                    {
                        *ptr = (byte)System.Math.Min(255.0, (double)(int)(*ptr) * num6 + (double)(int)(*ptr2) * num7);
                        num8++;
                        ptr++;
                        ptr2++;
                    }
                }
                ptr  += num4;
                ptr2 += num5;
            }
        }
예제 #5
0
        // Generate and show texture
        private void ShowTexture( )
        {
            // check generator
            if (textureGenerator == null)
            {
                pictureBox.Image = null;
                return;
            }

            int width  = pictureBox.ClientRectangle.Width;
            int height = pictureBox.ClientRectangle.Height;

            // generate texture
            float[,] texture = textureGenerator.Generate(width, height);

            // create bitmap from the texture
            Bitmap image = TextureTools.ToBitmap(texture);

            // show image
            pictureBox.Image = image;
        }
예제 #6
0
        protected unsafe override void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int num    = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int width  = rect.Width;
            int height = rect.Height;
            int num2   = width;
            int num3   = height;

            if (textureGenerator != null)
            {
                texture = textureGenerator.Generate(width, height);
            }
            else
            {
                num2 = System.Math.Min(width, texture.GetLength(1));
                num3 = System.Math.Min(height, texture.GetLength(0));
            }
            int   num4 = image.Stride - num2 * num;
            byte *ptr  = (byte *)image.ImageData.ToPointer();

            ptr += rect.Top * image.Stride + rect.Left * num;
            for (int i = 0; i < num3; i++)
            {
                for (int j = 0; j < num2; j++)
                {
                    double num5 = texture[i, j];
                    int    num6 = 0;
                    while (num6 < num)
                    {
                        *ptr = (byte)System.Math.Min(255.0, preserveLevel * (double)(int)(*ptr) + filterLevel * (double)(int)(*ptr) * num5);
                        num6++;
                        ptr++;
                    }
                }
                ptr += num4;
            }
        }
예제 #7
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="sourceData">Source image data.</param>
        /// <param name="destinationData">Destination image data.</param>
        ///
        /// <exception cref="InvalidImagePropertiesException">Texture size does not match image size.</exception>
        /// <exception cref="ApplicationException">Filters should not change image dimension.</exception>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            // get source image dimension
            int width  = sourceData.Width;
            int height = sourceData.Height;

            // if generator was specified, then generate a texture
            // otherwise use provided texture
            if (textureGenerator != null)
            {
                texture = textureGenerator.Generate(width, height);
            }
            else
            {
                // check existing texture
                if ((texture.GetLength(0) != height) || (texture.GetLength(1) != width))
                {
                    // sorry, but source image must have the same dimension as texture
                    throw new InvalidImagePropertiesException("Texture size does not match image size.");
                }
            }

            // apply first filter
            UnmanagedImage filteredImage1 = filter1.Apply(sourceData);

            // check size of the result image
            if ((width != filteredImage1.Width) || (height != filteredImage1.Height))
            {
                filteredImage1.Dispose( );
                throw new ApplicationException("Filters should not change image dimension.");
            }

            // convert 1st image to RGB if required
            if (filteredImage1.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                GrayscaleToRGB coloringFilter = new GrayscaleToRGB( );
                UnmanagedImage temp           = coloringFilter.Apply(filteredImage1);
                filteredImage1.Dispose( );
                filteredImage1 = temp;
            }

            UnmanagedImage filteredImage2 = null;

            // apply second filter, if it was specified
            if (filter2 != null)
            {
                filteredImage2 = filter2.Apply(sourceData);
                // check size of the result image
                if ((width != filteredImage2.Width) || (height != filteredImage2.Height))
                {
                    filteredImage1.Dispose( );
                    filteredImage2.Dispose( );
                    // we are not handling such situations yet
                    throw new ApplicationException("Filters should not change image dimension.");
                }

                // convert 2nd image to RGB if required
                if (filteredImage2.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    GrayscaleToRGB coloringFilter = new GrayscaleToRGB( );
                    UnmanagedImage temp           = coloringFilter.Apply(filteredImage2);
                    filteredImage2.Dispose( );
                    filteredImage2 = temp;
                }
            }

            // use source image as a second image, if second filter is not set
            if (filteredImage2 == null)
            {
                filteredImage2 = sourceData;
            }

            // do the job
            unsafe
            {
                byte *dst  = (byte *)destinationData.ImageData.ToPointer( );
                byte *src1 = (byte *)filteredImage1.ImageData.ToPointer( );
                byte *src2 = (byte *)filteredImage2.ImageData.ToPointer( );

                int dstOffset  = destinationData.Stride - 3 * width;
                int src1Offset = filteredImage1.Stride - 3 * width;
                int src2Offset = filteredImage2.Stride - 3 * width;

                if (preserveLevel != 0.0)
                {
                    // for each line
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++)
                        {
                            double t1 = texture[y, x];
                            double t2 = 1 - t1;

                            for (int i = 0; i < 3; i++, src1++, src2++, dst++)
                            {
                                *dst = (byte)Math.Min(255.0f,
                                                      filterLevel * (t1 * (*src1) + t2 * (*src2)) +
                                                      preserveLevel * (*src2));
                            }
                        }
                        src1 += src1Offset;
                        src2 += src2Offset;
                        dst  += dstOffset;
                    }
                }
                else
                {
                    // for each line
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++)
                        {
                            double t1 = texture[y, x];
                            double t2 = 1 - t1;

                            for (int i = 0; i < 3; i++, src1++, src2++, dst++)
                            {
                                *dst = (byte)Math.Min(255.0f, t1 * *src1 + t2 * *src2);
                            }
                        }
                        src1 += src1Offset;
                        src2 += src2Offset;
                        dst  += dstOffset;
                    }
                }
            }

            // dispose temp images
            filteredImage1.Dispose( );
            if (filteredImage2 != sourceData)
            {
                filteredImage2.Dispose( );
            }
        }
예제 #8
0
        protected unsafe override void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            int width  = sourceData.Width;
            int height = sourceData.Height;

            if (textureGenerator != null)
            {
                texture = textureGenerator.Generate(width, height);
            }
            else if (texture.GetLength(0) != height || texture.GetLength(1) != width)
            {
                throw new InvalidImagePropertiesException("Texture size does not match image size.");
            }
            UnmanagedImage unmanagedImage = filter1.Apply(sourceData);

            if (width != unmanagedImage.Width || height != unmanagedImage.Height)
            {
                unmanagedImage.Dispose();
                throw new ApplicationException("Filters should not change image dimension.");
            }
            if (unmanagedImage.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                GrayscaleToRGB grayscaleToRGB  = new GrayscaleToRGB();
                UnmanagedImage unmanagedImage2 = grayscaleToRGB.Apply(unmanagedImage);
                unmanagedImage.Dispose();
                unmanagedImage = unmanagedImage2;
            }
            UnmanagedImage unmanagedImage3 = null;

            if (filter2 != null)
            {
                unmanagedImage3 = filter2.Apply(sourceData);
                if (width != unmanagedImage3.Width || height != unmanagedImage3.Height)
                {
                    unmanagedImage.Dispose();
                    unmanagedImage3.Dispose();
                    throw new ApplicationException("Filters should not change image dimension.");
                }
                if (unmanagedImage3.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    GrayscaleToRGB grayscaleToRGB2 = new GrayscaleToRGB();
                    UnmanagedImage unmanagedImage4 = grayscaleToRGB2.Apply(unmanagedImage3);
                    unmanagedImage3.Dispose();
                    unmanagedImage3 = unmanagedImage4;
                }
            }
            if (unmanagedImage3 == null)
            {
                unmanagedImage3 = sourceData;
            }
            byte *ptr  = (byte *)destinationData.ImageData.ToPointer();
            byte *ptr2 = (byte *)unmanagedImage.ImageData.ToPointer();
            byte *ptr3 = (byte *)unmanagedImage3.ImageData.ToPointer();
            int   num  = destinationData.Stride - 3 * width;
            int   num2 = unmanagedImage.Stride - 3 * width;
            int   num3 = unmanagedImage3.Stride - 3 * width;

            if (preserveLevel != 0.0)
            {
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        double num4 = texture[i, j];
                        double num5 = 1.0 - num4;
                        int    num6 = 0;
                        while (num6 < 3)
                        {
                            *ptr = (byte)System.Math.Min(255.0, filterLevel * (num4 * (double)(int)(*ptr2) + num5 * (double)(int)(*ptr3)) + preserveLevel * (double)(int)(*ptr3));
                            num6++;
                            ptr2++;
                            ptr3++;
                            ptr++;
                        }
                    }
                    ptr2 += num2;
                    ptr3 += num3;
                    ptr  += num;
                }
            }
            else
            {
                for (int k = 0; k < height; k++)
                {
                    for (int l = 0; l < width; l++)
                    {
                        double num7 = texture[k, l];
                        double num8 = 1.0 - num7;
                        int    num9 = 0;
                        while (num9 < 3)
                        {
                            *ptr = (byte)System.Math.Min(255.0, num7 * (double)(int)(*ptr2) + num8 * (double)(int)(*ptr3));
                            num9++;
                            ptr2++;
                            ptr3++;
                            ptr++;
                        }
                    }
                    ptr2 += num2;
                    ptr3 += num3;
                    ptr  += num;
                }
            }
            unmanagedImage.Dispose();
            if (unmanagedImage3 != sourceData)
            {
                unmanagedImage3.Dispose();
            }
        }