summaryrefslogtreecommitdiffstats
path: root/private/sdktools/imagedit/util.c
blob: 0cf16b655ae999d88f83123e8cb4f6fa0a8545f3 (plain) (blame)
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/****************************************************************************/
/*                                                                          */
/*                         Microsoft Confidential                           */
/*                                                                          */
/*                 Copyright (c) Microsoft Corp.  1987, 1990                */
/*                           All Rights Reserved                            */
/*                                                                          */
/****************************************************************************/
/****************************** Module Header *******************************
* Module Name: util.c
*
* Contains miscellaneous utility functions for ImagEdit.
*
* History:
*
****************************************************************************/

#include "imagedit.h"

#include <stdio.h>
#include <stdarg.h>

#define CBOVERHEAD      (sizeof(INT)+sizeof(INT)+sizeof(INT))
#define MEMSIGHEAD      0x1234
#define MEMSIGTAIL      0x5678



/****************************************************************************
* MyAlloc
*
*
*
* History:
*  25-Jul-1989  Byron Dazey - Created
****************************************************************************/

VOID *MyAlloc(
    INT cbAlloc)
{
    register HANDLE hMem;

    if (hMem = LocalAlloc(LMEM_FIXED, cbAlloc)) {
        return (VOID *)hMem;
    }
    else {
        MessageBeep(0);
        Message(MSG_OUTOFMEMORY);

        return NULL;
    }
}



/****************************************************************************
* MyRealloc
*
*
*
* History:
*  25-Jul-1989  Byron Dazey - Created
****************************************************************************/

VOID *MyRealloc(
    VOID *npMem,
    INT cbNewAlloc)
{
    npMem = (VOID *)LocalReAlloc((HANDLE)npMem, cbNewAlloc, LMEM_MOVEABLE);

    if (!npMem) {
        MessageBeep(0);
        Message(MSG_OUTOFMEMORY);

        return NULL;
    }

    return npMem;
}



/****************************************************************************
* MyFree
*
*
* History:
*  25-Jul-1989  Byron Dazey - Created
****************************************************************************/

VOID *MyFree(
    VOID *npMem)
{
    if (LocalFree((HANDLE)npMem)) {
        MessageBeep(0);
        Message(MSG_MEMERROR);

        return npMem;
    }

    return NULL;
}



/************************************************************************
* Message
*
* This function puts up a message box.  The message is described in
* the gamdMessages table.
*
* Arguments:
*   UINT idMsg - Index to the message.
*   ...        - Optional arguments.
*
* Returns:
*     What MessageBox returns.
*
************************************************************************/

INT Message(
    UINT idMsg,
    ...)
{
    va_list marker;
    INT RetCode;
    CHAR szT[CCHTEXTMAX];

    va_start(marker, idMsg);
    vsprintf(szT, ids(gamdMessages[idMsg].ids), marker);

    RetCode = MessageBox(NULL, szT, ids(IDS_PGMTITLE),
            gamdMessages[idMsg].fMessageBox | MB_TASKMODAL);

    va_end(marker);

    return RetCode;
}



/************************************************************************
* CenterWindow
*
* This function centers the given window over its owner.  It ensures
* that the window is entirely within the visible screen, however.
* If the window does not have an owner, it is centered over the
* desktop.
*
* Arguments:
*   HWND hwnd - The window to center.
*
* History:
*
************************************************************************/

