homework 2 windows thread programming. goal implement a simple image sharpening program on windows...
TRANSCRIPT
HOMEWORK 2Windows thread programming
GOAL
Implement a simple image sharpening program on Windows
Use multithread programming to make your program faster
2
IMAGE SHARPENING
Asharp = A + k(A – Ablur) k = const.
Origin signal Blurred signal
Sharpened signal
3
Unsharp mask
Origin signal
FILTERINGImage origin
mask
y
x
Image f(x,y)
m(-1,-1) m(-1,0) m(-1,1)
m(0,-1) m(0,0) m(0,1)
m(1,-1) m(1,0) m(1,1)
Mask coefficient showing coordinate
arrangement
f(x-1,y-1) f(x-1,y) f(x-1,y+1)
f(x,y-1) f(x,y) f(x,y+1)
f(x+1,y-1) f(x+1,y) f(x+1,y+1)
Pixels of image section under
mask
4
SMOOTHING FILTER
1 1 1
1 1 1
1 1 1
5
ORIGINAL IMAGE
6
SHARPENED IMAGE
7
IMAGE CONTENT
imgWidth, imgHeight Unsigned char Pic_in[x] array Each pixel is represented by three values R G B R G B….. Accessing the ith row, jth col pixel :
pic_in[(i*imgWidth+j)*3+color] color = 0,1,2
8
……………..R G B R G B BPic_in
imgWidth*imgHeight*3
REQUIREMENT
We will give you an example code of smoothing and sharpening filter, and you need to modify it.
You can only write your own image processing code between the timing function (of course you can declare global variables or local variables anywhere) QueryPerformanceFrequency(&ticksPerSecond); QueryPerformanceCounter(&start_tick); //your code… QueryPerformanceCounter(&end_tick);
elapsed = ((double) (end_tick.QuadPart - start_tick.QuadPart) / ticksPerSecond.QuadPart); 9
REQUIREMENT
You can only use our library to read the input image (24bit uncompressed BMP file)
The format of the mask file: 1st line : FILTER SIZE (3x3 5x5 …) 2nd line : filtering times for smoothing 3rd line: mask number start from upper left (but
all of them will be 1)
10
REQUIREMENT
You must use more than 1 thread You can get basic score if your program is
faster than the example The man who can write the fastest program,
will get the highest score in this project! The demo computer spec: Intel Core i5, 4GB
ram, windows7 32bit ……
11
REQUIREMENT
Test the program in different number of threads
Make a line chart of “Elapsed Time” and “Number of Threads” in your report
Upload your code and report to the FTP
12
CREATE THREAD AND SYNCHRONIZATION FUNCTIONS IN WINDOWS Windows thread programming
CREATE THREAD AND SYNCHRONIZATION FUNCTIONS IN WINDOWS
#include <windows.h> #include <process.h>
14
CREATE THREAD IN WINDOWS
HANDLE WINAPI CreateThread( __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, __in SIZE_T dwStackSize, __in LPTHREAD_START_ROUTINE lpStartAddress, __in_opt LPVOID lpParameter, __in DWORD dwCreationFlags, __out_opt LPDWORD lpThreadId );
EX: to create a thread to execute function ABCCreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ABC,
NULL, 0, &ThreadID );15
SYNCHRONIZATION FUNCTIONSIN WINDOWS
HANDLE WINAPI CreateSemaphore (__in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,__in LONG lInitialCount,__in LONG lMaximumCount,__in_opt LPCTSTR lpName );
EX : The max value for the semaphore Sem is 5, and its initial value is 0
HANDLE Sem;Sem = CreateSemaphore( NULL, 0, 5, NULL ); 16
SYNCHRONIZATION FUNCTIONSIN WINDOWS
DWORD WINAPI WaitForSingleObject (__in HANDLE hHandle,__in DWORD dwMilliseconds );
EX : to wait for a semaphoreWaitForSingleObject( Sem, INFINITE );
17
SYNCHRONIZATION FUNCTIONSIN WINDOWS
DWORD WINAPI WaitForMultipleObject ( __in DWORD nCount,
__in const HANDLE *lpHandles, __in BOOL bWaitAll,
__in DWORD dwMilliseconds );
EX : to wait for two threadsHANDLE Thread[2];WaitForMultipleObject( 2, Thread, TRUE, INFINITE );18
SYNCHRONIZATION FUNCTIONSIN WINDOWS
BOOL WINAPI ReleaseSemaphore (__in HANDLE hSemaphore,__in LONG lReleaseCount,__out_opt LPLONG lpPreviousCount );
EX : to signal a semaphoreReleaseSemaphore( Sem, 1, NULL );
WaitForSingleObject( Sem, INFINITE );19
EX:HW1 IN WINDOWSint main(void){
top = down = 0;full = CreateSemaphore( NULL, 0, 5, NULL );empty = CreateSemaphore( NULL, 5, 5, NULL );int j, *tid_arg;srand((int)time(0)); printf("start\n");tid_arg = (int *) malloc(sizeof(int));*tid_arg = 1;DWORD tid;HANDLE Thread[2];Thread[0] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)producer, (void*)tid_arg, 0, &tid );Thread[1] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)consumer, (void*)tid_arg, 0, &tid );WaitForMultipleObjects(2, Thread, TRUE, INFINITE);printf("finish\n");system("pause");return 0;
}
20
//global variablesint top, down;int buf[5];HANDLE full, empty;
void * producer(void *arg)
{
printf("p_in\n");
int i, input, in_data;
input = *((int *) arg);
for (i=0; i<12; i++) {
WaitForSingleObject( empty, INFINITE );
Sleep(rand()%5);
in_data = rand()%256;
printf(">>p(%d) =(%d),buf[%d]\n", i, in_data, top);
buf[top] = in_data;
top = (top+1)%5;
ReleaseSemaphore(full, 1, NULL);
}
return 0;
}21
EX:HW1 IN WINDOWS
void * consumer(void *arg)
{
printf("c_in\n");
int i, input, out_data;
input = *((int *) arg);
for (i=0;i<12;i++){
WaitForSingleObject( full, INFINITE );
Sleep(rand()%10);
out_data = buf[down];
printf(">>c(%d) =(%d),buf[%d]\n", i, out_data, down);
down = (down+1)%5;
ReleaseSemaphore(empty, 1, NULL);
}
return 0;
}22
EX:HW1 IN WINDOWS