fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_Bitmap.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Bitmap.cxx 8190 2011-01-05 10:21:45Z manolo $"
00003 //
00004 // Bitmap drawing routines for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00034 #include <FL/Fl.H>
00035 #include <FL/x.H>
00036 #include <FL/fl_draw.H>
00037 #include <FL/Fl_Widget.H>
00038 #include <FL/Fl_Menu_Item.H>
00039 #include <FL/Fl_Bitmap.H>
00040 #include <FL/Fl_Printer.H>
00041 #include "flstring.h"
00042 
00043 #if defined(__APPLE_QUARTZ__)
00044 
00045 
00046 Fl_Bitmask fl_create_bitmask(int w, int h, const uchar *array) {
00047   static uchar reverse[16] =    /* Bit reversal lookup table */
00048     { 0x00, 0x88, 0x44, 0xcc, 0x22, 0xaa, 0x66, 0xee, 
00049       0x11, 0x99, 0x55, 0xdd, 0x33, 0xbb, 0x77, 0xff };
00050   int rowBytes = (w+7)>>3 ;
00051   uchar *bmask = (uchar*)malloc(rowBytes*h), *dst = bmask;
00052   const uchar *src = array;
00053   for ( int i=rowBytes*h; i>0; i--,src++ ) {
00054     *dst++ = ((reverse[*src & 0x0f] & 0xf0) | (reverse[(*src >> 4) & 0x0f] & 0x0f))^0xff;
00055   }
00056   CGDataProviderRef srcp = CGDataProviderCreateWithData( 0L, bmask, rowBytes*h, 0L);
00057   CGImageRef id_ = CGImageMaskCreate( w, h, 1, 1, rowBytes, srcp, 0L, false);
00058   CGDataProviderRelease(srcp);
00059   return (Fl_Bitmask)id_;
00060 }
00061 void fl_delete_bitmask(Fl_Bitmask bm) {
00062   if (bm) CGImageRelease((CGImageRef)bm);
00063 }
00064 
00065 
00066 #elif defined(WIN32) // Windows bitmask functions...
00067 
00068 
00069 // 'fl_create_bitmap()' - Create a 1-bit bitmap for drawing...
00070 static Fl_Bitmask fl_create_bitmap(int w, int h, const uchar *data) {
00071   // we need to pad the lines out to words & swap the bits
00072   // in each byte.
00073   int w1 = (w+7)/8;
00074   int w2 = ((w+15)/16)*2;
00075   uchar* newarray = new uchar[w2*h];
00076   const uchar* src = data;
00077   uchar* dest = newarray;
00078   Fl_Bitmask bm;
00079   static uchar reverse[16] =    /* Bit reversal lookup table */
00080               { 0x00, 0x88, 0x44, 0xcc, 0x22, 0xaa, 0x66, 0xee,
00081                 0x11, 0x99, 0x55, 0xdd, 0x33, 0xbb, 0x77, 0xff };
00082 
00083   for (int y=0; y < h; y++) {
00084     for (int n = 0; n < w1; n++, src++)
00085       *dest++ = (uchar)((reverse[*src & 0x0f] & 0xf0) |
00086                         (reverse[(*src >> 4) & 0x0f] & 0x0f));
00087     dest += w2-w1;
00088   }
00089 
00090   bm = CreateBitmap(w, h, 1, 1, newarray);
00091 
00092   delete[] newarray;
00093 
00094   return bm;
00095 }
00096 
00097 // 'fl_create_bitmask()' - Create an N-bit bitmap for masking...
00098 Fl_Bitmask fl_create_bitmask(int w, int h, const uchar *data) {
00099   // this won't work when the user changes display mode during run or
00100   // has two screens with differnet depths
00101   Fl_Bitmask bm;
00102   static uchar hiNibble[16] =
00103   { 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
00104     0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0 };
00105   static uchar loNibble[16] =
00106   { 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
00107     0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f };
00108   int np  = GetDeviceCaps(fl_gc, PLANES);       //: was always one on sample machines
00109   int bpp = GetDeviceCaps(fl_gc, BITSPIXEL);//: 1,4,8,16,24,32 and more odd stuff?
00110   int Bpr = (bpp*w+7)/8;                        //: bytes per row
00111   int pad = Bpr&1, w1 = (w+7)/8, shr = ((w-1)&7)+1;
00112   if (bpp==4) shr = (shr+1)/2;
00113   uchar *newarray = new uchar[(Bpr+pad)*h];
00114   uchar *dst = newarray;
00115   const uchar *src = data;
00116 
00117   for (int i=0; i<h; i++) {
00118     // This is slooow, but we do it only once per pixmap
00119     for (int j=w1; j>0; j--) {
00120       uchar b = *src++;
00121       if (bpp==1) {
00122         *dst++ = (uchar)( hiNibble[b&15] ) | ( loNibble[(b>>4)&15] );
00123       } else if (bpp==4) {
00124         for (int k=(j==1)?shr:4; k>0; k--) {
00125           *dst++ = (uchar)("\377\360\017\000"[b&3]);
00126           b = b >> 2;
00127         }
00128       } else {
00129         for (int k=(j==1)?shr:8; k>0; k--) {
00130           if (b&1) {
00131             *dst++=0;
00132             if (bpp>8) *dst++=0;
00133             if (bpp>16) *dst++=0;
00134             if (bpp>24) *dst++=0;
00135           } else {
00136             *dst++=0xff;
00137             if (bpp>8) *dst++=0xff;
00138             if (bpp>16) *dst++=0xff;
00139             if (bpp>24) *dst++=0xff;
00140           }
00141 
00142           b = b >> 1;
00143         }
00144       }
00145     }
00146 
00147     dst += pad;
00148   }
00149 
00150   bm = CreateBitmap(w, h, np, bpp, newarray);
00151   delete[] newarray;
00152 
00153   return bm;
00154 }
00155 
00156 
00157 void fl_delete_bitmask(Fl_Bitmask bm) {
00158   DeleteObject((HGDIOBJ)bm);
00159 }
00160 
00161 
00162 #else // X11 bitmask functions
00163 
00164 
00165 Fl_Bitmask fl_create_bitmask(int w, int h, const uchar *data) {
00166   return XCreateBitmapFromData(fl_display, fl_window, (const char *)data,
00167                                (w+7)&-8, h);
00168 }
00169 
00170 void fl_delete_bitmask(Fl_Bitmask bm) {
00171   fl_delete_offscreen((Fl_Offscreen)bm);
00172 }
00173 
00174 
00175 #endif // __APPLE__
00176 
00177 
00178 // Create a 1-bit mask used for alpha blending
00179 Fl_Bitmask fl_create_alphamask(int w, int h, int d, int ld, const uchar *array) {
00180   Fl_Bitmask bm;
00181   int bmw = (w + 7) / 8;
00182   uchar *bitmap = new uchar[bmw * h];
00183   uchar *bitptr, bit;
00184   const uchar *dataptr;
00185   int x, y;
00186   static uchar dither[16][16] = { // Simple 16x16 Floyd dither
00187     { 0,   128, 32,  160, 8,   136, 40,  168,
00188       2,   130, 34,  162, 10,  138, 42,  170 },
00189     { 192, 64,  224, 96,  200, 72,  232, 104,
00190       194, 66,  226, 98,  202, 74,  234, 106 },
00191     { 48,  176, 16,  144, 56,  184, 24,  152,
00192       50,  178, 18,  146, 58,  186, 26,  154 },
00193     { 240, 112, 208, 80,  248, 120, 216, 88,
00194       242, 114, 210, 82,  250, 122, 218, 90 },
00195     { 12,  140, 44,  172, 4,   132, 36,  164,
00196       14,  142, 46,  174, 6,   134, 38,  166 },
00197     { 204, 76,  236, 108, 196, 68,  228, 100,
00198       206, 78,  238, 110, 198, 70,  230, 102 },
00199     { 60,  188, 28,  156, 52,  180, 20,  148,
00200       62,  190, 30,  158, 54,  182, 22,  150 },
00201     { 252, 124, 220, 92,  244, 116, 212, 84,
00202       254, 126, 222, 94,  246, 118, 214, 86 },
00203     { 3,   131, 35,  163, 11,  139, 43,  171,
00204       1,   129, 33,  161, 9,   137, 41,  169 },
00205     { 195, 67,  227, 99,  203, 75,  235, 107,
00206       193, 65,  225, 97,  201, 73,  233, 105 },
00207     { 51,  179, 19,  147, 59,  187, 27,  155,
00208       49,  177, 17,  145, 57,  185, 25,  153 },
00209     { 243, 115, 211, 83,  251, 123, 219, 91,
00210       241, 113, 209, 81,  249, 121, 217, 89 },
00211     { 15,  143, 47,  175, 7,   135, 39,  167,
00212       13,  141, 45,  173, 5,   133, 37,  165 },
00213     { 207, 79,  239, 111, 199, 71,  231, 103,
00214       205, 77,  237, 109, 197, 69,  229, 101 },
00215     { 63,  191, 31,  159, 55,  183, 23,  151,
00216       61,  189, 29,  157, 53,  181, 21,  149 },
00217     { 254, 127, 223, 95,  247, 119, 215, 87,
00218       253, 125, 221, 93,  245, 117, 213, 85 }
00219   };
00220 
00221   // Generate a 1-bit "screen door" alpha mask; not always pretty, but
00222   // definitely fast...  In the future we may be able to support things
00223   // like the RENDER extension in XFree86, when available, to provide
00224   // true RGBA-blended rendering.  See:
00225   //
00226   //     http://www.xfree86.org/~keithp/render/protocol.html
00227   //
00228   // for more info on XRender...
00229   //
00230   // MacOS already provides alpha blending support and has its own
00231   // fl_create_alphamask() function...
00232   memset(bitmap, 0, bmw * h);
00233 
00234   for (dataptr = array + d - 1, y = 0; y < h; y ++, dataptr += ld)
00235     for (bitptr = bitmap + y * bmw, bit = 1, x = 0; x < w; x ++, dataptr += d) {
00236       if (*dataptr > dither[x & 15][y & 15])
00237         *bitptr |= bit;
00238       if (bit < 128) bit <<= 1;
00239       else {
00240         bit = 1;
00241         bitptr ++;
00242       }
00243     }
00244 
00245   bm = fl_create_bitmask(w, h, bitmap);
00246   delete[] bitmap;
00247 
00248   return (bm);
00249 }
00250 
00251 void Fl_Bitmap::draw(int XP, int YP, int WP, int HP, int cx, int cy) {
00252   fl_graphics_driver->draw(this, XP, YP, WP, HP, cx, cy);
00253 }
00254 
00255 static int start(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int w, int h, int &cx, int &cy, 
00256                  int &X, int &Y, int &W, int &H)
00257 {
00258   // account for current clip region (faster on Irix):
00259   fl_clip_box(XP,YP,WP,HP,X,Y,W,H);
00260   cx += X-XP; cy += Y-YP;
00261   // clip the box down to the size of image, quit if empty:
00262   if (cx < 0) {W += cx; X -= cx; cx = 0;}
00263   if (cx+W > w) W = w-cx;
00264   if (W <= 0) return 1;
00265   if (cy < 0) {H += cy; Y -= cy; cy = 0;}
00266   if (cy+H > h) H = h-cy;
00267   if (H <= 0) return 1;
00268   return 0;
00269 }
00270 
00271 #ifdef __APPLE__
00272 void Fl_Quartz_Graphics_Driver::draw(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int cx, int cy) {
00273   int X, Y, W, H;
00274   if (!bm->array) {
00275     bm->draw_empty(XP, YP);
00276     return;
00277   }
00278   if (start(bm, XP, YP, WP, HP, bm->w(), bm->h(), cx, cy, X, Y, W, H)) {
00279     return;
00280   }
00281   if (!bm->id_) bm->id_ = fl_create_bitmask(bm->w(), bm->h(), bm->array);
00282   if (bm->id_ && fl_gc) {
00283     CGRect rect = { { X, Y }, { W, H } };
00284     Fl_X::q_begin_image(rect, cx, cy, bm->w(), bm->h());
00285     CGContextDrawImage(fl_gc, rect, (CGImageRef)bm->id_);
00286     Fl_X::q_end_image();
00287   }
00288 }
00289 
00290 #elif defined(WIN32)
00291 void Fl_GDI_Graphics_Driver::draw(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int cx, int cy) {
00292   int X, Y, W, H;
00293   if (!bm->array) {
00294     bm->draw_empty(XP, YP);
00295     return;
00296   }
00297   if (start(bm, XP, YP, WP, HP, bm->w(), bm->h(), cx, cy, X, Y, W, H)) {
00298     return;
00299   }
00300   if (!bm->id_) bm->id_ = fl_create_bitmap(bm->w(), bm->h(), bm->array);
00301   
00302   typedef BOOL (WINAPI* fl_transp_func)  (HDC,int,int,int,int,HDC,int,int,int,int,UINT);
00303   static fl_transp_func fl_TransparentBlt;
00304   HDC tempdc;
00305   int save;
00306   BOOL use_print_algo = false;
00307   if (fl_surface->class_name() == Fl_Printer::class_id) {
00308     static HMODULE hMod = NULL;
00309     if (!hMod) {
00310       hMod = LoadLibrary("MSIMG32.DLL");
00311       if (hMod) fl_TransparentBlt = (fl_transp_func)GetProcAddress(hMod, "TransparentBlt");
00312     }
00313     if (fl_TransparentBlt) use_print_algo = true;
00314   }
00315   if (use_print_algo) { // algorithm for bitmap output to Fl_GDI_Printer
00316     Fl_Offscreen tmp_id = fl_create_offscreen(W, H);
00317     fl_begin_offscreen(tmp_id);
00318     Fl_Color save_c = fl_color(); // save bitmap's desired color
00319     uchar r, g, b;
00320     Fl::get_color(save_c, r, g, b);
00321     r = 255-r;
00322     g = 255-g;
00323     b = 255-b;
00324     Fl_Color background = fl_rgb_color(r, g, b); // a color very different from the bitmap's
00325     fl_color(background);
00326     fl_rectf(0,0,W,H); // use this color as offscreen background
00327     fl_color(save_c); // back to bitmap's color
00328     tempdc = CreateCompatibleDC(fl_gc);
00329     save = SaveDC(tempdc);
00330     SelectObject(tempdc, (HGDIOBJ)bm->id_);
00331     SelectObject(fl_gc, fl_brush()); // use bitmap's desired color
00332     BitBlt(fl_gc, 0, 0, W, H, tempdc, 0, 0, 0xE20746L); // draw bitmap to offscreen
00333     fl_end_offscreen(); // offscreen data is in tmp_id
00334     SelectObject(tempdc, (HGDIOBJ)tmp_id); // use offscreen data
00335     // draw it to printer context with background color as transparent
00336     fl_TransparentBlt(fl_gc, X,Y,W,H, tempdc, cx, cy, bm->w(), bm->h(), RGB(r, g, b) ); 
00337     fl_delete_offscreen(tmp_id);
00338   }
00339   else { // algorithm for bitmap output to display
00340     tempdc = CreateCompatibleDC(fl_gc);
00341     save = SaveDC(tempdc);
00342     SelectObject(tempdc, (HGDIOBJ)bm->id_);
00343     SelectObject(fl_gc, fl_brush());
00344     // secret bitblt code found in old MSWindows reference manual:
00345     BitBlt(fl_gc, X, Y, W, H, tempdc, cx, cy, 0xE20746L);
00346   }
00347   RestoreDC(tempdc, save);
00348   DeleteDC(tempdc);
00349 }  
00350 
00351 #else // Xlib
00352 void Fl_Xlib_Graphics_Driver::draw(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int cx, int cy) {
00353   int X, Y, W, H;
00354   if (!bm->array) {
00355     bm->draw_empty(XP, YP);
00356     return;
00357   }
00358   if (start(bm, XP, YP, WP, HP, bm->w(), bm->h(), cx, cy, X, Y, W, H)) {
00359     return;
00360   }
00361   if (!bm->id_) bm->id_ = fl_create_bitmask(bm->w(), bm->h(), bm->array);
00362   
00363   XSetStipple(fl_display, fl_gc, bm->id_);
00364   int ox = X-cx; if (ox < 0) ox += bm->w();
00365   int oy = Y-cy; if (oy < 0) oy += bm->h();
00366   XSetTSOrigin(fl_display, fl_gc, ox, oy);
00367   XSetFillStyle(fl_display, fl_gc, FillStippled);
00368   XFillRectangle(fl_display, fl_window, fl_gc, X, Y, W, H);
00369   XSetFillStyle(fl_display, fl_gc, FillSolid);
00370 }
00371 #endif
00372 
00377 Fl_Bitmap::~Fl_Bitmap() {
00378   uncache();
00379   if (alloc_array) delete[] (uchar *)array;
00380 }
00381 
00382 void Fl_Bitmap::uncache() {
00383   if (id_) {
00384 #ifdef __APPLE_QUARTZ__
00385     fl_delete_bitmask((Fl_Bitmask)id_);
00386 #else
00387     fl_delete_bitmask((Fl_Offscreen)id_);
00388 #endif
00389     id_ = 0;
00390   }
00391 }
00392 
00393 void Fl_Bitmap::label(Fl_Widget* widget) {
00394   widget->image(this);
00395 }
00396 
00397 void Fl_Bitmap::label(Fl_Menu_Item* m) {
00398   Fl::set_labeltype(_FL_IMAGE_LABEL, labeltype, measure);
00399   m->label(_FL_IMAGE_LABEL, (const char*)this);
00400 }
00401 
00402 Fl_Image *Fl_Bitmap::copy(int W, int H) {
00403   Fl_Bitmap     *new_image;     // New RGB image
00404   uchar         *new_array;     // New array for image data
00405 
00406   // Optimize the simple copy where the width and height are the same...
00407   if (W == w() && H == h()) {
00408     new_array = new uchar [H * ((W + 7) / 8)];
00409     memcpy(new_array, array, H * ((W + 7) / 8));
00410 
00411     new_image = new Fl_Bitmap(new_array, W, H);
00412     new_image->alloc_array = 1;
00413 
00414     return new_image;
00415   }
00416   if (W <= 0 || H <= 0) return 0;
00417 
00418   // OK, need to resize the image data; allocate memory and 
00419   uchar         *new_ptr,       // Pointer into new array
00420                 new_bit,        // Bit for new array
00421                 old_bit;        // Bit for old array
00422   const uchar   *old_ptr;       // Pointer into old array
00423   int           sx, sy,         // Source coordinates
00424                 dx, dy,         // Destination coordinates
00425                 xerr, yerr,     // X & Y errors
00426                 xmod, ymod,     // X & Y moduli
00427                 xstep, ystep;   // X & Y step increments
00428 
00429 
00430   // Figure out Bresenheim step/modulus values...
00431   xmod   = w() % W;
00432   xstep  = w() / W;
00433   ymod   = h() % H;
00434   ystep  = h() / H;
00435 
00436   // Allocate memory for the new image...
00437   new_array = new uchar [H * ((W + 7) / 8)];
00438   new_image = new Fl_Bitmap(new_array, W, H);
00439   new_image->alloc_array = 1;
00440 
00441   memset(new_array, 0, H * ((W + 7) / 8));
00442 
00443   // Scale the image using a nearest-neighbor algorithm...
00444   for (dy = H, sy = 0, yerr = H, new_ptr = new_array; dy > 0; dy --) {
00445     for (dx = W, xerr = W, old_ptr = array + sy * ((w() + 7) / 8), sx = 0, new_bit = 1;
00446          dx > 0;
00447          dx --) {
00448       old_bit = (uchar)(1 << (sx & 7));
00449       if (old_ptr[sx / 8] & old_bit) *new_ptr |= new_bit;
00450 
00451       if (new_bit < 128) new_bit <<= 1;
00452       else {
00453         new_bit = 1;
00454         new_ptr ++;
00455       }
00456 
00457       sx   += xstep;
00458       xerr -= xmod;
00459 
00460       if (xerr <= 0) {
00461         xerr += W;
00462         sx ++;
00463       }
00464     }
00465 
00466     if (new_bit > 1) new_ptr ++;
00467 
00468     sy   += ystep;
00469     yerr -= ymod;
00470     if (yerr <= 0) {
00471       yerr += H;
00472       sy ++;
00473     }
00474   }
00475 
00476   return new_image;
00477 }
00478 
00479 
00480 //
00481 // End of "$Id: Fl_Bitmap.cxx 8190 2011-01-05 10:21:45Z manolo $".
00482 //