summaryrefslogtreecommitdiffstats
path: root/utils/minixpm.c
blob: 997e6284066eaeec6981529aa603f5fc9daaf8b7 (plain) (blame)
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
/* xscreensaver, Copyright (c) 2001-2014 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

/* I implemented this subset of libXPM here because I don't want the
   xscreensaver daemon to depend on libXPM for two reasons: first,
   because I want the logo to show up even if libXPM is not installed
   on the system; and second, I don't want to have to security-audit
   libXPM.  The fewer libraries that are linked into the xscreensaver
   daemon, the more likely to be secure it is.

   Also, the Cocoa port uses this code since libXPM isn't available
   by default on MacOS.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef HAVE_JWXYZ
# include "jwxyz.h"
#else  /* real Xlib */
# include <X11/Xlib.h>
# include <X11/Xutil.h>
#endif /* !HAVE_JWXYZ */

#include "minixpm.h"

extern const char *progname;

static Bool
bigendian (void)
{
  union { int i; char c[sizeof(int)]; } u;
  u.i = 1;
  return !u.c[0];
}

static const char hex[128] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
                              0, 10,11,12,13,14,15,0, 0, 0, 0, 0, 0, 0, 0, 0,
                              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                              0, 10,11,12,13,14,15,0, 0, 0, 0, 0, 0, 0, 0, 0,
                              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

XImage *
minixpm_to_ximage (Display *dpy, Visual *visual, Colormap colormap, int depth,
                   unsigned long transparent_color,
                   const char * const * data,
                   int *width_ret, int *height_ret,
                   unsigned long **pixels_ret, int *npixels_ret,
                   unsigned char **mask_ret)
{
  int w, w8, h, ncolors, nbytes;
  char c;
  int x, y, i, pixel_count;
  struct {
    char byte;
    int cr; int cg; int cb;
    int mr; int mg; int mb;
  } cmap[256];
  unsigned char rmap[256];

  unsigned long *pixels;
  XImage *ximage = 0;
  
  memset (cmap, 0, sizeof(cmap)); /* avoid warnings */

  if (4 != sscanf ((const char *) *data,
                   "%d %d %d %d %c", &w, &h, &ncolors, &nbytes, &c)) {
    fprintf (stderr, "%s: unparsable XPM header\n", progname);
    abort();
  }

  if (ncolors < 1 || ncolors > 255) {
    fprintf (stderr, "%s: XPM: ncolors is %d\n", progname, ncolors);
    abort();
  }
  if (nbytes != 1) {
    fprintf (stderr, "%s: %d-byte XPM files not supported\n",
             progname, nbytes);
    abort();
  }
  data++;

  for (i = 0; i < ncolors; i++)
    {
      const char *line = *data;
      cmap[i].byte = *line++;
      while (*line)
        {
          int r, g, b;
          char which;
          while (*line == ' ' || *line == '\t')
            line++;
          which = *line;
          if (!which) continue;  /* whitespace at end of line */
          line++;
          if (which != 'c' && which != 'm') {
            fprintf (stderr, "%s: unknown XPM pixel type '%c' in \"%s\"\n",
                     progname, which, *data);
            abort();
          }
          while (*line == ' ' || *line == '\t')
            line++;
          if (!strncasecmp(line, "None", 4))
            {
              r = g = b = -1;
              line += 4;
            }
          else if (!strncasecmp(line, "white", 5))
            {
              r = g = b = 255;
              line += 5;
            }
          else if (!strncasecmp(line, "black", 5))
            {
              r = g = b = 0;
              line += 5;
            }
          else
            {
              if (*line != '#') {
                fprintf (stderr, "%s: unparsable XPM color spec: \"%s\"\n",
                         progname, line);
                abort();
              }
              if (*line == '#')
                line++;
              r = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
              g = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
              b = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
            }

          if (which == 'c')
            {
              cmap[i].cr = r;
              cmap[i].cg = g;
              cmap[i].cb = b;
            }
          else
            {
              cmap[i].mr = r;
              cmap[i].mg = g;
              cmap[i].mb = b;
            }
        }

      data++;
    }

  if (depth == 1) transparent_color = 1;

  pixels = (unsigned long *) calloc (ncolors+1, sizeof(*pixels));
  pixel_count = 0;
  for (i = 0; i < ncolors; i++)
    {
      if (cmap[i].cr == -1) /* transparent */
        {
          rmap[(int) cmap[i].byte] = 255;
        }
      else
        {
          XColor color;
          color.flags = DoRed|DoGreen|DoBlue;
          color.red   = (cmap[i].cr << 8) | cmap[i].cr;
          color.green = (cmap[i].cg << 8) | cmap[i].cg;
          color.blue  = (cmap[i].cb << 8) | cmap[i].cb;
          if (depth == 1 ||
              !XAllocColor (dpy, colormap, &color))
            {
              color.red   = (cmap[i].mr << 8) | cmap[i].mr;
              color.green = (cmap[i].mg << 8) | cmap[i].mg;
              color.blue  = (cmap[i].mb << 8) | cmap[i].mb;
              if (!XAllocColor (dpy, colormap, &color)) {
                fprintf (stderr, "%s: unable to allocate XPM color\n",
                         progname);
                abort();
              }
            }
          pixels[pixel_count] = color.pixel;
          rmap[(int) cmap[i].byte] = pixel_count;
          pixel_count++;
        }
    }

  ximage = XCreateImage (dpy, visual, depth,
                         (depth == 1 ? XYBitmap : ZPixmap),
                         0, 0, w, h, 8, 0);
  if (! ximage)
    {
      if (pixels) free (pixels);
      return 0;
    }

  ximage->bitmap_bit_order =
    ximage->byte_order =
    (bigendian() ? MSBFirst : LSBFirst);

  ximage->data = (char *) calloc (ximage->height, ximage->bytes_per_line);
  if (!ximage->data)
    {
      XDestroyImage (ximage);
      if (pixels) free (pixels);
      return 0;
    }

  w8 = (w + 7) / 8;
  if (mask_ret)
    {
      int s = (w8 * h) + 1;
      *mask_ret = (unsigned char *) malloc (s);
      if (!*mask_ret)
        mask_ret = 0;
      else
        memset (*mask_ret, 255, s);
    }

  for (y = 0; y < h; y++)
    {
      const char *line = *data++;
      for (x = 0; x < w; x++)
        {
          int p = rmap[(int) *line];
          line++;
          XPutPixel (ximage, x, y, (p == 255 ? transparent_color : pixels[p]));

          if (p == 255 && mask_ret)
            (*mask_ret)[(y * w8) + (x >> 3)] &= (~(1 << (x & 7)));
        }
    }

  *width_ret = w;
  *height_ret = h;
  *pixels_ret = pixels;
  *npixels_ret = pixel_count;
  return ximage;
}