Skip to content

Commit cca5fa2

Browse files
committed
Added introduction chapter and illustrations.
1 parent 1ad5fdb commit cca5fa2

File tree

16 files changed

+76
-25
lines changed

16 files changed

+76
-25
lines changed

ArtOfAscii/Chapters/01-ArtOfAscii.playgroundchapter/Pages/01-Introduction.playgroundpage/main.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,29 @@ import PlaygroundSupport
1010
PlaygroundPage.current.needsIndefiniteExecution = true
1111

1212
//#-end-hidden-code
13+
/*:
14+
# Art of ASCII — Introduction to Digital Image Processing
15+
16+
## What is an ASCII Art?
17+
18+
**ASCII (American Standard Code for Information Interchange)** is a standard for electronic communication, which
19+
contains 128 characters.
20+
21+
**ASCII arts** are artworks created with ASCII characters, which can be seen in many places where only textual data can
22+
be transmitted, for example, within a terminal.
23+
24+
* Experiment:
25+
Head to the panel beside, and *tap the **run** button*, have fun experiencing the ASCII effects I've created.
26+
27+
## What We'll Learn by Generating ASCII Arts?
28+
29+
Examining how to generate ASCII Arts is a great way to explore the world of image processing. You'll be introduced with
30+
**the RGB color model**, **histograms and equalization techniques** with piratical experiments, which can be a fun and
31+
delightful.
32+
33+
[Let's get started! 🏃‍♂️](@next)
34+
*/
35+
//#-hidden-code
36+
PlaygroundPage.current.assessmentStatus = .pass(message: nil)
37+
//#-end-hidden-code
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
If you're not familiar with *linear algebra*, the following figure explains how this **transform matrix** works.
2+
3+
![How Transform Matrix Works](transform-matrix.jpg)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>Hints</key>
6+
<array>
7+
<dict>
8+
<key>FileReference</key>
9+
<string>Hint.md</string>
10+
</dict>
11+
</array>
12+
</dict>
13+
</plist>

ArtOfAscii/Chapters/01-ArtOfAscii.playgroundchapter/Pages/02-HowImagesComposed.playgroundpage/main.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,26 @@ import UIKit
88
import PlaygroundSupport
99

1010
import BookCore
11+
import BookAPI
1112

1213
PlaygroundPage.current.needsIndefiniteExecution = true
1314

15+
func performCorrectnessCheck(matrix: [Double]) {
16+
if matrix == [
17+
1, 0, 0, 0,
18+
0, 1, 0, 0,
19+
0, 0, 1, 0,
20+
0, 0, 0, 1
21+
] {
22+
PlaygroundPage.current.assessmentStatus = .pass(
23+
message: """
24+
Congratulations, You've learnt how digital images composed.
25+
26+
Continue to [Preprocess Images for ASCII Art](@next)
27+
""")
28+
}
29+
}
30+
1431
//#-end-hidden-code
1532
/*:
1633
# Basics: How Images Composed
@@ -35,16 +52,11 @@ an extra **alpha** channel to describe the opacity of a color.
3552

3653
![RGB Color Model](rgb-model.png)
3754

38-
## Filters
39-
40-
**Filtering** is a general technique for image processing. A **digital image filter** resembles a mathematical function,
41-
which receives a raw image as a sequence of colors, performing a specific calculation and returns a new image.
42-
43-
### 🔨Your First Image Filter
55+
### 🔨Decomposing an Image
4456

4557
* Experiment:
46-
* You'll build a simple filter which separates red, green or blue component out from the source image.
47-
* Try completing following code snippet. Run your code and tap the *R, G and B* button to verify whether it works.
58+
* The following code separates red, green and blue components from an image by multiplying it with a transform matrix.
59+
* Try completing the code snippet. Run your code and tap the *R, G and B* button to verify whether it works.
4860
*/
4961

5062
//#-code-completion(everything, hide)
@@ -61,24 +73,20 @@ func applyRGBFilter(redEnabled: Bool,
6173
factorGreen = (greenEnabled ? 1 : 0)
6274
factorBlue = (blueEnabled ? 1 : 0)
6375

64-
// For most digital images, color components are arranged in the order of red, green, blue and alpha, pre pixel basis.
65-
// For alpha channel, 1 stands for opaque while 0 stands for transparent.
76+
// For most images, color components are arranged in the order of red, green, blue and alpha, per pixel basis.
77+
// For alpha channel, larger values means more opaque
6678
var filterMatrix: [Double] = [
6779
<#T##Red##Double#>, 0, 0, 0,
6880
0, <#T##Green##Double#>, 0, 0,
6981
0, 0, <#T##Blue##Double#>, 0,
7082
0, 0, 0, <#T##Alpha##Double#>
7183
]
7284
rawImage.multiplyByMatrix(matrix4x4: filterMatrix)
85+
86+
performCorrectnessCheck(matrix: filterMatrix)
7387
}
7488

