/*
- * $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)
*
* 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);
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;
/*
* 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.
*/
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_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