http://packetstormsecurity.org/UNIX/admin/xscreensaver-3.29.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 {
55   int w, h, ncolors, nbytes;
56   char c;
57   int i;
58   struct {
59     char byte;
60     int cr; int cg; int cb;
61     int mr; int mg; int mb;
62   } cmap[256];
63   unsigned char rmap[256];
64
65   unsigned long *pixels;
66   XImage *ximage = 0;
67
68   if (4 != sscanf (*data, "%d %d %d %d %c", &w, &h, &ncolors, &nbytes, &c))
69     abort();
70   if (ncolors < 1 || ncolors > 255)
71     abort();
72   if (nbytes != 1)
73     abort();
74   data++;
75   for (i = 0; i < ncolors; i++)
76     {
77       const unsigned char *line = *data;
78       cmap[i].byte = *line++;
79       while (*line)
80         {
81           int r, g, b;
82           char which;
83           while (*line == ' ' || *line == '\t')
84             line++;
85           which = *line++;
86           if (which != 'c' && which != 'm')
87             abort();
88           while (*line == ' ' || *line == '\t')
89             line++;
90           if (!strncasecmp(line, "None", 4))
91             {
92               r = g = b = -1;
93               line += 4;
94             }
95           else
96             {
97               if (*line == '#')
98                 line++;
99               r = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
100               g = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
101               b = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
102             }
103
104           if (which == 'c')
105             {
106               cmap[i].cr = r;
107               cmap[i].cg = g;
108               cmap[i].cb = b;
109             }
110           else
111             {
112               cmap[i].mr = r;
113               cmap[i].mg = g;
114               cmap[i].mb = b;
115             }
116         }
117
118       data++;
119     }
120
121   if (depth == 1) transparent_color = 1;
122
123   pixels = (unsigned long *) calloc (ncolors+1, sizeof(*pixels));
124   for (i = 0; i < ncolors; i++)
125     {
126       if (cmap[i].cr == -1) /* transparent */
127         {
128           rmap[(int) cmap[i].byte] = 255;
129         }
130       else
131         {
132           XColor color;
133           color.flags = DoRed|DoGreen|DoBlue;
134           color.red   = (cmap[i].cr << 8) | cmap[i].cr;
135           color.green = (cmap[i].cg << 8) | cmap[i].cg;
136           color.blue  = (cmap[i].cb << 8) | cmap[i].cb;
137           if (depth == 1 ||
138               !XAllocColor (dpy, colormap, &color))
139             {
140               color.pixel = (cmap[i].mr ? 1 : 0);
141             }
142           pixels[i] = color.pixel;
143           rmap[(int) cmap[i].byte] = i;
144         }
145     }
146
147   ximage = XCreateImage (dpy, visual, depth,
148                          (depth == 1 ? XYBitmap : ZPixmap),
149                          0, 0, w, h, 8, 0);
150   if (ximage)
151     {
152       int x, y;
153       ximage->data = (char *)
154         calloc ( ximage->height, ximage->bytes_per_line);
155       for (y = 0; y < h; y++)
156         {
157           const unsigned char *line = *data++;
158           for (x = 0; x < w; x++)
159             {
160               int p = rmap[*line++];
161               XPutPixel (ximage, x, y,
162                          (p == 255 ? transparent_color : pixels[p]));
163             }
164         }
165     }
166   
167   *width_ret = w;
168   *height_ret = h;
169   *pixels_ret = pixels;
170   *npixels_ret = ncolors;
171   return ximage;
172 }
173
174
175
176 /* Draws the logo centered in the given Drawable (presumably a Pixmap.)
177    next_frame_p means randomize the flame shape.
178  */
179 Pixmap
180 xscreensaver_logo (Display *dpy, Window window, Colormap cmap,
181                    unsigned long background_color,
182                    unsigned long **pixels_ret, int *npixels_ret,
183                    Bool big_p)
184 {
185   int npixels, iw, ih;
186   unsigned long *pixels;
187   XImage *image;
188   Pixmap p = 0;
189   XWindowAttributes xgwa;
190   XGetWindowAttributes (dpy, window, &xgwa);
191
192   image = parse_xpm_data (dpy, xgwa.visual, xgwa.colormap, xgwa.depth,
193                           background_color,
194                           (big_p ? logo_180_xpm : logo_50_xpm),
195                           &iw, &ih, &pixels, &npixels);
196   if (image)
197     {
198       XGCValues gcv;
199       GC gc;
200       p = XCreatePixmap (dpy, window, iw, ih, xgwa.depth);
201       gc = XCreateGC (dpy, p, 0, &gcv);
202       XPutImage (dpy, p, gc, image, 0, 0, 0, 0, iw, ih);
203       XDestroyImage (image);
204       XFreeGC (dpy, gc);
205     }
206   return p;
207 }