Skip to content

Commit 165eac9

Browse files
committed
clarify that header bytes are exact in comments, add 1x1 test
1 parent 9a505f4 commit 165eac9

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/inline.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ function get_ind(sub_arr, arr)
9696
return findfirst(i -> all(sub_arr .== arr[i:i+nsub-1]) , 1:search_end)
9797
end
9898

99-
# get w, h from PNG base64
99+
# extract width and height from a PNG file header inside a base64 string
100100
function png_wh(img::String)
101-
decoded = base64decode(img[1:32]) # you need 13 + 8 bytes, this is a bit extra
101+
# PNG header is 8 bytes, 4 byte chunk size, 4 byte IHDR string, 8 bytes for w, h
102+
decoded = base64decode(img[1:32])
102103
ihdr = get_ind(b"IHDR", decoded) + 4 # find the location of the header
103104
w, h = ntoh.(reinterpret(Int32, decoded[ihdr:ihdr+8-1])) # get the 8 bytes after
104105
return w, h

test/inline.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import IJulia: InlineIOContext, png_wh
2525

2626
# test that we can extract a PNG header
2727
@test png_wh(
28-
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAMCAYAAACqYHctAAAAE0lEQVR42mNk+P+/ngENMI4MQQCgfR3py/"
29-
* "xS9AAAAABJRU5ErkJggg==") == (5,12)
28+
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAMCAYAAACqYHctAAAAE0lEQVR42mNk+P"
29+
* "+/ngENMI4MQQCgfR3py/xS9AAAAABJRU5ErkJggg==") == (5,12)
30+
@test png_wh(
31+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x"
32+
* "8AAwMCAO+ip1sAAAAASUVORK5CYII=") == (1,1)
3033
end

0 commit comments

Comments
 (0)