-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prac2Parallel.cpp
189 lines (165 loc) · 6.2 KB
/
Prac2Parallel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//==============================================================================
// Copyright (C) John-Philip Taylor
// tyljoh010@myuct.ac.za
//
// This file is part of the EEE4084F Course
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//==============================================================================
#include "Prac2Parallel.h"
#include <iostream>
#include <math.h>
//------------------------------------------------------------------------------
// This is each thread's "main" function. It receives a unique ID
void* Thread_Main(void* Parameter){
params tParams = *((params*)Parameter);
pthread_mutex_lock(&Mutex);
printf("Hello from thread %d\n", tParams.ID);
pthread_mutex_unlock(&Mutex);
int* neighborPixels;
int x, y, fx, fy, winEdgeX, winEdgeY;
for(y = tParams.start_i; y < tParams.end_i; y++){
winEdgeY = y - (winSizeY/2);
int diffY = 0;
// Handle the edge cases in Y direction
if(winEdgeY < 0){
diffY = -winEdgeY;
winEdgeY = 0;
}
if((winEdgeY + winSizeY) > Input.Height){
diffY = (winEdgeY + winSizeY) - Input.Height;
}
for(x = 0; x < Input.Width*Input.Components; x+= Input.Components){
// get all the neighboring pixels and put them in and array of 81 items
// Window will be shrinked near the boundries
winEdgeX = x - (((int)(winSizeX/2))*Input.Components);
// Handle the edge cases in X direction
int diffX = 0;
if(winEdgeX < 0){
diffX = -winEdgeX;
winEdgeX = 0;
}
if((winEdgeX + (winSizeX*Input.Components)) > (Input.Width*Input.Components)){
diffX = (winEdgeX + (winSizeX*Input.Components)) - (Input.Width*Input.Components);
}
int n = (winSizeX - (diffX/Input.Components))*(winSizeY - diffY);
neighborPixels = new int[n];
int i = 0;
for(fy = 0; fy < (winSizeY - diffY); fy++){
for(fx = 0;fx < ((winSizeX*Input.Components) - diffX); fx+=Input.Components){
neighborPixels[i++] = getRGB_Integer(Input.Rows[winEdgeY+fy][winEdgeX+fx+0],Input.Rows[winEdgeY+fy][winEdgeX+fx+1],Input.Rows[winEdgeY+fy][winEdgeX+fx+2]);
}
}
// Sort this list
quickSort(neighborPixels, 0, n - 1);
// Save the median of the sorted list to the current pixel
int median = neighborPixels[(n - 1)/2];
delete[] neighborPixels;
// Convert the median back to RGB (3 components)
Output.Rows[y][x + 0] = (unsigned char)((median >> 16) & 0xff);
Output.Rows[y][x + 1] = (unsigned char)((median >> 8) & 0xff);
Output.Rows[y][x + 2] = (unsigned char)(median & 0xff);
}
}
pthread_mutex_lock(&Mutex);
printf("Thread %d: I QUIT!\n", tParams.ID);
pthread_mutex_unlock(&Mutex);
return 0;
}
//------------------------------------------------------------------------------
/* Function to print an array */
void printArray(int pArr[], int pSize)
{
int i;
for (i=0; i < pSize; i++){
printf("%d ", pArr[i]);
}
printf("\n----------------------------------------------------------------------------\n");
}
//------------------------------------------------------------------------------
/**
* @brief: Convert the given RGB to Hex equivalent.
* @return Int equivalent of the converted Hex number
*/
int getRGB_Integer(unsigned char pR, unsigned char pG, unsigned char pB){
return (((int)pR & 0xff) << 16) + (((int)pG & 0xff) << 8) + ((int)pB & 0xff);
}
//-----------------------------------------------------------------------------
void swap(int* pX, int* pY){
int temp = *pX;
*pX = *pY;
*pY = temp;
}
//-----------------------------------------------------------------------------
int partition(int* pArray, int pLower_i, int pHigher_i){
int pivot = pArray[pHigher_i];
int i = pLower_i - 1;
for(int j = pLower_i; j < pHigher_i; j++){
if(pArray[j] < pivot){
i++;
swap(&pArray[i], &pArray[j]);
}
}
swap(&pArray[i + 1],&pArray[pHigher_i]);
return (i + 1);
}
//-----------------------------------------------------------------------------
void quickSort(int* pArray, int pLower_i, int pHigher_i){
if(pLower_i < pHigher_i){
int partition_index = partition(pArray, pLower_i, pHigher_i);
// Recursively sort items before and after partition index
quickSort(pArray, pLower_i, partition_index - 1);
quickSort(pArray, partition_index + 1, pHigher_i);
}
}
//-----------------------------------------------------------------------------
int main(int argc, char** argv){
printf("Code is running...");
Input.Read("Data/small.jpg");
if(!Output.Allocate(Input.Width, Input.Height, Input.Components)){
return -2;
}
// Spawn threads...
params Thread_Params[Thread_Count]; // Structure to keep the tread ID
pthread_t Thread [Thread_Count]; // pThreads structure for thread admin
int Rows_Per_Thread = round(Input.Height/Thread_Count);
int j;
for(j = 0; j < Thread_Count; j++){
Thread_Params[j].ID = j;
Thread_Params[j].start_i = j*Rows_Per_Thread;
Thread_Params[j].end_i = (j + 1)*Rows_Per_Thread;
if((j + 1)*Rows_Per_Thread > Input.Height){
Thread_Params[j].end_i = Input.Height;
}
pthread_create(Thread+j, 0, Thread_Main, Thread_Params+j);
}
tic();
// Wait for threads to finish
for(j = 0; j < Thread_Count; j++){
if(pthread_join(Thread[j], 0)){
pthread_mutex_lock(&Mutex);
printf("Problem joining thread %d\n", j);
pthread_mutex_unlock(&Mutex);
}
}
printf("Run time is: %lg ms \n", toc()/(1e-3));
// Write the output image
if(!Output.Write("Data/Output.jpg")){
printf("Cannot write image\n");
return -3;
}
printf("Finish...");
return 0;
}
//------------------------------------------------------------------------------