Assembly
Computer Organization and Assembly Languages, Fall 2008

Jump to...

assignment #1
assignment #2
assignment #4
final project
assignments


Assignment #3: Box Filter

Assigned: 2008/11/24
Due: 11:59pm 2008/12/14

Description

Smoothing filters are useful image processing tools for blurring and for noise reduction. They use spatial coherence and pixel value homogeneity of a pixel's neighborhood as bases. Noise-cleaning techniques detect lack of coherence and either replace the incoherent pixel value by something more spatially coherent by using some or all of the pixels in a neighborhood containing the given pixel or smooth the pixel value with others in an appropriate neighborhood. The operator that computes the equally weighted average is called the box filter operator.

Given an image f, the box filter operator with a (2M+1)X(2N+1) smoothing neighborhood is defined by

where k(r, c) represents the pixel value of the filtered image k at the r-th row and the c-th column.

In this assignment, you have to implement an ARM assembly procedure for a 3x3 box filter and test it on a GBA emulator. Note that, for a 3x3 filter, a corner pixel only averages on 4 pixels, a border pixel on 6 pixels and a interior pixel on 9 pixels. For developing ARM assembly programs, we recommend HAM GBA DevKit. If you are familiar with Makefile, you can use DevKitARM or DevKit Advance instead. Although directly implementing the above function produces the correct filtering result, naïve implementation can be slow. Since such image-processing techniques usually have to meet the timing constraint for interactive playing, please try to improve its speed.

Preparation

We assume that you will use HAM as the development kits. Follow the following steps to install HAM.
  1. Download HAM 2.8 for Windows.
  2. Double click on ham-280-full-win32.exe to decompress and install HAM.
  3. Fill in OK and the path to install HAM. It is recommended to install it under the root such as d:\HAM.
  4. It will be installed and prompt an OK when it is done. Now, HAM is installed in d:\HAM.
Follow the following steps for programming your assignment.
  1. Download the template file for this assignment and uncompress it.
  2. In d:\HAM\system, replace the master.mak and standard-targets.mak with master.mak and standard-targets.mak. Replace Line #25 of master.mak so that HAMDIR points to where you have installed HAM.
  3. Run d:\HAM\vham\VHAM.exe and choose File->Open workspace to open hw3_template.vhw
  4. Press F7 to "build and run". Wait for a while and the following window should show up. Press the "left" key and the input image (figure in the middle) should show up. Press the "Down" key and the judge and clock number (figure on the right) should show up. Press the "Up" key and the filtered image (figure on the left) should show up. Note that the filtered image is blank since you have not implemented the filter yet.

    filtered image source image judge and clock

  5. Modify the file, myfilter.s, to implement the box filter.
For your reference, here are the slides that TA used in last year's class. Note that some links in the file might be broken. Please find the corresponding files at this webpage.

Specification

You have to implement myfilter.s as the 3x3 box filter. This function's prototype is
void myfilter(u16* ret, const u16* ori);
where ret is a pointer pointing to the output destination and ori is a pointer pointing to the input image data. ret is stored in R0 and ori is stored in R1. A skeleton code looks like:
	.file	"myfilter.c"
	.text
	.align	2
	.global	myfilter
	.type	myfilter, %function
myfilter:
	ldrh	r2, [r1, #0]	@ movhi	@ * ori
	strh	r2, [r0, #0]	@ movhi @ * ret
	
   	bx	lr
	
	.size	myfilter, .-myfilter
	.ident	"GCC: (GNU) 3.3.2"
Your job is to replace the two lines after the label myfilter (they are just placeholder) with the complete code for box filtering. In the template file, you can find the files you might need and main.cpp where other functions are implemented. They are used for image I/O and do timings, and you don't need to modify them.

A function called boxfilter() in boxfilter.c is a C implementation of the 3x3 box filter. The results of your ASM filter should be exactly the same as the output of this C implementation.
void boxfilter(u16 *ret,const u16* ori) {
	u32 r,g,b;
	u32 cc;
	int x,y,dx,dy;

	for(y=0;y<160;y++) {
	    for(x=0;x<240;x++) {
	        cc = r = g = b = 0;
            for(dy = -1;dy<=1;dy++) {
	            for(dx=-1;dx<=1;dx++) {
	                int nx = x+dx;
	                int ny = y+dy;
                    u16 ncolor;
                    if(nx < 0 || ny < 0 || nx >=240 || ny >= 160) continue;
                    ncolor = ori[ny*240+nx];
                    cc++;
                    r+= (ncolor&0x001f);
                    g+= ((ncolor&0x03e0)>>5);
                    b+= ((ncolor&0x7c00)>>10);
                }
            }
            r = r/cc;
            g = g/cc;
            b = b/cc;

            ret[y*240+x] = (b<<10)+ (g<<5) + r;
        }
    }

}
Note that GBA uses the BGR5 format in which every pixel uses 2 bytes. The lowest five bits are for red, the middle five for green and the higher five for blue. The highest bit is not used. If you would like to test on your own image, you can use the utility gfx2gba to convert your image. You can obtain gfx2gba from here. This program converts an input image into a C header file for the input image in GBA's image format. Replace input.h with the generated header file.

Grading

You will get 80 points if you implement this procedure correctly. Another 0~25 points will be added to your grade depends on the speed of your program.

Submission

We have provided a set of test images at here. Uncompress the archives under the directory where your hw3 project is at. Execute startham.bat under $HAM, where you have installed HAM. Then, under hw3 directory, execute "test_input N", where N=1..5, to test on different images. Submit your homework through online submission system.