]> mj.ucw.cz Git - libucw.git/blob - images/sig-dump.c
5c35eefcb328b17e97cf5567c5fb925ac6295172
[libucw.git] / images / sig-dump.c
1 /*
2  *      Image Library -- Dumping of image signatures
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12 #include "lib/unaligned.h"
13 #include "images/images.h"
14 #include "images/signature.h"
15 #include <stdio.h>
16
17 byte *
18 image_vector_dump(byte *buf, struct image_vector *vec)
19 {
20   byte *p = buf;
21   *p++ = '(';
22   for (uns i = 0; i < IMAGE_VEC_F; i++)
23     {
24       if (i)
25         *p++ = ' ';
26       p += sprintf(p, "%u", vec->f[i]);
27     }
28   *p++ = ')';
29   *p = 0;
30   return buf;
31 }
32
33 byte *
34 image_region_dump(byte *buf, struct image_region *reg)
35 {
36   byte *p = buf;
37   p += sprintf(p, "(txt=");
38   for (uns i = 0; i < IMAGE_REG_F; i++)
39     {
40       if (i)
41         *p++ = ' ';
42       p += sprintf(p, "%u", reg->f[i]);
43     }
44   p += sprintf(p, " shp=");
45   for (uns i = 0; i < IMAGE_REG_H; i++)
46     {
47       if (i)
48         *p++ = ' ';
49       p += sprintf(p, "%u", reg->h[i]);
50     }
51   p += sprintf(p, " wa=%u wb=%u)", reg->wa, reg->wb);
52   *p = 0;
53   return buf;
54 }
55
56 uns
57 get_image_signature(byte *buf, struct image_signature *sig)
58 {
59   uns size = image_signature_size(*buf);
60   memcpy(sig, buf, size);
61 #ifndef CPU_ALLOW_UNALIGNED
62 #define FIX_U16(x) PUT_U16(&(x), x)
63   FIX_U16(sig->dh);
64   struct image_region *reg = sig->reg;
65   for (uns i = 0; i < sig->len; i++, reg++)
66     {
67       for (uns j = 0; j < IMAGE_REG_H; j++)
68         FIX_U16(reg->h[j]);
69       FIX_U16(reg->wa);
70       FIX_U16(reg->wb);
71     }
72 #undef FIX_U16  
73 #endif
74   return size;
75 }
76
77 uns
78 put_image_signature(byte *buf, struct image_signature *sig)
79 {
80   uns size = image_signature_size(sig->len);
81   memcpy(buf, sig, size);
82 #ifndef CPU_ALLOW_UNALIGNED
83 #define FIX_U16(x) do { x = GET_U16(&(x)); } while(0)
84   struct image_signature *tmp = (struct image_signature *)buf;
85   FIX_U16(tmp->dh);
86   struct image_region *reg = tmp->reg;
87   for (uns i = 0; i < tmp->len; i++, reg++)
88     {
89       for (uns j = 0; j < IMAGE_REG_H; j++)
90         FIX_U16(reg->h[j]);
91       FIX_U16(reg->wa);
92       FIX_U16(reg->wb);
93     }
94 #undef FIX_U16
95 #endif
96   return size;
97 }
98 uns
99 bget_image_signature(struct fastbuf *fb, struct image_signature *sig)
100 {
101   uns size = image_signature_size(bpeekc(sig));
102   breadb(fb, sig, size);
103 #ifndef CPU_ALLOW_UNALIGNED
104 #define FIX_U16(x) PUT_U16(&(x), x)
105   FIX_U16(sig->dh);
106   struct image_region *reg = sig->reg;
107   for (uns i = 0; i < sig->len; i++, reg++)
108     {
109       for (uns j = 0; j < IMAGE_REG_H; j++)
110         FIX_U16(reg->h[j]);
111       FIX_U16(reg->wa);
112       FIX_U16(reg->wb);
113     }
114 #undef FIX_U16  
115 #endif
116   return size;
117 }
118
119 uns
120 bput_image_signature(struct fastbuf *fb, struct image_signature *sig)
121 {
122   uns size = image_signature_size(sig->len);
123 #ifdef CPU_ALLOW_UNALIGNED
124   bwrite(fb, sig, size);
125 #else
126   struct image_signature tmp;
127   memcpy(tmp, sig, size);
128 #define FIX_U16(x) do { x = GET_U16(&(x)); } while(0)
129   FIX_U16(tmp.dh);
130   struct image_region *reg = tmp.reg;
131   for (uns i = 0; i < tmp.len; i++, reg++)
132     {
133       for (uns j = 0; j < IMAGE_REG_H; j++)
134         FIX_U16(reg->h[j]);
135       FIX_U16(reg->wa);
136       FIX_U16(reg->wb);
137     }
138   bwrite(fb, &tmp, size);
139 #undef FIX_U16
140 #endif
141   return size;
142 }