You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.9KB

  1. //
  2. // "$Id: fl_read_image_mac.cxx 8362 2011-02-02 18:39:34Z manolo $"
  3. //
  4. // WIN32 image reading 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. #include <config.h>
  28. //
  29. // 'fl_read_image()' - Read an image from the current window or off-screen buffer.
  30. //
  31. uchar * // O - Pixel buffer or NULL if failed
  32. fl_read_image(uchar *p, // I - Pixel buffer or NULL to allocate
  33. int x, // I - Left position
  34. int y, // I - Top position
  35. int w, // I - Width of area to read
  36. int h, // I - Height of area to read
  37. int alpha) { // I - Alpha value for image (0 for none)
  38. uchar *base;
  39. int rowBytes, delta;
  40. if(fl_window == NULL) { // reading from an offscreen buffer
  41. CGContextRef src = (CGContextRef)fl_gc; // get bitmap context
  42. base = (uchar *)CGBitmapContextGetData(src); // get data
  43. if(!base) return NULL;
  44. int sw = CGBitmapContextGetWidth(src);
  45. int sh = CGBitmapContextGetHeight(src);
  46. rowBytes = CGBitmapContextGetBytesPerRow(src);
  47. delta = CGBitmapContextGetBitsPerPixel(src)/8;
  48. if( (sw - x < w) || (sh - y < h) ) return NULL;
  49. }
  50. else { // reading from current window
  51. Fl_Window *window = Fl_Window::current();
  52. while(window->window()) window = window->window();
  53. base = Fl_X::bitmap_from_window_rect(window,x,y,w,h,&delta);
  54. rowBytes = delta*w;
  55. x = y = 0;
  56. }
  57. // Allocate the image data array as needed...
  58. int d = alpha ? 4 : 3;
  59. if (!p) p = new uchar[w * h * d];
  60. // Initialize the default colors/alpha in the whole image...
  61. memset(p, alpha, w * h * d);
  62. // Copy the image from the off-screen buffer to the memory buffer.
  63. int idx, idy; // Current X & Y in image
  64. uchar *pdst, *psrc;
  65. for (idy = y, pdst = p; idy < h + y; idy ++) {
  66. for (idx = 0, psrc = base + idy * rowBytes + x * delta; idx < w; idx ++, psrc += delta, pdst += d) {
  67. pdst[0] = psrc[0]; // R
  68. pdst[1] = psrc[1]; // G
  69. pdst[2] = psrc[2]; // B
  70. }
  71. }
  72. if(fl_window != NULL) delete[] base;
  73. return p;
  74. }
  75. //
  76. // End of "$Id: fl_read_image_mac.cxx 8362 2011-02-02 18:39:34Z manolo $".
  77. //