Blame gdk-pixbuf/gdk-pixbuf-util.c

Packit a4058c
/* GdkPixbuf library - Utilities and miscellaneous convenience functions
Packit a4058c
 *
Packit a4058c
 * Copyright (C) 1999 The Free Software Foundation
Packit a4058c
 *
Packit a4058c
 * Authors: Federico Mena-Quintero <federico@gimp.org>
Packit a4058c
 *          Cody Russell  <bratsche@gnome.org>
Packit a4058c
 *
Packit a4058c
 * This library is free software; you can redistribute it and/or
Packit a4058c
 * modify it under the terms of the GNU Lesser General Public
Packit a4058c
 * License as published by the Free Software Foundation; either
Packit a4058c
 * version 2 of the License, or (at your option) any later version.
Packit a4058c
 *
Packit a4058c
 * This library is distributed in the hope that it will be useful,
Packit a4058c
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4058c
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4058c
 * Lesser General Public License for more details.
Packit a4058c
 *
Packit a4058c
 * You should have received a copy of the GNU Lesser General Public
Packit a4058c
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit a4058c
 */
Packit a4058c
Packit a4058c
#include "config.h"
Packit a4058c
#include <string.h>
Packit a4058c
#include <libintl.h>
Packit a4058c
Packit a4058c
#include "gdk-pixbuf-transform.h"
Packit a4058c
#include "gdk-pixbuf-private.h"
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * SECTION:util
Packit a4058c
 * @Short_description: Utility and miscellaneous convenience functions.
Packit a4058c
 * @Title: Utilities
Packit a4058c
 * @See_also: #GdkPixbuf
Packit a4058c
 * 
Packit a4058c
 * These functions provide miscellaneous utilities for manipulating
Packit a4058c
 * pixbufs.  The pixel data in pixbufs may of course be manipulated
Packit a4058c
 * directly by applications, but several common operations can be
Packit a4058c
 * performed by these functions instead.
Packit a4058c
 */
Packit a4058c

Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_add_alpha:
Packit a4058c
 * @pixbuf: A #GdkPixbuf.
Packit a4058c
 * @substitute_color: Whether to set a color to zero opacity.  If this
Packit a4058c
 * is %FALSE, then the (@r, @g, @b) arguments will be ignored.
Packit a4058c
 * @r: Red value to substitute.
Packit a4058c
 * @g: Green value to substitute.
Packit a4058c
 * @b: Blue value to substitute.
Packit a4058c
 *
Packit a4058c
 * Takes an existing pixbuf and adds an alpha channel to it.
Packit a4058c
 * If the existing pixbuf already had an alpha channel, the channel
Packit a4058c
 * values are copied from the original; otherwise, the alpha channel
Packit a4058c
 * is initialized to 255 (full opacity).
Packit a4058c
 * 
Packit a4058c
 * If @substitute_color is %TRUE, then the color specified by (@r, @g, @b) will be
Packit a4058c
 * assigned zero opacity. That is, if you pass (255, 255, 255) for the
Packit a4058c
 * substitute color, all white pixels will become fully transparent.
Packit a4058c
 *
Packit a4058c
 * Return value: (transfer full): A newly-created pixbuf with a reference count of 1.
Packit a4058c
 **/
Packit a4058c
GdkPixbuf *
Packit a4058c
gdk_pixbuf_add_alpha (const GdkPixbuf *pixbuf,
Packit a4058c
		      gboolean substitute_color, guchar r, guchar g, guchar b)
Packit a4058c
{
Packit a4058c
	GdkPixbuf *new_pixbuf;
Packit a4058c
	int x, y;
Packit a4058c
	const guint8 *src_pixels;
Packit a4058c
	guint8 *ret_pixels;
Packit a4058c
	const guchar *src;
Packit a4058c
	guchar *dest;
Packit a4058c
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
Packit a4058c
	g_return_val_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB, NULL);
Packit a4058c
	g_return_val_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4, NULL);
Packit a4058c
	g_return_val_if_fail (pixbuf->bits_per_sample == 8, NULL);
Packit a4058c
Packit a4058c
	src_pixels = gdk_pixbuf_read_pixels (pixbuf);
