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