From: Tilman Sauerbeck Date: Sun, 27 Mar 2005 18:35:51 +0000 (+0000) Subject: Added support for image compression parameters. X-Git-Tag: ruby-eet-0.1.1~7 X-Git-Url: http://git.code-monkey.de/?p=ruby-eet.git;a=commitdiff_plain;h=d7f7f0063af5a351fd41fa18eebc6025b26985af Added support for image compression parameters. --- diff --git a/ChangeLog b/ChangeLog index f0df52e..876f33a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,9 +1,11 @@ -- -$Id: ChangeLog 12 2005-03-27 16:31:30Z tilman $ +$Id: ChangeLog 13 2005-03-27 18:35:51Z tilman $ ++ 2005-03-27 Tilman Sauerbeck (tilman at code-monkey de) * README: Fixed casing of "Ruby-EET" + * ext/ext.c: Added support for compression parameters + to Eet::File#write_image and Eet::File#read_image 2005-03-27 Tilman Sauerbeck (tilman at code-monkey de) * Released version 0.1.0 diff --git a/ext/ext.c b/ext/ext.c index f7860d6..236df7c 100644 --- a/ext/ext.c +++ b/ext/ext.c @@ -1,5 +1,5 @@ /* - * $Id: ext.c 1 2005-03-26 01:45:38Z tilman $ + * $Id: ext.c 13 2005-03-27 18:35:51Z tilman $ * * Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de) * @@ -320,16 +320,18 @@ c_write (int argc, VALUE *argv, VALUE self) * Reads an image entry from _ef_ that is stored as _key_. * If the data cannot be read, an +IOError+ is raised, * otherwise an Array is returned that contains the image data, - * the image width, the image height and a boolean that indicates - * whether the image has an alpha channel or not. + * the image width, the image height, a boolean that indicates + * whether the image has an alpha channel or not and a hash + * that contains the compression options that were used to store + * the image (see Eet::File#write_image). */ static VALUE c_read_image (VALUE self, VALUE key) { - VALUE ret; + VALUE ret, comp; Eet_File **ef = NULL; void *data; - int w = 0, h = 0, has_alpha = 0; + int w = 0, h = 0, has_alpha = 0, level = 0, quality = 0, lossy = 0; Data_Get_Struct (self, Eet_File *, ef); @@ -339,13 +341,19 @@ c_read_image (VALUE self, VALUE key) CHECK_KEY (key); data = eet_data_image_read (*ef, StringValuePtr (key), &w, &h, - &has_alpha, NULL, NULL, NULL); + &has_alpha, &level, &quality, + &lossy); if (!data) rb_raise (rb_eIOError, "cannot read entry - %s", key); - ret = rb_ary_new3 (4, rb_str_new (data, w * h * 4), + comp = rb_hash_new (); + rb_hash_aset (comp, ID2SYM (rb_intern ("lossy")), INT2FIX (lossy)); + rb_hash_aset (comp, ID2SYM (rb_intern ("level")), INT2FIX (level)); + rb_hash_aset (comp, ID2SYM (rb_intern ("quality")), INT2FIX (quality)); + + ret = rb_ary_new3 (5, rb_str_new (data, w * h * 4), INT2FIX (w), INT2FIX (h), - has_alpha ? Qtrue : Qfalse); + has_alpha ? Qtrue : Qfalse, comp); free (data); return ret; @@ -353,10 +361,17 @@ c_read_image (VALUE self, VALUE key) /* * call-seq: - * ef.write_image(key, image_data, w, h [, alpha] ) -> integer + * ef.write_image(key, image_data, w, h [, alpha] [, comp] ) -> integer * * Stores _image_data_ with width _w_ and height _h_ in _ef_ as _key_. * Pass true for _alpha_ if the image contains an alpha channel. + * You can control how the image is compressed by passing _comp_, which + * is a hash whose :lossy entry is true if the image should be + * compressed lossily. If :lossy is true, the :quality entry + * (0-100, default 100) sets the compression quality. + * If :lossy is false, the :level entry (0-9, default 9) sets the + * compression level. If _comp_ isn't passed, then the + * image is stored losslessly. * If the data cannot be written, an +IOError+ is raised, * otherwise a the number of bytes written is returned. */ @@ -364,15 +379,17 @@ static VALUE c_write_image (int argc, VALUE *argv, VALUE self) { VALUE key = Qnil, buf = Qnil, w = Qnil, h = Qnil, has_alpha = Qnil; + VALUE comp = Qnil, tmp; Eet_File **ef = NULL; - int n; + int n, lossy = 0, level = 9, quality = 100; Data_Get_Struct (self, Eet_File *, ef); if (!*ef) rb_raise (rb_eIOError, "closed stream"); - rb_scan_args (argc, argv, "41", &key, &buf, &w, &h, &has_alpha); + rb_scan_args (argc, argv, "42", &key, &buf, &w, &h, &has_alpha, + &comp); if (NIL_P (has_alpha)) has_alpha = Qfalse; @@ -382,13 +399,30 @@ c_write_image (int argc, VALUE *argv, VALUE self) Check_Type (w, T_FIXNUM); Check_Type (h, T_FIXNUM); + if (!NIL_P (comp)) { + Check_Type (comp, T_HASH); + + tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("lossy"))); + if (!NIL_P (tmp)) + lossy = FIX2INT (tmp); + + tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("level"))); + if (!NIL_P (tmp)) + level = FIX2INT (tmp); + + tmp = rb_hash_aref (comp, ID2SYM (rb_intern ("quality"))); + if (!NIL_P (tmp)) + quality = FIX2INT (tmp); + } + if (!RSTRING (buf)->len) return INT2FIX (0); n = eet_data_image_write (*ef, StringValuePtr (key), StringValuePtr (buf), FIX2INT (w), FIX2INT (h), - has_alpha == Qtrue, 9, 0, 0); + has_alpha == Qtrue, + level, quality, lossy); if (!n) rb_raise (rb_eIOError, "couldn't write to file"); else