/* 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_VIDEO_FILTER_OVERLAYNODE_HPP_
#define _NMM_VIDEO_FILTER_OVERLAYNODE_HPP_

#include "nmm/nmm_prefix.hpp"

#include "IOverlayImpl.hpp"
#include "nmm/plugins/interfaces/IStillFrameImpl.hpp"
#include "nmm/multimedia/ISyncResetImpl.hpp"
#include "nmm/multimedia/GenericMultiplexerNode.hpp"

#include "visibility_OverlayNode.hpp"

namespace NMM {

  /**
   * This class takes two (RGBA32) video streams and overlays the foreground
   * onto the background. The output format is that of the background stream.
   * 
   * This Node has two input streams called "foreground" and "background"
   */
  class DLLEXPORT OverlayNode :
    public GenericMultiplexerNode,
    protected IOverlayImpl,
    protected ISyncResetImpl,
    protected IStillFrameImpl
  {
  private:
    int foreground_width;
    int foreground_height;
    int new_foreground_width;
    int new_foreground_height;
    
    unsigned int new_segment_width;
    unsigned int new_segment_height;
    unsigned int new_segment_x_offset;
    unsigned int new_segment_y_offset;
    
    unsigned int segment_width;
    unsigned int segment_height;
    unsigned int segment_x_offset;
    unsigned int segment_y_offset;
    
    int background_width;
    int background_height;
    int x_offset;
    int y_offset;
    int new_x_offset;
    int new_y_offset;
    int alpha; // 0..256, "fixed point value"
    int color;
    
    Buffer *saved_in_buffer;
    Buffer *last_foreground;
    Buffer *last_background;
    Buffer *saved_background;
    
    bool send_format;
    bool save_next_background;
    bool have_still_frame;
    bool save_still_frame;
    
    Format* in_b_rgba_format;
    Format* in_b_yv12_format;
    
    Format* out_rgba_format;
    Format* out_yv12_format;
    
    /**
     * Sets a pixel on the buffer image.
     */
    void setPixel(unsigned char *image, int x, int y,
    		  unsigned char r, unsigned char g, unsigned char b);
    
    
    /**
     * Sets a Y-pixel on a YV12 Plane
     */
    void setPixel_y(unsigned char *image, int x, int y,
                    unsigned char v);
    
  protected:
    /**
     * IOverlay interface.
     */
    virtual void setOffset(const nmm_int32& x, const nmm_int32& y);
    virtual void getOffset(nmm_int32& x, nmm_int32& y) const;
    virtual void setAlpha(const nmm_float& alpha);
    virtual nmm_float getAlpha() const;
    
    /**
     * ISync interface.
     */
    virtual Result syncReset();
    
    /**
     * IStillFrame interface.
     */
    virtual Result setStillFrame(const nmm_bool& value);
    
    /**
     * IFormatChange interface.
     */
    virtual Result setFormat( const Format& format );
    
    /**
     * IOverlay interface.
     */
    virtual void setForeground( const nmm_int32& x, const nmm_int32& y, const nmm_int32& w, const nmm_int32& h );
    virtual void setForegroundSegment( const nmm_int32& x, const nmm_int32& y, const nmm_int32& w, const nmm_int32& h );
    virtual Result disableOverlay();
    
    virtual Message *processBuffer(Buffer *in_buffer);
    
    virtual Result doInit();
    virtual Result doDeinit();
    virtual Result doInitOutput();
    virtual Result doDeinitOutput();
    virtual Result doActivate();
    
  public:

    /**
     * The default constructor. 
     */
    OverlayNode(const char *name = "OverlayNode", 
		const StreamQueue::Mode queue_mode = StreamQueue::DEFAULT_MODE, 
		const int queue_size = 1);
    ~OverlayNode();
  };
}

#include "nmm/nmm_suffix.hpp"

#endif //_NMM_VIDEO_FILTER_OVERLAYNODE_HPP_
