Namespace > IC.PhotoController.Filter Namespace > Classes > BaseFilter Class
PhotoController 2.2
ContentsIndexHome
PreviousUpNext
BaseFilter Class

Provides a base set of functionality for filters. This class must be inherited.

C++
public: class BaseFilter : public IDisposable;
C#
public unsafe abstract class BaseFilter : IDisposable;
Visual Basic
Public unsafe abstract Class BaseFilter
Inherits IDisposable

The descendants have to override ApplyFilter method to provide the filter algorithm. First, you shall understand an image contains rows and columns. Suppose column is x-axis, row is y-axis. Then the range of column is from 0 to Image.Width, and the range of row is from 0 to Image.Height. If DrawArea is specified, then the range of column shall be from DrawArea.X to DrawArea.Width, or DrawArea.Left to DrawArea.Right; and the range of row shall be from DrawArea.Y to DrawArea.Height, or DrawArea.Top to DarawArea.Bottom. What ever choice of combination you want to use. But it's crucial to understand the image coordination. 

The next step is to understand what you need to do to process the image. 

An image is formed by pixels, which is located at each [column, row]. A pixel is not either a dot or a rectangle, but it's harmless if you think about it in that way. Therefore, the easiest way to read the data of each pixel is to have a nested loop as the following example shows. After you read the data of each pixel, you can use our setPixel method to modify the pixel.

This example demonstrates how to build your own cutsomter filter by inheriting BaseFilter. The customer filter uses a simple algorithm to apply a fast edge detection to the image. 

Define Z = 256 

new[x,y] - the new pixel data 

old[x,y] - the old pixel data 

the formula is 

new [x,y] = old[x,y] + (Z/2 - old[x+2, y+2]) 

[C#]

 
public class FastEdgeDetectFilter : BaseFilter
{
   protected override void ApplyFilter()
   {
       // store the bitmap information first
       int width = this.DrawArea.Right;
       int height = this.DrawArea.Bottom
       int startX = this.DrawArea.Left;
       int startY = this.DrawArea.Right;

       // scan each column of the image
       for (int x = startX; x < width; x++)
       {
           // scan each row of the image
           for (int y = startY; y < height; y++)
           {
               if (x + 2 < width && y + 2 < height)
               {
                   Color offset = getPixel(x + 2, y + 2);
                   Color current = getPixel(x, y);

                   // apply the algorithm
                   byte red = (byte)(current.R + (128 - offset.R));
                   byte green = (byte)(current.G + (128 - offset.G));
                   byte blue = (byte)(current.B + (128 - offset.B));

                   Color newColor = Color.FromArgb(red, green, blue);
                   setPixel(x, y, newColor);
               }
           }
       }
   }
}

 

[VB.NET]

Imports IC.PhotoController.Filter
Imports System.Drawing
 
Public Class FastEdgeDetectFilter
    Inherits IC.PhotoController.Filter.BaseFilter
 
    Protected Overrides Sub ApplyFilter()
 
        Dim width As Integer = DrawArea.Width
        Dim height As Integer = DrawArea.Height
        Dim startX As Integer = DrawArea.Left
        Dim startY As Integer = DrawArea.Right
 
        Dim x As Integer
        Dim y As Integer
 
        For x = 0 To width
            For y = 0 To height
                If (x + 2 < width And y + 2 < height) Then
                    Dim offset As Color = getPixel(x + 2, y + 2)
                    Dim current As Color = getPixel(x, y)
 
                    Dim red As Byte
                    Dim blue As Byte
                    Dim green As Byte
 
                    red = IntToByte(current.R + (128 - offset.R))
                    green = IntToByte(current.G + (128 - offset.G))
                    blue = IntToByte(current.B + (128 - offset.B))
 
                    Dim newColor As Color = Color.FromArgb(red, green, blue)
                    setPixel(x, y, newColor)
                End If
            Next
        Next
    End Sub
End Class

BaseFilter.cs

Send comments about this topic.
Copyright (c) 2008-2009 @ ImageComponent.NET. All rights reserved.