Skip to content

Commit

Permalink
add load magick from buffer
Browse files Browse the repository at this point in the history
try:

irb(main):007:0> png_data = IO.read "k2.png"
irb(main):007:0> require 'vips'
irb(main):007:0> reader = VIPS::MagickReader.new png_data
irb(main):007:0> im = reader.read_buffer
irb(main):007:0> im.x_size
=> 1450

see:

#69
  • Loading branch information
jcupitt committed Oct 15, 2015
1 parent 89fead4 commit e0dbd48
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# master

# Version 0.3.11

* added magick load from buffer [John Cupitt]

# Version 0.3.10

* added webp write [John Cupitt]
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
GEM
remote: http://rubygems.org/
specs:
rake (10.4.2)
addressable (2.3.8)
builder (3.2.2)
diff-lcs (1.2.5)
Expand Down Expand Up @@ -39,6 +38,7 @@ GEM
multi_xml (~> 0.5)
rack (~> 1.2)
rack (1.6.4)
rake (10.4.2)
rdoc (4.2.0)
json (~> 1.4)
rdoc-data (4.0.1)
Expand Down
26 changes: 26 additions & 0 deletions ext/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ jpeg_buf_internal(VALUE obj, VALUE buf, VALUE shrink, VALUE fail)
return img_init(cVIPSImage, im_new);
}

static VALUE
magick_buf_internal(VALUE obj, VALUE buf)
{
VipsImage *im_new;

im_new = NULL;

#if ATLEAST_VIPS( 8, 2 )
buf = StringValue(buf);

if (!(im_new = im_open("", "p")))
vips_lib_error();

if (im_bufmagick2vips(RSTRING_PTR(buf), RSTRING_LEN(buf), im_new, FALSE))
vips_lib_error();
#else
rb_raise(eVIPSError, "This method is not implemented in your version of VIPS");
#endif

return img_init(cVIPSImage, im_new);
}

static VALUE
png_buf_internal(VALUE obj, VALUE buf)
{
Expand Down Expand Up @@ -214,6 +236,10 @@ init_Reader(void)
rb_define_private_method(magick_reader, "read_internal", magick_read_internal, 2);
reader_fmt_set(magick_reader, "magick");

magick_reader = rb_define_class_under(mVIPS, "MagickReader", reader);
rb_define_private_method(magick_reader, "buf_internal", magick_buf_internal, 1);
reader_fmt_set(magick_reader, "magick");

/*
* Read Analyze images.
*/
Expand Down
6 changes: 6 additions & 0 deletions lib/vips/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ def read_buffer
end
end

class MagickReader < Reader
def read_buffer
@_im = buf_internal @path
end
end

class Image

# Load a ppm file straight to a VIPS Image.
Expand Down
2 changes: 1 addition & 1 deletion lib/vips/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module VIPS
VERSION = "0.3.10"
VERSION = "0.3.11"
end
9 changes: 9 additions & 0 deletions spec/vips/magick_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@
res = VIPS::MagickReader.recognized? @path
res.should == true
end

it "should read a magick file from memory" do
if Spec::Helpers.match_vips_version(">= 8.2")
png_data = IO.read(@path)
reader = VIPS::MagickReader.new(png_data)
im = reader.read_buffer
im.x_size.should == @image.x_size
end
end
end

0 comments on commit e0dbd48

Please sign in to comment.