]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/xpm/macx.c
*** empty log message ***
[wxWidgets.git] / src / mac / xpm / macx.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 1998 Stefan Csomor
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 * 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.
20 *
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.
24 */
25
26/*****************************************************************************\
27* amigax.c: *
28* *
29* XPM library *
30* Emulates some Xlib functionality for Amiga. *
31* *
32* Developed by Lorens Younes (d93-hyo@nada.kth.se) 7/95 *
33* Revised 4/96 *
34\*****************************************************************************/
35
36#include "xpmi.h"
37//#include "macx.h"
38#include "simx.h"
39
40XImage *
41AllocXImage (
42 unsigned int width,
43 unsigned int height,
44 unsigned int depth)
45{
46 XImage *img;
47
48 img = XpmMalloc (sizeof (*img));
49 if (img != NULL)
50 {
51 Rect rect ;
52 rect.left = rect.top = 0 ;
53 rect.bottom = height ;
54 rect.right = width ;
55
56 img->width = width;
57 img->height = height;
58
59
60 NewGWorld( &img->gworldptr , depth , &rect , NULL , NULL , 0 ) ;
61 if (img->gworldptr == NULL)
62 {
63 XDestroyImage (img);
64 return NULL;
65 }
66 }
67
68 return img;
69}
70
71/*
72
73static struct RastPort *
74AllocRastPort (unsigned int, unsigned int, unsigned int);
75static void
76FreeRastPort (struct RastPort *, unsigned int,unsigned int);
77
78
79static struct RastPort *
80AllocRastPort (
81 unsigned int width,
82 unsigned int height,
83 unsigned int depth)
84{
85 struct RastPort *rp;
86
87 rp = XpmMalloc (sizeof (*rp));
88 if (rp != NULL)
89 {
90 InitRastPort (rp);
91 if (GfxBase->LibNode.lib_Version >= 39)
92 {
93 rp->BitMap = AllocBitMap (width, height, depth, BMF_CLEAR, NULL);
94 if (rp->BitMap == NULL)
95 {
96 FreeRastPort (rp, width, height);
97 return NULL;
98 }
99 }
100 else
101 {
102 unsigned int i;
103
104 rp->BitMap = XpmMalloc (sizeof (*rp->BitMap));
105 if (rp->BitMap == NULL)
106 {
107 FreeRastPort (rp, width, height);
108 return NULL;
109 }
110
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)
115 {
116 rp->BitMap->Planes[i] = (PLANEPTR)AllocRaster (width, height);
117 if (rp->BitMap->Planes[i] == NULL)
118 {
119 FreeRastPort (rp, width, height);
120 return NULL;
121 }
122 }
123 }
124 }
125
126 return rp;
127}
128
129
130static void
131FreeRastPort (
132 struct RastPort *rp,
133 unsigned int width,
134 unsigned int height)
135{
136 if (rp != NULL)
137 {
138 if (rp->BitMap != NULL)
139 {
140 WaitBlit ();
141 if (GfxBase->LibNode.lib_Version >= 39)
142 FreeBitMap (rp->BitMap);
143 else
144 {
145 unsigned int i;
146
147 for (i = 0; i < rp->BitMap->Depth; ++i)
148 {
149 if (rp->BitMap->Planes[i] != NULL)
150 FreeRaster (rp->BitMap->Planes[i], width, height);
151 }
152 XpmFree (rp->BitMap);
153 }
154 }
155 XpmFree (rp);
156 }
157}
158
159
160XImage *
161AllocXImage (
162 unsigned int width,
163 unsigned int height,
164 unsigned int depth)
165{
166 XImage *img;
167
168 img = XpmMalloc (sizeof (*img));
169 if (img != NULL)
170 {
171 img->width = width;
172 img->height = height;
173 img->rp = AllocRastPort (img->width, img->height, depth);
174 if (img->rp == NULL)
175 {
176 FreeXImage (img);
177 return NULL;
178 }
179 }
180
181 return img;
182}
183
184
185int
186FreeXImage (
187 XImage *ximage)
188{
189 if (ximage != NULL)
190 {
191 FreeRastPort (ximage->rp, ximage->width, ximage->height);
192 XpmFree (ximage);
193 }
194
195 return Success;
196}
197
198
199int
200XPutPixel (
201 XImage *ximage,
202 int x,
203 int y,
204 unsigned long pixel)
205{
206 SetAPen (ximage->rp, pixel);
207 WritePixel (ximage->rp, x, y);
208
209 return Success;
210}
211
212
213Status
214AllocBestPen (
215 Colormap colormap,
216 XColor *screen_in_out,
217 unsigned long precision,
218 Bool fail_if_bad)
219{
220 if (GfxBase->LibNode.lib_Version >= 39)
221 {
222 unsigned long r, g, b;
223
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,
230 TAG_DONE);
231 if (screen_in_out->pixel == -1)
232 return False;
233
234 QueryColor (colormap, screen_in_out);
235 }
236 else
237 {
238 XColor nearest, trial;
239 long nearest_delta, trial_delta;
240 int num_cells, i;
241
242 num_cells = colormap->Count;
243 nearest.pixel = 0;
244 QueryColor (colormap, &nearest);
245 nearest_delta = ((((screen_in_out->red >> 8) - (nearest.red >> 8))
246 * ((screen_in_out->red >> 8) - (nearest.red >> 8)))
247 +
248 (((screen_in_out->green >> 8) - (nearest.green >> 8))
249 * ((screen_in_out->green >> 8) - (nearest.green >> 8)))
250 +
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++)
254 {
255 // precision and fail_if_bad is ignored under pre V39
256 trial.pixel = i;
257 QueryColor (colormap, &trial);
258 trial_delta = ((((screen_in_out->red >> 8) - (trial.red >> 8))
259 * ((screen_in_out->red >> 8) - (trial.red >> 8)))
260 +
261 (((screen_in_out->green >> 8) - (trial.green >> 8))
262 * ((screen_in_out->green >> 8) - (trial.green >> 8)))
263 +
264 (((screen_in_out->blue >> 8) - (trial.blue >> 8))
265 * ((screen_in_out->blue >> 8) - (trial.blue >> 8))));
266 if (trial_delta < nearest_delta)
267 {
268 nearest = trial;
269 nearest_delta = trial_delta;
270 }
271 }
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;
276 }
277
278 return True;
279}
280
281
282int
283FreePens (
284 Colormap colormap,
285 unsigned long *pixels,
286 int npixels)
287{
288 if (GfxBase->LibNode.lib_Version >= 39)
289 {
290 int i;
291
292 for (i = 0; i < npixels; i++)
293 ReleasePen (colormap, pixels[i]);
294 }
295
296 return Success;
297}
298
299
300Status
301ParseColor (
302 char *spec,
303 XColor *exact_def_return)
304{
305 int spec_length;
306
307 if (spec == 0)
308 return False;
309
310 spec_length = strlen(spec);
311 if (spec[0] == '#')
312 {
313 int hexlen;
314 char hexstr[10];
315
316 hexlen = (spec_length - 1) / 3;
317 if (hexlen < 1 || hexlen > 4 || hexlen * 3 != spec_length - 1)
318 return False;
319
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);
327
328 return True;
329 }
330 else
331 {
332 FILE *rgbf;
333 int items, red, green, blue;
334 char line[512], name[512];
335 Bool success = False;
336
337 rgbf = fopen ("LIBS:rgb.txt", "r");
338 if (rgbf == NULL)
339 return False;
340
341 while (fgets(line, sizeof (line), rgbf) && !success)
342 {
343 items = sscanf (line, "%d %d %d %[^\n]\n",
344 &red, &green, &blue, name);
345 if (items != 4)
346 continue;
347
348 if (red < 0 || red > 0xFF
349 || green < 0 || green > 0xFF
350 || blue < 0 || blue > 0xFF)
351 {
352 continue;
353 }
354
355 if (0 == xpmstrcasecmp (spec, name))
356 {
357 exact_def_return->red = red * 0x0101;
358 exact_def_return->green = green * 0x0101;
359 exact_def_return->blue = blue * 0x0101;
360 success = True;
361 }
362 }
363 fclose (rgbf);
364
365 return success;
366 }
367}
368
369
370int
371QueryColor (
372 Colormap colormap,
373 XColor *def_in_out)
374{
375 if (GfxBase->LibNode.lib_Version >= 39)
376 {
377 unsigned long rgb[3];
378
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;
383 }
384 else
385 {
386 unsigned short rgb;
387
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;
392 }
393
394 return Success;
395}
396
397
398int
399QueryColors (
400 Colormap colormap,
401 XColor *defs_in_out,
402 int ncolors)
403{
404 int i;
405
406 for (i = 0; i < ncolors; i++)
407 QueryColor (colormap, &defs_in_out[i]);
408
409 return Success;
410}
411
412*/