| 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 |
#include <stdio.h> |
| 51 |
#include <X11/Xlib.h> |
| 52 |
#include <X11/Xutil.h> |
| 53 |
#include <XPlxExt.h> |
| 54 |
|
| 55 |
#include "ppm.h" |
| 56 |
#include "mallocvar.h" |
| 57 |
|
| 58 |
usage (p) |
| 59 |
char *p; |
| 60 |
|
| 61 |
{ |
| 62 |
fprintf (stderr, "Usage: %s filename width height start end outbase [quality]\n", p); |
| 63 |
exit (1); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static char buffer[300000]; |
| 69 |
|
| 70 |
static const char * |
| 71 |
squote(const char * const arg) { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
size_t const maxEscapedLen = strlen(arg)*4 + 3; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
char * buffer; |
| 92 |
unsigned int inCursor; |
| 93 |
unsigned int outCursor; |
| 94 |
|
| 95 |
MALLOCARRAY_NOFAIL(buffer, maxEscapedLen); |
| 96 |
|
| 97 |
outCursor = 0; |
| 98 |
|
| 99 |
buffer[outCursor++] = '\''; |
| 100 |
|
| 101 |
for (inCursor = 0; arg[inCursor]; ++inCursor) { |
| 102 |
if (arg[inCursor] == '\'') { |
| 103 |
buffer[outCursor++] = '\''; |
| 104 |
buffer[outCursor++] = '\\'; |
| 105 |
buffer[outCursor++] = '\''; |
| 106 |
buffer[outCursor++] = '\''; |
| 107 |
} else { |
| 108 |
buffer[outCursor++] = arg[inCursor]; |
| 109 |
} |
| 110 |
} |
| 111 |
buffer[outCursor++] = '\''; |
| 112 |
|
| 113 |
buffer[outCursor++] = '\0'; |
| 114 |
|
| 115 |
return buffer; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
Visual * |
| 121 |
FindFullColorVisual (dpy, depth) |
| 122 |
Display *dpy; |
| 123 |
int *depth; |
| 124 |
{ |
| 125 |
XVisualInfo vinfo; |
| 126 |
XVisualInfo *vinfo_ret; |
| 127 |
int numitems, maxdepth; |
| 128 |
|
| 129 |
vinfo.class = TrueColor; |
| 130 |
|
| 131 |
vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems); |
| 132 |
|
| 133 |
if (numitems == 0) return NULL; |
| 134 |
|
| 135 |
maxdepth = 0; |
| 136 |
while(numitems > 0) { |
| 137 |
if (vinfo_ret[numitems-1].depth > maxdepth) { |
| 138 |
maxdepth = vinfo_ret[numitems-1 ].depth; |
| 139 |
} |
| 140 |
numitems--; |
| 141 |
} |
| 142 |
XFree(vinfo_ret); |
| 143 |
|
| 144 |
if (maxdepth < 24) return NULL; |
| 145 |
|
| 146 |
if (XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth, |
| 147 |
TrueColor, &vinfo)) { |
| 148 |
*depth = maxdepth; |
| 149 |
return vinfo.visual; |
| 150 |
} |
| 151 |
|
| 152 |
return NULL; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
Window |
| 158 |
CreateFullColorWindow (dpy, x, y, w, h) |
| 159 |
Display *dpy; |
| 160 |
int x, y, w, h; |
| 161 |
{ |
| 162 |
int depth; |
| 163 |
Visual *visual; |
| 164 |
XSetWindowAttributes xswa; |
| 165 |
unsigned int mask; |
| 166 |
unsigned int class; |
| 167 |
int screen; |
| 168 |
|
| 169 |
screen = XDefaultScreen(dpy); |
| 170 |
class = InputOutput; |
| 171 |
visual = FindFullColorVisual (dpy, &depth); |
| 172 |
if (visual == NULL) { |
| 173 |
return 0; |
| 174 |
} |
| 175 |
mask = CWBackPixel | CWColormap | CWBorderPixel; |
| 176 |
xswa.colormap = XCreateColormap(dpy, XRootWindow(dpy, screen), |
| 177 |
visual, AllocNone); |
| 178 |
xswa.background_pixel = BlackPixel(dpy, DefaultScreen(dpy)); |
| 179 |
xswa.border_pixel = WhitePixel(dpy, DefaultScreen(dpy)); |
| 180 |
|
| 181 |
return XCreateWindow(dpy, RootWindow(dpy, screen), x, y, w, h, |
| 182 |
1, depth, class, visual, mask, &xswa); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
main (argc, argv) |
| 188 |
int argc; |
| 189 |
char **argv; |
| 190 |
|
| 191 |
{ |
| 192 |
char *filename; |
| 193 |
Display *dpy; |
| 194 |
int screen; |
| 195 |
Window root; |
| 196 |
XEvent event; |
| 197 |
GC gc; |
| 198 |
Window win; |
| 199 |
XPlxCImage image; |
| 200 |
int size; |
| 201 |
char *qTable; |
| 202 |
FILE *inFile; |
| 203 |
FILE *outFile; |
| 204 |
extern char *malloc(); |
| 205 |
int fd, r1, r2, i, j, r; |
| 206 |
int quality; |
| 207 |
int start, end; |
| 208 |
XImage *ximage; |
| 209 |
char *tdata; |
| 210 |
char *obase; |
| 211 |
char ofname[256]; |
| 212 |
int height, width; |
| 213 |
char command[256]; |
| 214 |
|
| 215 |
ppm_init(&argc, argv); |
| 216 |
|
| 217 |
if ((argc != 7) && (argc != 8))usage (argv[0]); |
| 218 |
filename = argv[1]; |
| 219 |
|
| 220 |
width = atoi(argv[2]); |
| 221 |
height = atoi(argv[3]); |
| 222 |
|
| 223 |
start = atoi(argv[4]); |
| 224 |
end = atoi(argv[5]); |
| 225 |
|
| 226 |
if ((start < 1) || (end < start)) { |
| 227 |
perror ("Bad start and end values."); |
| 228 |
exit(); |
| 229 |
} |
| 230 |
|
| 231 |
obase = argv[6]; |
| 232 |
|
| 233 |
quality = 100; |
| 234 |
|
| 235 |
if (argc > 7) |
| 236 |
quality = atoi (argv[7]); |
| 237 |
|
| 238 |
dpy = XOpenDisplay (NULL); |
| 239 |
screen = DefaultScreen(dpy); |
| 240 |
root = DefaultRootWindow(dpy); |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
win = CreateFullColorWindow(dpy, 0, 0, width+4, height+4); |
| 246 |
gc = XCreateGC(dpy, win, 0, NULL); |
| 247 |
|
| 248 |
if (!win ) { |
| 249 |
perror ("Unable to create window"); |
| 250 |
exit(1); |
| 251 |
} |
| 252 |
|
| 253 |
XMapWindow (dpy, win); |
| 254 |
XSelectInput (dpy, win, ExposureMask |ButtonPressMask); |
| 255 |
|
| 256 |
size = MakeQTables(quality, &qTable); |
| 257 |
XPlxPutTable(dpy, win, gc, qTable, size, 0); |
| 258 |
XPlxPutTable(dpy, win, gc, qTable, size, 1); |
| 259 |
XPlxVideoTag (dpy, win, gc, PLX_VIDEO); |
| 260 |
|
| 261 |
inFile = fopen(filename, "rb"); |
| 262 |
if (inFile == NULL) { |
| 263 |
perror (filename); |
| 264 |
exit (1); |
| 265 |
} |
| 266 |
fd = fileno(inFile); |
| 267 |
wait(2); |
| 268 |
|
| 269 |
for (i=0; i<start; i++) { |
| 270 |
if (read (fd, &image, sizeof(image)) != sizeof(image)) { |
| 271 |
perror("End of file."); |
| 272 |
exit(); |
| 273 |
} |
| 274 |
image.data = buffer; |
| 275 |
if (read (fd, buffer, image.size) != image.size) { |
| 276 |
perror("End of file."); |
| 277 |
exit(); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
for (i=start; i<=end; i++) { |
| 282 |
fprintf(stdout, "GRABBING FRAME %d\n", i); |
| 283 |
|
| 284 |
if (read (fd, &image, sizeof(image)) != sizeof(image)) { |
| 285 |
perror("End of file."); |
| 286 |
exit(); |
| 287 |
} |
| 288 |
image.data = buffer; |
| 289 |
if (read (fd, buffer, image.size) != image.size) { |
| 290 |
perror("End of file."); |
| 291 |
exit(); |
| 292 |
} |
| 293 |
|
| 294 |
XPlxPutCImage (dpy, win, gc, &image, 0, 0, image.width, |
| 295 |
image.height, 0, 0, width+2, height+2, 1); |
| 296 |
|
| 297 |
XFlush(dpy); |
| 298 |
|
| 299 |
ximage = XGetImage(dpy, win, 0, 0, width, height, 0x00ffffff, |
| 300 |
ZPixmap); |
| 301 |
|
| 302 |
if (i == 0) { |
| 303 |
fprintf(stderr, "Depth %d\n", ximage->depth); |
| 304 |
fprintf(stderr, "Height: %d Width: %d\n", height, width ); |
| 305 |
} |
| 306 |
tdata = ximage->data; |
| 307 |
|
| 308 |
|
| 309 |
sprintf(ofname, "%s%d.ppm", obase, i); |
| 310 |
outFile = fopen("/tmp/foobar", "wb"); |
| 311 |
if (!outFile) { |
| 312 |
perror("Couldn't open output file."); |
| 313 |
} |
| 314 |
|
| 315 |
for (r=0; r<height; r++) { |
| 316 |
for (j=0; j<width; j++) { |
| 317 |
fputc(*(tdata+3), outFile); |
| 318 |
fputc(*(tdata+2), outFile); |
| 319 |
fputc(*(tdata+1), outFile); |
| 320 |
tdata += 4; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
fclose(outFile); |
| 325 |
|
| 326 |
free(tdata); |
| 327 |
|
| 328 |
sprintf(command, "rawtoppm %d %d < /tmp/foobar > %s", |
| 329 |
width, height, squote(ofname)); |
| 330 |
system(command); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|