Module: Axon::PNG

Defined in:
ext/axon/png.c

Defined Under Namespace

Classes: Reader

Class Method Summary collapse

Class Method Details

.write(image_in, io_out) ⇒ Integer

Writes the given image image_in to io_out as compressed PNG data. Returns the number of bytes written.

image = Axon::Solid.new(200, 300)
io = File.open("test.jpg", "w")
Axon::PNG.write(image, io)     #=> 1234

Returns:

  • (Integer)


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'ext/axon/png.c', line 178

static VALUE
write_png(VALUE self, VALUE image_in, VALUE io_out)
{
    VALUE ensure_args[3];
    png_structp png_ptr;
    png_infop info_ptr;
    struct io_write data;

    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
              (png_error_ptr)png_error_fn,
              (png_error_ptr)png_warning_fn);

    if (png_ptr == NULL)
  rb_raise(rb_eRuntimeError, "unable to allocate a png object");

    info_ptr = png_create_info_struct(png_ptr);
    if (info_ptr == NULL) {
  png_destroy_write_struct(&png_ptr, (png_info **)NULL);
  rb_raise(rb_eRuntimeError, "unable to allocate a png info object");
    }

    data.io = io_out;
    data.total = 0;
    png_set_write_fn(png_ptr, (void *)&data, write_data, flush_data);

    ensure_args[0] = (VALUE)png_ptr;
    ensure_args[1] = (VALUE)info_ptr;
    ensure_args[2] = image_in;

    return rb_ensure(write_png2, (VALUE)ensure_args, write_png2_ensure,
                     (VALUE)ensure_args);
}