changeset 9680:73153525df9a

initial implementation of OpenGL image rendering
author Shai Ayal <shaiay@users.sourceforge.net>
date Thu, 01 Oct 2009 11:41:28 -0400
parents 0896714301e4
children 40775386ab58
files src/ChangeLog src/gl-render.cc src/gl-render.h src/graphics.cc src/graphics.h.in
diffstat 5 files changed, 106 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Thu Oct 01 11:10:10 2009 -0400
+++ b/src/ChangeLog	Thu Oct 01 11:41:28 2009 -0400
@@ -1,3 +1,8 @@
+2009-10-01  Shai Ayal  <shaiay@users.sourceforge.net>
+
+	* graphics.cc (image::properties::get_color_data): New function.
+	* gl-render.cc (opengl_renderer::draw): Handle RGB images.
+
 2009-10-01  Jaroslav Hajek  <highegg@gmail.com>
 
 	* DLD-FUNCTIONS/cellfun.cc 
--- a/src/gl-render.cc	Thu Oct 01 11:10:10 2009 -0400
+++ b/src/gl-render.cc	Thu Oct 01 11:41:28 2009 -0400
@@ -546,6 +546,8 @@
     draw (dynamic_cast<const hggroup::properties&> (props));
   else if (go.isa ("text"))
     draw (dynamic_cast<const text::properties&> (props));
+  else if (go.isa ("image"))
+    draw (dynamic_cast<const image::properties&> (props));
   else
     warning ("opengl_renderer: cannot render object of type `%s'",
 	     props.graphics_object_name ().c_str ());
@@ -2684,6 +2686,96 @@
 }
 
 void
+opengl_renderer::draw (const image::properties& props)
+{
+  octave_value cdata = props.get_cdata ();
+  dim_vector dv (cdata.dims ());
+  int h = dv(0), w = dv(1);
+  bool ok = true;
+  
+  Matrix x = props.get_xdata ().matrix_value ();
+  Matrix y = props.get_ydata ().matrix_value ();
+  ColumnVector p0 = xform.transform (x(0), y(0), 0);
+  ColumnVector p1 = xform.transform (x(1), y(1), 0);
+
+  glPixelZoom ( (p1(0)-p0(0))/(w-1) , -(p1(1)-p0(1))/(h-1));
+  glRasterPos3d (x(0), y(0), 0);
+
+  // Expect RGB data
+  if (dv.length () == 3 && dv(2) == 3)
+    {
+      if (cdata.is_double_type ())
+	{
+	  NDArray _a = cdata.array_value ();
+
+	  OCTAVE_LOCAL_BUFFER (GLfloat, a, (3*w*h));
+
+	  for (int i = 0; i < h; i++)
+	    for (int j = 0, idx = i*w*3; j < w; j++, idx += 3)
+	      {
+		a[idx]   = _a(i,j,0);
+		a[idx+1] = _a(i,j,1);
+		a[idx+2] = _a(i,j,2);
+	      }
+	  glDrawPixels (w, h,
+			GL_RGB, GL_FLOAT, a);
+
+	}
+      else if (cdata.is_uint16_type ())
+	{
+	  uint8NDArray _a = cdata.uint16_array_value ();
+
+	  OCTAVE_LOCAL_BUFFER (octave_uint16, a, (3*w*h));
+
+	  for (int i = 0; i < h; i++)
+	    for (int j = 0, idx = i*w*3; j < w; j++, idx += 3)
+	      {
+		a[idx]   = _a(i,j,0);
+		a[idx+1] = _a(i,j,1);
+		a[idx+2] = _a(i,j,2);
+	      }
+	  glDrawPixels (w, h,
+			GL_RGB, GL_UNSIGNED_SHORT, a);
+
+	}
+      else if (cdata.is_uint8_type ())
+	{
+	  uint8NDArray _a = cdata.uint8_array_value ();
+
+	  OCTAVE_LOCAL_BUFFER (octave_uint8, a, (3*w*h));
+
+	  for (int i = 0; i < h; i++)
+	    for (int j = 0, idx = i*w*3; j < w; j++, idx += 3)
+	      {
+		a[idx]   = _a(i,j,0);
+		a[idx+1] = _a(i,j,1);
+		a[idx+2] = _a(i,j,2);
+	      }
+	  glDrawPixels (w, h,
+			GL_RGB, GL_UNSIGNED_BYTE, a);
+
+	}
+      else
+	{
+	  ok = false;
+	  warning ("opengl_texture::draw: invalid image data type (expected double, uint16, or uint8)");
+	}
+    }
+  // indexed
+  else if (dv.length () == 2)
+    {
+      // FIXME -- deal with indexed data
+      warning ("opengl_texture::draw:image indexed images not supported yet");
+    }
+  else
+    {
+      ok = false;
+      warning ("opengl_texture::draw: invalid image size (expected n*m*3 or n*m)");
+    }
+  glPixelZoom (1, 1);
+}
+
+void
 opengl_renderer::set_viewport (int w, int h)
 {
   glViewport (0, 0, w, h);
--- a/src/gl-render.h	Thu Oct 01 11:10:10 2009 -0400
+++ b/src/gl-render.h	Thu Oct 01 11:41:28 2009 -0400
@@ -79,6 +79,7 @@
   virtual void draw (const patch::properties& props);
   virtual void draw (const hggroup::properties& props);
   virtual void draw (const text::properties& props);
+  virtual void draw (const image::properties& props);
 
   virtual void set_color (const Matrix& c);
   virtual void set_polygon_offset (bool on, double offset = 0.0);
--- a/src/graphics.cc	Thu Oct 01 11:10:10 2009 -0400
+++ b/src/graphics.cc	Thu Oct 01 11:41:28 2009 -0400
@@ -3932,7 +3932,12 @@
 
 // ---------------------------------------------------------------------
 
-// Note: "image" code is entirely auto-generated
+octave_value
+image::properties::get_color_data (void) const
+{
+  return convert_cdata (*this, get_cdata (),
+			cdatamapping_is ("scaled"), 3);
+}
 
 // ---------------------------------------------------------------------
 
--- a/src/graphics.h.in	Thu Oct 01 11:10:10 2009 -0400
+++ b/src/graphics.h.in	Thu Oct 01 11:41:28 2009 -0400
@@ -3143,6 +3143,8 @@
     std::string get_climinclude (void) const
       { return climinclude.current_value (); }
 
+    octave_value get_color_data (void) const;
+
     // See the genprops.awk script for an explanation of the
     // properties declarations.