*** gstalphacolor.c	2007-04-17 16:23:12.000000000 +0200
--- mygstalphacolor.c	2007-04-17 16:31:29.000000000 +0200
***************
*** 49,55 ****
  
    /* caps */
    gint in_width, in_height;
!   gboolean in_rgba;
    gint out_width, out_height;
  };
  
--- 49,56 ----
  
    /* caps */
    gint in_width, in_height;
!   gboolean in_rgb;
!   gint depth;
    gint out_width, out_height;
  };
  
***************
*** 69,75 ****
      GST_STATIC_PAD_TEMPLATE ("sink",
      GST_PAD_SINK,
      GST_PAD_ALWAYS,
!     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_BGRA)
      );
  
  static GstStaticPadTemplate gst_alpha_color_src_template =
--- 70,76 ----
      GST_STATIC_PAD_TEMPLATE ("sink",
      GST_PAD_SINK,
      GST_PAD_ALWAYS,
!     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_BGRA ";" GST_VIDEO_CAPS_RGB ";" GST_VIDEO_CAPS_BGR)
      );
  
  static GstStaticPadTemplate gst_alpha_color_src_template =
***************
*** 194,228 ****
    gboolean ret;
    const GValue *fps;
    gint red_mask;
  
    alpha = GST_ALPHA_COLOR (btrans);
    structure = gst_caps_get_structure (incaps, 0);
  
    ret = gst_structure_get_int (structure, "width", &alpha->in_width);
    ret &= gst_structure_get_int (structure, "height", &alpha->in_height);
    fps = gst_structure_get_value (structure, "framerate");
    ret &= (fps != NULL && GST_VALUE_HOLDS_FRACTION (fps));
    ret &= gst_structure_get_int (structure, "red_mask", &red_mask);
  
    if (!ret)
      return FALSE;
  
!   alpha->in_rgba = TRUE;
  #if (G_BYTE_ORDER == G_BIG_ENDIAN)
    if (red_mask != 0x000000ff)
  #else
    if (red_mask != 0x00ff0000)
  #endif
!     alpha->in_rgba = FALSE;
  
    return TRUE;
  }
  
  static void
! transform_rgb (guint8 * data, gint size)
  {
    guint8 y, u, v;
! 
    while (size > 0) {
      y = data[0] * 0.299 + data[1] * 0.587 + data[2] * 0.114 + 0;
      u = data[0] * -0.169 + data[1] * -0.332 + data[2] * 0.500 + 128;
--- 195,235 ----
    gboolean ret;
    const GValue *fps;
    gint red_mask;
+   gint depth = 0;
  
    alpha = GST_ALPHA_COLOR (btrans);
    structure = gst_caps_get_structure (incaps, 0);
  
    ret = gst_structure_get_int (structure, "width", &alpha->in_width);
    ret &= gst_structure_get_int (structure, "height", &alpha->in_height);
+   ret &= gst_structure_get_int (structure, "depth", &depth);
    fps = gst_structure_get_value (structure, "framerate");
    ret &= (fps != NULL && GST_VALUE_HOLDS_FRACTION (fps));
    ret &= gst_structure_get_int (structure, "red_mask", &red_mask);
+   
+ 
+   GST_DEBUG("Input depth is: %d", depth);
  
    if (!ret)
      return FALSE;
  
!   alpha->depth = depth;
!   alpha->in_rgb = TRUE;
  #if (G_BYTE_ORDER == G_BIG_ENDIAN)
    if (red_mask != 0x000000ff)
  #else
    if (red_mask != 0x00ff0000)
  #endif
!     alpha->in_rgb = FALSE;
  
    return TRUE;
  }
  
  static void
! transform_rgba (guint8 * data, gint size)
  {
    guint8 y, u, v;
!   GST_DEBUG("RGBA type\n");
    while (size > 0) {
      y = data[0] * 0.299 + data[1] * 0.587 + data[2] * 0.114 + 0;
      u = data[0] * -0.169 + data[1] * -0.332 + data[2] * 0.500 + 128;
***************
*** 239,245 ****
  }
  
  static void
! transform_bgr (guint8 * data, gint size)
  {
    guint8 y, u, v;
  
--- 246,252 ----
  }
  
  static void
! transform_bgra (guint8 * data, gint size)
  {
    guint8 y, u, v;
  
***************
*** 258,263 ****
--- 265,329 ----
    }
  }
  
+ static guint8*
+ transform_rgb (guint8 * data, gint size)
+ {
+   gint old_size = size;
+   gint new_size = size + size/3;
+ 
+   guint8 * data1 = g_malloc(new_size);
+ 
+   old_size -= 3;
+   new_size -= 4;
+ 
+   while(old_size > 0)
+   {
+     guint8 *from_loc = &data[old_size];
+     guint8 *to_loc = &data1[new_size];
+ 
+     to_loc[1] = from_loc[0] * 0.299 + from_loc[1] * 0.587 + from_loc[2] * 0.114 + 0;
+     to_loc[2] = from_loc[0] * -0.169 + from_loc[1] * -0.332 + from_loc[2] * 0.500 + 128;
+     to_loc[3] = from_loc[0] * 0.500 + from_loc[1] * -0.419 + from_loc[2] * -0.0813 + 128;
+     
+     to_loc[0] = 255;
+     
+     old_size -= 3;
+     new_size -= 4;
+   }
+ 
+   return data1;
+ }
+ 
+ static guint8*
+ transform_bgr (guint8 * data, gint size)
+ {
+   gint old_size = size;
+   gint new_size = size + size/3;
+ 
+   guint8 * data1 = g_malloc(new_size);
+ 
+   old_size -= 3;
+   new_size -= 4;
+ 
+   while(old_size > 0)
+   {
+     guint8 *from_loc = &data[old_size];
+     guint8 *to_loc = &data1[new_size];
+ 
+     to_loc[1] = from_loc[2] * 0.299 + from_loc[1] * 0.587 + from_loc[0] * 0.114 + 0;
+     to_loc[2] = from_loc[2] * -0.169 + from_loc[1] * -0.332 + from_loc[0] * 0.500 + 128;
+     to_loc[3] = from_loc[2] * 0.500 + from_loc[1] * -0.419 + from_loc[0] * -0.0813 + 128;
+     
+     to_loc[0] = 255;
+     
+     old_size -= 3;
+     new_size -= 4;
+   }
+ 
+   return data1;
+ }
+ 
+ 
  static GstFlowReturn
  gst_alpha_color_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
  {
***************
*** 267,276 ****
    alpha = GST_ALPHA_COLOR (btrans);
  
    /* Transform in place */
!   if (alpha->in_rgba)
!     transform_rgb (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
!   else
!     transform_bgr (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
  
    return ret;
  }
--- 333,360 ----
    alpha = GST_ALPHA_COLOR (btrans);
  
    /* Transform in place */
!   if(alpha->depth == 32){
!     if (alpha->in_rgb)
!       transform_rgba (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
!     else
!       transform_bgra (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
!   }else{
! 
!     if (alpha->in_rgb){
!       guint8 *newdata = transform_rgb (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
! 
!       inbuf->size = GST_BUFFER_SIZE (inbuf) + (GST_BUFFER_SIZE (inbuf)/3);
!       inbuf->data = newdata;
! 
!     }else{
!       transform_bgr (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
!       guint8 *newdata = transform_bgr (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
! 
!       inbuf->size = GST_BUFFER_SIZE (inbuf) + (GST_BUFFER_SIZE (inbuf)/3);
!       inbuf->data = newdata;
! 
!     }
!   }
  
    return ret;
  }
