Debian Codegraph @

src/jb2/jb2load.cpp

raw
1 /*
2 * jb2load.cpp - loading JB2 files by interpreting them record-by-record
3 */
4
5 #include "../base/mdjvucfg.h"
6 #include <minidjvu/minidjvu.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "zp.h"
10 #include "jb2coder.h"
11
12 // A piece of my old code... should it be eliminated?
13 template<class T> inline T *
14 append_to_list(T *&list, int32 &count, int32 &allocated)
15 {
16 if (allocated == count)
17 {
18 allocated <<= 1;
19 list = (T *) realloc(list, allocated * sizeof(T));
20 }
21 return &list[count++];
22 }
23
24 static mdjvu_bitmap_t decode_lib_shape/*{{{*/
25 (JB2Decoder &jb2, mdjvu_image_t img, bool with_blit, mdjvu_bitmap_t proto)
26 {
27 int32 blit = -1; // to please compilers
28 int32 index = mdjvu_image_get_bitmap_count(img);
29
30 mdjvu_bitmap_t shape = jb2.decode(img, proto);
31 if (with_blit)
32 {
33 blit = jb2.decode_blit(img, index);
34 }
35
36 int32 x, y;
37 mdjvu_bitmap_remove_margins(shape, &x, &y);
38
39 if (with_blit)
40 {
41 mdjvu_image_set_blit_x(img, blit, mdjvu_image_get_blit_x(img, blit) + x);
42 mdjvu_image_set_blit_y(img, blit, mdjvu_image_get_blit_y(img, blit) + y);
43 }
44
45 return shape;
46 }/*}}}*/
47
48 #define COMPLAIN \
49 { \
50 if (perr) *perr = mdjvu_get_error(mdjvu_error_corrupted_jb2); \
51 return NULL; \
52 }
53 MDJVU_IMPLEMENT mdjvu_image_t mdjvu_file_load_jb2(mdjvu_file_t file, int32 length, mdjvu_error_t *perr)/*{{{*/
54 {
55 if (perr) *perr = NULL;
56 FILE *f = (FILE *) file;
57 JB2Decoder jb2(f, length);
58 ZPDecoder &zp = jb2.zp;
59
60 int32 d = 0;
61 int32 t = jb2.decode_record_type();
62
63 if (t == jb2_require_dictionary_or_reset)
64 {
65 d = zp.decode(jb2.required_dictionary_size);
66 t = jb2.decode_record_type();
67 }
68
69 if (t != jb2_start_of_image) COMPLAIN;
70
71 int32 w = zp.decode(jb2.image_size);
72 int32 h = zp.decode(jb2.image_size);
73 zp.decode(jb2.eventual_image_refinement); // dropped
74 jb2.symbol_column_number.set_interval(1, w);
75 jb2.symbol_row_number.set_interval(1, h);
76
77 mdjvu_image_t img = mdjvu_image_create(w, h); /* d is dropped for now - XXX*/
78
79 int32 lib_count = 0, lib_alloc = 128;
80 mdjvu_bitmap_t *library;
81
82 library = (mdjvu_bitmap_t *) malloc(lib_alloc * sizeof(mdjvu_bitmap_t));
83
84 while(1)
85 {
86 t = jb2.decode_record_type();
87 switch(t)
88 {
89 case jb2_new_symbol_add_to_image_and_library:
90 *(append_to_list<mdjvu_bitmap_t>(library, lib_count, lib_alloc))
91 = decode_lib_shape(jb2, img, true, NULL);
92 break;
93 case jb2_new_symbol_add_to_library_only:
94 *(append_to_list<mdjvu_bitmap_t>(library, lib_count, lib_alloc))
95 = decode_lib_shape(jb2, img, false, NULL);
96 break;
97 case jb2_new_symbol_add_to_image_only:
98 {
99 jb2.decode(img);
100 jb2.decode_blit(img, mdjvu_image_get_bitmap_count(img)-1);
101 }
102 break;
103 case jb2_matched_symbol_with_refinement_add_to_image_and_library:
104 {
105 if (!lib_count)
106 {
107 mdjvu_image_destroy(img);
108 free(library);
109 COMPLAIN;
110 }
111 jb2.matching_symbol_index.set_interval(0, lib_count - 1);
112 int32 match = zp.decode(jb2.matching_symbol_index);
113 *(append_to_list<mdjvu_bitmap_t>(library, lib_count, lib_alloc))
114 = decode_lib_shape(jb2, img, true, library[match]);
115 }
116 break;
117 case jb2_matched_symbol_with_refinement_add_to_library_only:
118 {
119 if (!lib_count)
120 {
121 mdjvu_image_destroy(img);
122 free(library);
123 COMPLAIN;
124 }
125 jb2.matching_symbol_index.set_interval(0, lib_count - 1);
126 int32 match = zp.decode(jb2.matching_symbol_index);
127 *(append_to_list<mdjvu_bitmap_t>(library, lib_count, lib_alloc))
128 = decode_lib_shape(jb2, img, false, library[match]);
129 }
130 break;
131 case jb2_matched_symbol_with_refinement_add_to_image_only:
132 {
133 if (!lib_count)
134 {
135 mdjvu_image_destroy(img);
136 free(library);
137 COMPLAIN;
138 }
139 jb2.matching_symbol_index.set_interval(0, lib_count - 1);
140 int32 match = zp.decode(jb2.matching_symbol_index);
141 jb2.decode(img, library[match]);
142 jb2.decode_blit(img, mdjvu_image_get_bitmap_count(img)-1);
143 }
144 break;
145 case jb2_matched_symbol_copy_to_image_without_refinement:
146 {
147 if (!lib_count)
148 {
149 mdjvu_image_destroy(img);
150 free(library);
151 COMPLAIN;
152 }
153 jb2.matching_symbol_index.set_interval(0, lib_count - 1);
154 int32 match = zp.decode(jb2.matching_symbol_index);
155 jb2.decode_blit(img, match);
156 }
157 break;
158 case jb2_non_symbol_data:
159 {
160 mdjvu_bitmap_t bmp = jb2.decode(img);
161 int32 x = zp.decode(jb2.symbol_column_number) - 1;
162 int32 y = h - zp.decode(jb2.symbol_row_number);
163 mdjvu_image_add_blit(img, x, y, bmp);
164 }
165 break;
166
167 case jb2_require_dictionary_or_reset:
168 jb2.reset();
169 break;
170
171 case jb2_comment:
172 {
173 int32 len = zp.decode(jb2.comment_length);
174 while (len--) zp.decode(jb2.comment_octet);
175 }
176 break;
177
178 case jb2_end_of_data:
179 free(library);
180 return img;
181 default:
182 free(library);
183 mdjvu_image_destroy(img);
184 COMPLAIN;
185 } // switch
186 } // while(1)
187 }/*}}}*/