ROOT logo
// @(#)root/asimage:$Id: TASImage.h 23604 2008-04-29 14:37:35Z couet $
// Author: Fons Rademakers, Reiner Rohlfs 28/11/2001

/*************************************************************************
 * Copyright (C) 1995-2001, Rene Brun, Fons Rademakers and Reiner Rohlfs *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TASImage
#define ROOT_TASImage


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TASImage                                                             //
//                                                                      //
// Interface to image processing library using libAfterImage.           //
// It allows for the reading and writing of images in different         //
// formats, several image manipulations (scaling, tiling, merging,      //
// etc.) and displaying in pads.                                        //
// The size of the image on the screen does not depend on the original  //
// size of the image but on the size of the pad. Therefore it is very   //
// easy to resize the image on the screen by resizing the pad.          //
//                                                                      //
// Besides reading an image from a file an image can be defined by a    //
// two dimensional array of values. A palette defines the color of      //
// each value.                                                          //
//                                                                      //
// The image can be zoomed by defining a rectangle with the mouse.      //
// The color palette can be modified with a GUI, just select            //
// StartPaletteEditor() from the context menu.                          //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TImage
#include "TImage.h"
#endif

struct ASImage;
struct ASVisual;
class TBrowser;
class THashTable;

class TASImage : public TImage {

private:
   enum { kNoZoom = 0, kZoom = 1, kZoomOps = -1 };
   enum { kReadWritePNG, kReadWriteVector };

   void DrawVLine(UInt_t x, UInt_t y1, UInt_t y2, UInt_t col, UInt_t thick);
   void DrawHLine(UInt_t y, UInt_t x1, UInt_t x2, UInt_t col, UInt_t thick);
   void DrawLineInternal(UInt_t x1, UInt_t y1, UInt_t x2, UInt_t y2, UInt_t col, UInt_t thick);
   void DrawWideLine(UInt_t x1, UInt_t y1, UInt_t x2, UInt_t y2,  UInt_t col, UInt_t thick);
   void DrawDashHLine(UInt_t y, UInt_t x1, UInt_t x2, UInt_t nDash, const char *pDash, UInt_t col, UInt_t thick);
   void DrawDashVLine(UInt_t x, UInt_t y1, UInt_t y2, UInt_t nDash, const char *pDash, UInt_t col, UInt_t thick);
   void DrawDashZLine(UInt_t x1, UInt_t y1, UInt_t x2, UInt_t y2, UInt_t nDash, const char *pDash, UInt_t col);
   void DrawDashZTLine(UInt_t x1, UInt_t y1, UInt_t x2, UInt_t y2, UInt_t nDash, const char *pDash, UInt_t col, UInt_t thick);
   Bool_t GetPolygonSpans(UInt_t npt, TPoint *ppt, UInt_t *nspans, TPoint **firstPoint, UInt_t **firstWidth);
   void GetFillAreaSpans(UInt_t npt, TPoint *ppt, UInt_t *nspans, TPoint **firstPoint, UInt_t **firstWidth);
   void FillRectangleInternal(UInt_t col, Int_t x, Int_t y, UInt_t width, UInt_t height);
   void DrawTextTTF(Int_t x, Int_t y, const char *text, Int_t size, UInt_t color, const char *font_name, Float_t angle);
   void DrawGlyph(void *bitmap, UInt_t color, Int_t x, Int_t y);
   void SetDefaults();
   void CreateThumbnail();
   void DestroyImage();
   const char *TypeFromMagicNumber(const char *file);

protected:
   ASImage  *fImage;        //! pointer to image structure of original image
   TASImage *fScaledImage;  //! temporary scaled and zoomed image produced from original image
   Double_t  fMaxValue;     //! max value in image
   Double_t  fMinValue;     //! min value in image
   UInt_t    fZoomOffX;     //! X - offset for zooming in image pixels
   UInt_t    fZoomOffY;     //! Y - offset for zooming im image pixels
   UInt_t    fZoomWidth;    //! width of zoomed image in image pixels
   UInt_t    fZoomHeight;   //! hight of zoomed image in image pixels
   Int_t     fZoomUpdate;   //! kZoom - new zooming required, kZoomOps - other ops in action, kNoZoom - no zooming or ops
   Bool_t    fEditable;     //! kTRUE image can be resized, moved by resizing/moving gPad
   Int_t     fPaintMode;    //! 1 - fast mode, 0 - low memory slow mode
   ASImage  *fGrayImage;    //! gray image
   Bool_t    fIsGray;       //! kTRUE if image is gray
   static THashTable *fgPlugList;   //! hash table containing loaded plugins

   static ASVisual *fgVisual;  // pointer to visual structure
   static Bool_t    fgInit;    // global flag to init afterimage only once

   EImageFileTypes GetFileType(const char *ext);
   void MapFileTypes(EImageFileTypes &type, UInt_t &astype, Bool_t toas = kTRUE);
   void MapQuality(EImageQuality &quality, UInt_t &asquality, Bool_t toas = kTRUE);

   static Bool_t InitVisual();

public:
   TASImage();
   TASImage(UInt_t w, UInt_t h);
   TASImage(const char *file, EImageFileTypes type = kUnknown);
   TASImage(const char *name, const Double_t *imageData, UInt_t width, UInt_t height, TImagePalette *palette = 0);
   TASImage(const char *name, const TArrayD &imageData, UInt_t width, TImagePalette *palette = 0);
   TASImage(const char *name, const TVectorD &imageData, UInt_t width, TImagePalette *palette = 0);
   TASImage(const TASImage &img);
   TASImage &operator=(const TASImage &img);
   virtual ~TASImage();

   TObject *Clone(const char *newname) const;

   void  SetEditable(Bool_t on = kTRUE) { fEditable = on; }             //*TOGGLE*
   Bool_t IsEditable() const { return fEditable; }
   void  Browse(TBrowser *);
   void  SetTitle(const char *title="");                                // *MENU*
   const char *GetTitle() const;
   const char *GetIconName() const {  return GetTitle(); }

   // Pad conversions
   void  FromPad(TVirtualPad *pad, Int_t x = 0, Int_t y = 0,
                 UInt_t w = 0, UInt_t h = 0);
   void  Draw(Option_t *option = "");
   void  Paint(Option_t *option = "");
   Int_t DistancetoPrimitive(Int_t px, Int_t py);
   void  ExecuteEvent(Int_t event, Int_t px, Int_t py);
   char *GetObjectInfo(Int_t px, Int_t py) const;

   // Transformations
   void  SetPalette(const TImagePalette *palette);
   void  Zoom(UInt_t offX, UInt_t offY, UInt_t width, UInt_t height);   //*MENU*
   void  UnZoom();                                                      //*MENU*
   void  Flip(Int_t flip = 180);                                        //*MENU*
   void  Mirror(Bool_t vert = kTRUE);                                   //*MENU*
   void  Scale(UInt_t width, UInt_t height);                            //*MENU*
   void  Slice(UInt_t xStart, UInt_t xEnd, UInt_t yStart, UInt_t yEnd,
               UInt_t toWidth, UInt_t toHeight);                        //*MENU*
   void  Tile(UInt_t width, UInt_t height);                             //*MENU*
   void  Crop(Int_t x = 0, Int_t y = 0, UInt_t width = 0, UInt_t height = 0); //*MENU*
   void  Pad(const char *color = "#00FFFFFF", UInt_t left = 0,
             UInt_t right = 0, UInt_t top = 0, UInt_t bottom = 0);      //*MENU*
   void  Blur(Double_t hr = 3, Double_t vr = 3);                        //*MENU*
   Double_t *Vectorize(UInt_t max_colors = 256, UInt_t dither = 4, Int_t opaque_threshold = 1);
   void  Gray(Bool_t on = kTRUE);                                       //*TOGGLE* *GETTER=IsGray
   void  StartPaletteEditor();                                          //*MENU*
   void  HSV(UInt_t hue = 0, UInt_t radius = 360, Int_t H = 0, Int_t S = 0, Int_t V = 0,
             Int_t x = 0, Int_t y = 0, UInt_t width = 0, UInt_t height = 0);
   void  Merge(const TImage *im, const char *op = "alphablend", Int_t x = 0, Int_t y = 0);
   void  Append(const TImage *im, const char * option = "+", const char *color = "#00000000");
   void  Gradient(UInt_t angle = 0, const char *colors = "#FFFFFF #000000", const char *offsets = 0,
                  Int_t x = 0, Int_t y = 0, UInt_t width = 0, UInt_t height = 0);
   void  Bevel(Int_t x = 0, Int_t y = 0, UInt_t width = 0, UInt_t height = 0, const char *hi = "#ffdddddd",
               const char *lo = "#ff555555", UShort_t thick = 1, Bool_t pressed = kFALSE);
   void  DrawText(Int_t  x = 0, Int_t y = 0, const char *text = "", Int_t size = 12,
                  const char *color = 0, const char *font = "fixed", EText3DType type = TImage::kPlain,
                  const char *fore_file = 0, Float_t angle = 0);
   void DrawText(TText *text, Int_t x = 0, Int_t y = 0);

   // Vector graphics
   void  BeginPaint(Bool_t fast = kTRUE);
   void  EndPaint();
   void  DrawLine(UInt_t x1, UInt_t y1, UInt_t x2, UInt_t y2, const char *col = "#000000", UInt_t thick = 1);
   void  DrawDashLine(UInt_t x1, UInt_t y1, UInt_t x2, UInt_t y2, UInt_t nDash, const char *pDash, const char *col = "#000000", UInt_t thick = 1);
   void  DrawBox(Int_t x1, Int_t y1, Int_t x2, Int_t y2, const char *col = "#000000", UInt_t thick = 1, Int_t mode = 0);
   void  DrawRectangle(UInt_t x, UInt_t y, UInt_t w, UInt_t h, const char *col = "#000000", UInt_t thick = 1);
   void  FillRectangle(const char *col = 0, Int_t x = 0, Int_t y = 0, UInt_t width = 0, UInt_t height = 0);
   void  DrawPolyLine(UInt_t nn, TPoint *xy, const char *col = "#000000", UInt_t thick = 1, TImage::ECoordMode mode = kCoordModeOrigin);
   void  PutPixel(Int_t x, Int_t y, const char *col = "#000000");
   void  PolyPoint(UInt_t npt, TPoint *ppt, const char *col = "#000000", TImage::ECoordMode mode = kCoordModeOrigin);
   void  DrawSegments(UInt_t nseg, Segment_t *seg, const char *col = "#000000", UInt_t thick = 1);
   void  FillPolygon(UInt_t npt, TPoint *ppt, const char *col = "#000000", const char *stipple = 0, UInt_t w = 16, UInt_t h = 16);
   void  FillPolygon(UInt_t npt, TPoint *ppt, TImage *tile);
   void  CropPolygon(UInt_t npt, TPoint *ppt);
   void  DrawFillArea(UInt_t npt, TPoint *ppt, const char *col = "#000000", const char *stipple = 0, UInt_t w = 16, UInt_t h = 16);
   void  DrawFillArea(UInt_t npt, TPoint *ppt, TImage *tile);
   void  FillSpans(UInt_t npt, TPoint *ppt, UInt_t *widths, const char *col = "#000000", const char *stipple = 0, UInt_t w = 16, UInt_t h = 16);
   void  FillSpans(UInt_t npt, TPoint *ppt, UInt_t *widths, TImage *tile);
   void  CropSpans(UInt_t npt, TPoint *ppt, UInt_t *widths);
   void  CopyArea(TImage *dst, Int_t xsrc, Int_t ysrc, UInt_t w, UInt_t h, Int_t xdst = 0, Int_t ydst = 0, Int_t gfunc = 3, EColorChan chan = kAllChan);
   void  DrawCellArray(Int_t x1, Int_t y1, Int_t x2, Int_t y2, Int_t nx, Int_t ny, UInt_t *ic);
   void  FloodFill(Int_t x, Int_t y, const char *col, const char *min_col, const char *max_col = 0);
   void  DrawCubeBezier(Int_t x1, Int_t y1, Int_t x2, Int_t y2, Int_t x3, Int_t y3, const char *col = "#000000", UInt_t thick = 1);
   void  DrawStraightEllips(Int_t x, Int_t y, Int_t rx, Int_t ry, const char *col = "#000000", Int_t thick = 1);
   void  DrawCircle(Int_t x, Int_t y, Int_t r, const char *col = "#000000", Int_t thick = 1);
   void  DrawEllips(Int_t x, Int_t y, Int_t rx, Int_t ry, Int_t angle, const char *col = "#000000", Int_t thick = 1);
   void  DrawEllips2(Int_t x, Int_t y, Int_t rx, Int_t ry, Int_t angle, const char *col = "#000000", Int_t thick = 1);

   // Input / output
   void  ReadImage(const char *file, EImageFileTypes type = TImage::kUnknown);
   void  WriteImage(const char *file, EImageFileTypes type = TImage::kUnknown); //*MENU*
   void  SetImage(const Double_t *imageData, UInt_t width, UInt_t height, TImagePalette *palette = 0);
   void  SetImage(const TArrayD &imageData, UInt_t width, TImagePalette *palette = 0);
   void  SetImage(const TVectorD &imageData, UInt_t width, TImagePalette *palette = 0);
   void  SetImage(Pixmap_t pxm, Pixmap_t mask = 0);
   void  FromWindow(Drawable_t wid, Int_t x = 0, Int_t y = 0, UInt_t w = 0, UInt_t h = 0);
   void  FromGLBuffer(UChar_t* buf, UInt_t w, UInt_t h);

   // Utilities
   UInt_t     GetWidth() const;
   UInt_t     GetHeight() const;
   UInt_t     GetScaledWidth() const;
   UInt_t     GetScaledHeight() const;
   Bool_t     IsValid() const { return fImage ? kTRUE : kFALSE; }
   Bool_t     IsGray() const { return fIsGray; }
   ASImage   *GetImage() const { return fImage; }
   void       SetImage(ASImage *image) { DestroyImage(); fImage = image; }
   TImage    *GetScaledImage() const { return fScaledImage; }
   Pixmap_t   GetPixmap();
   Pixmap_t   GetMask();
   TArrayL   *GetPixels(Int_t x = 0, Int_t y = 0, UInt_t w = 0, UInt_t h = 0);
   TArrayD   *GetArray(UInt_t w = 0, UInt_t h = 0, TImagePalette *pal = gWebImagePalette);
   UInt_t    *GetArgbArray();
   UInt_t    *GetRgbaArray();
   Double_t  *GetVecArray();
   UInt_t    *GetScanline(UInt_t y);
   void       GetImageBuffer(char **buffer, int *size, EImageFileTypes type = TImage::kPng);
   void       GetZoomPosition(UInt_t &x, UInt_t &y, UInt_t &w, UInt_t &h) const;
   Bool_t     SetImageBuffer(char **buffer, EImageFileTypes type = TImage::kPng);
   void       PaintImage(Drawable_t wid, Int_t x, Int_t y, Int_t xsrc = 0, Int_t ysrc = 0, UInt_t wsrc = 0, UInt_t hsrc = 0, Option_t *opt = "");
   void       SetPaletteEnabled(Bool_t on = kTRUE);  // *TOGGLE*
   void       SavePrimitive(ostream &out, Option_t *option = "");

   static const ASVisual *GetVisual();
   static UInt_t AlphaBlend(UInt_t bot, UInt_t top);
   static void Image2Drawable(ASImage *im, Drawable_t wid, Int_t x, Int_t y, Int_t xsrc = 0, Int_t ysrc = 0, UInt_t wsrc = 0, UInt_t hsrc = 0, Option_t *opt = "");

   // some static functions
   Bool_t SetJpegDpi(const char *name, UInt_t dpi = 72);

   ClassDef(TASImage,2)  // image processing class
};

#endif
 TASImage.h:1
 TASImage.h:2
 TASImage.h:3
 TASImage.h:4
 TASImage.h:5
 TASImage.h:6
 TASImage.h:7
 TASImage.h:8
 TASImage.h:9
 TASImage.h:10
 TASImage.h:11
 TASImage.h:12
 TASImage.h:13
 TASImage.h:14
 TASImage.h:15
 TASImage.h:16
 TASImage.h:17
 TASImage.h:18
 TASImage.h:19
 TASImage.h:20
 TASImage.h:21
 TASImage.h:22
 TASImage.h:23
 TASImage.h:24
 TASImage.h:25
 TASImage.h:26
 TASImage.h:27
 TASImage.h:28
 TASImage.h:29
 TASImage.h:30
 TASImage.h:31
 TASImage.h:32
 TASImage.h:33
 TASImage.h:34
 TASImage.h:35
 TASImage.h:36
 TASImage.h:37
 TASImage.h:38
 TASImage.h:39
 TASImage.h:40
 TASImage.h:41
 TASImage.h:42
 TASImage.h:43
 TASImage.h:44
 TASImage.h:45
 TASImage.h:46
 TASImage.h:47
 TASImage.h:48
 TASImage.h:49
 TASImage.h:50
 TASImage.h:51
 TASImage.h:52
 TASImage.h:53
 TASImage.h:54
 TASImage.h:55
 TASImage.h:56
 TASImage.h:57
 TASImage.h:58
 TASImage.h:59
 TASImage.h:60
 TASImage.h:61
 TASImage.h:62
 TASImage.h:63
 TASImage.h:64
 TASImage.h:65
 TASImage.h:66
 TASImage.h:67
 TASImage.h:68
 TASImage.h:69
 TASImage.h:70
 TASImage.h:71
 TASImage.h:72
 TASImage.h:73
 TASImage.h:74
 TASImage.h:75
 TASImage.h:76
 TASImage.h:77
 TASImage.h:78
 TASImage.h:79
 TASImage.h:80
 TASImage.h:81
 TASImage.h:82
 TASImage.h:83
 TASImage.h:84
 TASImage.h:85
 TASImage.h:86
 TASImage.h:87
 TASImage.h:88
 TASImage.h:89
 TASImage.h:90
 TASImage.h:91
 TASImage.h:92
 TASImage.h:93
 TASImage.h:94
 TASImage.h:95
 TASImage.h:96
 TASImage.h:97
 TASImage.h:98
 TASImage.h:99
 TASImage.h:100
 TASImage.h:101
 TASImage.h:102
 TASImage.h:103
 TASImage.h:104
 TASImage.h:105
 TASImage.h:106
 TASImage.h:107
 TASImage.h:108
 TASImage.h:109
 TASImage.h:110
 TASImage.h:111
 TASImage.h:112
 TASImage.h:113
 TASImage.h:114
 TASImage.h:115
 TASImage.h:116
 TASImage.h:117
 TASImage.h:118
 TASImage.h:119
 TASImage.h:120
 TASImage.h:121
 TASImage.h:122
 TASImage.h:123
 TASImage.h:124
 TASImage.h:125
 TASImage.h:126
 TASImage.h:127
 TASImage.h:128
 TASImage.h:129
 TASImage.h:130
 TASImage.h:131
 TASImage.h:132
 TASImage.h:133
 TASImage.h:134
 TASImage.h:135
 TASImage.h:136
 TASImage.h:137
 TASImage.h:138
 TASImage.h:139
 TASImage.h:140
 TASImage.h:141
 TASImage.h:142
 TASImage.h:143
 TASImage.h:144
 TASImage.h:145
 TASImage.h:146
 TASImage.h:147
 TASImage.h:148
 TASImage.h:149
 TASImage.h:150
 TASImage.h:151
 TASImage.h:152
 TASImage.h:153
 TASImage.h:154
 TASImage.h:155
 TASImage.h:156
 TASImage.h:157
 TASImage.h:158
 TASImage.h:159
 TASImage.h:160
 TASImage.h:161
 TASImage.h:162
 TASImage.h:163
 TASImage.h:164
 TASImage.h:165
 TASImage.h:166
 TASImage.h:167
 TASImage.h:168
 TASImage.h:169
 TASImage.h:170
 TASImage.h:171
 TASImage.h:172
 TASImage.h:173
 TASImage.h:174
 TASImage.h:175
 TASImage.h:176
 TASImage.h:177
 TASImage.h:178
 TASImage.h:179
 TASImage.h:180
 TASImage.h:181
 TASImage.h:182
 TASImage.h:183
 TASImage.h:184
 TASImage.h:185
 TASImage.h:186
 TASImage.h:187
 TASImage.h:188
 TASImage.h:189
 TASImage.h:190
 TASImage.h:191
 TASImage.h:192
 TASImage.h:193
 TASImage.h:194
 TASImage.h:195
 TASImage.h:196
 TASImage.h:197
 TASImage.h:198
 TASImage.h:199
 TASImage.h:200
 TASImage.h:201
 TASImage.h:202
 TASImage.h:203
 TASImage.h:204
 TASImage.h:205
 TASImage.h:206
 TASImage.h:207
 TASImage.h:208
 TASImage.h:209
 TASImage.h:210
 TASImage.h:211
 TASImage.h:212
 TASImage.h:213
 TASImage.h:214
 TASImage.h:215
 TASImage.h:216
 TASImage.h:217
 TASImage.h:218
 TASImage.h:219
 TASImage.h:220
 TASImage.h:221
 TASImage.h:222
 TASImage.h:223
 TASImage.h:224
 TASImage.h:225
 TASImage.h:226
 TASImage.h:227
 TASImage.h:228
 TASImage.h:229