@@ -3,91 +3,42 @@ module SampleImages
3
3
4
4
using Colors
5
5
using Base64
6
+ using WebP
6
7
7
8
struct BeadsImageSVG <: Main.SVG
8
9
buf:: IOBuffer
9
10
end
10
11
12
+ const beads = WebP. read_webp (joinpath (@__DIR__ , " src" , " assets" , " figures" , " beads.webp" ))
11
13
12
14
# The following code is ad-hoc and highly depends on "beads.svg".
13
15
# Be careful when modifying the svg file or its code.
14
16
function BeadsImageSVG (caption:: String ; filter= nothing , width= " 64mm" , height= " 36mm" )
15
17
io = IOBuffer ()
16
- id = string (hash (caption), base= 16 )
17
18
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 """ )
32
23
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 )
38
26
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,""" )
54
28
29
+ b64enc = Base64EncodePipe (io)
30
+ write (b64enc, webp)
31
+ close (b64enc)
55
32
56
- function replace_palette (dest:: IO , src:: IO , filter)
57
- buf = IOBuffer () # to calculate chunk CRCs
33
+ write (io, """ " />\n """ )
58
34
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 """ )
80
37
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)
91
42
end
92
43
93
44
end
0 commit comments