]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/xpm/macx.cpp
2 * Copyright (C) 1998 Stefan Csomor
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 * Lorens Younes BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN 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 Lorens Younes 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 Lorens Younes.
26 /*****************************************************************************\
30 * Emulates some Xlib functionality for Amiga. *
32 * Developed by Lorens Younes (d93-hyo@nada.kth.se) 7/95 *
34 \*****************************************************************************/
43 XImage * AllocXImage (
50 img = (XImage*) XpmMalloc (sizeof (*img));
53 Rect rect = { 0 , 0 , height , width } ;
59 NewGWorld( &img->gworldptr , depth , &rect , NULL , NULL , 0 ) ;
60 if (img->gworldptr == NULL)
71 int XDestroyImage ( XImage *ximage)
75 if ( ximage->gworldptr )
76 DisposeGWorld( ximage->gworldptr ) ;
84 Status XAllocColor( Display dpy, Colormap colormap, XColor *c )
87 unsigned short red, green, blue;
88 c->pixel = ( ( (c->red)>>8 ) << 16 ) + ( ( (c->green)>>8 ) << 8 ) + ( ( (c->blue)>>8 ) ) ;
92 int XFreeColors(Display dpy, Colormap colormap, unsigned long *pixels,
93 int npixels, int planes)
98 int XParseColor(Display dpy, Colormap colormap, char *spec, XColor *exact_def_return)
105 spec_length = strlen(spec);
111 hexlen = (spec_length - 1) / 3;
112 if (hexlen < 1 || hexlen > 4 || hexlen * 3 != spec_length - 1)
115 hexstr[hexlen] = '\0';
116 strncpy (hexstr, spec + 1, hexlen);
117 exact_def_return->red = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
118 strncpy (hexstr, spec + 1 + hexlen, hexlen);
119 exact_def_return->green = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
120 strncpy (hexstr, spec + 1 + 2 * hexlen, hexlen);
121 exact_def_return->blue = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
131 int XQueryColor(Display dpy, Colormap colormap, XColor *c)
133 c->pixel = ( ( (c->red)>>8 ) << 16 ) + ( ( (c->green)>>8 ) << 8 ) + ( ( (c->blue)>>8 ) ) ;
135 if (GfxBase->LibNode.lib_Version >= 39)
137 unsigned long rgb[3];
139 GetRGB32 (colormap, def_in_out->pixel, 1, rgb);
140 def_in_out->red = rgb[0] >> 16;
141 def_in_out->green = rgb[1] >> 16;
142 def_in_out->blue = rgb[2] >> 16;
148 rgb = GetRGB4 (colormap, def_in_out->pixel);
149 def_in_out->red = ((rgb >> 8) & 0xF) * 0x1111;
150 def_in_out->green = ((rgb >> 4) & 0xF) * 0x1111;
151 def_in_out->blue = (rgb & 0xF) * 0x1111;
157 int XQueryColors(Display dpy, Colormap colormap, XColor *defs_in_out, int ncolors)
161 for (i = 0; i < ncolors; i++)
162 XQueryColor (dpy, colormap , &defs_in_out[i]);
167 int XPutPixel ( XImage *ximage,int x,int y,unsigned long pixel)
173 GetGWorld( &origPort , &origDev ) ;
174 SetGWorld( ximage->gworldptr , NULL ) ;
176 color.red = (pixel & 0x00FF0000) >> 16 ;
177 color.green = (pixel & 0x0000FF00) >> 8 ;
178 color.blue = (pixel & 0x000000FF) ;
179 color.red = ( color.red<<8) + color.red ;
180 color.green = ( color.green<<8) + color.green ;
181 color.blue = ( color.blue<<8) + color.blue ;
182 SetCPixel( x , y , &color ) ;
183 SetGWorld( origPort , origDev ) ;
189 static struct RastPort *
190 AllocRastPort (unsigned int, unsigned int, unsigned int);
192 FreeRastPort (struct RastPort *, unsigned int,unsigned int);
195 static struct RastPort *
203 rp = XpmMalloc (sizeof (*rp));
207 if (GfxBase->LibNode.lib_Version >= 39)
209 rp->BitMap = AllocBitMap (width, height, depth, BMF_CLEAR, NULL);
210 if (rp->BitMap == NULL)
212 FreeRastPort (rp, width, height);
220 rp->BitMap = XpmMalloc (sizeof (*rp->BitMap));
221 if (rp->BitMap == NULL)
223 FreeRastPort (rp, width, height);
227 InitBitMap (rp->BitMap, depth, width, height);
228 for (i = 0; i < depth; ++i)
229 rp->BitMap->Planes[i] = NULL;
230 for (i = 0; i < depth; ++i)
232 rp->BitMap->Planes[i] = (PLANEPTR)AllocRaster (width, height);
233 if (rp->BitMap->Planes[i] == NULL)
235 FreeRastPort (rp, width, height);
254 if (rp->BitMap != NULL)
257 if (GfxBase->LibNode.lib_Version >= 39)
258 FreeBitMap (rp->BitMap);
263 for (i = 0; i < rp->BitMap->Depth; ++i)
265 if (rp->BitMap->Planes[i] != NULL)
266 FreeRaster (rp->BitMap->Planes[i], width, height);
268 XpmFree (rp->BitMap);
282 SetAPen (ximage->rp, pixel);
283 WritePixel (ximage->rp, x, y);
292 XColor *screen_in_out,
293 unsigned long precision,
296 if (GfxBase->LibNode.lib_Version >= 39)
298 unsigned long r, g, b;
300 r = screen_in_out->red * 0x00010001;
301 g = screen_in_out->green * 0x00010001;
302 b = screen_in_out->blue * 0x00010001;
303 screen_in_out->pixel = ObtainBestPen (colormap, r, g, b,
304 OBP_Precision, precision,
305 OBP_FailIfBad, fail_if_bad,
307 if (screen_in_out->pixel == -1)
310 QueryColor (colormap, screen_in_out);
314 XColor nearest, trial;
315 long nearest_delta, trial_delta;
318 num_cells = colormap->Count;
320 QueryColor (colormap, &nearest);
321 nearest_delta = ((((screen_in_out->red >> 8) - (nearest.red >> 8))
322 * ((screen_in_out->red >> 8) - (nearest.red >> 8)))
324 (((screen_in_out->green >> 8) - (nearest.green >> 8))
325 * ((screen_in_out->green >> 8) - (nearest.green >> 8)))
327 (((screen_in_out->blue >> 8) - (nearest.blue >> 8))
328 * ((screen_in_out->blue >> 8) - (nearest.blue >> 8))));
329 for (i = 1; i < num_cells; i++)
331 // precision and fail_if_bad is ignored under pre V39
333 QueryColor (colormap, &trial);
334 trial_delta = ((((screen_in_out->red >> 8) - (trial.red >> 8))
335 * ((screen_in_out->red >> 8) - (trial.red >> 8)))
337 (((screen_in_out->green >> 8) - (trial.green >> 8))
338 * ((screen_in_out->green >> 8) - (trial.green >> 8)))
340 (((screen_in_out->blue >> 8) - (trial.blue >> 8))
341 * ((screen_in_out->blue >> 8) - (trial.blue >> 8))));
342 if (trial_delta < nearest_delta)
345 nearest_delta = trial_delta;
348 screen_in_out->pixel = nearest.pixel;
349 screen_in_out->red = nearest.red;
350 screen_in_out->green = nearest.green;
351 screen_in_out->blue = nearest.blue;
361 unsigned long *pixels,
364 if (GfxBase->LibNode.lib_Version >= 39)
368 for (i = 0; i < npixels; i++)
369 ReleasePen (colormap, pixels[i]);
379 XColor *exact_def_return)
386 spec_length = strlen(spec);
392 hexlen = (spec_length - 1) / 3;
393 if (hexlen < 1 || hexlen > 4 || hexlen * 3 != spec_length - 1)
396 hexstr[hexlen] = '\0';
397 strncpy (hexstr, spec + 1, hexlen);
398 exact_def_return->red = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
399 strncpy (hexstr, spec + 1 + hexlen, hexlen);
400 exact_def_return->green = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
401 strncpy (hexstr, spec + 1 + 2 * hexlen, hexlen);
402 exact_def_return->blue = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
409 int items, red, green, blue;
410 char line[512], name[512];
411 Bool success = False;
413 rgbf = fopen ("LIBS:rgb.txt", "r");
417 while (fgets(line, sizeof (line), rgbf) && !success)
419 items = sscanf (line, "%d %d %d %[^\n]\n",
420 &red, &green, &blue, name);
424 if (red < 0 || red > 0xFF
425 || green < 0 || green > 0xFF
426 || blue < 0 || blue > 0xFF)
431 if (0 == xpmstrcasecmp (spec, name))
433 exact_def_return->red = red * 0x0101;
434 exact_def_return->green = green * 0x0101;
435 exact_def_return->blue = blue * 0x0101;
451 if (GfxBase->LibNode.lib_Version >= 39)
453 unsigned long rgb[3];
455 GetRGB32 (colormap, def_in_out->pixel, 1, rgb);
456 def_in_out->red = rgb[0] >> 16;
457 def_in_out->green = rgb[1] >> 16;
458 def_in_out->blue = rgb[2] >> 16;
464 rgb = GetRGB4 (colormap, def_in_out->pixel);
465 def_in_out->red = ((rgb >> 8) & 0xF) * 0x1111;
466 def_in_out->green = ((rgb >> 4) & 0xF) * 0x1111;
467 def_in_out->blue = (rgb & 0xF) * 0x1111;
482 for (i = 0; i < ncolors; i++)
483 QueryColor (colormap, &defs_in_out[i]);