2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * This module is all original code
20 * Copyright 1993, Robert Nation
21 * You may use this code for any purpose, as long as the original
22 * copyright remains in the source code and all documentation
25 Changed 02/12/97 by Dan Espen:
26 - added routines to determine color closeness, for color use reduction.
27 Some of the logic comes from pixy2, so the copyright is below.
30 * Copyright 1996, Romano Giannetti. No guarantees or warantees or anything
31 * are provided or implied in any way whatsoever. Use this program at your
32 * own risk. Permission to use this program for any purpose is given,
33 * as long as the copyright is kept intact.
35 * Romano Giannetti - Dipartimento di Ingegneria dell'Informazione
36 * via Diotisalvi, 2 PISA
37 * mailto:romano@iet.unipi.it
38 * http://www.iet.unipi.it/~romano
44 * Routines to handle initialization, loading, and removing of xpm's or mono-
58 #include <X11/Xutil.h>
59 #include <X11/Intrinsic.h>
65 #include "PictureUtils.h"
68 static FvwmPicture
*FvwmPictureList
=NULL
;
70 FvwmPicture
*PGetFvwmPicture(
71 Display
*dpy
, Window win
, char *ImagePath
, const char *name
,
72 FvwmPictureAttributes fpa
)
74 char *path
= PictureFindImageFile(name
, ImagePath
, R_OK
);
81 p
= PImageLoadFvwmPictureFromFile(dpy
, win
, path
, fpa
);
90 void PFreeFvwmPictureData(FvwmPicture
*p
)
96 if (p
->alloc_pixels
!= NULL
)
98 free(p
->alloc_pixels
);
109 FvwmPicture
*PCacheFvwmPicture(
110 Display
*dpy
, Window win
, char *ImagePath
, const char *name
,
111 FvwmPictureAttributes fpa
)
115 FvwmPicture
*p
= FvwmPictureList
;
117 /* First find the full pathname */
118 if ((path
= PictureFindImageFile(name
, ImagePath
, R_OK
)) == NULL
)
122 /* Remove any svg rendering options from real_path */
123 if (USE_SVG
&& *path
== ':' &&
124 (real_path
= strchr(path
+ 1, ':')))
133 /* See if the picture is already cached */
136 register char *p1
, *p2
;
138 for (p1
= path
, p2
= p
->name
; *p1
&& *p2
; ++p1
, ++p2
)
146 /* If we have found a picture with the wanted name and stamp */
147 if (!*p1
&& !*p2
&& !isFileStampChanged(&p
->stamp
, real_path
)
148 && PICTURE_FPA_AGREE(p
,fpa
))
150 p
->count
++; /* Put another weight on the picture */
157 /* Not previously cached, have to load it ourself. Put it first in list
159 p
= PImageLoadFvwmPictureFromFile(dpy
, win
, path
, fpa
);
162 p
->next
=FvwmPictureList
;
173 void PDestroyFvwmPicture(Display
*dpy
, FvwmPicture
*p
)
175 FvwmPicture
*q
= FvwmPictureList
;
181 /* Remove a weight */
184 /* still too heavy? */
189 if (p
->alloc_pixels
!= NULL
)
191 if (p
->nalloc_pixels
!= 0)
194 dpy
, Pcmap
, p
->alloc_pixels
, p
->nalloc_pixels
,
197 free(p
->alloc_pixels
);
205 XFreePixmap(dpy
,p
->picture
);
209 XFreePixmap(dpy
,p
->mask
);
213 XFreePixmap(dpy
, p
->alpha
);
215 /* Link it out of the list (it might not be there) */
216 if(p
==q
) /* in head? simple */
218 FvwmPictureList
= p
->next
;
222 while(q
&& q
->next
!=p
) /* fast forward until end or found */
226 if(q
) /* not end? means we found it in there, possibly at end */
228 q
->next
= p
->next
; /* link around it */
236 FvwmPicture
*PLoadFvwmPictureFromPixmap(
237 Display
*dpy
, Window win
, char *name
, Pixmap pixmap
,
238 Pixmap mask
, Pixmap alpha
, int width
, int height
, int nalloc_pixels
,
239 Pixel
*alloc_pixels
, int no_limit
)
243 q
= (FvwmPicture
*)safemalloc(sizeof(FvwmPicture
));
244 memset(q
, 0, sizeof(FvwmPicture
));
255 q
->nalloc_pixels
= nalloc_pixels
;
256 q
->alloc_pixels
= alloc_pixels
;
257 q
->no_limit
= no_limit
;
261 FvwmPicture
*PCacheFvwmPictureFromPixmap(
262 Display
*dpy
, Window win
, char *name
, Pixmap pixmap
,
263 Pixmap mask
, Pixmap alpha
, int width
, int height
, int nalloc_pixels
,
264 Pixel
*alloc_pixels
, int no_limit
)
266 FvwmPicture
*p
= FvwmPictureList
;
268 /* See if the picture is already cached */
269 for(; p
!= NULL
; p
= p
->next
)
272 /* at th present time no good way to cache a pixmap */
273 if (!strcmp(p
->name
,name
))
281 /* Not previously cached, have to load. Put it first in list */
282 p
= PLoadFvwmPictureFromPixmap(
283 dpy
, win
, name
, pixmap
, mask
, alpha
, width
, height
,
284 nalloc_pixels
, alloc_pixels
, no_limit
);
287 p
->next
= FvwmPictureList
;
294 FvwmPicture
*PCloneFvwmPicture(FvwmPicture
*pic
)
304 void PicturePrintImageCache(int verbose
)
307 unsigned int count
= 0;
308 unsigned int hits
= 0;
309 unsigned int num_alpha
= 0;
310 unsigned int num_mask
= 0;
314 fprintf(stderr
, "fvwm info on Image cache:\n");
316 for (p
= FvwmPictureList
; p
!= NULL
; p
= p
->next
)
324 if (p
->alpha
!= None
)
332 "Image: %s (%d pixmaps; used %d times)\n",
333 p
->name
, num_pixmaps
, p
->count
);
339 fprintf(stderr
, "%u images in cache (%d reuses) "
340 "(%u masks, %u alpha channels => %u pixmaps)\n",
341 count
, hits
, num_mask
, num_alpha
, count
+ num_mask
+ num_alpha
);