7589
//#-end-editable-code
76-
/*:
77-
* Note:
78-
This code snippet transforms an image by multiplying it with a transform matrix. If you're not familiar with linear
79-
algebra, the following figure explains how this transform matrix works.
80-
(图)
81-
*/
8290
//#-hidden-code
8391
let remoteView = remoteViewAsLiveViewProxy()
8492
let eventListener = EventListener(proxy: remoteView) { message in

ArtOfAscii/Chapters/01-ArtOfAscii.playgroundchapter/Pages/03-GrayscaleHistogramEqualization.playgroundpage/main.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import UIKit
88
import PlaygroundSupport
99

1010
import BookCore
11+
import BookAPI
1112

1213
PlaygroundPage.current.needsIndefiniteExecution = true
1314

@@ -27,8 +28,8 @@ a dedicated set of coefficients can be applied.
2728
### 🔨Grayscale Filtering
2829

2930
* Experiment:
30-
* You'll build a filter to turn an image into grayscaled version.
31-
* Try completing following code snippet. Run your code and tap the *Grayscale* button to verify whether it works.
31+
* The following code turns an image into a grayscaled one.
32+
* Try completing the code snippet. Run your code and tap the *Grayscale* button to verify whether it works.
3233
*/
3334
//#-code-completion(everything, hide)
3435
//#-code-completion(literal, show, float, double, integer)
@@ -54,14 +55,14 @@ func applyGrayscaleFilter(rawImage: RawImage) {
5455

5556
Images with low contrast may produce ASCII arts that are hard to recognize, as the following figure shows.
5657

57-
(图)
58+
![Contrast Comparation](contrast-compare.jpg)
5859

5960
**Histogram** is an effective tool for visualizing the distribution of tones in an image. The *X axis* represents *the
6061
brightness* while the *Y axis* represents *the number of pixels at a specific brightness value*.
6162

62-
(图)
63+
![Histogram Examples](histogram-examples.jpg)
6364

64-
### 🔬Demystification of Histograms
65+
### 🔬Demystifing Histograms
6566

6667
* Experiment:
6768
* Choose an image and then tap 📊 to bring up the histogram. Tapping it again to show histograms for seperated red,
@@ -73,7 +74,7 @@ brightness* while the *Y axis* represents *the number of pixels at a specific br
7374
**Histogram Equalization** is a widely used technique to enhance the contrast of images by *redistributing tones to the
7475
whole brightness range*, spreading out pixels that has intense frequency.
7576

76-
(图)
77+
![Histogram Equalization](equalization.jpg)
7778

7879
### 🔨Equalizing an Image
7980

ArtOfAscii/Chapters/01-ArtOfAscii.playgroundchapter/Pages/04-Asciification.playgroundpage/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import UIKit
88
import PlaygroundSupport
99

10-
import BookAPI
1110
import BookCore
11+
import BookAPI
1212

1313
PlaygroundPage.current.needsIndefiniteExecution = true
1414

@@ -23,7 +23,7 @@ desired result, the font must be **monospaced**, which has fixed width for all c
2323

2424
Here is a **character map** built with font “Fira Code”, by arranging characters to fit a gradient from white to black.
2525

26-
(图)
26+
![Character Map](character-map.jpg)
2727

2828
*/
2929
//#-editable-code
@@ -78,7 +78,7 @@ func scaleImageForAsciification(rawImage: RawImage) -> RawImage? {
7878
//#-editable-code
7979

8080
func applyAsciification(rawImage: RawImage) -> UIImage? {
81-
let characterMap: [Character] = Array(characterMapStr)
81+
let characterMap = [Character](characterMapStr)
8282
let maxMappedBrightness = Double(characterMap.count - 1)
8383
var asciificationResult: String = ""
8484

-53.2 KB
Loading
-66.7 KB
Loading
-55.5 KB
Loading
Loading
-108 KB
Loading

0 commit comments

Comments
 (0)