~mc-return/compiz/compiz.merge-src-screen.cpp-improvements

« back to all changes in this revision

Viewing changes to plugins/imgpng/src/imgpng.cpp

  • Committer: Dennis kasprzyk
  • Author(s): Dennis Kasprzyk
  • Date: 2009-03-15 05:09:18 UTC
  • Revision ID: git-v1:163f6b6f3c3b7764987cbdf8e03cc355edeaa499
New generalized build system.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2006 Novell, Inc.
 
3
 *
 
4
 * Permission to use, copy, modify, distribute, and sell this software
 
5
 * and its documentation for any purpose is hereby granted without
 
6
 * fee, provided that the above copyright notice appear in all copies
 
7
 * and that both that copyright notice and this permission notice
 
8
 * appear in supporting documentation, and that the name of
 
9
 * Novell, Inc. not be used in advertising or publicity pertaining to
 
10
 * distribution of the software without specific, written prior permission.
 
11
 * Novell, Inc. makes no representations about the suitability of this
 
12
 * software for any purpose. It is provided "as is" without express or
 
13
 * implied warranty.
 
14
 *
 
15
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 
16
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 
17
 * NO EVENT SHALL NOVELL, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 
18
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 
19
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 
20
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 
21
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
22
 *
 
23
 * Author: David Reveman <davidr@novell.com>
 
24
 */
 
25
 
 
26
#include "imgpng.h"
 
27
 
 
28
COMPIZ_PLUGIN_20081216 (imgpng, PngPluginVTable)
 
29
 
 
30
PngScreen::PngScreen (CompScreen *screen) :
 
31
    PrivateHandler<PngScreen, CompScreen> (screen)
 
32
{
 
33
    ScreenInterface::setHandler (screen, true);
 
34
 
 
35
    screen->updateDefaultIcon ();
 
36
}
 
37
 
 
38
PngScreen::~PngScreen ()
 
39
{
 
40
    screen->updateDefaultIcon ();
 
41
}
 
42
 
 
43
static void
 
44
premultiplyData (png_structp   png,
 
45
                 png_row_infop row_info,
 
46
                 png_bytep     data)
 
47
{
 
48
    unsigned int i;
 
49
 
 
50
    for (i = 0; i < row_info->rowbytes; i += 4)
 
51
    {
 
52
        unsigned char *base = &data[i];
 
53
        unsigned char blue  = base[0];
 
54
        unsigned char green = base[1];
 
55
        unsigned char red   = base[2];
 
56
        unsigned char alpha = base[3];
 
57
        int           p;
 
58
 
 
59
        red   = (unsigned) red   * (unsigned) alpha / 255;
 
60
        green = (unsigned) green * (unsigned) alpha / 255;
 
61
        blue  = (unsigned) blue  * (unsigned) alpha / 255;
 
62
 
 
63
        p = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
 
64
        memcpy (base, &p, sizeof (int));
 
65
    }
 
66
}
 
67
 
 
68
bool
 
69
PngScreen::readPngData (png_struct *png,
 
70
                        png_info   *info,
 
71
                        void       *&data,
 
72
                        CompSize   &size)
 
