http://packetstormsecurity.org/UNIX/admin/xscreensaver-3.32.tar.gz
[xscreensaver] / utils / logo.c
1 /* xscreensaver, Copyright (c) 2001 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 /* XScreenSaver Logo designed by Angela Goodman <rzr_grl@yahoo.com>
13  */
14
15 /* I basically implemented libXPM here, because I don't want the
16    xscreensaver daemon to depend on libXPM for two reasons: first,
17    because I want the logo to show up even if libXPM is not installed
18    on the system; and second, I don't want to have to security-audit
19    libXPM.  The fewer libraries that are linked into the xscreensaver
20    daemon, the more likely to be secure it is.
21  */
22
23 /* If you are looking in here because you're trying to figure out how to
24    change the logo that xscreensaver displays on the splash screen and
25    password dialog, please don't.  The logo is xscreensaver's identity.
26    You wouldn't alter the name or copyright notice on a program that
27    you didn't write; please don't alter its logo either.
28  */
29
30 #include "utils.h"
31 #include "resources.h"
32
33 #include <stdio.h>
34 #include <X11/Xutil.h>
35
36 #include "logo-50.xpm"
37 #include "logo-180.xpm"
38
39 static const char hex[128] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42                               0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
43                               0, 10,11,12,13,14,15,0, 0, 0, 0, 0, 0, 0, 0, 0,
44                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45                               0, 10,11,12,13,14,15,0, 0, 0, 0, 0, 0, 0, 0, 0,
46                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
47
48 static XImage *
49 parse_xpm_data (Display *dpy, Visual *visual, Colormap colormap, int depth,
50                 unsigned long transparent_color,
51                 unsigned const char * const * data,
52                 int *width_ret, int *height_ret,
53                 unsigned long **pixels_ret, int *npixels_ret,
54                 unsigned char **mask_ret)
55 {
56   int w, w8, h, ncolors, nbytes;
57   char c;
58   int i;
59   struct {
60     char byte;
61     int cr; int cg; int cb;
62     int mr; int mg; int mb;
63   } cmap[256];
64   unsigned char rmap[256];
65
66   unsigned long *pixels;
67   XImage *ximage = 0;
68
69   if (4 != sscanf (*data, "%d %d %d %d %c", &w, &h, &ncolors, &nbytes, &c))
70     abort();
71   if (ncolors < 1 || ncolors > 255)
72     abort();
73   if (nbytes != 1)
74     abort();
75   data++;
76
77   w8 = (w + 8) / 8;
78
79   if (mask_ret)
80     {
81       int s = (w8 * h) + 1;
82       *mask_ret = (char *) malloc (s);
83       if (!*mask_ret)
84         mask_ret = 0;
85       else
86         memset (*mask_ret, 255, s);
87     }
88
89   for (i = 0; i < ncolors; i++)
90     {
91       const unsigned char *line = *data;
92       cmap[i].byte = *line++;
93       while (*line)
94         {
95           int r, g, b;
96           char which;
97           while (*line == ' ' || *line == '\t')
98             line++;
99           which = *line++;
100           if (which != 'c' && which != 'm')
101             abort();
102           while (*line == ' ' || *line == '\t')
103             line++;
104           if (!strncasecmp(line, "None", 4))
105             {
106               r = g = b = -1;
107               line += 4;
108             }
109           else
110             {
111               if (*line == '#')
112                 line++;
113               r = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
114               g = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
115               b = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
116             }
117
118           if (which == 'c')
119             {
120               cmap[i].cr = r;
121               cmap[i].cg = g;
122               cmap[i].cb = b;
123             }
124           else
125             {
126               cmap[i].mr = r;
127               cmap[i].mg = g;
128               cmap[i].mb = b;
129             }
130         }
131
132       data++;
133     }
134
135   if (depth == 1) transparent_color = 1;
136
137   pixels = (unsigned long *) calloc (ncolors+1, sizeof(*pixels));
138   for (i = 0; i < ncolors; i++)
139     {
140       if (cmap[i].cr == -1) /* transparent */
141         {
142           rmap[(int) cmap[i].byte] = 255;
143         }
144       else
145         {
146           XColor color;
147           color.flags = DoRed|DoGreen|DoBlue;
148           color.red   = (cmap[i].cr << 8) | cmap[i].cr;
149           color.green = (cmap[i].cg << 8) | cmap[i].cg;
150           color.blue  = (cmap[i].cb << 8) | cmap[i].cb;
151           if (depth == 1 ||
152               !XAllocColor (dpy, colormap, &color))
153             {
154               color.pixel = (cmap[i].mr ? 1 : 0);
155             }
156           pixels[i] = color.pixel;
157           rmap[(int) cmap[i].byte] = i;
158         }
159     }
160
161   ximage = XCreateImage (dpy, visual, depth,
162                          (depth == 1 ? XYBitmap : ZPixmap),
163                          0, 0, w, h, 8, 0);
164   if (ximage)
165     {
166       int x, y;
167       ximage->data = (char *)
168         calloc ( ximage->height, ximage->bytes_per_line);
169       for (y = 0; y < h; y++)
170         {
171           const unsigned char *line = *data++;
172           for (x = 0; x < w; x++)
173             {
174               int p = rmap[*line++];
175               XPutPixel (ximage, x, y,
176                          (p == 255 ? transparent_color : pixels[p]));
177
178               if (p == 255 && mask_ret)
179                 (*mask_ret)[(y * w8) + (x >> 3)] &= (~(1 << (x % 8)));
180             }
181         }
182     }
183   
184   *width_ret = w;
185   *height_ret = h;
186   *pixels_ret = pixels;
187   *npixels_ret = ncolors;
188   return ximage;
189 }
190
191
192
193 /* Draws the logo centered in the given Drawable (presumably a Pixmap.)
194    next_frame_p means randomize the flame shape.
195  */
196 Pixmap
197 xscreensaver_logo (Display *dpy, Window window, Colormap cmap,
198                    unsigned long background_color,
199                    unsigned long **pixels_ret, int *npixels_ret,
200                    Pixmap *mask_ret,
201                    Bool big_p)
202 {
203   int npixels, iw, ih;
204   unsigned long *pixels;
205   XImage *image;
206   Pixmap p = 0;
207   XWindowAttributes xgwa;
208   unsigned char *mask = 0;
209
210   XGetWindowAttributes (dpy, window, &xgwa);
211
212   image = parse_xpm_data (dpy, xgwa.visual, xgwa.colormap, xgwa.depth,
213                           background_color,
214                           (big_p ? logo_180_xpm : logo_50_xpm),
215                           &iw, &ih, &pixels, &npixels,
216                           (mask_ret ? &mask : 0));
217   if (image)
218     {
219       XGCValues gcv;
220       GC gc;
221       p = XCreatePixmap (dpy, window, iw, ih, xgwa.depth);
222       gc = XCreateGC (dpy, p, 0, &gcv);
223       XPutImage (dpy, p, gc, image, 0, 0, 0, 0, iw, ih);
224       XDestroyImage (image);
225       XFreeGC (dpy, gc);
226
227       if (mask_ret && mask)
228         {
229           *mask_ret = XCreatePixmapFromBitmapData (dpy, window, mask,
230                                                    iw, ih, 1L, 0L, 1);
231           free (mask);
232         }
233     }
234   return p;
235 }