# HG changeset patch # User Shai Ayal # Date 1254411688 14400 # Node ID 73153525df9a9bfba9f4975e4964361b8bdf7450 # Parent 0896714301e4b7238999c1052999370a56fe6dee initial implementation of OpenGL image rendering diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2009-10-01 Shai Ayal + + * graphics.cc (image::properties::get_color_data): New function. + * gl-render.cc (opengl_renderer::draw): Handle RGB images. + 2009-10-01 Jaroslav Hajek * DLD-FUNCTIONS/cellfun.cc diff --git a/src/gl-render.cc b/src/gl-render.cc --- a/src/gl-render.cc +++ b/src/gl-render.cc @@ -546,6 +546,8 @@ draw (dynamic_cast (props)); else if (go.isa ("text")) draw (dynamic_cast (props)); + else if (go.isa ("image")) + draw (dynamic_cast (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); diff --git a/src/gl-render.h b/src/gl-render.h --- a/src/gl-render.h +++ b/src/gl-render.h @@ -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); diff --git a/src/graphics.cc b/src/graphics.cc --- a/src/graphics.cc +++ b/src/graphics.cc @@ -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); +} // --------------------------------------------------------------------- diff --git a/src/graphics.h.in b/src/graphics.h.in --- a/src/graphics.h.in +++ b/src/graphics.h.in @@ -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.