/* 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_JPEGDECODENODE_HPP
#define NMM_JPEGDECODENODE_HPP

#include <stdio.h>
#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-Decompression via libjpeg. Right now, only RGB-Buffers can be generated.
   */
  class JPEGDecodeNode : public GenericProcessorNode {
    
  private:
    int image_width;
    int image_height;
    long readFromBuffer( JSAMPLE * image_buffer, long image_size, JSAMPLE *dest_buffer);
    
    /* struct & dummy-functions for libjepeg */
    jpeg_source_mgr myJpegSrcMgr;
    
    static void jpegdecInitSrc(j_decompress_ptr /*cinfo*/) { }
    static int jpegdecFillInputBuffer(j_decompress_ptr /*cinfo*/) { return 1; }
    static void jpegdecSkipInputData (j_decompress_ptr /*cinfo*/, long /*num_bytes*/) { }
    static int jpegdecResyncToRestart(j_decompress_ptr /*cinfo*/, int /*desired*/) { return 1; }
    static void jpegdecTermSrc(j_decompress_ptr /*cinfo*/) { }
    
  protected:
    virtual Result doInit();
    virtual Result doInitOutput();
    virtual Result doActivate();
    
    virtual Buffer *processBuffer(Buffer *in_buffer);
    
  public:
    JPEGDecodeNode(const char *name = "JPEGDecodeNode", 
		   const StreamQueue::Mode queue_mode = StreamQueue::MODE_SUSPEND, 
		   const int queue_size = 1) ;
    
    virtual ~JPEGDecodeNode();
  };
  
}

#endif
