[go: up one dir, main page]

Menu

[f2b791]: / src / image.c  Maximize  Restore  History

Download this file

831 lines (697 with data), 23.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/* $Id$
* Copyright (C) 1997-1999, Maciej Stachowiak and Greg J. Badros
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING.GPL. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <X11/Intrinsic.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_LIBXPM
#include <X11/xpm.h>
#endif
#include <guile/gh.h>
#define IMAGE_IMPLEMENTATION
#include "image.h"
#include "scwm.h"
#include "screen.h"
#include "callbacks.h"
#include "scwmpaths.h"
#include "guile-compat.h"
#include "xmisc.h"
#ifdef USE_DMALLOC
#include "dmalloc.h"
#endif
#ifdef USE_IMLIB
ImlibData *imlib_data;
#endif
static Colormap ImageColorMap;
/* MS:FIXME:: arbitrary choices for these sizes - I need to check out if
* there is a good way to have hash tables grow automatically.
*/
#define IMAGE_HASH_SIZE 41
#define IMAGE_LOADER_HASH_SIZE 11
/*
* A weak value hash table from image name strings to image objects.
* The hash table will maintain associations as long as long as the
* value (the image object) is otherwise gc-protected, but will remove
* the association and let the object be freed if no other references
* are held anywhere.
*/
/*
* MS:FIXME:: This architecture prevents new images from shadowing old
* ones during a single invocation if the old image is currently in
* core, but this can be fixed by adding some of the info from stat()
* on the image file to the image struct.
*/
static SCM image_hash_table = SCM_UNDEFINED;
/*
* The hash table of image loader procedures. This one is not
* weak; we don't want image loader procedures to be free-able until
* and unless they get unregistered.
*/
static SCM image_loader_hash_table = SCM_UNDEFINED;
/* The location of the value of the image-load-path variable. */
static SCM *pscm_image_load_path;
SCWM_HOOK(image_not_found_hook, "image-not-found-hook", 1);
/** Called with image name as a string when not found. */
static SCM str_default;
static SCM str_empty;
SCM_SYMBOL (sym_filename,"filename");
SCM_SYMBOL (sym_width,"width");
SCM_SYMBOL (sym_height,"height");
SCM_SYMBOL (sym_depth,"depth");
SCM_SYMBOL (sym_pixmap,"pixmap");
SCM_SYMBOL (sym_mask,"mask");
/**CONCEPT: Images
Images are first-class objects. However, anywhere that an image is
taken as an argument, a string containing a filename will also be
accepted, and will be automatically converted to the proper image
object by loading the image. Using the same image filename more than
once is not inefficient, as caching ensures that image objects are
shared.
*/
size_t
free_scwmimage(SCM obj)
{
scwm_image *si = IMAGE(obj);
if (!si)
return 0;
#ifdef USE_IMLIB
if (si->image != None)
Imlib_free_pixmap(imlib_data, si->image);
if (si->im)
Imlib_destroy_image(imlib_data, si->im);
#else
if (!si->foreign) {
if (si->image != None) {
XFreePixmap(dpy, si->image);
}
if (si->mask != None) {
XFreePixmap(dpy, si->mask);
}
}
#endif
FREE(si);
return (0);
}
int
print_scwmimage(SCM obj, SCM port, scm_print_state *ARG_IGNORE(pstate))
{
scm_puts("#<image ", port);
scm_write(IMAGE(obj)->full_name, port);
scm_putc('>', port);
return 1;
}
SCM
mark_scwmimage(SCM obj)
{
GC_MARK_SCM_IF_SET(IMAGE(obj)->full_name);
return SCM_BOOL_F;
}
SCWM_PROC(image_p, "image?", 1, 0, 0,
(SCM obj))
/** Returns #t if OBJ is an image object, otherwise #f. */
#define FUNC_NAME s_image_p
{
return SCM_BOOL_FromBool(IMAGE_P(obj));
}
#undef FUNC_NAME
SCWM_PROC(image_properties, "image-properties", 1, 0, 0,
(SCM image))
/** Return an association list giving some properties of IMAGE.
Currently defined properties are 'filename, the fully expanded
pathname of the image, 'width, its width, 'height, its height, and
depth, its color depth.
*/
#define FUNC_NAME s_image_properties
{
scwm_image *psimg;
VALIDATE_ARG_IMAGE_COPY(1,image,psimg);
return gh_list(gh_cons(sym_filename,psimg->full_name),
gh_cons(sym_width,gh_int2scm(psimg->width)),
gh_cons(sym_height,gh_int2scm(psimg->height)),
gh_cons(sym_depth,gh_int2scm(psimg->depth)),
gh_cons(sym_pixmap,gh_int2scm(psimg->image)),
gh_cons(sym_mask,gh_int2scm(psimg->mask)),
SCM_UNDEFINED);
}
#undef FUNC_NAME
/* Free returned value using FREE */
char *
SzNewImageShortName(scwm_image *psimg)
{
char *sz = gh_scm2newstr(psimg->full_name,NULL);
char *pch = strrchr(sz,'/');
char *szAnswer;
if (!pch) pch = sz;
else pch++;
szAnswer = strdup(pch);
gh_free(sz);
return szAnswer;
}
SCWM_PROC(image_short_name, "image-short-name", 1, 0, 0,
(SCM image))
/** Return the short name of IMAGE.
Use `image-properties' to access other properties of IMAGE
including its full name. */
#define FUNC_NAME s_image_short_name
{
scwm_image *psimg;
SCM answer;
char *sz;
VALIDATE_ARG_IMAGE_COPY(1,image,psimg);
sz = SzNewImageShortName(psimg);
answer = gh_str02scm(sz);
gh_free(sz);
return answer;
}
#undef FUNC_NAME
SCWM_PROC(image_size, "image-size", 1, 0, 0,
(SCM image))
/** Return the size of IMAGE as a list (width height). */
#define FUNC_NAME s_image_size
{
scwm_image *psimg;
VALIDATE_ARG_IMAGE_COPY(1,image,psimg);
return gh_list(gh_int2scm(psimg->width),gh_int2scm(psimg->height),SCM_UNDEFINED);
}
#undef FUNC_NAME
SCM
make_empty_image(SCM name)
{
SCM result;
scwm_image *ci;
ci = NEW(scwm_image);
ci->full_name = name;
#ifdef USE_IMLIB
ci->im = 0;
#endif
ci->image = None;
ci->mask = None;
ci->height = 0;
ci->width = 0;
ci->depth = 0;
ci->foreign = 0;
SCWM_NEWCELL_SMOB(result, scm_tc16_scwm_scwmimage, ci);
return result;
}
/**CONCEPT: Image Loaders
Different loaders are available for various images types. `load-xbm'
and `load-xpm' load X bitmaps and X pixmaps respectively. The user may
register other image loaders using the extension or the special string
"default" for the loader to be tried for an image that cannot be
loaded any other way.
*/
#ifndef USE_IMLIB
SCWM_PROC(load_xbm, "load-xbm", 1, 0, 0,
(SCM full_path))
/** Load an X Bitmap file identified by the pathname FULL-PATH. */
#define FUNC_NAME s_load_xbm
{
SCM result;
scwm_image *ci;
int ignore;
char *c_path;
int xbm_return;
result=make_empty_image(full_path);
ci=IMAGE(result);
c_path = gh_scm2newstr(full_path,&ignore);
xbm_return=XReadBitmapFile(dpy, Scr.Root, c_path,
&ci->width, &ci->height, &ci->image,
&ignore, &ignore);
if (xbm_return == BitmapSuccess) {
ci->depth = 0;
ci->mask = None;
} else {
/* warn that the image could not be loaded, then drop the result
on the floor and let GC clean it up. */
scwm_msg(WARN,FUNC_NAME,"Could not load bitmap `%s'",c_path);
result = SCM_BOOL_F;
}
gh_free(c_path);
return result;
}
#undef FUNC_NAME
#ifdef HAVE_LIBXPM
SCWM_PROC(load_xpm, "load-xpm", 1, 0, 0,
(SCM full_path))
/** Load an X Pixmap file identified by the pathname FULL-PATH. */
#define FUNC_NAME s_load_xpm
{
SCM result;
scwm_image *ci;
int ignore;
char *c_path;
XpmAttributes xpm_attributes;
int xpm_return;
result=make_empty_image(full_path);
ci=IMAGE(result);
c_path = gh_scm2newstr(full_path,&ignore);
xpm_attributes.colormap = ImageColorMap;
/* MS:FIXME:: the Xpm manual suggests that a closeness of 40000 is
usually best. 655535 will allow any color to match, thus
preventing failure solely due to color allocation problems. 0
presumably requires an exact match. This should be configurable
to some degree, but shouldn't be too high a priority. */
xpm_attributes.closeness = 40000;
xpm_attributes.valuemask =
XpmSize | XpmReturnPixels | XpmColormap | XpmCloseness;
xpm_return = XpmReadFileToPixmap(dpy, Scr.Root,
c_path, &ci->image, &ci->mask,
&xpm_attributes);
if (xpm_return == XpmSuccess) {
ci->width = xpm_attributes.width;
ci->height = xpm_attributes.height;
ci->depth = DefaultDepthOfScreen(DefaultScreenOfDisplay(dpy));
} else {
/* warn that the image could not be loaded, then drop the result
on the floor and let GC clean it up. */
scwm_msg(WARN,FUNC_NAME,"Could not load pixmap `%s'",c_path);
result = SCM_BOOL_F;
}
gh_free(c_path);
return result;
}
#undef FUNC_NAME
#endif /* HAVE_LIBXPM */
#else /* USE_IMLIB */
Bool
MakeScwmImageFromImlibImage(scwm_image *ci,ImlibImage *pimg)
{
ci->im = pimg;
ci->width = pimg->rgb_width;
ci->height = pimg->rgb_height;
if (!Imlib_render(imlib_data, pimg, ci->width, ci->height)) {
return False;
}
else {
ci->image = Imlib_move_image(imlib_data, pimg);
ci->mask = Imlib_move_mask(imlib_data, pimg);
ci->depth = imlib_data->x.depth;
}
return True;
}
SCWM_PROC(load_imlib_image, "load-imlib-image", 1, 0, 0,
(SCM full_path))
/** Load an image file using imlib identified by the pathname FULL-PATH. */
#define FUNC_NAME s_load_imlib_image
{
SCM result;
ImlibImage *pimg = NULL;
scwm_image *ci;
int ignore;
char *c_path;
result=make_empty_image(full_path);
ci=IMAGE(result);
c_path = gh_scm2newstr(full_path,&ignore);
pimg = Imlib_load_image(imlib_data, c_path);
if (!pimg) {
scwm_msg(WARN, FUNC_NAME, "load-imlib-image could not load image `%s'", c_path);
result = SCM_BOOL_F;
} else {
if (!MakeScwmImageFromImlibImage(ci,pimg)) {
scwm_msg(WARN, FUNC_NAME, "Imlib unable to render pixmaps from "
"image `%s'", c_path);
}
}
gh_free(c_path);
return result;
}
#undef FUNC_NAME
#endif /* USE_IMLIB */
SCWM_PROC(register_image_loader, "register-image-loader", 2, 0, 0,
(SCM extension, SCM proc))
/** Register PROC as the loader to use for images ending in EXTENSION.
EXTENSION must be a string beginning with a period, the
empty string (for files with no extension), or the string "default"
(for files that no other image loader succeeds in loading). PROC will
be called with the full pathname of the image and should return an
image object, or #f if it succeeds. */
#define FUNC_NAME s_register_image_loader
{
VALIDATE_ARG_STR(1,extension);
/* GJB:FIXME:MS: Why not check arity of procedure? */
VALIDATE_ARG_PROC(2,proc);
scm_hash_set_x(image_loader_hash_table, extension, proc);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCWM_PROC(unregister_image_loader, "unregister-image-loader", 1, 0, 0,
(SCM extension))
/** Unregister the loader, if any, for images ending in EXTENSION.
EXTENSION must be a string beginning with a period, the
empty string (for files with no extension), or the string "default"
(for files that no other image loader succeeds in loading). */
#define FUNC_NAME s_unregister_image_loader
{
VALIDATE_ARG_STR(1,extension);
scm_hash_remove_x(image_loader_hash_table, extension);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
/*SCWM_VALIDATE: name*/
SCM
path_expand_image_fname(SCM name, const char *func_name)
#define FUNC_NAME func_name
{
char *c_name, *c_fname;
int length;
SCM result;
VALIDATE_ARG_STR_NEWCOPY_LEN(1,name,c_name,length);
if ((length > 0 && c_name[0]=='/') ||
(length > 1 && c_name[0]=='.' &&
(c_name[1]=='/' || (length > 2 && c_name[1]=='.'
&& c_name[2]=='/')))) {
/* absolute path */
if (access(c_name, R_OK)==0) {
c_fname = c_name; /* alias the two names */
} else {
FREE(c_name);
return SCM_BOOL_F;
}
} else {
SCM p;
int max_path_len= 0;
/* relative path */
if (!gh_list_p(*pscm_image_load_path)) {
/* Warning, image-load-path is not a list. */
return SCM_BOOL_F;
}
/* traverse the path list to compute the max buffer size we will
need. */
/* GJB:FIXME:: ideally, we'd like to do this only after
*pscm_image_load_path changes -- maybe we could compare
against a hash of the old value before redoing this work */
for (p = *pscm_image_load_path; p != SCM_EOL; p = gh_cdr(p)) {
SCM elt = gh_car(p);
if (!gh_string_p(elt)) {
/* Warning, non-string in image-load-path */
scwm_msg(WARN,FUNC_NAME,"Non-string in image-load-path");
return SCM_BOOL_F; /* Why bail? just skip it--gjb 11/28/97 */
/* Assuming path is list of strings simplifies code below. */
} else {
int l=SCM_ROLENGTH(elt);
max_path_len= (l > max_path_len) ? l : max_path_len;
}
}
/* Add 2, one for the '/', one for the final NULL */
c_fname = NEWC(max_path_len + length + 2,char);
/* Try every possible path */
for(p = *pscm_image_load_path; p != SCM_EOL; p = gh_cdr(p)) {
SCM elt = gh_car(p);
int path_len = SCM_ROLENGTH(elt);
memcpy(c_fname, SCM_ROCHARS(elt), path_len);
c_fname[path_len] = '/';
memcpy(&(c_fname[path_len+1]), c_name, length);
c_fname[path_len+length+1] = 0;
if(access(c_fname, R_OK)==0) {
break;
}
}
if (p==SCM_EOL) {
/* warn that the file is not found. */
scwm_msg(WARN,FUNC_NAME,"Image file was not found: `%s'",c_name);
call1_hooks(image_not_found_hook,gh_str02scm(c_name));
FREE(c_name);
FREEC(c_fname);
return SCM_BOOL_F;
}
}
result = gh_str02scm(c_fname);
/* c_fname and c_name may be aliased from assignment above --
be sure to free only once */
if (c_fname != c_name)
FREE(c_fname);
FREE(c_name);
return result;
}
#undef FUNC_NAME
SCM
get_image_loader(SCM name)
{
char *c_name;
char *c_ext;
int length;
SCM result = SCM_BOOL_F;
c_name = gh_scm2newstr(name,&length);
c_ext = strchr (c_name,'.');
if (c_ext==NULL) {
scm_hash_ref (image_loader_hash_table, str_empty, SCM_BOOL_F);
} else {
while (c_ext != NULL && result == SCM_BOOL_F) {
result = scm_hash_ref (image_loader_hash_table,
gh_str02scm(c_ext), SCM_BOOL_F);
c_ext = strchr (c_ext+1,'.');
}
}
gh_free(c_name);
c_name = NULL;
if (result == SCM_BOOL_F) {
result = scm_hash_ref(image_loader_hash_table, str_default,
SCM_BOOL_F);
}
return result;
}
SCWM_PROC(make_image, "make-image", 1, 0, 0,
(SCM name))
/** Loads an image from the file NAME.
To load the image, the appropriate image loaders will be invoked as
needed. If NAME starts with "/", "./" or "../", it is treated as a
fully qualified pathname; otherwise, the image path is searched for an
appropriate file. */
#define FUNC_NAME s_make_image
{
SCM result;
SCM full_path;
SCM loader;
VALIDATE_ARG_STR(1,name);
/* First check the hash table for this image. */
result=scm_hash_ref(image_hash_table, name, SCM_BOOL_F);
if (result != SCM_BOOL_F) {
/* MS:FIXME:: should really check for up-to-date-ness here */
/* Never mind, let's just rely on the cache clear stuff */
return(result);
}
/* OK, it wasn't in the hash table - we need to expand the filename.
*/
full_path = path_expand_image_fname(name,FUNC_NAME);
if (full_path==SCM_BOOL_F) {
return (SCM_BOOL_F);
}
/* Check the cache again */
result=scm_hash_ref(image_hash_table, full_path, SCM_BOOL_F);
if (result != SCM_BOOL_F) {
scm_hash_set_x(image_hash_table, name, result);
return result;
}
/* still not there, find the right loader, and load it up. */
loader = get_image_loader(name);
if (loader != SCM_BOOL_F) {
result = scwm_safe_call1(loader, full_path);
}
if (result == SCM_BOOL_F) {
/* the load failed; try the default loader */
loader = scm_hash_ref(image_loader_hash_table, str_default, SCM_BOOL_F);
if (loader != SCM_BOOL_F) {
result = scwm_safe_call1(loader, full_path);
}
if (result == SCM_BOOL_F) {
/* Still failed, return #f and possibly warn. */
return SCM_BOOL_F;
}
}
IMAGE(result)->full_name=full_path;
/* Put it in the hash table under both the full name and the given name. */
scm_hash_set_x(image_hash_table, name, result);
scm_hash_set_x(image_hash_table, full_path, result);
return result;
}
#undef FUNC_NAME
/* GJB:FIXME:: make name == #t do a clear of all entries (ie. reset hash table). */
SCWM_PROC(clear_image_cache_entry, "clear-image-cache-entry", 1, 0, 0,
(SCM name))
/** Images are cached by both name and full pathname. It is
remotely possible that the file that should be used for a particular
name will change, for example if you alter the image file or change
your image path. For this unlikely eventuality,
`clear-image-cache-entry' is provided - it removes the image
associated with NAME from the image cache.*/
#define FUNC_NAME s_clear_image_cache_entry
{
scm_hash_remove_x(image_hash_table, name);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
#ifdef USE_IMLIB
SCWM_PROC(window_to_image,"window->image", 1, 4, 0,
(SCM win, SCM x_offset, SCM y_offset, SCM width, SCM height))
/** Return an image with the contents of window WIN.
WIN can be a window id (as a long), a window object, or
the symbol 'root-window. Captures the rectangle of the window
at X-OFFSET, Y-OFFSET with width WIDTH and height HEIGHT. */
#define FUNC_NAME s_window_to_image
{
Window w;
int x, y, wd, ht;
int win_width, win_height;
VALIDATE_ARG_WIN_ROOTSYM_OR_NUM_COPY(1,win,w);
VALIDATE_ARG_INT_COPY_USE_DEF(2,x_offset,x,0);
VALIDATE_ARG_INT_COPY_USE_DEF(3,y_offset,y,0);
VALIDATE_ARG_INT_COPY_USE_DEF(4,width,wd,-1);
VALIDATE_ARG_INT_COPY_USE_DEF(5,height,ht,-1);
FXGetWindowSize(w,&win_width,&win_height);
if (wd < 0) wd = win_width - x;
if (ht < 0) ht = win_height - y;
{ /* scope */
ImlibImage *pimg = Imlib_create_image_from_drawable(imlib_data,
w, None, x, y, wd, ht);
SCM result = make_empty_image(gh_str02scm("by window->image"));
scwm_image *ci = IMAGE(result);
if (!MakeScwmImageFromImlibImage(ci,pimg)) {
scwm_msg(WARN, FUNC_NAME, "Imlib unable to render pixmaps from window");
}
return result;
}
}
#undef FUNC_NAME
SCWM_PROC (clone_scaled_image, "clone-scaled-image", 3, 0, 0,
(SCM image, SCM width, SCM height))
/** Returns a copy of IMAGE scaled to have dimensions WIDTH by HEIGHT.
See also `clone-resized-image' from the background module. */
#define FUNC_NAME s_clone_scaled_image
{
int w, h;
VALIDATE_ARG_IMAGE(1,image);
VALIDATE_ARG_INT_COPY(2,width,w);
VALIDATE_ARG_INT_COPY(3,height,h);
{ /* scope */
ImlibImage *pimgSource = IMAGE(image)->im;
ImlibImage *pimgNew = Imlib_clone_scaled_image(imlib_data,pimgSource,w,h);
SCM result = make_empty_image(gh_str02scm("by window->image"));
scwm_image *ci = IMAGE(result);
if (!MakeScwmImageFromImlibImage(ci,pimgNew)) {
scwm_msg(WARN, FUNC_NAME, "Imlib unable to render pixmap");
}
return result;
}
}
#undef FUNC_NAME
#endif
SCM
make_image_from_pixmap(char *szDescription,
Pixmap image, Pixmap mask,
int width, int height, int depth)
{
SCM result = make_empty_image(gh_str02scm(szDescription));
scwm_image *ci = IMAGE(result);
ci->image = image;
ci->mask = mask;
ci->width = width;
ci->height = height;
ci->depth = depth;
return result;
}
void
init_image_colormap()
{
XWindowAttributes root_attributes;
XGetWindowAttributes(dpy, Scr.Root, &root_attributes);
ImageColorMap = root_attributes.colormap;
}
MAKE_SMOBFUNS(scwmimage);
void init_image()
{
#ifdef USE_IMLIB
SCM val_load_imlib_image;
#else /* !USE_IMLIB */
SCM val_load_xbm, val_load_xpm;
#endif
REGISTER_SCWMSMOBFUNS(scwmimage);
/* Save a convenient Scheme "default" string */
scm_permanent_object(str_default=gh_str02scm("default"));
/* Do the same for "" */
scm_permanent_object(str_empty=gh_str02scm(""));
/* Include registration of procedures and other things. */
#ifndef SCM_MAGIC_SNARFER
# include "image.x"
#endif
/* Initialize the image cache table. */
image_hash_table =
scm_make_weak_value_hash_table (SCM_MAKINUM(IMAGE_HASH_SIZE));
scm_permanent_object(image_hash_table);
/* Initialize the loader hash table. */
image_loader_hash_table =
gh_make_vector (SCM_MAKINUM(IMAGE_LOADER_HASH_SIZE), SCM_EOL);
scm_permanent_object(image_loader_hash_table);
/* Register the standard loaders. */
#ifdef USE_IMLIB
{ /* scope */
Screen *screen = ScreenOfDisplay(dpy, Scr.screen);
Visual *v = DefaultVisualOfScreen(screen);
ImlibInitParams imlib_params;
imlib_params.flags = PARAMS_VISUALID;
imlib_params.visualid = XVisualIDFromVisual(v);
/* init imlib */
imlib_data = Imlib_init_with_params(dpy, &imlib_params);
val_load_imlib_image = gh_lookup("load-imlib-image");
if(val_load_imlib_image == SCM_UNDEFINED) {
scwm_msg(ERR,"init_image","load-imlib-image not defined -- "
"probable build error\n consider 'rm *.x' and rebuild");
abort();
}
register_image_loader (str_empty, val_load_imlib_image);
register_image_loader (gh_str02scm("default"), val_load_imlib_image);
}
#else /* !USE_IMLIB */
val_load_xbm = gh_lookup("load-xbm");
val_load_xpm = gh_lookup("load-xpm");
if (val_load_xbm == SCM_UNDEFINED) {
scwm_msg(ERR,"init_image","load-xbm not defined -- probable build error\n\
consider 'rm *.x' and rebuild");
abort();
}
register_image_loader (str_empty, val_load_xbm);
register_image_loader (gh_str02scm(".icon"), val_load_xbm);
register_image_loader (gh_str02scm(".bitmap"), val_load_xbm);
register_image_loader (gh_str02scm(".xbm"), val_load_xbm);
if (!UNSET_SCM(val_load_xpm)) {
register_image_loader (gh_str02scm(".xpm"), val_load_xpm);
register_image_loader (gh_str02scm(".xpm.gz"), val_load_xpm);
register_image_loader (str_default, val_load_xbm);
}
#endif /* USE_IMLIB */
/* Make the image-load-path Scheme variable easily accessible from C,
and load it with a nice default value. */
SCWM_VAR_INIT(image_load_path,"image-load-path", gh_eval_str("\'"SCWM_IMAGE_LOAD_PATH));
/** List of strings of directories in which to look for image files. */
}
/* Local Variables: */
/* tab-width: 8 */
/* c-basic-offset: 2 */
/* End: */
/* vim:ts=8:sw=2:sta
*/