summaryrefslogtreecommitdiff
path: root/src/simx.c
blob: c76934b08c864637f3ec3176e67d68670cabf74a (plain)
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
/*
 * Copyright (C) 1989-95 GROUPE BULL
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of GROUPE BULL shall not be
 * used in advertising or otherwise to promote the sale, use or other dealings
 * in this Software without prior written authorization from GROUPE BULL.
 */

/*****************************************************************************\
* simx.c: 0.1a                                                                *
*                                                                             *
* This emulates some Xlib functionality for MSW. It's not a general solution, *
* it is close related to XPM-lib. It is only intended to satisfy what is need *
* there. Thus allowing to read XPM files under MS windows.                    *
*                                                                             *
* Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de)                           *
\*****************************************************************************/

#ifdef FOR_MSW

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "xpm.h"
#include "xpmi.h"			/* for XpmMalloc */

/*
 * On DOS size_t is only 2 bytes, thus malloc(size_t s) can only malloc
 * 64K. BUT an expression data=malloc(width*height) may result in an
 * overflow. So this function takes a long as input, and returns NULL if the
 * request is larger than 64K, is size_t is only 2 bytes.
 *
 * This requires casts like XpmMalloc( (long)width*(long(height)), else it
 * might have no effect at all.
 */

void *
boundCheckingMalloc(long s)
{
    if (sizeof(size_t) == sizeof(long)) {	/* same size, just do it */
	return (malloc((size_t) s));
    } else {
	if (sizeof(size_t) == 2) {
	    if (s > 0xFFFF)
		return (NULL);		/* to large, size_t with 2 bytes
					 * only allows 16 bits */
	    else
		return (malloc((size_t) s));
	} else {			/* it's not a long, not 2 bytes,
					 * what is it ??? */
	    return (malloc((size_t) s));
	}
    }
}
void *
boundCheckingCalloc(long num, long s)
{
    if (sizeof(size_t) == sizeof(long)) {	/* same size, just do it */
	return (calloc((size_t) num, (size_t) s));
    } else {
	if (sizeof(size_t) == 2) {
	    if (s > 0xFFFF || num * s > 0xFFFF)
		return (NULL);		/* to large, size_t with 2 bytes
					 * only allows 16 bits */
	    else
		return (calloc((size_t) num, (size_t) s));
	} else {			/* it's not a long, not 2 bytes,
					 * what is it ??? */
	    return (calloc((size_t) num, (size_t) s));
	}
    }
}
void *
boundCheckingRealloc(void *p, long s)
{
    if (sizeof(size_t) == sizeof(long)) {	/* same size, just do it */
	return (realloc(p, (size_t) s));
    } else {
	if (sizeof(size_t) == 2) {
	    if (s > 0xFFFF)
		return (NULL);		/* to large, size_t with 2 bytes
					 * only allows 16 bits */
	    else
		return (realloc(p, (size_t) s));
	} else {			/* it's not a long, not 2 bytes,
					 * what is it ??? */
	    return (realloc(p, (size_t) s));
	}
    }
}

/* static Visual theVisual = { 0 }; */
Visual *
XDefaultVisual(Display *display, Screen *screen)
{
    return (NULL);			/* struct could contain info about
					 * MONO, GRAY, COLOR */
}

Screen *
XDefaultScreen(Display *d)
{
    return (NULL);
}

/* I get only 1 plane but 8 bits per pixel,
   so I think BITSPIXEL should be depth */
int
XDefaultDepth(Display *display, Screen *screen)
{
    int d, b;

    b = GetDeviceCaps(*display, BITSPIXEL);
    d = GetDeviceCaps(*display, PLANES);
    return (b);
}

Colormap *
XDefaultColormap(Display *display, Screen *screen)
{
    return (NULL);
}

/* convert hex color names,
   wrong digits (not a-f,A-F,0-9) are treated as zero */
