D Paste by sergk
Description: pngtest.d
Hide line numbers

Create new paste
Post a reply
View replies

Paste:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
import libpng.api;
import std.c.string;
import std.stream;
import std.stdio;

void main()
{
    scope (exit) png_destroy_read_struct(&png_ptr, &info_ptr, null);

    std.stream.Stream stream = new std.stream.File("pngtest.png");
    png_struct* png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING.ptr,
            null, &error_fn, null);
    png_set_error_fn(png_ptr, null, &error_fn, null);
    png_info* info_ptr = png_create_info_struct(png_ptr);
    png_set_read_fn(png_ptr, cast(png_voidp)stream, &read_data_callback);

    png_read_info(png_ptr, info_ptr); // initialize info_ptr with actual values
    writefln("info_ptr.width:%d", info_ptr.width); // accessing directly NOT works!
    ulong width, height;
    int bit_depth, color_type;
    png_get_IHDR(png_ptr, info_ptr, 
            &width, &height, &bit_depth, &color_type, // retreive by inout works!
            null, null, null);
    writefln("width:%d", width);
    writefln("png_get_imagewidth:%d",
            png_get_image_width(png_ptr, info_ptr)); // retreive by return NOT works!
}

extern (C)
void read_data_callback(png_structp png_ptr, png_bytep buffer, png_size_t size)
{
    auto stream = cast(std.stream.Stream)png_get_io_ptr(png_ptr);
    stream.readBlock(buffer, size);
}

extern (C)
void error_fn(png_structp png_ptr, png_const_charp msg)
{
    throw new Exception(msg[0 .. strlen(msg)].dup);
}

Replies:

    (some replies deleted)