111
116
return GSWindowRectToMS(s, r);
119
void* get_bits(HDC dc,
128
info.bmiHeader.biSize = sizeof(BITMAPINFO);
129
info.bmiHeader.biWidth = w;
130
info.bmiHeader.biHeight = h;
131
info.bmiHeader.biPlanes = 1;
132
info.bmiHeader.biBitCount = 32;
133
info.bmiHeader.biCompression = BI_RGB;
134
info.bmiHeader.biSizeImage = 0;
135
info.bmiHeader.biXPelsPerMeter = 0;
136
info.bmiHeader.biYPelsPerMeter = 0;
137
info.bmiHeader.biClrUsed = 0;
138
info.bmiHeader.biClrImportant = 0;
140
if(!(cDC = CreateCompatibleDC(dc)))
142
NSLog(@"Could not create compatible DC");
146
if(!(*bitmap = CreateDIBSection(dc, (LPBITMAPINFO)&info,
147
DIB_RGB_COLORS, &bits,
150
NSLog(@"Could not create bit map from DC");
154
SelectObject(cDC, *bitmap);
159
BOOL alpha_blend_source_over(HDC destDC,
162
int x, int y, int w, int h,
167
#ifdef USE_ALPHABLEND
168
// Use (0..1) fraction to set a (0..255) alpha constant value
169
BYTE SourceConstantAlpha = (BYTE)(delta * 255);
170
BLENDFUNCTION blendFunc
171
= {AC_SRC_OVER, 0, SourceConstantAlpha, AC_SRC_ALPHA};
173
/* There is actually a very real chance this could fail, even on
174
computers that supposedly support it. It's not known why it
176
success = AlphaBlend(destDC,
179
rectFrom.left, rectFrom.top,
184
// unsigned char *sbits = (unsigned char *)get_bits(srcDC,w,h,&sbitmap);
185
// unsigned char *dbits = (unsigned char *)get_bits(destDC,w,h,&dbitmap);
114
192
@interface WIN32GState (WinOps)
115
193
- (void) setStyle: (HDC)hDC;
116
194
- (void) restoreStyle: (HDC)hDC;
1199
@implementation WIN32GState (PatternColor)
1207
newClipRegion = CreateRectRgn(0, 0, 1, 1);
1208
CombineRgn(newClipRegion, clipRegion, NULL, RGN_COPY);
1210
return newClipRegion;
1215
- (void) restoreClip: (void *)savedClip
1219
DeleteObject(clipRegion);
1221
clipRegion = savedClip;
1226
@implementation WIN32GState (ReadRect)
1228
- (NSDictionary *) GSReadRect: (NSRect)r
1230
NSMutableDictionary *dict;
1231
NSAffineTransform *matrix;
1233
NSMutableData *data;
1234
unsigned char *cdata;
1235
unsigned char *bits;
1238
HDC hdcMemDC = NULL;
1239
HBITMAP hbitmap = NULL;
1245
LPBITMAPV4HEADER lpbi;
1249
NSLog(@"No window in GSReadRect");
1253
r = [ctm rectInMatrixSpace: r];
1257
dict = [NSMutableDictionary dictionary];
1258
[dict setObject: NSDeviceRGBColorSpace forKey: @"ColorSpace"];
1260
[dict setObject: [NSNumber numberWithUnsignedInt: 8]
1261
forKey: @"BitsPerSample"];
1262
[dict setObject: [NSNumber numberWithUnsignedInt: 32]
1264
[dict setObject: [NSNumber numberWithUnsignedInt: 4]
1265
forKey: @"SamplesPerPixel"];
1266
[dict setObject: [NSNumber numberWithUnsignedInt: 1]
1267
forKey: @"HasAlpha"];
1269
matrix = [self GSCurrentCTM];
1270
[matrix translateXBy: -r.origin.x - offset.x
1271
yBy: r.origin.y + NSHeight(r) - offset.y];
1272
[dict setObject: matrix forKey: @"Matrix"];
1274
hDC = GetDC((HWND)window);
1277
NSLog(@"No DC for window %d in GSReadRect. Error %d",
1278
(int)window, GetLastError());
1282
// Create a compatible DC which is used in a BitBlt from the window DC
1283
hdcMemDC = CreateCompatibleDC(hDC);
1286
NSLog(@"No Compatible DC for window %d in GSReadRect. Error %d",
1287
(int)window, GetLastError());
1288
ReleaseDC((HWND)window, hDC);
1292
ReleaseDC((HWND)window, hDC);
1293
hDC = [self getHDC];
1295
// Get the client area for size calculation
1296
rcClient = GSWindowRectToMS(self, r);
1298
// Create a compatible bitmap from the Window DC
1299
hbitmap = CreateCompatibleBitmap(hDC, rcClient.right - rcClient.left,
1300
rcClient.bottom - rcClient.top);
1303
NSLog(@"No Compatible bitmap for window %d in GSReadRect. Error %d",
1304
(int)window, GetLastError());
1305
ReleaseDC((HWND)window, hdcMemDC);
1306
[self releaseHDC: hDC];
1310
// Select the compatible bitmap into the compatible memory DC.
1311
old = SelectObject(hdcMemDC, hbitmap);
1314
NSLog(@"SelectObject failed for window %d in GSReadRect. Error %d",
1315
(int)window, GetLastError());
1316
DeleteObject(hbitmap);
1317
ReleaseDC((HWND)window, hdcMemDC);
1318
[self releaseHDC: hDC];
1322
// Bit block transfer into our compatible memory DC.
1323
if (!BitBlt(hdcMemDC, 0, 0,
1324
rcClient.right - rcClient.left,
1325
rcClient.bottom - rcClient.top,
1330
NSLog(@"BitBlt failed for window %d in GSReadRect. Error %d",
1331
(int)window, GetLastError());
1332
DeleteObject(hbitmap);
1333
ReleaseDC((HWND)window, hdcMemDC);
1334
[self releaseHDC: hDC];
1338
// Get the BITMAP from the HBITMAP
1339
GetObject(hbitmap, sizeof(BITMAP), &bmpCopied);
1341
dwBmpSize = bmpCopied.bmWidth * 4 * bmpCopied.bmHeight;
1343
hDIB = GlobalAlloc(GHND, dwBmpSize + sizeof(BITMAPV4HEADER));
1344
lpbi = (LPBITMAPV4HEADER)GlobalLock(hDIB);
1345
lpbi->bV4Size = sizeof(BITMAPV4HEADER);
1346
lpbi->bV4V4Compression = BI_BITFIELDS;
1347
lpbi->bV4BlueMask = 0x000000FF;
1348
lpbi->bV4GreenMask = 0x0000FF00;
1349
lpbi->bV4RedMask = 0x00FF0000;
1350
lpbi->bV4AlphaMask = 0xFF000000;
1351
lpbi->bV4Width = bmpCopied.bmWidth;
1352
lpbi->bV4Height = bmpCopied.bmHeight;
1353
lpbi->bV4Planes = 1;
1354
lpbi->bV4BitCount = 32;
1355
lpbi->bV4SizeImage = 0;
1356
lpbi->bV4XPelsPerMeter = 0;
1357
lpbi->bV4YPelsPerMeter = 0;
1358
lpbi->bV4ClrUsed = 0;
1359
lpbi->bV4ClrImportant = 0;
1360
bits = (unsigned char *)lpbi + sizeof(BITMAPV4HEADER);
1362
// Gets the "bits" from the bitmap and copies them into a buffer
1363
// which is pointed to by lpbi
1364
if (GetDIBits(hdcMemDC, hbitmap, 0, (UINT)bmpCopied.bmHeight, bits,
1365
(BITMAPINFOHEADER *)lpbi, DIB_RGB_COLORS) == 0)
1367
NSLog(@"GetDIBits failed for window %d in GSReadRect. Error %d",
1368
(int)window, GetLastError());
1371
DeleteObject(hbitmap);
1372
ReleaseDC((HWND)window, hdcMemDC);
1373
[self releaseHDC: hDC];
1377
data = [NSMutableData dataWithLength: dwBmpSize];
1382
DeleteObject(hbitmap);
1383
ReleaseDC((HWND)window, hdcMemDC);
1384
[self releaseHDC: hDC];
1389
cdata = [data mutableBytes];
1390
while (i < dwBmpSize)
1392
cdata[i+0] = bits[i+2];
1393
cdata[i+1] = bits[i+1];
1394
cdata[i+2] = bits[i+0];
1395
cdata[i+3] = bits[i+3];
1400
//Unlock and Free the DIB from the heap
1405
DeleteObject(hbitmap);
1406
ReleaseDC((HWND)window, hdcMemDC);
1407
[self releaseHDC: hDC];
1409
[dict setObject: [NSValue valueWithSize: NSMakeSize(bmpCopied.bmWidth,
1410
bmpCopied.bmHeight)]
1412
[dict setObject: data forKey: @"Data"];