-
Notifications
You must be signed in to change notification settings - Fork 2
/
oil-image.c
241 lines (196 loc) · 5.81 KB
/
oil-image.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "oil.h"
#include "oil-image-private.h"
#include "oil-format-private.h"
#include <stdlib.h>
#include <stdio.h>
/*
* file IO implementation for plain old FILEs
*/
static size_t oil_image_read(void *file, void *data, size_t length) {
return fread(data, 1, length, file);
}
static size_t oil_image_write(void *file, void *data, size_t length) {
return fwrite(data, 1, length, file);
}
static void oil_image_flush(void *file) {
fflush((FILE *)file);
}
OILImage *oil_image_new(unsigned int width, unsigned int height) {
OILImage *im;
if (width == 0 || height == 0)
return NULL;
im = malloc(sizeof(OILImage));
if (im) {
im->width = width;
im->height = height;
im->locked = 0;
im->data = calloc(width * height, sizeof(OILPixel));
if (im->data == NULL) {
free(im);
return NULL;
}
im->backend = oil_backend;
}
im->backend->new(im);
return im;
}
void oil_image_free(OILImage *im) {
if (im) {
im->backend->free(im);
if (im->data)
free(im->data);
free(im);
}
}
OILImage *oil_image_load(const char *path) {
FILE *fp;
OILImage *ret;
OILFile file;
if (!path)
return NULL;
fp = fopen(path, "rb");
if (!fp)
return NULL;
file.file = fp;
file.read = oil_image_read;
file.write = oil_image_write;
file.flush = oil_image_flush;
ret = oil_image_load_ex(&file);
fclose(fp);
return ret;
}
OILImage *oil_image_load_ex(OILFile *file) {
return oil_formats[0]->load(file);
}
int oil_image_save(OILImage *im, const char *path, OILFormatOptions *opts) {
FILE *fp;
int ret;
OILFile file;
if (!path || !im)
return 0;
fp = fopen(path, "wb");
if (!fp)
return 0;
file.file = fp;
file.read = oil_image_read;
file.write = oil_image_write;
file.flush = oil_image_flush;
ret = oil_image_save_ex(im, &file, opts);
fclose(fp);
return ret;
}
int oil_image_save_ex(OILImage *im, OILFile *file, OILFormatOptions *opts) {
return oil_formats[0]->save(im, file, opts);
}
void oil_image_get_size(OILImage *im, unsigned int *width, unsigned int *height) {
if (im) {
if (width)
*width = im->width;
if (height)
*height = im->height;
} else {
if (width)
*width = 0;
if (height)
*height = 0;
}
}
const OILPixel *oil_image_get_data(OILImage *im) {
if (im) {
im->backend->load(im);
return im->data;
}
return NULL;
}
OILPixel *oil_image_lock(OILImage *im) {
if (im) {
if (im->locked)
return NULL;
im->locked = 1;
im->backend->load(im);
return im->data;
}
return NULL;
}
void oil_image_unlock(OILImage *im) {
if (im) {
im->locked = 0;
im->backend->save(im);
}
}
int oil_image_composite(OILImage *im, OILImage *src, unsigned char alpha, int dx, int dy, unsigned int sx, unsigned int sy, unsigned int xsize, unsigned int ysize) {
/* duh, this is bad */
if (im == NULL || src == NULL)
return 0;
/* firstly, are these images from the same backend? */
if (im->backend != src->backend)
return 0;
/* if alpha == 0, there's nothing to do */
if (alpha == 0)
return 1;
/* if dx, dy are out of bounds, we're already done */
if (dx >= im->width || dy >= im->height)
return 1;
/* same for sx, sy */
if (sx >= src->width || sy >= src->height)
return 1;
/* okay, now set up the 0 size == use as much as possible */
if (xsize == 0)
xsize = src->width - sx;
if (ysize == 0)
ysize = src->height - sy;
/* handle negative dx, dy */
if (dx < 0) {
sx -= dx;
xsize += dx;
dx = 0;
}
if (dy < 0) {
sy -= dy;
ysize += dy;
dy = 0;
}
/* clip the source rect to fit inside dest, if needed */
if (dx + xsize > im->width)
xsize = im->width - dx;
if (dy + ysize > im->height)
ysize = im->height - dy;
/* clip the source rect to fit inside src, if needed */
if (sx + xsize > src->width)
xsize = src->width - sx;
if (sy + ysize > src->height)
ysize = src->height - sy;
/* now, sx, sy, dx, dy, and xsize, ysize are all inside their respective
bounds, and data can be copied freely */
/* return now if there is nothing to copy */
if (xsize == 0 || ysize == 0)
return 1;
/* and finally, call back into the backend if there is work to do */
return im->backend->composite(im, src, alpha, dx, dy, sx, sy, xsize, ysize);
}
void oil_image_draw_triangles(OILImage *im, OILMatrix *matrix, OILImage *tex, OILVertex *vertices, unsigned int vertices_length, unsigned int *indices, unsigned int indices_length, OILTriangleFlags flags) {
/* all of these are unhandleable */
if (!im || !vertices || !matrix || !indices || indices_length % 3 != 0 || vertices_length == 0)
return;
if (tex && (im->backend != tex->backend))
return;
/* this is short-circuit-able */
if (indices_length == 0)
return;
/* ok now that that's out of the way, throw it to the backend */
im->backend->draw_triangles(im, matrix, tex, vertices, vertices_length, indices, indices_length, flags);
}
int oil_image_resize_half(OILImage *im, OILImage *src) {
if (!im || !src)
return 0;
if (im->backend != src->backend)
return 0;
if (im->height * 2 != src->height || im->width * 2 != src->width)
return 0;
return im->backend->resize_half(im, src);
}
void oil_image_clear(OILImage *im) {
if (!im)
return;
im->backend->clear(im);
}