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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/* $XFree86: xc/programs/xcursorgen/xcursorgen.c,v 1.8 2002/11/23 02:33:20 keithp Exp $ */
/*
* xcursorgen.c
*
* Copyright (C) 2002 Manish Singh
*
* 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, and that the name of Manish Singh not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Manish Singh makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* MANISH SINGH DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL MANISH SINGH BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xcursor/Xcursor.h>
#include <png.h>
#define VERSION_STR "0.1"
struct flist
{
int size;
int xhot, yhot;
char *pngfile;
int delay;
struct flist *next;
};
static void
usage (char *name)
{
fprintf (stderr, "usage: %s [-Vh] [--version] [--help] [-p <dir>] [--prefix <dir>] [CONFIG [OUT]]\n",
name);
fprintf (stderr, "Generate an Xcursor file from a series of PNG images\n");
fprintf (stderr, "\n");
fprintf (stderr, " -V, --version display the version number and exit\n");
fprintf (stderr, " -?, --help display this message and exit\n");
fprintf (stderr, " -p, --prefix <dir> find cursor images in <dir>\n");
fprintf (stderr, "\n");
fprintf (stderr, "With no CONFIG, or when CONFIG is -, read standard input. "
"Same with OUT and\n");
fprintf (stderr, "standard output.\n");
}
static int
read_config_file (char *config, struct flist **list)
{
FILE *fp;
char line[4096], pngfile[4000];
int size, xhot, yhot, delay;
struct flist *start = NULL, *end = NULL, *curr;
int count = 0;
if (strcmp (config, "-") != 0)
{
fp = fopen (config, "r");
if (!fp)
{
*list = NULL;
return 0;
}
}
else
fp = stdin;
while (fgets (line, sizeof (line), fp) != NULL)
{
if (line[0] == '#')
continue;
switch (sscanf (line, "%d %d %d %3999s %d", &size, &xhot, &yhot, pngfile, &delay))
{
case 4:
delay = 50;
break;
case 5:
break;
default:
{
fprintf (stderr, "Bad config file data!\n");
fclose (fp);
return 0;
}
}
curr = malloc (sizeof (struct flist));
curr->size = size;
curr->xhot = xhot;
curr->yhot = yhot;
curr->delay = delay;
curr->pngfile = malloc (strlen (pngfile) + 1);
strcpy (curr->pngfile, pngfile);
curr->next = NULL;
if (start)
{
end->next = curr;
end = curr;
}
else
{
start = curr;
end = curr;
}
count++;
}
fclose (fp);
*list = start;
return count;
}
static void
premultiply_data (png_structp png, png_row_infop row_info, png_bytep data)
{
int i;
for (i = 0; i < row_info->rowbytes; i += 4)
{
unsigned char *base = &data[i];
unsigned char blue = base[0];
unsigned char green = base[1];
unsigned char red = base[2];
unsigned char alpha = base[3];
XcursorPixel p;
red = (unsigned) red * (unsigned) alpha / 255;
green = (unsigned) green * (unsigned) alpha / 255;
blue = (unsigned) blue * (unsigned) alpha / 255;
p = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
memcpy (base, &p, sizeof (XcursorPixel));
}
}
static XcursorImage *
load_image (struct flist *list, char *prefix)
{
XcursorImage *image;
png_structp png;
png_infop info;
png_bytepp rows;
FILE *fp;
int i;
png_uint_32 width, height;
int depth, color, interlace;
char *file;
png = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png == NULL)
return NULL;
info = png_create_info_struct (png);
if (info == NULL)
{
png_destroy_read_struct (&png, NULL, NULL);
return NULL;
}
if (setjmp (png->jmpbuf))
{
png_destroy_read_struct (&png, &info, NULL);
return NULL;
}
if (prefix)
{
file = malloc (strlen (prefix) + 1 + strlen (list->pngfile) + 1);
strcpy (file, prefix);
strcat (file, "/");
strcat (file, list->pngfile);
}
else
file = list->pngfile;
fp = fopen (file, "rb");
if (prefix)
free (file);
if (fp == NULL)
{
png_destroy_read_struct (&png, &info, NULL);
return NULL;
}
png_init_io (png, fp);
png_read_info (png, info);
png_get_IHDR (png, info, &width, &height, &depth, &color, &interlace,
NULL, NULL);
/* TODO: More needs to be done here maybe */
if (color == PNG_COLOR_TYPE_PALETTE && depth <= 8)
png_set_expand (png);
if (color == PNG_COLOR_TYPE_GRAY && depth < 8)
png_set_expand (png);
if (png_get_valid (png, info, PNG_INFO_tRNS))
png_set_expand (png);
if (depth == 16)
png_set_strip_16 (png);
if (depth < 8)
png_set_packing (png);
if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb (png);
if (interlace != PNG_INTERLACE_NONE)
png_set_interlace_handling (png);
png_set_bgr (png);
png_set_filler (png, 255, PNG_FILLER_AFTER);
png_set_read_user_transform_fn (png, premultiply_data);
png_read_update_info (png, info);
image = XcursorImageCreate (width, height);
image->size = list->size;
image->xhot = list->xhot;
image->yhot = list->yhot;
image->delay = list->delay;
rows = malloc (sizeof (png_bytep) * height);
for (i = 0; i < height; i++)
rows[i] = (png_bytep) (image->pixels + i * width);
png_read_image (png, rows);
png_read_end (png, info);
free (rows);
fclose (fp);
png_destroy_read_struct (&png, &info, NULL);
return image;
}
static int
write_cursors (int count, struct flist *list, char *filename, char *prefix)
{
XcursorImages *cimages;
XcursorImage *image;
int i;
FILE *fp;
int ret;
if (strcmp (filename, "-") != 0)
{
fp = fopen (filename, "wb");
if (!fp)
return 1;
}
else
fp = stdout;
cimages = XcursorImagesCreate (count);
cimages->nimage = count;
for (i = 0; i < count; i++, list = list->next)
{
image = load_image (list, prefix);
if (image == NULL)
{
fprintf (stderr, "PNG error while reading %s!\n", list->pngfile);
return 1;
}
cimages->images[i] = image;
}
ret = XcursorFileSaveImages (fp, cimages);
fclose (fp);
return ret ? 0 : 1;
}
static int
check_image (char *image)
{
unsigned int width, height;
unsigned char *data;
int x_hot, y_hot;
XImage ximage;
unsigned char hash[XCURSOR_BITMAP_HASH_SIZE];
int i;
if (XReadBitmapFileData (image, &width, &height, &data, &x_hot, &y_hot) != BitmapSuccess)
{
fprintf (stderr, "Can't open bitmap file \"%s\"\n", image);
return 1;
}
ximage.height = height;
ximage.width = width;
ximage.depth = 1;
ximage.bits_per_pixel = 1;
ximage.xoffset = 0;
ximage.format = XYPixmap;
ximage.data = (char *)data;
ximage.byte_order = LSBFirst;
ximage.bitmap_unit = 8;
ximage.bitmap_bit_order = LSBFirst;
ximage.bitmap_pad = 8;
ximage.bytes_per_line = (width+7)/8;
XcursorImageHash (&ximage, hash);
printf ("%s: ", image);
for (i = 0; i < XCURSOR_BITMAP_HASH_SIZE; i++)
printf ("%02x", hash[i]);
printf ("\n");
return 0;
}
int
main (int argc, char *argv[])
{
struct flist *list;
int count;
char *in = 0, *out = 0;
char *prefix = 0;
int i;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-V") == 0 || strcmp (argv[i], "--version") == 0)
{
printf ("xcursorgen version %s\n", VERSION_STR);
return 0;
}
if (strcmp (argv[i], "-?") == 0 || strcmp (argv[i], "--help") == 0)
{
usage (argv[0]);
return 0;
}
if (strcmp (argv[i], "-image") == 0)
{
int i = 2;
int ret = 0;
while (argv[i])
{
if (check_image (argv[i]))
ret = i;
i++;
}
return ret;
}
if (strcmp (argv[i], "-p") == 0 || strcmp (argv[i], "--prefix") == 0)
{
i++;
if (argv[i] == 0)
{
usage (argv[0]);
return 1;
}
prefix = argv[i];
continue;
}
if (!in)
in = argv[i];
else if (!out)
out = argv[i];
else
{
usage (argv[0]);
return 1;
}
}
if (!in)
in = "-";
if (!out)
out = "-";
count = read_config_file (in, &list);
if (count == 0)
{
fprintf (stderr, "Error reading config file!\n");
return 1;
}
return write_cursors (count, list, out, prefix);
}
|