Back to index

gcompris  8.2.2
pixbuf_util.c
Go to the documentation of this file.
00001 /*
00002  * GQview
00003  * (C) 2001 John Ellis
00004  *
00005  * Author: John Ellis
00006  *
00007  * This software is released under the GNU General Public License (GNU GPL).
00008  * Please read the included file COPYING for more information.
00009  * This software comes with no warranty of any kind, use at your own risk!
00010  */
00011 
00012 
00013 #include "pixbuf_util.h"
00014 
00015 
00016 /*
00017  * Returns a copy of pixbuf mirrored and or flipped.
00018  * TO do a 180 degree rotations set both mirror and flipped TRUE
00019  * if mirror and flip are FALSE, result is a simple copy.
00020  */
00021 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gint mirror, gint flip)
00022 {
00023        GdkPixbuf *dest;
00024        gint has_alpha;
00025        gint w, h, srs;
00026        gint drs;
00027        guchar *s_pix;
00028         guchar *d_pix;
00029        guchar *sp;
00030         guchar *dp;
00031        gint i, j;
00032        gint a;
00033 
00034        if (!src) return NULL;
00035 
00036        w = gdk_pixbuf_get_width(src);
00037        h = gdk_pixbuf_get_height(src);
00038        has_alpha = gdk_pixbuf_get_has_alpha(src);
00039        srs = gdk_pixbuf_get_rowstride(src);
00040        s_pix = gdk_pixbuf_get_pixels(src);
00041 
00042        dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
00043        drs = gdk_pixbuf_get_rowstride(dest);
00044        d_pix = gdk_pixbuf_get_pixels(dest);
00045 
00046        a = has_alpha ? 4 : 3;
00047 
00048        for (i = 0; i < h; i++)
00049               {
00050               sp = s_pix + (i * srs);
00051               if (flip)
00052                      {
00053                      dp = d_pix + ((h - i - 1) * drs);
00054                      }
00055               else
00056                      {
00057                      dp = d_pix + (i * drs);
00058                      }
00059               if (mirror)
00060                      {
00061                      dp += (w - 1) * a;
00062                      for (j = 0; j < w; j++)
00063                             {
00064                             *(dp++) = *(sp++);   /* r */
00065                             *(dp++) = *(sp++);   /* g */
00066                             *(dp++) = *(sp++);   /* b */
00067                             if (has_alpha) *(dp) = *(sp++);    /* a */
00068                             dp -= (a + 3);
00069                             }
00070                      }
00071               else
00072                      {
00073                      for (j = 0; j < w; j++)
00074                             {
00075                             *(dp++) = *(sp++);   /* r */
00076                             *(dp++) = *(sp++);   /* g */
00077                             *(dp++) = *(sp++);   /* b */
00078                             if (has_alpha) *(dp++) = *(sp++);  /* a */
00079                             }
00080                      }
00081               }
00082 
00083        return dest;
00084 }
00085