Packit a4058c
Packit a4058c
	if (pixbuf->has_alpha) {
Packit a4058c
		new_pixbuf = gdk_pixbuf_copy (pixbuf);
Packit a4058c
		if (!new_pixbuf)
Packit a4058c
			return NULL;
Packit a4058c
Packit a4058c
                if (!substitute_color)
Packit a4058c
                        return new_pixbuf;
Packit a4058c
	} else {
Packit a4058c
                new_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, pixbuf->width, pixbuf->height);
Packit a4058c
        }
Packit a4058c
Packit a4058c
	if (!new_pixbuf)
Packit a4058c
		return NULL;
Packit a4058c
Packit a4058c
	ret_pixels = gdk_pixbuf_get_pixels (new_pixbuf);
Packit a4058c
Packit a4058c
	for (y = 0; y < pixbuf->height; y++, src_pixels += pixbuf->rowstride, ret_pixels += new_pixbuf->rowstride) {
Packit a4058c
		guchar tr, tg, tb;
Packit a4058c
Packit a4058c
                src = src_pixels;
Packit a4058c
                dest = ret_pixels;
Packit a4058c
Packit a4058c
                if (pixbuf->has_alpha) {
Packit a4058c
                        /* Just subst color, we already copied everything else */
Packit a4058c
                        for (x = 0; x < pixbuf->width; x++) {
Packit a4058c
                                if (src[0] == r && src[1] == g && src[2] == b)
Packit a4058c
                                        dest[3] = 0;
Packit a4058c
                                src += 4;
Packit a4058c
                                dest += 4;
Packit a4058c
                        }
Packit a4058c
                } else {
Packit a4058c
                        for (x = 0; x < pixbuf->width; x++) {
Packit a4058c
                                tr = *dest++ = *src++;
Packit a4058c
                                tg = *dest++ = *src++;
Packit a4058c
                                tb = *dest++ = *src++;
Packit a4058c
Packit a4058c
                                if (substitute_color && tr == r && tg == g && tb == b)
Packit a4058c
                                        *dest++ = 0;
Packit a4058c
                                else
Packit a4058c
                                        *dest++ = 255;
Packit a4058c
                        }
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return new_pixbuf;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_copy_area:
Packit a4058c
 * @src_pixbuf: Source pixbuf.
Packit a4058c
 * @src_x: Source X coordinate within @src_pixbuf.
Packit a4058c
 * @src_y: Source Y coordinate within @src_pixbuf.
Packit a4058c
 * @width: Width of the area to copy.
Packit a4058c
 * @height: Height of the area to copy.
Packit a4058c
 * @dest_pixbuf: Destination pixbuf.
Packit a4058c
 * @dest_x: X coordinate within @dest_pixbuf.
Packit a4058c
 * @dest_y: Y coordinate within @dest_pixbuf.
Packit a4058c
 *
Packit a4058c
 * Copies a rectangular area from @src_pixbuf to @dest_pixbuf.  Conversion of
Packit a4058c
 * pixbuf formats is done automatically.
Packit a4058c
 *
Packit a4058c
 * If the source rectangle overlaps the destination rectangle on the
Packit a4058c
 * same pixbuf, it will be overwritten during the copy operation.
Packit a4058c
 * Therefore, you can not use this function to scroll a pixbuf.
Packit a4058c
 **/
Packit a4058c
void
Packit a4058c
gdk_pixbuf_copy_area (const GdkPixbuf *src_pixbuf,
Packit a4058c
		      int src_x, int src_y,
Packit a4058c
		      int width, int height,
Packit a4058c
		      GdkPixbuf *dest_pixbuf,
Packit a4058c
		      int dest_x, int dest_y)
Packit a4058c
{
Packit a4058c
	g_return_if_fail (src_pixbuf != NULL);
Packit a4058c
	g_return_if_fail (dest_pixbuf != NULL);
Packit a4058c
Packit a4058c
	g_return_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width);
Packit a4058c
	g_return_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height);
Packit a4058c
Packit a4058c
	g_return_if_fail (dest_x >= 0 && dest_x + width <= dest_pixbuf->width);
Packit a4058c
	g_return_if_fail (dest_y >= 0 && dest_y + height <= dest_pixbuf->height);
Packit a4058c
Packit a4058c
        g_return_if_fail (!(gdk_pixbuf_get_has_alpha (src_pixbuf) && !gdk_pixbuf_get_has_alpha (dest_pixbuf)));
Packit a4058c
        
Packit a4058c
	/* This will perform format conversions automatically */
Packit a4058c
Packit a4058c
	gdk_pixbuf_scale (src_pixbuf,
Packit a4058c
			  dest_pixbuf,
Packit a4058c
			  dest_x, dest_y,
Packit a4058c
			  width, height,
Packit a4058c
			  (double) (dest_x - src_x),
Packit a4058c
			  (double) (dest_y - src_y),
Packit a4058c
			  1.0, 1.0,
Packit a4058c
			  GDK_INTERP_NEAREST);
Packit a4058c
}
Packit a4058c
Packit a4058c
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_saturate_and_pixelate:
Packit a4058c
 * @src: source image
