-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageprocessor.scala
322 lines (231 loc) · 8.5 KB
/
imageprocessor.scala
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import io.threadcso._
/* Layout of file
1. The ImageProcessor class that does the actual work
2. Test results
3. The test object that automates testing
*/
class ImageProcessor(val sourceImage: ImageProcessor.Image) {
case class Position(x: Int, y: Int)
private var image: ImageProcessor.Image = _
private val width = sourceImage.length
private val height = if(width == 0) -1 else sourceImage(0).length
private val pixelCount = width * height
private var changeBarrier = new CombiningBarrier[Boolean](10, false, logicalOrOperator)
private var syncBarrier = new Barrier(10)
def smooth(): ImageProcessor.Image =
{
resetImage()
val workerCount = pixelCount
changeBarrier = new CombiningBarrier[Boolean](workerCount, false, logicalOrOperator)
syncBarrier = new Barrier(workerCount)
val workers = || (for (i <- 0 until width; j <- 0 until height) yield pixelSmoother(Position(i, j)))
workers()
return image
}
private def pixelSmoother(pos: Position) = proc
{
var changeMade: Boolean = true
while(changeBarrier.sync(changeMade))
{
changeMade = (pixelIsSet(pos) != smoothedValue(pos))
syncBarrier.sync()
if(changeMade)
{
flipPixel(pos)
}
}
}
private def pixelIsValid(pos: Position): Boolean =
{
return (0 <= pos.x && pos.x < width && 0 <= pos.y && pos.y < height)
}
private def pixelIsSet(pos: Position): Boolean =
{
return pixelIsValid(pos) && image(pos.x)(pos.y)
}
private def flipPixel(pos: Position): Unit =
{
image(pos.x)(pos.y) = !image(pos.x)(pos.y)
}
private def populationOfBlock(pos: Position): Int =
{
var population: Int = 9
var xOnBorder, yOnBorder = false
if(0 == pos.x || width - 1 == pos.x)
{
population -= 3
xOnBorder = true
}
if(0 == pos.y || height - 1 == pos.y)
{
population -= 3
yOnBorder = true
}
if(xOnBorder && yOnBorder)
{
population += 1
}
return population
}
private def pixelsSetInBlock(pos: Position): Int =
{
val neighbours = Position(pos.x-1, pos.y-1) :: Position(pos.x-1, pos.y) :: Position(pos.x-1, pos.y+1) ::
Position(pos.x, pos.y-1) :: Position(pos.x, pos.y) :: Position(pos.x, pos.y+1) ::
Position(pos.x+1, pos.y-1) :: Position(pos.x+1, pos.y) :: Position(pos.x+1, pos.y+1) :: Nil
return neighbours.count(pixelIsSet)
}
private def smoothedValue(pos: Position): Boolean =
{
return (populationOfBlock(pos) * 0.5 < pixelsSetInBlock(pos))
}
private def logicalOrOperator(p: Boolean, q: Boolean): Boolean =
{
return (p || q)
}
private def resetImage(): Unit =
{
image = sourceImage.map(_.clone)
}
}
object ImageProcessor
{
type Row = Array[Boolean]
type Image = Array[Row]
}
/* Test output
Running test on image of dimensions: 17 x 7...
░█░█░██ ░░░░░░░
█░░░█░█ ░░░░░░░
███░░█░ ░░░░░░░
░░░█░█░ ░░░░░░░
░░██░░░ ░███░░░
██░███░ █████░░
██░█░█░ ███████
░█░█░██ ░░█████
░░░█░░█ ░░░░███
██░█░██ ░░░░░░░
░██░░██ ░░░░░░░
░░█░░░░ ░░░░░░░
██░█░█░ ░░░░░░░
█░░░██░ ██░░░░░
█░██░░░ █████░░
████░█░ ██████░
█░░████ ██████░
Running test on image of dimensions: 9 x 8...
░██░░░██ ░░░░░░░░
░░░░█░░█ ░░░░░░░░
░░██░░██ ░░░░░░░░
█░░░█░░█ ░░░░░░░░
█░█████░ ░░░░░░░░
███░█░░█ ░░░░░░░░
█░░░█░░█ ░░░░░░░░
░░█░░░█░ ░░░░░░░░
██░░░░██ ░░░░░░░░
Running test on image of dimensions: 12 x 15...
█░███░░█░██░░░█ ░░░░░░░░████░░░
█░░░░█░░██████░ ░░░░░░░░█████░░
░█░██░░░█████░░ █████░░░█████░░
████░███░█░░░░░ ██████░░█████░░
█░████░░██░███░ ██████░░░███░░░
░█░░█░░░█░█░██░ ░█████░░░░░░░░░
░█░░███░██░░░█░ ░░████░░░░░░░░░
█░██░█░░░█░██░█ ░░█████░░░░░░░░
░░██░██░█░░░░█░ ░██████░░░░░░░░
░██████░░░█░███ ███████░░░░░░░░
░██░░███░░░█░░█ ███████░░░░░░░░
████░█░░█░░░███ ██████░░░░░░░░░
*/
object ImageProcessorTest
{
private val minWidth = 5
private val maxWidth = 20
private val minHeight = 5
private val maxHeight = 20
private val testCount = 3
private val whitePixel: Char = '░'
private val blackPixel: Char = '█'
def main(args: Array[String]) = runTests()
def runTests(): Unit =
{
println("Running " + testCount + " random tests...")
val images = generateRandomImages(testCount)
images.foreach(testImage)
}
def testImage(image: ImageProcessor.Image): Unit =
{
println("Running test on image of dimensions: " + image.length + " x " + image.head.length + "...")
val processor = new ImageProcessor(image)
if(image == processor.smooth())
{
println("lofasz")
}
printImagesSideBySide(image, processor.smooth())
}
def printImagesSideBySide(l: ImageProcessor.Image, r: ImageProcessor.Image): Unit =
{
for(i <- 0 until l.length)
{
for(j <- 0 until l.head.length)
{
if(l(i)(j))
{
print(blackPixel)
}
else
{
print(whitePixel)
}
}
print(" ")
for(j <- 0 until r.head.length)
{
if(r(i)(j))
{
print(blackPixel)
}
else
{
print(whitePixel)
}
}
println()
}
}
private def getRandomIntegerInRange(l: Int, h: Int): Int =
{
val rnd = new scala.util.Random
return l + rnd.nextInt(h - l + 1)
}
private def getRandomBoolean(): Boolean =
{
return (0 == getRandomIntegerInRange(0, 1))
}
private def getRandomWidth(): Int =
{
return getRandomIntegerInRange(minWidth, maxWidth)
}
private def getRandomHeight(): Int =
{
return getRandomIntegerInRange(minHeight, maxHeight)
}
private def generateRandomImage(): ImageProcessor.Image =
{
val width = getRandomWidth()
val height = getRandomHeight()
val image = new Array[ImageProcessor.Row](width)
for(x <- 0 until width)
{
image(x) = new Array[Boolean](height)
for(y <- 0 until height)
{
image(x)(y) = getRandomBoolean()
}
}
return image
}
private def generateRandomImages(imageCount: Int): Array[ImageProcessor.Image] =
{
val images = new Array[Int](imageCount)
return images.map(_ => generateRandomImage)
}
}