Example #1
0
std::unique_ptr<float[]> loadStbiHdr(const Path &path, TexelConversion request, int &w, int &h)
{
    InputStreamHandle in = FileUtils::openInputStream(path);
    if (!in)
        return nullptr;

    int channels;
    std::unique_ptr<float[], void(*)(void *)> img(stbi_loadf_from_callbacks(&istreamCallback, in.get(),
            &w, &h, &channels, 0), stbi_image_free);

    // We only expect Radiance HDR for now, which only has RGB support.
    if (!img || channels != 3)
        return nullptr;

    int targetChannels = (request == TexelConversion::REQUEST_RGB) ? 3 : 1;

    std::unique_ptr<float[]> texels(new float[w*h*targetChannels]);
    if (targetChannels == 3) {
        std::memcpy(texels.get(), img.get(), w*h*targetChannels*sizeof(float));
    } else {
        for (int i = 0; i < w*h; ++i)
            texels[i] = convertToScalar(request, img[i*3], img[i*3 + 1], img[i*3 + 2], 1.0f, false);
    }

    return std::move(texels);
}
Example #2
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;
  }
}