make reduceDepth set the im->data member, with newly allocated data, so the pixelData...
authorDana Jansens <danakj@orodu.net>
Fri, 14 Feb 2003 19:18:19 +0000 (19:18 +0000)
committerDana Jansens <danakj@orodu.net>
Fri, 14 Feb 2003 19:18:19 +0000 (19:18 +0000)
otk/pseudorendercontrol.cc
otk/rendercontrol.cc
otk/rendercontrol.hh
otk/truerendercontrol.cc

index 990a60d..7af5719 100644 (file)
@@ -124,18 +124,19 @@ inline const XColor *PseudoRenderControl::pickColor(int r, int g, int b) const
 void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const
 {
   pixel32 *data = sf.pixelData();
-  char *p = (char *)data;
+  pixel32 *ret = (pixel32*)malloc(im->width * im->height * 4);
+  char *p = (char *)ret;
   int x, y;
-    for (y = 0; y < im->height; y++) {
-      for (x = 0; x < im->width; x++) {
-        p[x] = pickColor(data[x] >> default_red_shift,
-                         data[x] >> default_green_shift,
-                         data[x] >> default_blue_shift)->pixel;
-      }
-      data += im->width;
-      p += im->bytes_per_line;
+  for (y = 0; y < im->height; y++) {
+    for (x = 0; x < im->width; x++) {
+      p[x] = pickColor(data[x] >> default_red_shift,
+                       data[x] >> default_green_shift,
+                       data[x] >> default_blue_shift)->pixel;
     }
-
+    data += im->width;
+    p += im->bytes_per_line;
+  }
+  im->data = (char*)ret;
 }
 
 void PseudoRenderControl::allocateColor(XColor *color) const
index 90f9963..a4de270 100644 (file)
@@ -285,12 +285,7 @@ void RenderControl::drawGradientBackground(
   }
 
   reduceDepth(sf, im);
-
-  im->data = (char*) data;
-
   sf.setPixmap(im);
-
-  im->data = NULL;
   XDestroyImage(im);
 }
 
@@ -451,7 +446,7 @@ void RenderControl::drawImage(Surface &sf, int w, int h,
   if (x < 0) x = 0;
   if (y < 0) y = 0;
 
-  // XXX SCALING!@!&*(@! to make it fit on the surface
+  // Reduce the image size if its too big to make it fit on the surface
   int oldw = w, oldh = h;
   unsigned long *olddata = data;
   if (w > sfw) w = sfw;
@@ -507,12 +502,7 @@ void RenderControl::drawImage(Surface &sf, int w, int h,
   im->byte_order = endian;
 
   reduceDepth(sf, im);
-
-  im->data = (char*) bg;
-
   sf.setPixmap(im);
-
-  im->data = NULL;
   XDestroyImage(im);
 }
 
index 0164282..56f45c8 100644 (file)
@@ -26,8 +26,6 @@ protected:
 
   RenderControl(int screen);
 
-  virtual void reduceDepth(Surface &sf, XImage *im) const = 0;
-  
   inline void highlight(pixel32 *x, pixel32 *y, bool raised) const;
   void verticalGradient(Surface &sf, const RenderTexture &texture) const;
   void diagonalGradient(Surface &sf, const RenderTexture &texture) const;
@@ -37,11 +35,20 @@ protected:
   virtual void drawSolidBackground(Surface& sf,
                                    const RenderTexture& texture) const;
 
+  //! Reduces a Surface's Surface::pixelData so that it will display correctly
+  //! on the screen's depth
+  /*!
+    This function allocates and sets the im->data member. The allocated memory
+    will be freed when XDetroyImage is called on the XImage.
+  */
+  virtual void reduceDepth(Surface &sf, XImage *im) const = 0;
+
 public:
   virtual ~RenderControl();
 
   static RenderControl *getRenderControl(int screen);
 
+  //! Draws onto the root window
   virtual void drawRoot(const RenderColor &color) const;
   
   //! Draws a background onto a Surface, as specified by a RenderTexture
index 2f1b710..2ce771e 100644 (file)
@@ -60,7 +60,8 @@ void TrueRenderControl::reduceDepth(Surface &sf, XImage *im) const
   int r, g, b;
   int x,y;
   pixel32 *data = sf.pixelData();
-  pixel16 *p = (pixel16*) data;
+  pixel32 *ret = (pixel32*)malloc(im->width * im->height * 4);
+  pixel16 *p = (pixel16*) ret;
   switch (im->bits_per_pixel) {
   case 32:
     if ((_red_offset != default_red_shift) ||
@@ -72,13 +73,15 @@ void TrueRenderControl::reduceDepth(Surface &sf, XImage *im) const
           r = (data[x] >> default_red_shift) & 0xFF;
           g = (data[x] >> default_green_shift) & 0xFF;
           b = (data[x] >> default_blue_shift) & 0xFF;
-          data[x] = (r << _red_offset) + (g << _green_offset) +
+          ret[x] = (r << _red_offset) + (g << _green_offset) +
             (b << _blue_offset);
         }
         data += im->width;
       } 
-   }
-   return;
+    } else {
+      memcpy(ret, data, im->width * im->height * 4);
+    }
+    break;
   case 16:
     for (y = 0; y < im->height; y++) {
       for (x = 0; x < im->width; x++) {
@@ -97,6 +100,7 @@ void TrueRenderControl::reduceDepth(Surface &sf, XImage *im) const
   default:
     printf("your bit depth is currently unhandled\n");
   }
+  im->data = (char*)ret;
 }
 
 void TrueRenderControl::allocateColor(XColor *color) const