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