forked from valderez/Signal-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.cc
More file actions
275 lines (247 loc) · 7.33 KB
/
Copy pathimage.cc
File metadata and controls
275 lines (247 loc) · 7.33 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Generic image output handling. This feature allows
* for extensible image output formats and permits for
* cleaner image rendering code in the model generation
* routines by moving all of the file-format specific
* logic to be handled here.
*/
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <dlfcn.h>
#include "image.hh"
#include "image-ppm.hh"
int get_dt(image_dispatch_table_t *dt, int format);
int load_library(image_dispatch_table_t *dt);
#define DISPATCH_TABLE(ctx) ((image_dispatch_table_t*)(ctx)->_dt)
static int default_format = IMAGE_PPM;
char *dynamic_backend = NULL;
/*
* image_set_format
* Changes the default format for the next
* uninitialized image canvas
*/
int image_set_format(int format){
if(format <= IMAGE_DEFAULT || format >= IMAGE_FORMAT_MAX)
return EINVAL;
default_format = format;
return 0;
}
/*
* image_init
* Initialize an image context. Must be called
* before attempting to write any image data
*/
int image_init(image_ctx_t *ctx, \
const size_t width, \
const size_t height, \
const int model, \
const int format) {
int success = 0;
image_dispatch_table_t *dt;
/* Perform some sanity checking on provided arguments */
if(ctx == NULL)
return EINVAL;
if(width == 0 || height == 0)
return EINVAL;
if(model < 0 || model > IMAGE_MODEL_MAX)
return EINVAL;
if(format >= IMAGE_FORMAT_MAX || (format == IMAGE_LIBRARY && dynamic_backend == NULL))
return EINVAL;
memset(ctx,0x00,sizeof(image_ctx_t));
/* Assign the initialize values to the processing context */
ctx->width = width;
ctx->height = height;
ctx->model = model;
if(format == IMAGE_DEFAULT)
ctx->format = default_format;
else
ctx->format = format;
/* Get the dispatch table for this image format */
dt = (image_dispatch_table_t *) calloc(1,sizeof(image_dispatch_table_t));
if(dt == NULL){
fprintf(stderr,"Error allocating dispatch table\n");
return ENOMEM;
}
success = get_dt(dt,ctx->format);
if(success != 0){
fprintf(stderr,"Error locating dispatch table\n");
free(dt);
return success;
}
ctx->_dt = (void*)dt;
/* Call the format-specific initialization function */
success = dt->init(ctx);
if(success != 0){
fprintf(stderr,"Error initializing image context\n");
free(dt);
return success;
}
ctx->initialized = 1;
return success;
}
/*
* image_add_pixel, image_set_pixel, image_get_pixel, image_write, image_free
* Various setters ad getters for assigning pixel data. image_write
* takes an open file handle and writes image data to it.
* These functions simply wrap the underlying format-specific functions
*/
int image_add_pixel(image_ctx_t *ctx, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){
if(ctx->initialized != 1) return EINVAL;
return DISPATCH_TABLE(ctx)->add_pixel(ctx,r,g,b,a);
}
int image_set_pixel(image_ctx_t *ctx, const size_t x, const size_t y, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){
size_t block_size;
if(ctx->initialized != 1) return EINVAL;
/* Image format handlers have the option to specify a _set_pixel handler */
if(DISPATCH_TABLE(ctx)->set_pixel != NULL){
return DISPATCH_TABLE(ctx)->set_pixel(ctx,x,y,r,g,b,a);
}
block_size = ctx->model == IMAGE_RGB ? RGB_SIZE : RGBA_SIZE;
ctx->next_pixel = ctx->canvas + PIXEL_OFFSET(x,y,ctx->width,block_size);
return DISPATCH_TABLE(ctx)->add_pixel(ctx,r,g,b,a);
}
int image_get_pixel(image_ctx_t *ctx, const size_t x, const size_t y, const uint8_t *r, const uint8_t *g, const uint8_t *b, const uint8_t *a){
if(ctx->initialized != 1) return EINVAL;
if(DISPATCH_TABLE(ctx)->get_pixel != NULL)
return DISPATCH_TABLE(ctx)->get_pixel(ctx,x,y,r,g,b,a);
return ENOSYS;
}
int image_write(image_ctx_t *ctx, FILE *fd){
if(ctx->initialized != 1) return EINVAL;
return DISPATCH_TABLE(ctx)->write(ctx,fd);
}
void image_free(image_ctx_t *ctx){
if(ctx->initialized != 1) return;
if(DISPATCH_TABLE(ctx)->free != NULL){
DISPATCH_TABLE(ctx)->free(ctx);
}
if(ctx->canvas != NULL) free(ctx->canvas);
}
/*
* image_get_filename
* Creates an appropriate file name using data supplied
* by the user. If the extension is already correct, return
* that; if not append if there is space
*/
int image_get_filename(image_ctx_t *ctx, char *out, size_t len_out, char *in){
size_t len_src;
size_t len_ext;
int success = 0;
if(ctx->initialized != 1)
return EINVAL;
/* Get various lengths */
len_src = strlen(in);
len_ext = strlen(ctx->extension);
if(len_src == 0){
in = (char*)"output";
len_src = 6;
}
if(len_src > len_ext && strcmp(in+len_src-len_ext,ctx->extension) == 0){
/* Already has correct extension and fits in buffer */
if(len_src < len_out)
strncpy(in,out,len_out);
else
success = ENOMEM;
}else if(len_src > len_ext){
/* Doesn't have correct extension and fits */
if(len_src + len_ext < len_out){
strncpy(out,in,len_out);
strncat(out,ctx->extension,len_out);
}else
success = ENOMEM;
}else{
/* The input buffer plus an extension cannot fit in the output buffer */
fprintf(stderr,"Error building image output filename\n");
success = ENOMEM;
}
return success;
}
/*
* get_dt
* Load the dispatch table for the specified image
* format. Currently only pixmap supported.
*/
int get_dt(image_dispatch_table_t *dt, int format){
int success = 0;
memset((void*)dt,0x00,sizeof(image_dispatch_table_t));
switch(format){
case IMAGE_PPM:
*dt = ppm_dt;
break;
case IMAGE_LIBRARY:
success = load_library(dt);
break;
default:
success = EINVAL;
}
return success;
}
/*
* ==WARNING==: Here be dragons!
* Experimantal features beyond this point.
* Only use if you are sure you know what you
* are doing!
*/
/*
* image_set_library
* Set the library to use for generating images
*/
int image_set_library(char *library){
char *libname;
size_t length;
length = strlen(library) + 1;
libname = (char*)calloc(length,sizeof(char));
if(libname == NULL)
return ENOMEM;
strncpy(libname,library,length);
dynamic_backend = libname;
default_format = IMAGE_LIBRARY;
return 0;
}
/*
* image_get_library
* Returns the current saved image rendering
* library
*/
char* image_get_library(){
return dynamic_backend;
}
/*
* load_library
* External image processing: experimental feature
* Load an external library to perform image processing
* It must be a custom compatible library
*/
int load_library(image_dispatch_table_t *dt){
void *hndl;
int success = 0;
/* Validate object file */
if(dynamic_backend == NULL || strlen(dynamic_backend) == 0){
fprintf(stderr,"Custom image processor requested without specification\n");
return EINVAL;
}
/* Load shared object and locate required exports */
hndl = dlopen(dynamic_backend,RTLD_LAZY);
if(hndl == NULL){
fprintf(stderr,"Error loading shared object\n");
return EINVAL;
}
/* Perform symbol lookup */
if((dt->init = (_init*)dlsym(hndl,"lib_init")) == NULL ||
(dt->add_pixel = (_add_pixel*)dlsym(hndl,"lib_add_pixel")) == NULL ||
(dt->write = (_write*)dlsym(hndl,"lib_write")) == NULL){
fprintf(stderr,"Invalid image processing module specified\n\t%s",dlerror());
success = EINVAL;
(void) dlclose(hndl);
}
/* Lookup optional symbols, these can return NULL */
if(success == 0){
dt->get_pixel = (_get_pixel*)dlsym(hndl,"lib_get_pixel");
dt->set_pixel = (_set_pixel*)dlsym(hndl,"lib_set_pixel");
dt->free = (_free*)dlsym(hndl,"lib_free");
}
return success;
}