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

Blur, Sharpen & the Like

Some Programming Practice

Trace

Tracing: "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)
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

demo of source-level debugging

Pointer Arithmetic

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* data = (byte*)d.Scan0.ToPointer();
    int n = d.Height*d.Stride;
    for(int z = 0; z < n; z++)
    {
        *data = (byte)(255-*data);
        data++;
    }
}
b.UnlockBits(d);

Note that this also inverts the dummy bytes that may be appended at the end of each row. Be careful to exclude the dummy bytes when computing blur effects. Otherwise they will be smeared into the visible part of the image.

Commenting Style

Do no longer use comments for each line. Write self-explaining code that needs virtually no comments.

Antialiasing and Interpolation