Skip to content

Commit 08cbafb

Browse files
authored
Add another method to compute anaglyphs based on grdview. (#1804)
* Add another method to compute anaglyphs based on grdview. * Fix syntax errors.
1 parent 71382b0 commit 08cbafb

3 files changed

Lines changed: 59 additions & 11 deletions

File tree

src/common_options.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4193,7 +4193,7 @@ function showfig(d::Dict{Symbol, Any}, fname_ps::String, fname_ext::String, opt_
41934193
gmt(pscvt_cmd * fname_ps * opt_T * " *")
41944194
reset_theme()
41954195
out::String = fname_ps[1:end-2] * fname_ext
4196-
(fname != "") && (out = mv(out, fname, force=true))
4196+
(fname != "" && fname != out) && (out = mv(out, fname, force=true))
41974197
elseif (fname_ps != "")
41984198
(K) && close_PS_file(fname_ps) # Close the PS file first
41994199
out = (fname != "") ? mv(fname_ps, fname, force=true) : fname_ps

src/extras/anaglyph.jl

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,55 @@
11
"""
2-
I = anaglyph(G|fname, vscale=1, sscale=2; kw...)
2+
I = anaglyph(G|fname; vscale=1, sscale=2, kw...)
3+
4+
---
5+
6+
I = anaglyph(G|fname; view3d::Bool=false, zsize=4, azim=190, dazim=2, cmap="gray", kw...)
7+
8+
---
39
410
Generate an anaglyph image from the input grid `G`.
511
612
### Args
713
- `G`: The input GMTgrid or filename of data to be processed.
8-
- `vscale`: Vertical scale factor (default: 1).
9-
- `sscale`: Stereo separation scale factor (default: 2).
1014
1115
### Kwargs
16+
- `vscale`: Terrain vertical scale factor (default: 1).
17+
- `sscale`: Stereo separation scale factor (default: 2).
1218
- `R`: Region of interest when reading a grid from disk (default: entire grid).
13-
Ignored when `G` is a GMTgrid.
19+
Ignored when `G` is a GMTgrid.
20+
- `view3d`: If true, selects an alternative method that generates 2 3D views using the `grdview` program
21+
and construct the anaglyph from those two images (default: false).
22+
- `zsize`: z-axis size of the 3D view. Same as in `grdview` (default: 4 cm).
23+
- `azim`: Azimuth of the 3D view (default: 190).
24+
- `dazim`: Azimuth step (default: 2). It means, create the anaglyph from the pair of images obtained
25+
with `azim` and `azim - dazim`.
26+
- `cmap`: Color map (default: "gray").
1427
1528
### Returns
1629
An anaglyph image suitable for viewing with red-cyan glasses.
1730
31+
### Credits
32+
The method that uses the grid's gradient is based on an ancient program called ManipRaster by Tierrt Souriot.
33+
The second method, the one that uses the `grdview` program, was proposed by Tim Hume in the GMT forum.
34+
(https://forum.generic-mapping-tools.org/t/bringing-the-third-dimension-to-gmt-stereograms/6189)
35+
1836
### Example
19-
```julia
37+
```
2038
I = anaglyph("@earth_relief_30s", region="-13/-5.5/35/44")
2139
```
2240
"""
23-
function anaglyph(fname::String, vscale=1, sscale=2; kw...)
41+
function anaglyph(fname::String; vscale=1, sscale=2, view3d::Bool=false, zsize=4, azim=190, dazim=2, cmap="gray", kw...)
42+
2443
d = KW(kw)
2544
opt_R::String = parse_R(d, "")[2]
2645
G = (opt_R === "") ? gmtread(fname) : gmtread(fname, R=opt_R[4:end])
2746
!isa(G, GMTgrid) && error("Input must be a GMTgrid")
28-
anaglyph(G, vscale, sscale)
47+
return view3d ? anaglyph_3d(G, zsize=zsize, azim=azim, dazim=dazim, cmap=cmap) : anaglyph(G, vscale=vscale, sscale=sscale)
2948
end
30-
function anaglyph(G::GMTgrid, vscale=1, sscale=2; kw...)
49+
50+
function anaglyph(G::GMTgrid; vscale=1, sscale=2, view3d=false, zsize=4, azim=190, dazim=2, cmap="gray")
51+
52+
view3d && return anaglyph_3d(G, zsize=zsize, azim=azim, dazim=dazim, cmap=cmap)
3153

3254
m_scale = -vscale / 50 # Amp factor
3355

@@ -88,4 +110,29 @@ function anaglyph(G::GMTgrid, vscale=1, sscale=2; kw...)
88110
left .= UInt8(0); right .= UInt8(0)
89111
end
90112
mat2img(argg, G)
91-
end
113+
end
114+
115+
# ---------------------------------------------------------------------------------------------------
116+
function anaglyph_3d(G::GMTgrid; zsize=4, azim=190, dazim=2, cmap="gray")
117+
118+
p_left, p_right = "$(azim)/30/0", "$(azim - dazim)/30/0"
119+
pato = TMPDIR_USR[1] * "/" * "GMTjl_" * TMPDIR_USR[2] * TMPDIR_USR[3]
120+
grdview(G, J="Q20c", JZ="$zsize", B=:none, C=string(cmap), Q=:i, p=p_left, I=true, figname=pato * ".jpg")
121+
Il = gdalread(pato * ".jpg", "-b 1", layout="TCBa")
122+
nr_l, nc_l = size(Il,1), size(Il,2)
123+
grdview(G, J="Q20c", JZ="$zsize", B=:none, C=string(cmap), Q=:i, p=p_right, I=true, figname=pato * ".jpg")
124+
Ir = gdalread(pato * ".jpg", layout="TCBa")
125+
nr_r, nc_r = size(Ir,1), size(Ir,2)
126+
127+
center_left = (nr_l ÷ 2 + 1, nc_l ÷ 2 + 1)
128+
center_right = (nr_r ÷ 2 + 1, nc_r ÷ 2 + 1)
129+
130+
nc, nr = round(Int, min(nc_l, nc_r)*0.85), round(Int, min(nr_l, nr_r)*0.85)
131+
nc2, nr2 = div(nc, 2), div(nr, 2)
132+
133+
im_right = Ir[center_right[1]-nr2:center_right[1]+nr2 , center_right[2]-nc2:center_right[2]+nc2, :]
134+
im_left = Il[center_left[1]-nr2:center_left[1]+nr2 , center_left[2]-nc2:center_left[2]+nc2]
135+
im_right[:,:,1] = im_left
136+
im_right[:,:,2] = im_right[:,:,3]
137+
mat2img(im_right)
138+
end

test/test_imgfuns.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@
4343
binarize(I, [30, 80]);
4444

4545
# Anaglyph
46-
anaglyph("@earth_relief_10m", region=(-10.5, -5, 35, 44));
46+
anaglyph("@earth_relief_10m", region=(-10, -6, 36, 42));
47+
anaglyph("@earth_relief_10m", region=(-10, -6, 36, 42), view3d=true);
4748
end

0 commit comments

Comments
 (0)