|
| 1 | + |
| 2 | +module SampleImages |
| 3 | + |
| 4 | +using Colors |
| 5 | +using Base64 |
| 6 | + |
| 7 | +struct BeadsImageSVG <: Main.SVG |
| 8 | + buf::IOBuffer |
| 9 | +end |
| 10 | + |
| 11 | + |
| 12 | +# The following code is ad-hoc and highly depends on "beads.svg". |
| 13 | +# Be careful when modifying the svg file or its code. |
| 14 | +function BeadsImageSVG(caption::String; filter=nothing, width="64mm", height="36mm") |
| 15 | + io = IOBuffer() |
| 16 | + id = string(hash(caption), base=16) |
| 17 | + |
| 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 |
| 32 | + |
| 33 | + m = match(r"data:image/png;base64,([^\"]+)", line) |
| 34 | + if filter === nothing || m === nothing |
| 35 | + write(io, line) |
| 36 | + continue |
| 37 | + end |
| 38 | + |
| 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 |
| 54 | + |
| 55 | + |
| 56 | +function replace_palette(dest::IO, src::IO, filter) |
| 57 | + buf = IOBuffer() # to calculate chunk CRCs |
| 58 | + |
| 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) |
| 80 | + 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))) |
| 91 | +end |
| 92 | + |
| 93 | +end |
0 commit comments