1 /* ximage-loader.c --- converts image files or data to XImages or Pixmap.
2 * xscreensaver, Copyright (c) 1998-2018 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
24 # include <X11/Xlib.h>
25 # include <X11/Xutil.h>
28 #include "ximage-loader.h"
30 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_COCOA) || defined(HAVE_ANDROID)
35 # include "grabscreen.h" /* for osx_load_image_file() */
38 #ifdef HAVE_GDK_PIXBUF
39 # include <gdk-pixbuf/gdk-pixbuf.h>
41 # include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
42 # else /* !HAVE_GTK2 */
43 # include <gdk-pixbuf/gdk-pixbuf-xlib.h>
44 # endif /* !HAVE_GTK2 */
45 #endif /* HAVE_GDK_PIXBUF */
52 /* So that debug output shows up in logcat... */
53 extern void Log(const char *format, ...);
55 # define fprintf(S, ...) Log(__VA_ARGS__)
58 extern char *progname;
63 union { int i; char c[sizeof(int)]; } u;
69 #ifdef HAVE_GDK_PIXBUF
71 /* Loads the image to an XImage, RGBA -- GDK Pixbuf version.
74 make_ximage (Display *dpy, Visual *visual, const char *filename,
75 const unsigned char *image_data, unsigned long data_size)
78 static int initted = 0;
86 # if !GLIB_CHECK_VERSION(2, 36 ,0)
90 gdk_pixbuf_xlib_init (dpy, DefaultScreen (dpy));
91 xlib_rgb_init (dpy, DefaultScreenOfDisplay (dpy));
98 pb = gdk_pixbuf_new_from_file (filename, &gerr);
101 fprintf (stderr, "%s: %s\n", progname, gerr->message);
105 pb = gdk_pixbuf_new_from_file (filename);
108 fprintf (stderr, "%s: GDK unable to load %s\n", progname, filename);
111 # endif /* HAVE_GTK2 */
117 g_memory_input_stream_new_from_data (image_data, data_size, 0);
118 pb = gdk_pixbuf_new_from_stream (s, 0, &gerr);
122 fprintf (stderr, "%s: GDK unable to parse built-in image data\n",
126 # else /* !HAVE_GTK2 */
127 fprintf (stderr, "%s: image loading not supported with GTK 1.x\n",
130 # endif /* !HAVE_GTK2 */
137 int w = gdk_pixbuf_get_width (pb);
138 int h = gdk_pixbuf_get_height (pb);
139 guchar *row = gdk_pixbuf_get_pixels (pb);
140 int stride = gdk_pixbuf_get_rowstride (pb);
141 int chan = gdk_pixbuf_get_n_channels (pb);
144 image = XCreateImage (dpy, visual, 32, ZPixmap, 0, 0, w, h, 32, 0);
145 image->data = (char *) malloc(h * image->bytes_per_line);
147 /* Set the bit order in the XImage structure to whatever the
148 local host's native bit order is.
150 image->bitmap_bit_order =
152 (bigendian() ? MSBFirst : LSBFirst);
156 fprintf (stderr, "%s: out of memory (%d x %d)\n", progname, w, h);
160 for (y = 0; y < h; y++)
163 for (x = 0; x < w; x++)
165 unsigned long rgba = 0;
168 rgba = ((0xFF << 24) |
175 rgba = ((0xFF << 24) |
182 rgba = ((i[3] << 24) |
192 XPutPixel (image, x, y, rgba);
202 #elif defined(HAVE_JWXYZ) /* MacOS, iOS or Android */
204 /* Loads the image to an XImage, RGBA -- MacOS, iOS or Android version.
207 make_ximage (Display *dpy, Visual *visual, const char *filename,
208 const unsigned char *image_data, unsigned long data_size)
214 # ifdef HAVE_COCOA /* MacOS */
216 Screen *screen = DefaultScreenOfDisplay (dpy);
217 Window window = RootWindowOfScreen (screen);
218 XWindowAttributes xgwa;
219 XGetWindowAttributes (dpy, window, &xgwa);
221 XCreatePixmap (dpy, window, xgwa.width, xgwa.height, xgwa.depth);
224 if (! osx_load_image_file (screen, window, pixmap, filename, &geom))
226 fprintf (stderr, "%s: %s failed\n", progname, filename);
230 ximage = XGetImage (dpy, pixmap, geom.x, geom.y,
231 geom.width, geom.height,
233 if (!ximage) abort();
235 /* Have to convert ABGR to RGBA */
236 for (y = 0; y < ximage->height; y++)
237 for (x = 0; x < ximage->width; x++)
239 unsigned long p = XGetPixel (ximage, x, y);
240 unsigned long a = (p >> 24) & 0xFF;
241 unsigned long b = (p >> 16) & 0xFF;
242 unsigned long g = (p >> 8) & 0xFF;
243 unsigned long r = (p >> 0) & 0xFF;
244 p = (r << 24) | (g << 16) | (b << 8) | (a << 0);
245 XPutPixel (ximage, x, y, p);
248 XFreePixmap (dpy, pixmap);
250 # else /* !HAVE_COCOA -- iOS or Android. */
251 fprintf (stderr, "%s: image file loading not supported\n", progname);
253 # endif /* !HAVE_COCOA */
257 ximage = jwxyz_png_to_ximage (dpy, visual, image_data, data_size);
263 #elif defined(HAVE_LIBPNG)
266 const unsigned char *buf;
271 png_reader_fn (png_structp png_ptr, png_bytep buf, png_size_t siz)
273 png_read_closure *r = png_get_io_ptr (png_ptr);
274 if (siz > r->siz - r->ptr)
275 png_error (png_ptr, "PNG internal read error");
276 memcpy (buf, r->buf + r->ptr, siz);
281 /* Loads the image to an XImage, RGBA -- libpng version.
284 make_ximage (Display *dpy, Visual *visual,
285 const char *filename, const unsigned char *image_data,
286 unsigned long data_size)
292 png_uint_32 width, height, channels;
293 int bit_depth, color_type, interlace_type;
296 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
297 if (!png_ptr) return 0;
299 info_ptr = png_create_info_struct (png_ptr);
302 png_destroy_read_struct (&png_ptr, 0, 0);
306 end_info = png_create_info_struct (png_ptr);
309 png_destroy_read_struct (&png_ptr, &info_ptr, 0);
313 if (setjmp (png_jmpbuf(png_ptr)))
315 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
321 fp = fopen (filename, "r");
324 fprintf (stderr, "%s: unable to read %s\n", progname, filename);
327 png_init_io (png_ptr, fp);
331 png_read_closure closure;
332 closure.buf = image_data;
333 closure.siz = data_size;
335 png_set_read_fn (png_ptr, (void *) &closure, png_reader_fn);
338 png_read_info (png_ptr, info_ptr);
339 png_get_IHDR (png_ptr, info_ptr,
340 &width, &height, &bit_depth, &color_type,
341 &interlace_type, 0, 0);
343 png_set_strip_16 (png_ptr); /* Truncate 16 bits per component to 8 */
344 png_set_packing (png_ptr); /* Unpack to 1 pixel per byte */
347 if (color_type == PNG_COLOR_TYPE_PALETTE) /* Colormap to RGB */
348 png_set_palette_rgb (png_ptr);
350 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) /* Mono to 8bit */
351 png_set_gray_1_2_4_to_8 (png_ptr);
354 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) /* Fix weird alpha */
355 png_set_tRNS_to_alpha (png_ptr);
357 /* At least 8 bits deep */
358 if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
359 png_set_expand (png_ptr);
361 if (bit_depth == 8 && /* Convert RGB to RGBA */
362 (color_type == PNG_COLOR_TYPE_RGB ||
363 color_type == PNG_COLOR_TYPE_PALETTE))
364 png_set_filler (png_ptr, 0xFF, PNG_FILLER_AFTER);
366 /* Grayscale to color */
367 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
368 png_set_expand (png_ptr);
371 /* Convert graysale to color */
372 if (color_type == PNG_COLOR_TYPE_GRAY ||
373 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
374 png_set_gray_to_rgb (png_ptr);
379 if (png_get_bKGD (png_ptr, info_ptr, &bg))
380 png_set_background (png_ptr, bg, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
385 png_read_update_info (png_ptr, info_ptr);
387 channels = png_get_channels (png_ptr, info_ptr);
390 png_bytep *rows = png_malloc (png_ptr, height * sizeof(*rows));
392 for (y = 0; y < height; y++)
393 rows[y] = png_malloc (png_ptr, png_get_rowbytes (png_ptr, info_ptr));
394 png_read_image (png_ptr, rows);
395 png_read_end (png_ptr, info_ptr);
397 image = XCreateImage (dpy, visual, 32, ZPixmap, 0, 0,
398 width, height, 32, 0);
399 image->data = (char *) malloc (height * image->bytes_per_line);
401 /* Set the bit order in the XImage structure to whatever the
402 local host's native bit order is.
404 image->bitmap_bit_order =
406 (bigendian() ? MSBFirst : LSBFirst);
410 fprintf (stderr, "%s: out of memory (%lu x %lu)\n",
411 progname, (unsigned long)width, (unsigned long)height);
415 for (y = 0; y < height; y++)
417 png_bytep i = rows[y];
418 for (x = 0; x < width; x++)
423 rgba = ((i[3] << 24) |
429 rgba = ((0xFF << 24) |
435 rgba = ((i[1] << 24) |
441 rgba = ((0xFF << 24) |
449 XPutPixel (image, x, y, rgba);
452 png_free (png_ptr, rows[y]);
455 png_free (png_ptr, rows);
458 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
465 #else /* No image loaders! */
468 make_ximage (Display *dpy, Visual *visual,
469 const char *filename, const unsigned char *image_data,
470 unsigned long data_size)
472 fprintf (stderr, "%s: no image loading support!\n", progname);
476 #endif /* no loaders */
479 /* Given a bitmask, returns the position and width of the field.
482 decode_mask (unsigned long mask, unsigned long *pos_ret,
483 unsigned long *size_ret)
486 for (i = 0; i < 32; i++)
487 if (mask & (1L << i))
491 for (; i < 32; i++, j++)
492 if (! (mask & (1L << i)))
500 /* Loads the image to a Pixmap and optional 1-bit mask.
503 make_pixmap (Display *dpy, Window window,
504 const char *filename,
505 const unsigned char *image_data, unsigned long data_size,
506 int *width_ret, int *height_ret, Pixmap *mask_ret)
508 XWindowAttributes xgwa;
509 XImage *in, *out, *mask = 0;
515 unsigned long crpos=0, cgpos=0, cbpos=0, capos=0; /* bitfield positions */
516 unsigned long srpos=0, sgpos=0, sbpos=0;
517 unsigned long srmsk=0, sgmsk=0, sbmsk=0;
518 unsigned long srsiz=0, sgsiz=0, sbsiz=0;
521 // BlackPixel has alpha: 0xFF000000.
522 unsigned long black = BlackPixelOfScreen (DefaultScreenOfDisplay (dpy));
524 unsigned long black = 0;
527 XGetWindowAttributes (dpy, window, &xgwa);
529 in = make_ximage (dpy, xgwa.visual, filename, image_data, data_size);
532 /* Create a new image in the depth and bit-order of the server. */
533 out = XCreateImage (dpy, xgwa.visual, xgwa.depth, ZPixmap, 0, 0,
534 in->width, in->height, 8, 0);
536 out->bitmap_bit_order = in->bitmap_bit_order;
537 out->byte_order = in->byte_order;
539 out->bitmap_bit_order = BitmapBitOrder (dpy);
540 out->byte_order = ImageByteOrder (dpy);
542 out->data = (char *) malloc (out->height * out->bytes_per_line);
543 if (!out->data) abort();
547 mask = XCreateImage (dpy, xgwa.visual, 1, XYPixmap, 0, 0,
548 in->width, in->height, 8, 0);
549 mask->byte_order = in->byte_order;
550 mask->data = (char *) malloc (mask->height * mask->bytes_per_line);
553 /* Find the server's color masks.
555 srmsk = out->red_mask;
556 sgmsk = out->green_mask;
557 sbmsk = out->blue_mask;
559 if (!(srmsk && sgmsk && sbmsk)) abort(); /* No server color masks? */
561 decode_mask (srmsk, &srpos, &srsiz);
562 decode_mask (sgmsk, &sgpos, &sgsiz);
563 decode_mask (sbmsk, &sbpos, &sbsiz);
565 /* 'in' is RGBA in client endianness. Convert to what the server wants. */
567 crpos = 24, cgpos = 16, cbpos = 8, capos = 0;
569 crpos = 0, cgpos = 8, cbpos = 16, capos = 24;
571 for (y = 0; y < in->height; y++)
572 for (x = 0; x < in->width; x++)
574 unsigned long p = XGetPixel (in, x, y);
575 unsigned char a = (p >> capos) & 0xFF;
576 unsigned char b = (p >> cbpos) & 0xFF;
577 unsigned char g = (p >> cgpos) & 0xFF;
578 unsigned char r = (p >> crpos) & 0xFF;
579 XPutPixel (out, x, y, ((r << srpos) |
584 XPutPixel (mask, x, y, (a ? 1 : 0));
590 pixmap = XCreatePixmap (dpy, window, out->width, out->height, xgwa.depth);
591 gc = XCreateGC (dpy, pixmap, 0, &gcv);
592 XPutImage (dpy, pixmap, gc, out, 0, 0, 0, 0, out->width, out->height);
597 Pixmap p2 = XCreatePixmap (dpy, window, mask->width, mask->height, 1);
600 gc = XCreateGC (dpy, p2, GCForeground|GCBackground, &gcv);
601 XPutImage (dpy, p2, gc, mask, 0, 0, 0, 0, mask->width, mask->height);
603 XDestroyImage (mask);
608 if (width_ret) *width_ret = out->width;
609 if (height_ret) *height_ret = out->height;
617 /* Textures are upside down, so invert XImages before returning them.
620 flip_ximage (XImage *ximage)
622 char *data2, *in, *out;
625 data2 = malloc (ximage->bytes_per_line * ximage->height);
628 out = data2 + ximage->bytes_per_line * (ximage->height - 1);
629 for (y = 0; y < ximage->height; y++)
631 memcpy (out, in, ximage->bytes_per_line);
632 in += ximage->bytes_per_line;
633 out -= ximage->bytes_per_line;
636 ximage->data = data2;
641 image_data_to_pixmap (Display *dpy, Window window,
642 const unsigned char *image_data, unsigned long data_size,
643 int *width_ret, int *height_ret,
646 return make_pixmap (dpy, window, 0, image_data, data_size,
647 width_ret, height_ret, mask_ret);
651 file_to_pixmap (Display *dpy, Window window, const char *filename,
652 int *width_ret, int *height_ret,
655 return make_pixmap (dpy, window, filename, 0, 0,
656 width_ret, height_ret, mask_ret);
661 image_data_to_ximage (Display *dpy, Visual *visual,
662 const unsigned char *image_data,
663 unsigned long data_size)
665 XImage *ximage = make_ximage (dpy, visual, 0, image_data, data_size);
666 flip_ximage (ximage);
670 /* This XImage has RGBA data, which is what OpenGL code typically expects.
671 X11 typically expects 0RGB as it has no notion of alpha, only 1-bit masks.
672 With X11 code, you should probably use the _pixmap routines instead.
675 file_to_ximage (Display *dpy, Visual *visual, const char *filename)
677 XImage *ximage = make_ximage (dpy, visual, filename, 0, 0);
678 flip_ximage (ximage);