2 * Image Library -- Image cards manipulations
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
11 #include "sherlock/sherlock.h"
12 #include "lib/base224.h"
13 #include "lib/mempool.h"
14 #include "lib/fastbuf.h"
15 #include "sherlock/object.h"
16 #include "images/images.h"
17 #include "images/object.h"
18 #include "images/color.h"
19 #include "images/signature.h"
24 get_image_obj_info(struct image_obj_info *ioi, struct odes *o)
26 byte *v = obj_find_aval(o, 'G');
29 DBG("Missing image info attribute");
32 byte color_space[16], thumb_format[16];
33 UNUSED uns cnt = sscanf(v, "%d%d%s%d%d%d%s", &ioi->cols, &ioi->rows, color_space,
34 &ioi->colors, &ioi->thumb_cols, &ioi->thumb_rows, thumb_format);
36 ioi->thumb_format = (*thumb_format == 'p') ? IMAGE_FORMAT_PNG : IMAGE_FORMAT_JPEG;
37 DBG("Readed image info attribute: dim=%ux%u", ioi->cols, ioi->rows);
42 get_image_obj_thumb(struct image_obj_info *ioi, struct odes *o, struct mempool *pool)
44 struct oattr *a = obj_find_attr(o, 'N');
47 DBG("Missing image thumbnail attribute");
52 for (struct oattr *b = a; b; b = b->same)
55 max_len += strlen(b->val);
57 byte buf[max_len + 1], *b = buf;
58 for (; a; a = a->same)
59 b += base224_decode(b, a->val, strlen(a->val));
61 ioi->thumb_data = mp_alloc(pool, ioi->thumb_size = b - buf);
62 memcpy(ioi->thumb_data, buf, ioi->thumb_size);
63 DBG("Readed thumbnail of size %u", ioi->thumb_size);
68 read_image_obj_thumb(struct image_obj_info *ioi, struct fastbuf *fb, struct image_io *io, struct mempool *pool)
70 struct fastbuf tmp_fb;
72 fbbuf_init_read(fb = &tmp_fb, ioi->thumb_data, ioi->thumb_size, 0);
73 io->format = ioi->thumb_format;
75 if (!image_io_read_header(io))
78 io->flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
79 if (!io->background_color.color_space)
80 io->background_color = color_white;
82 if (!(img = image_io_read_data(io, 1)))
84 DBG("Decompressed thumbnail: size=%ux%u", img->cols, img->rows);
87 DBG("Failed to decompress thumbnail: %s", io->thread->err_msg);
92 put_image_obj_signature(struct odes *o, struct image_signature *sig)
94 /* signatures should be short enough to in a single attribute */
95 uns size = image_signature_size(sig->len);
96 byte buf[BASE224_ENC_LENGTH(size) + 1];
97 buf[base224_encode(buf, (byte *)sig, size)] = 0;
98 obj_set_attr(o, 'H', buf);
102 get_image_obj_signature(struct image_signature *sig, struct odes *o)
104 byte *a = obj_find_aval(o, 'H');
107 UNUSED uns size = base224_decode((byte *)sig, a, strlen(a));
108 ASSERT(size == image_signature_size(sig->len));