source: rtems-graphics-toolkit/fltk-1.1.10/src/fl_read_image_mac.cxx @ 513eea1

Last change on this file since 513eea1 was 513eea1, checked in by Joel Sherrill <joel.sherrill@…>, on 01/09/10 at 22:43:24

2010-01-08 Joel Sherrill <joel.sherrill@…>

fltk 1.1.10. imported

  • ORIGIN: Updated.
  • Property mode set to 100644
File size: 4.0 KB
Line 
1//
2// "$Id$"
3//
4// WIN32 image reading routines for the Fast Light Tool Kit (FLTK).
5//
6// Copyright 1998-2006 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
28#include <config.h>
29
30// warning: this function is only implemented in Quickdraw. The function
31//          below may not work If FLTK is compiled with Quartz enabled
32
33//
34// 'fl_read_image()' - Read an image from the current window.
35//
36
37uchar *                         // O - Pixel buffer or NULL if failed
38fl_read_image(uchar *p,         // I - Pixel buffer or NULL to allocate
39              int   x,          // I - Left position
40              int   y,          // I - Top position
41              int   w,          // I - Width of area to read
42              int   h,          // I - Height of area to read
43              int   alpha) {    // I - Alpha value for image (0 for none)
44  Rect          src,            // Source rectangle
45                dst;            // Destination rectangle
46  GWorldPtr     osbuffer;       // Temporary off-screen buffer for copy
47  GrafPtr       srcPort;        // Source port
48  RGBColor      rgb;            // RGB colors for copy mask...
49  PixMapHandle  pm;             // Pixmap handle for off-screen buffer
50  uchar         *base,          // Base address of off-screen buffer
51                *psrc,          // Pointer into off-screen buffer
52                *pdst;          // Pointer into pixel buffer
53  int           idx, idy;       // Current X & Y in image
54  int           d;              // Depth of image
55  int           rowBytes;       // Number of bytes per row...
56
57  // Set the source and destination rectangles...
58  src.top    = y;
59  src.left   = x;
60  src.bottom = y + h;
61  src.right  = x + w;
62
63  dst.top    = 0;
64  dst.left   = 0;
65  dst.bottom = h;
66  dst.right  = w;
67
68  // Get an off-screen buffer for copying the image...
69  QDErr err = NewGWorld(&osbuffer, 0, &dst, 0L, 0L, 0);
70  if (!osbuffer) return 0;
71  if (err!=noErr) {
72    DisposeGWorld(osbuffer);
73    return 0;
74  }
75
76  // Get the source port...
77  GetPort(&srcPort);
78
79  // Set the RGB copy mask via the foreground/background colors...
80  rgb.red   = 0xffff;
81  rgb.green = 0xffff;
82  rgb.blue  = 0xffff;
83  RGBBackColor(&rgb);
84
85  rgb.red   = 0x0000;
86  rgb.green = 0x0000;
87  rgb.blue  = 0x0000;
88  RGBForeColor(&rgb);
89
90  // Copy the screen image to the off-screen buffer...
91  CopyBits(GetPortBitMapForCopyBits(srcPort),
92           GetPortBitMapForCopyBits(osbuffer), &src, &dst, srcCopy, 0L);
93
94  // Allocate the image data array as needed...
95  d = alpha ? 4 : 3;
96
97  if (!p) p = new uchar[w * h * d];
98
99  // Initialize the default colors/alpha in the whole image...
100  memset(p, alpha, w * h * d);
101
102  // Set the correct port for the off-screen buffer and lock the buffer
103  SetGWorld(osbuffer, 0);
104
105  pm = GetGWorldPixMap(osbuffer);
106  LockPixels(pm);
107
108  base     = (uchar *)GetPixBaseAddr(pm);
109  rowBytes = (*pm)->rowBytes & 0x3fff;
110
111  // Copy the image from the off-screen buffer to the memory buffer.
112  for (idy = 0, pdst = p; idy < h; idy ++)
113#ifdef __i386__
114    for (idx = 0, psrc = base + idy * rowBytes; idx < w; idx ++, psrc += 4, pdst += d) {
115      pdst[0] = psrc[2];
116      pdst[1] = psrc[1];
117      pdst[2] = psrc[0];
118    }
119#else
120    for (idx = 0, psrc = base + idy * rowBytes + 1; idx < w; idx ++, psrc += 4, pdst += d) {
121      pdst[0] = psrc[0];
122      pdst[1] = psrc[1];
123      pdst[2] = psrc[2];
124    }
125#endif // __i386__
126  // Unlock and delete the off-screen buffer, then return...
127  UnlockPixels(pm);
128  DisposeGWorld(osbuffer);
129
130  SetPort(srcPort);
131  return p;
132}
133
134
135//
136// End of "$Id$".
137//
Note: See TracBrowser for help on using the repository browser.