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

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

Support C99 types on Win32 through the same hacks as in libcaca.

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