Skip to content

Commit

Permalink
throw an error if the IHDR is missing from the PNG
Browse files Browse the repository at this point in the history
  • Loading branch information
xzackli committed Dec 8, 2020
1 parent 4c66eaf commit c9a7f1e
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,15 @@ function display(d::InlineDisplay, M::MIME, x)
end


# look for a short sequence in an array
function get_ind(sub_arr, arr)
nsub = length(sub_arr)
search_end = length(arr) - nsub + 1
return findfirst(i -> all(sub_arr .== arr[i:i+nsub-1]) , 1:search_end)
end

# extract width and height from a PNG file header inside a base64 string
function png_wh(img::String)
# PNG header is 8 bytes, 4 byte chunk size, 4 byte IHDR string, 8 bytes for w, h
decoded = base64decode(img[1:32])
ihdr = get_ind(b"IHDR", decoded) + 4 # find the location of the header
w, h = ntoh.(reinterpret(Int32, decoded[ihdr:ihdr+8-1])) # get the 8 bytes after
decoded = base64decode(img[1:32]) # Base64 encodes 6 bits per character
if any(decoded[13:16] .!= b"IHDR") # check if the header looks reasonable
throw(ArgumentError("Base64-encoded PNG has a badly formed header."))
end
w, h = ntoh.(reinterpret(Int32, decoded[17:24])) # get the 8 bytes after
return w, h
end

Expand Down

0 comments on commit c9a7f1e

Please sign in to comment.