2
Copyright (C) 2007-2010 Fons Adriaensen <fons@linuxaudio.org>
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
#include <clxclient.h>
25
XImage *png2img (const char *file, X_display *disp, XftColor *bgnd)
31
const unsigned char **data, *p;
33
float vr, vg, vb, va, br, bg, bb;
34
unsigned long mr, mg, mb, pix;
37
F = fopen (file, "r");
40
fprintf (stderr, "Can't open '%s'\n", file);
44
if (png_sig_cmp (hdr, 0, 8))
46
fprintf (stderr, "'%s' is not a PNG file\n", file);
49
fseek (F, 0, SEEK_SET);
51
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
57
png_info = png_create_info_struct (png_ptr);
60
png_destroy_read_struct (&png_ptr, 0, 0);
64
if (setjmp (png_jmpbuf (png_ptr)))
66
png_destroy_read_struct (&png_ptr, &png_info, 0);
68
fprintf (stderr, "png:longjmp()\n");
72
png_init_io (png_ptr, F);
73
png_read_png (png_ptr, png_info,
74
PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND,
77
// This requires libpng14 or later. If you still have an
78
// older version, use the three commented lines instead.
80
dx = png_get_image_width (png_ptr, png_info);
81
dy = png_get_image_height (png_ptr, png_info);
82
dp = (png_get_color_type (png_ptr, png_info) & PNG_COLOR_MASK_ALPHA) ? 4 : 3;
84
// dx = png_info->width;
85
// dy = png_info->height;
86
// dp = (png_info->color_type & PNG_COLOR_MASK_ALPHA) ? 4 : 3;
88
data = (const unsigned char **)(png_get_rows (png_ptr, png_info));
90
image = XCreateImage (disp->dpy (),
92
DefaultDepth (disp->dpy (), disp->dsn ()),
93
ZPixmap, 0, 0, dx, dy, 32, 0);
94
image->data = new char [image->height * image->bytes_per_line];
97
mg = image->green_mask;
98
mb = image->blue_mask;
104
br = bgnd->color.red >> 8;
105
bg = bgnd->color.green >> 8;
106
bb = bgnd->color.blue >> 8;
108
else br = bg = bb = 0;
110
for (y = 0; y < dy; y++)
113
for (x = 0; x < dx; x++)
115
va = (dp == 4) ? (p [3] / 255.0f) : 1;
116
pix = ((unsigned long)((p [0] * va + (1 - va) * br) * vr) & mr)
117
| ((unsigned long)((p [1] * va + (1 - va) * bg) * vg) & mg)
118
| ((unsigned long)((p [2] * va + (1 - va) * bb) * vb) & mb);
119
XPutPixel (image, x, y, pix);
124
png_destroy_read_struct (&png_ptr, &png_info, 0);