[wip] show_image(): Introduce crop_box to specify visible part of the image
[elinks/images.git] / src / terminal / image.c
blob7aa01cddc4dd43e3ceb2ca2fece0cc2626d39c5c
1 /** Support for image rendering
2 * @file
3 */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
16 #include "elinks.h"
18 #include "cache/cache.h"
19 #include "osdep/image.h"
20 #include "osdep/osdep.h"
21 #include "terminal/terminal.h"
22 #include "terminal/image.h"
23 #include "util/error.h"
24 #include "util/memory.h"
25 #include "util/string.h"
28 void
29 show_image(struct terminal *term, int imgid, struct box *img_box, struct box *crop_box, struct cache_entry *ce)
31 struct string s;
32 int fd;
33 struct fragment *f;
35 #if 0
36 if (!term->imgfiles[imgid]) {
37 /* FIXME: We hardcode /tmp here. */
38 term->imgfiles[imgid] = stracpy("/tmp/elinksimgXXXXXX");
39 fd = mkstemp(term->imgfiles[imgid]);
40 } else {
41 fd = open(term->imgfiles[imgid], O_WRONLY);
43 #else
44 if (term->imgfiles[imgid]) {
45 unlink(term->imgfiles[imgid]);
46 mem_free(term->imgfiles[imgid]);
48 /* FIXME: We hardcode /tmp here. */
49 term->imgfiles[imgid] = stracpy("/tmp/elinksimgXXXXXX");
50 fd = mkstemp(term->imgfiles[imgid]);
51 #endif
52 f = get_cache_fragment(ce);
53 assert(f->offset == 0);
54 safe_write(fd, f->data, f->length);
55 close(fd);
57 if (!init_string(&s))
58 return;
59 add_format_to_string(&s, "%d %d %d %d %d %d %d %d %d %s", imgid,
60 img_box->x, img_box->y, img_box->width, img_box->height,
61 crop_box->x, crop_box->y, crop_box->width, crop_box->height,
62 term->imgfiles[imgid]);
63 do_terminal_function(term, TERM_FN_IMG_DRAW, s.source);
64 done_string(&s);
67 void
68 sync_images(struct terminal *term)
70 do_terminal_function(term, TERM_FN_IMG_SYNC, "");
75 void
76 get_image_size(struct cache_entry *ce, int *w, int *h)
78 int fd;
79 struct fragment *f;
80 unsigned char tmpfile[] = "/tmp/elinksimgXXXXXX";
82 fd = mkstemp(tmpfile);
83 f = get_cache_fragment(ce);
84 assert(f->offset == 0);
85 safe_write(fd, f->data, f->length);
86 close(fd);
88 image_size(tmpfile, w, h);
90 unlink(tmpfile);