forked from bjpop/scala-fungraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fun_graph.scala
360 lines (297 loc) · 11.7 KB
/
fun_graph.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* Functional graphics.
Computer graphic images are represented as functions from
real 2D coordinates to values. Animations are represented
as functions from time to images. This module was written
to support a talk at the Melbourne Scala Users Group.
Motivated by the paper "Functional Images" by Conal Elliot
http://conal.net/papers/functional-images/fop-conal.pdf
Author: Bernie Pope
Date: 1/4/2013
*/
import swing._
import java.awt.{Color}
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import java.io.{File}
import math.{cos, sin, abs, round, sqrt, pow, Pi, min, max}
// Convert images of various types into color images
object ColorImplicits {
import ImageFun._
import Math._
// true = white, false = black
implicit def BoolToColor(image:Image[Boolean]):Image[Color] =
mapImage((value:Boolean) => if (value) Color.white else Color.black, image)
implicit def DoubleToColor(image:Image[Double]):Image[Color] =
mapImage((value:Double) => {
val intensity = clampIntensity((value * 255).toInt)
new Color(intensity, intensity, intensity)}, image)
}
object Math {
// Cartesian distance between two points.
def distance(x1:Double, y1:Double, x2:Double, y2:Double):Double =
sqrt(pow(y1 - y2, 2) + pow(x1 - x2, 2))
// Modulus which returns a positive result, even for
// negative numerators.
def modDouble(x:Double, y:Double):Double = {
val m = x % y
if (m < 0) m + y else m
}
// Modulus which returns a positive result, even for
// negative numerators.
def modInt(x:Int, y:Int):Int = {
val m = x % y
if (m < 0) m + y else m
}
// Make sure intensity value is in the range [0, 255] inclusive.
def clampIntensity(intensity:Int) =
min(max(intensity, 0), 255)
}
object ImageFun {
import Math._
type Image[T] = (Double, Double) => T
type ImageTrans[T] = Image[T] => Image[T]
type Animation[T] = Double => Image[T]
type CoordTrans = (Double, Double) => (Double, Double)
def mapImage[A,B](fun:A => B, image:Image[A]):Image[B] =
(col, row) => fun(image(col, row))
def combineImage[A,B,C](image1:Image[A], image2:Image[B], combine:(A, B) => C):Image[C] =
(col, row) => combine(image1(col, row), image2(col, row))
def combineMany[T](images:List[Image[T]], combine:(T, T) => T):Image[T] =
images.reduceLeft((image1, image2) => combineImage(image1, image2, combine))
/* Want to write:
def coordTrans[T](trans:CoordTrans):ImageTrans[T] =
(image:Image[T]) => image.tupled compose trans
*/
// Transform the coordinates of an image by some function
def coordTrans[T](trans:CoordTrans):ImageTrans[T] =
(image:Image[T]) =>
(col:Double, row:Double) =>
image.tupled(trans(col, row))
// Apply an image transformation about a point
def aboutPoint[T](transform:ImageTrans[T], col:Double, row:Double):ImageTrans[T] =
translate(-col, -row) andThen transform andThen translate(col, row)
// Translate an image
def translate[T](colDelta:Double, rowDelta:Double):ImageTrans[T] =
coordTrans((col, row) => (col - colDelta, row - rowDelta))
// Scale image about the origin
def scaleOrigin[T](factor:Double):ImageTrans[T] =
coordTrans((col, row) => (col / factor, row / factor))
// Scale image about a center point
def scale[T](factor:Double,
centerCol:Double, centerRow:Double):ImageTrans[T] =
aboutPoint(scaleOrigin(factor), centerCol, centerRow)
// Rotate image clockwise about the origin, angle in radians
def rotateOrigin[T](angle:Double):ImageTrans[T] = {
val cosAngle = cos(angle)
val sinAngle = sin(angle)
coordTrans(
(col, row) =>
(col * cosAngle - row * sinAngle, col * sinAngle + row * cosAngle))
}
// Rotate image clockwise about some point, angle in radians
def rotate[T](angle:Double,
centerCol:Double, centerRow:Double):ImageTrans[T] =
aboutPoint(rotateOrigin(angle), centerCol, centerRow)
// Return an image from a bitmap file, given a filepath to the image.
// Bitmap is tiled infinitely in all directions.
def bitmap(filepath:String):Image[Color] = {
val pixels = ImageIO.read(new File(filepath))
val numRows = pixels.getHeight
val numCols = pixels.getWidth
(col:Double, row:Double) => {
val colInt = modInt(col.toInt, numCols)
val rowInt = modInt(row.toInt, numRows)
new Color(pixels.getRGB(colInt, rowInt))
}
}
}
object ImageTransExamples {
import Math._
import ImageFun._
import ImageExamples._
// Scale the magnification of a image based on a wave function, based on
// distance of point from the origin.
def waveScaleOrigin[T](phaseShift: Double, vertShift:Double,
amp:Double, period:Double):ImageTrans[T] = {
(image:Image[T]) => {
(col:Double, row:Double) => {
val scaleAmount = waveIntensityOrigin(phaseShift, vertShift, amp, period)(col, row)
scaleOrigin(scaleAmount)(image)(col, row)
}
}
}
// Scale the magnification of a image based on a wave function, based on
// distance of point from some other point.
def waveScale[T](phaseShift: Double, vertShift:Double, amp:Double,
period:Double, centerCol:Double, centerRow:Double):ImageTrans[T] = {
aboutPoint(waveScaleOrigin(phaseShift, vertShift, amp, period),
centerCol, centerRow)
}
// Translate an image vertically based on a wave function.
def waveTranslate[T](phaseShift: Double, vertShift:Double, amp:Double, period:Double):ImageTrans[T] = {
(image:Image[T]) =>
(col, row) => {
val trans = waveIntensityOrigin(phaseShift, vertShift, amp, period)(col, 0)
translate(0, trans)(image)(col, row)
}
}
}
object ImageExamples {
import Math._
import ImageFun._
val redImage = constImage(Color.red)
// Images entirely one value
def constImage[T](value:T):Image[T] =
(_, _) => value
def grid(cellSize:Double, lineThickness:Double):Image[Boolean] = {
(col, row) =>
(modDouble(col, cellSize) >= lineThickness &&
modDouble(row, cellSize) >= lineThickness)
}
/* Compute a two dimensional wave based on cosine, and distance from
the origin.
General form of a cosine wave:
F(x) = A cos(Bx - C) + D
A = amplitude
B = compression factor on x axis, B = 2Pi / period
C/B = phase shift
D = vertical shift
*/
def waveIntensityOrigin(phaseShift: Double, vertShift:Double,
amp:Double, period:Double):Image[Double] = {
val compress = 2 * Pi / period
val phaseFactor = phaseShift * compress
(col:Double, row:Double) => {
val d = distance(col, row, 0, 0)
amp * + cos(compress * d - phaseFactor) + vertShift
}
}
def waveIntensity(phaseShift: Double, vertShift:Double, amp:Double,
period:Double, col:Double, row:Int):Image[Double] =
translate(col, row)(waveIntensityOrigin(phaseShift, vertShift, amp, period))
val gridImage = grid(20, 5)
def scaleRotate[T](factor:Double, angle:Double):ImageTrans[T] =
scaleOrigin(factor) andThen rotateOrigin(angle)
val scaledRotatedGrid = scaleRotate(2, Pi/4)(gridImage)
val bitmapImage = bitmap("floyd.png")
val scaledRotatedBitmap = scaleRotate(0.25, Pi/4)(bitmapImage)
def waveImage:Image[Double] = {
val wave1 = waveIntensity(0, 0.3, 0.2, 50, 300, 200)
val wave2 = waveIntensity(0, 0.2, 0.1, 70, 50, 100)
combineImage(wave1, wave2, (x:Double, y:Double) => x + y)
}
}
object AnimationExamples {
import ImageFun._
import ImageExamples._
import ImageTransExamples._
def waveAnimation(time:Double):Image[Double] = {
val wave1 = waveIntensity(time * 6, 0.3, 0.2, 50, 300, 200)
val wave2 = waveIntensity(time * 2, 0.2, 0.1, 70, 50, 100)
combineImage(wave1, wave2, (x:Double, y:Double) => x + y)
}
def waveGridAnimation(time:Double):Image[Boolean] = {
val gridImage = ImageExamples.grid(20, 2)
waveScale(time * 2, 1, 0.3, 100, 200, 150)(gridImage)
}
def waveBitmapAnimation(time:Double):Image[Color] = {
waveScale(time * 2, 2, 0.8, 100, 200, 150)(scaledRotatedBitmap)
}
def waveTranslateAnimation(time:Double):Image[Color] = {
val floydBitmap = bitmap("floyd.png")
waveTranslate(time * 10, 0, 10, 100)(floydBitmap)
}
}
class Display(cols:Int, rows:Int) {
import ImageFun._
private val buffer = new BufferedImage(cols, rows, BufferedImage.TYPE_INT_RGB)
private val panel = new Panel {
override def paint(graphics:Graphics2D) {
graphics.drawImage(buffer, 0, 0, null)
}
preferredSize = new Dimension(cols, rows)
}
private val frame = new MainFrame {
title = "Functional Images"
contents = panel
centerOnScreen()
}
// Quantize an image function and draw into a pixel buffer
def rasterize(image:Image[Color]) = {
for (x <- 0 to cols - 1; y <- 0 to rows - 1)
buffer.setRGB(x, y, image(x, y).getRGB)
}
def repaint() = panel.repaint()
def open() = frame.open()
}
// Run an animation in a window indefinitely
class Animate(cols:Int, rows:Int, animation:ImageFun.Animation[Color]) {
def show() = {
val display = new Display(cols, rows)
var time = 0.0
var timeDelta = 1.0
display.open()
while(true) {
display.rasterize(animation(time))
display.repaint()
time += timeDelta
}
}
}
// Draw a still image in a window
class Draw(cols:Int, rows:Int, image:ImageFun.Image[Color]) {
def show() = {
val display = new Display(cols, rows)
display.rasterize(image)
display.open()
}
}
object Main {
import AnimationExamples._
import ImageExamples._
import ColorImplicits._
val usage = """
fun_graph demo
demo must be one of:
red_image an entirely red image
grid black and white grid
scaled_rotated_grid grid rotated 45 degrees
bitmap a tiled bitmap
scaled_rotated_bitmap a tiled bitmap
wave_image grey scale waves image
wave_animation grey scale waves animation
translate_wave translate pixels in a bitmap as vertical wave
grid_wave translate scale of a grid as wave from center of image
bitmap_wave translate scale of a bitmap as wave from center of image
"""
def main(args: Array[String]) = {
if (args.length == 0)
println(usage)
else
args(0) match {
case "red_image" =>
new Draw(400, 300, redImage).show()
case "grid" =>
new Draw(400, 300, gridImage).show()
case "scaled_rotated_grid" =>
new Draw(400, 300, scaledRotatedGrid).show()
case "bitmap" =>
new Draw(400, 300, bitmapImage).show()
case "scaled_rotated_bitmap" =>
new Draw(400, 300, scaledRotatedBitmap).show()
case "wave_image" =>
new Draw(400, 300, waveImage).show()
case "wave_animation" =>
new Animate(400, 300, waveAnimation).show()
case "translate_wave" =>
new Animate(400, 300, waveTranslateAnimation).show()
case "grid_wave" =>
new Animate(400, 300, waveGridAnimation).show()
case "bitmap_wave" =>
new Animate(400, 300, waveBitmapAnimation).show()
case _ =>
println("fun_graph: Unrecognised demo name")
println(usage)
}
}
}