Home | Lehre | Videos | Texte | Vorträge | Software | Person | Impressum, Datenschutzerklärung | Blog RSS

Blur, Sharpen & the Like

Some Programming Practice

Trace

Tracing: Let the program tells us what it does. In .NET use the following:
Define TRACE as a symbol (compiler settings)
using System.Diagnostics;
// on initialization:
Trace.AutoFlush = true;
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
// or:
Trace.Listeners.Add(new TextWriterTraceListener("logfile.txt"));
// throughout the program:
Trace.WriteLine("blah");

Assert

Assertions [Zusicherungen]: For debugging, check pre- and post-conditions and similar with code that will be disabled in the final version, which thus is faster and smaller.
Define DEBUG as a symbol (automatically done in compiler settings for the debug version, not for the release version).
using System.Diagnostics;
//check a simple condition:
Debug.Assert(earnings <= 10000, "Earnings may not exceed 10000 Euros.");
//check a complex condition:
#if DEBUG
//do the computation necessary for the test
if( /*...*/ ) Debug.Fail("This and that has happened.");
#endif

Debugger

The most important tool. Already shown in the lab.

Pointer Arithmetics

Example: Invert the colors of a bitmap
Bitmap b = new Bitmap("some_bitmap.bmp");
Rectangle r = new Rectangle(0, 0, b.Width, b.Height);
BitmapData d = b.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
    byte* p = (byte*)d.Scan0.ToPointer();
    int n = d.Height*d.Stride;
    for(int z = 0; z < n; z++)
    {
        *p = (byte)(255-*p);
        p++;
    }
}
b.UnlockBits(d);

Note that this also inverts the dummy bytes that may be appended at the end of each row. (The value of stride need not be three times the width, but can be larger!) Be careful to exclude the dummy bytes when computing blur or similar effects. Otherwise they will smear into the visible part of the image.

Antialiasing and Interpolation