Packit a4058c
 * @dest: place to write modified version of @src
Packit a4058c
 * @saturation: saturation factor
Packit a4058c
 * @pixelate: whether to pixelate
Packit a4058c
 *
Packit a4058c
 * Modifies saturation and optionally pixelates @src, placing the result in
Packit a4058c
 * @dest. @src and @dest may be the same pixbuf with no ill effects.  If
Packit a4058c
 * @saturation is 1.0 then saturation is not changed. If it's less than 1.0,
Packit a4058c
 * saturation is reduced (the image turns toward grayscale); if greater than
Packit a4058c
 * 1.0, saturation is increased (the image gets more vivid colors). If @pixelate
Packit a4058c
 * is %TRUE, then pixels are faded in a checkerboard pattern to create a
Packit a4058c
 * pixelated image. @src and @dest must have the same image format, size, and
Packit a4058c
 * rowstride.
Packit a4058c
 * 
Packit a4058c
 **/
Packit a4058c
void
Packit a4058c
gdk_pixbuf_saturate_and_pixelate(const GdkPixbuf *src,
Packit a4058c
                                 GdkPixbuf *dest,
Packit a4058c
                                 gfloat saturation,
Packit a4058c
                                 gboolean pixelate)
Packit a4058c
{
Packit a4058c
        /* NOTE that src and dest MAY be the same pixbuf! */
Packit a4058c
  
Packit a4058c
        g_return_if_fail (GDK_IS_PIXBUF (src));
Packit a4058c
        g_return_if_fail (GDK_IS_PIXBUF (dest));
Packit a4058c
        g_return_if_fail (gdk_pixbuf_get_height (src) == gdk_pixbuf_get_height (dest));
Packit a4058c
        g_return_if_fail (gdk_pixbuf_get_width (src) == gdk_pixbuf_get_width (dest));
Packit a4058c
        g_return_if_fail (gdk_pixbuf_get_has_alpha (src) == gdk_pixbuf_get_has_alpha (dest));
Packit a4058c
        g_return_if_fail (gdk_pixbuf_get_colorspace (src) == gdk_pixbuf_get_colorspace (dest));
Packit a4058c
  
Packit a4058c
        if (saturation == 1.0 && !pixelate) {
Packit a4058c
                if (dest != src)
Packit a4058c
                        gdk_pixbuf_copy_area (src, 0, 0, 
Packit a4058c
                                              gdk_pixbuf_get_width (src),
Packit a4058c
                                              gdk_pixbuf_get_height (src),
Packit a4058c
                                              dest, 0, 0);
Packit a4058c
        } else {
Packit a4058c
                int i, j, t;
Packit a4058c
                int width, height, has_alpha, src_rowstride, dest_rowstride, bytes_per_pixel;
Packit a4058c
		const guchar *src_line;
Packit a4058c
		guchar *dest_line;
Packit a4058c
                const guchar *src_pixel;
Packit a4058c
		guchar *dest_pixel;
Packit a4058c
                guchar intensity;
Packit a4058c
Packit a4058c
                has_alpha = gdk_pixbuf_get_has_alpha (src);
Packit a4058c
		bytes_per_pixel = has_alpha ? 4 : 3;
Packit a4058c
                width = gdk_pixbuf_get_width (src);
Packit a4058c
                height = gdk_pixbuf_get_height (src);
Packit a4058c
                src_rowstride = gdk_pixbuf_get_rowstride (src);
Packit a4058c
                dest_rowstride = gdk_pixbuf_get_rowstride (dest);
Packit a4058c
                
Packit a4058c
                dest_line = gdk_pixbuf_get_pixels (dest);
Packit a4058c
                src_line = gdk_pixbuf_read_pixels (src);
Packit a4058c
		
Packit a4058c
#define DARK_FACTOR 0.7
Packit a4058c
#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
Packit a4058c
#define CLAMP_UCHAR(v) (t = (v), CLAMP (t, 0, 255))
Packit a4058c
#define SATURATE(v) ((1.0 - saturation) * intensity + saturation * (v))
Packit a4058c
Packit a4058c
		for (i = 0 ; i < height ; i++) {
Packit a4058c
			src_pixel = src_line;
Packit a4058c
			src_line += src_rowstride;
Packit a4058c
			dest_pixel = dest_line;
Packit a4058c
			dest_line += dest_rowstride;
Packit a4058c
Packit a4058c
			for (j = 0 ; j < width ; j++) {
Packit a4058c
                                intensity = INTENSITY (src_pixel[0], src_pixel[1], src_pixel[2]);
Packit a4058c
                                if (pixelate && (i + j) % 2 == 0) {
Packit a4058c
                                        dest_pixel[0] = intensity / 2 + 127;
Packit a4058c
                                        dest_pixel[1] = intensity / 2 + 127;
Packit a4058c
                                        dest_pixel[2] = intensity / 2 + 127;
Packit a4058c
                                } else if (pixelate) {
Packit a4058c
                                        dest_pixel[0] = CLAMP_UCHAR ((SATURATE (src_pixel[0])) * DARK_FACTOR);
Packit a4058c
					dest_pixel[1] = CLAMP_UCHAR ((SATURATE (src_pixel[1])) * DARK_FACTOR);
Packit a4058c
                                        dest_pixel[2] = CLAMP_UCHAR ((SATURATE (src_pixel[2])) * DARK_FACTOR);
Packit a4058c
                                } else {
Packit a4058c
                                        dest_pixel[0] = CLAMP_UCHAR (SATURATE (src_pixel[0]));
Packit a4058c
                                        dest_pixel[1] = CLAMP_UCHAR (SATURATE (src_pixel[1]));
Packit a4058c
                                        dest_pixel[2] = CLAMP_UCHAR (SATURATE (src_pixel[2]));
Packit a4058c
                                }
Packit a4058c
				
Packit a4058c
                                if (has_alpha)
Packit a4058c
                                        dest_pixel[3] = src_pixel[3];
Packit a4058c
Packit a4058c
				src_pixel += bytes_per_pixel;
Packit a4058c
				dest_pixel += bytes_per_pixel;
Packit a4058c
			}
Packit a4058c
                }
Packit a4058c
        }
Packit a4058c
}
Packit a4058c
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_apply_embedded_orientation:
Packit a4058c
 * @src: A #GdkPixbuf.
