forked from hakimel/reveal.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
1003 lines (872 loc) · 29.9 KB
/
index.html
File metadata and controls
1003 lines (872 loc) · 29.9 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<!-- reveal.js 2.6.2 -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>What an Interface Can Do - Static Duck Typing</title>
<meta name="description" content="An exploration of Go interfaces">
<meta name="author" content="Thomas A. Boyer">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/moon.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/idea.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<style type="text/css">
.reveal h6 {
text-transform: none;
}
.reveal .intro a:not(.image) {
color: #eee8d5;
}
.reveal pre code {
font-size: 120%;
}
.reveal .slides {
text-align: left;
}
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>What an Interface Can Do</h1>
<h3>Utah Gophers</h3>
<h3>4 November 2014</h3>
<p>Thom Boyer</p>
<p>Dish Digital</p>
<p><small>
This talk is online at
<a href="http://thomboyer.com/Go-Interfaces-Talk?theme=simple">
thomboyer.com/Go-Interfaces-Talk</a>.
<br/>The sources are available on
github at
<a href="https://github.com/perlmonger42/Go-Interfaces-Talk">
perlmonger42 / Go-Interfaces-Talk</a>.
</small></p>
</section>
<section>
<h2>Interfaces Are Cool</h2>
<blockquote cite="http://research.swtch.com/interfaces">
“Go's interfaces—static, checked at compile time, dynamic
when asked for—are, for me, the most exciting part of Go from a
language design point of view. If I could export one feature of Go
into other languages, it would be interfaces. ”
<br/>
— Russ Cox
</blockquote>
</section>
<section>
<h2>Safe Duck Typing</h2>
<blockquote cite="http://research.swtch.com/interfaces">
“Go's interfaces let you use duck typing like you would in a
purely dynamic language like Python but still have the compiler
catch obvious mistakes like passing an int where an object with a
Read method was expected, or like calling the Read method with the
wrong number of arguments.”
<br/>
— Russ Cox
</blockquote>
</section>
<section>
<h2>Example Interfaces</h2>
<p>
To demonstrate the power of interfaces, we'll mostly be playing
with the <code>Image</code> interface. But first let's touch on
some of the supporting types:
<ul>
<li><code>image.Point</code></li>
<li><code>image.Rectangle</code></li>
<li><code>color.Color</code></li>
<li><code>color.Model</code></li>
</ul>
</p>
</section>
<section>
<h2><code>image.Point</code> and <code>image.Rectangle</code></h2>
<pre><code class="go" data-trim contenteditable>
// An X, Y coordinate pair. The axes increase right and down.
type Point struct {
X, Y int
}
// A Rectangle contains the points with Min.X <= X < Max.X,
// Min.Y <= Y < Max.Y. It is well-formed if Min.X <= Max.X
// and likewise for Y. Points are always well-formed. A
// rectangle's methods always return well-formed outputs for
// well-formed inputs.
type Rectangle struct {
Min, Max Point
}
</code></pre>
</section>
<section>
<h2><code>color.Color</code> and <code>color.Model</code></h2>
<pre><code class="go" data-trim contenteditable>
// Color can convert itself to alpha-premultiplied 16-bits
// per channel RGBA. The conversion may be lossy.
type Color interface {
// RGBA returns the alpha-premultiplied red, green, blue and
// alpha values for the color. Each value ranges within [0, 0xFFFF],
// but is represented by a uint32 so that multiplying by a
// blend factor up to 0xFFFF will not overflow.
RGBA() (r, g, b, a uint32)
}
// Model can convert any Color to one from its own color model.
// The conversion may be lossy.
type Model interface {
Convert(c Color) Color
}
</code></pre>
</section>
<section>
<h2><code>image.Image</code></h2>
<pre><code class="go" data-trim contenteditable>
type Image interface {
// ColorModel returns the Image's color model.
ColorModel() color.Model
// Bounds returns the domain for which At can return
// non-zero color. The bounds do not necessarily contain
// the point (0, 0).
Bounds() Rectangle
// At returns the color of the pixel at (x, y).
// At(Bounds().Min.X, Bounds().Min.Y) returns the
// upper-left pixel of the grid.
// At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the
// lower-right one.
At(x, y int) color.Color
}
</code></pre>
</section>
<section>
<h2>What an Image Is Not</h2>
<p>
Package <code>image</code> provides <code>image.RGBA</code>,
a concrete type that implements <code>image.Image</code>.
It contains a slice of bytes (<code>Pix
[]uint8</code>) that encodes the red, green, blue, and
alpha components of each pixel the image contains.
</p>
<p>But — as you can tell from the interface definition —
an <code>Image</code> does not <em>have</em> to contain pixel
storage. As long as its <code>At()</code> method
can tell what color each pixel is, that is
sufficient.
</section>
<section>
<h2>Creating an Empty Picture</h2>
<p>Here is a simple program that creates a blank image and
writes it to a file in PNG format.</p>
<pre><code class="go" data-trim contenteditable>
package main
import ("image"; "image/png"; "log"; "os")
func main() {
size := image.Point{850, 400}
m := image.NewRGBA(image.Rectangle{image.ZP, size})
outFilename := "transparent.png"
outFile, err := os.Create(outFilename)
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
log.Print("Saving image to: ", outFilename)
png.Encode(outFile, m)
}
</code></pre>
</section>
<section data-background="code/CiucasMountains-Romania.jpg">
<h2>transparent.png</h2>
<p>And here's the resulting image:</p>
<img src="code/transparent.png"/>
</section>
<section>
<h2>The Simplest <code>Image</code> Possible</h2>
<pre><code class="go" data-trim contenteditable>
package main
import ("image"; "image/png"; "image/color"; "log"; "os")
func main() {
outFilename, m := makeSampleImage()
outFile, err := os.Create(outFilename)
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
log.Print("Saving image to: ", outFilename)
png.Encode(outFile, m)
}
func makeSampleImage() (string, image.Image) {
return "rectangle.png", Rectangle{}
}
type Rectangle struct { }
func (shape Rectangle) ColorModel() color.Model {
return color.RGBAModel
}
func (shape Rectangle) Bounds() image.Rectangle {
return image.Rect(0, 0, 850, 400)
}
func (shape Rectangle) At(x, y int) color.Color {
return color.RGBA{0, 0, 255, 255} // blue
}
</code></pre>
</section>
<section data-background="code/CiucasMountains-Romania.jpg">
<h2>rectangle.png</h2>
<p>And here's the resulting image:</p>
<img src="code/rectangle.png"/>
</section>
<section>
<h2>The Simplest Geometric Shape Possible</h2>
<pre><code class="go" data-trim contenteditable>
func makeSampleImage() (string, image.Image) {
hotpink := color.RGBA{255, 25, 155, 255}
return "circle.png", Circle{Radius: 200, Color: hotpink}
}
type Circle struct {
Radius int
Color color.Color
}
func (shape Circle) Bounds() image.Rectangle {
r := shape.Radius
return image.Rect(-r, -r, r, r)
}
func (shape Circle) At(x, y int) color.Color {
r := shape.Radius
if x*x + y*y < r*r {
return shape.Color
}
return color.RGBA{0, 0, 0, 0}
}
</code></pre>
</section>
<section data-background="code/CiucasMountains-Romania.jpg">
<h2>circle.png</h2>
<p>And here's the resulting image:</p>
<img src="code/circle.png"/>
</section>
<section>
<h2>Nested Circles</code></h2>
<pre><code class="go" data-trim contenteditable>
func makeSampleImage() (string, image.Image) {
return "colorings.png", ColorRings{Radius: 200}
}
type ColorRings struct {
Radius int
}
func (shape ColorRings) Bounds() image.Rectangle {
r := shape.Radius
return image.Rect(-r, -r, r, r)
}
func (shape ColorRings) At(x, y int) color.Color {
distance := int(math.Hypot(float64(x), float64(y)))
return RainbowColor(distance * 6 / shape.Radius)
}
func RainbowColor(n int) color.Color {
switch n {
case 0: return color.RGBA{0xFF, 0x00, 0x00, 255} // Red
case 1: return color.RGBA{0xFF, 0xA5, 0x00, 255} // Orange
case 2: return color.RGBA{0xFF, 0xFF, 0x00, 255} // Yellow
case 3: return color.RGBA{0x3C, 0xB3, 0x71, 255} // Medium Sea Green
case 4: return color.RGBA{0x1E, 0x90, 0xFF, 255} // Dodger Blue
case 5: return color.RGBA{0x93, 0x70, 0xDB, 255} // Medium Purple
}
return color.RGBA{0, 0, 0, 0}
}
</code></pre>
</section>
<section data-background="code/CiucasMountains-Romania.jpg">
<h2>colorings.png</h2>
<p>And here's the resulting image:</p>
<img src="code/colorings.png"/>
</section>
<section>
<h2>Blur</code></h2>
<pre><code class="go" data-trim contenteditable>
func makeSampleImage() (string, image.Image) {
return "blur.png", Blur{
Image: ColorRings{Radius: 200},
BlurRadius: 8,
}
}
type Blur struct {
Image image.Image
BlurRadius int
}
func (shape Blur) Bounds() image.Rectangle {
return shape.Image.Bounds()
}
func (shape Blur) At(x, y int) color.Color {
var sumWeight, sumR, sumG, sumB float64
radius := float64(shape.BlurRadius)
for i := x - shape.BlurRadius; i < x+shape.BlurRadius+1; i++ {
for j := y - shape.BlurRadius; j < y+shape.BlurRadius+1; j++ {
dist := math.Hypot(float64(i-x), float64(j-y))
if dist <= radius {
weight := 1 / (1 + dist)
r, g, b, _ := shape.Image.At(i, j).RGBA()
sumR += float64(r) * weight
sumG += float64(g) * weight
sumB += float64(b) * weight
sumWeight += weight
}
}
}
_, _, _, a := shape.Image.At(x, y).RGBA()
return color.RGBA{
uint8(sumR / sumWeight * (255.0 / 65535)),
uint8(sumG / sumWeight * (255.0 / 65535)),
uint8(sumB / sumWeight * (255.0 / 65535)),
uint8(a * 255 / 65535),
}
}
type ColorRings struct {
Radius int
}
func (shape ColorRings) ColorModel() color.Model {
return color.RGBAModel
}
func (shape ColorRings) Bounds() image.Rectangle {
r := shape.Radius
return image.Rect(-r, -r, r, r)
}
func (shape ColorRings) At(x, y int) color.Color {
distance := int(math.Hypot(float64(x), float64(y)))
return RainbowColor(distance * 6 / shape.Radius)
}
func RainbowColor(n int) color.Color {
switch n {
case 0: return color.RGBA{0xFF, 0x00, 0x00, 255} // Red
case 1: return color.RGBA{0xFF, 0xA5, 0x00, 255} // Orange
case 2: return color.RGBA{0xFF, 0xFF, 0x00, 255} // Yellow
case 3: return color.RGBA{0x3C, 0xB3, 0x71, 255} // Medium Sea Green
case 4: return color.RGBA{0x1E, 0x90, 0xFF, 255} // Dodger Blue
case 5: return color.RGBA{0x93, 0x70, 0xDB, 255} // Medium Purple
}
return color.RGBA{0, 0, 0, 0}
}
</code></pre>
</section>
<section data-background="code/CiucasMountains-Romania.jpg">
<h2>blur.png</h2>
<p>And here's the resulting image:</p>
<img src="code/blur.png"/>
</section>
<section>
<h2>Sierpinski Carpet</h2>
Take a square carpet, divide it into nine smaller squares,
and remove the middle square. Recur on the remaining squares.
<br/>
<img src="code/183px-Menger_0.PNG"/>
<img src="code/183px-Menger_1.PNG"/>
<img src="code/183px-Menger_2.PNG"/>
<br/>
<img src="code/183px-Menger_3.PNG"/>
<img src="code/183px-Menger_4.PNG"/>
<img src="code/183px-Menger_5.PNG"/>
</section>
<section>
<h2>Sierpinski Carpet</h2>
<pre><code class="go" data-trim contenteditable>
func makeSampleImage() (string, image.Image) {
return "sierpinski.png", SierpinskiCarpet{}
}
type SierpinskiCarpet struct { }
func (shape SierpinskiCarpet) Bounds() image.Rectangle {
return image.Rect(0, 0, 850, 400)
}
func (shape SierpinskiCarpet) At(x, y int) color.Color {
for ; x > 0 || y > 0; x, y = x/3, y/3 {
if x%3 == 1 && y%3 == 1 {
return color.RGBA{0, 0, 0, 0}
}
}
return color.RGBA{0,55,255,255};
}
</code></pre>
</section>
<section data-background="code/CiucasMountains-Romania.jpg">
<h2>sierpinski.png</h2>
<p>And here's the resulting image:</p>
<img src="code/sierpinski.png"/>
</section>
<section>
<h2>Mandelbrot Set</h2>
<pre><code class="go" data-trim contenteditable>
func makeSampleImage() (string, image.Image) {
return "mandelbrot.png", Mandelbrot{
Size: image.Point{700, 400},
Cx: -1,
Cy: -0.285,
Zoom: 120,
}
}
type Mandelbrot struct {
Size image.Point
Cx float64
Cy float64
Zoom float64
}
func (shape Mandelbrot) At(xi, yi int) color.Color {
x0 := float64(xi-shape.Size.X/2)/(shape.Zoom*100) + shape.Cx
y0 := float64(yi-shape.Size.Y/2)/(shape.Zoom*100) + shape.Cy
x, y := x0, y0
xx, yy := x*x, y*y
iteration := 0
max_iterations := 1000
for xx+yy < 4 && iteration < max_iterations {
x, y = xx-yy+x0, 2*x*y+y0
xx, yy = x*x, y*y
iteration += 1
}
return ThousandColors(iteration)
}
func RainbowColor(n int) color.Color {
switch n {
case 0: return color.RGBA{0xFF, 0x00, 0x00, 255} // Red
case 1: return color.RGBA{0xFF, 0xA5, 0x00, 255} // Orange
case 2: return color.RGBA{0xFF, 0xFF, 0x00, 255} // Yellow
case 3: return color.RGBA{0x3C, 0xB3, 0x71, 255} // Medium Sea Green
case 4: return color.RGBA{0x1E, 0x90, 0xFF, 255} // Dodger Blue
case 5: return color.RGBA{0x93, 0x70, 0xDB, 255} // Medium Purple
}
return color.RGBA{0, 0, 0, 0}
}
func ThousandColors(n int) color.Color {
// Interpolate a thousand colors between yellow and purple.
if n < 0 || n >= 1000 {
return color.RGBA{0, 0, 0, 0}
}
const W = 250 // band width: number of values to map between 2 adjacent colors
i := n / W // convert 0..999 to 0..3
c1 := RainbowColor(i + 2)
c2 := RainbowColor(i + 3)
// interpolate between c1 and c2
d := uint32(n % W) // d is 0..W-1, the relative distance between c1 & c2
d2 := (W - 1) - d // d2 is W-1..0, the relative distance between c2 & c1
r1, g1, b1, a1 := c1.RGBA()
r2, g2, b2, a2 := c2.RGBA()
r := ((r1*d2 + r2*d) / W) * 255 / 65535
g := ((g1*d2 + g2*d) / W) * 255 / 65535
b := ((b1*d2 + b2*d) / W) * 255 / 65535
a := ((a1*d2 + a2*d) / W) * 255 / 65535
return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
}
</code></pre>
</section>
<section data-background="code/CiucasMountains-Romania.jpg">
<h2>mandelbrot.png</h2>
<p>And here's the resulting image:</p>
<img src="code/mandelbrot.png"/>
</section>
<section>
<h2>The Big Picture</h2>
<p>We can also create an <code>Image</code> by composing
smaller <code>Image</code>s.</p>
<pre><code class="go" data-trim contenteditable>
func makeSampleImage() (string, image.Image) {
circle := Circle{Radius: 100, Color: RainbowColor(3)}
mandelbrot := Mandelbrot{
Size: image.Point{350, 200},
Cx: -0.75,
Cy: -0.0,
Zoom: 1,
}
gingham := Gingham{Size: image.Point{175, 175}}
composedImage := hcAppend(circle, mandelbrot, gingham)
return "composite.png", composedImage
}
type Composite struct {
images []image.Image
bounds []image.Rectangle
}
func (shape Composite) Bounds() image.Rectangle {
if len(shape.images) == 0 {
return image.Rect(0, 0, 0, 0)
}
bounds := shape.bounds[0]
for _, b := range shape.bounds {
bounds = bounds.Union(b)
}
return bounds
}
func (shape Composite) At(x, y int) color.Color {
p := image.Point{x, y}
for i, b := range shape.bounds {
if p.In(b) {
img := shape.images[i]
ib := img.Bounds()
p = p.Sub(b.Min).Add(ib.Min)
return img.At(p.X, p.Y)
}
}
return color.RGBA{0, 0, 0, 0}
}
func NewComposite() Composite {
return Composite{make([]image.Image, 0), make([]image.Rectangle, 0)}
}
func maxheight(images []image.Image) int {
tallest := 0
for _, img := range images {
if h := img.Bounds().Dy(); h > tallest {
tallest = h
}
}
return tallest
}
func maxwidth(images []image.Image) int {
widest := 0
for _, img := range images {
if w := img.Bounds().Dx(); w > widest {
widest = w
}
}
return widest
}
// hcAppend builds a composite image from a list of images, arranging them
// horizontally from left to right, with adjacent images touching, and with
// their vertical centers aligned. The 'h' in 'hc' means horizontally-arranged,
// and the 'c' in 'hc' means center-aligned.
func hcAppend(images ...image.Image) image.Image {
shape := NewComposite()
tallest := maxheight(images)
left := 0
for _, img := range images {
sz := img.Bounds().Size()
w, h := sz.X, sz.Y
top := (tallest - h) / 2
shape.images = append(shape.images, img)
shape.bounds = append(shape.bounds, image.Rect(left, top, left+w, top+h))
left += w
}
return shape
}
// vcAppend builds a composite image from a list of images, arranging them
// vertically from top to bottom, with adjacent images touching, and with their
// horizontal centers aligned. The 'v' in 'vc' means vertically-arranged, and
// the 'c' in 'vc' means center-aligned.
func vcAppend(images ...image.Image) image.Image {
shape := NewComposite()
widest := maxwidth(images)
top := 0
for _, img := range images {
sz := img.Bounds().Size()
w, h := sz.X, sz.Y
left := (widest - w) / 2
shape.images = append(shape.images, img)
shape.bounds = append(shape.bounds, image.Rect(left, top, left+w, top+h))
top += h
}
return shape
}
</code></pre>
</section>
<section data-background="code/CiucasMountains-Romania.jpg">
<h2>composite.png</h2>
<p>And here's the resulting image:</p>
<img src="code/composite.png"/>
</section>
<section>
<h2>The <code>image/draw</code> Package</h2>
<p>Package <code>image/draw</code> defines only one operation:
drawing a source image onto a destination image, through an optional
mask image. This one operation is surprisingly versatile and can
perform a number of common image manipulation tasks elegantly and
efficiently.</p>
<pre><code class="go" data-trim contenteditable>
func DrawMask(dst image.Image, r image.Rectangle,
src image.Image, sp image.Point,
mask image.Image, mp image.Point,
op Op)
</code></pre>
</section>
<section>
<img src="code/go-imagedraw-package_20.png" width="800" height="600"/>
</section>
<section>
<h2>A Relevant Example</h2>
<p>Check out this gnarly gopher. (Or is that "gnawrly"?)</p>
<img src="code/gophers1.jpg"/>
</section>
<section>
<h2>A Gopher Chewing Holes in a Carpet</h2>
<p>Let's copy that gopher into a PNG file, using the Sierpinski
Carpet as a mask.</p>
<pre><code class="go" data-trim contenteditable>
func makeSampleImage() (string, image.Image) {
gopher := loadGopher()
rect := gopher.Bounds()
carpet := SierpinskiCarpet{rect.Size(), image.Point{0, 200}}
// Copy the gopher into an RGBA image, using Sierpinski mask.
result := image.NewRGBA(rect)
draw.DrawMask(result, rect,
gopher, rect.Min,
carpet, image.ZP,
draw.Over)
return "mask.png", result
}
func loadGopher() image.Image {
inFilename := "gophers1.jpg"
inFile, err := os.Open(inFilename)
if err != nil { log.Fatal(err) }
defer inFile.Close()
log.Print("Reading image from: ", inFilename)
gopher, err := jpeg.Decode(inFile)
if err != nil { log.Fatal(err) }
return gopher
}
type SierpinskiCarpet struct {
Size image.Point
Offset image.Point
}
func (shape SierpinskiCarpet) Bounds() image.Rectangle {
return image.Rectangle{image.ZP, shape.Size}
}
func (shape SierpinskiCarpet) At(x, y int) color.Color {
x += shape.Offset.X
y += shape.Offset.Y
for ; x > 0 || y > 0; x, y = x/3, y/3 {
if x%3 == 1 && y%3 == 1 {
return color.RGBA{255, 255, 255, 255}
}
}
return color.RGBA{0,0,0,0};
}
</code></pre>
</section>
<section data-background="code/CiucasMountains-Romania.jpg">
<h2>mask.png</h2>
<p>And here's the resulting image:</p>
<img src="code/mask.png"/>
</section>
<section>
<h2>Interfaces</h2>
<p>All these examples of <code>Image</code> implementations are
interesting enough (or not, depending on your taste). But how
expensive is it to call methods through an interface,
when the concrete type is not known until runtime?
</p>
<p>
We'll need to cover some background first,
but eventually we'll have an answer to this question.
</p>
</section>
<section>
<h2>An Acknowledgement</h2>
<p>
<em>
Before we go any further, I'd like to acknowledge my
indebtedness to Russ Cox and his excellent blog post on this
subject, available at
<a="http://research.swtch.com/interfaces">
http://research.swtch.com/interfaces</a>.
The discussion that follows is a summary of that post, and
I have shamelessly borrowed his pictures and examples.
</em>
</p>
<p>
<em>
The post is five years old, so some details are likely to have
changed.
</em>
</p>
</section>
<section>
<h2>A Sample Interface</h2>
<p>
It will be helpful to have a specific example as we discuss
interface implementation. So let's define the interface
<code>Stringer</code>…
</p>
<pre><code class="go" data-trim contenteditable>
type Stringer interface {
String() string
}
</code></pre>
</section>
<section>
<h2>Interface Operations</h2>
<pre><code class="go" data-trim contenteditable>
func ToString(any interface{}) string {
if v, ok := any.(Stringer); ok {
return v.String()
}
switch v := any.(type) {
case int:
return strconv.Itoa(v)
case float:
return strconv.Ftoa(v, 'g', -1)
}
return "???"
}
</code></pre>
<p>This code uses interface values in three different ways:</p>
<ul>
<li>Call a method defined by the interface.</li>
<li>Convert to another type (a <em>type assertion</em>).</li>
<li>Get the concrete type.</li>
</ul>
<p>
So the implementation of interfaces must support (at least)
these three use cases.
</p>
</section>
<section>
<h2>A Sample Implementation</h2>
<p>Now, let's define a type that implements <code>Stringer</code>.</p>
<pre><code class="go" data-trim contenteditable>
type Binary uint64
func (i Binary) String() string {
return strconv.FormatUint(i.Get(), 2)
}
func (i Binary) Get() uint64 {
return uint64(i)
}
</code></pre>
</section>
<section>
<h2>A Binary Value</h2>
<p>
A value of type <code>Binary</code> is just a 64-bit integer
made up of two 32-bit words (we'll assume a 32-bit machine).
So, when we assign a value to <code>var b Binary</code>, the
situation looks like this:
</p>
<img src="code/gointer1.png" style="border: 10px solid white;"/>
<p>
That's pretty straightforward. But what happens when we
copy <code>b</code> into a variable of type <code>Stringer</code>?
</section>
<section>
<h2>A Stringer Value</h2>
<p>An interface variable is a two-word pair, containing information
about the concrete type and the actual value stored in the
interface variable.</p>
<p>
<img src="code/gointer2.png"
style="border: 10px solid white;"
align="left" />
The info about the concrete type is a pointer to an
<em>itable</em>,
the first item of which is a pointer to the run-time
representation of type <code>Binary</code>; it also contains a
pointer to <code>Binary</code>'s <code>String()</code> method.
The info about the actual data is a pointer to a heap-allocated
copy of the original <code>Binary</code> value.
</p>
</section>
<section>
<h2>The Cost of A Call</h2>
<p>So, how expensive <em>is</em> a method call on an interface?</p>
<br/>
<blockquote>
Actually, it's pretty cheap.
It's about as expensive as a virtual function call in C++.
</blockquote>
<br/>
</section>
<section>
<h2>Further Reading</h2>
<ul>
<li>Russ Cox: “<a href="http://research.swtch.com/interfaces">Go Data Structures: Interfaces</a>”
<br/>
A discussion of the <em>itable</em> data structure.
</li>
<li>
Rob Pike: “<a
href="http://blog.golang.org/laws-of-reflection">The
Laws of Reflection</a>”
<br/>
Explains type assertions,
and how the reflection library yields information
from an interface value's type/value pair.
</li>
<li>
Nigel Tao: “<a
href="http://blog.golang.org/go-image-package">The
Go image package</a>”
</li>
<li>
Nigel Tao: “<a
href="http://blog.golang.org/go-imagedraw-package">The
Go image/draw package</a>”
</li>
</ul>
</section>
<section id="setup">
<h2>Slideshow Setup</h3>
<p>
Special thanks to
<a href="http://hakim.se/">Hakim El Hattab</a>,
for the slideshow framework used to create this deck:
“<a
href="http://lab.hakim.se/reveal-js/#/">Reveal.js: HTML
Presentations Made Easy</a>.”
</p>
<h3>Themes</h3>
<p>
Reveal.js comes with a few themes built in: <br/>
<a href="?#/setup">Default</a> -
<a href="?theme=sky#/setup">Sky</a> -
<a href="?theme=beige#/setup">Beige</a> -
<a href="?theme=simple#/setup">Simple</a> -
<a href="?theme=serif#/setup">Serif</a> -
<a href="?theme=night#/setup">Night</a> <br/>
<a href="?theme=moon#/setup">Moon</a> -
<a href="?theme=solarized#/themes">Solarized</a>
</p>
<h3>Transition Styles</h3>
<p>
You can select from different transitions, like: <br>
<a href="?#/setup">Default</a> -
<a href="?transition=none#/setup">None</a> -
<a href="?transition=fade#/setup">Fade</a> -
<a href="?transition=slide#/setup">Slide</a> -
<a href="?transition=concave#/setup">Concave</a> -
<a href="?transition=zoom#/setup">Zoom</a>
</p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: 'simple',
//theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
//transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none
transition: 'linear',
// Parallax scrolling
// parallaxBackgroundImage: 'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg',
// parallaxBackgroundSize: '2100px 900px',
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>