73
{
 
74
    png_uint_32  pngWidth, pngHeight;
 
75
    int          depth, colorType, interlace, i;
 
76
    unsigned int pixelSize;
 
77
    png_byte     **rowPointers;
 
78
    char         *d;
 
79
 
 
80
    png_read_info (png, info);
 
81
 
 
82
    png_get_IHDR (png, info,
 
83
                  &pngWidth, &pngHeight, &depth,
 
84
                  &colorType, &interlace, NULL, NULL);
 
85
 
 
86
    size.setWidth (pngWidth);
 
87
    size.setHeight (pngHeight);
 
88
 
 
89
    /* convert palette/gray image to rgb */
 
90
    if (colorType == PNG_COLOR_TYPE_PALETTE)
 
91
        png_set_palette_to_rgb (png);
 
92
 
 
93
    /* expand gray bit depth if needed */
 
94
    if (colorType == PNG_COLOR_TYPE_GRAY && depth < 8)
 
95
        png_set_gray_1_2_4_to_8 (png);
 
96
 
 
97
    /* transform transparency to alpha */
 
98
    if (png_get_valid(png, info, PNG_INFO_tRNS))
 
99
        png_set_tRNS_to_alpha (png);
 
100
 
 
101
    if (depth == 16)
 
102
        png_set_strip_16 (png);
 
103
 
 
104
    if (depth < 8)
 
105
        png_set_packing (png);
 
106
 
 
107
    /* convert grayscale to RGB */
 
108
    if (colorType == PNG_COLOR_TYPE_GRAY ||
 
109
        colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
 
110
        png_set_gray_to_rgb (png);
 
111
 
 
112
    if (interlace != PNG_INTERLACE_NONE)
 
113
        png_set_interlace_handling (png);
 
114
 
 
115
    png_set_bgr (png);
 
116
    png_set_filler (png, 0xff, PNG_FILLER_AFTER);
 
117
 
 
118
    png_set_read_user_transform_fn (png, premultiplyData);
 
119
 
 
120
    png_read_update_info (png, info);
 
121
 
 
122
    pixelSize = 4;
 
123
    d = (char *) malloc (pngWidth * pngHeight * pixelSize);
 
124
    if (!d)
 
125
        return false;
 
126
 
 
127
    data = d;
 
128
 
 
129
    rowPointers = new png_byte *[pngHeight];
 
130
    if (!rowPointers)
 
131
    {
 
132
        free (d);
 
133
        return false;
 
134
    }
 
135
 
 
136
    for (i = 0; i < pngHeight; i++)
 
137
        rowPointers[i] = (png_byte *) (d + i * pngWidth * pixelSize);
 
138
 
 
139
    png_read_image (png, rowPointers);
 
140
    png_read_end (png, info);
 
141
 
 
142
    delete [] rowPointers;
 
143
 
 
144
    return true;
 
145
}
 
146
 
 
147
static void
 
148
stdioReadFunc (png_structp png,
 
149
               png_bytep   data,
 
150
               png_size_t  size)
 
151
{
 
152
    std::ifstream *file = (std::ifstream *) png_get_io_ptr (png);
 
153
 
 
154
    file->read ((char *) data, size);
 
155
    if (file->fail ())
 
156
        png_error (png, "Read Error");
 
157
}
 
158
 
 
159
bool
 
160
PngScreen::readPng (std::ifstream &file,
 
161
                    CompSize      &size,
 
162
                    void          *&data)
 
163
{
 
164
    unsigned char png_sig[PNG_SIG_SIZE];
 
165
    png_struct    *png;
 
166
    png_info      *info;
 
167
    bool          status;
 
168
 
 
169
    file.read ((char *) png_sig, PNG_SIG_SIZE);
 
170
    if (file.fail ())
 
171
        return false;
 
172
    if (png_check_sig (png_sig, PNG_SIG_SIZE) == 0)
 
173
        return false;
 
174
 
 
175
    png = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
 
176
    if (!png)
 
177
        return false;
 
178
 
 
179
    info = png_create_info_struct (png);
 
180
    if (!info)
 
181
    {
 
182
        png_destroy_read_struct (&png, NULL, NULL);
 
183
        return false;
 
184
    }
 
185
 
 
186
    png_set_read_fn (png, &file, stdioReadFunc);
 
187
    png_set_sig_bytes (png, PNG_SIG_SIZE);
 
188
 
 
189
    status = readPngData (png, info, data, size);
 
190
 
 
191
    png_destroy_read_struct (&png, &info, NULL);
 
192
 
 
193
    return status;
 
194
}
 
195
 
 
196
static void
 
197
stdioWriteFunc (png_structp png,
 
198
                png_bytep   data,
 
199
                png_size_t  size)
 
200
{
 
201
    std::ofstream *file = (std::ofstream *) png_get_io_ptr (png);
 
202
 
 
203
    file->write ((char *) data, size);
 
204
    if (file->bad ())
 
205
        png_error (png, "Write Error");
 
206
}
 
207
 
 
208
bool
 
209
PngScreen::writePng (unsigned char *buffer,
 
210
                     std::ofstream &file,
 
211
                     CompSize      &size,
 
212
                     int           stride)
 
213
{
 
214
    png_struct   *png;
 
215
    png_info     *info;
 
216
    png_byte     **rows;
 
217
    png_color_16 white;
 
218
    int          i, height = size.height ();
 
219
 
 
220
    rows = new png_byte *[height];
 
221
    if (!rows)
 
222
        return false;
 
223
 
 
224
    for (i = 0; i < height; i++)
 
225
        rows[height - i - 1] = buffer + i * stride;
 
226
 
 
227
    png = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
 
228
    if (!png)
 
229
    {
 
230
        delete [] rows;
 
231
        return false;
 
232
    }
 
233
 
 
234
    info = png_create_info_struct (png);
 
235
    if (!info)
 
236
    {
 
237
        png_destroy_write_struct (&png, NULL);
 
238
        delete [] rows;
 
239
        return false;
 
240
    }
 
241
 
 
242
    if (setjmp (png_jmpbuf (png)))
 
243
    {
 
244
        png_destroy_write_struct (&png, NULL);
 
245
        delete [] rows;
 
246
        return false;
 
247
    }
 
248
 
 
249
    png_set_write_fn (png, &file, stdioWriteFunc, NULL);
 
250
 
 
251
    png_set_IHDR (png, info,
 
252
                  size.width (), size.height (), 8,
 
253
                  PNG_COLOR_TYPE_RGB_ALPHA,
 
254
                  PNG_INTERLACE_NONE,
 
255
                  PNG_COMPRESSION_TYPE_DEFAULT,
 
256
                  PNG_FILTER_TYPE_DEFAULT);
 
257
 
 
258
    white.red   = 0xff;
 
259
    white.blue  = 0xff;
 
260
    white.green = 0xff;
 
261
 
 
262
    png_set_bKGD (png, info, &white);
 
263
 
 
264
    png_write_info (png, info);
 
265
    png_write_image (png, rows);
 
266
    png_write_end (png, info);
 
267
 
 
268
    png_destroy_write_struct (&png, &info);
 
269
    delete [] rows;
 
270
 
 
271
    return true;
 
272
}
 
273
 
 
274
CompString
 
275
PngScreen::fileNameWithExtension (CompString &path)
 
276
{
 
277
    unsigned int len = path.length ();
 
278
 
 
279
    if (len > 4 && path.substr (len - 4, 4) == ".png")
 
280
        return path;
 
281
 
 
282
    return path + ".png";
 
283
}
 
284
 
 
285
bool
 
286
PngScreen::imageToFile (CompString &path,
 
287
                        CompString &format,
 
288
                        CompSize   &size,
 
289
                        int        stride,
 
290
                        void       *data)
 
291
{
 
292
    bool          status = false;
 
293
    std::ofstream file;
 
294
    CompString    fileName = fileNameWithExtension (path);
 
295
 
 
296
    if (format == "png")
 
297
    {
 
298
        file.open (fileName.c_str ());
 
299
        if (file.is_open ())
 
300
        {
 
301
            status = writePng ((unsigned char *) data, file, size, stride);
 
302
            file.close ();
 
303
        }
 
304
 
 
305
        if (status)
 
306
            return true;
 
307
    }
 
308
 
 
309
    status = screen->imageToFile (path, format, size, stride, data);
 
310
 
 
311
    if (!status)
 
312
    {
 
313
        file.open (fileName.c_str ());
 
314
        if (file.is_open ())
 
315
        {
 
316
            status = writePng ((unsigned char *) data, file, size, stride);
 
317
            file.close ();
 
318
        }
 
319
    }
 
320
 
 
321
    return status;
 
322
}
 
323
 
 
324
bool
 
325
PngScreen::fileToImage (CompString &name,
 
326
                        CompSize   &size,
 
327
                        int        &stride,
 
328
                        void       *&data)
 
329
{
 
330
    bool          status = false;
 
331
    std::ifstream file;
 
332
    CompString    fileName = fileNameWithExtension (name);
 
333
 
 
334
    file.open (fileName.c_str ());
 
335
    if (file.is_open ())
 
336
    {
 
337
        status = readPng (file, size, data);
 
338
        file.close ();
 
339
    }
 
340
 
 
341
    if (status)
 
342
    {
 
343
        stride = size.width () * 4;
 
344
        return true;
 
345
    }
 
346
 
 
347
    return screen->fileToImage (name, size, stride, data);
 
348
}
 
349
 
 
350
bool
 
351
PngPluginVTable::init ()
 
352
{
 
353
    if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION))
 
354
        return false;
 
355
 
 
356
 
 
357
    getMetadata ()->addFromFile (name ());
 
358
 
 
359
    return true;
 
360
}
 
361