Method: STBI.load_float_image

Defined in:
ext/bindings.c

.load_float_image(io, required_components = COMPONENTS_DEFAULT) ⇒ Array .load_float_image(io, required_components = COMPONENTS_DEFAULT) {|info| ... } ⇒ Object

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 if info ... end 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)
  }
}

Overloads:

  • .load_float_image(io, required_components = COMPONENTS_DEFAULT) ⇒ Array

    Returns:

    • (Array)
  • .load_float_image(io, required_components = COMPONENTS_DEFAULT) {|info| ... } ⇒ Object

    Yields:

    • (info)

    Returns:

    • (Object)


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'ext/bindings.c', line 237

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;
  }
}