static int
hexCharToInt(c)
{
    int r;

    if (c >= '0' && c <= '9')
	r = c - '0';
    else if (c >= 'a' && c <= 'f')
	r = c - 'a' + 10;
    else if (c >= 'A' && c <= 'F')
	r = c - 'A' + 10;
    else
	r = 0;

    return (r);
}

static int
rgbFromHex(char *hex, int *r, int *g, int *b)
{
    int len;

    if (hex == NULL || hex[0] != '#')
	return (0);

    len = strlen(hex);
    if (len == 3 + 1) {
	*r = hexCharToInt(hex[1]);
	*g = hexCharToInt(hex[2]);
	*b = hexCharToInt(hex[3]);
    } else if (len == 6 + 1) {
	*r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
	*g = hexCharToInt(hex[3]) * 16 + hexCharToInt(hex[4]);
	*b = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
    } else if (len == 12 + 1) {
	/* it's like c #32329999CCCC */
	/* so for now only take two digits */
	*r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
	*g = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
	*b = hexCharToInt(hex[9]) * 16 + hexCharToInt(hex[10]);
    } else
	return (0);

    return (1);
}

/* Color related functions */
int
XParseColor(Display *d, Colormap *cmap, char *name, XColor *color)
{
    int r, g, b;			/* only 8 bit values used */
    int okay;

/* TODO: use colormap via PALETTE */
    /* parse name either in table or #RRGGBB #RGB */
    if (name == NULL)
	return (0);

    if (name[0] == '#') {		/* a hex string */
	okay = rgbFromHex(name, &r, &g, &b);
    } else {
	okay = xpmGetRGBfromName(name, &r, &g, &b);
    }

    if (okay) {
	color->pixel = RGB(r, g, b);
	color->red = (BYTE) r;
	color->green = (BYTE) g;
	color->blue = (BYTE) b;
	return (1);
    } else
	return (0);			/* --> ColorError */
}


int
XAllocColor(Display *d, Colormap cmap, XColor *color)
{
/* colormap not used yet so color->pixel is the real COLORREF (RBG) and not an
   index in some colormap as in X */
    return (1);
}
void
XQueryColors(Display *display, Colormap *colormap,
	     XColor *xcolors, int ncolors)
{
/* under X this fills the rgb values to given .pixel */
/* since there no colormap use FOR_MSW (not yet!!), rgb is plain encoded */
    XColor *xc = xcolors;
    int i;

    for (i = 0; i < ncolors; i++, xc++) {
	xc->red = GetRValue(xc->pixel);
	xc->green = GetGValue(xc->pixel);
	xc->blue = GetBValue(xc->pixel);
    }
    return;
}
int
XFreeColors(Display *d, Colormap cmap,
	    unsigned long pixels[], int npixels, unsigned long planes)
{
    /* no colormap yet */
    return (0);				/* correct ??? */
}

/* XImage functions */
XImage *
XCreateImage(Display *d, Visual *v,
	     int depth, int format,
	     int x, int y, int width, int height,
	     int pad, int foo)
{
    XImage *img = (XImage *) XpmMalloc(sizeof(XImage));

    if (img) {
	/*JW: This is what it should be, but the picture comes out
	      just black!?  It appears to be doing monochrome reduction,
	      but I've got no clue why.  Using CreateBitmap() is supposed
	      to be slower, but otherwise ok
	  if ( depth == GetDeviceCaps(*d, BITSPIXEL) ) {
	    img->bitmap = CreateCompatibleBitmap(*d, width, height);
        } else*/ {
	    img->bitmap = CreateBitmap(width, height, 1 /* plane */ ,
				       depth /* bits per pixel */ , NULL);
	}
	img->width = width;
	img->height = height;
	img->depth = depth;
    }
    return (img);

}

void
XImageFree(XImage *img)
{
    if (img) {
	XpmFree(img);
    }
}
void
XDestroyImage(XImage *img)
{
    if (img) {
	DeleteObject(img->bitmap);	/* check return ??? */
	XImageFree(img);
    }
}

#endif