Packit a4058c
 *
Packit a4058c
 * Takes an existing pixbuf and checks for the presence of an
Packit a4058c
 * associated "orientation" option, which may be provided by the 
Packit a4058c
 * jpeg loader (which reads the exif orientation tag) or the 
Packit a4058c
 * tiff loader (which reads the tiff orientation tag, and
Packit a4058c
 * compensates it for the partial transforms performed by 
Packit a4058c
 * libtiff). If an orientation option/tag is present, the
Packit a4058c
 * appropriate transform will be performed so that the pixbuf
Packit a4058c
 * is oriented correctly.
Packit a4058c
 *
Packit a4058c
 * Return value: (transfer full): A newly-created pixbuf, %NULL if
Packit a4058c
 * not enough memory could be allocated for it, or a reference to the
Packit a4058c
 * input pixbuf (with an increased reference count).
Packit a4058c
 *
Packit a4058c
 * Since: 2.12
Packit a4058c
 **/
Packit a4058c
GdkPixbuf *
Packit a4058c
gdk_pixbuf_apply_embedded_orientation (GdkPixbuf *src)
Packit a4058c
{
Packit a4058c
  	const gchar *orientation_string;
Packit a4058c
	int          transform = 0;
Packit a4058c
	GdkPixbuf   *temp;
Packit a4058c
	GdkPixbuf   *dest;
Packit a4058c
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (src), NULL);
Packit a4058c
Packit a4058c
	/* Read the orientation option associated with the pixbuf */
Packit a4058c
	orientation_string = gdk_pixbuf_get_option (src, "orientation");	
