root/trunk/third/nautilus/libnautilus-extensions/bug-5712-pr3-workaround--gdk-pixbuf-drawable.c @ 15547

Revision 15547, 30.0 KB (checked in by ghudson, 9 years ago)

This commit was generated by cvs2svn to compensate for changes in r15546,
which included commits to RCS files with non-trunk default branches.

Line 
1/* FIXME bugzilla.eazel.com 5813:
2 * As soon as gtk 1.2.9 is released, this hack needs to be exorcised.
3 */
4
5/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
6/* GdkPixbuf library - convert X drawable information to RGB
7 *
8 * Copyright (C) 1999 Michael Zucchi
9 *
10 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
11 *          Cody Russell <bratsche@gnome.org>
12 *          Federico Mena-Quintero <federico@gimp.org>
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Library General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 * Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with this library; if not, write to the
26 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
28 */
29
30#include <config.h>
31#include <stdio.h>
32#include <string.h>
33#include "bug-5712-pr3-workaround--gdk-pixbuf-private.h"
34
35#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
36#define LITTLE
37#endif
38#define d(x)
39
40
41
42static guint32 mask_table[] = {
43        0x00000000, 0x00000001, 0x00000003, 0x00000007,
44        0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
45        0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
46        0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
47        0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
48        0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
49        0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
50        0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
51        0xffffffff
52};
53
54
55
56/*
57  convert 1 bits-pixel data
58  no alpha
59*/
60static void
61rgb1 (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
62{
63        int xx, yy;
64        int width, height;
65        int bpl;
66        guint8 *s;
67        register guint8 data;
68        guint8 *o;
69        guint8 *srow = image->mem, *orow = pixels;
70
71        d (printf ("1 bits/pixel\n"));
72
73        /* convert upto 8 pixels/time */
74        /* its probably not worth trying to make this run very fast, who uses
75           1 bit displays anymore? */
76        width = image->width;
77        height = image->height;
78        bpl = image->bpl;
79
80        for (yy = 0; yy < height; yy++) {
81                s = srow;
82                o = orow;
83
84                for (xx = 0; xx < width; xx ++) {
85                        data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
86                        *o++ = colormap->colors[data].red;
87                        *o++ = colormap->colors[data].green;
88                        *o++ = colormap->colors[data].blue;
89                }
90                srow += bpl;
91                orow += rowstride;
92        }
93}
94
95/*
96  convert 1 bits/pixel data
97  with alpha
98*/
99static void
100rgb1a (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
101{
102        int xx, yy;
103        int width, height;
104        int bpl;
105        guint8 *s;
106        register guint8 data;
107        guint8 *o;
108        guint8 *srow = image->mem, *orow = pixels;
109        guint32 remap[2];
110
111        d (printf ("1 bits/pixel\n"));
112
113        /* convert upto 8 pixels/time */
114        /* its probably not worth trying to make this run very fast, who uses
115           1 bit displays anymore? */
116        width = image->width;
117        height = image->height;
118        bpl = image->bpl;
119
120        for (xx = 0; xx < 2; xx++) {
121#ifdef LITTLE
122                remap[xx] = 0xff000000
123                        | colormap->colors[xx].blue << 16
124                        | colormap->colors[xx].green << 8
125                        | colormap->colors[xx].red;
126#else
127                remap[xx] = 0xff
128                        | colormap->colors[xx].red << 24
129                        | colormap->colors[xx].green << 16
130                        | colormap->colors[xx].blue << 8;
131#endif
132        }
133
134        for (yy = 0; yy < height; yy++) {
135                s = srow;
136                o = orow;
137
138                for (xx = 0; xx < width; xx ++) {
139                        data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
140                        *o++ = remap[data];
141                }
142                srow += bpl;
143                orow += rowstride;
144        }
145}
146
147/*
148  convert 8 bits/pixel data
149  no alpha
150*/
151static void
152rgb8 (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
153{
154        int xx, yy;
155        int width, height;
156        int bpl;
157        guint32 mask;
158        register guint32 data;
159        guint8 *srow = image->mem, *orow = pixels;
160        register guint8 *s;
161        register guint8 *o;
162
163        width = image->width;
164        height = image->height;
165        bpl = image->bpl;
166
167        d (printf ("8 bit, no alpha output\n"));
168
169        mask = mask_table[image->depth];
170
171        for (yy = 0; yy < height; yy++) {
172                s = srow;
173                o = orow;
174                for (xx = 0; xx < width; xx++) {
175                        data = *s++ & mask;
176                        *o++ = colormap->colors[data].red;
177                        *o++ = colormap->colors[data].green;
178                        *o++ = colormap->colors[data].blue;
179                }
180                srow += bpl;
181                orow += rowstride;
182        }
183}
184
185/*
186  convert 8 bits/pixel data
187  with alpha
188*/
189static void
190rgb8a (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
191{
192        int xx, yy;
193        int width, height;
194        int bpl;
195        guint32 mask;
196        register guint32 data;
197        guint32 remap[256];
198        register guint8 *s;     /* read 2 pixels at once */
199        register guint32 *o;
200        guint8 *srow = image->mem, *orow = pixels;
201
202        width = image->width;
203        height = image->height;
204        bpl = image->bpl;
205
206        d (printf ("8 bit, with alpha output\n"));
207
208        mask = mask_table[image->depth];
209
210        for (xx = 0; xx < colormap->size; xx++) {
211#ifdef LITTLE
212                remap[xx] = 0xff000000
213                        | colormap->colors[xx].blue << 16
214                        | colormap->colors[xx].green << 8
215                        | colormap->colors[xx].red;
216#else
217                remap[xx] = 0xff
218                        | colormap->colors[xx].red << 24
219                        | colormap->colors[xx].green << 16
220                        | colormap->colors[xx].blue << 8;
221#endif
222        }
223
224        for (yy = 0; yy < height; yy++) {
225                s = srow;
226                o = (guint32 *) orow;
227                for (xx = 0; xx < width; xx ++) {
228                        data = *s++ & mask;
229                        *o++ = remap[data];
230                }
231                srow += bpl;
232                orow += rowstride;
233        }
234}
235
236/*
237  convert 16 bits/pixel data
238  no alpha
239  data in lsb format
240*/
241static void
242rgb565lsb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
243{
244        int xx, yy;
245        int width, height;
246        int bpl;
247
248#ifdef LITTLE
249        register guint32 *s;    /* read 2 pixels at once */
250#else
251        register guint8 *s;     /* read 2 pixels at once */
252#endif
253        register guint16 *o;
254        guint8 *srow = image->mem, *orow = pixels;
255
256        width = image->width;
257        height = image->height;
258        bpl = image->bpl;
259
260        for (yy = 0; yy < height; yy++) {
261#ifdef LITTLE
262                s = (guint32 *) srow;
263#else
264                s = srow;
265#endif
266                o = (guint16 *) orow;
267                for (xx = 1; xx < width; xx += 2) {
268                        register guint32 data;
269#ifdef LITTLE
270                        data = *s++;
271                        *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
272                                | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
273                        *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
274                                | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
275                        *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
276                                | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
277#else
278                        /* swap endianness first */
279                        data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
280                        s += 4;
281                        *o++ = (data & 0xf800) | (data & 0xe000) >> 5
282                                | (data & 0x7e0) >> 3 | (data & 0x600) >> 9;
283                        *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
284                                | (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;
285                        *o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17
286                                | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
287#endif
288                }
289                /* check for last remaining pixel */
290                if (width & 1) {
291                        register guint16 data;
292#ifdef LITTLE
293                        data = *((short *) s);
294#else
295                        data = *((short *) s);
296                        data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
297#endif
298                        ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
299                        ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
300                        ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
301                }
302                srow += bpl;
303                orow += rowstride;
304        }
305}
306
307/*
308  convert 16 bits/pixel data
309  no alpha
310  data in msb format
311*/
312static void
313rgb565msb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
314{
315        int xx, yy;
316        int width, height;
317        int bpl;
318
319#ifdef LITTLE
320        register guint8 *s;     /* need to swap data order */
321#else
322        register guint32 *s;    /* read 2 pixels at once */
323#endif
324        register guint16 *o;
325        guint8 *srow = image->mem, *orow = pixels;
326
327        width = image->width;
328        height = image->height;
329        bpl = image->bpl;
330
331        for (yy = 0; yy < height; yy++) {
332#ifdef LITTLE
333                s = srow;
334#else
335                s = (guint32 *) srow;
336#endif
337                o = (guint16 *) orow;
338                for (xx = 1; xx < width; xx += 2) {
339                        register guint32 data;
340#ifdef LITTLE
341                        /* swap endianness first */
342                        data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
343                        s += 4;
344                        *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
345                                | (data & 0x7e0) << 5 | (data & 0x600) >> 1;
346                        *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
347                                | (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;
348                        *o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25
349                                | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
350#else
351                        data = *s++;
352                        *o++ = (data & 0xf800) | (data & 0xe000) >> 5
353                                | (data & 0x7e0) >> 3 | (data & 0x600) >> 9;
354                        *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
355                                | (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;
356                        *o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17
357                                | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
358#endif
359                }
360                /* check for last remaining pixel */
361                if (width & 1) {
362                        register guint16 data;
363#ifdef LITTLE
364                        data = *((short *) s);
365                        data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
366#else
367                        data = *((short *) s);
368#endif
369                        ((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);
370                        ((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);
371                        ((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);
372                }
373                srow += bpl;
374                orow += rowstride;
375        }
376}
377
378/*
379  convert 16 bits/pixel data
380  with alpha
381  data in lsb format
382*/
383static void
384rgb565alsb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
385{
386        int xx, yy;
387        int width, height;
388        int bpl;
389
390#ifdef LITTLE
391        register guint16 *s;    /* read 1 pixels at once */
392#else
393        register guint8 *s;
394#endif
395        register guint32 *o;
396
397        guint8 *srow = image->mem, *orow = pixels;
398
399        width = image->width;
400        height = image->height;
401        bpl = image->bpl;
402
403        for (yy = 0; yy < height; yy++) {
404#ifdef LITTLE
405                s = (guint16 *) srow;
406#else
407                s = (guint8 *) srow;
408#endif
409                o = (guint32 *) orow;
410                for (xx = 0; xx < width; xx ++) {
411                        register guint32 data;
412                        /*  rrrrrggg gggbbbbb -> rrrrrRRR ggggggGG bbbbbBBB aaaaaaaa */
413                        /*  little endian: aaaaaaaa bbbbbBBB ggggggGG rrrrrRRR */
414#ifdef LITTLE
415                        data = *s++;
416                        *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
417                                | (data & 0x7e0) << 5 | (data & 0x600) >> 1
418                                | (data & 0x1f) << 19 | (data & 0x1c) << 14
419                                | 0xff000000;
420#else
421                        /* swap endianness first */
422                        data = s[0] | s[1] << 8;
423                        s += 2;
424                        *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
425                                | (data & 0x7e0) << 13 | (data & 0x600) << 7
426                                | (data & 0x1f) << 11 | (data & 0x1c) << 6
427                                | 0xff;
428#endif
429                }
430                srow += bpl;
431                orow += rowstride;
432        }
433}
434
435/*
436  convert 16 bits/pixel data
437  with alpha
438  data in msb format
439*/
440static void
441rgb565amsb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
442{
443        int xx, yy;
444        int width, height;
445        int bpl;
446
447#ifdef LITTLE
448        register guint8 *s;
449#else
450        register guint16 *s;    /* read 1 pixels at once */
451#endif
452        register guint32 *o;
453
454        guint8 *srow = image->mem, *orow = pixels;
455
456        width = image->width;
457        height = image->height;
458        bpl = image->bpl;
459
460        for (yy = 0; yy < height; yy++) {
461                s = srow;
462                o = (guint32 *) orow;
463                for (xx = 0; xx < width; xx ++) {
464                        register guint32 data;
465                        /*  rrrrrggg gggbbbbb -> rrrrrRRR gggggg00 bbbbbBBB aaaaaaaa */
466                        /*  little endian: aaaaaaaa bbbbbBBB gggggg00 rrrrrRRR */
467#ifdef LITTLE
468                        /* swap endianness first */
469                        data = s[0] | s[1] << 8;
470                        s += 2;
471                        *o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13
472                                | (data & 0x7e0) << 5 | (data & 0x600) >> 1
473                                | (data & 0x1f) << 19 | (data & 0x1c) << 14
474                                | 0xff000000;
475#else
476                        data = *s++;
477                        *o++ = (data & 0xf800) << 16 | (data & 0xe000) << 11
478                                | (data & 0x7e0) << 13 | (data & 0x600) << 7
479                                | (data & 0x1f) << 11 | (data & 0x1c) << 6
480                                | 0xff;
481#endif
482                }
483                srow += bpl;
484                orow += rowstride;
485        }
486}
487
488/*
489  convert 15 bits/pixel data
490  no alpha
491  data in lsb format
492*/
493static void
494rgb555lsb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
495{
496        int xx, yy;
497        int width, height;
498        int bpl;
499
500#ifdef LITTLE
501        register guint32 *s;    /* read 2 pixels at once */
502#else
503        register guint8 *s;     /* read 2 pixels at once */
504#endif
505        register guint16 *o;
506        guint8 *srow = image->mem, *orow = pixels;
507
508        width = image->width;
509        height = image->height;
510        bpl = image->bpl;
511
512        for (yy = 0; yy < height; yy++) {
513#ifdef LITTLE
514                s = (guint32 *) srow;
515#else
516                s = srow;
517#endif
518                o = (guint16 *) orow;
519                for (xx = 1; xx < width; xx += 2) {
520                        register guint32 data;
521#ifdef LITTLE
522                        data = *s++;
523                        *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
524                                | (data & 0x3e0) << 6 | (data & 0x380) << 1;
525                        *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
526                                | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
527                        *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
528                                | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
529#else
530                        /* swap endianness first */
531                        data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
532                        s += 4;
533                        *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
534                                | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
535                        *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
536                                | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
537                        *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
538                                | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
539#endif
540                }
541                /* check for last remaining pixel */
542                if (width & 1) {
543                        register guint16 data;
544#ifdef LITTLE
545                        data = *((short *) s);
546#else
547                        data = *((short *) s);
548                        data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
549#endif
550                        ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
551                        ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
552                        ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
553                }
554                srow += bpl;
555                orow += rowstride;
556        }
557}
558
559/*
560  convert 15 bits/pixel data
561  no alpha
562  data in msb format
563*/
564static void
565rgb555msb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
566{
567        int xx, yy;
568        int width, height;
569        int bpl;
570
571#ifdef LITTLE
572        register guint8 *s;     /* read 2 pixels at once */
573#else
574        register guint32 *s;    /* read 2 pixels at once */
575#endif
576        register guint16 *o;
577        guint8 *srow = image->mem, *orow = pixels;
578
579        width = image->width;
580        height = image->height;
581        bpl = image->bpl;
582
583        for (yy = 0; yy < height; yy++) {
584                s = srow;
585                o = (guint16 *) orow;
586                for (xx = 1; xx < width; xx += 2) {
587                        register guint32 data;
588#ifdef LITTLE
589                        /* swap endianness first */
590                        data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
591                        s += 4;
592                        *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
593                                | (data & 0x3e0) << 6 | (data & 0x380) << 1;
594                        *o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2
595                                | (data & 0x7c000000) >> 15 | (data & 0x70000000) >> 20;
596                        *o++ = (data & 0x3e00000) >> 18 | (data & 0x3800000) >> 23
597                                | (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;
598#else
599                        data = *s++;
600                        *o++ = (data & 0x7c00) << 1 | (data & 0x7000) >> 4
601                                | (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
602                        *o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6
603                                | (data & 0x7c000000) >> 23 | (data & 0x70000000) >> 28;
604                        *o++ = (data & 0x3e00000) >> 10 | (data & 0x3800000) >> 15
605                                | (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;
606#endif
607                }
608                /* check for last remaining pixel */
609                if (width & 1) {
610                        register guint16 data;
611#ifdef LITTLE
612                        data = *((short *) s);
613                        data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
614#else
615                        data = *((short *) s);
616#endif
617                        ((char *) o)[0] = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12;
618                        ((char *) o)[1] = (data & 0x3e0) >> 2 | (data & 0x380) >> 7;
619                        ((char *) o)[2] = (data & 0x1f) << 3 | (data & 0x1c) >> 2;
620                }
621                srow += bpl;
622                orow += rowstride;
623        }
624}
625
626/*
627  convert 15 bits/pixel data
628  with alpha
629  data in lsb format
630*/
631static void
632rgb555alsb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
633{
634        int xx, yy;
635        int width, height;
636        int bpl;
637
638#ifdef LITTLE
639        register guint16 *s;    /* read 1 pixels at once */
640#else
641        register guint8 *s;
642#endif
643        register guint32 *o;
644
645        guint8 *srow = image->mem, *orow = pixels;
646
647        width = image->width;
648        height = image->height;
649        bpl = image->bpl;
650
651        for (yy = 0; yy < height; yy++) {
652#ifdef LITTLE
653                s = (guint16 *) srow;
654#else
655                s = srow;
656#endif
657                o = (guint32 *) orow;
658                for (xx = 0; xx < width; xx++) {
659                        register guint32 data;
660                        /*  rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
661                        /*  little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
662#ifdef LITTLE
663                        data = *s++;
664                        *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
665                                | (data & 0x3e0) << 6 | (data & 0x380) << 1
666                                | (data & 0x1f) << 19 | (data & 0x1c) << 14
667                                | 0xff000000;
668#else
669                        /* swap endianness first */
670                        data = s[0] | s[1] << 8;
671                        s += 2;
672                        *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
673                                | (data & 0x3e0) << 14 | (data & 0x380) << 9
674                                | (data & 0x1f) << 11 | (data & 0x1c) << 6
675                                | 0xff;
676#endif
677                }
678                srow += bpl;
679                orow += rowstride;
680        }
681}
682
683/*
684  convert 15 bits/pixel data
685  with alpha
686  data in msb format
687*/
688static void
689rgb555amsb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
690{
691        int xx, yy;
692        int width, height;
693        int bpl;
694
695#ifdef LITTLE
696        register guint16 *s;    /* read 1 pixels at once */
697#else
698        register guint8 *s;
699#endif
700        register guint32 *o;
701
702        guint8 *srow = image->mem, *orow = pixels;
703
704        width = image->width;
705        height = image->height;
706        bpl = image->bpl;
707
708        for (yy = 0; yy < height; yy++) {
709#ifdef LITTLE
710                s = (guint16 *) srow;
711#else
712                s = srow;
713#endif
714                o = (guint32 *) orow;
715                for (xx = 0; xx < width; xx++) {
716                        register guint32 data;
717                        /*  rrrrrggg gggbbbbb -> rrrrrRRR gggggGGG bbbbbBBB aaaaaaaa */
718                        /*  little endian: aaaaaaaa bbbbbBBB gggggGGG rrrrrRRR */
719#ifdef LITTLE
720                        /* swap endianness first */
721                        data = s[0] | s[1] << 8;
722                        s += 2;
723                        *o++ = (data & 0x7c00) >> 7 | (data & 0x7000) >> 12
724                                | (data & 0x3e0) << 6 | (data & 0x380) << 1
725                                | (data & 0x1f) << 19 | (data & 0x1c) << 14
726                                | 0xff000000;
727#else
728                        data = *s++;
729                        *o++ = (data & 0x7c00) << 17 | (data & 0x7000) << 12
730                                | (data & 0x3e0) << 14 | (data & 0x380) << 9
731                                | (data & 0x1f) << 11 | (data & 0x1c) << 6
732                                | 0xff;
733#endif
734                }
735                srow += bpl;
736                orow += rowstride;
737        }
738}
739
740
741static void
742rgb888alsb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
743{
744        int xx, yy;
745        int width, height;
746        int bpl;
747
748        guint8 *s;      /* for byte order swapping */
749        guint8 *o;
750        guint8 *srow = image->mem, *orow = pixels;
751
752        width = image->width;
753        height = image->height;
754        bpl = image->bpl;
755
756        d (printf ("32 bits/pixel with alpha\n"));
757
758        /* lsb data */
759        for (yy = 0; yy < height; yy++) {
760                s = srow;
761                o = orow;
762                for (xx = 0; xx < width; xx++) {
763                        *o++ = s[2];
764                        *o++ = s[1];
765                        *o++ = s[0];
766                        *o++ = 0xff;
767                        s += 4;
768                }
769                srow += bpl;
770                orow += rowstride;
771        }
772}
773
774static void
775rgb888lsb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
776{
777        int xx, yy;
778        int width, height;
779        int bpl;
780
781        guint8 *srow = image->mem, *orow = pixels;
782        guint8 *o, *s;
783
784        width = image->width;
785        height = image->height;
786        bpl = image->bpl;
787
788        d (printf ("32 bit, lsb, no alpha\n"));
789
790        for (yy = 0; yy < height; yy++) {
791                s = srow;
792                o = orow;
793                for (xx = 0; xx < width; xx++) {
794                        *o++ = s[2];
795                        *o++ = s[1];
796                        *o++ = s[0];
797                        s += 4;
798                }
799                srow += bpl;
800                orow += rowstride;
801        }
802}
803
804static void
805rgb888amsb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
806{
807        int xx, yy;
808        int width, height;
809        int bpl;
810
811        guint8 *srow = image->mem, *orow = pixels;
812#ifdef LITTLE
813        guint32 *o;
814        guint32 *s;
815#else
816        guint8 *s;      /* for byte order swapping */
817        guint8 *o;
818#endif
819
820        d (printf ("32 bit, msb, with alpha\n"));
821
822        width = image->width;
823        height = image->height;
824        bpl = image->bpl;
825
826        /* msb data */
827        for (yy = 0; yy < height; yy++) {
828#ifdef LITTLE
829                s = (guint32 *) srow;
830                o = (guint32 *) orow;
831#else
832                s = srow;
833                o = orow;
834#endif
835                for (xx = 0; xx < width; xx++) {
836#ifdef LITTLE
837                        *o++ = s[1];
838                        *o++ = s[2];
839                        *o++ = s[3];
840                        *o++ = 0xff;
841                        s += 4;
842#else
843                        *o++ = (*s << 8) | 0xff; /* untested */
844                        s++;
845#endif
846                }
847                srow += bpl;
848                orow += rowstride;
849        }
850}
851
852static void
853rgb888msb (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *colormap)
854{
855        int xx, yy;
856        int width, height;
857        int bpl;
858
859        guint8 *srow = image->mem, *orow = pixels;
860        guint8 *s;
861        guint8 *o;
862
863        d (printf ("32 bit, msb, no alpha\n"));
864
865        width = image->width;
866        height = image->height;
867        bpl = image->bpl;
868
869        for (yy = 0; yy < height; yy++) {
870                s = srow;
871                o = orow;
872                for (xx = 0; xx < width; xx++) {
873                        *o++ = s[1];
874                        *o++ = s[2];
875                        *o++ = s[3];
876                        s += 4;
877                }
878                srow += bpl;
879                orow += rowstride;
880        }
881}
882
883/*
884  This should work correctly with any display/any endianness, but will probably
885  run quite slow
886*/
887static void
888convert_real_slow (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *cmap, int alpha)
889{
890        int xx, yy;
891        int width, height;
892        int bpl;
893        guint8 *srow = image->mem, *orow = pixels;
894        guint8 *s;
895        guint8 *o;
896        guint32 pixel;
897        GdkVisual *v;
898        guint8 component;
899        int i;
900
901        width = image->width;
902        height = image->height;
903        bpl = image->bpl;
904        v = gdk_colormap_get_visual(cmap);
905
906        d(printf("rgb  mask/shift/prec = %x:%x:%x %d:%d:%d  %d:%d:%d\n",
907                 v->red_mask, v->green_mask, v->blue_mask,
908                 v->red_shift, v->green_shift, v->blue_shift,
909                 v->red_prec, v->green_prec, v->blue_prec));
910
911        for (yy = 0; yy < height; yy++) {
912                s = srow;
913                o = orow;
914                for (xx = 0; xx < width; xx++) {
915                        pixel = gdk_image_get_pixel(image, xx, yy);
916                        switch (v->type) {
917                                /* I assume this is right for static & greyscale's too? */
918                        case GDK_VISUAL_STATIC_GRAY:
919                        case GDK_VISUAL_GRAYSCALE:
920                        case GDK_VISUAL_STATIC_COLOR:
921                        case GDK_VISUAL_PSEUDO_COLOR:
922                                *o++ = cmap->colors[pixel].red;
923                                *o++ = cmap->colors[pixel].green;
924                                *o++ = cmap->colors[pixel].blue;
925                                break;
926                        case GDK_VISUAL_TRUE_COLOR:
927                                /* This is odd because it must sometimes shift left (otherwise
928                                   I'd just shift >> (*_shift - 8 + *_prec + <0-7>). This logic
929                                   should work for all bit sizes/shifts/etc. */
930                                component = 0;
931                                for (i = 24; i < 32; i += v->red_prec)
932                                        component |= ((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> i;
933                                *o++ = component;
934                                component = 0;
935                                for (i = 24; i < 32; i += v->green_prec)
936                                        component |= ((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> i;
937                                *o++ = component;
938                                component = 0;
939                                for (i = 24; i < 32; i += v->blue_prec)
940                                        component |= ((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> i;
941                                *o++ = component;
942                                break;
943                        case GDK_VISUAL_DIRECT_COLOR:
944                                *o++ = cmap->colors[((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> 24].red;
945                                *o++ = cmap->colors[((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> 24].green;
946                                *o++ = cmap->colors[((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> 24].blue;
947                                break;
948                        }
949                        if (alpha)
950                                *o++ = 0xff;
951                }
952                srow += bpl;
953                orow += rowstride;
954        }
955}
956
957typedef void (* cfunc) (GdkImage *image, guchar *pixels, int rowstride, GdkColormap *cmap);
958
959static cfunc convert_map[] = {
960        rgb1,rgb1,rgb1a,rgb1a,
961        rgb8,rgb8,rgb8a,rgb8a,
962        rgb555lsb,rgb555msb,rgb555alsb,rgb555amsb,
963        rgb565lsb,rgb565msb,rgb565alsb,rgb565amsb,
964        rgb888lsb,rgb888msb,rgb888alsb,rgb888amsb
965};
966
967/*
968  perform actual conversion
969
970  If we can, try and use the optimised code versions, but as a default
971  fallback, and always for direct colour, use the generic/slow but complete
972  conversion function.
973*/
974static void
975rgbconvert (GdkImage *image, guchar *pixels, int rowstride, int alpha, GdkColormap *cmap)
976{
977        int index = (image->byte_order == GDK_MSB_FIRST) | (alpha != 0) << 1;
978        int bank=5;             /* default fallback converter */
979        GdkVisual *v = gdk_colormap_get_visual(cmap);
980
981        d(printf("masks = %x:%x:%x\n", v->red_mask, v->green_mask, v->blue_mask));
982        d(printf("image depth = %d, bpp = %d\n", image->depth, image->bpp));
983
984        switch (v->type) {
985                                /* I assume this is right for static & greyscale's too? */
986        case GDK_VISUAL_STATIC_GRAY:
987        case GDK_VISUAL_GRAYSCALE:
988        case GDK_VISUAL_STATIC_COLOR:
989        case GDK_VISUAL_PSEUDO_COLOR:
990                switch (image->bpp) {
991                case 1:
992                        bank = 0;
993                        break;
994                case 8:
995                        bank = 1;
996                        break;
997                }
998                break;
999        case GDK_VISUAL_TRUE_COLOR:
1000                switch (image->depth) {
1001                case 15:
1002                        if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1003                            && image->bpp == 16)
1004                                bank = 2;
1005                        break;
1006                case 16:
1007                        if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1008                            && image->bpp == 16)
1009                                bank = 3;
1010                        break;
1011                case 24:
1012                case 32:
1013                        if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1014                            && image->bpp == 32)
1015                                bank = 4;
1016                        break;
1017                }
1018                break;
1019        case GDK_VISUAL_DIRECT_COLOR:
1020                /* always use the slow version */
1021                break;
1022        }
1023
1024        d(printf("converting using conversion function in bank %d\n", bank));
1025
1026        if (bank==5) {
1027                convert_real_slow(image, pixels, rowstride, cmap, alpha);
1028        } else {
1029                index |= bank << 2;
1030                (* convert_map[index]) (image, pixels, rowstride, cmap);
1031        }
1032}
1033
1034
1035/* Exported functions */
1036
1037/**
1038 * gdk_pixbuf_get_from_drawable:
1039 * @dest: Destination pixbuf, or NULL if a new pixbuf should be created.
1040 * @src: Source drawable.
1041 * @cmap: A colormap if @src is a pixmap.  If it is a window, this argument will
1042 * be ignored.
1043 * @src_x: Source X coordinate within drawable.
1044 * @src_y: Source Y coordinate within drawable.
1045 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1046 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1047 * @width: Width in pixels of region to get.
1048 * @height: Height in pixels of region to get.
1049 *
1050 * Transfers image data from a Gdk drawable and converts it to an RGB(A)
1051 * representation inside a GdkPixbuf.
1052 *
1053 * If the drawable @src is a pixmap, then a suitable colormap must be specified,
1054 * since pixmaps are just blocks of pixel data without an associated colormap.
1055 * If the drawable is a window, the @cmap argument will be ignored and the
1056 * window's own colormap will be used instead.
1057 *
1058 * If the specified destination pixbuf @dest is #NULL, then this function will
1059 * create an RGB pixbuf with 8 bits per channel and no alpha, with the same size
1060 * specified by the @width and @height arguments.  In this case, the @dest_x and
1061 * @dest_y arguments must be specified as 0, otherwise the function will return
1062 * #NULL.  If the specified destination pixbuf is not NULL and it contains alpha
1063 * information, then the filled pixels will be set to full opacity.
1064 *
1065 * If the specified drawable is a pixmap, then the requested source rectangle
1066 * must be completely contained within the pixmap, otherwise the function will
1067 * return #NULL.
1068 *
1069 * If the specified drawable is a window, then it must be viewable, i.e. all of
1070 * its ancestors up to the root window must be mapped.  Also, the specified
1071 * source rectangle must be completely contained within the window and within
1072 * the screen.  If regions of the window are obscured by noninferior windows, the
1073 * contents of those regions are undefined.  The contents of regions obscured by
1074 * inferior windows of a different depth than that of the source window will also
1075 * be undefined.
1076 *
1077 * Return value: The same pixbuf as @dest if it was non-NULL, or a newly-created
1078 * pixbuf with a reference count of 1 if no destination pixbuf was specified; in
1079 * the latter case, NULL will be returned if not enough memory could be
1080 * allocated for the pixbuf to be created.
1081 **/
1082GdkPixbuf * NAUTILUS_BUG_5712_PR3_WORKAROUND__gdk_pixbuf_get_from_drawable (GdkPixbuf *dest,
1083                                                                            GdkDrawable *src, GdkColormap *cmap,
1084                                                                            int src_x, int src_y,
1085                                                                            int dest_x, int dest_y,
1086                                                                            int width, int height);
1087
1088
1089GdkImage* NAUTILUS_BUG_5712_PR3_WORKAROUND__gdk_image_get (GdkWindow *window,
1090                                                           gint       x,
1091                                                           gint       y,
1092                                                           gint       width,
1093                                                           gint       height);
1094
1095GdkPixbuf *
1096NAUTILUS_BUG_5712_PR3_WORKAROUND__gdk_pixbuf_get_from_drawable (GdkPixbuf *dest,
1097                              GdkDrawable *src, GdkColormap *cmap,
1098                              int src_x, int src_y,
1099                              int dest_x, int dest_y,
1100                              int width, int height)
1101{
1102        GdkWindowType window_type;
1103        int src_width, src_height;
1104        GdkImage *image;
1105        int rowstride, bpp, alpha;
1106
1107        /* General sanity checks */
1108
1109        g_return_val_if_fail (src != NULL, NULL);
1110
1111        window_type = gdk_window_get_type (src);
1112
1113        if (window_type == GDK_WINDOW_PIXMAP)
1114                g_return_val_if_fail (cmap != NULL, NULL);
1115        else
1116                /* FIXME: this is not perfect, since is_viewable() only tests
1117                 * recursively up the Gdk parent window tree, but stops at
1118                 * foreign windows or Gdk toplevels.  I.e. if a window manager
1119                 * unmapped one of its own windows, this won't work.
1120                 */
1121                g_return_val_if_fail (gdk_window_is_viewable (src), NULL);
1122
1123        if (!dest)
1124                g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1125        else {
1126                g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1127                g_return_val_if_fail (dest->n_channels == 3 || dest->n_channels == 4, NULL);
1128                g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1129        }
1130
1131        /* Coordinate sanity checks */
1132
1133        gdk_window_get_size (src, &src_width, &src_height);
1134
1135        g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1136        g_return_val_if_fail (src_x + width <= src_width && src_y + height <= src_height, NULL);
1137
1138        if (dest) {
1139                g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1140                g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1141                g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1142        }
1143
1144        if (window_type != GDK_WINDOW_PIXMAP) {
1145                int ret;
1146                int src_xorigin, src_yorigin;
1147                int screen_width, screen_height;
1148                int screen_srcx, screen_srcy;
1149
1150                ret = gdk_window_get_origin (src, &src_xorigin, &src_yorigin);
1151                g_return_val_if_fail (ret != FALSE, NULL);
1152
1153                screen_width = gdk_screen_width ();
1154                screen_height = gdk_screen_height ();
1155
1156                screen_srcx = src_xorigin + src_x;
1157                screen_srcy = src_yorigin + src_y;
1158
1159                g_return_val_if_fail (screen_srcx >= 0 && screen_srcy >= 0, NULL);
1160                g_return_val_if_fail (screen_srcx + width <= screen_width, NULL);
1161                g_return_val_if_fail (screen_srcy + height <= screen_height, NULL);
1162        }
1163
1164        /* Get Image in ZPixmap format (packed bits). */
1165        image = NAUTILUS_BUG_5712_PR3_WORKAROUND__gdk_image_get (src, src_x, src_y, width, height);
1166        if (!image)
1167                return NULL;
1168
1169        /* Create the pixbuf if needed */
1170        if (!dest) {
1171                dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1172                if (!dest) {
1173                        gdk_image_destroy(image);
1174                        return NULL;
1175                }
1176        }
1177
1178        /* Get the colormap if needed */
1179        if (window_type != GDK_WINDOW_PIXMAP)
1180                cmap = gdk_window_get_colormap (src);
1181
1182        alpha = dest->has_alpha;
1183        rowstride = dest->rowstride;
1184        bpp = alpha ? 4 : 3;
1185
1186        /* we offset into the image data based on the position we are retrieving from */
1187        rgbconvert (image, dest->pixels +
1188                    (dest_y * rowstride) + (dest_x * bpp),
1189                    rowstride,
1190                    alpha,
1191                    cmap);
1192
1193        gdk_image_destroy(image);
1194
1195        return dest;
1196}
Note: See TracBrowser for help on using the browser.