]>
Commit | Line | Data |
---|---|---|
0240e8b1 SC |
1 | /* |
2 | * Copyright (C) 1989-95 GROUPE BULL | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to | |
6 | * deal in the Software without restriction, including without limitation the | |
7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
8 | * sell copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
17 | * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | |
18 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
20 | * | |
21 | * Except as contained in this notice, the name of GROUPE BULL shall not be | |
22 | * used in advertising or otherwise to promote the sale, use or other dealings | |
23 | * in this Software without prior written authorization from GROUPE BULL. | |
24 | */ | |
25 | ||
26 | /*****************************************************************************\ | |
27 | * simx.c: 0.1a * | |
28 | * * | |
29 | * This emulates some Xlib functionality for MSW. It's not a general solution, * | |
30 | * it is close related to XPM-lib. It is only intended to satisfy what is need * | |
31 | * there. Thus allowing to read XPM files under MS windows. * | |
32 | * * | |
33 | * Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de) * | |
34 | \*****************************************************************************/ | |
35 | ||
36 | #include "xpm.h" | |
37 | #include "xpmi.h" /* for XpmMalloc */ | |
2f1ae414 | 38 | #include "rgbtab.h" |
0240e8b1 SC |
39 | #ifdef FOR_MSW |
40 | ||
41 | /* | |
42 | * On DOS size_t is only 2 bytes, thus malloc(size_t s) can only malloc | |
43 | * 64K. BUT an expression data=malloc(width*height) may result in an | |
44 | * overflow. So this function takes a long as input, and returns NULL if the | |
45 | * request is larger than 64K, is size_t is only 2 bytes. | |
46 | * | |
47 | * This requires casts like XpmMalloc( (long)width*(long(height)), else it | |
48 | * might have no effect at all. | |
49 | */ | |
50 | ||
51 | void * | |
52 | boundCheckingMalloc(long s) | |
53 | { | |
54 | if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */ | |
55 | return (malloc((size_t) s)); | |
56 | } else { | |
57 | if (sizeof(size_t) == 2) { | |
58 | if (s > 0xFFFF) | |
59 | return (NULL); /* to large, size_t with 2 bytes | |
60 | * only allows 16 bits */ | |
61 | else | |
62 | return (malloc((size_t) s)); | |
63 | } else { /* it's not a long, not 2 bytes, | |
64 | * what is it ??? */ | |
65 | return (malloc((size_t) s)); | |
66 | } | |
67 | } | |
68 | } | |
69 | void * | |
70 | boundCheckingCalloc(long num, long s) | |
71 | { | |
72 | if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */ | |
73 | return (calloc((size_t) num, (size_t) s)); | |
74 | } else { | |
75 | if (sizeof(size_t) == 2) { | |
76 | if (s > 0xFFFF || num * s > 0xFFFF) | |
77 | return (NULL); /* to large, size_t with 2 bytes | |
78 | * only allows 16 bits */ | |
79 | else | |
80 | return (calloc((size_t) num, (size_t) s)); | |
81 | } else { /* it's not a long, not 2 bytes, | |
82 | * what is it ??? */ | |
83 | return (calloc((size_t) num, (size_t) s)); | |
84 | } | |
85 | } | |
86 | } | |
87 | void * | |
88 | boundCheckingRealloc(void *p, long s) | |
89 | { | |
90 | if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */ | |
91 | return (realloc(p, (size_t) s)); | |
92 | } else { | |
93 | if (sizeof(size_t) == 2) { | |
94 | if (s > 0xFFFF) | |
95 | return (NULL); /* to large, size_t with 2 bytes | |
96 | * only allows 16 bits */ | |
97 | else | |
98 | return (realloc(p, (size_t) s)); | |
99 | } else { /* it's not a long, not 2 bytes, | |
100 | * what is it ??? */ | |
101 | return (realloc(p, (size_t) s)); | |
102 | } | |
103 | } | |
104 | } | |
105 | #endif | |
106 | ||
107 | /* static Visual theVisual = { 0 }; */ | |
108 | Visual * | |
109 | XDefaultVisual(Display *display, Screen *screen) | |
110 | { | |
111 | return (NULL); /* struct could contain info about | |
112 | * MONO, GRAY, COLOR */ | |
113 | } | |
114 | ||
115 | Screen * | |
116 | XDefaultScreen(Display *d) | |
117 | { | |
118 | return (NULL); | |
119 | } | |
120 | ||
121 | /* I get only 1 plane but 8 bits per pixel, | |
122 | so I think BITSPIXEL should be depth */ | |
123 | int | |
124 | XDefaultDepth(Display *display, Screen *screen) | |
125 | { | |
126 | #ifdef FOR_MSW | |
127 | int d, b; | |
128 | ||
129 | b = GetDeviceCaps(*display, BITSPIXEL); | |
130 | d = GetDeviceCaps(*display, PLANES); | |
131 | return (b); | |
132 | #else | |
133 | return (**((*display)->gdPMap)).pixelSize ; | |
134 | #endif | |
135 | } | |
136 | ||
137 | Colormap * | |
138 | XDefaultColormap(Display *display, Screen *screen) | |
139 | { | |
140 | return (NULL); | |
141 | } | |
142 | ||
143 | /* convert hex color names, | |
144 | wrong digits (not a-f,A-F,0-9) are treated as zero */ | |
145 | static int | |
146 | hexCharToInt(c) | |
147 | { | |
148 | int r; | |
149 | ||
150 | if (c >= '0' && c <= '9') | |
151 | r = c - '0'; | |
152 | else if (c >= 'a' && c <= 'f') | |
153 | r = c - 'a' + 10; | |
154 | else if (c >= 'A' && c <= 'F') | |
155 | r = c - 'A' + 10; | |
156 | else | |
157 | r = 0; | |
158 | ||
159 | return (r); | |
160 | } | |
161 | ||
162 | static int | |
163 | rgbFromHex(char *hex, int *r, int *g, int *b) | |
164 | { | |
165 | int len; | |
166 | ||
167 | if (hex == NULL || hex[0] != '#') | |
168 | return (0); | |
169 | ||
170 | len = strlen(hex); | |
171 | if (len == 3 + 1) | |
172 | { | |
173 | *r = hexCharToInt(hex[1]); | |
174 | *g = hexCharToInt(hex[2]); | |
175 | *b = hexCharToInt(hex[3]); | |
9c1175db | 176 | #if defined(macintosh) || defined(__APPLE__) |
0240e8b1 SC |
177 | *r <<= 12 ; |
178 | *g <<= 12 ; | |
179 | *b <<= 12 ; | |
180 | #endif | |
181 | } else if (len == 6 + 1) | |
182 | { | |
183 | *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]); | |
184 | *g = hexCharToInt(hex[3]) * 16 + hexCharToInt(hex[4]); | |
185 | *b = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]); | |
9c1175db | 186 | #if defined(macintosh) || defined(__APPLE__) |
0240e8b1 SC |
187 | *r <<= 8 ; |
188 | *g <<= 8 ; | |
189 | *b <<= 8 ; | |
190 | #endif | |
191 | } else if (len == 12 + 1) | |
192 | { | |
193 | /* it's like c #32329999CCCC */ | |
9c1175db | 194 | #if defined(macintosh) || defined(__APPLE__) |
0240e8b1 SC |
195 | *r = hexCharToInt(hex[1]) * 0x1000 + hexCharToInt(hex[2]) *0x0100 + hexCharToInt(hex[3]) *0x0010 + hexCharToInt(hex[4]) ; |
196 | *g = hexCharToInt(hex[5]) * 0x1000 + hexCharToInt(hex[6]) *0x0100 + hexCharToInt(hex[7]) *0x0010 + hexCharToInt(hex[8]); | |
197 | *b =hexCharToInt(hex[9]) * 0x1000 + hexCharToInt(hex[10]) *0x0100 + hexCharToInt(hex[11]) *0x0010 + hexCharToInt(hex[12]); | |
198 | #else | |
199 | /* so for now only take two digits */ | |
200 | *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]); | |
201 | *g = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]); | |
202 | *b = hexCharToInt(hex[9]) * 16 + hexCharToInt(hex[10]); | |
203 | #endif | |
204 | } else | |
205 | return (0); | |
206 | ||
207 | return (1); | |
208 | } | |
209 | ||
210 | /* Color related functions */ | |
211 | int | |
212 | XParseColor(Display *d, Colormap *cmap, char *name, XColor *color) | |
213 | { | |
214 | int r, g, b; /* only 8 bit values used */ | |
215 | int okay; | |
216 | ||
217 | /* TODO: use colormap via PALETTE */ | |
218 | /* parse name either in table or #RRGGBB #RGB */ | |
219 | if (name == NULL) | |
220 | return (0); | |
221 | ||
222 | if (name[0] == '#') { /* a hex string */ | |
223 | okay = rgbFromHex(name, &r, &g, &b); | |
224 | } else { | |
225 | okay = xpmGetRGBfromName(name, &r, &g, &b); | |
226 | } | |
227 | ||
228 | if (okay) | |
229 | { | |
230 | #ifdef FOR_MSW | |
231 | color->pixel = RGB(r, g, b); | |
232 | color->red = (BYTE) r; | |
233 | color->green = (BYTE) g; | |
234 | color->blue = (BYTE) b; | |
9c1175db | 235 | #elif defined(macintosh) || defined(__APPLE__) |
0240e8b1 SC |
236 | color->pixel.red = r ; |
237 | color->pixel.green = g ; | |
238 | color->pixel.blue = b ; | |
239 | ||
240 | color->red = r; | |
241 | color->green = g; | |
242 | color->blue = b; | |
243 | #endif | |
244 | return (1); | |
245 | } | |
246 | else | |
247 | return (0); /* --> ColorError */ | |
248 | } | |
249 | ||
250 | ||
251 | int | |
252 | XAllocColor(Display *d, Colormap *cmap, XColor *color) | |
253 | { | |
254 | /* colormap not used yet so color->pixel is the real COLORREF (RBG) and not an | |
255 | index in some colormap as in X */ | |
256 | return (1); | |
257 | } | |
258 | void | |
259 | XQueryColors(Display *display, Colormap *colormap, | |
260 | XColor *xcolors, int ncolors) | |
261 | { | |
262 | /* under X this fills the rgb values to given .pixel */ | |
263 | /* since there no colormap use FOR_MSW (not yet!!), rgb is plain encoded */ | |
264 | XColor *xc = xcolors; | |
265 | int i; | |
266 | ||
267 | for (i = 0; i < ncolors; i++, xc++) { | |
268 | xc->red = GetRValue(xc->pixel); | |
269 | xc->green = GetGValue(xc->pixel); | |
270 | xc->blue = GetBValue(xc->pixel); | |
271 | } | |
272 | return; | |
273 | } | |
274 | int | |
275 | XFreeColors(Display *d, Colormap cmap, | |
9c1175db | 276 | Pixel *pixels, int npixels, unsigned long planes) |
0240e8b1 SC |
277 | { |
278 | /* no colormap yet */ | |
279 | return (0); /* correct ??? */ | |
280 | } | |
281 | ||
282 | /* XImage functions */ | |
283 | XImage * | |
284 | XCreateImage(Display *d, Visual *v, | |
285 | int depth, int format, | |
286 | int x, int y, int width, int height, | |
287 | int pad, int foo) | |
288 | { | |
289 | XImage *img = (XImage *) XpmMalloc(sizeof(XImage)); | |
290 | ||
291 | if (img) | |
292 | { | |
293 | #if FOR_MSW | |
294 | /*JW: This is what it should be, but the picture comes out | |
295 | just black!? It appears to be doing monochrome reduction, | |
296 | but I've got no clue why. Using CreateBitmap() is supposed | |
297 | to be slower, but otherwise ok | |
298 | if ( depth == GetDeviceCaps(*d, BITSPIXEL) ) { | |
299 | img->bitmap = CreateCompatibleBitmap(*d, width, height); | |
300 | } else*/ | |
301 | { | |
302 | img->bitmap = CreateBitmap(width, height, 1 /* plane */ , | |
303 | depth /* bits per pixel */ , NULL); | |
304 | } | |
9c1175db | 305 | #elif defined(macintosh) || defined(__APPLE__) |
0240e8b1 SC |
306 | Rect rect ; |
307 | ||
308 | rect.left= rect.top = 0 ; | |
309 | rect.right = width ; | |
310 | rect.bottom = height ; | |
311 | ||
312 | NewGWorld( &img->gworldptr , depth , &rect , NULL , NULL , 0 ) ; | |
2f1ae414 | 313 | LockPixels( GetGWorldPixMap( img->gworldptr ) ) ; |
0240e8b1 SC |
314 | if (img->gworldptr == NULL) |
315 | { | |
316 | XDestroyImage (img); | |
317 | return NULL; | |
318 | } | |
319 | /* | |
320 | else | |
321 | { | |
322 | RGBColor gray = { 0xEEEE ,0xEEEE , 0xEEEE } ; | |
323 | RGBBackColor( &gray ) ; | |
324 | EraseRect(&rect) ; | |
325 | } | |
326 | */ | |
327 | #endif | |
328 | img->width = width; | |
329 | img->height = height; | |
330 | img->depth = depth; | |
331 | } | |
332 | return (img); | |
333 | ||
334 | } | |
335 | ||
336 | void | |
337 | XImageFree(XImage *img) | |
338 | { | |
339 | if (img) { | |
340 | XpmFree(img); | |
341 | } | |
342 | } | |
343 | void | |
344 | XDestroyImage(XImage *img) | |
345 | { | |
346 | if (img) { | |
347 | #if FOR_MSW | |
348 | DeleteObject(img->bitmap); /* check return ??? */ | |
9c1175db | 349 | #elif defined(macintosh) || defined(__APPLE__) |
0240e8b1 | 350 | if ( img->gworldptr ) |
2f1ae414 SC |
351 | { |
352 | UnlockPixels( GetGWorldPixMap( img->gworldptr ) ) ; | |
0240e8b1 | 353 | DisposeGWorld( img->gworldptr ) ; |
2f1ae414 | 354 | } |
0240e8b1 SC |
355 | #endif |
356 | XImageFree(img); | |
357 | } | |
358 | } | |
359 |