masked images weren't using the image allocation API
[imager.git] / context.c
CommitLineData
74315ca9
TC
1#include "imageri.h"
2
3/*
4=item im_context_new()
5
6Create a new Imager context object.
7
8=cut
9*/
10
11im_context_t
12im_context_new(void) {
cf8c77ae 13 im_context_t ctx = malloc(sizeof(im_context_struct));
74315ca9
TC
14 int i;
15
cf8c77ae
TC
16 if (!ctx)
17 return NULL;
18
74315ca9
TC
19 ctx->error_sp = IM_ERROR_COUNT-1;
20 for (i = 0; i < IM_ERROR_COUNT; ++i) {
21 ctx->error_alloc[i] = 0;
22 ctx->error_stack[i].msg = NULL;
23 ctx->error_stack[i].code = 0;
24 }
25#ifdef IMAGER_LOG
26 ctx->log_level = 0;
27 ctx->lg_file = NULL;
28#endif
44d86483
TC
29 ctx->max_width = 0;
30 ctx->max_height = 0;
cf8c77ae 31 ctx->max_bytes = DEF_BYTES_LIMIT;
74315ca9
TC
32
33 return ctx;
34}
35
36/*
37=item im_context_delete(ctx)
38
39Release memory used by an Imager context object.
40
41=cut
42*/
43
44void
45im_context_delete(im_context_t ctx) {
46 int i;
47
48 for (i = 0; i < IM_ERROR_COUNT; ++i) {
49 if (ctx->error_stack[i].msg)
50 myfree(ctx->error_stack[i].msg);
51 }
52#ifdef IMAGER_LOG
53 if (ctx->lg_file)
54 fclose(ctx->lg_file);
55#endif
cf8c77ae
TC
56
57 free(ctx);
74315ca9
TC
58}
59
60/*
61=item im_context_clone(ctx)
62
63Clone an Imager context object, returning the result.
64
65=cut
66*/
67
68im_context_t
69im_context_clone(im_context_t ctx) {
cf8c77ae 70 im_context_t nctx = malloc(sizeof(im_context_struct));
74315ca9
TC
71 int i;
72
cf8c77ae
TC
73 if (!nctx)
74 return NULL;
75
74315ca9
TC
76 nctx->error_sp = ctx->error_sp;
77 for (i = 0; i < IM_ERROR_COUNT; ++i) {
78 if (ctx->error_stack[i].msg) {
79 size_t sz = ctx->error_alloc[i];
80 nctx->error_alloc[i] = sz;
81 nctx->error_stack[i].msg = mymalloc(sz);
82 memcpy(nctx->error_stack[i].msg, ctx->error_stack[i].msg, sz);
83 }
84 else {
85 nctx->error_alloc[i] = 0;
86 nctx->error_stack[i].msg = NULL;
87 }
88 nctx->error_stack[i].code = ctx->error_stack[i].code;
89 }
90#ifdef IMAGER_LOG
91 nctx->log_level = ctx->log_level;
92 if (ctx->lg_file) {
93 /* disable buffering, this isn't perfect */
94 setvbuf(ctx->lg_file, NULL, _IONBF, 0);
95
96 /* clone that and disable buffering some more */
97 nctx->lg_file = fdopen(fileno(ctx->lg_file), "a");
98 if (nctx->lg_file)
99 setvbuf(nctx->lg_file, NULL, _IONBF, 0);
100 }
101 else {
44d86483 102 nctx->lg_file = NULL;
74315ca9
TC
103 }
104#endif
44d86483
TC
105 nctx->max_width = ctx->max_width;
106 nctx->max_height = ctx->max_height;
107 nctx->max_bytes = ctx->max_bytes;
74315ca9
TC
108
109 return ctx;
110}