00001 /***************************** License ********************************** 00002 00003 Copyright (C) 2008 by Communicative Machines 00004 http://www.cmlabs.com All rights reserved. 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 00020 ************************************************************************/ 00021 00022 namespace cmlabs { 00023 00024 00025 ////////////////////////////////////////////////////////////////////// 00026 // Win32 Drawing Routines 00027 ////////////////////////////////////////////////////////////////////// 00028 00029 bool Bitmap::drawOn(HDC hdc, int x, int y, int w, int h) { 00030 00031 if (data == NULL) 00032 return false; 00033 00034 ObjectCollection* col = (ObjectCollection*) aux; 00035 if (col == NULL) 00036 return true; 00037 00038 int byteWidth = width*4; 00039 00040 int iSize = sizeof(BITMAPINFO) + 2 * sizeof(RGBQUAD); 00041 BITMAPINFO* bInfo = (BITMAPINFO*) malloc(iSize); 00042 memset(bInfo, 0, iSize); 00043 00044 bInfo->bmiHeader.biSize = iSize; 00045 bInfo->bmiHeader.biWidth = width; 00046 bInfo->bmiHeader.biHeight = -1 * height; 00047 bInfo->bmiHeader.biPlanes = 1; 00048 // bInfo->bmiHeader.biBitCount = 24; // depth; 00049 bInfo->bmiHeader.biBitCount = 32; // depth; 00050 // bInfo->bmiHeader.biSizeImage = height * width * 3; // size; 00051 bInfo->bmiHeader.biSizeImage = height * width * 4; // size; 00052 bInfo->bmiHeader.biXPelsPerMeter = 0; 00053 bInfo->bmiHeader.biYPelsPerMeter = 0; 00054 bInfo->bmiHeader.biClrUsed = 0; 00055 bInfo->bmiHeader.biClrImportant = 0; 00056 // bInfo->bmiHeader.biCompression = BI_RGB; 00057 bInfo->bmiHeader.biCompression = BI_BITFIELDS; 00058 bInfo->bmiColors[0].rgbBlue = 0xff; 00059 bInfo->bmiColors[1].rgbGreen = 0xff; 00060 bInfo->bmiColors[2].rgbRed = 0xff; 00061 00062 #ifdef WIN32_POCKETPC 00063 00064 // Use SetDIBitsToDevice for non-stretch... 00065 SetDIBitsToDevice(hdc, 00066 x,y,w,h, // screen coordinates 00067 0,0,0,height, // bitmap coordinates 00068 data, bInfo, DIB_RGB_COLORS); 00069 00070 #else 00071 00072 Box* box; 00073 int n = col->getCount(); 00074 if (n > 0) { 00075 int xx, yy, ww, hh; 00076 for (int i=0; i<n; i++) { 00077 if ( (box = (Box*) col->get(i)) != NULL) { 00078 xx = (int)box->getLeftX(); 00079 yy = (int)box->getUpperY(); 00080 ww = (int)box->getWidth(); 00081 hh = (int)box->getHeight(); 00082 // Use SetDIBitsToDevice for non-stretch... 00083 SetDIBitsToDevice(hdc, 00084 x+xx, y+yy, ww, hh, // screen coordinates 00085 xx,height-yy-hh,0,height, // bitmap coordinates 00086 data, bInfo, DIB_RGB_COLORS); 00087 } 00088 } 00089 } 00090 else { 00091 // Use SetDIBitsToDevice for non-stretch... 00092 SetDIBitsToDevice(hdc, 00093 x,y,w,h, // screen coordinates 00094 0,0,0,height, // bitmap coordinates 00095 data, bInfo, DIB_RGB_COLORS); 00096 } 00097 00098 #endif 00099 00100 00101 00102 /* StretchDIBits(hdc, 00103 0,0,width,height, // screen coordinates 00104 0,0,width,height, // bitmap coordinates 00105 data, bInfo, DIB_RGB_COLORS, SRCCOPY); 00106 */ 00107 free(bInfo); 00108 00109 /* if (hBitmap == NULL) 00110 return false; 00111 00112 JTime t1; 00113 HDC paintDC = CreateCompatibleDC(0); 00114 JTime t2; 00115 00116 SelectObject(paintDC, hBitmap); 00117 JTime t3; 00118 if ( (w > 0) && (h > 0) ) 00119 BitBlt(hdc, x, y, w, h, paintDC, 0, 0, SRCCOPY); 00120 else 00121 BitBlt(hdc, x, y, width, height, paintDC, 0, 0, SRCCOPY); 00122 JTime t4; 00123 DeleteDC(paintDC); 00124 JTime t5; 00125 00126 JString str = JString::format("%d %d %d %d", t2 - t1, t3 - t2, t4 - t3, t5 - t4); 00127 */ 00128 return true; 00129 } 00130 00131 /* 00132 char* Bitmap::createHBitmap() { 00133 00134 if (data != NULL) 00135 return false; 00136 00137 int iSize = sizeof(BITMAPINFO) + 2 * sizeof(RGBQUAD); 00138 BITMAPINFO* bInfo = (BITMAPINFO*) malloc(iSize); 00139 memset(bInfo, 0, iSize); 00140 00141 bInfo->bmiHeader.biSize = iSize; 00142 bInfo->bmiHeader.biWidth = width; 00143 bInfo->bmiHeader.biHeight = -1 * height; 00144 bInfo->bmiHeader.biPlanes = planes; 00145 bInfo->bmiHeader.biBitCount = 32; // depth; 00146 bInfo->bmiHeader.biSizeImage = height * width * 4; // size; 00147 bInfo->bmiHeader.biXPelsPerMeter = 0; 00148 bInfo->bmiHeader.biYPelsPerMeter = 0; 00149 bInfo->bmiHeader.biClrUsed = 0; 00150 bInfo->bmiHeader.biClrImportant = 0; 00151 bInfo->bmiHeader.biBitCount = 32; // depth; 00152 bInfo->bmiHeader.biCompression = BI_BITFIELDS; 00153 bInfo->bmiColors[0].rgbBlue = 0xff; 00154 bInfo->bmiColors[1].rgbGreen = 0xff; 00155 bInfo->bmiColors[2].rgbRed = 0xff; 00156 00157 void* hBitmapData; 00158 HDC fakeDC = CreateCompatibleDC(0); 00159 hBitmap = CreateDIBSection(fakeDC, bInfo, DIB_RGB_COLORS, &hBitmapData, NULL, 0); 00160 DeleteDC(fakeDC); 00161 00162 free(bInfo); 00163 00164 if (hBitmap == NULL) { 00165 return NULL; 00166 } 00167 00168 return (char*) hBitmapData; 00169 00170 // if (hBitmapChanged) { 00171 00172 // memcpy((char*) hBitmapData, data, byteWidth*height); 00173 00174 // char* hdata = (char*) hBitmapData; 00175 // char* dst = hdata + (height-1) * byteWidth; 00176 // char* dst = hdata; 00177 // char* src = data; 00178 00179 // memcpy(dst, src, byteWidth*height); 00180 00181 00182 // memcpy(dst, src, byteWidth*height); 00183 00184 00185 // memcpy(dst, src, byteWidth); 00186 // src += byteWidth; 00187 // dst += byteWidth; 00188 // } 00189 00190 // hBitmapChanged = false; 00191 // } 00192 00193 // return true; 00194 } 00195 */ 00196 00197 bool Bitmap::copyFromHDC(HDC hdc, int x, int y, int w, int h) { 00198 00199 // if (hBitmap == NULL) 00200 // return false; 00201 00202 // HDC paintDC = CreateCompatibleDC(0); 00203 // SelectObject(paintDC, hBitmap); 00204 00205 // if ( (w > 0) && (h > 0) ) 00206 // BitBlt(paintDC, 0, 0, w, h, hdc, x, y, SRCCOPY); 00207 // else 00208 // BitBlt(paintDC, 0, 0, width, height, hdc, x, y, SRCCOPY); 00209 00210 // DeleteDC(paintDC); 00211 return true; 00212 } 00213 00214 00215 } // namespace cmlabs 00216