| 1 |
/* |
| 2 |
* jb2save.cpp - saving JB2 raw streams |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "../base/mdjvucfg.h" |
| 6 |
#include <minidjvu/minidjvu.h> |
| 7 |
#include "jb2coder.h" |
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
static void encode_bitmap_with_erosion_support |
| 11 |
(JB2Encoder &jb2, mdjvu_bitmap_t bitmap, mdjvu_bitmap_t proto, int erosion) |
| 12 |
{ |
| 13 |
if (erosion) |
| 14 |
{ |
| 15 |
mdjvu_bitmap_t flip_candidates = mdjvu_get_erosion_mask(bitmap); |
| 16 |
jb2.encode(bitmap, proto, flip_candidates); |
| 17 |
mdjvu_bitmap_destroy(flip_candidates); |
| 18 |
} |
| 19 |
else |
| 20 |
jb2.encode(bitmap, proto); |
| 21 |
} |
| 22 |
|
| 23 |
static int open_bitmap_record(mdjvu_image_t img, int32 index, |
| 24 |
bool with_blit, int32 *table, int32 &library_size, JB2Encoder &jb2, |
| 25 |
mdjvu_error_t *perr, int erosion) |
| 26 |
{ |
| 27 |
mdjvu_image_t dictionary = mdjvu_image_get_dictionary(img); |
| 28 |
int32 d = 0; /* shared dictionary size */ |
| 29 |
if (dictionary) d = mdjvu_image_get_bitmap_count(dictionary); |
| 30 |
|
| 31 |
table[index] = -2; |
| 32 |
mdjvu_bitmap_t bitmap = mdjvu_image_get_bitmap(img, index); |
| 33 |
mdjvu_bitmap_t proto = mdjvu_image_get_prototype(img, bitmap); |
| 34 |
if (proto && mdjvu_image_has_bitmap(img, proto)) |
| 35 |
proto = mdjvu_image_get_substitution(img, proto); |
| 36 |
|
| 37 |
if (!proto) |
| 38 |
{ |
| 39 |
// encode directly |
| 40 |
|
| 41 |
if (with_blit) |
| 42 |
jb2.open_record(jb2_new_symbol_add_to_image_and_library); |
| 43 |
else |
| 44 |
jb2.open_record(jb2_new_symbol_add_to_library_only); |
| 45 |
|
| 46 |
encode_bitmap_with_erosion_support(jb2, bitmap, NULL, erosion); |
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
if (mdjvu_image_has_bitmap(img, proto)) |
| 51 |
{ |
| 52 |
int32 proto_index = mdjvu_bitmap_get_index(proto); |
| 53 |
|
| 54 |
if (table[proto_index] == -2) |
| 55 |
{ |
| 56 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_recursive_prototypes); |
| 57 |
return 0; // prototype recursion error |
| 58 |
} |
| 59 |
|
| 60 |
if (table[proto_index] == -1) |
| 61 |
{ |
| 62 |
// prototype is not yet encoded - encode it |
| 63 |
int32 bl, bt, bw, bh; |
| 64 |
mdjvu_bitmap_get_bounding_box(bitmap, &bl, &bt, &bw, &bh); |
| 65 |
assert(!bl); |
| 66 |
assert(!bt); |
| 67 |
assert(bw == mdjvu_bitmap_get_width(bitmap)); |
| 68 |
assert(bh == mdjvu_bitmap_get_height(bitmap)); |
| 69 |
|
| 70 |
int result = open_bitmap_record |
| 71 |
(img, proto_index, false, table, library_size, jb2, perr, erosion); |
| 72 |
if (!result) return 0; |
| 73 |
jb2.close_record(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (with_blit) |
| 78 |
jb2.open_record(jb2_matched_symbol_with_refinement_add_to_image_and_library); |
| 79 |
else |
| 80 |
jb2.open_record(jb2_matched_symbol_with_refinement_add_to_library_only); |
| 81 |
jb2.matching_symbol_index.set_interval(0, d + library_size - 1); |
| 82 |
if (mdjvu_image_has_bitmap(img, proto)) |
| 83 |
{ |
| 84 |
int32 encoded_index = table[mdjvu_bitmap_get_index(proto)]; |
| 85 |
jb2.zp.encode(d + encoded_index, jb2.matching_symbol_index); |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
assert(mdjvu_image_has_bitmap(dictionary, proto)); |
| 90 |
jb2.zp.encode(mdjvu_image_get_dictionary_index(dictionary, proto), |
| 91 |
jb2.matching_symbol_index); |
| 92 |
} |
| 93 |
|
| 94 |
encode_bitmap_with_erosion_support(jb2, bitmap, proto, erosion); |
| 95 |
} |
| 96 |
table[index] = library_size++; |
| 97 |
return 1; |
| 98 |
} |
| 99 |
|
| 100 |
MDJVU_IMPLEMENT int mdjvu_file_save_jb2_dictionary(mdjvu_image_t image, mdjvu_file_t f, mdjvu_error_t *perr, int erosion) |
| 101 |
{ |
| 102 |
if (!mdjvu_image_has_prototypes(image)) |
| 103 |
mdjvu_find_prototypes(image); |
| 104 |
|
| 105 |
if (perr) *perr = NULL; |
| 106 |
int32 n = mdjvu_image_get_bitmap_count(image); |
| 107 |
JB2Encoder jb2((FILE *) f); |
| 108 |
ZPEncoder &zp = jb2.zp; |
| 109 |
|
| 110 |
/* opening record */ |
| 111 |
jb2.open_record(jb2_start_of_image); |
| 112 |
zp.encode(0, jb2.image_size); |
| 113 |
zp.encode(0, jb2.image_size); |
| 114 |
zp.encode(0, jb2.eventual_image_refinement); /* seems to be unused */ |
| 115 |
jb2.close_record(); |
| 116 |
|
| 117 |
/* Now let's start. */ |
| 118 |
int32 library_size = 0; |
| 119 |
|
| 120 |
// The library table keeps indices of shapes in the encoded library. |
| 121 |
// If the shape wasn't yet encoded, the value is -1. |
| 122 |
// If the shape is to be encoded, the value is -2. |
| 123 |
int32 *library_table = (int32 *) malloc(n * sizeof(int32)); |
| 124 |
int32 i; |
| 125 |
for (i = 0; i < n; i++) library_table[i] = -1; |
| 126 |
|
| 127 |
for (i = 0; i < n; i++) |
| 128 |
{ |
| 129 |
if (library_table[i] == -1) |
| 130 |
{ |
| 131 |
// we have not encoded the bitmap yet |
| 132 |
// encode it! |
| 133 |
if (!open_bitmap_record(image, i, false, library_table, |
| 134 |
library_size, |
| 135 |
jb2, perr, erosion)) |
| 136 |
{ |
| 137 |
free(library_table); |
| 138 |
return 0; |
| 139 |
} |
| 140 |
jb2.close_record(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/* end of data record */ |
| 145 |
jb2.open_record(jb2_end_of_data); |
| 146 |
jb2.close_record(); |
| 147 |
|
| 148 |
mdjvu_image_enable_dictionary_indices(image); |
| 149 |
for (i = 0; i < n; i++) |
| 150 |
{ |
| 151 |
int32 dict_index = library_table[i]; |
| 152 |
assert(dict_index >= 0); |
| 153 |
mdjvu_image_set_dictionary_index(image, |
| 154 |
mdjvu_image_get_bitmap(image, i), |
| 155 |
dict_index); |
| 156 |
} |
| 157 |
free(library_table); |
| 158 |
return 1; |
| 159 |
} |
| 160 |
|
| 161 |
MDJVU_IMPLEMENT int mdjvu_file_save_jb2(mdjvu_image_t image, mdjvu_file_t f, mdjvu_error_t *perr, int erosion) |
| 162 |
{ |
| 163 |
if (!mdjvu_image_has_prototypes(image)) |
| 164 |
mdjvu_find_prototypes(image); |
| 165 |
|
| 166 |
if (perr) *perr = NULL; |
| 167 |
|
| 168 |
mdjvu_image_t dictionary = mdjvu_image_get_dictionary(image); |
| 169 |
|
| 170 |
int32 n = mdjvu_image_get_bitmap_count(image); |
| 171 |
int32 b = mdjvu_image_get_blit_count(image); |
| 172 |
JB2Encoder jb2((FILE *) f); |
| 173 |
ZPEncoder &zp = jb2.zp; |
| 174 |
|
| 175 |
int32 d = 0; |
| 176 |
if (dictionary) |
| 177 |
{ |
| 178 |
d = mdjvu_image_get_bitmap_count(dictionary); |
| 179 |
jb2.open_record(jb2_require_dictionary_or_reset); |
| 180 |
zp.encode(d, jb2.required_dictionary_size); |
| 181 |
jb2.close_record(); |
| 182 |
} |
| 183 |
|
| 184 |
/* opening record */ |
| 185 |
jb2.open_record(jb2_start_of_image); |
| 186 |
zp.encode(mdjvu_image_get_width(image), jb2.image_size); |
| 187 |
zp.encode(mdjvu_image_get_height(image), jb2.image_size); |
| 188 |
zp.encode(0, jb2.eventual_image_refinement); /* seems to be unused */ |
| 189 |
jb2.close_record(); |
| 190 |
|
| 191 |
/* Now let's start. */ |
| 192 |
int32 library_size = 0; |
| 193 |
|
| 194 |
// The library table keeps indices of shapes in the encoded library. |
| 195 |
// If the shape wasn't yet encoded, the value is -1. |
| 196 |
// If the shape is to be encoded, the value is -2. |
| 197 |
int32 *library_table = (int32 *) malloc(n * sizeof(int32)); |
| 198 |
int32 i; |
| 199 |
for (i = 0; i < n; i++) library_table[i] = -1; |
| 200 |
|
| 201 |
/* encode all blits with bitmaps as necessary */ |
| 202 |
for (i = 0; i < b; i++) |
| 203 |
{ |
| 204 |
mdjvu_bitmap_t bitmap = mdjvu_image_get_blit_bitmap(image, i); |
| 205 |
if (!bitmap) continue; |
| 206 |
if (mdjvu_image_has_bitmap(image, bitmap)) |
| 207 |
bitmap = mdjvu_image_get_substitution(image, bitmap); |
| 208 |
assert(bitmap); |
| 209 |
|
| 210 |
if (mdjvu_image_has_bitmap(image, bitmap)) // not the same check as before! |
| 211 |
{ |
| 212 |
int32 bmp_i = mdjvu_bitmap_get_index(bitmap); |
| 213 |
|
| 214 |
if (library_table[bmp_i] == -1) |
| 215 |
{ |
| 216 |
// we have not encoded the bitmap yet |
| 217 |
// encode it! |
| 218 |
if (!open_bitmap_record(image, bmp_i, /* with_blit: */ true, library_table, |
| 219 |
library_size, |
| 220 |
jb2, perr, erosion)) |
| 221 |
{ |
| 222 |
free(library_table); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
// we have our bitmap already |
| 229 |
assert(library_table[bmp_i] >= 0); |
| 230 |
jb2.open_record(jb2_matched_symbol_copy_to_image_without_refinement); |
| 231 |
jb2.matching_symbol_index.set_interval(0, d + library_size - 1); |
| 232 |
|
| 233 |
// encode the index |
| 234 |
jb2.zp.encode(d + library_table[bmp_i], jb2.matching_symbol_index); |
| 235 |
} |
| 236 |
} |
| 237 |
else |
| 238 |
{ |
| 239 |
// the bitmap in question belongs to dictionary |
| 240 |
assert(d > 0); |
| 241 |
jb2.open_record(jb2_matched_symbol_copy_to_image_without_refinement); |
| 242 |
jb2.matching_symbol_index.set_interval(0, d + library_size - 1); |
| 243 |
assert(dictionary); |
| 244 |
assert(mdjvu_image_has_bitmap(dictionary, bitmap)); |
| 245 |
assert(mdjvu_image_has_dictionary_indices(dictionary)); |
| 246 |
jb2.zp.encode(mdjvu_image_get_dictionary_index(dictionary, bitmap), |
| 247 |
jb2.matching_symbol_index); |
| 248 |
} |
| 249 |
|
| 250 |
jb2.encode_blit(image, i, mdjvu_bitmap_get_width(bitmap), |
| 251 |
mdjvu_bitmap_get_height(bitmap)); |
| 252 |
jb2.close_record(); |
| 253 |
} |
| 254 |
|
| 255 |
/* end of data record */ |
| 256 |
jb2.open_record(jb2_end_of_data); |
| 257 |
jb2.close_record(); |
| 258 |
|
| 259 |
free(library_table); |
| 260 |
return 1; |
| 261 |
} |
| 262 |
|
| 263 |
MDJVU_IMPLEMENT int mdjvu_save_jb2(mdjvu_image_t image, const char *path, mdjvu_error_t *perr, int erosion) |
| 264 |
{ |
| 265 |
FILE *f = fopen(path, "wb"); |
| 266 |
if (perr) *perr = NULL; |
| 267 |
if (!f) |
| 268 |
{ |
| 269 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_fopen_write); |
| 270 |
return 0; |
| 271 |
} |
| 272 |
int result = mdjvu_file_save_jb2(image, (mdjvu_file_t) f, perr, erosion); |
| 273 |
fclose(f); |
| 274 |
return result; |
| 275 |
} |
| 276 |
|
| 277 |
/* FIXME: code too close to mdjvu_save_jb2(); maybe, unite them? */ |
| 278 |
MDJVU_IMPLEMENT int mdjvu_save_jb2_dictionary(mdjvu_image_t image, const char *path, mdjvu_error_t *perr, int erosion) |
| 279 |
{ |
| 280 |
FILE *f = fopen(path, "wb"); |
| 281 |
if (perr) *perr = NULL; |
| 282 |
if (!f) |
| 283 |
{ |
| 284 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_fopen_write); |
| 285 |
return 0; |
| 286 |
} |
| 287 |
int result = mdjvu_file_save_jb2_dictionary(image, (mdjvu_file_t) f, perr, erosion); |
| 288 |
fclose(f); |
| 289 |
return result; |
| 290 |
} |
| 291 |
|