File: surface.c

package info (click to toggle)
xsystem35 1.7.3-pre5-7
  • links: PTS
  • area: main
  • in suites: buster
  • size: 7,332 kB
  • sloc: ansic: 51,002; sh: 12,048; asm: 863; makefile: 410; xml: 281; perl: 142
file content (191 lines) | stat: -rw-r--r-- 4,105 bytes parent folder | download | duplicates (4)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "config.h"

#include <stdio.h>
#include <glib.h>
#include <string.h>

#include "portab.h"
#include "surface.h"
#include "system.h"

static surface_t *create(int width, int height, int depth, boolean has_pixel, boolean has_alpha) {
	surface_t *s = g_new0(surface_t, 1);
	
	s->width = width;
	s->height = height;
	s->has_alpha = has_alpha;
	s->has_pixel = has_pixel;
	
	s->bytes_per_line = width;
	s->bytes_per_pixel = 1;
	s->depth = depth;
	
	if (s->has_pixel) {
		switch (s->depth) {
		case 8:
			s->pixel = g_new0(BYTE, width * (height +1));
			s->bytes_per_line = width;
			s->bytes_per_pixel = 1;
			break;
		case 15:
		case 16:
			s->pixel = g_new0(BYTE, width * (height +1) * 2);
			s->bytes_per_line = width * 2;
			s->bytes_per_pixel = 2;
			break;
		case 24:
		case 32:
			s->pixel = g_new0(BYTE, width * (height +1) * 4);
			s->bytes_per_line = width * 4;
			s->bytes_per_pixel = 4;
			break;
		default:
			WARNING("depth %d is not supported\n", s->depth);
		}
	} else {
		s->pixel = NULL;
	}
	
	if (s->has_alpha) {
		s->alpha = g_new0(BYTE, width * (height +1));
	}
	
	return s;
}

/**
 * surface (pixel + alpha)
 * @param width:  surface
 * @param height: surfaceι⤵
 * @param depth:  surfacepixelBPP (8|15|16|24|32), alpha8
 * @return  surface ֥
 */
surface_t *sf_create_surface(int width, int height, int depth) {
	return create(width, height, depth, TRUE, TRUE);
}

/**
 * surface (alphaΤ)
 * @param width:  surface
 * @param height: surfaceι⤵
 * @return  surface ֥
 */
surface_t *sf_create_alpha(int width, int height) {
	return create(width, height, 8, FALSE, TRUE);
}

/**
 * surface (pixelΤ)
 * @param width:  surface
 * @param height: surfaceι⤵
 * @param depth:  surfaceBPP(8|15|16|24|32)
 * @return  surface ֥
 */
surface_t *sf_create_pixel(int width, int height, int depth) {
	return create(width, height, depth, TRUE, FALSE);
}

/**
 * surface֥Ȥγ
 * @param s: 륪֥
 * @return:  ʤ
 */
void sf_free(surface_t *s) {
	if (s == NULL) return;
	if (s->pixel) g_free(s->pixel);
	if (s->alpha) g_free(s->alpha);
	g_free(s);
}

/**
 * surface ʣ(dupulicate)
 * @param in: ʣ
 * @return  : ʣsurface
 */
surface_t *sf_dup(surface_t *in) {
	surface_t *sf;
	int len;
	
	if (in == NULL) return NULL;
	
	sf = g_new(surface_t, 1);
	memcpy(sf, in, sizeof(surface_t));
	
	if (in->has_pixel) {
		len = sf->bytes_per_line * sf->height;
		sf->pixel = g_new(BYTE, len + sf->bytes_per_line);
		memcpy(sf->pixel, in->pixel, len);
	}
	
	if (in->has_alpha) {
		len = sf->width * sf->height;
		sf->alpha = g_new(BYTE, len + sf->width);
		memcpy(sf->alpha, in->alpha, len);
	}
	
	return sf;
}

/**
 * surfaceΤΥԡ
 * @param dst: ԡ surface
 * @param src: ԡ surface
 * @return ʤ
 */
void sf_copyall(surface_t *dst, surface_t *src) {
	int len;
	
	if (src == NULL || dst == NULL) return;
	
	if (src->width != dst->width) return;
	
	if (src->height != dst->height) return;
	
	if (src->bytes_per_pixel != dst->bytes_per_pixel) return;
	
	if (src->has_alpha && dst->has_alpha) {
		len = src->width * src->height;
		memcpy(dst->alpha, src->alpha, len);
	}
	
	if (src->has_pixel && dst->has_pixel) {
		len = src->bytes_per_line * src->height;
		memcpy(dst->pixel, src->pixel, len);
	}
}

/**
 * surface ʣ
 * @param in: ʣ
 * @param copypixel: pixel򥳥ԡ뤫
 * @param copyalpha: alpha pixel 򥳥ԡ뤫
 * @return: ʣ surface
 */
surface_t *sf_dup2(surface_t *in, boolean copypixel, boolean copyalpha) {
	surface_t *sf;
	int len;
	
	if (in == NULL) return NULL;
	
	sf = g_new(surface_t, 1);
	memcpy(sf, in, sizeof(surface_t));
	
	if (in->has_pixel) {
		len = sf->bytes_per_line * sf->height;
		sf->pixel = g_new(BYTE, len + sf->bytes_per_line);
		if (copypixel) {
			memcpy(sf->pixel, in->pixel, len);
		}
	}
	
	if (in->has_alpha) {
		len = sf->width * sf->height;
		sf->alpha = g_new(BYTE, len + sf->width);
		if (copyalpha) {
			memcpy(sf->alpha, in->alpha, len);
		}
	}
	
	return sf;
}