summaryrefslogtreecommitdiff
path: root/image.c
blob: d0f868e7abb84515a9627e892624686dbbee15c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <glib.h>
#include "image.h"

complex_image_t *
complex_image_new (int width, int height)
{
    complex_image_t *image = g_new0 (complex_image_t, 1);
    
    image->width = width;
    image->height = height;
    image->red = g_new0 (complex_t, width * height);
    image->green = g_new0 (complex_t, width * height);
    image->blue = g_new0 (complex_t, width * height);
    
    return image;
}

complex_image_t *
complex_image_copy (complex_image_t *image)
{
    complex_image_t *copy = complex_image_new (image->width, image->height);
    int width = image->width;
    int height = image->height;
    
    copy->red = g_memdup (image->red, width * height * sizeof (complex_t));
    copy->green = g_memdup (image->green, width * height * sizeof (complex_t));
    copy->blue = g_memdup (image->blue, width * height * sizeof (complex_t));
    
    return copy;
}

void
complex_image_subtract (complex_image_t *image, complex_image_t *other)
{
    int i, j;
    int h = image->height;
    int w = image->width;
    
    for (i = 0; i < h; ++i)
    {
	for (j = 0; j < w; ++j)
	{
	    int idx = i * w + j;
	    
	    image->red[idx] = complex_sub (image->red[idx], other->red[idx]);
	    image->green[idx] = complex_sub (image->green[idx], other->green[idx]);
	    image->blue[idx] = complex_sub (image->blue[idx], other->blue[idx]);
	}
    }
}

complex_image_t *
complex_image_from_pixbuf (GdkPixbuf *pixbuf)
{
    complex_image_t *result;
    uint8_t *pdata;
    int w, h, s;
    int i, j;
    gboolean has_alpha;
    int n_channels;

    w = gdk_pixbuf_get_width (pixbuf);
    h = gdk_pixbuf_get_height (pixbuf);
    s = gdk_pixbuf_get_rowstride (pixbuf);
    pdata = gdk_pixbuf_get_pixels (pixbuf);
    has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
    n_channels = 3 + has_alpha;

    printf ("has alpha: %d\n", has_alpha);

    result = complex_image_new (w, h);

    for (i = 0; i < h; ++i)
    {
	for (j = 0; j < w; ++j)
	{
	    uint8_t *p = &pdata[i * s + j * n_channels];
	    int idx = i * w + j;

	    result->red[idx].re = p[0] / 255.0;
	    result->green[idx].re = p[1] / 255.0;
	    result->blue[idx].re = p[2] / 255.0;
	}
    }

    return result;
}