X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=ext%2Fext.c;h=485bfc6062b9da6106912f5a98b612c5883b0e5e;hb=802b343606893c51ec568679c13b5e2bcb3c5d31;hp=f7860d6d2f2ff0c23799ea2868c1f7ab6f4f4aee;hpb=f54bec7b431be7885c49b2077e9116898298d2dd;p=ruby-eet.git diff --git a/ext/ext.c b/ext/ext.c index f7860d6..485bfc6 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 29 2005-04-12 17:07:48Z tilman $ * * Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de) * @@ -27,11 +27,16 @@ #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"); +#define CHECK_CLOSED(ef) \ + if (!*(ef)) \ + rb_raise (rb_eIOError, "closed stream"); + static VALUE c_close (VALUE self); static void @@ -100,10 +105,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+")) @@ -151,15 +156,12 @@ c_close (VALUE self) Eet_File **ef = NULL; Data_Get_Struct (self, Eet_File *, ef); + CHECK_CLOSED (ef); - if (!*ef) - rb_raise (rb_eIOError, "closed stream"); - else { - eet_close (*ef); - *ef = NULL; + eet_close (*ef); + *ef = NULL; - eet_shutdown (); - } + eet_shutdown (); return self; } @@ -180,9 +182,7 @@ c_list (int argc, VALUE *argv, VALUE self) int i, count = 0; Data_Get_Struct (self, Eet_File *, ef); - - if (!*ef) - rb_raise (rb_eIOError, "closed stream"); + CHECK_CLOSED (ef); switch (eet_mode_get (*ef)) { case EET_FILE_MODE_READ: @@ -194,10 +194,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 (); @@ -226,9 +224,7 @@ c_delete (VALUE self, VALUE key) char *tmp; Data_Get_Struct (self, Eet_File *, ef); - - if (!*ef) - rb_raise (rb_eIOError, "closed stream"); + CHECK_CLOSED (ef); CHECK_KEY (key); @@ -257,9 +253,7 @@ c_read (VALUE self, VALUE key) int size = 0; Data_Get_Struct (self, Eet_File *, ef); - - if (!*ef) - rb_raise (rb_eIOError, "closed stream"); + CHECK_CLOSED (ef); CHECK_KEY (key); @@ -292,9 +286,7 @@ c_write (int argc, VALUE *argv, VALUE self) int n; Data_Get_Struct (self, Eet_File *, ef); - - if (!*ef) - rb_raise (rb_eIOError, "closed stream"); + CHECK_CLOSED (ef); rb_scan_args (argc, argv, "21", &key, &buf, &comp); @@ -302,7 +294,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,32 +312,38 @@ 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); - - if (!*ef) - rb_raise (rb_eIOError, "closed stream"); + CHECK_CLOSED (ef); 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 +351,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 +369,48 @@ 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); + CHECK_CLOSED (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