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

#include "nmm/nmm_prefix.hpp"

#include <string>
#include <map>
#include <png.h>

using namespace std;

namespace NMM {

  class BitmapReader{
  public:

    enum format { RGBA, YV12, YV12A };
    typedef struct {
      unsigned int   width;
      unsigned int   height;
      format         cformat;
      unsigned char* pixeldata;
    } bitmap_t;

    typedef map<string, bitmap_t*> bitmaplist_t;


    BitmapReader();
    ~BitmapReader();

    /**
     * loads a (PNG) image that can be accessed by the given identifier
     * \param id identifier for accessing image later
     * \param filename filename of the (png) image
     * \param f color format that should be used (RGBA, YV12, YV12A)
     * \returns true, if bitmap could be read
     * Note: an image that is already saved under the given id, will be deleted before automatically
     */
    bool loadImage( string id, string filename, format f=RGBA  );

    /**
     * creates a image with the given color
     * \param id identifier for accessing image later
     * \param width width of the bitmap
     * \param height height of the bitmap
     * \param color color of the image (RGBA)
     * \param f color format that should be used (RGBA, YV12, YV12A)
     */
     bool createImage( string id, unsigned int width, unsigned int height,
                       unsigned int color1, unsigned int color2, unsigned int color3, format f=RGBA );

    /**
     * deletes image with the given id
     */
    void freeImage( string id );

    /**
     * deletes all read images
     */
    void freeAll();

    /**
     * get data of a read image
     * \param id identifier of the image
     * \returns pointer to the pixel data (data format depends on parameter specified in loadImage)
     * \returns width and height of the image
     */
    unsigned char* getImage( string id, unsigned int* width, unsigned int* height );

    private:
    bitmaplist_t bitmaplist;

    /**
     * loads a (PNG) Bitmap
     * \param filename filename of the bitmap
     * \param width pointer to a variable that holds the bitmap width after loading
     * \param height pointer to a variable that holds the bitmap height after loading
     * \returns pointer to the bitmap data (rgba format)
     */
    unsigned char* loadBitmap( string filename, unsigned int* width, unsigned int* height );

    /**
     * deallocates memory that was used with the loadBitmap method
     * \param data pointer to the bitmap data
     */
    void freeBitmap( unsigned char* data );

    void drawFilledBox( unsigned int* frame, unsigned int stride,
                        unsigned int xs, unsigned int ys,
                        unsigned int width, unsigned int height,
			unsigned int color );

    void drawBox( unsigned int* frame, unsigned int stride,
                  unsigned int xs, unsigned int ys,
                  unsigned int width, unsigned int height,
                  unsigned int color1, unsigned int color2 );

    /**
    * \param R red color component
    * \param G green color component
    * \param B blue color component
    * \return Y color component
    */
    double getYComponent( unsigned char R, unsigned char G, unsigned char B );

    /**
    * \param R red color component
    * \param G green color component
    * \param B blue color component
    * \return U color component
    */
    double getUComponent( unsigned char R, unsigned char G, unsigned char B );

    /**
    * \param R red color component
    * \param G green color component
    * \param B blue color component
    * \return V color component
    */
    double getVComponent( unsigned char R, unsigned char G, unsigned char B );

    /**
    * converts rgba bitmap to yv12 or yv12a
    * \param rgb_buffer pointer to rgba bitmap
    * \param width width of the bitmap
    * \param height height of the bitmap
    * \param f color format to convert bitmap to
    * \returns pointer to converted pixel data, NULL if there was an error
    */
    unsigned char* convertToYUV( unsigned char* rgbbuffer, int width, int height, format f=RGBA );

  };

}

#include "nmm/nmm_suffix.hpp"

#endif
