/********************************************************************************
*                                                                               *
*                             I m a g e    O b j e c t                          *
*                                                                               *
*********************************************************************************
* Copyright (C) 1997,2003 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or                 *
* modify it under the terms of the GNU Lesser General Public                    *
* License as published by the Free Software Foundation; either                  *
* version 2.1 of the License, or (at your option) any later version.            *
*                                                                               *
* This library 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             *
* Lesser General Public License for more details.                               *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public              *
* License along with this library; if not, write to the Free Software           *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
*********************************************************************************
* $Id: FXImage.h,v 1.42 2003/03/25 13:09:23 fox Exp $                           *
********************************************************************************/
#ifndef FXIMAGE_H
#define FXIMAGE_H

#ifndef FXDRAWABLE_H
#include "FXDrawable.h"
#endif

namespace FX {


/// Image rendering hints
enum {
  IMAGE_KEEP       = 0x00000001,      /// Keep pixel data in client
  IMAGE_OWNED      = 0x00000002,      /// Pixel data is owned by image
  IMAGE_DITHER     = 0,               /// Dither image to look better
  IMAGE_NEAREST    = 0x00000004,      /// Turn off dithering and map to nearest color
  IMAGE_ALPHA      = 0x00000008,      /// Data has alpha channel
  IMAGE_OPAQUE     = 0x00000010,      /// Force opaque background
  IMAGE_ALPHACOLOR = 0x00000020,      /// Override transparancy color
  IMAGE_SHMI       = 0x00000040,      /// Using shared memory image
  IMAGE_SHMP       = 0x00000080,      /// Using shared memory pixmap
  IMAGE_ALPHAGUESS = 0x00000100       /// Guess transparency color from corners
  };


class FXDC;
class FXDCWindow;
class FXDrawable;
class FXTopWindow;


/**
* An Image is a rectangular array of pixels.  It supports two representations
* of these pixels: a client-side pixel buffer which is stored as an array of
* RGB or RGBA (if IMAGE_ALPHA is true) tuples, and a server-side pixmap which
* is stored in an organization directly compatible with the screen, for fast
* drawing onto the device.  The server-side representation is not directly
* accessible from the current process as it lives in the process of the
* X Server or GDI.
*/
class FXAPI FXImage : public FXDrawable {
  FXDECLARE(FXImage)
  friend class FXDC;
  friend class FXDCWindow;
  friend class FXDrawable;
  friend class FXTopWindow;
protected:
  FXuchar *data;                // Pixel data
  FXuint   options;             // Options
  FXuint   channels;            // Number of channels 3 or 4
private:
#ifdef WIN32
  virtual FXID GetDC() const;
  virtual int ReleaseDC(FXID) const;
#endif
#ifndef WIN32
  void render_true_32(void *xim,FXuchar *img);
  void render_true_24(void *xim,FXuchar *img);
  void render_true_16_fast(void *xim,FXuchar *img);
  void render_true_16_dither(void *xim,FXuchar *img);
  void render_true_8_fast(void *xim,FXuchar *img);
  void render_true_8_dither(void *xim,FXuchar *img);
  void render_true_N_fast(void *xim,FXuchar *img);
  void render_true_N_dither(void *xim,FXuchar *img);
  void render_index_4_fast(void *xim,FXuchar *img);
  void render_index_4_dither(void *xim,FXuchar *img);
  void render_index_8_fast(void *xim,FXuchar *img);
  void render_index_8_dither(void *xim,FXuchar *img);
  void render_index_N_fast(void *xim,FXuchar *img);
  void render_index_N_dither(void *xim,FXuchar *img);
  void render_gray_8_fast(void *xim,FXuchar *img);
  void render_gray_8_dither(void *xim,FXuchar *img);
  void render_gray_N_fast(void *xim,FXuchar *img);
  void render_gray_N_dither(void *xim,FXuchar *img);
  void render_mono_1_fast(void *xim,FXuchar *img);
  void render_mono_1_dither(void *xim,FXuchar *img);
#endif
protected:
  FXImage();
private:
  FXImage(const FXImage&);
  FXImage &operator=(const FXImage&);
public:

