/
Snapshot.c
executable file
·447 lines (358 loc) · 12.8 KB
/
Snapshot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
File: Snapshot.c
Contains: This application demonstrates how to quickly and
efficiently capture the main device's desktop into
a window. The program basically reads the image
stored in the the main device's pixmap then copies
it to a custom pixmap. The custom pixmap is de-
fined at the same depth of the main device and
contains an identical copy of that device's color-
table. This is done to provide the fastest
performance possible when copying from an offscreen
to onscreen pixmap. By making sure the pixel values
map to the exact same colors in both colortables,
copybits will do a direct transfer of bits without
wasting time remapping the colors. Also the ctSeed
field for each colortable should be the same. Finally,
since the main device's bounding rect is different
than that of the offscreen's, the copying performance
for the device to the offscreen is slightly affected
because of the scaling required. However, the copying
performance for the offscreen to the window is the
best possible since the bounding rects for each are
identical.
Written by: EL
Copyright: Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
You may incorporate this Apple sample source code into your program(s) without
restriction. This Apple sample source code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to redistribute
this Apple sample source code as "Apple sample source code" after having made
changes. If you're going to re-distribute the source, we require that you make
it clear in the source that the code was descended from Apple sample source
code, but that you've made changes.
Change History (most recent first):
08/2000 JM Carbonized, non-Carbon code is commented out
for demonstration purposes.
11/6/1999 GGS Updated to work with modern (1999) Mac OS.
Fixed a PixMap disposing bug. Updated casts
and headers.
7/14/1999 KG Updated for Metrowerks Codewarror Pro 2.1
*/
#ifdef __APPLE_CC__
#include <Carbon/Carbon.h>
#else
#include <Carbon.h>
#endif
/* Constant Declarations */
#define WWIDTH ((qd.screenBits.bounds.right - qd.screenBits.bounds.left) / 2)
#define WHEIGHT ((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) / 2)
#define WLEFT (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
#define WTOP (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
#define MENU_BAR_ID 128
#define MENU_BAR_IDX 129
#define FILE_MENU 128
enum {
FILE_NEW = 1,
FILE_REFRESH,
FILE_CLOSE,
FILE_SAVE,
FILE_QUIT
};
/* Global Variable Definitions */
WindowPtr gWindow; /* Main window */
PixMap gPixMap; /* Offscreen pixmap */
Rect gBounds; /* Offscreen pixmap's bounding rect */
Boolean gDone = false; // Application termination global
void initMac();
void setUp();
void handleMenuSelection(long result);
void handleKeyPress(EventRecord *);
void saveToPICTFile();
void createWindow();
void initPixmap();
void createImage();
void drawImage();
pascal OSErr AEQuitHandler(const AppleEvent *messagein, AppleEvent *reply, long refIn);
void doEventLoop();
int main()
{
initMac();
setUp();
createWindow(); /* Create a window to display the final image. */
initPixmap(); /* Initialize offscreen pixmap. */
createImage(); /* Copy the main screen's pixmap into the offscreen's. */
drawImage(); /* Copy the offscreen's pixmap onto the window. */
doEventLoop(); /* Handle any events. */
DisposeWindow( gWindow );
return 0;
}
void initMac()
{
/*MaxApplZone();
InitGraf( &qd.thePort );
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs( nil );*/
InitCursor();
FlushEvents( 0, everyEvent );
}
void setUp()
{
Handle menuBar;
OSErr anErr = noErr;
long aLong;
long response;
anErr = Gestalt(gestaltSystemVersion, &response);
// Carbon Porting guidelines say provide alternate menu bar/menu scheme for OS X
// This is just one way of doing this, which is pretty static
if (response >= 0x01000)
menuBar = GetNewMBar(MENU_BAR_IDX);
else
menuBar = GetNewMBar(MENU_BAR_ID);
if ( menuBar == nil || anErr != noErr )
ExitToShell();
SetMenuBar(menuBar);
DisposeHandle(menuBar);
DrawMenuBar();
// Install 'quit' event handler
if ((Gestalt(gestaltAppleEventsAttr, &aLong) == noErr)) {
anErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
NewAEEventHandlerUPP(AEQuitHandler), 0, false);
if (anErr != noErr)
ExitToShell();
}
}
void createWindow()
{
Rect wBounds;
BitMap bitMap;
Rect tempRect1;
int top, left, width, height;
GetQDGlobalsScreenBits(&bitMap);
width = ((bitMap.bounds.right - bitMap.bounds.left) / 2);
height = ((bitMap.bounds.bottom - bitMap.bounds.top) / 2);
left = (((bitMap.bounds.right - bitMap.bounds.left) - width) / 2);
top = (((bitMap.bounds.bottom - bitMap.bounds.top) - height) / 2);
/* Create a window to display the final offscreen image. */
//SetRect( &wBounds, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
SetRect( &wBounds, left, top, left + width, top + height );
gWindow = NewCWindow( 0L, &wBounds, "\pSnapshot Test", false, documentProc,
(WindowPtr)-1L, true, 0L );
//SetRect( &gBounds, 0, 0, gWindow->portRect.right - gWindow->portRect.left,
// gWindow->portRect.bottom - gWindow->portRect.top );
GetPortBounds(GetWindowPort(gWindow), &tempRect1);
SetRect( &gBounds, 0, 0, tempRect1.right - tempRect1.left,
tempRect1.bottom - tempRect1.top);
//SetPort( gWindow );
SetPortWindowPort( gWindow );
}
void initPixmap()
{
Ptr offBaseAddr; /* Pointer to the off-screen pixel image */
short bytesPerRow;
GDHandle mainDevice;
CTabHandle cTable;
short depth;
/* Get a handle to the main device. */
mainDevice = GetMainDevice();
/* Store its current pixel depth. */
depth = (**(**mainDevice).gdPMap).pixelSize;
/* Make an identical copy of its pixmap's colortable. */
cTable = (**(**mainDevice).gdPMap).pmTable;
(void) HandToHand( (Handle*)(&cTable) );
bytesPerRow = ((gBounds.right - gBounds.left) * depth) / 8;
offBaseAddr = NewPtr((unsigned long)bytesPerRow * (gBounds.bottom - gBounds.top));
gPixMap.baseAddr = offBaseAddr; /* Point to image */
gPixMap.rowBytes = bytesPerRow | 0x8000; /* MSB set for PixMap */
gPixMap.bounds = gBounds; /* Use given bounds */
gPixMap.pmVersion = 0; /* No special stuff */
gPixMap.packType = 0; /* Default PICT pack */
gPixMap.packSize = 0; /* Always zero in mem */
gPixMap.hRes = 72; /* 72 DPI default res */
gPixMap.vRes = 72; /* 72 DPI default res */
gPixMap.pixelSize = depth; /* Set # bits/pixel */
//gPixMap.planeBytes = 0; /* Not used */
//gPixMap.pmReserved = 0; /* Not used */
gPixMap.pixelType = 0; /* Indicates indexed */
gPixMap.cmpCount = 1; /* Have 1 component */
gPixMap.cmpSize = depth; /* Component size=depth */
gPixMap.pmTable = cTable; /* Handle to CLUT */
}
void createImage()
{
GDHandle mainDevice;
mainDevice = GetMainDevice();
/* Store the screen's pixmap image in the offscreen pixmap. */
CopyBits( (BitMap *)*(**mainDevice).gdPMap, (BitMap *)(&gPixMap),
&(**(**mainDevice).gdPMap).bounds, &gPixMap.bounds, srcCopy, 0l );
}
void drawImage(WindowPtr theWindow)
{
Rect tempRect1;
/* Copy the offscreen image back onto the window. */
//CopyBits( (BitMap *)&gPixMap, &gWindow->portBits, &gPixMap.bounds,
// &gWindow->portRect, srcCopy, 0l);
CopyBits( (BitMap *)&gPixMap, GetPortBitMapForCopyBits(GetWindowPort(theWindow)), &gPixMap.bounds,
GetPortBounds(GetWindowPort(gWindow), &tempRect1), srcCopy, 0L);
ShowWindow( gWindow );
}
void saveToPICTFile()
{
/*
Saving a PixMap as a PICT file isn't too hard.
1. Open a Picture with the port set to the destination of #2.
2. CopyBits the PixMap onto itself or another port. (Because CopyBits is
recorded in Pictures.
3. Close the picture.
4. Open the data fork for the file.
5. Write out 512 bytes of zeros followed by the contents of the Picture
handle.
6. Close the file.
*/
PicHandle picHandle;
OSErr anErr = noErr;
OSType fileTypeToSave = 'PICT';
OSType creatorType = 'ogle';
NavReplyRecord reply;
NavDialogOptions dialogOptions;
FSSpec documentFSSpec;
long inOutCount;
short refNum, count;
AEKeyword theKeyword;
DescType actualType;
unsigned char header[512];
Size actualSize;
Rect tempRect1;
CopyBits(GetPortBitMapForCopyBits(GetWindowPort(FrontWindow())), (BitMap*) &gPixMap,
GetPortBounds(GetWindowPort(gWindow), &tempRect1), &gPixMap.bounds, srcCopy, 0L);
SetPortWindowPort(gWindow);
picHandle = OpenPicture(&gPixMap.bounds);
CopyBits((BitMap*) &gPixMap, GetPortBitMapForCopyBits(GetWindowPort(FrontWindow())), &gPixMap.bounds,
GetPortBounds(GetWindowPort(gWindow), &tempRect1), srcCopy, 0L);
ClosePicture();
for (count = 0; count < 512; count++)
header[count] = 0x00;
anErr = NavGetDefaultDialogOptions(&dialogOptions);
dialogOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
anErr = NavPutFile( nil,
&reply,
&dialogOptions,
nil,
fileTypeToSave,
creatorType,
nil );
if (anErr == noErr && reply.validRecord) {
anErr = AEGetNthPtr(&(reply.selection), 1, typeFSS,
&theKeyword, &actualType,
&documentFSSpec, sizeof(documentFSSpec),
&actualSize );
if (anErr == noErr) {
anErr = FSpCreate(&documentFSSpec, creatorType, fileTypeToSave, smSystemScript);
if (anErr == dupFNErr) {
anErr = FSpDelete(&documentFSSpec);
anErr = FSpCreate(&documentFSSpec, creatorType, fileTypeToSave, smSystemScript);
} // this is quick 'n' dirty or there'd be more robust handling here
// write the file
FSpOpenDF(&documentFSSpec, fsRdWrPerm, &refNum );
inOutCount = 512;
anErr = FSWrite(refNum, &inOutCount, header); // write the header
if (anErr == noErr) {
inOutCount = GetHandleSize((Handle)picHandle);
anErr = FSWrite(refNum,&inOutCount,*picHandle);
}
FSClose( refNum );
}
reply.translationNeeded = false;
anErr = NavCompleteSave(&reply, kNavTranslateInPlace);
NavDisposeReply(&reply);
}
KillPicture(picHandle);
}
void handleMenuSelection(long result)
{
int menuID, menuItem;
RgnHandle rgnHandle = NewRgn();
menuID = HiWord(result);
menuItem = LoWord(result);
if (menuID == FILE_MENU) {
if (menuItem == FILE_SAVE)
saveToPICTFile();
else if (menuItem == FILE_QUIT)
gDone = true;
else if (menuItem == FILE_CLOSE) {
DisposeWindow(FrontWindow());
if (FrontWindow() == NULL)
gDone = true;
}
else if (menuItem == FILE_NEW) {
}
else if (menuItem == FILE_REFRESH) {
createImage();
drawImage(FrontWindow());
QDFlushPortBuffer(GetWindowPort(FrontWindow()), GetPortVisibleRegion(GetWindowPort(FrontWindow()), rgnHandle));
}
}
HiliteMenu(0);
DisposeRgn(rgnHandle);
}
void handleKeyPress(EventRecord *event)
{
char key;
key = event->message & charCodeMask;
// just check to see if we want to quit...
if ( event->modifiers & cmdKey ) /* Command key down? */
handleMenuSelection(MenuKey(key));
}
// Apple Event - "Quit"
pascal OSErr AEQuitHandler(const AppleEvent *messagein, AppleEvent *reply, long refIn)
{
#pragma unused (messagein,refIn,reply)
gDone = true;
return noErr;
}
void doEventLoop()
{
EventRecord anEvent;
WindowPtr evtWind;
short clickArea;
Rect screenRect;
while (!gDone)
{
if (WaitNextEvent( everyEvent, &anEvent, 0, nil ))
{
if (anEvent.what == mouseDown)
{
clickArea = FindWindow( anEvent.where, &evtWind );
if (clickArea == inMenuBar)
handleMenuSelection(MenuSelect(anEvent.where));
else if (clickArea == inDrag)
{
//screenRect = (**GetGrayRgn ()).rgnBBox;
GetRegionBounds(GetGrayRgn(), &screenRect);
DragWindow( evtWind, anEvent.where, &screenRect );
}
else if (clickArea == inContent)
{
if (evtWind != FrontWindow())
SelectWindow( evtWind );
}
else if (clickArea == inGoAway)
if (TrackGoAway( evtWind, anEvent.where ))
gDone = true;
}
else if (anEvent.what == updateEvt)
{
evtWind = (WindowPtr)anEvent.message;
//SetPort( evtWind );
SetPortWindowPort( evtWind );
BeginUpdate( evtWind );
drawImage( evtWind );
EndUpdate (evtWind);
}
else if (anEvent.what == autoKey || anEvent.what == keyDown)
handleKeyPress(&anEvent);
}
}
}