diff --git a/src/inline.jl b/src/inline.jl index f687bd28..d30d102b 100644 --- a/src/inline.jl +++ b/src/inline.jl @@ -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 diff --git a/test/inline.jl b/test/inline.jl index d07dfc31..9d2e0878 100644 --- a/test/inline.jl +++ b/test/inline.jl @@ -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