|
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) ![]() |
00001 // 00002 // "$Id: fl_read_image_win32.cxx 8048 2010-12-16 20:23:57Z AlbrechtS $" 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 // 00029 // 'fl_read_image()' - Read an image from the current window. 00030 // 00031 00032 uchar * // O - Pixel buffer or NULL if failed 00033 fl_read_image(uchar *p, // I - Pixel buffer or NULL to allocate 00034 int X, // I - Left position 00035 int Y, // I - Top position 00036 int w, // I - Width of area to read 00037 int h, // I - Height of area to read 00038 int alpha) { // I - Alpha value for image (0 for none) 00039 00040 int d; // Depth of image 00041 00042 // Allocate the image data array as needed... 00043 d = alpha ? 4 : 3; 00044 00045 if (!p) p = new uchar[w * h * d]; 00046 00047 // Initialize the default colors/alpha in the whole image... 00048 memset(p, alpha, w * h * d); 00049 00050 // Grab all of the pixels in the image... 00051 00052 // Assure that we are not trying to read non-existing data. If it is so, the 00053 // function should still work, but the out-of-bounds part of the image is 00054 // untouched (initialized with the alpha value or 0 (black), resp.). 00055 00056 int ww = w; // We need the original width for output data line size 00057 00058 int shift_x = 0; // X target shift if X modified 00059 int shift_y = 0; // Y target shift if X modified 00060 00061 if (X < 0) { 00062 shift_x = -X; 00063 w += X; 00064 X = 0; 00065 } 00066 if (Y < 0) { 00067 shift_y = -Y; 00068 h += Y; 00069 Y = 0; 00070 } 00071 00072 if (h < 1 || w < 1) return p; // nothing to copy 00073 00074 int line_size = ((3*w+3)/4) * 4; // each line is aligned on a DWORD (4 bytes) 00075 uchar *dib = new uchar[line_size*h]; // create temporary buffer to read DIB 00076 00077 // fill in bitmap info for GetDIBits 00078 00079 BITMAPINFO bi; 00080 bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 00081 bi.bmiHeader.biWidth = w; 00082 bi.bmiHeader.biHeight = -h; // negative => top-down DIB 00083 bi.bmiHeader.biPlanes = 1; 00084 bi.bmiHeader.biBitCount = 24; // 24 bits RGB 00085 bi.bmiHeader.biCompression = BI_RGB; 00086 bi.bmiHeader.biSizeImage = 0; 00087 bi.bmiHeader.biXPelsPerMeter = 0; 00088 bi.bmiHeader.biYPelsPerMeter = 0; 00089 bi.bmiHeader.biClrUsed = 0; 00090 bi.bmiHeader.biClrImportant = 0; 00091 00092 // copy bitmap from original DC (Window, Fl_Offscreen, ...) 00093 00094 HDC hdc = CreateCompatibleDC(fl_gc); 00095 HBITMAP hbm = CreateCompatibleBitmap(fl_gc,w,h); 00096 00097 int save_dc = SaveDC(hdc); // save context for cleanup 00098 SelectObject(hdc,hbm); // select bitmap 00099 BitBlt(hdc,0,0,w,h,fl_gc,X,Y,SRCCOPY); // copy image section to DDB 00100 00101 // copy RGB image data to the allocated DIB 00102 00103 GetDIBits(hdc, hbm, 0, h, dib, (BITMAPINFO *)&bi, DIB_RGB_COLORS); 00104 00105 // finally copy the image data to the user buffer 00106 00107 for (int j = 0; j<h; j++) { 00108 const uchar *src = dib + j * line_size; // source line 00109 uchar *tg = p + (j + shift_y) * d * ww + shift_x * d; // target line 00110 for (int i = 0; i<w; i++) { 00111 uchar b = *src++; 00112 uchar g = *src++; 00113 *tg++ = *src++; // R 00114 *tg++ = g; // G 00115 *tg++ = b; // B 00116 if (alpha) 00117 *tg++ = alpha; // alpha 00118 } 00119 } 00120 00121 // free used GDI and other structures 00122 00123 RestoreDC(hdc,save_dc); // reset DC 00124 DeleteDC(hdc); 00125 DeleteObject(hbm); 00126 delete[] dib; // delete DIB temporary buffer 00127 00128 return p; 00129 } 00130 00131 // 00132 // End of "$Id: fl_read_image_win32.cxx 8048 2010-12-16 20:23:57Z AlbrechtS $". 00133 //