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>
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)
69 int small_size = (w > h ? h : w);
71 int bevel_size = small_size * (bevel_pct / 100.0);
73 /* Use a proportionally larger bevel size for especially small images. */
74 if (bevel_size < 20 && small_size > 40) bevel_size = 20;
75 else if (bevel_size < 10 && small_size > 20) bevel_size = 10;
76 else if (bevel_size < 5) /* too small to bother bevelling */
79 /* Ensure the pixbuf has an alpha channel. */
80 if (! gdk_pixbuf_get_has_alpha (pb))
82 GdkPixbuf *pb2 = gdk_pixbuf_add_alpha (pb, FALSE, 0, 0, 0);
83 gdk_pixbuf_unref (pb);
88 guchar *data = gdk_pixbuf_get_pixels (pb);
90 int rs = gdk_pixbuf_get_rowstride (pb);
91 int ch = gdk_pixbuf_get_n_channels (pb);
93 double *ramp = (double *) malloc (sizeof(*ramp) * (bevel_size + 1));
97 fprintf (stderr, "%s: out of memory (%d)\n", progname, bevel_size);
101 for (xx = 0; xx <= bevel_size; xx++)
105 ramp[xx] = xx / (double) bevel_size;
107 # else /* sinusoidal */
108 double p = (xx / (double) bevel_size);
109 double s = sin (p * M_PI / 2);
114 line = data + (rs * y);
115 for (yy = 0; yy < h; yy++)
117 guchar *p = line + (x * ch);
118 for (xx = 0; xx < w; xx++)
122 if (xx < bevel_size) rx = ramp[xx];
123 else if (xx >= w - bevel_size) rx = ramp[w - xx - 1];
126 if (yy < bevel_size) ry = ramp[yy];
127 else if (yy >= h - bevel_size) ry = ramp[h - yy - 1];
139 #if 0 /* show the ramp */
140 line = data + (rs * y);
141 for (yy = 0; yy < h; yy++)
143 guchar *p = line + (x * ch);
144 for (xx = 0; xx < w; xx++)
147 for (cc = 0; cc < ch-1; cc++)
158 fprintf (stderr, "%s: added %d%% bevel (%d px)\n", progname,
159 bevel_pct, bevel_size);
167 paste (const char *paste_file,
168 const char *base_file,
170 double opacity, int bevel_pct,
171 int from_x, int from_y, int to_x, int to_y,
177 int paste_w, paste_h;
180 paste_pb = load_pixbuf (paste_file);
181 base_pb = load_pixbuf (base_file);
183 paste_w = gdk_pixbuf_get_width (paste_pb);
184 paste_h = gdk_pixbuf_get_height (paste_pb);
186 base_w = gdk_pixbuf_get_width (base_pb);
187 base_h = gdk_pixbuf_get_height (base_pb);
191 fprintf (stderr, "%s: loaded %s: %dx%d\n",
192 progname, base_file, base_w, base_h);
193 fprintf (stderr, "%s: loaded %s: %dx%d\n",
194 progname, paste_file, paste_w, paste_h);
197 if (from_scale != 1.0)
199 int new_w = paste_w * from_scale;
200 int new_h = paste_h * from_scale;
201 GdkPixbuf *new_pb = gdk_pixbuf_scale_simple (paste_pb, new_w, new_h,
203 gdk_pixbuf_unref (paste_pb);
205 paste_w = gdk_pixbuf_get_width (paste_pb);
206 paste_h = gdk_pixbuf_get_height (paste_pb);
209 fprintf (stderr, "%s: %s: scaled to %dx%d (%.2f)\n",
210 progname, paste_file, paste_w, paste_h, from_scale);
213 if (w == 0) w = paste_w - from_x;
214 if (h == 0) h = paste_h - from_y;
226 if (from_x < 0) /* from left out of bounds */
233 if (from_y < 0) /* from top out of bounds */
240 if (to_x < 0) /* to left out of bounds */
248 if (to_y < 0) /* to top out of bounds */
256 if (from_x + w > paste_w) /* from right out of bounds */
258 w = paste_w - from_x;
262 if (from_y + h > paste_h) /* from bottom out of bounds */
264 h = paste_h - from_y;
268 if (to_x + w > base_w) /* to right out of bounds */
274 if (to_y + h > base_h) /* to bottom out of bounds */
281 if (clipped && verbose_p)
283 fprintf (stderr, "clipped from: %dx%d %d,%d %d,%d\n",
284 ow, oh, ofx, ofy, otx, oty);
285 fprintf (stderr, "clipped to: %dx%d %d,%d %d,%d\n",
286 w, h, from_x, from_y, to_x, to_y);
291 bevel_image (&paste_pb, bevel_pct,
292 from_x, from_y, w, h);
294 if (opacity == 1.0 && bevel_pct == 0)
295 gdk_pixbuf_copy_area (paste_pb,
296 from_x, from_y, w, h,
301 from_x++; /* gdk_pixbuf_composite gets confused about the bevel: */
302 from_y++; /* it leaves a stripe on the top and left if we try to */
303 to_x++; /* start at 0,0, so pull it right and down by 1 pixel. */
304 to_y++; /* (problem seen in gtk2-2.4.14-2.fc3) */
309 gdk_pixbuf_composite (paste_pb, base_pb,
311 to_x - from_x, to_y - from_y,
318 fprintf (stderr, "%s: pasted %dx%d from %d,%d to %d,%d\n",
319 progname, paste_w, paste_h, from_x, from_y, to_x, to_y);
321 gdk_pixbuf_unref (paste_pb);
322 write_pixbuf (base_pb, base_file);
323 gdk_pixbuf_unref (base_pb);
328 write_pixbuf (GdkPixbuf *pb, const char *file)
330 int jpeg_quality = 85;
332 int w = gdk_pixbuf_get_width (pb);
333 int h = gdk_pixbuf_get_height (pb);
334 guchar *data = gdk_pixbuf_get_pixels (pb);
335 int ww = gdk_pixbuf_get_rowstride (pb);
336 int channels = gdk_pixbuf_get_n_channels (pb);
338 struct jpeg_compress_struct cinfo;
339 struct jpeg_error_mgr jerr;
344 fprintf (stderr, "%s: %d channels?\n", progname, channels);
348 cinfo.err = jpeg_std_error (&jerr);
349 jpeg_create_compress (&cinfo);
351 out = fopen (file, "wb");
355 sprintf (buf, "%.100s: %.100s", progname, file);
360 fprintf (stderr, "%s: writing %s...", progname, file);
362 jpeg_stdio_dest (&cinfo, out);
364 cinfo.image_width = w;
365 cinfo.image_height = h;
366 cinfo.input_components = channels;
367 cinfo.in_color_space = JCS_RGB;
369 jpeg_set_defaults (&cinfo);
370 jpeg_simple_progression (&cinfo);
371 jpeg_set_quality (&cinfo, jpeg_quality, TRUE);
373 jpeg_start_compress (&cinfo, TRUE);
374 add_jpeg_comment (&cinfo);
378 guchar *end = d + (ww * h);
381 jpeg_write_scanlines (&cinfo, &d, 1);
386 jpeg_finish_compress (&cinfo);
387 jpeg_destroy_compress (&cinfo);
393 if (fstat (fileno (out), &st))
396 sprintf (buf, "%.100s: %.100s", progname, file);
400 fprintf (stderr, " %luK\n", ((unsigned long) st.st_size + 1023) / 1024);
408 add_jpeg_comment (struct jpeg_compress_struct *cinfo)
410 time_t now = time ((time_t *) 0);
411 struct tm *tm = localtime (&now);
414 " Generated by WebCollage: Exterminate All Rational Thought. \r\n"
415 " Copyright (c) 1999-%Y by Jamie Zawinski <jwz@jwz.org> \r\n"
417 " http://www.jwz.org/webcollage/ \r\n"
419 " This is what the web looked like on %d %b %Y at %I:%M:%S %p %Z. \r\n"
421 char comment[sizeof(fmt) + 100];
422 strftime (comment, sizeof(comment)-1, fmt, tm);
423 jpeg_write_marker (cinfo, JPEG_COM,
424 (unsigned char *) comment,
432 fprintf (stderr, "usage: %s [-v] paste-file base-file\n"
433 "\t from-scale opacity\n"
434 "\t from-x from-y to-x to-y w h\n"
436 "\t Pastes paste-file into base-file.\n"
437 "\t base-file will be overwritten (with JPEG data.)\n"
438 "\t scaling is applied first: coordinates apply to scaled image.\n",
445 main (int argc, char **argv)
448 char *paste_file, *base_file, *s, dummy;
449 double from_scale, opacity;
450 int from_x, from_y, to_x, to_y, w, h, bevel_pct;
453 progname = argv[i++];
454 s = strrchr (progname, '/');
455 if (s) progname = s+1;
457 if (argc != 11 && argc != 12) usage();
459 if (!strcmp(argv[i], "-v"))
462 paste_file = argv[i++];
463 base_file = argv[i++];
465 if (*paste_file == '-') usage();
466 if (*base_file == '-') usage();
469 if (1 != sscanf (s, " %lf %c", &from_scale, &dummy)) usage();
470 if (from_scale <= 0 || from_scale > 100) usage();
473 if (1 != sscanf (s, " %lf %c", &opacity, &dummy)) usage();
474 if (opacity <= 0 || opacity > 1) usage();
476 s = argv[i++]; if (1 != sscanf (s, " %d %c", &from_x, &dummy)) usage();
477 s = argv[i++]; if (1 != sscanf (s, " %d %c", &from_y, &dummy)) usage();
478 s = argv[i++]; if (1 != sscanf (s, " %d %c", &to_x, &dummy)) usage();
479 s = argv[i++]; if (1 != sscanf (s, " %d %c", &to_y, &dummy)) usage();
480 s = argv[i++]; if (1 != sscanf (s, " %d %c", &w, &dummy)) usage();
481 s = argv[i++]; if (1 != sscanf (s, " %d %c", &h, &dummy)) usage();
483 bevel_pct = 10; /* #### */
490 #endif /* HAVE_GTK2 */
492 paste (paste_file, base_file,
493 from_scale, opacity, bevel_pct,
494 from_x, from_y, to_x, to_y,
499 #endif /* HAVE_GDK_PIXBUF && HAVE_JPEGLIB -- whole file */