changeset 18659:3277514f36da stable

Fix inverted colors when printing uint8/uint16 images (bug #42107). * gl2ps-renderer.cc (draw_pixels): Convert the data type to GL_FLOAT, and divide by the maximum data type value so that range is [0,1]. * gl2ps-renderer.cc (glps_renderer::draw_pixels): Convert and normalize uint8/uint16 inputs to GL_FLOAT by calling draw_pixels().
author Rik <rik@octave.org>
date Fri, 18 Apr 2014 10:05:08 -0700
parents 5fb180e37d7c
children 01aa90ece9a4
files libinterp/corefcn/gl2ps-renderer.cc
diffstat 1 files changed, 11 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/gl2ps-renderer.cc	Fri Apr 18 14:21:33 2014 +0200
+++ b/libinterp/corefcn/gl2ps-renderer.cc	Fri Apr 18 10:05:08 2014 -0700
@@ -190,13 +190,14 @@
 
 template <typename T>
 static void
-draw_pixels (GLsizei w, GLsizei h, GLenum format, const T *data)
+draw_pixels (GLsizei w, GLsizei h, GLenum format, const T *data, float maxval)
 {
   OCTAVE_LOCAL_BUFFER (GLfloat, a, 3*w*h);
 
-  for (int i = 0; i < 3*w*h; i++)
-    a[i] = data[i];
-
+  // Convert to GL_FLOAT as it is the only type gl2ps accepts.
+  for (unsigned int i = 0; i < 3*w*h; i++)
+    a[i] = data[i] / maxval;
+  
   gl2psDrawPixels (w, h, 0, 0, format, GL_FLOAT, a);
 }
 
@@ -204,10 +205,12 @@
 glps_renderer::draw_pixels (GLsizei w, GLsizei h, GLenum format,
                             GLenum type, const GLvoid *data)
 {
-  if (type == GL_UNSIGNED_SHORT)
-    ::draw_pixels (w, h, format, static_cast<const GLushort *> (data));
-  else if (type == GL_UNSIGNED_BYTE)
-    ::draw_pixels (w, h, format, static_cast<const GLubyte *> (data));
+  // gl2psDrawPixels only supports the GL_FLOAT type.
+  // Other formats, such as uint8, must be converted first.
+  if (type == GL_UNSIGNED_BYTE)
+    ::draw_pixels (w, h, format, static_cast<const GLubyte *> (data), 255.0f);
+  else if (type == GL_UNSIGNED_SHORT)
+    ::draw_pixels (w, h, format, static_cast<const GLushort *> (data), 65535.0f);
   else
     gl2psDrawPixels (w, h, 0, 0, format, type, data);
 }