-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamAnim.c
452 lines (407 loc) · 10.8 KB
/
streamAnim.c
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// $Id: streamAnim.c,v 1.7 2009/12/21 18:00:59 hayes Exp $
// $Author: hayes $
// $Date: 2009/12/21 18:00:59 $
// $Revision: 1.7 $
//
// streamAnim is designed to be a lower level image streaming utility
// to XWindows platforms including Linux with a minimal windowing and
// library need. streamAnim never completely loads the stream of
// images into memory, only loading one image at a time. This is
// exceptionaly useful with very large images and very large streams
// of images which can rapidly bring a systems memory grinding to a
// halt in mid animation.....
//
// This version relies on imlib for greater flexibility in image
// format loading....
//
// http://freshmeat.net/projects/imlib/
// http://ftp.gnome.org/pub/GNOME/sources/imlib/
// http://ftp.gnome.org/pub/GNOME/sources/imlib/
// http://ftp.gnome.org/pub/GNOME/sources/imlib/1.9/imlib-1.9.15.tar.bz2
// http://ftp.gnome.org/pub/GNOME/sources/imlib/1.9/imlib-1.9.15.tar.gz
//
// Copyright (C) 1999 Paul Robert Hayes
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// When reporting or displaying any results or animations created
// using this code or modification of this code, make the appropriate
// citation referencing streamAnim by name and including the version
// number. You should know better. 8)
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// http://www.cemtach.com/
//
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/shape.h>
#include <Imlib.h>
#include <string.h>
#include <linux/limits.h>
#include <errno.h>
#include "getopt.h"
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
struct _listItem
{
void *filename;
//
struct _listItem *next;
};
struct _sList
{
struct _listItem *head;
struct _listItem *tail;
};
void
usage()
{
fprintf(stderr, "\t\n");
fprintf(stderr, "Copyright (C) 1999-\n");
fprintf(stderr, "\tstreamAnim is designed for minimal memory footprint while delivering frames to the screen as fast as possible.\n");
fprintf(stderr, "\t\n");
fprintf(stderr, "Usage:\n streamAnim [options] imageFiles\n");
fprintf(stderr, "\t\n");
fprintf(stderr, "\t--resize resize at each frame, in case images vary in dimensions\n");
fprintf(stderr, "\t--loop loop through all images and repeat\n");
fprintf(stderr, "\t--scale {factor} scale by {factor} amount\n");
fprintf(stderr, "\t--sleep {seconds} sleep for X seconds after each frame\n");
fprintf(stderr, "\t--usleep {microseconds} sleep for X microseconds after each frame\n");
fprintf(stderr, "\t--listFile {myFile} parse myFile acquiring image file names from it (1 per line)\n");
fprintf(stderr, "\t\n");
}
void
addFile(struct _sList *thisList,
char *filename)
{
struct _listItem *newItem = NULL;
newItem = malloc(sizeof(struct _listItem));
if (newItem == NULL)
{
fprintf(stderr, "Unable to allocate memory for file list storage: %s", strerror(errno));
exit(1);
}
newItem->next = NULL;
//
newItem->filename = strdup(filename);
if (newItem->filename == NULL)
{
fprintf(stderr, "Unable to allocate memory for file name storage: %s", strerror(errno));
exit(1);
}
//
if (thisList->head == NULL)
{
thisList->head = newItem;
thisList->tail = thisList->head;
//
return;
}
//
// Otherwise add into the tail...
thisList->tail->next = newItem;
thisList->tail = newItem;
}
int
main(int argc, char **argv)
{
Display *disp;
ImlibData *id;
XSetWindowAttributes attr;
Window win;
ImlibImage *im;
Pixmap p,m;
int w,h;
int c, i, j, k, numberFiles;
// char **fileNames;
char *listFilename = NULL;
struct _sList fileList;
struct _listItem *listItem = NULL;
char parseLine[PATH_MAX];
FILE *parseStream = NULL;
char parseStreamRoot[PATH_MAX];
char testFilename[PATH_MAX];
char cwd[PATH_MAX];
char *pathSplit = NULL;
char *imageFilename = NULL;
XWindowChanges windowChanges;
unsigned int sleepValue = 0;
unsigned int usleepValue = 0;
float scaleValue = 1.0;
XTextProperty windowTitle;
int loop = FALSE;
int resize = FALSE;
fileList.head = NULL;
fileList.tail = NULL;
if (argc<=1)
{
usage();
exit(1);
}
getcwd(cwd, PATH_MAX);
//
// Options parsing to explore variations without recompiling....
while (1)
{
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] =
{
{"loop", 0, 0, 0},
{"resize", 0, 0, 0},
{"listFile", 1, 0, 0},
{"sleep", 1, 0, 0},
{"scale", 1, 0, 0},
{"usleep", 1, 0, 0},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "",
long_options, &option_index);
if (c == -1)
break;
if (c == EOF)
break;
switch (c)
{
case 0:
if (strcmp(long_options[option_index].name, "loop") == 0)
{
loop = TRUE;
}
if (strcmp(long_options[option_index].name, "resize") == 0)
{
resize = TRUE;
}
if (strcmp(long_options[option_index].name, "usleep") == 0)
{
usleepValue = strtoul(optarg, NULL, 10);
}
if (strcmp(long_options[option_index].name, "sleep") == 0)
{
sleepValue = strtoul(optarg, NULL, 10);
}
if (strcmp(long_options[option_index].name, "scale") == 0)
{
scaleValue = strtod(optarg, NULL);
}
if (strcmp(long_options[option_index].name, "listFile") == 0)
{
listFilename = strdup(optarg);
}
break;
default:
usage();
exit(1);
}
}
//
// access list file if it's available...
if (listFilename != NULL)
{
if (access(listFilename, R_OK) != 0)
{
fprintf(stderr, "%s", strerror(errno));
}
else
{
parseStream = fopen(listFilename, "r");
strncpy(parseStreamRoot, listFilename, PATH_MAX);
//
pathSplit = strrchr(parseStreamRoot, '/');
if (pathSplit != NULL)
{
pathSplit[0] = '\0';
}
//
while (fgets(parseLine, PATH_MAX, parseStream) != NULL)
{
while ( (pathSplit = strrchr(parseLine, '\n')) != NULL)
{
pathSplit[0] = '\0';
}
//
// Try to access the file listed on this line
if (access(parseLine, R_OK) == 0)
{
addFile(&fileList, parseLine);
continue;
}
//
snprintf(testFilename, PATH_MAX, "%s/%s", cwd, parseLine);
if (access(testFilename, R_OK) == 0)
{
addFile(&fileList, testFilename);
continue;
}
//
snprintf(testFilename, PATH_MAX, "%s/%s", parseStreamRoot, parseLine);
if (access(testFilename, R_OK) == 0)
{
addFile(&fileList, testFilename);
continue;
}
//
// We could not find the file....
fprintf(stderr, "Unable to access: [%s] %s\n", parseLine, strerror(errno));
} // while (fgets(parseLine, PATH_MAX, parseStream) != NULL)
}
}
//
// Acquire the remaining commandline values as filenames....
j=optind;
while(optind < argc)
{
//
fprintf(stderr, "%s\n", argv[optind]);
//
if (access(argv[optind], F_OK) != 0)
{
fprintf(stderr, "%s", strerror(errno));
}
else
{
addFile(&fileList, argv[optind]);
}
//
optind++;
}
//
//
listItem = fileList.head;
while(listItem != NULL)
{
fprintf(stderr, "%s\n", listItem->filename);
//
listItem = listItem->next;
}
fprintf(stderr, "\n");
//
// Connect to the default Xserver
//
disp=XOpenDisplay(NULL);
//
// Immediately afterwards Intitialise Imlib
//
id=Imlib_init(disp);
//
// Load the image specified as the first argument
//
imageFilename = (fileList.head)->filename;
im=Imlib_load_image(id,imageFilename);
//
// Suck the image's original width and height out of the Image structure
//
w=im->rgb_width;
h=im->rgb_height;
//
// Create a Window to display in
//
win=XCreateWindow(disp,DefaultRootWindow(disp),0,0,w,h,0,id->x.depth,
InputOutput,id->x.visual,0,&attr);
XSelectInput(disp,win,StructureNotifyMask);
//
// Actually display the window
//
XMapWindow(disp,win);
//
// Event loop to watch for resizes and file changes.....
//
if (loop)
(fileList.tail)->next = fileList.head;
listItem = fileList.head;
while(listItem != NULL)
{
imageFilename = listItem->filename;
//
//
if (sleepValue > 0)
sleep(sleepValue);
if (usleepValue > 0)
usleep(usleepValue);
//
// Set new title for new file...
XStringListToTextProperty(&imageFilename, 1, &windowTitle);
XSetWMName(disp, win, &windowTitle);
XSetWMIconName(disp, win, &windowTitle);
//
//
Imlib_free_pixmap(id,p);
//
// Load the image specified as the first argument
Imlib_kill_image(id,im);
im=Imlib_load_image(id,imageFilename);
//
// Suck the image's original width and height out of the Image
// structure
w=im->rgb_width*scaleValue;
h=im->rgb_height*scaleValue;
//
if (resize)
{
windowChanges.width = w;
windowChanges.height = h;
//
XConfigureWindow(disp, win, CWWidth | CWHeight, &windowChanges);
}
//
// Re-render the Image to the new size
Imlib_render(id,im,w,h);
//
// Extract the Image and mask pixmaps from the Image
p=Imlib_move_image(id,im);
//
// The mask will be 0 if the image has no transparency
m=Imlib_move_mask(id,im);
//
// Put the Image in the window, at the window's size and apply a
// shape mask if applicable, or remove one if not
Imlib_apply_image(id,im,win);
//
// Put the Image pixmap in the background of the window
XSetWindowBackgroundPixmap(disp,win,p);
//
// /* If there was a mask to the image, set the Image's
// mask to it */ if (m)
// XShapeCombineMask(disp,win,ShapeBounding,0,0,m,ShapeSet);
// /* Actually display the window */
// XMapWindow(disp,win);
XClearWindow(disp,win);
//
// Synchronize with the Xserver without discarding any events to
// refresh with the new image
XSync(disp,False);
//
//
listItem = listItem->next;
}
}
// typedef struct {
// int x, y;
// int width, height;
// int border_width;
// Window sibling;
// int stack_mode;
// } XWindowChanges;
// /* Configure window value mask bits */
// #define CWX (1<<0)
// #define CWY (1<<1)
// #define CWWidth (1<<2)
// #define CWHeight (1<<3)
// #define CWBorderWidth (1<<4)
// #define CWSibling (1<<5)
// #define CWStackMode (1<<6)