X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=ext%2Fext.c;h=94df85d4e8ff2d601c580ca91531228201331326;hb=c2f066281c7c4b2bff662aab05d934bff2155223;hp=f7860d6d2f2ff0c23799ea2868c1f7ab6f4f4aee;hpb=f54bec7b431be7885c49b2077e9116898298d2dd;p=ruby-eet.git diff --git a/ext/ext.c b/ext/ext.c index f7860d6..94df85d 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 28 2005-04-11 20:58:23Z tilman $ * * Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de) * @@ -27,9 +27,10 @@ #include #define CHECK_KEY(key) \ - Check_Type ((key), T_STRING); \ - if (rb_funcall ((key), rb_intern ("include?"), \ - 1, INT2FIX (0)) == Qtrue) \ + StringValue((key)); \ +\ + if (rb_funcall (key, rb_intern ("include?"), \ + 1, INT2FIX (0)) == Qtrue) \ rb_raise (rb_eArgError, "key must not contain binary zeroes"); static VALUE c_close (VALUE self); @@ -100,10 +101,10 @@ c_init (int argc, VALUE *argv, VALUE self) rb_scan_args (argc, argv, "11", &file, &mode); - Check_Type (file, T_STRING); + StringValue (file); if (!NIL_P (mode)) { - Check_Type (mode, T_STRING); + StringValue (mode); tmp = StringValuePtr (mode); if (!strcmp (tmp, "r+")) @@ -194,10 +195,8 @@ c_list (int argc, VALUE *argv, VALUE self) rb_scan_args (argc, argv, "01", &glob); - if (!NIL_P (glob)) { - Check_Type (glob, T_STRING); + if (!NIL_P (glob)) tmp = StringValuePtr (glob); - } ret = rb_ary_new (); @@ -302,7 +301,7 @@ c_write (int argc, VALUE *argv, VALUE self) comp = Qtrue; CHECK_KEY (key); - Check_Type (buf, T_STRING); + StringValue (buf); n = eet_write (*ef, StringValuePtr (key), StringValuePtr (buf), RSTRING (buf)->len, @@ -320,16 +319,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 +340,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 +360,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,31 +378,50 @@ 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; CHECK_KEY (key); - Check_Type (buf, T_STRING); + StringValue (buf); 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