source: libpipi/trunk/pipi/combine/subadd.c @ 2911

Last change on this file since 2911 was 2911, checked in by Sam Hocevar, 11 years ago

libpipi: include <stdlib.h> in files where NULL is used.

File size: 4.6 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface 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 * subadd.c: sub, add and difference computation functions
17 */
18
19#include "config.h"
20
21#include <stdlib.h>
22#include <math.h>
23
24#include "pipi.h"
25#include "pipi_internals.h"
26
27pipi_image_t *pipi_add(pipi_image_t *img1, pipi_image_t *img2)
28{
29    pipi_image_t *dst;
30    pipi_pixels_t *img1p, *img2p, *dstp;
31    float *img1data, *img2data, *dstdata;
32    int x, y, w, h;
33
34    if(img1->w != img2->w || img1->h != img2->h)
35        return NULL;
36
37    w = img1->w;
38    h = img1->h;
39
40    dst = pipi_new(w, h);
41    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
42    dstdata = (float *)dstp->pixels;
43
44    img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
45    img1data = (float *)img1p->pixels;
46    img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
47    img2data = (float *)img2p->pixels;
48
49    for(y = 0; y < h; y++)
50    {
51        for(x = 0; x < w; x++)
52        {
53            float p, q;
54
55            p = img1data[4 * (y * w + x)];
56            q = img2data[4 * (y * w + x)];
57            dstdata[4 * (y * w + x)] = (p + q < 1.) ? p + q : 1.;
58
59            p = img1data[4 * (y * w + x) + 1];
60            q = img2data[4 * (y * w + x) + 1];
61            dstdata[4 * (y * w + x) + 1] = (p + q < 1.) ? p + q : 1.;
62
63            p = img1data[4 * (y * w + x) + 2];
64            q = img2data[4 * (y * w + x) + 2];
65            dstdata[4 * (y * w + x) + 2] = (p + q < 1.) ? p + q : 1.;
66
67            p = img1data[4 * (y * w + x) + 3];
68            q = img2data[4 * (y * w + x) + 3];
69            dstdata[4 * (y * w + x) + 3] = (p + q < 1.) ? p + q : 1.;
70        }
71    }
72
73    return dst;
74}
75
76pipi_image_t *pipi_sub(pipi_image_t *img1, pipi_image_t *img2)
77{
78    pipi_image_t *dst;
79    pipi_pixels_t *img1p, *img2p, *dstp;
80    float *img1data, *img2data, *dstdata;
81    int x, y, w, h;
82
83    if(img1->w != img2->w || img1->h != img2->h)
84        return NULL;
85
86    w = img1->w;
87    h = img1->h;
88
89    dst = pipi_new(w, h);
90    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
91    dstdata = (float *)dstp->pixels;
92
93    img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
94    img1data = (float *)img1p->pixels;
95    img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
96    img2data = (float *)img2p->pixels;
97
98    for(y = 0; y < h; y++)
99    {
100        for(x = 0; x < w; x++)
101        {
102            float p, q;
103
104            p = img1data[4 * (y * w + x)];
105            q = img2data[4 * (y * w + x)];
106            dstdata[4 * (y * w + x)] = p < q ? 0 : p - q;
107
108            p = img1data[4 * (y * w + x) + 1];
109            q = img2data[4 * (y * w + x) + 1];
110            dstdata[4 * (y * w + x) + 1] = p < q ? 0 : p - q;
111
112            p = img1data[4 * (y * w + x) + 2];
113            q = img2data[4 * (y * w + x) + 2];
114            dstdata[4 * (y * w + x) + 2] = p < q ? 0 : p - q;
115
116            p = img1data[4 * (y * w + x) + 3];
117            q = img2data[4 * (y * w + x) + 3];
118            dstdata[4 * (y * w + x) + 3] = p < q ? 0 : p - q;
119        }
120    }
121
122    return dst;
123}
124
125pipi_image_t *pipi_difference(pipi_image_t *img1, pipi_image_t *img2)
126{
127    pipi_image_t *dst;
128    pipi_pixels_t *img1p, *img2p, *dstp;
129    float *img1data, *img2data, *dstdata;
130    int x, y, w, h;
131
132    if(img1->w != img2->w || img1->h != img2->h)
133        return NULL;
134
135    w = img1->w;
136    h = img1->h;
137
138    dst = pipi_new(w, h);
139    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
140    dstdata = (float *)dstp->pixels;
141
142    img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
143    img1data = (float *)img1p->pixels;
144    img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
145    img2data = (float *)img2p->pixels;
146
147    for(y = 0; y < h; y++)
148    {
149        for(x = 0; x < w; x++)
150        {
151            float p, q;
152
153            p = img1data[4 * (y * w + x)];
154            q = img2data[4 * (y * w + x)];
155            dstdata[4 * (y * w + x)] = fabsf(p - q);
156
157            p = img1data[4 * (y * w + x) + 1];
158            q = img2data[4 * (y * w + x) + 1];
159            dstdata[4 * (y * w + x) + 1] = fabsf(p - q);
160
161            p = img1data[4 * (y * w + x) + 2];
162            q = img2data[4 * (y * w + x) + 2];
163            dstdata[4 * (y * w + x) + 2] = fabsf(p - q);
164
165            p = img1data[4 * (y * w + x) + 3];
166            q = img2data[4 * (y * w + x) + 3];
167            dstdata[4 * (y * w + x) + 3] = fabsf(p - q);
168        }
169    }
170
171    return dst;
172}
173
Note: See TracBrowser for help on using the repository browser.