~ubuntu-branches/ubuntu/quantal/tetraproc/quantal

« back to all changes in this revision

Viewing changes to source/png2img.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-03-24 14:56:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110324145648-r9qi83fs3wej4801
Tags: upstream-0.8.2
ImportĀ upstreamĀ versionĀ 0.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2007-2010 Fons Adriaensen <fons@linuxaudio.org>
 
3
    
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
17
*/
 
18
 
 
19
 
 
20
#include <png.h>
 
21
#include <clxclient.h>
 
22
 
 
23
 
 
24
 
 
25
XImage *png2img (const char *file, X_display *disp, XftColor *bgnd)
 
26
{
 
27
    FILE                 *F;
 
28
    png_byte             hdr [8];
 
29
    png_structp          png_ptr;
 
30
    png_infop            png_info;
 
31
    const unsigned char  **data, *p;
 
32
    int                  dx, dy, x, y, dp;
 
33
    float                vr, vg, vb, va, br, bg, bb;
 
34
    unsigned long        mr, mg, mb, pix;
 
35
    XImage               *image;
 
36
 
 
37
    F = fopen (file, "r");
 
38
    if (!F)
 
39
    {
 
40
        fprintf (stderr, "Can't open '%s'\n", file);
 
41
        return 0;
 
42
    }
 
43
    fread (hdr, 1, 8, F);
 
44
    if (png_sig_cmp (hdr, 0, 8))
 
45
    {
 
46
        fprintf (stderr, "'%s' is not a PNG file\n", file);
 
47
        return 0;
 
48
    }
 
49
    fseek (F, 0, SEEK_SET);
 
50
 
 
51
    png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
 
52
    if (! png_ptr)
 
53
    {
 
54
        fclose (F);
 
55
        return 0;
 
56
    }
 
57
    png_info = png_create_info_struct (png_ptr);
 
58
    if (! png_info)
 
59
    {
 
60
        png_destroy_read_struct (&png_ptr, 0, 0);
 
61
        fclose (F);
 
62
        return 0;
 
63
    }
 
64
    if (setjmp (png_jmpbuf (png_ptr)))
 
65
    {
 
66
        png_destroy_read_struct (&png_ptr, &png_info, 0);
 
67
        fclose (F);
 
68
        fprintf (stderr, "png:longjmp()\n");
 
69
        return 0;
 
70
    }
 
71
 
 
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,
 
75
                  0);
 
76
 
 
77
// This requires libpng14 or later. If you still have an
 
78
// older version, use the three commented lines instead.
 
79
 
 
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;
 
83
 
 
84
//    dx = png_info->width;
 
85
//    dy = png_info->height;
 
86
//    dp = (png_info->color_type & PNG_COLOR_MASK_ALPHA) ? 4 : 3;
 
87
 
 
88
    data = (const unsigned char **)(png_get_rows (png_ptr, png_info));
 
89
 
 
90
    image = XCreateImage (disp->dpy (),
 
91
                          disp->dvi (),
 
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];
 
95
 
 
96
    mr = image->red_mask;
 
97
    mg = image->green_mask;
 
98
    mb = image->blue_mask;
 
99
    vr = mr / 255.0f;
 
100
    vg = mg / 255.0f;
 
101
    vb = mb / 255.0f;
 
102
    if (bgnd)
 
103
    {
 
104
        br = bgnd->color.red   >> 8;
 
105
        bg = bgnd->color.green >> 8;
 
106
        bb = bgnd->color.blue  >> 8;
 
107
    }
 
108
    else br = bg = bb = 0;
 
109
 
 
110
    for (y = 0; y < dy; y++)
 
111
    {
 
112
        p = data [y];
 
113
        for (x = 0; x < dx; x++)
 
114
        {
 
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);
 
120
            p += dp;
 
121
        }
 
122
    }
 
123
 
 
124
    png_destroy_read_struct (&png_ptr, &png_info, 0);
 
125
    fclose (F);
 
126
 
 
127
    return image;
 
128
}