  /**
  * Create an image.  If a client-side pixel buffer has been specified,
  * the image does not own the pixel buffer and the IMAGE_OWNED flag is
  * not set.  If the IMAGE_OWNED flag is set but a NULL pixel buffer is
  * passed, a pixel buffer will be automatically created and will be owned
  * by the image. The flag IMAGE_ALPHA determines whether an alpha channel is
  * present or not. The flags IMAGE_SHMI and IMAGE_SHMP may be specified for large
  * images to instruct render() to use shared memory to communicate with the server.
  */
  FXImage(FXApp* a,const void *pix=NULL,FXuint opts=0,FXint w=1,FXint h=1);

  /// To get to the pixel data
  FXuchar* getData() const { return data; }

  /// To get to the option flags
  FXuint getOptions() const { return options; }

  /// Change options
  void setOptions(FXuint opts);

  /**
  * Return number of channels, 3 if in RGB mode, and 4 if in RGBA mode.
  */
  FXint getChannels() const { return channels; }

  /// Get pixel at x,y
  FXColor getPixel(FXint x,FXint y) const;

  /// Change pixel at x,y
  void setPixel(FXint x,FXint y,FXColor color);

  /**
  * Create the server side pixmap, then call render() to fill it with the
  * pixel data from the client-side buffer.  After the server-side image has
  * been created, the client-side pixel buffer will be deleted unless
  * IMAGE_KEEP has been specified.  If the pixel buffer is not owned, i.e.
  * the flag IMAGE_OWNED is not set, the pixel buffer will not be deleted.
  */
  virtual void create();

  /**
  * Detach the server side pixmap from the Image.
  * Afterwards, the Image is left as if it never had a server-side resources.
  */
  virtual void detach();

  /**
  * Destroy the server-side pixmap.
  * The client-side pixel buffer is not affected.
  */
  virtual void destroy();

  /**
  * Retrieves pixels from the server-side image.  For example, to make
  * screen snapshots, or to retrieve an image after it has been drawin
  * into by various means.
  */
  virtual void restore();

  /**
  * Render the server-side representation of the image from client-side
  * pixels.  Normally, IMAGE_DITHER is used which causes the server-side
  * representation to be rendered using a 16x16 ordered dither if necessary;
  * however if IMAGE_NEAREST is used a faster (but uglier-looking), nearest
  * neighbor algorithm is used.
  */
  virtual void render();

  /**
  * Resize both client-side and server-side representations (if any) to the
  * given width and height.  The new representations typically contain garbage
  * after this operation and need to be re-filled.
  */
  virtual void resize(FXint w,FXint h);

  /**
  * Rescale pixels image to the specified width and height; this calls
  * resize() to adjust the client and server side representations.
  */
  virtual void scale(FXint w,FXint h);

  /// Mirror image horizontally and/or vertically
  virtual void mirror(FXbool horizontal,FXbool vertical);

  /**
  * Rotate image by degrees ccw; this calls resize() to adjust the client
  * and server side representations if necessary.
  */
  virtual void rotate(FXint degrees);

  /**
  * Crop image to given rectangle; this calls resize() to adjust the client
  * and server side representations.
  */
  virtual void crop(FXint x,FXint y,FXint w,FXint h);

  /// Fill image with uniform color
  virtual void fill(FXColor color);

  /// Fade image to uniform color
  virtual void fade(FXColor color,FXint factor=255);

  /// Fill horizontal gradient
  virtual void hgradient(FXColor left,FXColor right);

  /// Fill vertical gradient
  virtual void vgradient(FXColor top,FXColor bottom);

  /// Fill with gradient
  virtual void gradient(FXColor topleft,FXColor topright,FXColor bottomleft,FXColor bottomright);

  /// Blend image over uniform color
  virtual void blend(FXColor color);

  /// Save object to stream
  virtual void save(FXStream& store) const;

  /// Load object from stream
  virtual void load(FXStream& store);

  /// Save pixel data only
  virtual FXbool savePixels(FXStream& store) const;

  /// Load pixel data only
  virtual FXbool loadPixels(FXStream& store);

  /// Destructor
  virtual ~FXImage();
  };

}

#endif
