/* 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_JPEGENCODENODE_HPP
#define NMM_JPEGENCODENODE_HPP


#include <stdio.h>
#include "nmm/plugins/interfaces/IEncodeImpl.hpp"
#include "nmm/multimedia/GenericProcessorNode.hpp"
#include "nmm/utils/time/Benchmark.hpp"

extern "C" {
#ifdef HAVE_STDLIB_H
#  define JPEGDECODE_NODE_SAVES_HAVE_STDLIB_H HAVE_STDLIB_H
#  undef HAVE_STDLIB_H
#endif
#include <jpeglib.h>
#ifdef JPEGDECODE_NODE_SAVES_HAVE_STDLIB_H
#  undef HAVE_STDLIB_H
#  define HAVE_STDLIB_H JPEGDECODE_NODE_HAVE_STDLIB_H
#  undef JPEGDECODE_NODE_HAVE_STDLIB_H
#endif
}

namespace NMM {

  /**
   * This Node uses JPEG-Compression via libjpeg. Right now, only RGB-Buffers can be processed.
   */
  class JPEGEncodeNode : public GenericProcessorNode,
			 public IEncodeImpl{
  private:
    Benchmark benchmark;
    float quality;
  protected:

    int image_width;
    int image_height;

    long writeToBuffer(int quality, JSAMPLE * image_buffer, int image_width, int image_height, JSAMPLE *dest_buffer );
    virtual Buffer *processBuffer(Buffer *in_buffer);

    /* struct & dummy-functions for libjepeg */
    struct jpeg_destination_mgr myJpegDestMgr;

    static void jpegencInitDest(j_compress_ptr /*cinfo*/) {  }
    static int jpegencFlushDest(j_compress_ptr /*cinfo*/) {  return 1; }
    static void jpegencTermDest(j_compress_ptr /*cinfo*/) { }
    
    virtual Result doInit();
    virtual Result doInitOutput();

    virtual void setQuality(const nmm_float& q);
    virtual nmm_float getQuality();
    virtual void setBitrate(const nmm_int32& i);
    virtual nmm_int32 getBitrate();
    
   public:

    JPEGEncodeNode(const char *name = "JPEGEncodeNode", 
		   const StreamQueue::Mode queue_mode = StreamQueue::MODE_SUSPEND, 
		   const int queue_size = 1);
    virtual ~JPEGEncodeNode();

  };
  
}

#endif
