2 // "$Id: fl_read_image_win32.cxx 8595 2011-04-17 08:48:40Z manolo $"
4 // WIN32 image reading routines for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
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
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
29 // 'fl_read_image()' - Read an image from the current window.
32 uchar
* // O - Pixel buffer or NULL if failed
33 fl_read_image(uchar
*p
, // I - Pixel buffer or NULL to allocate
34 int X
, // I - Left position
35 int Y
, // I - Top position
36 int w
, // I - Width of area to read
37 int h
, // I - Height of area to read
38 int alpha
) { // I - Alpha value for image (0 for none)
40 int d
; // Depth of image
42 // Allocate the image data array as needed...
45 if (!p
) p
= new uchar
[w
* h
* d
];
47 // Initialize the default colors/alpha in the whole image...
48 memset(p
, alpha
, w
* h
* d
);
50 // Grab all of the pixels in the image...
52 // Assure that we are not trying to read non-existing data. If it is so, the
53 // function should still work, but the out-of-bounds part of the image is
54 // untouched (initialized with the alpha value or 0 (black), resp.).
56 int ww
= w
; // We need the original width for output data line size
58 int shift_x
= 0; // X target shift if X modified
59 int shift_y
= 0; // Y target shift if X modified
72 if (h
< 1 || w
< 1) return p
; // nothing to copy
74 int line_size
= ((3*w
+3)/4) * 4; // each line is aligned on a DWORD (4 bytes)
75 uchar
*dib
= new uchar
[line_size
*h
]; // create temporary buffer to read DIB
77 // fill in bitmap info for GetDIBits
80 bi
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
81 bi
.bmiHeader
.biWidth
= w
;
82 bi
.bmiHeader
.biHeight
= -h
; // negative => top-down DIB
83 bi
.bmiHeader
.biPlanes
= 1;
84 bi
.bmiHeader
.biBitCount
= 24; // 24 bits RGB
85 bi
.bmiHeader
.biCompression
= BI_RGB
;
86 bi
.bmiHeader
.biSizeImage
= 0;
87 bi
.bmiHeader
.biXPelsPerMeter
= 0;
88 bi
.bmiHeader
.biYPelsPerMeter
= 0;
89 bi
.bmiHeader
.biClrUsed
= 0;
90 bi
.bmiHeader
.biClrImportant
= 0;
92 // copy bitmap from original DC (Window, Fl_Offscreen, ...)
94 HDC hdc
= CreateCompatibleDC(fl_gc
);
95 HBITMAP hbm
= CreateCompatibleBitmap(fl_gc
,w
,h
);
97 int save_dc
= SaveDC(hdc
); // save context for cleanup
98 SelectObject(hdc
,hbm
); // select bitmap
99 BitBlt(hdc
,0,0,w
,h
,fl_gc
,X
,Y
,SRCCOPY
); // copy image section to DDB
101 // copy RGB image data to the allocated DIB
103 GetDIBits(hdc
, hbm
, 0, h
, dib
, (BITMAPINFO
*)&bi
, DIB_RGB_COLORS
);
105 // finally copy the image data to the user buffer
107 for (int j
= 0; j
<h
; j
++) {
108 const uchar
*src
= dib
+ j
* line_size
; // source line
109 uchar
*tg
= p
+ (j
+ shift_y
) * d
* ww
+ shift_x
* d
; // target line
110 for (int i
= 0; i
<w
; i
++) {
117 *tg
++ = alpha
; // alpha
121 // free used GDI and other structures
123 RestoreDC(hdc
,save_dc
); // reset DC
126 delete[] dib
; // delete DIB temporary buffer
132 // End of "$Id: fl_read_image_win32.cxx 8595 2011-04-17 08:48:40Z manolo $".