Packit a4058c
Packit a4058c
	if (orientation_string) {
Packit a4058c
		/* If an orientation option was found, convert the 
Packit a4058c
		   orientation string into an integer. */
Packit a4058c
		transform = (int) g_ascii_strtoll (orientation_string, NULL, 10);
Packit a4058c
	}
Packit a4058c
Packit a4058c
	/* Apply the actual transforms, which involve rotations and flips. 
Packit a4058c
	   The meaning of orientation values 1-8 and the required transforms
Packit a4058c
	   are defined by the TIFF and EXIF (for JPEGs) standards. */
Packit a4058c
        switch (transform) {
Packit a4058c
        case 1:
Packit a4058c
                dest = src;
Packit a4058c
                g_object_ref (dest);
Packit a4058c
                break;
Packit a4058c
        case 2:
Packit a4058c
                dest = gdk_pixbuf_flip (src, TRUE);
Packit a4058c
                break;
Packit a4058c
        case 3:
Packit a4058c
                dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
Packit a4058c
                break;
Packit a4058c
        case 4:
Packit a4058c
                dest = gdk_pixbuf_flip (src, FALSE);
Packit a4058c
                break;
Packit a4058c
        case 5:
Packit a4058c
                temp = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
Packit a4058c
                dest = gdk_pixbuf_flip (temp, TRUE);
Packit a4058c
                g_object_unref (temp);
Packit a4058c
                break;
Packit a4058c
        case 6:
Packit a4058c
                dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
Packit a4058c
                break;
Packit a4058c
        case 7:
Packit a4058c
                temp = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
Packit a4058c
                dest = gdk_pixbuf_flip (temp, FALSE);
Packit a4058c
                g_object_unref (temp);
Packit a4058c
                break;
Packit a4058c
        case 8:
Packit a4058c
                dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
Packit a4058c
                break;
Packit a4058c
        default:
Packit a4058c
		/* if no orientation tag was present */
Packit a4058c
                dest = src;
Packit a4058c
                g_object_ref (dest);
Packit a4058c
                break;
Packit a4058c
        }
Packit a4058c
Packit a4058c
        return dest;
Packit a4058c
}
Packit a4058c
Packit a4058c
#ifdef GDK_PIXBUF_RELOCATABLE
Packit a4058c
Packit a4058c
static const gchar *
Packit a4058c
get_localedir (void)
Packit a4058c
{
Packit a4058c
    gchar *temp;
Packit a4058c
    
Packit a4058c
    temp = g_build_filename (gdk_pixbuf_get_toplevel (), "share/locale", NULL);
Packit a4058c
Packit a4058c
#ifdef G_OS_WIN32
Packit a4058c
    {
Packit a4058c
      gchar *retval;
Packit a4058c
      /* The localedir is passed to bindtextdomain() which isn't
Packit a4058c
      * UTF-8-aware.
Packit a4058c
      */
Packit a4058c
      retval = g_win32_locale_filename_from_utf8 (temp);
Packit a4058c
      g_free (temp);
Packit a4058c
      return retval;
Packit a4058c
    }
Packit a4058c
#else
Packit a4058c
    return temp;
Packit a4058c
#endif
Packit a4058c
}
Packit a4058c
Packit a4058c
#undef GDK_PIXBUF_LOCALEDIR
Packit a4058c
#define GDK_PIXBUF_LOCALEDIR get_localedir ()
Packit a4058c
Packit a4058c
#endif
Packit a4058c
Packit a4058c
void
Packit a4058c
_gdk_pixbuf_init_gettext (void)
Packit a4058c
{
Packit a4058c
        static gsize gettext_initialized = FALSE;
Packit a4058c
Packit a4058c
        if (G_UNLIKELY (g_once_init_enter (&gettext_initialized))) {
Packit a4058c
                bindtextdomain (GETTEXT_PACKAGE, GDK_PIXBUF_LOCALEDIR);
Packit a4058c
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
Packit a4058c
                bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Packit a4058c
#endif
Packit a4058c
                g_once_init_leave (&gettext_initialized, TRUE);
Packit a4058c
        }
Packit a4058c
}
Packit a4058c
Packit a4058c
const gchar *
Packit a4058c
gdk_pixbuf_gettext (const gchar *msgid)
Packit a4058c
{
Packit a4058c
        return g_dgettext (GETTEXT_PACKAGE, msgid);
Packit a4058c
}