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[MAX_ATTR_SIZE], thumb_format[MAX_ATTR_SIZE];
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");
51 for (struct oattr *b = a; b; b = b->same)
53 byte buf[count * MAX_ATTR_SIZE], *b = buf;
54 for (; a; a = a->same)
55 b += base224_decode(b, a->val, strlen(a->val));
57 ioi->thumb_data = mp_alloc(pool, ioi->thumb_size = b - buf);
58 memcpy(ioi->thumb_data, buf, ioi->thumb_size);
59 DBG("Readed thumbnail of size %u", ioi->thumb_size);
64 read_image_obj_thumb(struct image_obj_info *ioi, struct fastbuf *fb, struct image_io *io, struct mempool *pool)
66 struct fastbuf tmp_fb;
68 fbbuf_init_read(fb = &tmp_fb, ioi->thumb_data, ioi->thumb_size, 0);
69 io->format = ioi->thumb_format;
71 if (!image_io_read_header(io))
74 io->flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
75 if (!io->background_color.color_space)
76 io->background_color = color_white;
78 if (!(img = image_io_read_data(io, 1)))
80 DBG("Decompressed thumbnail: size=%ux%u", img->cols, img->rows);
83 DBG("Failed to decompress thumbnail: %s", io->thread->err_msg);
88 put_image_obj_signature(struct odes *o, struct image_signature *sig)
90 /* signatures should be short enough to fit one attribute */
91 byte buf[MAX_ATTR_SIZE];
92 uns size = image_signature_size(sig->len);
93 ASSERT(MAX_ATTR_SIZE > BASE224_ENC_LENGTH(size));
94 buf[base224_encode(buf, (byte *)sig, size)] = 0;
95 obj_set_attr(o, 'H', buf);
99 get_image_obj_signature(struct image_signature *sig, struct odes *o)
101 byte *a = obj_find_aval(o, 'H');
104 UNUSED uns size = base224_decode((byte *)sig, a, strlen(a));
105 ASSERT(size == image_signature_size(sig->len));