Fl_Bitmap.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. //
  2. // "$Id: Fl_Bitmap.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Bitmap drawing routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. /** \fn Fl_Bitmap::Fl_Bitmap(const char *array, int W, int H)
  28. The constructors create a new bitmap from the specified bitmap data.*/
  29. /** \fn Fl_Bitmap::Fl_Bitmap(const unsigned char *array, int W, int H)
  30. The constructors create a new bitmap from the specified bitmap data.*/
  31. #include <FL/Fl.H>
  32. #include <FL/x.H>
  33. #include <FL/fl_draw.H>
  34. #include <FL/Fl_Widget.H>
  35. #include <FL/Fl_Menu_Item.H>
  36. #include <FL/Fl_Bitmap.H>
  37. #include <FL/Fl_Printer.H>
  38. #include "flstring.h"
  39. #if defined(__APPLE_QUARTZ__)
  40. Fl_Bitmask fl_create_bitmask(int w, int h, const uchar *array) {
  41. static uchar reverse[16] = /* Bit reversal lookup table */
  42. { 0x00, 0x88, 0x44, 0xcc, 0x22, 0xaa, 0x66, 0xee,
  43. 0x11, 0x99, 0x55, 0xdd, 0x33, 0xbb, 0x77, 0xff };
  44. int rowBytes = (w+7)>>3 ;
  45. uchar *bmask = (uchar*)malloc(rowBytes*h), *dst = bmask;
  46. const uchar *src = array;
  47. for ( int i=rowBytes*h; i>0; i--,src++ ) {
  48. *dst++ = ((reverse[*src & 0x0f] & 0xf0) | (reverse[(*src >> 4) & 0x0f] & 0x0f))^0xff;
  49. }
  50. CGDataProviderRef srcp = CGDataProviderCreateWithData( 0L, bmask, rowBytes*h, 0L);
  51. CGImageRef id_ = CGImageMaskCreate( w, h, 1, 1, rowBytes, srcp, 0L, false);
  52. CGDataProviderRelease(srcp);
  53. return (Fl_Bitmask)id_;
  54. }
  55. void fl_delete_bitmask(Fl_Bitmask bm) {
  56. if (bm) CGImageRelease((CGImageRef)bm);
  57. }
  58. #elif defined(WIN32) // Windows bitmask functions...
  59. // 'fl_create_bitmap()' - Create a 1-bit bitmap for drawing...
  60. static Fl_Bitmask fl_create_bitmap(int w, int h, const uchar *data) {
  61. // we need to pad the lines out to words & swap the bits
  62. // in each byte.
  63. int w1 = (w+7)/8;
  64. int w2 = ((w+15)/16)*2;
  65. uchar* newarray = new uchar[w2*h];
  66. const uchar* src = data;
  67. uchar* dest = newarray;
  68. Fl_Bitmask bm;
  69. static uchar reverse[16] = /* Bit reversal lookup table */
  70. { 0x00, 0x88, 0x44, 0xcc, 0x22, 0xaa, 0x66, 0xee,
  71. 0x11, 0x99, 0x55, 0xdd, 0x33, 0xbb, 0x77, 0xff };
  72. for (int y=0; y < h; y++) {
  73. for (int n = 0; n < w1; n++, src++)
  74. *dest++ = (uchar)((reverse[*src & 0x0f] & 0xf0) |
  75. (reverse[(*src >> 4) & 0x0f] & 0x0f));
  76. dest += w2-w1;
  77. }
  78. bm = CreateBitmap(w, h, 1, 1, newarray);
  79. delete[] newarray;
  80. return bm;
  81. }
  82. // 'fl_create_bitmask()' - Create an N-bit bitmap for masking...
  83. Fl_Bitmask fl_create_bitmask(int w, int h, const uchar *data) {
  84. // this won't work when the user changes display mode during run or
  85. // has two screens with differnet depths
  86. Fl_Bitmask bm;
  87. static uchar hiNibble[16] =
  88. { 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  89. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0 };
  90. static uchar loNibble[16] =
  91. { 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
  92. 0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f };
  93. int np = GetDeviceCaps(fl_gc, PLANES); //: was always one on sample machines
  94. int bpp = GetDeviceCaps(fl_gc, BITSPIXEL);//: 1,4,8,16,24,32 and more odd stuff?
  95. int Bpr = (bpp*w+7)/8; //: bytes per row
  96. int pad = Bpr&1, w1 = (w+7)/8, shr = ((w-1)&7)+1;
  97. if (bpp==4) shr = (shr+1)/2;
  98. uchar *newarray = new uchar[(Bpr+pad)*h];
  99. uchar *dst = newarray;
  100. const uchar *src = data;
  101. for (int i=0; i<h; i++) {
  102. // This is slooow, but we do it only once per pixmap
  103. for (int j=w1; j>0; j--) {
  104. uchar b = *src++;
  105. if (bpp==1) {
  106. *dst++ = (uchar)( hiNibble[b&15] ) | ( loNibble[(b>>4)&15] );
  107. } else if (bpp==4) {
  108. for (int k=(j==1)?shr:4; k>0; k--) {
  109. *dst++ = (uchar)("\377\360\017\000"[b&3]);
  110. b = b >> 2;
  111. }
  112. } else {
  113. for (int k=(j==1)?shr:8; k>0; k--) {
  114. if (b&1) {
  115. *dst++=0;
  116. if (bpp>8) *dst++=0;
  117. if (bpp>16) *dst++=0;
  118. if (bpp>24) *dst++=0;
  119. } else {
  120. *dst++=0xff;
  121. if (bpp>8) *dst++=0xff;
  122. if (bpp>16) *dst++=0xff;
  123. if (bpp>24) *dst++=0xff;
  124. }
  125. b = b >> 1;
  126. }
  127. }
  128. }
  129. dst += pad;
  130. }
  131. bm = CreateBitmap(w, h, np, bpp, newarray);
  132. delete[] newarray;
  133. return bm;
  134. }
  135. void fl_delete_bitmask(Fl_Bitmask bm) {
  136. DeleteObject((HGDIOBJ)bm);
  137. }
  138. #else // X11 bitmask functions
  139. Fl_Bitmask fl_create_bitmask(int w, int h, const uchar *data) {
  140. return XCreateBitmapFromData(fl_display, fl_window, (const char *)data,
  141. (w+7)&-8, h);
  142. }
  143. void fl_delete_bitmask(Fl_Bitmask bm) {
  144. fl_delete_offscreen((Fl_Offscreen)bm);
  145. }
  146. #endif // __APPLE__
  147. // Create a 1-bit mask used for alpha blending
  148. Fl_Bitmask fl_create_alphamask(int w, int h, int d, int ld, const uchar *array) {
  149. Fl_Bitmask bm;
  150. int bmw = (w + 7) / 8;
  151. uchar *bitmap = new uchar[bmw * h];
  152. uchar *bitptr, bit;
  153. const uchar *dataptr;
  154. int x, y;
  155. static uchar dither[16][16] = { // Simple 16x16 Floyd dither
  156. { 0, 128, 32, 160, 8, 136, 40, 168,
  157. 2, 130, 34, 162, 10, 138, 42, 170 },
  158. { 192, 64, 224, 96, 200, 72, 232, 104,
  159. 194, 66, 226, 98, 202, 74, 234, 106 },
  160. { 48, 176, 16, 144, 56, 184, 24, 152,
  161. 50, 178, 18, 146, 58, 186, 26, 154 },
  162. { 240, 112, 208, 80, 248, 120, 216, 88,
  163. 242, 114, 210, 82, 250, 122, 218, 90 },
  164. { 12, 140, 44, 172, 4, 132, 36, 164,
  165. 14, 142, 46, 174, 6, 134, 38, 166 },
  166. { 204, 76, 236, 108, 196, 68, 228, 100,
  167. 206, 78, 238, 110, 198, 70, 230, 102 },
  168. { 60, 188, 28, 156, 52, 180, 20, 148,
  169. 62, 190, 30, 158, 54, 182, 22, 150 },
  170. { 252, 124, 220, 92, 244, 116, 212, 84,
  171. 254, 126, 222, 94, 246, 118, 214, 86 },
  172. { 3, 131, 35, 163, 11, 139, 43, 171,
  173. 1, 129, 33, 161, 9, 137, 41, 169 },
  174. { 195, 67, 227, 99, 203, 75, 235, 107,
  175. 193, 65, 225, 97, 201, 73, 233, 105 },
  176. { 51, 179, 19, 147, 59, 187, 27, 155,
  177. 49, 177, 17, 145, 57, 185, 25, 153 },
  178. { 243, 115, 211, 83, 251, 123, 219, 91,
  179. 241, 113, 209, 81, 249, 121, 217, 89 },
  180. { 15, 143, 47, 175, 7, 135, 39, 167,
  181. 13, 141, 45, 173, 5, 133, 37, 165 },
  182. { 207, 79, 239, 111, 199, 71, 231, 103,
  183. 205, 77, 237, 109, 197, 69, 229, 101 },
  184. { 63, 191, 31, 159, 55, 183, 23, 151,
  185. 61, 189, 29, 157, 53, 181, 21, 149 },
  186. { 254, 127, 223, 95, 247, 119, 215, 87,
  187. 253, 125, 221, 93, 245, 117, 213, 85 }
  188. };
  189. // Generate a 1-bit "screen door" alpha mask; not always pretty, but
  190. // definitely fast... In the future we may be able to support things
  191. // like the RENDER extension in XFree86, when available, to provide
  192. // true RGBA-blended rendering. See:
  193. //
  194. // http://www.xfree86.org/~keithp/render/protocol.html
  195. //
  196. // for more info on XRender...
  197. //
  198. // MacOS already provides alpha blending support and has its own
  199. // fl_create_alphamask() function...
  200. memset(bitmap, 0, bmw * h);
  201. for (dataptr = array + d - 1, y = 0; y < h; y ++, dataptr += ld)
  202. for (bitptr = bitmap + y * bmw, bit = 1, x = 0; x < w; x ++, dataptr += d) {
  203. if (*dataptr > dither[x & 15][y & 15])
  204. *bitptr |= bit;
  205. if (bit < 128) bit <<= 1;
  206. else {
  207. bit = 1;
  208. bitptr ++;
  209. }
  210. }
  211. bm = fl_create_bitmask(w, h, bitmap);
  212. delete[] bitmap;
  213. return (bm);
  214. }
  215. void Fl_Bitmap::draw(int XP, int YP, int WP, int HP, int cx, int cy) {
  216. fl_graphics_driver->draw(this, XP, YP, WP, HP, cx, cy);
  217. }
  218. static int start(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int w, int h, int &cx, int &cy,
  219. int &X, int &Y, int &W, int &H)
  220. {
  221. // account for current clip region (faster on Irix):
  222. fl_clip_box(XP,YP,WP,HP,X,Y,W,H);
  223. cx += X-XP; cy += Y-YP;
  224. // clip the box down to the size of image, quit if empty:
  225. if (cx < 0) {W += cx; X -= cx; cx = 0;}
  226. if (cx+W > w) W = w-cx;
  227. if (W <= 0) return 1;
  228. if (cy < 0) {H += cy; Y -= cy; cy = 0;}
  229. if (cy+H > h) H = h-cy;
  230. if (H <= 0) return 1;
  231. return 0;
  232. }
  233. #ifdef __APPLE__
  234. void Fl_Quartz_Graphics_Driver::draw(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int cx, int cy) {
  235. int X, Y, W, H;
  236. if (!bm->array) {
  237. bm->draw_empty(XP, YP);
  238. return;
  239. }
  240. if (start(bm, XP, YP, WP, HP, bm->w(), bm->h(), cx, cy, X, Y, W, H)) {
  241. return;
  242. }
  243. if (!bm->id_) bm->id_ = fl_create_bitmask(bm->w(), bm->h(), bm->array);
  244. if (bm->id_ && fl_gc) {
  245. CGRect rect = { { X, Y }, { W, H } };
  246. Fl_X::q_begin_image(rect, cx, cy, bm->w(), bm->h());
  247. CGContextDrawImage(fl_gc, rect, (CGImageRef)bm->id_);
  248. Fl_X::q_end_image();
  249. }
  250. }
  251. #elif defined(WIN32)
  252. void Fl_GDI_Graphics_Driver::draw(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int cx, int cy) {
  253. int X, Y, W, H;
  254. if (!bm->array) {
  255. bm->draw_empty(XP, YP);
  256. return;
  257. }
  258. if (start(bm, XP, YP, WP, HP, bm->w(), bm->h(), cx, cy, X, Y, W, H)) {
  259. return;
  260. }
  261. if (!bm->id_) bm->id_ = fl_create_bitmap(bm->w(), bm->h(), bm->array);
  262. typedef BOOL (WINAPI* fl_transp_func) (HDC,int,int,int,int,HDC,int,int,int,int,UINT);
  263. static fl_transp_func fl_TransparentBlt;
  264. HDC tempdc;
  265. int save;
  266. BOOL use_print_algo = false;
  267. if (fl_surface->type() == Fl_Printer::device_type) {
  268. static HMODULE hMod = NULL;
  269. if (!hMod) {
  270. hMod = LoadLibrary("MSIMG32.DLL");
  271. if (hMod) fl_TransparentBlt = (fl_transp_func)GetProcAddress(hMod, "TransparentBlt");
  272. }
  273. if (fl_TransparentBlt) use_print_algo = true;
  274. }
  275. if (use_print_algo) { // algorithm for bitmap output to Fl_GDI_Printer
  276. Fl_Offscreen tmp_id = fl_create_offscreen(W, H);
  277. fl_begin_offscreen(tmp_id);
  278. Fl_Color save_c = fl_color(); // save bitmap's desired color
  279. uchar r, g, b;
  280. Fl::get_color(save_c, r, g, b);
  281. r = 255-r;
  282. g = 255-g;
  283. b = 255-b;
  284. Fl_Color background = fl_rgb_color(r, g, b); // a color very different from the bitmap's
  285. fl_color(background);
  286. fl_rectf(0,0,W,H); // use this color as offscreen background
  287. fl_color(save_c); // back to bitmap's color
  288. tempdc = CreateCompatibleDC(fl_gc);
  289. save = SaveDC(tempdc);
  290. SelectObject(tempdc, (HGDIOBJ)bm->id_);
  291. SelectObject(fl_gc, fl_brush()); // use bitmap's desired color
  292. BitBlt(fl_gc, 0, 0, W, H, tempdc, 0, 0, 0xE20746L); // draw bitmap to offscreen
  293. fl_end_offscreen(); // offscreen data is in tmp_id
  294. SelectObject(tempdc, (HGDIOBJ)tmp_id); // use offscreen data
  295. // draw it to printer context with background color as transparent
  296. fl_TransparentBlt(fl_gc, X,Y,W,H, tempdc, cx, cy, bm->w(), bm->h(), RGB(r, g, b) );
  297. fl_delete_offscreen(tmp_id);
  298. }
  299. else { // algorithm for bitmap output to display
  300. tempdc = CreateCompatibleDC(fl_gc);
  301. save = SaveDC(tempdc);
  302. SelectObject(tempdc, (HGDIOBJ)bm->id_);
  303. SelectObject(fl_gc, fl_brush());
  304. // secret bitblt code found in old MSWindows reference manual:
  305. BitBlt(fl_gc, X, Y, W, H, tempdc, cx, cy, 0xE20746L);
  306. }
  307. RestoreDC(tempdc, save);
  308. DeleteDC(tempdc);
  309. }
  310. #else // Xlib
  311. void Fl_Xlib_Graphics_Driver::draw(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int cx, int cy) {
  312. int X, Y, W, H;
  313. if (!bm->array) {
  314. bm->draw_empty(XP, YP);
  315. return;
  316. }
  317. if (start(bm, XP, YP, WP, HP, bm->w(), bm->h(), cx, cy, X, Y, W, H)) {
  318. return;
  319. }
  320. if (!bm->id_) bm->id_ = fl_create_bitmask(bm->w(), bm->h(), bm->array);
  321. XSetStipple(fl_display, fl_gc, bm->id_);
  322. int ox = X-cx; if (ox < 0) ox += bm->w();
  323. int oy = Y-cy; if (oy < 0) oy += bm->h();
  324. XSetTSOrigin(fl_display, fl_gc, ox, oy);
  325. XSetFillStyle(fl_display, fl_gc, FillStippled);
  326. XFillRectangle(fl_display, fl_window, fl_gc, X, Y, W, H);
  327. XSetFillStyle(fl_display, fl_gc, FillSolid);
  328. }
  329. #endif
  330. /**
  331. The destructor free all memory and server resources that are used by
  332. the bitmap.
  333. */
  334. Fl_Bitmap::~Fl_Bitmap() {
  335. uncache();
  336. if (alloc_array) delete[] (uchar *)array;
  337. }
  338. void Fl_Bitmap::uncache() {
  339. if (id_) {
  340. #ifdef __APPLE_QUARTZ__
  341. fl_delete_bitmask((Fl_Bitmask)id_);
  342. #else
  343. fl_delete_bitmask((Fl_Offscreen)id_);
  344. #endif
  345. id_ = 0;
  346. }
  347. }
  348. void Fl_Bitmap::label(Fl_Widget* widget) {
  349. widget->image(this);
  350. }
  351. void Fl_Bitmap::label(Fl_Menu_Item* m) {
  352. Fl::set_labeltype(_FL_IMAGE_LABEL, labeltype, measure);
  353. m->label(_FL_IMAGE_LABEL, (const char*)this);
  354. }
  355. Fl_Image *Fl_Bitmap::copy(int W, int H) {
  356. Fl_Bitmap *new_image; // New RGB image
  357. uchar *new_array; // New array for image data
  358. // Optimize the simple copy where the width and height are the same...
  359. if (W == w() && H == h()) {
  360. new_array = new uchar [H * ((W + 7) / 8)];
  361. memcpy(new_array, array, H * ((W + 7) / 8));
  362. new_image = new Fl_Bitmap(new_array, W, H);
  363. new_image->alloc_array = 1;
  364. return new_image;
  365. }
  366. if (W <= 0 || H <= 0) return 0;
  367. // OK, need to resize the image data; allocate memory and
  368. uchar *new_ptr, // Pointer into new array
  369. new_bit, // Bit for new array
  370. old_bit; // Bit for old array
  371. const uchar *old_ptr; // Pointer into old array
  372. int sx, sy, // Source coordinates
  373. dx, dy, // Destination coordinates
  374. xerr, yerr, // X & Y errors
  375. xmod, ymod, // X & Y moduli
  376. xstep, ystep; // X & Y step increments
  377. // Figure out Bresenheim step/modulus values...
  378. xmod = w() % W;
  379. xstep = w() / W;
  380. ymod = h() % H;
  381. ystep = h() / H;
  382. // Allocate memory for the new image...
  383. new_array = new uchar [H * ((W + 7) / 8)];
  384. new_image = new Fl_Bitmap(new_array, W, H);
  385. new_image->alloc_array = 1;
  386. memset(new_array, 0, H * ((W + 7) / 8));
  387. // Scale the image using a nearest-neighbor algorithm...
  388. for (dy = H, sy = 0, yerr = H, new_ptr = new_array; dy > 0; dy --) {
  389. for (dx = W, xerr = W, old_ptr = array + sy * ((w() + 7) / 8), sx = 0, new_bit = 1;
  390. dx > 0;
  391. dx --) {
  392. old_bit = (uchar)(1 << (sx & 7));
  393. if (old_ptr[sx / 8] & old_bit) *new_ptr |= new_bit;
  394. if (new_bit < 128) new_bit <<= 1;
  395. else {
  396. new_bit = 1;
  397. new_ptr ++;
  398. }
  399. sx += xstep;
  400. xerr -= xmod;
  401. if (xerr <= 0) {
  402. xerr += W;
  403. sx ++;
  404. }
  405. }
  406. if (new_bit > 1) new_ptr ++;
  407. sy += ystep;
  408. yerr -= ymod;
  409. if (yerr <= 0) {
  410. yerr += H;
  411. sy ++;
  412. }
  413. }
  414. return new_image;
  415. }
  416. //
  417. // End of "$Id: Fl_Bitmap.cxx 7903 2010-11-28 21:06:39Z matt $".
  418. //