source: libpipi/trunk/pipi/filter/transform.c @ 2749

Last change on this file since 2749 was 2749, checked in by Sam Hocevar, 12 years ago
  • Implement pipi_vflip() and pipi_hflip().
File size: 2.9 KB
Line 
1/*
2 *  libpipi       Proper image processing implementation library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
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 * transform.c: basic transformation functions
17 */
18
19#include "config.h"
20#include "common.h"
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <math.h>
26
27#include "pipi.h"
28#include "pipi_internals.h"
29
30pipi_image_t *pipi_hflip(pipi_image_t *src)
31{
32    pipi_image_t *dst;
33    pipi_pixels_t *srcp, *dstp;
34    float *srcdata, *dstdata;
35    int x, y, w, h, gray;
36
37    w = src->w;
38    h = src->h;
39
40    gray = (src->last_modified == PIPI_PIXELS_Y_F);
41
42    srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
43                : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
44    srcdata = (float *)srcp->pixels;
45
46    dst = pipi_new(w, h);
47    dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
48                : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
49    dstdata = (float *)dstp->pixels;
50
51    for(y = 0; y < h; y++)
52    {
53        for(x = 0; x < w; x++)
54        {
55            if(gray)
56            {
57                dstdata[y * w + x] = srcdata[y * w + w - 1 - x];
58            }
59            else
60            {
61                dstdata[4 * (y * w + x)]
62                                = srcdata[4 * (y * w + w - 1 - x)];
63                dstdata[4 * (y * w + x) + 1]
64                                = srcdata[4 * (y * w + w - 1 - x) + 1];
65                dstdata[4 * (y * w + x) + 2]
66                                = srcdata[4 * (y * w + w - 1 - x) + 2];
67                dstdata[4 * (y * w + x) + 3]
68                                = srcdata[4 * (y * w + w - 1 - x) + 3];
69            }
70        }
71    }
72
73    return dst;
74}
75
76pipi_image_t *pipi_vflip(pipi_image_t *src)
77{
78    pipi_image_t *dst;
79    pipi_pixels_t *srcp, *dstp;
80    float *srcdata, *dstdata;
81    int x, y, w, h, gray;
82
83    w = src->w;
84    h = src->h;
85
86    gray = (src->last_modified == PIPI_PIXELS_Y_F);
87
88    srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
89                : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
90    srcdata = (float *)srcp->pixels;
91
92    dst = pipi_new(w, h);
93    dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
94                : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
95    dstdata = (float *)dstp->pixels;
96
97    for(y = 0; y < h; y++)
98    {
99        if(gray)
100        {
101            memcpy(dstdata + y * w,
102                   srcdata + (h - 1 - y) * w,
103                   w * sizeof(*dstdata));
104        }
105        else
106        {
107            memcpy(dstdata + 4 * y * w,
108                   srcdata + 4 * (h - 1 - y) * w,
109                   4 * w * sizeof(*dstdata));
110        }
111    }
112
113    return dst;
114}
115
Note: See TracBrowser for help on using the repository browser.