Skip to content

Commit 9df6029

Browse files
committed
first try and sort-of works
try: ---------- require 'rubygems' require 'vips' include VIPS data = IO.read('/home/john/pics/shark.jpg') reader = JPEGReader.new(data, :shrink_factor => 2, :fail_on_warn => true) im = reader.read_buffer im.write('out.png') ----------
1 parent 81fc893 commit 9df6029

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

TODO

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
btw, there's a method called to_memory in JPEGWriter and PNGWriter, that works
2+
cool to retrieve the VIPS::Image as a memory buffer, will be cool if this
3+
method is moved up to Writer and then it handles the correct to_memory method
4+
using the image headers.
5+
6+
add from_memory? or should the open method accept a blob in place of a path?
7+
8+
9+
10+
111
TODO
212
* Verify that all memory gets released when vips ops return errors. Namely,
313
make sure that the allocated IMAGEs get released via ruby's free callbacks.

ext/reader.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ reader_read_internal(VALUE obj, VALUE path, VALUE flags)
5151
return img_init(cVIPSImage, im_new);
5252
}
5353

54+
static VALUE
55+
jpeg_buf_internal(VALUE obj, VALUE buf, VALUE shrink, VALUE fail)
56+
{
57+
VipsImage *im_new;
58+
59+
buf = StringValue(buf);
60+
61+
if (vips_jpegload_buffer(RSTRING_PTR(buf), RSTRING_LEN(buf), &im_new,
62+
"shrink", NUM2INT(shrink),
63+
"fail", NUM2INT(fail),
64+
NULL))
65+
vips_lib_error();
66+
67+
return img_init(cVIPSImage, im_new);
68+
}
69+
5470
/* :nodoc: */
5571

5672
static VALUE
@@ -118,6 +134,7 @@ init_Reader(void)
118134
*/
119135

120136
VALUE jpeg_reader = rb_define_class_under(mVIPS, "JPEGReader", reader);
137+
rb_define_private_method(jpeg_reader, "buf_internal", jpeg_buf_internal, 3);
121138
reader_fmt_set(jpeg_reader, "jpeg");
122139

123140
/*

lib/vips/reader.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ def read
7979
@_im = read_retry str, seq
8080
end
8181

82+
def b_to_i(b)
83+
if b
84+
1
85+
else
86+
0
87+
end
88+
end
89+
90+
def read_buffer
91+
@_im = buf_internal @path, @shrink_factor, b_to_i(@fail_on_warn)
92+
end
93+
8294
# Shrink the jpeg while reading from disk. This means that the entire image
8395
# never has to be loaded in memory. Shrink factor can be 1 (no shrink), 2,
8496
# 4, or 8.

lib/vips/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module VIPS
2-
VERSION = "0.3.5"
2+
VERSION = "0.3.6"
33
end

0 commit comments

Comments
 (0)