]>
git.saurik.com Git - wxWidgets.git/blob - src/xpm/simx.c
2 * Copyright (C) 1989-95 GROUPE BULL
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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.
26 /*****************************************************************************\
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. *
33 * Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de) *
34 \*****************************************************************************/
36 /* Moved here so that FOR_MSW gets defined if we are using wxWindows (GRG) */
41 #include "xpmi.h" /* for XpmMalloc */
44 * On DOS size_t is only 2 bytes, thus malloc(size_t s) can only malloc
45 * 64K. BUT an expression data=malloc(width*height) may result in an
46 * overflow. So this function takes a long as input, and returns NULL if the
47 * request is larger than 64K, is size_t is only 2 bytes.
49 * This requires casts like XpmMalloc( (long)width*(long(height)), else it
50 * might have no effect at all.
54 boundCheckingMalloc(long s
)
56 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
57 return (malloc((size_t) s
));
59 if (sizeof(size_t) == 2) {
61 return (NULL
); /* to large, size_t with 2 bytes
62 * only allows 16 bits */
64 return (malloc((size_t) s
));
65 } else { /* it's not a long, not 2 bytes,
67 return (malloc((size_t) s
));
72 boundCheckingCalloc(long num
, long s
)
74 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
75 return (calloc((size_t) num
, (size_t) s
));
77 if (sizeof(size_t) == 2) {
78 if (s
> 0xFFFF || num
* s
> 0xFFFF)
79 return (NULL
); /* to large, size_t with 2 bytes
80 * only allows 16 bits */
82 return (calloc((size_t) num
, (size_t) s
));
83 } else { /* it's not a long, not 2 bytes,
85 return (calloc((size_t) num
, (size_t) s
));
90 boundCheckingRealloc(void *p
, long s
)
92 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
93 return (realloc(p
, (size_t) s
));
95 if (sizeof(size_t) == 2) {
97 return (NULL
); /* to large, size_t with 2 bytes
98 * only allows 16 bits */
100 return (realloc(p
, (size_t) s
));
101 } else { /* it's not a long, not 2 bytes,
103 return (realloc(p
, (size_t) s
));
108 /* static Visual theVisual = { 0 }; */
110 XDefaultVisual(Display
*display
, Screen
*screen
)
112 return (NULL
); /* struct could contain info about
113 * MONO, GRAY, COLOR */
117 XDefaultScreen(Display
*d
)
122 /* I get only 1 plane but 8 bits per pixel,
123 so I think BITSPIXEL should be depth */
125 XDefaultDepth(Display
*display
, Screen
*screen
)
129 b
= GetDeviceCaps(*display
, BITSPIXEL
);
130 d
= GetDeviceCaps(*display
, PLANES
);
135 XDefaultColormap(Display
*display
, Screen
*screen
)
140 /* convert hex color names,
141 wrong digits (not a-f,A-F,0-9) are treated as zero */
147 if (c
>= '0' && c
<= '9')
149 else if (c
>= 'a' && c
<= 'f')
151 else if (c
>= 'A' && c
<= 'F')
160 rgbFromHex(char *hex
, int *r
, int *g
, int *b
)
164 if (hex
== NULL
|| hex
[0] != '#')
169 *r
= hexCharToInt(hex
[1]);
170 *g
= hexCharToInt(hex
[2]);
171 *b
= hexCharToInt(hex
[3]);
172 } else if (len
== 6 + 1) {
173 *r
= hexCharToInt(hex
[1]) * 16 + hexCharToInt(hex
[2]);
174 *g
= hexCharToInt(hex
[3]) * 16 + hexCharToInt(hex
[4]);
175 *b
= hexCharToInt(hex
[5]) * 16 + hexCharToInt(hex
[6]);
176 } else if (len
== 12 + 1) {
177 /* it's like c #32329999CCCC */
178 /* so for now only take two digits */
179 *r
= hexCharToInt(hex
[1]) * 16 + hexCharToInt(hex
[2]);
180 *g
= hexCharToInt(hex
[5]) * 16 + hexCharToInt(hex
[6]);
181 *b
= hexCharToInt(hex
[9]) * 16 + hexCharToInt(hex
[10]);
188 /* Color related functions */
190 XParseColor(Display
*d
, Colormap
*cmap
, char *name
, XColor
*color
)
192 int r
, g
, b
; /* only 8 bit values used */
195 /* TODO: use colormap via PALETTE */
196 /* parse name either in table or #RRGGBB #RGB */
200 if (name
[0] == '#') { /* a hex string */
201 okay
= rgbFromHex(name
, &r
, &g
, &b
);
203 okay
= xpmGetRGBfromName(name
, &r
, &g
, &b
);
207 color
->pixel
= RGB(r
, g
, b
);
208 color
->red
= (BYTE
) r
;
209 color
->green
= (BYTE
) g
;
210 color
->blue
= (BYTE
) b
;
213 return (0); /* --> ColorError */
217 /* GRG: 2nd arg is Colormap*, not Colormap */
220 XAllocColor(Display
*d
, Colormap
*cmap
, XColor
*color
)
222 /* colormap not used yet so color->pixel is the real COLORREF (RBG) and not an
223 index in some colormap as in X */
227 XQueryColors(Display
*display
, Colormap
*colormap
,
228 XColor
*xcolors
, int ncolors
)
230 /* under X this fills the rgb values to given .pixel */
231 /* since there no colormap use FOR_MSW (not yet!!), rgb is plain encoded */
232 XColor
*xc
= xcolors
;
235 for (i
= 0; i
< ncolors
; i
++, xc
++) {
236 xc
->red
= GetRValue(xc
->pixel
);
237 xc
->green
= GetGValue(xc
->pixel
);
238 xc
->blue
= GetBValue(xc
->pixel
);
243 XFreeColors(Display
*d
, Colormap cmap
,
244 unsigned long pixels
[], int npixels
, unsigned long planes
)
246 /* no colormap yet */
247 return (0); /* correct ??? */
250 /* XImage functions */
252 XCreateImage(Display
*d
, Visual
*v
,
253 int depth
, int format
,
254 int x
, int y
, int width
, int height
,
257 XImage
*img
= (XImage
*) XpmMalloc(sizeof(XImage
));
260 /*JW: This is what it should be, but the picture comes out
261 just black!? It appears to be doing monochrome reduction,
262 but I've got no clue why. Using CreateBitmap() is supposed
263 to be slower, but otherwise ok
264 if ( depth == GetDeviceCaps(*d, BITSPIXEL) ) {
265 img->bitmap = CreateCompatibleBitmap(*d, width, height);
267 img
->bitmap
= CreateBitmap(width
, height
, 1 /* plane */ ,
268 depth
/* bits per pixel */ , NULL
);
271 img
->height
= height
;
279 XImageFree(XImage
*img
)
286 XDestroyImage(XImage
*img
)
289 DeleteObject(img
->bitmap
); /* check return ??? */