source: libpipi/trunk/pipi/combine/mulscreen.c @ 2844

Last change on this file since 2844 was 2844, checked in by Sam Hocevar, 12 years ago

Fix headers.

File size: 5.9 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 * mulscreen.c: multiply and screen computation functions
17 */
18
19#include "config.h"
20#include "common.h"
21
22#include "pipi.h"
23#include "pipi_internals.h"
24
25pipi_image_t *pipi_multiply(pipi_image_t *img1, pipi_image_t *img2)
26{
27    pipi_image_t *dst;
28    pipi_pixels_t *img1p, *img2p, *dstp;
29    float *img1data, *img2data, *dstdata;
30    int x, y, w, h;
31
32    if(img1->w != img2->w || img1->h != img2->h)
33        return NULL;
34
35    w = img1->w;
36    h = img1->h;
37
38    dst = pipi_new(w, h);
39    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
40    dstdata = (float *)dstp->pixels;
41
42    img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
43    img1data = (float *)img1p->pixels;
44    img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
45    img2data = (float *)img2p->pixels;
46
47    for(y = 0; y < h; y++)
48    {
49        for(x = 0; x < w; x++)
50        {
51            float p, q;
52
53            p = img1data[4 * (y * w + x)];
54            q = img2data[4 * (y * w + x)];
55            dstdata[4 * (y * w + x)] = p * q;
56
57            p = img1data[4 * (y * w + x) + 1];
58            q = img2data[4 * (y * w + x) + 1];
59            dstdata[4 * (y * w + x) + 1] = p * q;
60
61            p = img1data[4 * (y * w + x) + 2];
62            q = img2data[4 * (y * w + x) + 2];
63            dstdata[4 * (y * w + x) + 2] = p * q;
64
65            p = img1data[4 * (y * w + x) + 3];
66            q = img2data[4 * (y * w + x) + 3];
67            dstdata[4 * (y * w + x) + 3] = p * q;
68        }
69    }
70
71    return dst;
72}
73
74pipi_image_t *pipi_divide(pipi_image_t *img1, pipi_image_t *img2)
75{
76    pipi_image_t *dst;
77    pipi_pixels_t *img1p, *img2p, *dstp;
78    float *img1data, *img2data, *dstdata;
79    int x, y, w, h;
80
81    if(img1->w != img2->w || img1->h != img2->h)
82        return NULL;
83
84    w = img1->w;
85    h = img1->h;
86
87    dst = pipi_new(w, h);
88    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
89    dstdata = (float *)dstp->pixels;
90
91    img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
92    img1data = (float *)img1p->pixels;
93    img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
94    img2data = (float *)img2p->pixels;
95
96    for(y = 0; y < h; y++)
97    {
98        for(x = 0; x < w; x++)
99        {
100            float p, q;
101
102            p = img1data[4 * (y * w + x)];
103            q = img2data[4 * (y * w + x)];
104            dstdata[4 * (y * w + x)] = p >= q ? 1. : p / q;
105
106            p = img1data[4 * (y * w + x) + 1];
107            q = img2data[4 * (y * w + x) + 1];
108            dstdata[4 * (y * w + x) + 1] = p >= q ? 1. : p / q;
109
110            p = img1data[4 * (y * w + x) + 2];
111            q = img2data[4 * (y * w + x) + 2];
112            dstdata[4 * (y * w + x) + 2] = p >= q ? 1. : p / q;
113
114            p = img1data[4 * (y * w + x) + 3];
115            q = img2data[4 * (y * w + x) + 3];
116            dstdata[4 * (y * w + x) + 3] = p >= q ? 1. : p / q;
117        }
118    }
119
120    return dst;
121}
122
123pipi_image_t *pipi_screen(pipi_image_t *img1, pipi_image_t *img2)
124{
125    pipi_image_t *dst;
126    pipi_pixels_t *img1p, *img2p, *dstp;
127    float *img1data, *img2data, *dstdata;
128    int x, y, w, h;
129
130    if(img1->w != img2->w || img1->h != img2->h)
131        return NULL;
132
133    w = img1->w;
134    h = img1->h;
135
136    dst = pipi_new(w, h);
137    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
138    dstdata = (float *)dstp->pixels;
139
140    img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
141    img1data = (float *)img1p->pixels;
142    img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
143    img2data = (float *)img2p->pixels;
144
145    for(y = 0; y < h; y++)
146    {
147        for(x = 0; x < w; x++)
148        {
149            float p, q;
150
151            p = img1data[4 * (y * w + x)];
152            q = img2data[4 * (y * w + x)];
153            dstdata[4 * (y * w + x)] = p + q - p * q;
154
155            p = img1data[4 * (y * w + x) + 1];
156            q = img2data[4 * (y * w + x) + 1];
157            dstdata[4 * (y * w + x) + 1] = p + q - p * q;
158
159            p = img1data[4 * (y * w + x) + 2];
160            q = img2data[4 * (y * w + x) + 2];
161            dstdata[4 * (y * w + x) + 2] = p + q - p * q;
162
163            p = img1data[4 * (y * w + x) + 3];
164            q = img2data[4 * (y * w + x) + 3];
165            dstdata[4 * (y * w + x) + 3] = p + q - p * q;
166        }
167    }
168
169    return dst;
170}
171
172pipi_image_t *pipi_overlay(pipi_image_t *img1, pipi_image_t *img2)
173{
174    pipi_image_t *dst;
175    pipi_pixels_t *img1p, *img2p, *dstp;
176    float *img1data, *img2data, *dstdata;
177    int x, y, w, h;
178
179    if(img1->w != img2->w || img1->h != img2->h)
180        return NULL;
181
182    w = img1->w;
183    h = img1->h;
184
185    dst = pipi_new(w, h);
186    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
187    dstdata = (float *)dstp->pixels;
188
189    img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
190    img1data = (float *)img1p->pixels;
191    img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
192    img2data = (float *)img2p->pixels;
193
194    for(y = 0; y < h; y++)
195    {
196        for(x = 0; x < w; x++)
197        {
198            float p, q;
199
200            p = img1data[4 * (y * w + x)];
201            q = img2data[4 * (y * w + x)];
202            dstdata[4 * (y * w + x)] = p * (p + 2. * q * (1. - p));
203
204            p = img1data[4 * (y * w + x) + 1];
205            q = img2data[4 * (y * w + x) + 1];
206            dstdata[4 * (y * w + x) + 1] = p * (p + 2. * q * (1. - p));
207
208            p = img1data[4 * (y * w + x) + 2];
209            q = img2data[4 * (y * w + x) + 2];
210            dstdata[4 * (y * w + x) + 2] = p * (p + 2. * q * (1. - p));
211
212            p = img1data[4 * (y * w + x) + 3];
213            q = img2data[4 * (y * w + x) + 3];
214            dstdata[4 * (y * w + x) + 3] = p * (p + 2. * q * (1. - p));
215        }
216    }
217
218    return dst;
219}
220
Note: See TracBrowser for help on using the repository browser.