fpi-img-pixman.c 2.54 KB
Newer Older
1 2 3
/*
 * Imaging utility functions for libfprint
 * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
4
 * Copyright (C) 2013 Vasily Khoruzhick <anarsoul@gmail.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21 22
#include <pixman.h>
#include <string.h>
23 24 25

#include "fp_internal.h"

26 27 28 29 30 31 32 33 34 35 36 37
/**
 * fpi_img_resize:
 * @img: an #fp_img image
 * @w_factor: horizontal factor to resize the image by
 * @h_factor: vertical factor to resize the image by
 *
 * Resizes the #fp_img image by scaling it by @w_factor times horizontally
 * and @h_factor times vertically.
 *
 * Returns: a newly allocated #fp_img, the original @img will not be modified
 * and will also need to be freed
 */
38
struct fp_img *fpi_img_resize(struct fp_img *img, unsigned int w_factor, unsigned int h_factor)
39
{
40 41
	int new_width = img->width * w_factor;
	int new_height = img->height * h_factor;
42 43
	pixman_image_t *orig, *resized;
	pixman_transform_t transform;
44 45
	struct fp_img *newimg;

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	orig = pixman_image_create_bits(PIXMAN_a8, img->width, img->height, (uint32_t *)img->data, img->width);
	resized = pixman_image_create_bits(PIXMAN_a8, new_width, new_height, NULL, new_width);

	pixman_transform_init_identity(&transform);
	pixman_transform_scale(NULL, &transform, pixman_int_to_fixed(w_factor), pixman_int_to_fixed(h_factor));
	pixman_image_set_transform(orig, &transform);
	pixman_image_set_filter(orig, PIXMAN_FILTER_BILINEAR, NULL, 0);
	pixman_image_composite32(PIXMAN_OP_SRC,
		orig, /* src */
		NULL, /* mask */
		resized, /* dst */
		0, 0, /* src x y */
		0, 0, /* mask x y */
		0, 0, /* dst x y */
		new_width, new_height /* width height */
		);
62 63 64 65 66 67

	newimg = fpi_img_new(new_width * new_height);
	newimg->width = new_width;
	newimg->height = new_height;
	newimg->flags = img->flags;

68
	memcpy(newimg->data, pixman_image_get_data(resized), new_width * new_height);
69

70 71
	pixman_image_unref(orig);
	pixman_image_unref(resized);
72 73 74 75

	return newimg;
}