Created
February 5, 2013 05:03
-
-
Save jwmerrill/4712289 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run(`wget http://julialang.org/images/logo.png`) | |
# Open png file for reading | |
fp = ccall(:fopen, Ptr{Uint8}, (Ptr{Uint8}, Ptr{Uint8}), "logo.png", "r") | |
# Read first 8 bytes | |
initial_bytes = Array(Uint8, 8) | |
ccall(:fread, Ptr{Uint8}, (Ptr{Uint8}, Uint8, Uint8, Ptr{Uint8}), initial_bytes, 1, 8, fp) | |
# Check that we actually have a png file | |
@assert ccall((:png_sig_cmp, "libpng"), Bool, (Array{Uint8}, Int, Int), initial_bytes, 0, 8) | |
png_ptr = ccall((:png_create_read_struct, "libpng"), Ptr{Uint8}, (Ptr{Uint8}, Ptr{Uint8}, Ptr{Uint8}, Ptr{Uint8}), "1.5.13", C_NULL, C_NULL, C_NULL) | |
info_ptr = ccall((:png_create_info_struct, "libpng"), Ptr{Uint8}, (Ptr{Uint8}, ), png_ptr) | |
ccall((:png_init_io, "libpng"), Void, (Ptr{Uint8}, Ptr{Uint8}), png_ptr, fp) | |
# Alert libpng that we've already read the first 8 bytes of the file | |
ccall((:png_set_sig_bytes, "libpng"), Void, (Ptr{Uint8}, Uint8), png_ptr, 8); | |
ccall((:png_read_info, "libpng"), Void, (Ptr{Uint8}, Ptr{Uint8}), png_ptr, info_ptr) | |
height = ccall((:png_get_image_height, "libpng"), Int, (Ptr{Uint8}, Ptr{Uint8}), png_ptr, info_ptr) | |
width = ccall((:png_get_image_width, "libpng"), Int, (Ptr{Uint8}, Ptr{Uint8}), png_ptr, info_ptr) | |
bit_depth = ccall((:png_get_bit_depth, "libpng"), Int, (Ptr{Uint8}, Ptr{Uint8}), png_ptr, info_ptr) | |
channels = ccall((:png_get_channels, "libpng"), Int, (Ptr{Uint8}, Ptr{Uint8}), png_ptr, info_ptr) | |
# color_type = ccall((:png_get_color_type, "libpng"), String, (Ptr{Uint8}, Ptr{Uint8}), png_ptr, info_ptr) | |
rowbytes = ccall((:png_get_rowbytes, "libpng"), Int, (Ptr{Uint8}, Ptr{Uint8}), png_ptr, info_ptr) | |
pixel_type = (bit_depth == 8 ) ? Uint8 : Uint16; | |
rows = [ Array(pixel_type, rowbytes) for h in 1:height ] | |
for row in rows | |
ccall((:png_read_row, "libpng"), Void, (Ptr{Uint8}, Ptr{Uint8}, Ptr{Uint8}), png_ptr, row, C_NULL) | |
end | |
#so far so good, but I'd like to replace the for loop above with the following. Unfortunately, it segfaults. | |
# ccall((:png_read_image, "libpng"), Void, (Ptr{Uint8}, Array{Ptr{Uint8}}), png_ptr, rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment