source: libpipi/trunk/pipi/crop.c @ 4690

Last change on this file since 4690 was 4690, checked in by Sam Hocevar, 10 years ago

Move resize.c to an algorithm-specific file so that we can implement
other resizing methods.

  • Property svn:keywords set to Id
File size: 1.5 KB
RevLine 
[4690]1/*
2 *  libpipi       Pathetic image processing interface library
3 *  Copyright (c) 2004-2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: crop.c 4690 2010-10-16 21:52:38Z sam $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * crop.c: image cropping functions
17 */
18
19#include "config.h"
20
21#include <stdlib.h>
22#include <string.h>
23
24#include "pipi.h"
25#include "pipi_internals.h"
26
27pipi_image_t *pipi_crop(pipi_image_t *src, int w, int h, int dx, int dy)
28{
29    float *srcdata, *dstdata;
30    pipi_image_t *dst;
31    pipi_pixels_t *srcp, *dstp;
32    int y, off, len;
33
34    srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
35    srcdata = (float *)srcp->pixels;
36
37    dst = pipi_new(w, h);
38    dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
39    dstdata = (float *)dstp->pixels;
40
41    off = dx;
42    len = w;
43
44    if(dx < 0)
45    {
46        len += dx;
47        dx = 0;
48    }
49
50    if(dx + len > srcp->w)
51        len = srcp->w - dx;
52
53    if(len > 0)
54    {
55        for(y = 0; y < h; y++)
56        {
57            if(y + dy < 0 || y + dy >= srcp->h)
58                continue;
59
60            memcpy(dstdata + y * w * 4,
61                   srcdata + ((y + dy) * srcp->w + dx) * 4,
62                   len * 4 * sizeof(float));
63        }
64    }
65
66    return dst;
67}
68
Note: See TracBrowser for help on using the repository browser.