/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */


#ifndef _NMM_YUVtoRGBCONVERTERNODE_HPP_
#define _NMM_YUVtoRGBCONVERTERNODE_HPP_

#include "nmm/multimedia/GenericConverterNode.hpp"

#define CLIPPED(x) ((x)>255?255:((x)<0?0:(x)))

namespace NMM {
 /** \class YUVtoRGBConverterNode
   * \ingroup VideoConverter
   * This Node converts an incoming YUV image to a BGR0 image
   * (blue, green, red and ignored channel, in this order).
   */
class YUVtoRGBConverterNode : public GenericConverterNode {

public:

  YUVtoRGBConverterNode(const char* name = "YUVtoRGBConverterNode");
  virtual ~YUVtoRGBConverterNode() {};


protected:


  /* YUV422 (in UYVY-format) converter routines*/
  static Buffer* UYVY2BGR24 (Buffer* in_buffer, const GenericConverterNode*);
  static Buffer* UYVY2RGB24 (Buffer* in_buffer, const GenericConverterNode*);
  static Buffer* UYVY2RGBA32(Buffer* in_buffer, const GenericConverterNode*);

  static Buffer* UYVYplan2BGRA32(Buffer* in_buffer, const GenericConverterNode*);
  static Buffer* YV12toRGB24(Buffer* in_buffer, const GenericConverterNode*);
  static Buffer* YV12toRGB32(Buffer* in_buffer, const GenericConverterNode*);

  /**
   * Helper method for convertYV12ToRGB
   */
  static unsigned int yuv2rgb(int Y, int Cb, int Cr);

  /** 
   * Converts an image in YV12 or YV12A format into an RGB image with colorspace rgb24,
   * 24 bits per pixel (no padding bits!)
   */
  static void convertYV12ToRGB24(unsigned char* rgb_image, unsigned char* yv12_image, unsigned int width, unsigned int height, bool use_alpha);

  /** 
   * Converts an image in YV12 or YV12A format into an RGB image with colorspace rgb32,
   * 32 bits per pixel (no padding bits!)
   */
  static void convertYV12ToRGB32(unsigned char* rgb_image, unsigned char* yv12_image, unsigned int width, unsigned int height, bool use_alpha);

  
  virtual Result doInitOutput();

private:
  int width, height;
  
  void allocate();
  static inline int clipTo_0_255(int x) { return ((x)>255?255:((x)<0?0:(x))); }
};

}

#endif //_NMM_YUV2BGR0NODE_HPP_
