author | Tero Marttila <terom@fixme.fi> |
Tue, 26 Jan 2010 21:22:43 +0200 | |
changeset 123 | 81d1cad8b588 |
parent 86 | d4a62899587f |
child 177 | b2768f3982f3 |
permissions | -rw-r--r-- |
18 | 1 |
#include "tile.h" |
2 |
#include "error.h" |
|
3 |
#include "shared/log.h" // only FATAL |
|
4 |
||
5 |
#include <stdlib.h> |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
6 |
#include <assert.h> |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
7 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
8 |
int pt_tile_mem_write (struct pt_tile_mem *buf, void *data, size_t len) |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
9 |
{ |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
10 |
size_t buf_len = buf->len; |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
11 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
12 |
// grow? |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
13 |
while (buf->off + len > buf_len) |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
14 |
buf_len *= 2; |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
15 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
16 |
if (buf_len != buf->len) { |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
17 |
char *tmp; |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
18 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
19 |
if ((tmp = realloc(buf->base, buf_len)) == NULL) |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
20 |
RETURN_ERROR(PT_ERR_MEM); |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
21 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
22 |
buf->base = tmp; |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
23 |
buf->len = buf_len; |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
24 |
} |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
25 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
26 |
// copy |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
27 |
memcpy(buf->base + buf->off, data, len); |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
28 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
29 |
buf->off += len; |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
30 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
31 |
return 0; |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
32 |
} |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
33 |
|
18 | 34 |
|
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
35 |
int pt_tile_new (struct pt_tile **tile_ptr) |
18 | 36 |
{ |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
37 |
struct pt_tile *tile; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
38 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
39 |
if ((tile = calloc(1, sizeof(*tile))) == NULL) |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
40 |
return -PT_ERR_MEM; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
41 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
42 |
*tile_ptr = tile; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
43 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
44 |
return 0; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
45 |
} |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
46 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
47 |
static void pt_tile_init (struct pt_tile *tile, struct pt_cache *cache, const struct pt_tile_info *info, enum pt_tile_output out_type) |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
48 |
{ |
18 | 49 |
// init |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
50 |
tile->cache = cache; |
18 | 51 |
tile->info = *info; |
52 |
tile->out_type = out_type; |
|
53 |
} |
|
54 |
||
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
55 |
int pt_tile_init_file (struct pt_tile *tile, struct pt_cache *cache, const struct pt_tile_info *info, FILE *out) |
18 | 56 |
{ |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
57 |
pt_tile_init(tile, cache, info, PT_TILE_OUT_FILE); |
18 | 58 |
|
59 |
tile->out.file = out; |
|
60 |
||
61 |
return 0; |
|
62 |
} |
|
63 |
||
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
64 |
int pt_tile_init_mem (struct pt_tile *tile, struct pt_cache *cache, const struct pt_tile_info *info) |
18 | 65 |
{ |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
66 |
pt_tile_init(tile, cache, info, PT_TILE_OUT_MEM); |
18 | 67 |
|
68 |
// init buffer |
|
69 |
if ((tile->out.mem.base = malloc(PT_TILE_BUF_SIZE)) == NULL) |
|
70 |
RETURN_ERROR(PT_ERR_MEM); |
|
71 |
||
72 |
tile->out.mem.len = PT_TILE_BUF_SIZE; |
|
73 |
tile->out.mem.off = 0; |
|
74 |
||
75 |
return 0; |
|
76 |
} |
|
77 |
||
78 |
||
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
79 |
int pt_tile_render (struct pt_tile *tile) |
18 | 80 |
{ |
86
d4a62899587f
PT_ERR_TILE_DIM and PT_ERR_TILE_ZOOM
Tero Marttila <terom@fixme.fi>
parents:
56
diff
changeset
|
81 |
// validate dimensions |
d4a62899587f
PT_ERR_TILE_DIM and PT_ERR_TILE_ZOOM
Tero Marttila <terom@fixme.fi>
parents:
56
diff
changeset
|
82 |
if (!tile->info.width || !tile->info.height) |
d4a62899587f
PT_ERR_TILE_DIM and PT_ERR_TILE_ZOOM
Tero Marttila <terom@fixme.fi>
parents:
56
diff
changeset
|
83 |
RETURN_ERROR(PT_ERR_TILE_DIM); |
d4a62899587f
PT_ERR_TILE_DIM and PT_ERR_TILE_ZOOM
Tero Marttila <terom@fixme.fi>
parents:
56
diff
changeset
|
84 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
85 |
return pt_cache_tile(tile->cache, tile); |
18 | 86 |
} |
87 |
||
88 |
void pt_tile_abort (struct pt_tile *tile) |
|
89 |
{ |
|
90 |
// cleanup |
|
91 |
switch (tile->out_type) { |
|
92 |
case PT_TILE_OUT_FILE: |
|
93 |
// no-op |
|
94 |
break; |
|
95 |
||
96 |
case PT_TILE_OUT_MEM: |
|
97 |
// drop buffer |
|
98 |
free(tile->out.mem.base); |
|
99 |
||
100 |
break; |
|
101 |
} |
|
102 |
} |
|
103 |
||
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
104 |
void pt_tile_destroy (struct pt_tile *tile) |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
105 |
{ |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
106 |
pt_tile_abort(tile); |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
107 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
108 |
free(tile); |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
109 |
} |
21 | 110 |