Main Page Class Hierarchy Compound List File List Compound Members File Members

# bitmap.h

Go to the documentation of this file.

    00001
    00002
    00003 //
    00004 //      BITMAP.H                                        (c)     YoY'99                                          WEB: www.aestesis.org
    00005 //
    00008 #ifndef                                                 _BITMAP_H_
    00009 #define                                                 _BITMAP_H_
    00012 #include                                                <windows.h>
    00013 #include                                                "types.h"
    00014 #include                                                "node.h"
    00015 #include                                                "rectangle.h"
    00018
    00019 //      If you use directly the variables in the class API, it's at your own risks. Prefer the methods...
    00020
    00023
    00024 #define                                                 guidBITMAP                                              (Abitmap::CI.guid)
    00025
    00028
    00029 #define                                                 bitmapMAXSIZE                                   8192
    00030
    00032 enum
    00033 {
    00034                                                                 lineaNORMAL,
    00035                                                                 lineaADD,
    00036                                                                 lineaSUB,
    00037                                                                 lineaXOR,
    00038                                                                 lineaOLDALPHA
    00039 };
    00040
    00041 #define                                                 lineaSATURATION                                 bitmapSATURATION
    00042
    00043
    00045 enum
    00046 {
    00047                                                                 bitmapNORMAL=0,
    00048                                                                 bitmapOR,
    00049                                                                 bitmapXOR,
    00050                                                                 bitmapAND,
    00051                                                                 bitmapADD,
    00052                                                                 bitmapSUB
    00053 };
    00054
    00056 #define                                                 bitmapSATURATION                                (1<<31)
    00057 #define                                                 bitmapGLOBALALPHA                               (1<<30)
    00058 #define                                                 bitmapALPHA                                             (1<<29)
    00059 #define                                                 bitmapCOLORKEY                                  (1<<28)
    00060
    00062 #define                                                 bitmapBLACK                                             0xff000000
    00063 #define                                                 bitmapRED                                               0xffff0000
    00064 #define                                                 bitmapGREEN                                             0xff00ff00
    00065 #define                                                 bitmapBLUE                                              0xff0000ff
    00066 #define                                                 bitmapYELLOW                                    0xffffff00
    00067 #define                                                 bitmapCYAN                                              0xff00ffff
    00068 #define                                                 bitmapMAGENTA                                   0xffff00ff
    00069 #define                                                 bitmapWHITE                                             0xffffffff
    00070 #define                                                 bitmapGRAY                                              0xff808080
    00071 #define                                                 bitmapDARKGRAY                                  0xff404040
    00072
    00074 #define                                                 bltmodeSRC                                              0
    00075 #define                                                 bltmodeXOR                                              1
    00076 #define                                                 bltmodeOR                                               2
    00077 #define                                                 bltmodeAND                                              3
    00078
    00080 #define                                                 bitmapBMP                                               0
    00081 #define                                                 bitmapPNG                                               1
    00082 #define                                                 bitmapJPG                                               2
    00083
    00084
    00087
    00089 class Abitmap : public Anode
    00090 {
    00091 public:
    00092         ADLL static ACI                         CI;
    00093         virtual ACI                                     *getCI                                                  ()                                              { return &CI; }
    00094
    00096         dword                                           state;
    00098         dword                                           flags;
    00100         dword                                           colorkey;
    00102         float                                           alpha;
    00103
    00104         int                                                     w;
    00105         int                                                     h;
    00106         int                                                     nbBits;
    00107         int                                                     bodysize;
    00108
    00109         union
    00110         {
    00111                 void                                    *body;
    00112                 byte                                    *body8;
    00113                 word                                    *body16;
    00114                 dword                                   *body32;
    00115         };
    00116         dword                                           *palette;
    00117
    00118         Arectangle                                      clip;
    00119         Arectangle                                      view;
    00120
    00121                                                                 Abitmap                                                 (const class Abitmap &b);
    00122         ADLL                                            Abitmap                                                 (int w, int h, int nbBits=32);
    00123         ADLL                                            Abitmap                                                 (char *filename);
    00124         ADLL                                            Abitmap                                                 (class Aresobj *o);
    00125         ADLL virtual                            ~Abitmap                                                ();
    00126
    00128         ADLL bool                                       size                                                    (int w, int h);
    00130         ADLL bool                                       set                                                             (HDC hdc, int x=0, int y=0, int mblt=bltmodeSRC);
    00131
    00133         bool                                            set                                                             (int x, int y, class Abitmap *b, bool useFlags=TRUE);
    00135         ADLL bool                                       set                                                             (int xd, int yd, int xs, int ys, int w, int h, class Abitmap *b, bool useFlags=TRUE);
    00137         ADLL bool                                       set                                                             (int xd, int yd, int wd, int hd, int xs, int ys, int ws, int hs, class Abitmap *b, bool useFlags=TRUE);
    00138
    00140         bool                                            pixel                                                   (int x, int y, dword c);
    00142         ADLL bool                                       line                                                    (int x0, int y0, int x1, int y1, dword c);
    00144         ADLL bool                                       box                                                             (int x0, int y0, int x1, int y1, dword c);
    00146         ADLL bool                                       boxf                                                    (int x0, int y0, int x1, int y1, dword c);
    00147
    00149         ADLL bool                                       linea                                                   (int x0, int y0, int x1, int y1, dword c, float alpha, dword mode=lineaNORMAL);
    00151         ADLL bool                                       boxa                                                    (int x0, int y0, int x1, int y1, dword c, float alpha, dword mode=lineaNORMAL);
    00153         ADLL bool                                       boxfa                                                   (int x0, int y0, int x1, int y1, dword c, float alpha, dword mode=lineaNORMAL);
    00154
    00155         ADLL bool                                       flipY                                                   ();
    00157         ADLL bool                                       blur                                                    (Abitmap *b, float coefx, float coefy);
    00158
    00159         ADLL static Abitmap *           getDesktop                                              ();
    00160         ADLL bool                                       getDesktop                                              (int x, int y);
    00161
    00163
    00164         ADLL bool                                       load                                                    (char *filename);
    00166
    00167         ADLL bool                                       save                                                    (char *filename, int quality=100);
    00168         ADLL bool                                       save                                                    (class Afile *f, int type=bitmapJPG, int quality=100);
    00169
    00170         ADLL void                                       operator=						(const Abitmap& b);
    00171
    00172         // private
    00173
    00174         dword                                           *adr;
    00175
    00176 private:
    00177
    00178         ADLL bool                                       NCcreate                                                ();
    00179         ADLL bool                                       NCfree                                                  ();
    00180         ADLL bool                                       NCcopy                                                  (const Abitmap *b);
    00181         ADLL bool                                       NCreadPNG                                               (struct png_struct_def *png_ptr, struct png_info_struct *info_ptr);
    00182         ADLL bool                                       NCreadJPG                                               (Afile *file);
    00183         ADLL bool                                       NCwriteJPG                                              (Afile *file, int quality);
    00184 };
    00185
    00188
    00189 __inline Abitmap::Abitmap(const Abitmap &b) : Anode()
    00190 {
    00191         adr=NULL;
    00192         palette=NULL;
    00193         body=NULL;
    00194         NCcopy(&b);
    00195 }
    00196
    00198
    00199 __inline bool Abitmap::pixel(int x, int y, dword c)
    00200 {
    00201         x+=view.x;
    00202         y+=view.y;
    00203         if(clip.contains(x, y))
    00204         {
    00205                 switch(nbBits)
    00206                 {
    00207                         case 8:
    00208                         body8[adr[y]+x]=(byte)c;
    00209                         return TRUE;
    00210
    00211                         case 16:
    00212                         body16[adr[y]+x]=(word)c;
    00213                         return TRUE;
    00214
    00215                         case 32:
    00216                         body32[adr[y]+x]=c;
    00217                         return TRUE;
    00218                 }
    00219         }
    00220         return FALSE;
    00221 }
    00222
    00224
    00225 __inline bool Abitmap::set(int x, int y, class Abitmap *b, bool useFlags)
    00226 {
    00227         return set(x, y, 0, 0, b->w, b->h, b, useFlags);
    00228 }
    00229
    00232
    00233 __inline word color32to16(dword color)
    00234 {
    00235         return (word)(((color&0x0000ff)>>3)|((color&0x00ff00)>>6)|((color&0xff0000)>>9));
    00236 }
    00237
    00238 __inline dword color16to32(word color)
    00239 {
    00240         return (((dword)color&0x001f)<<3)|(((dword)color&0x03e0)<<6)|(((dword)color&0x7c00)<<9);
    00241 }
    00242
    00243 __inline void colorRGB(byte *r, byte *g, byte *b, dword color)
    00244 {
    00245         *r=(byte)(color>>16);
    00246         *g=(byte)(color>>8);
    00247         *b=(byte)(color);
    00248 }
    00249
    00250 __inline void colorRGBA(byte *r, byte *g, byte *b, byte *a, dword color)
    00251 {
    00252         *a=(byte)(color>>24);
    00253         *r=(byte)(color>>16);
    00254         *g=(byte)(color>>8);
    00255         *b=(byte)(color);
    00256 }
    00257
    00258 __inline void colorRGB(byte *r, byte *g, byte *b, word color)
    00259 {
    00260         *r=(byte)((color&0x07c0)>>7);
    00261         *g=(byte)((color&0x03e0)>>2);
    00262         *b=(byte)((color&0x001f)<<3);
    00263 }
    00264
    00265 __inline dword color32(byte r, byte g, byte b)
    00266 {
    00267         return 0xff000000|((dword)r<<16)|((dword)g<<8)|(dword)b;
    00268 }
    00269
    00270 __inline dword color32(byte r, byte g, byte b, byte a)
    00271 {
    00272         return ((dword)a<<24)|((dword)r<<16)|((dword)g<<8)|(dword)b;
    00273 }
    00274
    00275 __inline word color16(byte r, byte g, byte b)
    00276 {
    00277         return (word)((((dword)r&0xf8)<<7)|(((dword)g&0xf8)<<2)|((dword)b>>3));
    00278 }
    00279
    00280
    00283 //      HLS conversion
    00286
    00287 __inline void rgb2hls(float *h, float *l, float *s, byte r0, byte g0, byte b0)
    00288 {
    00289         float   r=(float)r0/255;
    00290         float   g=(float)g0/255;
    00291         float   b=(float)b0/255;
    00292         float   max=maxi(maxi(r, g), b);
    00293         float   min=mini(mini(r, g), b);
    00294         float   d=max-min;
    00295         *l=(max+min)/2;
    00296         if(((d<0)?(-d):d)<0.001f)
    00297         {
    00298                 *h=0;
    00299                 *s=0;
    00300         }
    00301         else
    00302         {
    00303                 if(*l<0.5)
    00304                         *s=d/(max+min);
    00305                 else
    00306                         *s=d/(2-max-min);
    00307
    00308                 if(max==r)
    00309                         *h=(g-b)/d;
    00310                 else if(max==g)
    00311                         *h=2+(b-r)/d;
    00312                 else if(max==b)
    00313                         *h=4+(r-g)/d ;
    00314
    00315                 *h=*h*60;
    00316         if(*h<0)
    00317                         *h=*h+360;
    00318         }
    00319 }
    00320
    00321 __inline float hls2rgb_rgb(float p1, float p2, float h) // call by hls2rgb
    00322 {
    00323         if(h>360)
    00324                 h-=360;
    00325         else if(h<0)
    00326                 h=h+360;
    00327         if(h<60)
    00328                 return mini(1.f, maxi(0.f, p1+(p2+p1)*h/60));
    00329         else if(h<180)
    00330                 return mini(1.f, maxi(0.f, p2));
    00331         else if(h<=240)
    00332                 return mini(1.f, maxi(0.f,p1+(p2-p1)*(240-h)/60));
    00333         return mini(1.f, maxi(0.f, p1));
    00334 }
    00335
    00336 __inline void hls2rgb(float h, float l, float s, byte *r, byte *g, byte *b)
    00337 {
    00338         float   p1,p2;
    00339         if(h<0)
    00340                 h+=360;
    00341         else if(h>=360)
    00342                 h-=360;
    00343         if(l<0.5)
    00344                 p2=l*(1+s);
    00345         else
    00346                 p2=l+s-(l*s);
    00347         p1=2*l-p2;
    00348         if(s==0)
    00349         {
    00350                 *r=*g=*b=(byte)(l*255.99);
    00351         }
    00352         else
    00353         {
    00354                 *r=(byte)(hls2rgb_rgb(p1, p2, h+120)*255.99);
    00355                 *g=(byte)(hls2rgb_rgb(p1, p2, h)*255.99);
    00356                 *b=(byte)(hls2rgb_rgb(p1, p2, h-120)*255.99);
    00357         }
    00358 }
    00359
    00360
    00361 // h = [0,360], s = [0,1], v = [0,1]
    00362 //              if s == 0, then h = -1 (undefined)
    00363
    00364 /*
    00365 __inline void rgb2hsv(byte r0, byte g0, byte b0, float *h, float *s, float *v)
    00366 {
    00367         float   r=(float)r0/255;
    00368         float   g=(float)g0/255;
    00369         float   b=(float)b0/255;
    00370         float   min, max, delta;
    00371         min = mini(mini(r, g), b);
    00372         max = maxi(maxi(r, g), b);
    00373         *v = max;                               // v
    00374         delta = max - min;
    00375         if( max != 0 )
    00376                 *s = delta / max;               // s
    00377         else {
    00378                 // r = g = b = 0                // s = 0, v is undefined
    00379                 *s = 0;
    00380                 *h = -1;
    00381                 return;
    00382         }
    00383         if( r == max )
    00384                 *h = ( g - b ) / delta;         // between yellow & magenta
    00385         else if( g == max )
    00386                 *h = 2 + ( b - r ) / delta;     // between cyan & yellow
    00387         else
    00388                 *h = 4 + ( r - g ) / delta;     // between magenta & cyan
    00389         *h *= 60;                               // degrees
    00390         if( *h < 0 )
    00391                 *h += 360;
    00392 }
    00393
    00394 __inline void hsv2rgb(byte *r, byte *g, byte *b, float h, float s, float v)
    00395 {
    00396         int i;
    00397         float f, p, q, t;
    00398         if(s==0)
    00399         {
    00400                 // achromatic (grey)
    00401                 *r = *g = *b = v;
    00402                 return;
    00403         }
    00404         h /= 60;                        // sector 0 to 5
    00405         i = floor( h );
    00406         f = h - i;                      // factorial part of h
    00407         p = v * ( 1 - s );
    00408         q = v * ( 1 - s * f );
    00409         t = v * ( 1 - s * ( 1 - f ) );
    00410         switch( i )
    00411         {
    00412                 case 0:
    00413                         *r = v;
    00414                         *g = t;
    00415                         *b = p;
    00416                         break;
    00417                 case 1:
    00418                         *r = q;
    00419                         *g = v;
    00420                         *b = p;
    00421                         break;
    00422                 case 2
    00423                         *r = p;
    00424                         *g = v;
    00425                         *b = t;
    00426                         break;
    00427                 case 3:
    00428                         *r = p;
    00429                         *g = q;
    00430                         *b = v;
    00431                         break;
    00432                 case 4:
    00433                         *r = t;
    00434                         *g = p;
    00435                         *b = v;
    00436                         break;
    00437                 default:                // case 5:
    00438                         *r = v;
    00439                         *g = p;
    00440                         *b = q;
    00441                         break;
    00442         }
    00443 }
    00444 */
    00445
    00448 #endif                                                  //_BITMAP_H_