]> mj.ucw.cz Git - libucw.git/blob - images/object.c
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / images / object.c
1 /*
2  *      Image Library -- Image cards manipulations
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  */
8
9 #undef LOCAL_DEBUG
10
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"
20 #include <stdio.h>
21 #include <string.h>
22
23 uns
24 get_image_obj_info(struct image_obj_info *ioi, struct odes *o)
25 {
26   byte *v = obj_find_aval(o, 'G');
27   if (!v)
28     {
29       DBG("Missing image info attribute");
30       return 0;
31     }
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);
35   ASSERT(cnt == 7);
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);
38   return 1;
39 }
40
41 uns
42 get_image_obj_thumb(struct image_obj_info *ioi, struct odes *o, struct mempool *pool)
43 {
44   struct oattr *a = obj_find_attr(o, 'N');
45   if (!a)
46     {
47       DBG("Missing image thumbnail attribute");
48       return 0;
49     }
50   uns count = 0;
51   for (struct oattr *b = a; b; b = b->same)
52     count++;
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));
56   ASSERT(b != buf);
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);
60   return 1;
61 }
62
63 struct image *
64 read_image_obj_thumb(struct image_obj_info *ioi, struct fastbuf *fb, struct image_io *io, struct mempool *pool)
65 {
66   struct fastbuf tmp_fb;
67   if (!fb)
68     fbbuf_init_read(fb = &tmp_fb, ioi->thumb_data, ioi->thumb_size, 0);
69   io->format = ioi->thumb_format;
70   io->fastbuf = fb;
71   if (!image_io_read_header(io))
72     goto error;
73   io->pool = pool;
74   io->flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
75   if (!io->background_color.color_space)
76     io->background_color = color_white;
77   struct image *img;
78   if (!(img = image_io_read_data(io, 1)))
79     goto error;
80   DBG("Decompressed thumbnail: size=%ux%u", img->cols, img->rows);
81   return img;
82 error:
83   DBG("Failed to decompress thumbnail: %s", io->thread->err_msg);
84   return NULL;  
85 }
86
87 void
88 put_image_obj_signature(struct odes *o, struct image_signature *sig)
89 {
90   /* signatures should be short enough to in a single 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);
96 }
97
98 uns
99 get_image_obj_signature(struct image_signature *sig, struct odes *o)
100 {
101   byte *a = obj_find_aval(o, 'H');
102   if (!a)
103     return 0;
104   UNUSED uns size = base224_decode((byte *)sig, a, strlen(a));
105   ASSERT(size == image_signature_size(sig->len));
106   return 1;
107 }