2 * Copyright (C) 19896 Lorens Younes
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 \*****************************************************************************/
42 #include <graphics/gfxbase.h>
43 #include <intuition/screens.h>
45 #include <proto/exec.h>
48 static struct RastPort *
49 AllocRastPort (unsigned int, unsigned int, unsigned int);
51 FreeRastPort (struct RastPort *, unsigned int,unsigned int);
54 static struct RastPort *
62 rp = XpmMalloc (sizeof (*rp));
66 if (GfxBase->LibNode.lib_Version >= 39)
68 rp->BitMap = AllocBitMap (width, height, depth, BMF_CLEAR, NULL);
69 if (rp->BitMap == NULL)
71 FreeRastPort (rp, width, height);
79 rp->BitMap = XpmMalloc (sizeof (*rp->BitMap));
80 if (rp->BitMap == NULL)
82 FreeRastPort (rp, width, height);
86 InitBitMap (rp->BitMap, depth, width, height);
87 for (i = 0; i < depth; ++i)
88 rp->BitMap->Planes[i] = NULL;
89 for (i = 0; i < depth; ++i)
91 rp->BitMap->Planes[i] = (PLANEPTR)AllocRaster (width, height);
92 if (rp->BitMap->Planes[i] == NULL)
94 FreeRastPort (rp, width, height);
113 if (rp->BitMap != NULL)
116 if (GfxBase->LibNode.lib_Version >= 39)
117 FreeBitMap (rp->BitMap);
122 for (i = 0; i < rp->BitMap->Depth; ++i)
124 if (rp->BitMap->Planes[i] != NULL)
125 FreeRaster (rp->BitMap->Planes[i], width, height);
127 XpmFree (rp->BitMap);
143 img = XpmMalloc (sizeof (*img));
147 img->height = height;
148 img->rp = AllocRastPort (img->width, img->height, depth);
166 FreeRastPort (ximage->rp, ximage->width, ximage->height);
181 SetAPen (ximage->rp, pixel);
182 WritePixel (ximage->rp, x, y);
191 XColor *screen_in_out,
192 unsigned long precision,
195 if (GfxBase->LibNode.lib_Version >= 39)
197 unsigned long r, g, b;
199 r = screen_in_out->red * 0x00010001;
200 g = screen_in_out->green * 0x00010001;
201 b = screen_in_out->blue * 0x00010001;
202 screen_in_out->pixel = ObtainBestPen (colormap, r, g, b,
203 OBP_Precision, precision,
204 OBP_FailIfBad, fail_if_bad,
206 if (screen_in_out->pixel == -1)
209 QueryColor (colormap, screen_in_out);
213 XColor nearest, trial;
214 long nearest_delta, trial_delta;
217 num_cells = colormap->Count;
219 QueryColor (colormap, &nearest);
220 nearest_delta = ((((screen_in_out->red >> 8) - (nearest.red >> 8))
221 * ((screen_in_out->red >> 8) - (nearest.red >> 8)))
223 (((screen_in_out->green >> 8) - (nearest.green >> 8))
224 * ((screen_in_out->green >> 8) - (nearest.green >> 8)))
226 (((screen_in_out->blue >> 8) - (nearest.blue >> 8))
227 * ((screen_in_out->blue >> 8) - (nearest.blue >> 8))));
228 for (i = 1; i < num_cells; i++)
230 /* precision and fail_if_bad is ignored under pre V39 */
232 QueryColor (colormap, &trial);
233 trial_delta = ((((screen_in_out->red >> 8) - (trial.red >> 8))
234 * ((screen_in_out->red >> 8) - (trial.red >> 8)))
236 (((screen_in_out->green >> 8) - (trial.green >> 8))
237 * ((screen_in_out->green >> 8) - (trial.green >> 8)))
239 (((screen_in_out->blue >> 8) - (trial.blue >> 8))
240 * ((screen_in_out->blue >> 8) - (trial.blue >> 8))));
241 if (trial_delta < nearest_delta)
244 nearest_delta = trial_delta;
247 screen_in_out->pixel = nearest.pixel;
248 screen_in_out->red = nearest.red;
249 screen_in_out->green = nearest.green;
250 screen_in_out->blue = nearest.blue;
260 unsigned long *pixels,
263 if (GfxBase->LibNode.lib_Version >= 39)
267 for (i = 0; i < npixels; i++)
268 ReleasePen (colormap, pixels[i]);
278 XColor *exact_def_return)
285 spec_length = strlen(spec);
291 hexlen = (spec_length - 1) / 3;
292 if (hexlen < 1 || hexlen > 4 || hexlen * 3 != spec_length - 1)
295 hexstr[hexlen] = '\0';
296 strncpy (hexstr, spec + 1, hexlen);
297 exact_def_return->red = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
298 strncpy (hexstr, spec + 1 + hexlen, hexlen);
299 exact_def_return->green = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
300 strncpy (hexstr, spec + 1 + 2 * hexlen, hexlen);
301 exact_def_return->blue = strtoul (hexstr, NULL, 16) << (16 - 4*hexlen);
308 int items, red, green, blue;
309 char line[512], name[512];
310 Bool success = False;
312 rgbf = fopen ("LIBS:rgb.txt", "r");
316 while (fgets(line, sizeof (line), rgbf) && !success)
318 items = sscanf (line, "%d %d %d %[^\n]\n",
319 &red, &green, &blue, name);
323 if (red < 0 || red > 0xFF
324 || green < 0 || green > 0xFF
325 || blue < 0 || blue > 0xFF)
330 if (0 == xpmstrcasecmp (spec, name))
332 exact_def_return->red = red * 0x0101;
333 exact_def_return->green = green * 0x0101;
334 exact_def_return->blue = blue * 0x0101;
350 if (GfxBase->LibNode.lib_Version >= 39)
352 unsigned long rgb[3];
354 GetRGB32 (colormap, def_in_out->pixel, 1, rgb);
355 def_in_out->red = rgb[0] >> 16;
356 def_in_out->green = rgb[1] >> 16;
357 def_in_out->blue = rgb[2] >> 16;
363 rgb = GetRGB4 (colormap, def_in_out->pixel);
364 def_in_out->red = ((rgb >> 8) & 0xF) * 0x1111;
365 def_in_out->green = ((rgb >> 4) & 0xF) * 0x1111;
366 def_in_out->blue = (rgb & 0xF) * 0x1111;
381 for (i = 0; i < ncolors; i++)
382 QueryColor (colormap, &defs_in_out[i]);