Assembly
Computer Organization and Assembly Languages, Fall 2010

Jump to...

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


Assignment #3: Box Filter

Assigned: 2010/12/06
Due: 11:59pm 2010/12/26

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. 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

For developing ARM assembly programs, we recommend DevKitPro. Follow the following steps to install DevKitPro.
  1. Download this program for Windows.
  2. Double click the download install program. (You need network connection for downloading the required components to install the program.)
  3. Select "Download and install/install from downloaded files"
  4. Select "Remove downloaded files"
  5. In "Choose Components", check Minimal System, devkitARM and Insight and uncheck others to save time. Click on the "+" before devkitARM to expand its options. For this project, only the first two, devkitARM and libgba, are required. You can uncheck others. Click "Next".
  6. Choose where to install and install it.
To run gba program, you need a GBA simulator such as Visual Boy Advance. You can download the local copy of VBA SDL (with debugging support) or VBA DirectX (more friendly UI). Simply double click on the downloaded file to run the simulator. Load a .gba file to start the simulation.

Follow the following steps for programming your assignment.
  1. Download the template file for this assignment and uncompress it.
  2. Unzip the file.
  3. Launch a command-line box and switch to the directory you have unziped the template.
  4. type "make" to make the program and hw3.gba will be generated.
  5. Run the simulator and load hw3.gba. Wait for a while. Press the "left" key and the input image (the 1st figure) should show up. Press the "right" key and the reference image (the 2nd figure) should show up. Press the "Down" key and the judge and clock number (the 3rd figure) should show up. Press the "Up" key and the filtered image (the 4th figure) should show up. Note that the filtered image is blank since you have not implemented the filter yet.

    source image reference image
    judge and clock filtered image

  6. Modify the file, myfilter.s, to implement the box filter.
For your reference, here are the slides that TA presented in the class. It explains how to debug using gdb-like commands.

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:
	.text
	.align	2
	.global	myfilter
	.type	myfilter, %function
myfilter:
	@ write your codes here
	@ ldrh	r2, [r1, #0]	@ movhi	@ * ori
	@ strh	r2, [r0, #0]	@ movhi @ * ret
	
   	bx	lr		@ return
	
	.size	myfilter, .-myfilter
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

Submit your homework through online submission system.