]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/xpm/macx.c
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 \*****************************************************************************/
48 img
= XpmMalloc (sizeof (*img
));
52 rect
.left
= rect
.top
= 0 ;
53 rect
.bottom
= height
;
60 NewGWorld( &img
->gworldptr
, depth
, &rect
, NULL
, NULL
, 0 ) ;
61 if (img
->gworldptr
== NULL
)
73 static struct RastPort *
74 AllocRastPort (unsigned int, unsigned int, unsigned int);
76 FreeRastPort (struct RastPort *, unsigned int,unsigned int);
79 static struct RastPort *
87 rp = XpmMalloc (sizeof (*rp));
91 if (GfxBase->LibNode.lib_Version >= 39)
93 rp->BitMap = AllocBitMap (width, height, depth, BMF_CLEAR, NULL);
94 if (rp->BitMap == NULL)
96 FreeRastPort (rp, width, height);
104 rp->BitMap = XpmMalloc (sizeof (*rp->BitMap));
105 if (rp->BitMap == NULL)
107 FreeRastPort (rp, width, height);
111 InitBitMap (rp->BitMap, depth, width, height);
112 for (i = 0; i < depth; ++i)
113 rp->BitMap->Planes[i] = NULL;
114 for (i = 0; i < depth; ++i)
116 rp->BitMap->Planes[i] = (PLANEPTR)AllocRaster (width, height);
117 if (rp->BitMap->Planes[i] == NULL)
119 FreeRastPort (rp, width, height);
138 if (rp->BitMap != NULL)
141 if (GfxBase->LibNode.lib_Version >= 39)
142 FreeBitMap (rp->BitMap);
147 for (i = 0; i < rp->BitMap->Depth; ++i)
149 if (rp->BitMap->Planes[i] != NULL)
150 FreeRaster (rp->BitMap->Planes[i], width, height);
152 XpmFree (rp->BitMap);
168 img = XpmMalloc (sizeof (*img));
172 img->height = height;
173 img->rp = AllocRastPort (img->width, img->height, depth);
191 FreeRastPort (ximage->rp, ximage->width, ximage->height);
206 SetAPen (ximage->rp, pixel);
207 WritePixel (ximage->rp, x, y);
216 XColor *screen_in_out,
217 unsigned long precision,
220 if (GfxBase->LibNode.lib_Version >= 39)
222 unsigned long r, g, b;
224 r = screen_in_out->red * 0x00010001;
225 g = screen_in_out->green * 0x00010001;
226 b = screen_in_out->blue * 0x00010001;
227 screen_in_out->pixel = ObtainBestPen (colormap, r, g, b,
228 OBP_Precision, precision,
229 OBP_FailIfBad, fail_if_bad,
231 if (screen_in_out->pixel == -1)
234 QueryColor (colormap, screen_in_out);
238 XColor nearest, trial;
239 long nearest_delta, trial_delta;
242 num_cells = colormap->Count;
244 QueryColor (colormap, &nearest);
245 nearest_delta = ((((screen_in_out->red >> 8) - (nearest.red >> 8))
246 * ((screen_in_out->red >> 8) - (nearest.red >> 8)))
248 (((screen_in_out->green >> 8) - (nearest.green >> 8))
249 * ((screen_in_out->green >> 8) - (nearest.green >> 8)))
251 (((screen_in_out->blue >> 8) - (nearest.blue >> 8))
252 * ((screen_in_out->blue >> 8) - (nearest.blue >> 8))));
253 for (i = 1; i < num_cells; i++)
255 // precision and fail_if_bad is ignored under pre V39
257 QueryColor (colormap, &trial);
258 trial_delta = ((((screen_in_out->red >> 8) - (trial.red >> 8))
259 * ((screen_in_out->red >> 8) - (trial.red >> 8)))
261 (((screen_in_out->green >> 8) - (trial.green >> 8))
262 * ((screen_in_out->green >> 8) - (trial.green >> 8)))
264 (((screen_in_out->blue >> 8) - (trial.blue >> 8))
265 * ((screen_in_out->blue >> 8) - (trial.blue >> 8))));
266 if (trial_delta < nearest_delta)
269 nearest_delta = trial_delta;
272 screen_in_out->pixel = nearest.pixel;
273 screen_in_out->red = nearest.red;
274 screen_in_out->green = nearest.green;
275 screen_in_out->blue = nearest.blue;
285 unsigned long *pixels,
288 if (GfxBase->LibNode.lib_Version >= 39)
292 for (i = 0; i < npixels; i++)
293 ReleasePen (colormap, pixels[i]);
303 XColor *exact_def_return)
310 spec_length = strlen(spec);
316 hexlen = (spec_length - 1) / 3;
317 if (hexlen < 1 || hexlen > 4 || hexlen * 3 != spec_length - 1)
320 hexstr[hexlen] = '\0';
321 strncpy (hexstr, spec + 1, hexlen);
322 exact_def_return->red = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
323 strncpy (hexstr, spec + 1 + hexlen, hexlen);
324 exact_def_return->green = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
325 strncpy (hexstr, spec + 1 + 2 * hexlen, hexlen);
326 exact_def_return->blue = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
333 int items, red, green, blue;
334 char line[512], name[512];
335 Bool success = False;
337 rgbf = fopen ("LIBS:rgb.txt", "r");
341 while (fgets(line, sizeof (line), rgbf) && !success)
343 items = sscanf (line, "%d %d %d %[^\n]\n",
344 &red, &green, &blue, name);
348 if (red < 0 || red > 0xFF
349 || green < 0 || green > 0xFF
350 || blue < 0 || blue > 0xFF)
355 if (0 == xpmstrcasecmp (spec, name))
357 exact_def_return->red = red * 0x0101;
358 exact_def_return->green = green * 0x0101;
359 exact_def_return->blue = blue * 0x0101;
375 if (GfxBase->LibNode.lib_Version >= 39)
377 unsigned long rgb[3];
379 GetRGB32 (colormap, def_in_out->pixel, 1, rgb);
380 def_in_out->red = rgb[0] >> 16;
381 def_in_out->green = rgb[1] >> 16;
382 def_in_out->blue = rgb[2] >> 16;
388 rgb = GetRGB4 (colormap, def_in_out->pixel);
389 def_in_out->red = ((rgb >> 8) & 0xF) * 0x1111;
390 def_in_out->green = ((rgb >> 4) & 0xF) * 0x1111;
391 def_in_out->blue = (rgb & 0xF) * 0x1111;
406 for (i = 0; i < ncolors; i++)
407 QueryColor (colormap, &defs_in_out[i]);