summaryrefslogtreecommitdiff
path: root/graymap.c
blob: c44347ca506dd2981f5ba1bb67f4a0eb08935fd1 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "graymap.h"
#include "common_types.h"
#include <stdlib.h>

graymap_t create_graymap(int w, int h)
{
	graymap_t rv;
	rv.w = -1;
	rv.h = -1;
	rv.data = (double*) malloc(sizeof(double) * w * h);
	if(rv.data == NULL)
		return rv;
	rv.w = w;
	rv.h = h;
	return rv;
}

void destroy_graymap(graymap_t* gm)
{
	free(gm->data);
	gm->w = gm->h = -1;
}

void clear_graymap(graymap_t* gm, double grayval)
{
	int limit = gm->w * gm->h;
	int i;
	double* data_p = gm->data;
	for(i = 0; i < limit; ++i, ++data_p)
		(*data_p) = grayval;
}

void set_pix_graymap(graymap_t* gm, int x, int y, double gray)
{
	GM_PIX((*gm),x,y) = gray;
}

double get_pix_graymap(graymap_t* gm, int x, int y)
{
	return GM_PIX(*gm,x,y);
}

void blit_graymaps(graymap_t* src, graymap_t* dst, int src_x, int src_y,
 int dst_x, int dst_y, int w, int h)
{
	if(src_x + w >= src->w)
		w = src->w - src_x;
	if(src_y + h >= src->h)
		h = src->h - src_y;
	if(dst_x + w >= dst->w)
		w = dst->w - dst_x;
	if(dst_y + h >= dst->h)
		h = dst->h - dst_y;
	int x,y;
	for(y = 0; y < h; ++y)
		for(x = 0; x < w; ++x)
			GM_PIX(*dst,dst_x+x, dst_y+y) = GM_PIX(*src, src_x+x, src_y+y);
}

color_t get_palette_color(double grayval, color_t* pal_cols, double* pal_grays,
 int pal_n)
{
	color_t rv;
	rv.r = rv.g = rv.b = rv.a = .0;
	int index;
	double factor1, factor2;
	BOOL found = FALSE;
	/* find the index number */
	for(index = 0; index < ( pal_n - 1 ); ++index)
	{
		if((grayval >= pal_grays[index]) && (grayval <= pal_grays[index+1]))
		{
			found = TRUE;
			break;
		}
	}
	
	if(!found)
		return rv;
	
	/* Calculate factors */
	factor1 = (pal_grays[index+1] - grayval ) /
	 ( pal_grays[index+1] -pal_grays[index] );
	factor2 = 1.0 - factor1;
	
	/* Calculate color */
	rv.r = ( pal_cols[index].r * factor1 ) +
	 ( pal_cols[index+1].r * factor2 );
	rv.g = ( pal_cols[index].g * factor1 ) +
	 ( pal_cols[index+1].g * factor2 );
	rv.b = ( pal_cols[index].b * factor1 ) +
	 ( pal_cols[index+1].b * factor2 );
	rv.a = ( pal_cols[index].a * factor1 ) +
	 ( pal_cols[index+1].a * factor2 );
	return rv;
}

color_t mkcol(double r, double g, double b, double a)
{
	color_t rv;
	rv.r = r;
	rv.g = g;
	rv.b = b;
	rv.a = a;
	return rv;
}