1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <X11/Xlib.h>
5 #include <X11/Xutil.h>
6 #include <X11/xpm.h>
7
8 #include "sunclock.h"
9
10 extern Display * dpy;
11 extern Visual * visual;
12 extern Colormap tmp_cmap;
13
14 extern int scr;
15 extern int color_depth;
16 extern int color_pad;
17 extern int bytes_per_pixel;
18 extern int verbose;
19
20 extern char * salloc();
21
22 int
readXPM(path,Context)23 readXPM(path, Context)
24 char * path;
25 struct Sundata * Context;
26 {
27 XpmAttributes attrib;
28 XImage *image = NULL;
29 int b, c, i, j, k, ip, jp;
30
31 attrib.colormap = tmp_cmap;
32 attrib.valuemask = XpmColormap | XpmReturnPixels;
33
34 if (verbose)
35 fprintf(stderr, "Loading XPM image file %s\n", path);
36 if (XpmReadFileToImage(dpy, path, &image, NULL, &attrib)) {
37 fprintf(stderr, "Cannot read XPM file %s,\nalthough it exists !!\n",
38 path);
39 return 1;
40 }
41 if (image)
42 bytes_per_pixel = image->bits_per_pixel/8;
43 else
44 return 4;
45
46 Context->xim = XCreateImage(dpy, visual,
47 DefaultDepth(dpy, scr), ZPixmap,0, NULL,
48 Context->geom.width, Context->geom.height, color_pad,0);
49 XFlush(dpy);
50 if (!Context->xim) return 4;
51 Context->xim->data = (char *) salloc(Context->xim->bytes_per_line
52 * Context->xim->height);
53 if (!Context->xim->data) return 4;
54 Context->ncolors = attrib.npixels;
55 if (color_depth<=8)
56 for (j=0; j<attrib.npixels; j++)
57 Context->daypixel[j] = (unsigned char) attrib.pixels[j];
58 for (j=0; j<Context->geom.height; j++) {
59 jp = ((j+Context->zoom.dy) * image->height)/Context->zoom.height;
60 for (i=0; i<Context->geom.width; i++) {
61 ip = ((i+Context->zoom.dx) * image->width)/Context->zoom.width;
62 b = i*bytes_per_pixel + j*Context->xim->bytes_per_line;
63 c = ip*bytes_per_pixel + jp*image->bytes_per_line;
64 for (k=0; k<bytes_per_pixel; k++)
65 Context->xim->data[b+k] = image->data[c+k];
66 }
67 }
68 XDestroyImage(image);
69 return 0;
70 }
71
72
73
74