]> mj.ucw.cz Git - libucw.git/blob - images/object.c
Fastbuf: fbmulti: *'s and whitespace
[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 <ucw/base224.h>
13 #include <ucw/mempool.h>
14 #include <ucw/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[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);
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   uns max_len = 0;
52   for (struct oattr *b = a; b; b = b->same)
53     {
54       count++;
55       max_len += strlen(b->val);
56     }
57   byte buf[max_len + 1], *b = buf;
58   for (; a; a = a->same)
59     b += base224_decode(b, a->val, strlen(a->val));
60   ASSERT(b != buf);
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);
64   return 1;
65 }
66
67 struct image *
68 read_image_obj_thumb(struct image_obj_info *ioi, struct fastbuf *fb, struct image_io *io, struct mempool *pool)
69 {
70   struct fastbuf tmp_fb;
71   if (!fb)
72     fbbuf_init_read(fb = &tmp_fb, ioi->thumb_data, ioi->thumb_size, 0);
73   io->format = ioi->thumb_format;
74   io->fastbuf = fb;
75   if (!image_io_read_header(io))
76     goto error;
77   io->pool = pool;
78   io->flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
79   if (!io->background_color.color_space)
80     io->background_color = color_white;
81   struct image *img;
82   if (!(img = image_io_read_data(io, 1)))
83     goto error;
84   DBG("Decompressed thumbnail: size=%ux%u", img->cols, img->rows);
85   return img;
86 error:
87   DBG("Failed to decompress thumbnail: %s", io->thread->err_msg);
88   return NULL;
89 }
90
91 void
92 put_image_obj_signature(struct odes *o, struct image_signature *sig)
93 {
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);
99 }
100
101 uns
102 get_image_obj_signature(struct image_signature *sig, struct odes *o)
103 {
104   byte *a = obj_find_aval(o, 'H');
105   if (!a)
106     return 0;
107   UNUSED uns size = base224_decode((byte *)sig, a, strlen(a));
108   ASSERT(size == image_signature_size(sig->len));
109   return 1;
110 }