Skip to content

Commit

Permalink
clarify that header bytes are exact in comments, add 1x1 test
Browse files Browse the repository at this point in the history
  • Loading branch information
xzackli committed Apr 22, 2020
1 parent 9a505f4 commit 165eac9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ function get_ind(sub_arr, arr)
return findfirst(i -> all(sub_arr .== arr[i:i+nsub-1]) , 1:search_end)
end

# get w, h from PNG base64
# extract width and height from a PNG file header inside a base64 string
function png_wh(img::String)
decoded = base64decode(img[1:32]) # you need 13 + 8 bytes, this is a bit extra
# 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
return w, h
Expand Down
7 changes: 5 additions & 2 deletions test/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import IJulia: InlineIOContext, png_wh

# test that we can extract a PNG header
@test png_wh(
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAMCAYAAACqYHctAAAAE0lEQVR42mNk+P+/ngENMI4MQQCgfR3py/"
* "xS9AAAAABJRU5ErkJggg==") == (5,12)
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAMCAYAAACqYHctAAAAE0lEQVR42mNk+P"
* "+/ngENMI4MQQCgfR3py/xS9AAAAABJRU5ErkJggg==") == (5,12)
@test png_wh(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x"
* "8AAwMCAO+ip1sAAAAASUVORK5CYII=") == (1,1)
end

0 comments on commit 165eac9

Please sign in to comment.