1 #include "config.h"
2
3 #include <stdio.h>
4 #include <glib.h>
5 #include <string.h>
6
7 #include "portab.h"
8 #include "surface.h"
9 #include "system.h"
10
create(int width,int height,int depth,boolean has_pixel,boolean has_alpha)11 static surface_t *create(int width, int height, int depth, boolean has_pixel, boolean has_alpha) {
12 surface_t *s = g_new0(surface_t, 1);
13
14 s->width = width;
15 s->height = height;
16 s->has_alpha = has_alpha;
17 s->has_pixel = has_pixel;
18
19 s->bytes_per_line = width;
20 s->bytes_per_pixel = 1;
21 s->depth = depth;
22
23 if (s->has_pixel) {
24 switch (s->depth) {
25 case 8:
26 s->pixel = g_new0(BYTE, width * (height +1));
27 s->bytes_per_line = width;
28 s->bytes_per_pixel = 1;
29 break;
30 case 15:
31 case 16:
32 s->pixel = g_new0(BYTE, width * (height +1) * 2);
33 s->bytes_per_line = width * 2;
34 s->bytes_per_pixel = 2;
35 break;
36 case 24:
37 case 32:
38 s->pixel = g_new0(BYTE, width * (height +1) * 4);
39 s->bytes_per_line = width * 4;
40 s->bytes_per_pixel = 4;
41 break;
42 default:
43 WARNING("depth %d is not supported\n", s->depth);
44 }
45 } else {
46 s->pixel = NULL;
47 }
48
49 if (s->has_alpha) {
50 s->alpha = g_new0(BYTE, width * (height +1));
51 }
52
53 return s;
54 }
55
56 /**
57 * surface������ (pixel + alpha)
58 * @param width: surface����
59 * @param height: surface�ι⤵
60 * @param depth: surface��pixel��BPP (8|15|16|24|32), alpha��8����
61 * @return �������� surface ���֥�������
62 */
sf_create_surface(int width,int height,int depth)63 surface_t *sf_create_surface(int width, int height, int depth) {
64 return create(width, height, depth, TRUE, TRUE);
65 }
66
67 /**
68 * surface������ (alpha��)
69 * @param width: surface����
70 * @param height: surface�ι⤵
71 * @return �������� surface ���֥�������
72 */
sf_create_alpha(int width,int height)73 surface_t *sf_create_alpha(int width, int height) {
74 return create(width, height, 8, FALSE, TRUE);
75 }
76
77 /**
78 * surface������ (pixel��)
79 * @param width: surface����
80 * @param height: surface�ι⤵
81 * @param depth: surface��BPP(8|15|16|24|32)
82 * @return �������� surface ���֥�������
83 */
sf_create_pixel(int width,int height,int depth)84 surface_t *sf_create_pixel(int width, int height, int depth) {
85 return create(width, height, depth, TRUE, FALSE);
86 }
87
88 /**
89 * surface���֥������Ȥγ���
90 * @param s: �������륪�֥�������
91 * @return: �ʤ�
92 */
sf_free(surface_t * s)93 void sf_free(surface_t *s) {
94 if (s == NULL) return;
95 if (s->pixel) g_free(s->pixel);
96 if (s->alpha) g_free(s->alpha);
97 g_free(s);
98 }
99
100 /**
101 * surface ��ʣ�������(dupulicate)
102 * @param in: ʣ�����
103 * @return : ʣ������surface
104 */
sf_dup(surface_t * in)105 surface_t *sf_dup(surface_t *in) {
106 surface_t *sf;
107 int len;
108
109 if (in == NULL) return NULL;
110
111 sf = g_new(surface_t, 1);
112 memcpy(sf, in, sizeof(surface_t));
113
114 if (in->has_pixel) {
115 len = sf->bytes_per_line * sf->height;
116 sf->pixel = g_new(BYTE, len + sf->bytes_per_line);
117 memcpy(sf->pixel, in->pixel, len);
118 }
119
120 if (in->has_alpha) {
121 len = sf->width * sf->height;
122 sf->alpha = g_new(BYTE, len + sf->width);
123 memcpy(sf->alpha, in->alpha, len);
124 }
125
126 return sf;
127 }
128
129 /**
130 * surface���ΤΥ��ԡ�
131 * @param dst: ���ԡ��� surface
132 * @param src: ���ԡ��� surface
133 * @return �ʤ�
134 */
sf_copyall(surface_t * dst,surface_t * src)135 void sf_copyall(surface_t *dst, surface_t *src) {
136 int len;
137
138 if (src == NULL || dst == NULL) return;
139
140 if (src->width != dst->width) return;
141
142 if (src->height != dst->height) return;
143
144 if (src->bytes_per_pixel != dst->bytes_per_pixel) return;
145
146 if (src->has_alpha && dst->has_alpha) {
147 len = src->width * src->height;
148 memcpy(dst->alpha, src->alpha, len);
149 }
150
151 if (src->has_pixel && dst->has_pixel) {
152 len = src->bytes_per_line * src->height;
153 memcpy(dst->pixel, src->pixel, len);
154 }
155 }
156
157 /**
158 * surface ��ʣ��
159 * @param in: ʣ�����
160 * @param copypixel: pixel�ԡ����뤫
161 * @param copyalpha: alpha pixel �ԡ����뤫
162 * @return: ʣ������ surface
163 */
sf_dup2(surface_t * in,boolean copypixel,boolean copyalpha)164 surface_t *sf_dup2(surface_t *in, boolean copypixel, boolean copyalpha) {
165 surface_t *sf;
166 int len;
167
168 if (in == NULL) return NULL;
169
170 sf = g_new(surface_t, 1);
171 memcpy(sf, in, sizeof(surface_t));
172
173 if (in->has_pixel) {
174 len = sf->bytes_per_line * sf->height;
175 sf->pixel = g_new(BYTE, len + sf->bytes_per_line);
176 if (copypixel) {
177 memcpy(sf->pixel, in->pixel, len);
178 }
179 }
180
181 if (in->has_alpha) {
182 len = sf->width * sf->height;
183 sf->alpha = g_new(BYTE, len + sf->width);
184 if (copyalpha) {
185 memcpy(sf->alpha, in->alpha, len);
186 }
187 }
188
189 return sf;
190 }
191
192