Back to index

scribus-ng  1.3.4.dfsg+svn20071115
gdk-pixbuf-xlib-render.c
Go to the documentation of this file.
00001 /* GdkPixbuf library - Rendering functions
00002  *
00003  * Copyright (C) 1999 The Free Software Foundation
00004  *
00005  * Author: Federico Mena-Quintero <federico@gimp.org>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Library General Public
00018  * License along with this library; if not, write to the
00019  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
00021  */
00022 
00023 /* Trivially ported to Xlib(RGB) by John Harper. */
00024 
00025 #include "scconfig.h"
00026 /* #include "gdk-pixbuf-private.h" */
00027 #include "gdk-pixbuf-xlib-private.h"
00028 
00029 
00030 /*
00031 **
00032  * gdk_pixbuf_xlib_render_threshold_alpha:
00033  * @pixbuf: A pixbuf.
00034  * @bitmap: Bitmap where the bilevel mask will be painted to.
00035  * @src_x: Source X coordinate.
00036  * @src_y: source Y coordinate.
00037  * @dest_x: Destination X coordinate.
00038  * @dest_y: Destination Y coordinate.
00039  * @width: Width of region to threshold.
00040  * @height: Height of region to threshold.
00041  * @alpha_threshold: Opacity values below this will be painted as zero; all
00042  * other values will be painted as one.
00043  *
00044  * Takes the opacity values in a rectangular portion of a pixbuf and thresholds
00045  * them to produce a bi-level alpha mask that can be used as a clipping mask for
00046  * a drawable.
00047  *
00048 void
00049 gdk_pixbuf_xlib_render_threshold_alpha (GdkPixbuf *pixbuf, Pixmap bitmap,
00050                                    int src_x, int src_y,
00051                                    int dest_x, int dest_y,
00052                                    int width, int height,
00053                                    int alpha_threshold)
00054 {
00055        GC gc;
00056        XColor color;
00057        int x, y;
00058        guchar *p;
00059        int start, start_status;
00060        int status;
00061        XGCValues gcv;
00062 
00063        g_return_if_fail (pixbuf != NULL);
00064        g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
00065        g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
00066        g_return_if_fail (pixbuf->bits_per_sample == 8);
00067 
00068        g_return_if_fail (bitmap != 0);
00069        g_return_if_fail (width >= 0 && height >= 0);
00070        g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
00071        g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
00072 
00073        g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
00074 
00075        if (width == 0 || height == 0)
00076               return;
00077 
00078        gc = XCreateGC (gdk_pixbuf_dpy, bitmap, 0, &gcv);
00079 
00080        if (!pixbuf->has_alpha) {
00081               color.pixel = (alpha_threshold == 255) ? 0 : 1;
00082               XSetForeground (gdk_pixbuf_dpy, gc, color.pixel);
00083               XFillRectangle (gdk_pixbuf_dpy, bitmap, gc,
00084                             dest_x, dest_y, width, height);
00085               XFreeGC (gdk_pixbuf_dpy, gc);
00086               return;
00087        }
00088 
00089        color.pixel = 0;
00090        XSetForeground (gdk_pixbuf_dpy, gc, color.pixel);
00091        XFillRectangle (gdk_pixbuf_dpy, bitmap, gc,
00092                      dest_x, dest_y, width, height);
00093 
00094        color.pixel = 1;
00095        XSetForeground (gdk_pixbuf_dpy, gc, color.pixel);
00096 
00097        for (y = 0; y < height; y++) {
00098               p = (pixbuf->pixels + (y + src_y) * pixbuf->rowstride + src_x * pixbuf->n_channels
00099                    + pixbuf->n_channels - 1);
00100 
00101               start = 0;
00102               start_status = *p < alpha_threshold;
00103 
00104               for (x = 0; x < width; x++) {
00105                      status = *p < alpha_threshold;
00106 
00107                      if (status != start_status) {
00108                             if (!start_status)
00109                                    XDrawLine (gdk_pixbuf_dpy, bitmap, gc,
00110                                              start + dest_x, y + dest_y,
00111                                              x - 1 + dest_x, y + dest_y);
00112 
00113                             start = x;
00114                             start_status = status;
00115                      }
00116 
00117                      p += pixbuf->n_channels;
00118               }
00119 
00120               if (!start_status)
00121                      XDrawLine (gdk_pixbuf_dpy, bitmap, gc,
00122                                start + dest_x, y + dest_y,
00123                                x - 1 + dest_x, y + dest_y);
00124        }
00125 
00126        XFreeGC (gdk_pixbuf_dpy, gc);
00127 }
00128 
00129 
00130 static guchar *
00131 remove_alpha (GdkPixbuf *pixbuf, int x, int y, int width, int height, int *rowstride)
00132 {
00133        guchar *buf;
00134        int xx, yy;
00135        guchar *src, *dest;
00136 
00137        g_assert (pixbuf->n_channels == 4);
00138        g_assert (pixbuf->has_alpha);
00139        g_assert (width > 0 && height > 0);
00140        g_assert (x >= 0 && x + width <= pixbuf->width);
00141        g_assert (y >= 0 && y + height <= pixbuf->height);
00142 
00143        *rowstride = 4 * ((width * 3 + 3) / 4);
00144 
00145        buf = g_new (guchar, *rowstride * height);
00146 
00147        for (yy = 0; yy < height; yy++) {
00148               src = pixbuf->pixels + pixbuf->rowstride * (yy + y) + x * pixbuf->n_channels;
00149               dest = buf + *rowstride * yy;
00150 
00151               for (xx = 0; xx < width; xx++) {
00152                      *dest++ = *src++;
00153                      *dest++ = *src++;
00154                      *dest++ = *src++;
00155                      src++;
00156               }
00157        }
00158 
00159        return buf;
00160 }
00161 
00162  * gdk_pixbuf_xlib_render_to_drawable:
00163  * @pixbuf: A pixbuf.
00164  * @drawable: Destination drawable.
00165  * @gc: GC used for rendering.
00166  * @src_x: Source X coordinate within pixbuf.
00167  * @src_y: Source Y coordinate within pixbuf.
00168  * @dest_x: Destination X coordinate within drawable.
00169  * @dest_y: Destination Y coordinate within drawable.
00170  * @width: Width of region to render, in pixels.
00171  * @height: Height of region to render, in pixels.
00172  * @dither: Dithering mode for XlibRGB.
00173  * @x_dither: X offset for dither.
00174  * @y_dither: Y offset for dither.
00175  *
00176  * Renders a rectangular portion of a pixbuf to a drawable while using the
00177  * specified GC.  This is done using XlibRGB, so the specified drawable must
00178  * have the XlibRGB visual and colormap.  Note that this function will ignore
00179  * the opacity information for images with an alpha channel; the GC must already
00180  * have the clipping mask set if you want transparent regions to show through.
00181  *
00182  * For an explanation of dither offsets, see the XlibRGB documentation.  In
00183  * brief, the dither offset is important when re-rendering partial regions of an
00184  * image to a rendered version of the full image, or for when the offsets to a
00185  * base position change, as in scrolling.  The dither matrix has to be shifted
00186  * for consistent visual results.  If you do not have any of these cases, the
00187  * dither offsets can be both zero.
00188 void
00189 gdk_pixbuf_xlib_render_to_drawable (GdkPixbuf *pixbuf,
00190                                 Drawable drawable, GC gc,
00191                                 int src_x, int src_y,
00192                                 int dest_x, int dest_y,
00193                                 int width, int height,
00194                                 XlibRgbDither dither,
00195                                 int x_dither, int y_dither)
00196 {
00197        guchar *buf;
00198        int rowstride;
00199 
00200        g_return_if_fail (pixbuf != NULL);
00201        g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
00202        g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
00203        g_return_if_fail (pixbuf->bits_per_sample == 8);
00204 
00205        g_return_if_fail (drawable != 0);
00206        g_return_if_fail (gc != 0);
00207 
00208        g_return_if_fail (width >= 0 && height >= 0);
00209        g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
00210        g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
00211 
00212        if (width == 0 || height == 0)
00213               return;
00214 
00215        if (pixbuf->has_alpha)
00216               buf = remove_alpha (pixbuf, src_x, src_y, width, height, &rowstride);
00217        else {
00218               buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
00219               rowstride = pixbuf->rowstride;
00220        }
00221 
00222        xlib_draw_rgb_image_dithalign (drawable, gc,
00223                                    dest_x, dest_y,
00224                                    width, height,
00225                                    dither,
00226                                    buf, rowstride,
00227                                    x_dither, y_dither);
00228 
00229        if (pixbuf->has_alpha)
00230               g_free (buf);
00231 }
00232 
00233 
00234 
00235  * gdk_pixbuf_xlib_render_to_drawable_alpha:
00236  * @pixbuf: A pixbuf.
00237  * @drawable: Destination drawable.
00238  * @src_x: Source X coordinate within pixbuf.
00239  * @src_y: Source Y coordinates within pixbuf.
00240  * @dest_x: Destination X coordinate within drawable.
00241  * @dest_y: Destination Y coordinate within drawable.
00242  * @width: Width of region to render, in pixels.
00243  * @height: Height of region to render, in pixels.
00244  * @alpha_mode: If the image does not have opacity information, this is ignored.
00245  * Otherwise, specifies how to handle transparency when rendering.
00246  * @alpha_threshold: If the image does have opacity information and @alpha_mode
00247  * is GDK_PIXBUF_ALPHA_BILEVEL, specifies the threshold value for opacity
00248  * values.
00249  * @dither: Dithering mode for XlibRGB.
00250  * @x_dither: X offset for dither.
00251  * @y_dither: Y offset for dither.
00252  *
00253  * Renders a rectangular portion of a pixbuf to a drawable.  This is done using
00254  * XlibRGB, so the specified drawable must have the XlibRGB visual and colormap.
00255  *
00256  * When used with #GDK_PIXBUF_ALPHA_BILEVEL, this function has to create a bitmap
00257  * out of the thresholded alpha channel of the image and, it has to set this
00258  * bitmap as the clipping mask for the GC used for drawing.  This can be a
00259  * significant performance penalty depending on the size and the complexity of
00260  * the alpha channel of the image.  If performance is crucial, consider handling
00261  * the alpha channel yourself (possibly by caching it in your application) and
00262  * using gdk_pixbuf_xlib_render_to_drawable() or GdkRGB directly instead.
00263 void
00264 gdk_pixbuf_xlib_render_to_drawable_alpha (GdkPixbuf *pixbuf, Drawable drawable,
00265                                      int src_x, int src_y,
00266                                      int dest_x, int dest_y,
00267                                      int width, int height,
00268                                      GdkPixbufAlphaMode alpha_mode,
00269                                      int alpha_threshold,
00270                                      XlibRgbDither dither,
00271                                      int x_dither, int y_dither)
00272 {
00273        Pixmap bitmap = 0;
00274        GC gc;
00275        XGCValues gcv;
00276 
00277        g_return_if_fail (pixbuf != NULL);
00278        g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
00279        g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
00280        g_return_if_fail (pixbuf->bits_per_sample == 8);
00281 
00282        g_return_if_fail (drawable != 0);
00283        g_return_if_fail (width >= 0 && height >= 0);
00284        g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
00285        g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
00286 
00287        if (width == 0 || height == 0)
00288               return;
00289 
00290        gc = XCreateGC (gdk_pixbuf_dpy, drawable, 0, &gcv);
00291 
00292        if (pixbuf->has_alpha) {
00293 
00294               bitmap = XCreatePixmap (gdk_pixbuf_dpy,
00295                                    RootWindow (gdk_pixbuf_dpy,
00296                                               gdk_pixbuf_screen),
00297                                    width, height, 1);
00298               gdk_pixbuf_xlib_render_threshold_alpha (pixbuf, bitmap,
00299                                                  src_x, src_y,
00300                                                  0, 0,
00301                                                  width, height,
00302                                                  alpha_threshold);
00303 
00304               XSetClipMask (gdk_pixbuf_dpy, gc, bitmap);
00305               XSetClipOrigin (gdk_pixbuf_dpy, gc, dest_x, dest_y);
00306        }
00307 
00308        gdk_pixbuf_xlib_render_to_drawable (pixbuf, drawable, gc,
00309                                        src_x, src_y,
00310                                        dest_x, dest_y,
00311                                        width, height,
00312                                        dither,
00313                                        x_dither, y_dither);
00314 
00315        if (bitmap)
00316                XFreePixmap (gdk_pixbuf_dpy, bitmap);
00317 
00318        XFreeGC (gdk_pixbuf_dpy, gc);
00319 }
00320 
00321  * @pixbuf: A pixbuf.
00322  * @pixmap_return: Return value for the created pixmap.
00323  * @mask_return: Return value for the created mask.
00324  * @alpha_threshold: Threshold value for opacity values.
00325  *
00326  * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
00327  * and @mask_return arguments, respectively, and renders a pixbuf and its
00328  * corresponding tresholded alpha mask to them.  This is merely a convenience
00329  * function; applications that need to render pixbufs with dither offsets or to
00330  * given drawables should use gdk_pixbuf_xlib_render_to_drawable_alpha() or
00331  * gdk_pixbuf_xlib_render_to_drawable(), and
00332  * gdk_pixbuf_xlib_render_threshold_alpha().
00333  *
00334  * If the pixbuf does not have an alpha channel, then *@mask_return will be set
00335  * to None.
00336 void
00337 gdk_pixbuf_xlib_render_pixmap_and_mask (GdkPixbuf *pixbuf,
00338                                    Pixmap *pixmap_return,
00339                                    Pixmap *mask_return,
00340                                    int alpha_threshold)
00341 {
00342         g_return_if_fail (pixbuf != NULL);
00343 
00344        if (pixmap_return) {
00345               GC gc;
00346               XGCValues gcv;
00347 
00348               *pixmap_return = XCreatePixmap (gdk_pixbuf_dpy,
00349                                           RootWindow (gdk_pixbuf_dpy,
00350                                                      gdk_pixbuf_screen),
00351                                           pixbuf->width,
00352                                           pixbuf->height,
00353                                           xlib_rgb_get_depth ());
00354               gc = XCreateGC (gdk_pixbuf_dpy, *pixmap_return, 0, &gcv);
00355               gdk_pixbuf_xlib_render_to_drawable (pixbuf, *pixmap_return, gc,
00356                                               0, 0, 0, 0,
00357                                               pixbuf->width,
00358                                               pixbuf->height,
00359                                               XLIB_RGB_DITHER_NORMAL,
00360                                               0, 0);
00361               XFreeGC (gdk_pixbuf_dpy, gc);
00362        }
00363 
00364        if (mask_return) {
00365               if (pixbuf->has_alpha) {
00366                   *mask_return = XCreatePixmap (gdk_pixbuf_dpy,
00367                                             RootWindow (gdk_pixbuf_dpy,
00368                                                        gdk_pixbuf_screen),
00369                                             pixbuf->width,
00370                                             pixbuf->height, 1);
00371                   gdk_pixbuf_xlib_render_threshold_alpha (pixbuf,
00372                                                      *mask_return,
00373                                                      0, 0, 0, 0,
00374                                                      pixbuf->width,
00375                                                      pixbuf->height,
00376                                                      alpha_threshold);
00377               } else
00378                      *mask_return = 0;
00379        }
00380 }*/