Assembly
Computer Organization and Assembly Languages, Fall 2012

Jump to...

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


Assignment #3: Box Filter

Assigned: 2012/11/28
Due: 2012/12/18 11:59pm

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 an ARM machine. 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.

Specification

Please check out TA's slides first. (pdf version)

Download the template file. You have to implement myfilter.s as the 3x3 box filter. This function's prototype is
void myfilter(uint16_t *dst, const uint16_t *src, uint32_t width, uint32_t height);
where dst is a pointer pointing to the output destination and src is a pointer pointing to the input image data. dst is stored in R0 and src is stored in R1. The size of both src and dst is width by height, which are stored in R2 and R3, respectively. A skeleton code looks like:
    arch armv6
    .text
    .align  2
    .global myfilter
    .type   myfilter, %function

myfilter:
    @ r0: dst's address
    @ r1: src's address
    @ r2: width
    @ r3: height
				
    @ push r4-r11 and lr
    stmfd   sp!, {r4-r11, lr}


    @ WRITE YOUR CODES HERE
					    

    @ pop r4-r11 and load lr to pc
    ldmfd   sp!, {r4-r11, pc}
    .size   myfilter, .-myfilter
Your job is to replace the line "@ WRITE YOUR CODES HERE" 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 format conversion, 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(uint16_t* dst, const uint16_t* src, uint32_t width, uint32_t height)
{
    int32_t x, y;

    for (y = 0; y < height; ++y) {
        for (x = 0; x < width; ++x) {
            uint32_t r = 0, g = 0, b = 0, cc = 0;
            int32_t dx, dy;

            for(dy = -1; dy <= 1; ++dy) {
                for(dx=-1; dx <= 1; ++dx) {
                    int32_t nx = x + dx;
                    int32_t ny = y + dy;
                    uint16_t ncolor;
                    if (nx < 0 || ny < 0 || nx >= width || ny >= height)
                        continue;
                    ncolor = src[ny*width + nx];
                    ++cc;
	            r += (ncolor&0x001f);
	            g += ((ncolor&0x03e0)>>5);
	            b += ((ncolor&0x7c00)>>10);
	        }
            }
            r = r/cc;
	    g = g/cc;
	    b = b/cc;

	    dst[y*width + x] = (b<<10) + (g<<5) + r;	    
	}
    }
}
Note that the BGR5 format is used, 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 just provide the 24-bit BMP file. Our main function will load the BMP file and convert it to RGB5 format for the box filtering function.

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.