VOID CenterWindow(
    HWND hwnd)
{
    RECT rc;
    RECT rcOwner;
    RECT rcCenter;
    HWND hwndOwner;

    GetWindowRect(hwnd, &rc);

    if (!(hwndOwner = GetWindow(hwnd, GW_OWNER)))
        hwndOwner = GetDesktopWindow();

    GetWindowRect(hwndOwner, &rcOwner);

    /*
     *  Calculate the starting x,y for the new
     *  window so that it would be centered.
     */
    rcCenter.left = rcOwner.left +
            (((rcOwner.right - rcOwner.left) -
            (rc.right - rc.left))
            / 2);

    rcCenter.top = rcOwner.top +
            (((rcOwner.bottom - rcOwner.top) -
            (rc.bottom - rc.top))
            / 2);

    rcCenter.right = rcCenter.left + (rc.right - rc.left);
    rcCenter.bottom = rcCenter.top + (rc.bottom - rc.top);

    FitRectToScreen(&rcCenter);

    SetWindowPos(hwnd, NULL, rcCenter.left, rcCenter.top, 0, 0,
            SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
}



/************************************************************************
* FitRectToScreen
*
* This function ensures that the given rectangle is entirely within
* the visible screen, adjusting it if necessary.
*
* Arguments:
*   PRECT prc - The rectangle.
*
* History:
*
************************************************************************/

VOID FitRectToScreen(
    PRECT prc)
{
    INT cxScreen;
    INT cyScreen;
    INT delta;

    cxScreen = GetSystemMetrics(SM_CXSCREEN);
    cyScreen = GetSystemMetrics(SM_CYSCREEN);

    if (prc->right > cxScreen) {
        delta = prc->right - prc->left;
        prc->right = cxScreen;
        prc->left = prc->right - delta;
    }

    if (prc->left < 0) {
        delta = prc->right - prc->left;
        prc->left = 0;
        prc->right = prc->left + delta;
    }

    if (prc->bottom > cyScreen) {
        delta = prc->bottom - prc->top;
        prc->bottom = cyScreen;
        prc->top = prc->bottom - delta;
    }

    if (prc->top < 0) {
        delta = prc->bottom - prc->top;
        prc->top = 0;
        prc->bottom = prc->top + delta;
    }
}



/************************************************************************
* ids
*
* This function will return a string, given the string id.  If this is
* the first time that the string has been retrieved, memory will be
* allocated for it and it will be loaded.  After it is loaded once, it
* is then cached in a PSTR array and is available for later without
* having to load it again.
*
* Arguments:
*   UINT idString - String ID of the string to retrieve.
*
* History:
*
************************************************************************/

PSTR ids(
    UINT idString)
{
    static PSTR apstr[CSTRINGS];        // String resource array cache.
    PSTR pstr;
    INT cch;

    if (apstr[idString])
        return apstr[idString];

    if (!(pstr = MyAlloc(CCHTEXTMAX)))
        return "";

    if (!(cch = LoadString(ghInst, idString, pstr, CCHTEXTMAX))) {
        MyFree(pstr);
        return "";
    }

    apstr[idString] = pstr = MyRealloc(pstr, cch + 1);

    return (pstr ? pstr : "");
}



/************************************************************************
* MyCreateBitmap
*
*
*
* Arguments:
*
* History:
*
************************************************************************/

HBITMAP MyCreateBitmap(
    HDC hdc,
    INT cx,
    INT cy,
    INT nColors)
{
    BITMAPINFOHEADER bmih;

    if (nColors == 2) {
        return CreateBitmap(cx, cy, 1, 1, NULL);
    }
    else {
        bmih.biSize = sizeof(BITMAPINFOHEADER);
        bmih.biWidth = cx;
        bmih.biHeight = cy;
        bmih.biPlanes = 1;              // 1 plane, 4 bpp is
        bmih.biBitCount = 4;            // 16 colors.

        bmih.biCompression =
        bmih.biSizeImage =
        bmih.biXPelsPerMeter =
        bmih.biYPelsPerMeter =
        bmih.biClrUsed =
        bmih.biClrImportant = 0;

        return CreateDIBitmap(hdc, &bmih, 0L, NULL, NULL, 0);
    }
}



#if DBG && defined(WIN16)
/****************************************************************************
* DBGStackReport
*
* This debugging function reports how much stack is used by a program.
* To use it, call it with fInit == TRUE right at the beginning of the
* program and then with fInit == FALSE just before the program exits.
* The stack space used during the current run of the program will be
* displayed on the debug terminal.
*
* It is implemented by filling the stack with a certain value down to
* the bottom of the stack (if fInit is TRUE).  When it is called with
* fInit == FALSE, it starts at the bottom of the stack and looks for
* where this "signature" value has been overwritten with data, then
* does a little math to compute the used stack.
*
* Arguments:
*   BOOL fInit - TRUE if the stack should be initialized, FALSE to
*                print out the report.
*
* History:
*  28-Aug-1990  Byron Dazey - Created
****************************************************************************/

/*
 * This signature byte will be used to fill the stack.
 */
#define STACKSIG    'A'

/*
 * This is a C runtime global that is always at the very end of the
 * global data.  Taking its address is a way that the "bottom" of the
 * stack can be found.
 */
extern CHAR end;

VOID DBGStackReport(
    BOOL fInit)
{
    static PBYTE pbStackTop;
    PBYTE pb;
    BYTE bDummy;

    if (fInit) {
        /*
         * The address of one of this functions local variables is
         * taken and considered the "top" of the stack.  This means
         * that it will work best when it is called first thing in
         * the program.
         */
        pbStackTop = pb = &bDummy;

        /*
         * Fill the stack up.
         */
        while (pb > &end)
            *pb-- = STACKSIG;
    }
    else {
        /*
         * Start at the bottom of the stack and search upwards.
         */
        pb = &end;
        while (*(++pb) == STACKSIG && pb < pbStackTop)
            ;

        /*
         * Display the results.
         */
        DBGprintf("ImagEdit stack used: %d bytes.", pbStackTop - pb);
    }
}
#endif



#if DBG
/****************************************************************************
* DBGBltImage
*
* This debugging function blits out the given image in the specified
* DC to the screen.  Every time that it is called, it will blit the
* image to the right of the last one, starting at the top of the
* screen.  It assumes that each image is 32x32 pixels.
*
* Arguments:
*   HDC hdc - The DC with the image to blit.  If this is NULL, the
*             current XOR and AND images are blit'd, with the AND
*             image below the XOR image.
*
* History:
*  16-Sep-1991  Byron Dazey - Created
****************************************************************************/

VOID DBGBltImage(
    HDC hdc)
{
    static INT x;
    HDC hdcScreen;

    hdcScreen = GetDC(NULL);

    if (hdc) {
        BitBlt(hdcScreen, x, 0, 32, 32, hdc, 0, 0, SRCCOPY);
    }
    else {
        BitBlt(hdcScreen, x, 0, 32, 32, ghdcImage, 0, 0, SRCCOPY);
        BitBlt(hdcScreen, x, 32 + 1, 32, 32, ghdcANDMask, 0, 0, SRCCOPY);
    }

    ReleaseDC(NULL, hdcScreen);
    x += 32 + 1;
}



/****************************************************************************
* DBGprintf
*
* This debugging function prints out a string to the debug output.
* An optional set of substitutional parameters can be specified,
* and the final output will be the processed result of these combined
* with the format string, just like printf.  A newline is always
* output after every call to this function.
*
* Arguments:
*   PSTR fmt - Format string (printf style).
*   ...      - Variable number of arguments.
*
* History:
*  28-Aug-1990  Byron Dazey - Created
****************************************************************************/

VOID DBGprintf(PSTR fmt, ...)
{
    va_list marker;
    CHAR szBuf[CCHTEXTMAX];

    va_start(marker, fmt);
    vsprintf(szBuf, fmt, marker);
    va_end(marker);

    OutputDebugString(szBuf);
    OutputDebugString("\r\n");
}
#endif