PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/fltk/src/Fl_Image.cxx

http://luafltk.googlecode.com/
C++ | 559 lines | 360 code | 81 blank | 118 comment | 106 complexity | 92232ec17312e3777428af1243d0d3f6 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, 0BSD
  1. //
  2. // "$Id: Fl_Image.cxx 7522 2010-04-18 06:57:37Z manolo $"
  3. //
  4. // Image drawing code for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2009 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. #include <FL/Fl.H>
  28. #include <FL/fl_draw.H>
  29. #include <FL/x.H>
  30. #include <FL/Fl_Widget.H>
  31. #include <FL/Fl_Menu_Item.H>
  32. #include <FL/Fl_Image.H>
  33. #include "flstring.h"
  34. #ifdef WIN32
  35. void fl_release_dc(HWND, HDC); // from Fl_win32.cxx
  36. #endif
  37. void fl_restore_clip(); // from fl_rect.cxx
  38. //
  39. // Base image class...
  40. //
  41. /**
  42. The destructor is a virtual method that frees all memory used
  43. by the image.
  44. */
  45. Fl_Image::~Fl_Image() {
  46. }
  47. /**
  48. If the image has been cached for display, delete the cache
  49. data. This allows you to change the data used for the image and
  50. then redraw it without recreating an image object.
  51. */
  52. void Fl_Image::uncache() {
  53. }
  54. void Fl_Image::draw(int XP, int YP, int, int, int, int) {
  55. draw_empty(XP, YP);
  56. }
  57. /**
  58. The protected method draw_empty() draws a box with
  59. an X in it. It can be used to draw any image that lacks image
  60. data.
  61. */
  62. void Fl_Image::draw_empty(int X, int Y) {
  63. if (w() > 0 && h() > 0) {
  64. fl_color(FL_FOREGROUND_COLOR);
  65. fl_rect(X, Y, w(), h());
  66. fl_line(X, Y, X + w() - 1, Y + h() - 1);
  67. fl_line(X, Y + h() - 1, X + w() - 1, Y);
  68. }
  69. }
  70. /**
  71. The copy() method creates a copy of the specified
  72. image. If the width and height are provided, the image is
  73. resized to the specified size. The image should be deleted (or in
  74. the case of Fl_Shared_Image, released) when you are done
  75. with it.
  76. */
  77. Fl_Image *Fl_Image::copy(int W, int H) {
  78. return new Fl_Image(W, H, d());
  79. }
  80. /**
  81. The color_average() method averages the colors in
  82. the image with the FLTK color value c. The i
  83. argument specifies the amount of the original image to combine
  84. with the color, so a value of 1.0 results in no color blend, and
  85. a value of 0.0 results in a constant image of the specified
  86. color. <I>The original image data is not altered by this
  87. method.</I>
  88. */
  89. void Fl_Image::color_average(Fl_Color, float) {
  90. }
  91. /**
  92. The desaturate() method converts an image to
  93. grayscale. If the image contains an alpha channel (depth = 4),
  94. the alpha channel is preserved. <I>This method does not alter
  95. the original image data.</I>
  96. */
  97. void Fl_Image::desaturate() {
  98. }
  99. /**
  100. The label() methods are an obsolete way to set the
  101. image attribute of a widget or menu item. Use the
  102. image() or deimage() methods of the
  103. Fl_Widget and Fl_Menu_Item classes
  104. instead.
  105. */
  106. void Fl_Image::label(Fl_Widget* widget) {
  107. widget->image(this);
  108. }
  109. /**
  110. The label() methods are an obsolete way to set the
  111. image attribute of a widget or menu item. Use the
  112. image() or deimage() methods of the
  113. Fl_Widget and Fl_Menu_Item classes
  114. instead.
  115. */
  116. void Fl_Image::label(Fl_Menu_Item* m) {
  117. Fl::set_labeltype(_FL_IMAGE_LABEL, labeltype, measure);
  118. m->label(_FL_IMAGE_LABEL, (const char*)this);
  119. }
  120. void
  121. Fl_Image::labeltype(const Fl_Label *lo, // I - Label
  122. int lx, // I - X position
  123. int ly, // I - Y position
  124. int lw, // I - Width of label
  125. int lh, // I - Height of label
  126. Fl_Align la) { // I - Alignment
  127. Fl_Image *img; // Image pointer
  128. int cx, cy; // Image position
  129. img = (Fl_Image *)(lo->value);
  130. if (la & FL_ALIGN_LEFT) cx = 0;
  131. else if (la & FL_ALIGN_RIGHT) cx = img->w() - lw;
  132. else cx = (img->w() - lw) / 2;
  133. if (la & FL_ALIGN_TOP) cy = 0;
  134. else if (la & FL_ALIGN_BOTTOM) cy = img->h() - lh;
  135. else cy = (img->h() - lh) / 2;
  136. fl_color((Fl_Color)lo->color);
  137. img->draw(lx, ly, lw, lh, cx, cy);
  138. }
  139. void
  140. Fl_Image::measure(const Fl_Label *lo, // I - Label
  141. int &lw, // O - Width of image
  142. int &lh) { // O - Height of image
  143. Fl_Image *img; // Image pointer
  144. img = (Fl_Image *)(lo->value);
  145. lw = img->w();
  146. lh = img->h();
  147. }
  148. //
  149. // RGB image class...
  150. //
  151. /** The destructor free all memory and server resources that are used by the image. */
  152. Fl_RGB_Image::~Fl_RGB_Image() {
  153. uncache();
  154. if (alloc_array) delete[] (uchar *)array;
  155. }
  156. void Fl_RGB_Image::uncache() {
  157. #ifdef __APPLE_QUARTZ__
  158. if (id_) {
  159. CGImageRelease((CGImageRef)id_);
  160. id_ = 0;
  161. }
  162. #else
  163. if (id_) {
  164. fl_delete_offscreen((Fl_Offscreen)id_);
  165. id_ = 0;
  166. }
  167. if (mask_) {
  168. fl_delete_bitmask((Fl_Bitmask)mask_);
  169. mask_ = 0;
  170. }
  171. #endif
  172. }
  173. Fl_Image *Fl_RGB_Image::copy(int W, int H) {
  174. Fl_RGB_Image *new_image; // New RGB image
  175. uchar *new_array; // New array for image data
  176. // Optimize the simple copy where the width and height are the same,
  177. // or when we are copying an empty image...
  178. if ((W == w() && H == h()) ||
  179. !w() || !h() || !d() || !array) {
  180. if (array) {
  181. // Make a copy of the image data and return a new Fl_RGB_Image...
  182. new_array = new uchar[w() * h() * d()];
  183. if (ld() && ld()!=w()*d()) {
  184. const uchar *src = array;
  185. uchar *dst = new_array;
  186. int dy, dh = h(), wd = w()*d(), wld = ld();
  187. for (dy=0; dy<dh; dy++) {
  188. memcpy(dst, src, wd);
  189. src += wld;
  190. dst += wd;
  191. }
  192. } else {
  193. memcpy(new_array, array, w() * h() * d());
  194. }
  195. new_image = new Fl_RGB_Image(new_array, w(), h(), d());
  196. new_image->alloc_array = 1;
  197. return new_image;
  198. } else return new Fl_RGB_Image(array, w(), h(), d(), ld());
  199. }
  200. if (W <= 0 || H <= 0) return 0;
  201. // OK, need to resize the image data; allocate memory and
  202. uchar *new_ptr; // Pointer into new array
  203. const uchar *old_ptr; // Pointer into old array
  204. int c, // Channel number
  205. sy, // Source coordinate
  206. dx, dy, // Destination coordinates
  207. xerr, yerr, // X & Y errors
  208. xmod, ymod, // X & Y moduli
  209. xstep, ystep, // X & Y step increments
  210. line_d; // stride from line to line
  211. // Figure out Bresenheim step/modulus values...
  212. xmod = w() % W;
  213. xstep = (w() / W) * d();
  214. ymod = h() % H;
  215. ystep = h() / H;
  216. line_d = ld() ? ld() : w() * d();
  217. // Allocate memory for the new image...
  218. new_array = new uchar [W * H * d()];
  219. new_image = new Fl_RGB_Image(new_array, W, H, d());
  220. new_image->alloc_array = 1;
  221. // Scale the image using a nearest-neighbor algorithm...
  222. for (dy = H, sy = 0, yerr = H, new_ptr = new_array; dy > 0; dy --) {
  223. for (dx = W, xerr = W, old_ptr = array + sy * line_d; dx > 0; dx --) {
  224. for (c = 0; c < d(); c ++) *new_ptr++ = old_ptr[c];
  225. old_ptr += xstep;
  226. xerr -= xmod;
  227. if (xerr <= 0) {
  228. xerr += W;
  229. old_ptr += d();
  230. }
  231. }
  232. sy += ystep;
  233. yerr -= ymod;
  234. if (yerr <= 0) {
  235. yerr += H;
  236. sy ++;
  237. }
  238. }
  239. return new_image;
  240. }
  241. void Fl_RGB_Image::color_average(Fl_Color c, float i) {
  242. // Don't average an empty image...
  243. if (!w() || !h() || !d() || !array) return;
  244. // Delete any existing pixmap/mask objects...
  245. uncache();
  246. // Allocate memory as needed...
  247. uchar *new_array,
  248. *new_ptr;
  249. if (!alloc_array) new_array = new uchar[h() * w() * d()];
  250. else new_array = (uchar *)array;
  251. // Get the color to blend with...
  252. uchar r, g, b;
  253. unsigned ia, ir, ig, ib;
  254. Fl::get_color(c, r, g, b);
  255. if (i < 0.0f) i = 0.0f;
  256. else if (i > 1.0f) i = 1.0f;
  257. ia = (unsigned)(256 * i);
  258. ir = r * (256 - ia);
  259. ig = g * (256 - ia);
  260. ib = b * (256 - ia);
  261. // Update the image data to do the blend...
  262. const uchar *old_ptr;
  263. int x, y;
  264. int line_i = ld() ? ld() - (w()*d()) : 0; // increment from line end to beginning of next line
  265. if (d() < 3) {
  266. ig = (r * 31 + g * 61 + b * 8) / 100 * (256 - ia);
  267. for (new_ptr = new_array, old_ptr = array, y = 0; y < h(); y ++, old_ptr += line_i)
  268. for (x = 0; x < w(); x ++) {
  269. *new_ptr++ = (*old_ptr++ * ia + ig) >> 8;
  270. if (d() > 1) *new_ptr++ = *old_ptr++;
  271. }
  272. } else {
  273. for (new_ptr = new_array, old_ptr = array, y = 0; y < h(); y ++, old_ptr += line_i)
  274. for (x = 0; x < w(); x ++) {
  275. *new_ptr++ = (*old_ptr++ * ia + ir) >> 8;
  276. *new_ptr++ = (*old_ptr++ * ia + ig) >> 8;
  277. *new_ptr++ = (*old_ptr++ * ia + ib) >> 8;
  278. if (d() > 3) *new_ptr++ = *old_ptr++;
  279. }
  280. }
  281. // Set the new pointers/values as needed...
  282. if (!alloc_array) {
  283. array = new_array;
  284. alloc_array = 1;
  285. ld(0);
  286. }
  287. }
  288. void Fl_RGB_Image::desaturate() {
  289. // Don't desaturate an empty image...
  290. if (!w() || !h() || !d() || !array) return;
  291. // Can only desaturate color images...
  292. if (d() < 3) return;
  293. // Delete any existing pixmap/mask objects...
  294. uncache();
  295. // Allocate memory for a grayscale image...
  296. uchar *new_array,
  297. *new_ptr;
  298. int new_d;
  299. new_d = d() - 2;
  300. new_array = new uchar[h() * w() * new_d];
  301. // Copy the image data, converting to grayscale...
  302. const uchar *old_ptr;
  303. int x, y;
  304. int line_i = ld() ? ld() - (w()*d()) : 0; // increment from line end to beginning of next line
  305. for (new_ptr = new_array, old_ptr = array, y = 0; y < h(); y ++, old_ptr += line_i)
  306. for (x = 0; x < w(); x ++, old_ptr += d()) {
  307. *new_ptr++ = (uchar)((31 * old_ptr[0] + 61 * old_ptr[1] + 8 * old_ptr[2]) / 100);
  308. if (d() > 3) *new_ptr++ = old_ptr[3];
  309. }
  310. // Free the old array as needed, and then set the new pointers/values...
  311. if (alloc_array) delete[] (uchar *)array;
  312. array = new_array;
  313. alloc_array = 1;
  314. ld(0);
  315. d(new_d);
  316. }
  317. #if !defined(WIN32) && !defined(__APPLE_QUARTZ__)
  318. // Composite an image with alpha on systems that don't have accelerated
  319. // alpha compositing...
  320. static void alpha_blend(Fl_RGB_Image *img, int X, int Y, int W, int H, int cx, int cy) {
  321. uchar *srcptr = (uchar*)img->array + img->d() * (img->w() * cy + cx);
  322. int srcskip = img->d() * (img->w() - W);
  323. uchar *dst = new uchar[W * H * 3];
  324. uchar *dstptr = dst;
  325. fl_read_image(dst, X, Y, W, H, 0);
  326. uchar srcr, srcg, srcb, srca;
  327. uchar dstr, dstg, dstb, dsta;
  328. if (img->d() == 2) {
  329. // Composite grayscale + alpha over RGB...
  330. // Composite RGBA over RGB...
  331. for (int y = H; y > 0; y--, srcptr+=srcskip)
  332. for (int x = W; x > 0; x--) {
  333. srcg = *srcptr++;
  334. srca = *srcptr++;
  335. dstr = dstptr[0];
  336. dstg = dstptr[1];
  337. dstb = dstptr[2];
  338. dsta = 255 - srca;
  339. *dstptr++ = (srcg * srca + dstr * dsta) >> 8;
  340. *dstptr++ = (srcg * srca + dstg * dsta) >> 8;
  341. *dstptr++ = (srcg * srca + dstb * dsta) >> 8;
  342. }
  343. } else {
  344. // Composite RGBA over RGB...
  345. for (int y = H; y > 0; y--, srcptr+=srcskip)
  346. for (int x = W; x > 0; x--) {
  347. srcr = *srcptr++;
  348. srcg = *srcptr++;
  349. srcb = *srcptr++;
  350. srca = *srcptr++;
  351. dstr = dstptr[0];
  352. dstg = dstptr[1];
  353. dstb = dstptr[2];
  354. dsta = 255 - srca;
  355. *dstptr++ = (srcr * srca + dstr * dsta) >> 8;
  356. *dstptr++ = (srcg * srca + dstg * dsta) >> 8;
  357. *dstptr++ = (srcb * srca + dstb * dsta) >> 8;
  358. }
  359. }
  360. fl_draw_image(dst, X, Y, W, H, 3, 0);
  361. delete[] dst;
  362. }
  363. #endif // !WIN32 && !__APPLE_QUARTZ__
  364. void Fl_RGB_Image::draw(int XP, int YP, int WP, int HP, int cx, int cy) {
  365. fl_device->draw(this, XP, YP, WP, HP, cx, cy);
  366. }
  367. void Fl_RGB_Image::generic_device_draw(int XP, int YP, int WP, int HP, int cx, int cy) {
  368. // Don't draw an empty image...
  369. if (!d() || !array) {
  370. draw_empty(XP, YP);
  371. return;
  372. }
  373. // account for current clip region (faster on Irix):
  374. int X,Y,W,H; fl_clip_box(XP,YP,WP,HP,X,Y,W,H);
  375. cx += X-XP; cy += Y-YP;
  376. // clip the box down to the size of image, quit if empty:
  377. if (cx < 0) {W += cx; X -= cx; cx = 0;}
  378. if (cx+W > w()) W = w()-cx;
  379. if (W <= 0) return;
  380. if (cy < 0) {H += cy; Y -= cy; cy = 0;}
  381. if (cy+H > h()) H = h()-cy;
  382. if (H <= 0) return;
  383. if (!id_) {
  384. #ifdef __APPLE_QUARTZ__
  385. CGColorSpaceRef lut = 0;
  386. if (d()<=2)
  387. lut = CGColorSpaceCreateDeviceGray();
  388. else
  389. lut = CGColorSpaceCreateDeviceRGB();
  390. CGDataProviderRef src = CGDataProviderCreateWithData( 0L, array, w()*h()*d(), 0L);
  391. id_ = CGImageCreate( w(), h(), 8, d()*8, ld()?ld():w()*d(),
  392. lut, (d()&1)?kCGImageAlphaNone:kCGImageAlphaLast,
  393. src, 0L, false, kCGRenderingIntentDefault);
  394. CGColorSpaceRelease(lut);
  395. CGDataProviderRelease(src);
  396. #elif defined(WIN32)
  397. id_ = fl_create_offscreen(w(), h());
  398. if ((d() == 2 || d() == 4) && fl_can_do_alpha_blending()) {
  399. fl_begin_offscreen((Fl_Offscreen)id_);
  400. fl_draw_image(array, 0, 0, w(), h(), d()|FL_IMAGE_WITH_ALPHA, ld());
  401. fl_end_offscreen();
  402. } else {
  403. fl_begin_offscreen((Fl_Offscreen)id_);
  404. fl_draw_image(array, 0, 0, w(), h(), d(), ld());
  405. fl_end_offscreen();
  406. if (d() == 2 || d() == 4) {
  407. mask_ = fl_create_alphamask(w(), h(), d(), ld(), array);
  408. }
  409. }
  410. #else
  411. if (d() == 1 || d() == 3) {
  412. id_ = fl_create_offscreen(w(), h());
  413. fl_begin_offscreen((Fl_Offscreen)id_);
  414. fl_draw_image(array, 0, 0, w(), h(), d(), ld());
  415. fl_end_offscreen();
  416. }
  417. #endif
  418. }
  419. #if defined(USE_X11)
  420. if (id_) {
  421. if (mask_) {
  422. // I can't figure out how to combine a mask with existing region,
  423. // so cut the image down to a clipped rectangle:
  424. int nx, ny; fl_clip_box(X,Y,W,H,nx,ny,W,H);
  425. cx += nx-X; X = nx;
  426. cy += ny-Y; Y = ny;
  427. // make X use the bitmap as a mask:
  428. XSetClipMask(fl_display, fl_gc, mask_);
  429. int ox = X-cx; if (ox < 0) ox += w();
  430. int oy = Y-cy; if (oy < 0) oy += h();
  431. XSetClipOrigin(fl_display, fl_gc, X-cx, Y-cy);
  432. }
  433. fl_copy_offscreen(X, Y, W, H, id_, cx, cy);
  434. if (mask_) {
  435. // put the old clip region back
  436. XSetClipOrigin(fl_display, fl_gc, 0, 0);
  437. fl_restore_clip();
  438. }
  439. } else {
  440. // Composite image with alpha manually each time...
  441. alpha_blend(this, X, Y, W, H, cx, cy);
  442. }
  443. #elif defined(WIN32)
  444. if (mask_) {
  445. HDC new_gc = CreateCompatibleDC(fl_gc);
  446. int save = SaveDC(new_gc);
  447. SelectObject(new_gc, (void*)mask_);
  448. BitBlt(fl_gc, X, Y, W, H, new_gc, cx, cy, SRCAND);
  449. SelectObject(new_gc, (void*)id_);
  450. BitBlt(fl_gc, X, Y, W, H, new_gc, cx, cy, SRCPAINT);
  451. RestoreDC(new_gc,save);
  452. DeleteDC(new_gc);
  453. } else if (d()==2 || d()==4) {
  454. fl_copy_offscreen_with_alpha(X, Y, W, H, (Fl_Offscreen)id_, cx, cy);
  455. } else {
  456. fl_copy_offscreen(X, Y, W, H, (Fl_Offscreen)id_, cx, cy);
  457. }
  458. #elif defined(__APPLE_QUARTZ__)
  459. if (id_ && fl_gc) {
  460. CGRect rect = { { X, Y }, { W, H } };
  461. Fl_X::q_begin_image(rect, cx, cy, w(), h());
  462. CGContextDrawImage(fl_gc, rect, (CGImageRef)id_);
  463. Fl_X::q_end_image();
  464. }
  465. #else
  466. # error unsupported platform
  467. #endif
  468. }
  469. void Fl_RGB_Image::label(Fl_Widget* widget) {
  470. widget->image(this);
  471. }
  472. void Fl_RGB_Image::label(Fl_Menu_Item* m) {
  473. Fl::set_labeltype(_FL_IMAGE_LABEL, labeltype, measure);
  474. m->label(_FL_IMAGE_LABEL, (const char*)this);
  475. }
  476. //
  477. // End of "$Id: Fl_Image.cxx 7522 2010-04-18 06:57:37Z manolo $".
  478. //