]>
Commit | Line | Data |
---|---|---|
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 | /* | |
37 | extern "C" | |
38 | { | |
39 | #include "XpmI.h" | |
40 | #include "macx.h" | |
41 | } ; | |
42 | ||
43 | XImage * AllocXImage ( | |
44 | unsigned int width, | |
45 | unsigned int height, | |
46 | unsigned int depth) | |
47 | { | |
48 | XImage *img; | |
49 | ||
50 | img = (XImage*) XpmMalloc (sizeof (*img)); | |
51 | if (img != NULL) | |
52 | { | |
53 | Rect rect = { 0 , 0 , height , width } ; | |
54 | ||
55 | img->width = width; | |
56 | img->height = height; | |
57 | ||
58 | ||
59 | NewGWorld( &img->gworldptr , depth , &rect , NULL , NULL , 0 ) ; | |
60 | if (img->gworldptr == NULL) | |
61 | { | |
62 | XDestroyImage (img); | |
63 | return NULL; | |
64 | } | |
65 | } | |
66 | ||
67 | return img; | |
68 | } | |
69 | ||
70 | ||
71 | int XDestroyImage ( XImage *ximage) | |
72 | { | |
73 | if (ximage != NULL) | |
74 | { | |
75 | if ( ximage->gworldptr ) | |
76 | DisposeGWorld( ximage->gworldptr ) ; | |
77 | ||
78 | XpmFree (ximage); | |
79 | } | |
80 | ||
81 | return Success; | |
82 | } | |
83 | ||
84 | Status XAllocColor( Display dpy, Colormap colormap, XColor *c ) | |
85 | { | |
86 | unsigned long pixel; | |
87 | unsigned short red, green, blue; | |
88 | c->pixel = ( ( (c->red)>>8 ) << 16 ) + ( ( (c->green)>>8 ) << 8 ) + ( ( (c->blue)>>8 ) ) ; | |
89 | return Success; | |
90 | } | |
91 | ||
92 | int XFreeColors(Display dpy, Colormap colormap, unsigned long *pixels, | |
93 | int npixels, int planes) | |
94 | { | |
95 | return Success; | |
96 | } | |
97 | ||
98 | int XParseColor(Display dpy, Colormap colormap, char *spec, XColor *exact_def_return) | |
99 | { | |
100 | int spec_length; | |
101 | ||
102 | if (spec == 0) | |
103 | return False; | |
104 | ||
105 | spec_length = strlen(spec); | |
106 | if (spec[0] == '#') | |
107 | { | |
108 | int hexlen; | |
109 | char hexstr[10]; | |
110 | ||
111 | hexlen = (spec_length - 1) / 3; | |
112 | if (hexlen < 1 || hexlen > 4 || hexlen * 3 != spec_length - 1) | |
113 | return False; | |
114 | ||
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); | |
122 | ||
123 | return True; | |
124 | } | |
125 | else | |
126 | { | |
127 | } | |
128 | return FALSE ; | |
129 | } | |
130 | ||
131 | int XQueryColor(Display dpy, Colormap colormap, XColor *c) | |
132 | { | |
133 | c->pixel = ( ( (c->red)>>8 ) << 16 ) + ( ( (c->green)>>8 ) << 8 ) + ( ( (c->blue)>>8 ) ) ; | |
134 | ||
135 | if (GfxBase->LibNode.lib_Version >= 39) | |
136 | { | |
137 | unsigned long rgb[3]; | |
138 | ||
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; | |
143 | } | |
144 | else | |
145 | { | |
146 | unsigned short rgb; | |
147 | ||
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; | |
152 | } | |
153 | ||
154 | return Success; | |
155 | } | |
156 | ||
157 | int XQueryColors(Display dpy, Colormap colormap, XColor *defs_in_out, int ncolors) | |
158 | { | |
159 | int i; | |
160 | ||
161 | for (i = 0; i < ncolors; i++) | |
162 | XQueryColor (dpy, colormap , &defs_in_out[i]); | |
163 | ||
164 | return Success; | |
165 | } | |
166 | ||
167 | int XPutPixel ( XImage *ximage,int x,int y,unsigned long pixel) | |
168 | { | |
169 | ||
170 | CGrafPtr origPort ; | |
171 | GDHandle origDev ; | |
172 | ||
173 | GetGWorld( &origPort , &origDev ) ; | |
174 | SetGWorld( ximage->gworldptr , NULL ) ; | |
175 | RGBColor color ; | |
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 ) ; | |
184 | return Success; | |
185 | } | |
186 | */ | |
187 | /* | |
188 | ||
189 | static struct RastPort * | |
190 | AllocRastPort (unsigned int, unsigned int, unsigned int); | |
191 | static void | |
192 | FreeRastPort (struct RastPort *, unsigned int,unsigned int); | |
193 | ||
194 | ||
195 | static struct RastPort * | |
196 | AllocRastPort ( | |
197 | unsigned int width, | |
198 | unsigned int height, | |
199 | unsigned int depth) | |
200 | { | |
201 | struct RastPort *rp; | |
202 | ||
203 | rp = XpmMalloc (sizeof (*rp)); | |
204 | if (rp != NULL) | |
205 | { | |
206 | InitRastPort (rp); | |
207 | if (GfxBase->LibNode.lib_Version >= 39) | |
208 | { | |
209 | rp->BitMap = AllocBitMap (width, height, depth, BMF_CLEAR, NULL); | |
210 | if (rp->BitMap == NULL) | |
211 | { | |
212 | FreeRastPort (rp, width, height); | |
213 | return NULL; | |
214 | } | |
215 | } | |
216 | else | |
217 | { | |
218 | unsigned int i; | |
219 | ||
220 | rp->BitMap = XpmMalloc (sizeof (*rp->BitMap)); | |
221 | if (rp->BitMap == NULL) | |
222 | { | |
223 | FreeRastPort (rp, width, height); | |
224 | return NULL; | |
225 | } | |
226 | ||
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) | |
231 | { | |
232 | rp->BitMap->Planes[i] = (PLANEPTR)AllocRaster (width, height); | |
233 | if (rp->BitMap->Planes[i] == NULL) | |
234 | { | |
235 | FreeRastPort (rp, width, height); | |
236 | return NULL; | |
237 | } | |
238 | } | |
239 | } | |
240 | } | |
241 | ||
242 | return rp; | |
243 | } | |
244 | ||
245 | ||
246 | static void | |
247 | FreeRastPort ( | |
248 | struct RastPort *rp, | |
249 | unsigned int width, | |
250 | unsigned int height) | |
251 | { | |
252 | if (rp != NULL) | |
253 | { | |
254 | if (rp->BitMap != NULL) | |
255 | { | |
256 | WaitBlit (); | |
257 | if (GfxBase->LibNode.lib_Version >= 39) | |
258 | FreeBitMap (rp->BitMap); | |
259 | else | |
260 | { | |
261 | unsigned int i; | |
262 | ||
263 | for (i = 0; i < rp->BitMap->Depth; ++i) | |
264 | { | |
265 | if (rp->BitMap->Planes[i] != NULL) | |
266 | FreeRaster (rp->BitMap->Planes[i], width, height); | |
267 | } | |
268 | XpmFree (rp->BitMap); | |
269 | } | |
270 | } | |
271 | XpmFree (rp); | |
272 | } | |
273 | } | |
274 | ||
275 | int | |
276 | XPutPixel ( | |
277 | XImage *ximage, | |
278 | int x, | |
279 | int y, | |
280 | unsigned long pixel) | |
281 | { | |
282 | SetAPen (ximage->rp, pixel); | |
283 | WritePixel (ximage->rp, x, y); | |
284 | ||
285 | return Success; | |
286 | } | |
287 | ||
288 | ||
289 | Status | |
290 | AllocBestPen ( | |
291 | Colormap colormap, | |
292 | XColor *screen_in_out, | |
293 | unsigned long precision, | |
294 | Bool fail_if_bad) | |
295 | { | |
296 | if (GfxBase->LibNode.lib_Version >= 39) | |
297 | { | |
298 | unsigned long r, g, b; | |
299 | ||
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, | |
306 | TAG_DONE); | |
307 | if (screen_in_out->pixel == -1) | |
308 | return False; | |
309 | ||
310 | QueryColor (colormap, screen_in_out); | |
311 | } | |
312 | else | |
313 | { | |
314 | XColor nearest, trial; | |
315 | long nearest_delta, trial_delta; | |
316 | int num_cells, i; | |
317 | ||
318 | num_cells = colormap->Count; | |
319 | nearest.pixel = 0; | |
320 | QueryColor (colormap, &nearest); | |
321 | nearest_delta = ((((screen_in_out->red >> 8) - (nearest.red >> 8)) | |
322 | * ((screen_in_out->red >> 8) - (nearest.red >> 8))) | |
323 | + | |
324 | (((screen_in_out->green >> 8) - (nearest.green >> 8)) | |
325 | * ((screen_in_out->green >> 8) - (nearest.green >> 8))) | |
326 | + | |
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++) | |
330 | { | |
331 | // precision and fail_if_bad is ignored under pre V39 | |
332 | trial.pixel = i; | |
333 | QueryColor (colormap, &trial); | |
334 | trial_delta = ((((screen_in_out->red >> 8) - (trial.red >> 8)) | |
335 | * ((screen_in_out->red >> 8) - (trial.red >> 8))) | |
336 | + | |
337 | (((screen_in_out->green >> 8) - (trial.green >> 8)) | |
338 | * ((screen_in_out->green >> 8) - (trial.green >> 8))) | |
339 | + | |
340 | (((screen_in_out->blue >> 8) - (trial.blue >> 8)) | |
341 | * ((screen_in_out->blue >> 8) - (trial.blue >> 8)))); | |
342 | if (trial_delta < nearest_delta) | |
343 | { | |
344 | nearest = trial; | |
345 | nearest_delta = trial_delta; | |
346 | } | |
347 | } | |
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; | |
352 | } | |
353 | ||
354 | return True; | |
355 | } | |
356 | ||
357 | ||
358 | int | |
359 | FreePens ( | |
360 | Colormap colormap, | |
361 | unsigned long *pixels, | |
362 | int npixels) | |
363 | { | |
364 | if (GfxBase->LibNode.lib_Version >= 39) | |
365 | { | |
366 | int i; | |
367 | ||
368 | for (i = 0; i < npixels; i++) | |
369 | ReleasePen (colormap, pixels[i]); | |
370 | } | |
371 | ||
372 | return Success; | |
373 | } | |
374 | ||
375 | ||
376 | Status | |
377 | ParseColor ( | |
378 | char *spec, | |
379 | XColor *exact_def_return) | |
380 | { | |
381 | int spec_length; | |
382 | ||
383 | if (spec == 0) | |
384 | return False; | |
385 | ||
386 | spec_length = strlen(spec); | |
387 | if (spec[0] == '#') | |
388 | { | |
389 | int hexlen; | |
390 | char hexstr[10]; | |
391 | ||
392 | hexlen = (spec_length - 1) / 3; | |
393 | if (hexlen < 1 || hexlen > 4 || hexlen * 3 != spec_length - 1) | |
394 | return False; | |
395 | ||
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); | |
403 | ||
404 | return True; | |
405 | } | |
406 | else | |
407 | { | |
408 | FILE *rgbf; | |
409 | int items, red, green, blue; | |
410 | char line[512], name[512]; | |
411 | Bool success = False; | |
412 | ||
413 | rgbf = fopen ("LIBS:rgb.txt", "r"); | |
414 | if (rgbf == NULL) | |
415 | return False; | |
416 | ||
417 | while (fgets(line, sizeof (line), rgbf) && !success) | |
418 | { | |
419 | items = sscanf (line, "%d %d %d %[^\n]\n", | |
420 | &red, &green, &blue, name); | |
421 | if (items != 4) | |
422 | continue; | |
423 | ||
424 | if (red < 0 || red > 0xFF | |
425 | || green < 0 || green > 0xFF | |
426 | || blue < 0 || blue > 0xFF) | |
427 | { | |
428 | continue; | |
429 | } | |
430 | ||
431 | if (0 == xpmstrcasecmp (spec, name)) | |
432 | { | |
433 | exact_def_return->red = red * 0x0101; | |
434 | exact_def_return->green = green * 0x0101; | |
435 | exact_def_return->blue = blue * 0x0101; | |
436 | success = True; | |
437 | } | |
438 | } | |
439 | fclose (rgbf); | |
440 | ||
441 | return success; | |
442 | } | |
443 | } | |
444 | ||
445 | ||
446 | int | |
447 | QueryColor ( | |
448 | Colormap colormap, | |
449 | XColor *def_in_out) | |
450 | { | |
451 | if (GfxBase->LibNode.lib_Version >= 39) | |
452 | { | |
453 | unsigned long rgb[3]; | |
454 | ||
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; | |
459 | } | |
460 | else | |
461 | { | |
462 | unsigned short rgb; | |
463 | ||
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; | |
468 | } | |
469 | ||
470 | return Success; | |
471 | } | |
472 | ||
473 | ||
474 | int | |
475 | QueryColors ( | |
476 | Colormap colormap, | |
477 | XColor *defs_in_out, | |
478 | int ncolors) | |
479 | { | |
480 | int i; | |
481 | ||
482 | for (i = 0; i < ncolors; i++) | |
483 | QueryColor (colormap, &defs_in_out[i]); | |
484 | ||
485 | return Success; | |
486 | } | |
487 | ||
488 | */ |