Skip to content

Commit 798b2b0

Browse files
committed
cleanups for new buffer read
use vips8 API for buffer read/write stuff, if possible, it improves compatibility fix an oops in version #if-ing
1 parent 9df6029 commit 798b2b0

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ Image.jpeg('large.png', :sequential => true).shrink(2).png('out.png')
185185
# If you want to let vips determine file formats, you can use the generic
186186
# reader and writer:
187187
Image.new('mypic.jpg').shrink(2).write('out.png')
188+
189+
# You can also read and write images from memory areas. For example:
190+
191+
jpeg_data = IO.read('mypic.jpg')
192+
reader = JPEGReader.new(jpeg_data, :shrink_factor => 2, :fail_on_warn => true)
193+
im = reader.read_buffer
194+
195+
# brighten
196+
im = im.lin(1.5, 0)
197+
198+
# As above, the image will not be processed until the .to_memory() method
199+
# is called, and then will only decompress the section being processed.
200+
# You will need to have all of the compressed data in memory at once though.
201+
202+
writer = JPEGWriter.new(im, :compression => 2, :interlace => false)
203+
png_data = writer.to_memory
204+
IO.write('out.png', png_data)
205+
206+
writer = JPEGWriter.new(im, :quality => 50)
207+
jpeg_data = writer.to_memory
208+
IO.write('out.jpg', jpeg_data)
209+
210+
# VIPS currently only suports JPEG and PNG memory compress and decompress.
211+
# We hope to add other formats in future.
188212
```
189213

190214
## Why use ruby-vips?

ext/reader.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,19 @@ jpeg_buf_internal(VALUE obj, VALUE buf, VALUE shrink, VALUE fail)
5656
{
5757
VipsImage *im_new;
5858

59+
im_new = NULL;
60+
61+
#if IM_MAJOR_VERSION >= 7 || IM_MINOR_VERSION >= 28
5962
buf = StringValue(buf);
6063

6164
if (vips_jpegload_buffer(RSTRING_PTR(buf), RSTRING_LEN(buf), &im_new,
6265
"shrink", NUM2INT(shrink),
6366
"fail", NUM2INT(fail),
6467
NULL))
6568
vips_lib_error();
69+
#else
70+
rb_raise(eVIPSError, "This method is not implemented in your version of VIPS");
71+
#endif
6672

6773
return img_init(cVIPSImage, im_new);
6874
}

ext/ruby_vips.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ init_vips_library()
130130
/* We use the vips7 interface, so the vips8 cache will not help us.
131131
* Disable it and save 100mb or so of memory in vips-7.28 and later.
132132
*/
133-
#if IM_MAJOR_VERSION >= 7 && IM_MINOR_VERSION >= 28
133+
#if IM_MAJOR_VERSION >= 7 || IM_MINOR_VERSION >= 28
134134
vips_cache_set_max_mem( 0 );
135135
#endif
136136
}

ext/writer.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,23 @@ jpeg_buf_internal(VALUE obj, VALUE quality)
147147

148148
GetImg(obj, data, im);
149149

150+
#if IM_MAJOR_VERSION >= 7 || IM_MINOR_VERSION >= 28
151+
{
152+
size_t len;
153+
154+
if (vips_jpegsave_buffer(im, &buf, &len,
155+
"Q", NUM2INT(quality),
156+
NULL))
157+
vips_lib_error();
158+
159+
/* Argh.
160+
*/
161+
length = len;
162+
}
163+
#else
150164
if (im_vips2bufjpeg(im, NULL, NUM2INT(quality), &buf, &length))
151165
vips_lib_error();
166+
#endif
152167

153168
return rb_tainted_str_new(buf, length);
154169
}
@@ -197,19 +212,25 @@ ppm_write_internal(VALUE obj, VALUE path)
197212
static VALUE
198213
png_buf_internal(VALUE obj, VALUE compression, VALUE interlace)
199214
{
200-
#if IM_MAJOR_VERSION > 7 || IM_MINOR_VERSION >= 23
201215
char *buf;
202216
size_t length;
203217
GetImg(obj, data, im);
204218

219+
#if IM_MAJOR_VERSION > 7 || IM_MINOR_VERSION >= 28
220+
if (vips_pngsave_buffer(im, &buf, &length,
221+
"compression", NUM2INT(compression),
222+
"interlace", NUM2INT(interlace),
223+
NULL))
224+
vips_lib_error();
225+
#elif IM_MAJOR_VERSION > 7 || IM_MINOR_VERSION >= 23
205226
if (im_vips2bufpng(im, NULL, NUM2INT(compression), NUM2INT(interlace),
206227
&buf, &length))
207228
vips_lib_error();
208-
209-
return rb_tainted_str_new(buf, length);
210229
#else
211230
rb_raise(eVIPSError, "This method is not implemented in your version of VIPS");
212231
#endif
232+
233+
return rb_tainted_str_new(buf, length);
213234
}
214235

215236
/* :nodoc: */

0 commit comments

Comments
 (0)