Esempio n. 1
0
/*
 * call-seq:
 *     each(*args) -> Enumerator
 *     each(*args) { |item| block } -> Enumerator
 *
 * Iterate over the collection, passing each item to a given block.
 * If no block was supplied, return an enumerator for the collection.
 *
 */
static VALUE rbpod_collection_each(VALUE self, VALUE argv)
{
    GList *current = NULL, *collection = TYPED_DATA_PTR(self, GList);
    VALUE klass, item, arguments;

    /* Return an enumerator if a block was not supplied. */
    RETURN_ENUMERATOR(self, 0, 0);

    /* What sort of items are we casting this data to? */
    klass = rb_funcall(self, rb_intern("type"), 0);

    /* Create a shallow copy of the passed arguments. */
    arguments = rb_ary_dup(argv);

    /* Prepend an empty element as a placeholder. */
    rb_ary_unshift(arguments, Qnil);

    /* If we were supplied a block, enumerate the entire list. */
    for (current = collection; current != NULL; current = g_list_next(current)) {
        /* TODO: Find a better workaround than this or Data_Wrap_Struct. */
        item = rb_class_new_instance_with_data(0, NULL, klass, current->data);
        rb_ary_store(arguments, 0, item);
        rb_yield_splat(arguments);
    }

    return self;
}
Esempio n. 2
0
/*
  call-seq:
    load_image(io, required_components = COMPONENTS_DEFAULT) => [data, width, height, components]
    load_image(io, required_components = COMPONENTS_DEFAULT) { |info| ... } => obj

  Loads an image using stb_image and returns the resulting data and its width,
  height, and the number of components per pixel in the data. The returned data
  is a packed string of unsigned 8-bit integers (8 bits per component). The
  length of the string will always be width * height * components.

  In the second form, the info array is yielded to the block if the image is
  successfully loaded. Otherwise, the method returns nil. This is possibly more
  convenient than doing an <tt>if info ... end</tt> block to check if the
  image was successfully loaded.

  === IO Objects

  IO objects accepted for loading data with must implement at least
  IO#read(length) and either IO#eof or IO#eof?. In addition, they may also
  implement a skip(length) method that skips length bytes and does not return
  any value. If the IO object does not respond to #skip, #read will be called
  instead and its result will be discarded. If you want to avoid unnecessary
  allocations, it may be wise to implement a skip method.

  === Components

  If required_components is provided and not the default value, the image data
  returned will have as many components as are requested. In turn, the number of
  components returned via the array will be the same as required_components.

  Valid options for required_components are:

  [::COMPONENTS_DEFAULT]    The default value, loads as many components as are
                            provided by the image.
  [::COMPONENTS_GREY]       Loads only one component.
  [::COMPONENTS_GREY_ALPHA] Loads two components.
  [::COMPONENTS_RGB]        Loads three components (red, green, and blue).
  [::COMPONENTS_RGB_ALPHA]  Loads four components (red, green, blue, and alpha).

  === Example

    open('image.png') { |io|
      STBI.load_image(io) { |data, width, height, components|
        format = case components
                 when STBI::COMPONENTS_GREY       then Gl::GL_RED
                 when STBI::COMPONENTS_GREY_ALPHA then Gl::GL_RG
                 when STBI_COMPONENTS_RGB         then Gl::RGB
                 when STBI_COMPONENTS_RGB_ALPHA   then Gl::RGBA
                 end

        Gl::glTexImage2D(Gl::GL_TEXTURE_2D, 0, format, width, height, 0,
                         format, Gl::GL_UNSIGNED_BYTE, data)
      }
    }
 */
