1 /* webcollage-helper --- scales and pastes one image into another
2 * xscreensaver, Copyright (c) 2002-2005 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>
27 #undef HAVE_STDLIB_H /* stupid jconfig.h! */
29 #include <gdk-pixbuf/gdk-pixbuf.h>
33 static int verbose_p = 0;
35 static void add_jpeg_comment (struct jpeg_compress_struct *cinfo);
36 static void write_pixbuf (GdkPixbuf *pb, const char *file);
39 load_pixbuf (const char *file)
45 pb = gdk_pixbuf_new_from_file (file, &err);
46 #else /* !HAVE_GTK2 */
47 pb = gdk_pixbuf_new_from_file (file);
48 #endif /* HAVE_GTK2 */
53 fprintf (stderr, "%s: %s\n", progname, err->message);
55 #else /* !HAVE_GTK2 */
56 fprintf (stderr, "%s: unable to load %s\n", progname, file);
57 #endif /* !HAVE_GTK2 */
66 bevel_image (GdkPixbuf **pbP, int bevel_pct,
67 int x, int y, int w, int h)
70 int small_size = (w > h ? h : w);
72 int bevel_size = small_size * (bevel_pct / 100.0);
74 /* Use a proportionally larger bevel size for especially small images. */
75 if (bevel_size < 20 && small_size > 40) bevel_size = 20;
76 else if (bevel_size < 10 && small_size > 20) bevel_size = 10;
77 else if (bevel_size < 5) /* too small to bother bevelling */
80 /* Ensure the pixbuf has an alpha channel. */
81 if (! gdk_pixbuf_get_has_alpha (pb))
83 GdkPixbuf *pb2 = gdk_pixbuf_add_alpha (pb, FALSE, 0, 0, 0);
84 gdk_pixbuf_unref (pb);
89 guchar *data = gdk_pixbuf_get_pixels (pb);
91 int rs = gdk_pixbuf_get_rowstride (pb);
92 int ch = gdk_pixbuf_get_n_channels (pb);
94 double *ramp = (double *) malloc (sizeof(*ramp) * (bevel_size + 1));
98 fprintf (stderr, "%s: out of memory (%d)\n", progname, bevel_size);
102 for (xx = 0; xx <= bevel_size; xx++)
106 ramp[xx] = xx / (double) bevel_size;
108 # else /* sinusoidal */
109 double p = (xx / (double) bevel_size);
110 double s = sin (p * M_PI / 2);
115 line = data + (rs * y);
116 for (yy = 0; yy < h; yy++)
118 guchar *p = line + (x * ch);
119 for (xx = 0; xx < w; xx++)
123 if (xx < bevel_size) rx = ramp[xx];
124 else if (xx >= w - bevel_size) rx = ramp[w - xx - 1];
127 if (yy < bevel_size) ry = ramp[yy];
128 else if (yy >= h - bevel_size) ry = ramp[h - yy - 1];
140 #if 0 /* show the ramp */
141 line = data + (rs * y);
142 for (yy = 0; yy < h; yy++)
144 guchar *p = line + (x * ch);
145 for (xx = 0; xx < w; xx++)
148 for (cc = 0; cc < ch-1; cc++)
159 fprintf (stderr, "%s: added %d%% bevel (%d px)\n", progname,
160 bevel_pct, bevel_size);
168 paste (const char *paste_file,
169 const char *base_file,
171 double opacity, int bevel_pct,
172 int from_x, int from_y, int to_x, int to_y,
178 int paste_w, paste_h;
181 paste_pb = load_pixbuf (paste_file);
182 base_pb = load_pixbuf (base_file);
184 paste_w = gdk_pixbuf_get_width (paste_pb);
185 paste_h = gdk_pixbuf_get_height (paste_pb);
187 base_w = gdk_pixbuf_get_width (base_pb);
188 base_h = gdk_pixbuf_get_height (base_pb);
192 fprintf (stderr, "%s: loaded %s: %dx%d\n",
193 progname, base_file, base_w, base_h);
194 fprintf (stderr, "%s: loaded %s: %dx%d\n",
195 progname, paste_file, paste_w, paste_h);
198 if (from_scale != 1.0)
200 int new_w = paste_w * from_scale;
201 int new_h = paste_h * from_scale;
202 GdkPixbuf *new_pb = gdk_pixbuf_scale_simple (paste_pb, new_w, new_h,
204 gdk_pixbuf_unref (paste_pb);
206 paste_w = gdk_pixbuf_get_width (paste_pb);
207 paste_h = gdk_pixbuf_get_height (paste_pb);
210 fprintf (stderr, "%s: %s: scaled to %dx%d (%.2f)\n",
211 progname, paste_file, paste_w, paste_h, from_scale);
214 if (w == 0) w = paste_w - from_x;
215 if (h == 0) h = paste_h - from_y;
227 if (from_x < 0) /* from left out of bounds */
234 if (from_y < 0) /* from top out of bounds */
241 if (to_x < 0) /* to left out of bounds */
249 if (to_y < 0) /* to top out of bounds */
257 if (from_x + w > paste_w) /* from right out of bounds */
259 w = paste_w - from_x;
263 if (from_y + h > paste_h) /* from bottom out of bounds */
265 h = paste_h - from_y;
269 if (to_x + w > base_w) /* to right out of bounds */
275 if (to_y + h > base_h) /* to bottom out of bounds */
282 if (clipped && verbose_p)
284 fprintf (stderr, "clipped from: %dx%d %d,%d %d,%d\n",
285 ow, oh, ofx, ofy, otx, oty);
286 fprintf (stderr, "clipped to: %dx%d %d,%d %d,%d\n",
287 w, h, from_x, from_y, to_x, to_y);
292 bevel_image (&paste_pb, bevel_pct,
293 from_x, from_y, w, h);
295 if (opacity == 1.0 && bevel_pct == 0)
296 gdk_pixbuf_copy_area (paste_pb,
297 from_x, from_y, w, h,
302 from_x++; /* gdk_pixbuf_composite gets confused about the bevel: */
303 from_y++; /* it leaves a stripe on the top and left if we try to */
304 to_x++; /* start at 0,0, so pull it right and down by 1 pixel. */
305 to_y++; /* (problem seen in gtk2-2.4.14-2.fc3) */
310 gdk_pixbuf_composite (paste_pb, base_pb,
312 to_x - from_x, to_y - from_y,
319 fprintf (stderr, "%s: pasted %dx%d from %d,%d to %d,%d\n",
320 progname, paste_w, paste_h, from_x, from_y, to_x, to_y);
322 gdk_pixbuf_unref (paste_pb);
323 write_pixbuf (base_pb, base_file);
324 gdk_pixbuf_unref (base_pb);
329 write_pixbuf (GdkPixbuf *pb, const char *file)
331 int jpeg_quality = 85;
333 int w = gdk_pixbuf_get_width (pb);
334 int h = gdk_pixbuf_get_height (pb);
335 guchar *data = gdk_pixbuf_get_pixels (pb);
336 int ww = gdk_pixbuf_get_rowstride (pb);
337 int channels = gdk_pixbuf_get_n_channels (pb);
339 struct jpeg_compress_struct cinfo;
340 struct jpeg_error_mgr jerr;
345 fprintf (stderr, "%s: %d channels?\n", progname, channels);
349 cinfo.err = jpeg_std_error (&jerr);
350 jpeg_create_compress (&cinfo);
352 out = fopen (file, "wb");
356 sprintf (buf, "%.100s: %.100s", progname, file);
361 fprintf (stderr, "%s: writing %s...", progname, file);
363 jpeg_stdio_dest (&cinfo, out);
365 cinfo.image_width = w;
366 cinfo.image_height = h;
367 cinfo.input_components = channels;
368 cinfo.in_color_space = JCS_RGB;
370 jpeg_set_defaults (&cinfo);
371 jpeg_simple_progression (&cinfo);
372 jpeg_set_quality (&cinfo, jpeg_quality, TRUE);
374 jpeg_start_compress (&cinfo, TRUE);
375 add_jpeg_comment (&cinfo);
379 guchar *end = d + (ww * h);
382 jpeg_write_scanlines (&cinfo, &d, 1);
387 jpeg_finish_compress (&cinfo);
388 jpeg_destroy_compress (&cinfo);
394 if (fstat (fileno (out), &st))
397 sprintf (buf, "%.100s: %.100s", progname, file);
401 fprintf (stderr, " %luK\n", ((unsigned long) st.st_size + 1023) / 1024);
409 add_jpeg_comment (struct jpeg_compress_struct *cinfo)
411 time_t now = time ((time_t *) 0);
412 struct tm *tm = localtime (&now);
415 " Generated by WebCollage: Exterminate All Rational Thought. \r\n"
416 " Copyright (c) 1999-%Y by Jamie Zawinski <jwz@jwz.org> \r\n"
418 " http://www.jwz.org/webcollage/ \r\n"
420 " This is what the web looked like on %d %b %Y at %I:%M:%S %p %Z. \r\n"
422 char comment[sizeof(fmt) + 100];
423 strftime (comment, sizeof(comment)-1, fmt, tm);
424 jpeg_write_marker (cinfo, JPEG_COM,
425 (unsigned char *) comment,
433 fprintf (stderr, "usage: %s [-v] paste-file base-file\n"
434 "\t from-scale opacity\n"
435 "\t from-x from-y to-x to-y w h\n"
437 "\t Pastes paste-file into base-file.\n"
438 "\t base-file will be overwritten (with JPEG data.)\n"
439 "\t scaling is applied first: coordinates apply to scaled image.\n",
446 main (int argc, char **argv)
449 char *paste_file, *base_file, *s, dummy;
450 double from_scale, opacity;
451 int from_x, from_y, to_x, to_y, w, h, bevel_pct;
454 progname = argv[i++];
455 s = strrchr (progname, '/');
456 if (s) progname = s+1;
458 if (argc != 11 && argc != 12) usage();
460 if (!strcmp(argv[i], "-v"))
463 paste_file = argv[i++];
464 base_file = argv[i++];
466 if (*paste_file == '-') usage();
467 if (*base_file == '-') usage();
470 if (1 != sscanf (s, " %lf %c", &from_scale, &dummy)) usage();
471 if (from_scale <= 0 || from_scale > 100) usage();
474 if (1 != sscanf (s, " %lf %c", &opacity, &dummy)) usage();
475 if (opacity <= 0 || opacity > 1) usage();
477 s = argv[i++]; if (1 != sscanf (s, " %d %c", &from_x, &dummy)) usage();
478 s = argv[i++]; if (1 != sscanf (s, " %d %c", &from_y, &dummy)) usage();
479 s = argv[i++]; if (1 != sscanf (s, " %d %c", &to_x, &dummy)) usage();
480 s = argv[i++]; if (1 != sscanf (s, " %d %c", &to_y, &dummy)) usage();
481 s = argv[i++]; if (1 != sscanf (s, " %d %c", &w, &dummy)) usage();
482 s = argv[i++]; if (1 != sscanf (s, " %d %c", &h, &dummy)) usage();
484 bevel_pct = 10; /* #### */
491 #endif /* HAVE_GTK2 */
493 paste (paste_file, base_file,
494 from_scale, opacity, bevel_pct,
495 from_x, from_y, to_x, to_y,
500 #endif /* HAVE_GDK_PIXBUF && HAVE_JPEGLIB -- whole file */