Skip to content

Commit 59063a7

Browse files
authored
Merge pull request #553 from kimikage/doc_cvd_webp
Use WebP for CVD simulation demo in docs instead of indexed PNGs
2 parents 71a6c38 + d6e55da commit 59063a7

File tree

5 files changed

+41
-80
lines changed

5 files changed

+41
-80
lines changed

docs/make.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
using Pkg
2+
dependents = ["ImageCore"] # direct
3+
idependents = ["WebP"] # indirect
4+
devdir = get(ENV, "JULIA_PKG_DEVDIR", nothing) # backup
5+
workdir = joinpath(@__DIR__, "work")
6+
ENV["JULIA_PKG_DEVDIR"] = workdir
7+
Pkg.activate(workdir)
8+
Pkg.develop(dependents, preserve=PRESERVE_DIRECT)
9+
ENV["JULIA_PKG_DEVDIR"] = devdir # restore
10+
for dep in dependents
11+
Pkg.activate(joinpath(workdir, dep))
12+
Pkg.compat("Colors", "< 1")
13+
end
14+
Pkg.activate(@__DIR__) # this project
15+
pkgspecs = [PackageSpec(path=joinpath(workdir, dep)) for dep in dependents]
16+
Pkg.develop(pkgspecs, preserve=PRESERVE_DIRECT)
17+
Pkg.add(idependents)
18+
119
using Documenter, Colors
220

321
abstract type SVG end
@@ -39,3 +57,6 @@ makedocs(
3957
deploydocs(
4058
repo = "github.com/JuliaGraphics/Colors.jl.git",
4159
target = "build")
60+
61+
62+
Pkg.rm([idependents; dependents], io=devnull)

docs/sampleimages.jl

Lines changed: 19 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,42 @@ module SampleImages
33

44
using Colors
55
using Base64
6+
using WebP
67

78
struct BeadsImageSVG <: Main.SVG
89
buf::IOBuffer
910
end
1011

12+
const beads = WebP.read_webp(joinpath(@__DIR__, "src", "assets", "figures", "beads.webp"))
1113

1214
# The following code is ad-hoc and highly depends on "beads.svg".
1315
# Be careful when modifying the svg file or its code.
1416
function BeadsImageSVG(caption::String; filter=nothing, width="64mm", height="36mm")
1517
io = IOBuffer()
16-
id = string(hash(caption), base=16)
1718

18-
open(joinpath("assets", "figures", "beads.svg"), "r") do file
19-
for line in eachline(file, keep=true)
20-
occursin("<?xml", line) && continue
21-
if occursin("<svg", line)
22-
line = replace(line, ">"=>
23-
""" width="$width" height="$height" style="display:inline; margin-left:1em; margin-bottom:1em">""")
24-
elseif occursin("filter_beads_g", line)
25-
line = replace(line, "filter_beads_g"=>"filter_"*id)
26-
elseif occursin("</svg>", line)
27-
text = """
28-
<text x="16" y="1000" style="fill:black;opacity:0.9;font-size:80px;">$caption</text>
29-
</svg>"""
30-
line = replace(line, "</svg>"=>text)
31-
end
19+
write(io, """<svg version="1.1" viewBox="0 0 1920 1080" """)
20+
write(io, """xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" """)
21+
write(io, """width="$width" height="$height" image-rendering="optimizeQuality" """)
22+
write(io, """style="display:inline; margin-left:1em; margin-bottom:1em">\n""")
3223

33-
m = match(r"data:image/png;base64,([^\"]+)", line)
34-
if filter === nothing || m === nothing
35-
write(io, line)
36-
continue
37-
end
24+
img = filter === nothing ? beads : filter.(beads)
25+
webp = WebP.encode(img, lossy=true, quality=85)
3826

39-
head = m.offsets[1]
40-
write(io, SubString(line, 1, head - 1))
41-
src = IOBuffer(m.captures[1])
42-
b64dec = Base64DecodePipe(src) # decode all for simplicity
43-
b64enc = Base64EncodePipe(io)
44-
write(b64enc, read(b64dec, 33)) # before the length of "PLTE"
45-
replace_palette(b64enc, b64dec, filter)
46-
n = write(b64enc, read(b64dec))
47-
close(b64enc)
48-
close(b64dec)
49-
write(io, SubString(line, head + length(m.captures[1]), length(line)))
50-
end
51-
end
52-
BeadsImageSVG(io)
53-
end
27+
write(io, """<image width="1920" height="1080" xlink:href="data:image/webp;base64,""")
5428

29+
b64enc = Base64EncodePipe(io)
30+
write(b64enc, webp)
31+
close(b64enc)
5532

56-
function replace_palette(dest::IO, src::IO, filter)
57-
buf = IOBuffer() # to calculate chunk CRCs
33+
write(io, """" />\n""")
5834

59-
u8(x) = write(buf, UInt8(x & 0xFF))
60-
u16(x) = (u8((x & 0xFFFF)>>8); u8(x))
61-
u32(x) = (u16((x & 0xFFFFFFFF)>>16); u16(x))
62-
function read_palette()
63-
uint32 = (UInt32(read(src, UInt8)) << 16) |
64-
(UInt32(read(src, UInt8)) << 8) | read(src, UInt8)
65-
reinterpret(RGB24, uint32)
66-
end
67-
function write_palette(c::Color)
68-
rgb24 = convert(RGB24,c)
69-
u8(rgb24.color>>16); u8(rgb24.color>>8); u8(rgb24.color)
70-
end
71-
crct(x) = (for i = 1:8; x = x & 1==1 ? 0xEDB88320 (x>>1) : x>>1 end; x)
72-
table = UInt32[crct(i) for i = 0x00:0xFF]
73-
function crc32()
74-
seekstart(buf)
75-
crc = 0xFFFFFFFF
76-
while !eof(buf)
77-
crc = (crc>>8) table[(crc&0xFF) read(buf, UInt8) + 1]
78-
end
79-
u32(crc 0xFFFFFFFF)
35+
for style in ("fill:white;stroke:white;stroke-width:20;opacity:0.2", "fill:black;opacity:0.9")
36+
write(io,"""<text x="16" y="1000" style="$style;font-size:80px;">$caption</text>\n""")
8037
end
81-
lenbytes = read(src, 4)
82-
len = (UInt32(lenbytes[3]) << 8) | lenbytes[4]
83-
write(dest, lenbytes)
84-
write(buf, read(src, 4)) # "PLTE"
85-
for i = 1:(len÷3)
86-
write_palette(filter(read_palette()))
87-
end
88-
read(src, 4) # CRC
89-
crc32()
90-
write(dest, take!(seekstart(buf)))
38+
39+
write(io, """</svg>\n""")
40+
41+
BeadsImageSVG(io)
9142
end
9243

9344
end

0 commit comments

Comments
 (0)