-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedMemoryThread
More file actions
129 lines (103 loc) · 4.94 KB
/
Copy pathSharedMemoryThread
File metadata and controls
129 lines (103 loc) · 4.94 KB
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Drawing;
using System.Globalization;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Diagnostics;
namespace SpidermanGelap
{
class Program
{
static Thread[] threads;
static Barrier _barrier = new Barrier(participantCount: 0);
//Extra feature, geng just change path here and put original picture there too
static String path = @"D:\Google Drive\UiTM Jasin (CS230)\Sem 5\CSC580 (Parallel Processing)\Lab 5 Spiderman";
static void Main(string[] args)
{
//Start Stopwatch
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//Process of Splitting Image
Console.WriteLine("Process Splitting Starting...");
Image image1;
image1 = new Bitmap(Image.FromFile(Path.Combine(path,"Spiderman.jpg")));
int rows = 7;//Tukar nombor of process dekat sini.
int columns = 1;//No of columns as per Desire
var imgarray = new Image[rows, columns];//Create Image Array of Size Rows X Colums
var img = image1;//Get Image from anywhere, From File Or Using Dialogbox used previously
int height = img.Height;
int width = img.Width;//Get image Height & Width of Input Image
int one_img_h = height / rows;
int one_img_w = width / columns;//You need Rows x Columns, So get 1/rows Height, 1/columns width of original Image
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
imgarray[i, j] = new Bitmap(one_img_w, one_img_h);//generating new bitmap
var graphics = Graphics.FromImage(imgarray[i, j]);
graphics.DrawImage(img, new Rectangle(0, 0, one_img_w, one_img_h), new Rectangle(j * one_img_w, i * one_img_h, one_img_w, one_img_h), GraphicsUnit.Pixel);//Generating Splitted Pieces of Image
graphics.Dispose();
((Bitmap)imgarray[i, j]).Save(Path.Combine(path, "Spidey" + i + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
Console.WriteLine("Splitting image "+i);
}
}
Console.WriteLine("Process Splitting Ended...");
//Splitting habis
//Creating threads to process color changing on splitted images
int noOfThreads = rows;
threads = new Thread[noOfThreads];
//make array of Task
Task[] tasks = new Task[noOfThreads];
for (int i = 0; i < noOfThreads; i++)
{
int j = i;
Console.WriteLine("Create thread no "+i);
_barrier.AddParticipant(); //add count to participant for barrier
tasks[j] = Task.Factory.StartNew(() => Run(j)); //create thread
}
Task.WaitAll(tasks); //It will wait all of thread to finish first then baru sambung dekat bawah
Console.WriteLine("Process Combining Starting...");
//Process of Combining Image
//Images that gone through color changes will be combine back
Bitmap temp = new Bitmap(5000, 5000);
Graphics graph1 = Graphics.FromImage((Image)temp);
int n = 0;
for (int i = 0; i < rows; i++)
{
graph1.DrawImage((Image)Image.FromFile(Path.Combine(path, "BlackSpidey" + i + ".jpg")), 0, n);
n += (5000/rows);
}
graph1.Dispose();
temp.Save(Path.Combine(path, "combine1.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
Console.WriteLine("Process Combining Ended...Yayyy :)");
//Stopwatch End
stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
Console.ReadKey();
}
static void Run(object id)
{
{ //Process of changing color
Bitmap spodey = new Bitmap(Image.FromFile(Path.Combine(path, "Spidey" + id + ".jpg")));
for (int rowpix = 0; rowpix < spodey.Width; rowpix++)
{
for (int colpix = 0; colpix < spodey.Height; colpix++)
{
if ((spodey.GetPixel(rowpix, colpix).R - spodey.GetPixel(rowpix, colpix).G) > 40)
{
spodey.SetPixel(rowpix, colpix, Color.Black);
}
}
}
spodey.Save(Path.Combine(path, "BlackSpidey" + id + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
Console.WriteLine("Done changing color of Image " + id);
_barrier.SignalAndWait();
}
}
}
}