]> mj.ucw.cz Git - libucw.git/blob - images/object.c
sources backup... changed:
[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 #define 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(buf, 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, 0)))
79     goto error;
80   ASSERT(img->cols == ioi->thumb_cols && img->rows == ioi->thumb_rows);
81   DBG("Decompressed thumbnail: size=%ux%u", img->cols, img->rows);
82   return img;
83 error:
84   DBG("Failed to decompress thumbnail: %s", io->thread->err_msg);
85   return NULL;  
86 }
87
88 void
89 put_image_obj_signature(struct odes *o, struct image_signature *sig)
90 {
91   /* signatures should be short enough to fit one attribute */
92   ASSERT(MAX_ATTR_SIZE > BASE224_ENC_LENGTH(sizeof(struct image_vector) + 4 + sig->len * sizeof(struct image_region)));
93   byte buf[MAX_ATTR_SIZE], *b = buf;
94   memcpy(b, &sig->vec, sizeof(struct image_vector));
95   b += sizeof(struct image_vector);
96   *b++ = sig->len;
97   *b++ = sig->df;
98   *(u16 *)b++ = sig->dh;
99   for (uns i = 0; i < sig->len; i++)
100     {
101       memcpy(b, sig->reg + i, sizeof(struct image_region));
102       b += sizeof(struct image_region);
103     }
104   uns len = b - buf;
105   byte b224[MAX_ATTR_SIZE];
106   b224[base224_encode(b224, buf, len)] = 0;
107   obj_set_attr(o, 'H', b224);
108 }