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_read_image_mac.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: fl_read_image_mac.cxx 8055 2010-12-18 22:31:01Z manolo $"
00003 //
00004 // WIN32 image reading 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 
00028 #include <config.h>
00029 
00030 //
00031 // 'fl_read_image()' - Read an image from the current window or off-screen buffer.
00032 //
00033 
00034 uchar *                         // O - Pixel buffer or NULL if failed
00035 fl_read_image(uchar *p,         // I - Pixel buffer or NULL to allocate
00036               int   x,          // I - Left position
00037               int   y,          // I - Top position
00038               int   w,          // I - Width of area to read
00039               int   h,          // I - Height of area to read
00040               int   alpha) {    // I - Alpha value for image (0 for none)
00041   uchar *base;
00042   int rowBytes, delta;
00043   if(fl_window == NULL) { // reading from an offscreen buffer
00044     CGContextRef src = (CGContextRef)fl_gc;   // get bitmap context
00045     base = (uchar *)CGBitmapContextGetData(src);  // get data
00046     if(!base) return NULL;
00047     int sw = CGBitmapContextGetWidth(src);
00048     int sh = CGBitmapContextGetHeight(src);
00049     rowBytes = CGBitmapContextGetBytesPerRow(src);
00050     delta = CGBitmapContextGetBitsPerPixel(src)/8;
00051     if( (sw - x < w) || (sh - y < h) )  return NULL;
00052     }
00053   else { // reading from current window
00054     Fl_Window *window = Fl_Window::current();
00055     while(window->window()) window = window->window();
00056     base = Fl_X::bitmap_from_window_rect(window,x,y,w,h,&delta);
00057     rowBytes = delta*w;
00058     x = y = 0;
00059     }
00060   // Allocate the image data array as needed...
00061   int d = alpha ? 4 : 3;
00062   if (!p) p = new uchar[w * h * d];
00063   // Initialize the default colors/alpha in the whole image...
00064   memset(p, alpha, w * h * d);
00065   // Copy the image from the off-screen buffer to the memory buffer.
00066   int           idx, idy;       // Current X & Y in image
00067   uchar *pdst, *psrc;
00068   for (idy = y, pdst = p; idy < h + y; idy ++) {
00069     for (idx = 0, psrc = base + idy * rowBytes + x * delta; idx < w; idx ++, psrc += delta, pdst += d) {
00070       pdst[0] = psrc[0];  // R
00071       pdst[1] = psrc[1];  // G
00072       pdst[2] = psrc[2];  // B
00073     }
00074   }
00075   if(fl_window != NULL) delete base;
00076   return p;
00077 }
00078 
00079 
00080 //
00081 // End of "$Id: fl_read_image_mac.cxx 8055 2010-12-18 22:31:01Z manolo $".
00082 //