static VALUE sr_load_image(int argc, VALUE *argv, VALUE sr_self)
{
  VALUE sr_callbacks;
  VALUE sr_req_comp;
  VALUE sr_image_data = Qnil;
  int x = 0;
  int y = 0;
  int components[2] = {
    STBI_default,
    0
  };

  rb_scan_args(argc, argv, "11", &sr_callbacks, &sr_req_comp);

  if (NIL_P(sr_callbacks)) {
    rb_raise(rb_eArgError, "IO object cannot be nil");
    return Qnil;
  } if (RTEST(sr_req_comp)) {
    components[0] = FIX2INT(sr_req_comp);
  }

  if (!rb_obj_respond_to(sr_callbacks, s_read_funcid, 0) ||
      !(rb_obj_respond_to(sr_callbacks, s_eof_funcid, 0) ||
        rb_obj_respond_to(sr_callbacks, s_eof_qm_funcid, 0))) {
    rb_raise(rb_eTypeError, "IO object does not respond to either read or eof/eof?");
  }

  stbi_uc *data = stbi_load_from_callbacks(&s_st_callbacks, &sr_callbacks,
    &x, &y, &components[1], components[0]);

  if (data) {
    const long length = x * y * components[!components[0]];
    sr_image_data = rb_ary_new3(4,
      rb_external_str_new((const char *)data, length),
      INT2FIX(x), INT2FIX(y),
      INT2FIX(components[!components[0]]));
    stbi_image_free(data);
  }

  if (!NIL_P(sr_image_data) && rb_block_given_p()) {
    return rb_yield_splat(sr_image_data);
  } else {
    return sr_image_data;
  }
}
Esempio n. 3
0
/*
  call-seq:
    load_float_image(io, required_components = COMPONENTS_DEFAULT) => [data, width, height, components]
    load_float_image(io, required_components = COMPONENTS_DEFAULT) { |info| ... } => obj

  Similar to ::load_image, except the returned image data is a packaed string
  for an array of 32-bit floats (e.g., String#unpack('f*') will extract an array
  of floating point values representing the components of the image's pixels).

  In the second form, the info array is yielded to the block if the image is
  successfully loaded. Otherwise, the method returns nil. This is possibly more
  convenient than doing an <tt>if info ... end</tt> block to check if the
  image was successfully loaded.

  For further information on the IO object, the required_components argument,
  and so on, see the documentation for load_image.

  === Example

    open('image.png') { |io|
      STBI.load_float_image(io) { |data, width, height, components|
        format = case components
                 when STBI::COMPONENTS_GREY       then Gl::GL_RED
                 when STBI::COMPONENTS_GREY_ALPHA then Gl::GL_RG
                 when STBI_COMPONENTS_RGB         then Gl::RGB
                 when STBI_COMPONENTS_RGB_ALPHA   then Gl::RGBA
                 end

        Gl::glTexImage2D(Gl::GL_TEXTURE_2D, 0, format, width, height, 0,
                         format, Gl::GL_FLOAT, data)
      }
    }
 */
static VALUE sr_load_float_image(int argc, VALUE *argv, VALUE sr_self)
{
  VALUE sr_callbacks;
  VALUE sr_req_comp;
  VALUE sr_image_data = Qnil;
  int x = 0;
  int y = 0;
  int components[2] = {
    STBI_default,
    0
  };

  rb_scan_args(argc, argv, "11", &sr_callbacks, &sr_req_comp);

  if (NIL_P(sr_callbacks)) {
    rb_raise(rb_eArgError, "IO object cannot be nil");
    return Qnil;
  } if (RTEST(sr_req_comp)) {
    components[0] = FIX2INT(sr_req_comp);
  }

  float *data = stbi_loadf_from_callbacks(&s_st_callbacks, &sr_callbacks,
    &x, &y, &components[1], components[0]);

  if (data) {
    const long length = x * y * components[!components[0]] * sizeof(float);
    sr_image_data = rb_ary_new3(4,
      rb_external_str_new((const char *)data, length),
      INT2FIX(x), INT2FIX(y),
      INT2FIX(components[!components[0]]));
    stbi_image_free(data);
  }

  if (!NIL_P(sr_image_data) && rb_block_given_p()) {
    return rb_yield_splat(sr_image_data);
  } else {
    return sr_image_data;
  }
}
Esempio n. 4
0
static VALUE kernel_spec_rb_yield_splat(VALUE self, VALUE ary) {
  return rb_yield_splat(ary);
}