Ejemplo n.º 1
0
VALUE OR_Matrix::to_ruby()
{
  Matrix matrix;
  MArray<double> values;
  double cell;
  int i, number_of_values;
  VALUE argv[2];
  
  matrix = octave_val.matrix_value();
  int number_of_rows = matrix.rows();
  int number_of_columns = matrix.columns();
  
  if ((number_of_rows == 0) && (number_of_columns == 0)) {
    return rb_ary_new2(0);
  } else if (number_of_columns == 1) {
    values = matrix.column(0);
  } else {
    argv[0] = INT2FIX(number_of_rows);
    argv[1] = INT2FIX(number_of_columns);
    ruby_val = rb_class_new_instance(2, argv, rb_path2class("Octave::Matrix"));
    
    int row_index, column_index = 0;
    VALUE cells, row;
    cells = rb_ary_new2(number_of_rows);
    for (row_index = 0; row_index < number_of_rows; row_index++) {
      row = rb_ary_new2(number_of_columns);
      values = matrix.row(row_index);
      
      for (column_index = 0; column_index < number_of_columns; column_index++) {
        cell = values(column_index);
        if (xisnan(cell) || octave_is_NA(cell)) {
          rb_ary_push(row, Qnil);
        } else {
          rb_ary_push(row, rb_float_new(cell));
        }
      }
      
      rb_ary_push(cells, row);
    }
    
    rb_iv_set(ruby_val, "@cells", cells);
    return ruby_val;
  }
  
  number_of_values = values.length();
  ruby_val = rb_ary_new2(number_of_values);
  for (i = 0; i < number_of_values; i++) {
    cell = values(i);
    if (xisnan(cell) || octave_is_NA(cell)) {
      rb_ary_push(ruby_val, Qnil);
    } else {
      rb_ary_push(ruby_val, rb_float_new(cell));
    }
  }
  
  return ruby_val;
}
Ejemplo n.º 2
0
VALUE OR_Variable::to_ruby()
{
  if (octave_val.is_string()) {
    return OR_String(octave_val).to_ruby();
  } else if (octave_val.is_bool_matrix()) {
    return OR_BooleanMatrix(octave_val).to_ruby();
  } else if (octave_val.is_bool_type()) {
    return (octave_val.bool_value() ? Qtrue : Qfalse);
  } else if (octave_val.is_cell()) {
    return OR_CellMatrix(octave_val).to_ruby();
  } else if (octave_val.is_real_matrix()) {
    return OR_Matrix(octave_val).to_ruby();
  } else if (octave_val.is_map()) {
    return OR_StructMatrix(octave_val).to_ruby();
  } else if (octave_val.is_numeric_type() && !xisnan(octave_val.double_value())) {
    return rb_float_new(octave_val.double_value());
  } else {
    return Qnil;
  }
}
Ejemplo n.º 3
0
QImage makeImageFromCData (const octave_value& v, int width, int height)
{
  dim_vector dv (v.dims ());

  if (dv.length () == 3 && dv(2) == 3)
    {
      int w = qMin (dv(1), width);
      int h = qMin (dv(0), height);

      int x_off = (w < width ? (width - w) / 2 : 0);
      int y_off = (h < height ? (height - h) / 2 : 0);

      QImage img (width, height, QImage::Format_ARGB32);
      img.fill (qRgba (0, 0, 0, 0));

      if (v.is_uint8_type ())
	{
	  uint8NDArray d = v.uint8_array_value ();

	  for (int i = 0; i < w; i++)
	    for (int j = 0; j < h; j++)
	      {
		int r = d(j, i, 0);
		int g = d(j, i, 1);
		int b = d(j, i, 2);
		int a = 255;

		img.setPixel (x_off + i, y_off + j, qRgba (r, g, b, a));
	      }
	}
      else if (v.is_single_type ())
	{
	  FloatNDArray f = v.float_array_value ();

	  for (int i = 0; i < w; i++)
	    for (int j = 0; j < h; j++)
	      {
		float r = f(j, i, 0);
		float g = f(j, i, 1);
		float b = f(j, i, 2);
		int a = (xisnan (r) || xisnan (g) || xisnan (b) ? 0 : 255);

		img.setPixel (x_off + i, y_off + j,
			      qRgba (xround (r * 255),
				     xround (g * 255),
				     xround (b * 255),
				     a));
	      }
	}
      else if (v.is_real_type ())
	{
	  NDArray d = v.array_value ();

	  for (int i = 0; i < w; i++)
	    for (int j = 0; j < h; j++)
	      {
		double r = d(j, i, 0);
		double g = d(j, i, 1);
		double b = d(j, i, 2);
		int a = (xisnan (r) || xisnan (g) || xisnan (b) ? 0 : 255);

		img.setPixel (x_off + i, y_off + j,
			      qRgba (xround (r * 255),
				     xround (g * 255),
				     xround (b * 255),
				     a));
	      }
	}

      return img;
    }

  return QImage ();
}