1 /* webcollage-helper --- scales and pastes one image into another
2 * xscreensaver, Copyright (c) 2002, 2003, 2004 Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
17 #if defined(HAVE_GDK_PIXBUF) && defined(HAVE_JPEGLIB) /* whole file */
24 #include <sys/types.h>
28 #include <gdk-pixbuf/gdk-pixbuf.h>
32 static int verbose_p = 0;
34 static void add_jpeg_comment (struct jpeg_compress_struct *cinfo);
35 static void write_pixbuf (GdkPixbuf *pb, const char *file);
38 load_pixbuf (const char *file)
44 pb = gdk_pixbuf_new_from_file (file, &err);
45 #else /* !HAVE_GTK2 */
46 pb = gdk_pixbuf_new_from_file (file);
47 #endif /* HAVE_GTK2 */
52 fprintf (stderr, "%s: %s\n", progname, err->message);
54 #else /* !HAVE_GTK2 */
55 fprintf (stderr, "%s: unable to load %s\n", progname, file);
56 #endif /* !HAVE_GTK2 */
65 bevel_image (GdkPixbuf **pbP, int bevel_pct,
66 int x, int y, int w, int h)
70 int bevel_size = (w > h ? h : w) * (bevel_pct / 100.0);
72 if (bevel_size < 10) /* too small to bother */
75 /* Ensure the pixbuf has an alpha channel. */
76 if (! gdk_pixbuf_get_has_alpha (pb))
78 GdkPixbuf *pb2 = gdk_pixbuf_add_alpha (pb, FALSE, 0, 0, 0);
79 gdk_pixbuf_unref (pb);
84 guchar *data = gdk_pixbuf_get_pixels (pb);
86 int rs = gdk_pixbuf_get_rowstride (pb);
87 int ch = gdk_pixbuf_get_n_channels (pb);
89 double *ramp = (double *) malloc (sizeof(*ramp) * (bevel_size + 1));
93 fprintf (stderr, "%s: out of memory (%d)\n", progname, bevel_size);
97 for (xx = 0; xx <= bevel_size; xx++)
101 ramp[xx] = xx / (double) bevel_size;
103 # else /* sinusoidal */
104 double p = (xx / (double) bevel_size);
105 double s = sin (p * M_PI / 2);
110 line = data + (rs * y);
111 for (yy = 0; yy < h; yy++)
113 guchar *p = line + (x * ch);
114 for (xx = 0; xx < w; xx++)
118 if (xx < bevel_size) rx = ramp[xx];
119 else if (xx >= w - bevel_size) rx = ramp[w - xx - 1];
122 if (yy < bevel_size) ry = ramp[yy];
123 else if (yy >= h - bevel_size) ry = ramp[h - yy - 1];
130 /* p[0]=p[1]=p[2]=0; / * #### */
137 #if 0 /* show the ramp */
138 for (xx = 0; xx < bevel_size * 2; xx++)
140 int ii = (256 * (xx >= bevel_size ? 1 : ramp[xx]));
142 for (yy = 0; yy < ii; yy++)
144 data [((y + (256-yy)) * rs) + ((x + xx) * ch) + 0] = 0;
145 data [((y + (256-yy)) * rs) + ((x + xx) * ch) + 1] = 0;
146 data [((y + (256-yy)) * rs) + ((x + xx) * ch) + 2] = 0;
147 data [((y + (256-yy)) * rs) + ((x + xx) * ch) + 3] = 255;
155 fprintf (stderr, "%s: added %d%% bevel (%d px)\n", progname,
156 bevel_pct, bevel_size);
164 paste (const char *paste_file,
165 const char *base_file,
167 double opacity, int bevel_pct,
168 int from_x, int from_y, int to_x, int to_y,
174 int paste_w, paste_h;
177 paste_pb = load_pixbuf (paste_file);
178 base_pb = load_pixbuf (base_file);
180 paste_w = gdk_pixbuf_get_width (paste_pb);
181 paste_h = gdk_pixbuf_get_height (paste_pb);
183 base_w = gdk_pixbuf_get_width (base_pb);
184 base_h = gdk_pixbuf_get_height (base_pb);
188 fprintf (stderr, "%s: loaded %s: %dx%d\n",
189 progname, base_file, base_w, base_h);
190 fprintf (stderr, "%s: loaded %s: %dx%d\n",
191 progname, paste_file, paste_w, paste_h);
194 if (from_scale != 1.0)
196 int new_w = paste_w * from_scale;
197 int new_h = paste_h * from_scale;
198 GdkPixbuf *new_pb = gdk_pixbuf_scale_simple (paste_pb, new_w, new_h,
200 gdk_pixbuf_unref (paste_pb);
202 paste_w = gdk_pixbuf_get_width (paste_pb);
203 paste_h = gdk_pixbuf_get_height (paste_pb);
206 fprintf (stderr, "%s: %s: scaled to %dx%d (%.2f)\n",
207 progname, paste_file, paste_w, paste_h, from_scale);
210 if (w == 0) w = paste_w - from_x;
211 if (h == 0) h = paste_h - from_y;
223 if (from_x < 0) /* from left out of bounds */
230 if (from_y < 0) /* from top out of bounds */
237 if (to_x < 0) /* to left out of bounds */
245 if (to_y < 0) /* to top out of bounds */
253 if (from_x + w > paste_w) /* from right out of bounds */
255 w = paste_w - from_x;
259 if (from_y + h > paste_h) /* from bottom out of bounds */
261 h = paste_h - from_y;
265 if (to_x + w > base_w) /* to right out of bounds */
271 if (to_y + h > base_h) /* to bottom out of bounds */
278 if (clipped && verbose_p)
280 fprintf (stderr, "clipped from: %dx%d %d,%d %d,%d\n",
281 ow, oh, ofx, ofy, otx, oty);
282 fprintf (stderr, "clipped to: %dx%d %d,%d %d,%d\n",
283 w, h, from_x, from_y, to_x, to_y);
288 bevel_image (&paste_pb, bevel_pct,
289 from_x, from_y, w, h);
291 if (opacity == 1.0 && bevel_pct == 0)
292 gdk_pixbuf_copy_area (paste_pb,
293 from_x, from_y, w, h,
297 gdk_pixbuf_composite (paste_pb, base_pb,
299 to_x - from_x, to_y - from_y,
305 fprintf (stderr, "%s: pasted %dx%d from %d,%d to %d,%d\n",
306 progname, paste_w, paste_h, from_x, from_y, to_x, to_y);
308 gdk_pixbuf_unref (paste_pb);
309 write_pixbuf (base_pb, base_file);
310 gdk_pixbuf_unref (base_pb);
315 write_pixbuf (GdkPixbuf *pb, const char *file)
317 int jpeg_quality = 85;
319 int w = gdk_pixbuf_get_width (pb);
320 int h = gdk_pixbuf_get_height (pb);
321 guchar *data = gdk_pixbuf_get_pixels (pb);
322 int ww = gdk_pixbuf_get_rowstride (pb);
323 int channels = gdk_pixbuf_get_n_channels (pb);
325 struct jpeg_compress_struct cinfo;
326 struct jpeg_error_mgr jerr;
331 fprintf (stderr, "%s: %d channels?\n", progname, channels);
335 cinfo.err = jpeg_std_error (&jerr);
336 jpeg_create_compress (&cinfo);
338 out = fopen (file, "wb");
342 sprintf (buf, "%.100s: %.100s", progname, file);
347 fprintf (stderr, "%s: writing %s...", progname, file);
349 jpeg_stdio_dest (&cinfo, out);
351 cinfo.image_width = w;
352 cinfo.image_height = h;
353 cinfo.input_components = channels;
354 cinfo.in_color_space = JCS_RGB;
356 jpeg_set_defaults (&cinfo);
357 jpeg_simple_progression (&cinfo);
358 jpeg_set_quality (&cinfo, jpeg_quality, TRUE);
360 jpeg_start_compress (&cinfo, TRUE);
361 add_jpeg_comment (&cinfo);
365 guchar *end = d + (ww * h);
368 jpeg_write_scanlines (&cinfo, &d, 1);
373 jpeg_finish_compress (&cinfo);
374 jpeg_destroy_compress (&cinfo);
380 if (fstat (fileno (out), &st))
383 sprintf (buf, "%.100s: %.100s", progname, file);
387 fprintf (stderr, " %luK\n", ((unsigned long) st.st_size + 1023) / 1024);
395 add_jpeg_comment (struct jpeg_compress_struct *cinfo)
397 time_t now = time ((time_t *) 0);
398 struct tm *tm = localtime (&now);
401 " Generated by WebCollage: Exterminate All Rational Thought. \r\n"
402 " Copyright (c) 1999-%Y by Jamie Zawinski <jwz@jwz.org> \r\n"
404 " http://www.jwz.org/webcollage/ \r\n"
406 " This is what the web looked like on %d %b %Y at %I:%M:%S %p %Z. \r\n"
408 char comment[sizeof(fmt) + 100];
409 strftime (comment, sizeof(comment)-1, fmt, tm);
410 jpeg_write_marker (cinfo, JPEG_COM,
411 (unsigned char *) comment,
419 fprintf (stderr, "usage: %s [-v] paste-file base-file\n"
420 "\t from-scale opacity\n"
421 "\t from-x from-y to-x to-y w h\n"
423 "\t Pastes paste-file into base-file.\n"
424 "\t base-file will be overwritten (with JPEG data.)\n"
425 "\t scaling is applied first: coordinates apply to scaled image.\n",
432 main (int argc, char **argv)
435 char *paste_file, *base_file, *s, dummy;
436 double from_scale, opacity;
437 int from_x, from_y, to_x, to_y, w, h, bevel_pct;
440 progname = argv[i++];
441 s = strrchr (progname, '/');
442 if (s) progname = s+1;
444 if (argc != 11 && argc != 12) usage();
446 if (!strcmp(argv[i], "-v"))
449 paste_file = argv[i++];
450 base_file = argv[i++];
452 if (*paste_file == '-') usage();
453 if (*base_file == '-') usage();
456 if (1 != sscanf (s, " %lf %c", &from_scale, &dummy)) usage();
457 if (from_scale <= 0 || from_scale > 100) usage();
460 if (1 != sscanf (s, " %lf %c", &opacity, &dummy)) usage();
461 if (opacity <= 0 || opacity > 1) usage();
463 s = argv[i++]; if (1 != sscanf (s, " %d %c", &from_x, &dummy)) usage();
464 s = argv[i++]; if (1 != sscanf (s, " %d %c", &from_y, &dummy)) usage();
465 s = argv[i++]; if (1 != sscanf (s, " %d %c", &to_x, &dummy)) usage();
466 s = argv[i++]; if (1 != sscanf (s, " %d %c", &to_y, &dummy)) usage();
467 s = argv[i++]; if (1 != sscanf (s, " %d %c", &w, &dummy)) usage();
468 s = argv[i++]; if (1 != sscanf (s, " %d %c", &h, &dummy)) usage();
470 bevel_pct = 10; /* #### */
477 #endif /* HAVE_GTK2 */
479 paste (paste_file, base_file,
480 from_scale, opacity, bevel_pct,
481 from_x, from_y, to_x, to_y,
486 #endif /* HAVE_GDK_PIXBUF && HAVE_JPEGLIB -- whole file */