Stereogram Algorithms - Text
Stereograms are usually made by manipulating individual pixels - random dots (pixels)
or tiled background images (also made of pixels). But it is also possible to create
stereograms using text characters. Text-based stereograms are called Single Image
Random Text Stereograms (SIRTS).
Although text stereograms do not offer as rich a 3D image as pixel-based stereograms,
they continue to enjoy some popularity because they are easy to create without the
need for special software. Text stereograms can be created manually (if your
penmanship is good), using a typewriter, or by using a simple text editor such as
Notepad.
Whether text or pixel based, stereograms are created one line at a time. Each line is
in fact a complete stereogram but whereas a line of pixels is too small for the human
eye to detect the 3D content, the 3D content of a single line text stereogram can be
seen by most people.
Text Stereogram Example
Consider the following line consisting of just the repetitive
text "Gary Beene".
Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene
There is no 3D information in the line above. But by applying the algorithm
described below, the following single line stereogram can be created:
Gary Beene Gary Beene Ga Beene Ga Beene Gaor Beene Gaor Beene Gao
Even though the line above is a complete stereogram, most people can
more easily view the 3D content when viewing multiple lines of the
stereogram, as in the the following 12-line text stereogram (same line
as above, just repeated 12 times):
Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene
Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene
Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene
Gary Beene Gary Beene Ga Beene Ga Beene Gaor Beene Gaor Beene Gao
Gary Beene Gary Beene Ga Beene Ga Beene Gaor Beene Gaor Beene Gao
Gary Beene Gary Beene Ga Beene Ga Beene Gaor Beene Gaor Beene Gao
Gary Beene Gary Beene Ga Beene Ga Beene Gaor Beene Gaor Beene Gao
Gary Beene Gary Beene Ga Beene Ga Beene Gaor Beene Gaor Beene Gao
Gary Beene Gary Beene Ga Beene Ga Beene Gaor Beene Gaor Beene Gao
Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene
Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene
Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene Gary Beene
In this example, a rectangle can be seen floating above the background.
Pattern Algorithm
The following source code implements what I call the "pattern" algorithm
for creating a text stereogram. I wrote the code based on algorithms
descriptions from several Internet sites, as well as the algorithm described
in the book "Hidden Images: Making Random Dot Stereograms" by Bob Hankinson
and Alfonso Hermida (1994).
In general, the algorithm involves creating a stereogram line by line.
Each line consists of a text pattern which is repeated left to right.
The repeating pattern is modified in accordance with a depth mask
which describes the 3D image content. As image levels increase text
characters are dropped from the pattern and as image levels decrease
text characters are added to the pattern. Additional discussion is
provided following the source code.
1 Dim Pattern As String, Stereogram As String, PatternWidth as Long
2 Dim PL As Long, NL As Long, Ptr As Long, i As Long, Stereogram as String
3 maxX = picOut.ScaleWidth / picOut.TextWidth("a")
4 maxY = picOut.ScaleHeight / picOut.TextHeight("a")
5 ReDim hArray(maxX, maxY): CreateShapeDepthArray: PatternWidth = 12
6 For y = 0 To maxY - 1
7 Pattern = "": Ptr = 1: PL = 0
8 For i = 1 To PatternWidth: Pattern = Pattern & Chr(Int(Rnd * 95 + 32)): Next i
9 Stereogram = Stereogram & Pattern
10 For X = 0 To maxX - 1
11 NL = hArray(X, y)
12 If NL > PL Then
13 For i = 1 To NL - PL
14 Pattern = Left(Pattern, Ptr - 1) & Mid$(Pattern, Ptr + 1)
15 If Ptr > Len(Pattern) Then Ptr = 1
16 Next i
17 PL = NL
18 ElseIf NL < PL Then
19 For i = 1 To PL - NL
20 Pattern = Left(Pattern, Ptr - 1) & Chr(Int(Rnd * 95 + 32)) & Mid$(Pattern, Ptr)
21 Next i
22 PL = NL
23 End If
24 Stereogram = Stereogram & Mid$(Pattern, Ptr, 1)
25 Ptr = Ptr + 1
26 If Ptr > Len(Pattern) Then Ptr = 1
27 Next X
28 Stereogram = Stereogram & vbCrLf
29 Next y
30 picOut.Cls: picOut.Picture = LoadPicture("")
31 picOut.CurrentX = 0: picOut.CurrentY = 0: picOut.Print Stereogram
The following source code implements the same pattern algorithm. It is
more cryptic but takes only half the number of lines of code. This code
is based on work published by A. Fontana. It uses a very clever technique
for handling the pattern modifications.
1 Dim iLevel As Long, minString As Long, Pattern As String, Diff As Long
2 maxX = picOut.ScaleWidth / picOut.TextWidth("a")
3 maxY = picOut.ScaleHeight / picOut.TextHeight("a")
4 ReDim hArray(maxX, maxY): CreateShapeDepthArray: PatternWidth = 12
5 For y = 0 To maxY - 1
6 iLevel = 0: Diff = PatternWidth
7 For i = 1 To PatternWidth: Pattern = Pattern & Chr(Int(Rnd * 95 + 32)): Next i
8 For X = 0 To maxX - 1
9 If hArray(X, y) <> iLevel Then
10 Diff = minString - hArray(X, y)
11 iLevel = hArray(X, y)
12 End If
13 Pattern = Pattern & Left(Right(Pattern, Diff), 1)
14 Next X
15 Stereogram = Stereogram & Pattern & vbCrLf
16 Next y
Source Code Discussion
... in work
|