| 1 |
/* |
| 2 |
* 5image.c - manipulating split images, the main data structure of minidjvu |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "../base/mdjvucfg.h" |
| 6 |
#include <minidjvu/minidjvu.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <assert.h> |
| 10 |
#include <stdio.h> |
| 11 |
|
| 12 |
#define IMG ((Image *) image) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
/* _______________________________ artifacts ___________________________ */ |
| 17 |
|
| 18 |
/* Artifacts are pieces of information stored for every bitmap. */ |
| 19 |
/* Each artifact type is initially disabled, but may be turned on later. */ |
| 20 |
|
| 21 |
/* When adding new artifact type, see if it needs destructor other than just |
| 22 |
* cleaning the memory. Modify mdjvu_image_disable_artifact() as necessary. |
| 23 |
*/ |
| 24 |
|
| 25 |
typedef enum |
| 26 |
{ |
| 27 |
mdjvu_artifact_prototype, |
| 28 |
mdjvu_artifact_substitution, |
| 29 |
mdjvu_artifact_not_a_letter_flag, |
| 30 |
mdjvu_artifact_suspiciously_big_flag, |
| 31 |
mdjvu_artifact_mass, |
| 32 |
mdjvu_artifact_dictionary_index, |
| 33 |
mdjvu_artifact_center, |
| 34 |
mdjvu_artifacts_count |
| 35 |
} mdjvu_artifact_type_enum; |
| 36 |
|
| 37 |
typedef struct |
| 38 |
{ |
| 39 |
int32 x, y; |
| 40 |
} Point; |
| 41 |
|
| 42 |
#define MDJVU_ARTIFACT_SIZES \ |
| 43 |
{ \ |
| 44 |
sizeof(mdjvu_bitmap_t), \ |
| 45 |
sizeof(mdjvu_bitmap_t), \ |
| 46 |
sizeof(unsigned char), \ |
| 47 |
sizeof(unsigned char), \ |
| 48 |
sizeof(int32), \ |
| 49 |
sizeof(int32), \ |
| 50 |
sizeof(Point) \ |
| 51 |
} |
| 52 |
|
| 53 |
const int32 artifact_sizes[] = MDJVU_ARTIFACT_SIZES; |
| 54 |
|
| 55 |
#define MAX_ARTIFACT_SIZE 16 /* supposing that pointers can't have size > 16 */ |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
typedef struct |
| 60 |
{ |
| 61 |
int32 width, height; |
| 62 |
|
| 63 |
/* bitmaps */ |
| 64 |
mdjvu_bitmap_t *bitmaps; |
| 65 |
int32 bitmaps_count, bitmaps_allocated; |
| 66 |
|
| 67 |
/* blits */ |
| 68 |
int32 *x, *y; |
| 69 |
mdjvu_bitmap_t *blits; |
| 70 |
int32 blits_count, blits_allocated; |
| 71 |
|
| 72 |
/* image additional info */ |
| 73 |
mdjvu_image_t dictionary; |
| 74 |
int32 resolution; /* 0 - unknown */ |
| 75 |
|
| 76 |
void *artifacts[mdjvu_artifacts_count]; |
| 77 |
} Image; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static void initialize_artifact(void **artifacts, mdjvu_bitmap_t bitmap, mdjvu_artifact_type_enum a) |
| 82 |
{ |
| 83 |
int32 i = mdjvu_bitmap_get_index(bitmap); |
| 84 |
switch(a) |
| 85 |
{ |
| 86 |
case mdjvu_artifact_prototype: |
| 87 |
((mdjvu_bitmap_t *) artifacts[mdjvu_artifact_prototype])[i] = NULL; |
| 88 |
break; |
| 89 |
case mdjvu_artifact_substitution: |
| 90 |
((mdjvu_bitmap_t *) artifacts[mdjvu_artifact_substitution])[i] = NULL; |
| 91 |
break; |
| 92 |
case mdjvu_artifact_mass: |
| 93 |
((int32 *) artifacts[mdjvu_artifact_mass])[i] = |
| 94 |
mdjvu_bitmap_get_mass(bitmap); |
| 95 |
break; |
| 96 |
case mdjvu_artifact_dictionary_index: |
| 97 |
((int32 *) artifacts[mdjvu_artifact_dictionary_index])[i] = -1; |
| 98 |
break; |
| 99 |
case mdjvu_artifact_not_a_letter_flag: |
| 100 |
((unsigned char *) artifacts[mdjvu_artifact_not_a_letter_flag])[i] = 0; |
| 101 |
break; |
| 102 |
case mdjvu_artifact_suspiciously_big_flag: |
| 103 |
((unsigned char *) artifacts[mdjvu_artifact_suspiciously_big_flag])[i] = 0; |
| 104 |
break; |
| 105 |
case mdjvu_artifact_center: /* initializing centers may be non-obvious */ |
| 106 |
case mdjvu_artifacts_count:; /* just to complete switch */ |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
static void initialize_artifacts(void **artifacts, mdjvu_bitmap_t bitmap) |
| 111 |
{ |
| 112 |
int a; |
| 113 |
for (a = 0; a < mdjvu_artifacts_count; a++) |
| 114 |
{ |
| 115 |
if (artifacts[a]) |
| 116 |
initialize_artifact(artifacts, bitmap, |
| 117 |
(mdjvu_artifact_type_enum) a); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
static void mdjvu_image_enable_artifact |
| 122 |
(mdjvu_image_t image, mdjvu_artifact_type_enum artifact_index) |
| 123 |
{ |
| 124 |
int32 i; |
| 125 |
int32 n = IMG->bitmaps_count; |
| 126 |
if (!IMG->artifacts[artifact_index]) |
| 127 |
{ |
| 128 |
IMG->artifacts[artifact_index] = |
| 129 |
malloc(IMG->bitmaps_allocated * artifact_sizes[artifact_index]); |
| 130 |
} |
| 131 |
for (i = 0; i < n; i++) |
| 132 |
{ |
| 133 |
initialize_artifact(IMG->artifacts, IMG->bitmaps[i], artifact_index); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static void mdjvu_image_disable_artifact |
| 138 |
(mdjvu_image_t image, mdjvu_artifact_type_enum artifact_index) |
| 139 |
{ |
| 140 |
if (IMG->artifacts[artifact_index]) |
| 141 |
{ |
| 142 |
free(IMG->artifacts[artifact_index]); |
| 143 |
IMG->artifacts[artifact_index] = NULL; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/* ______________________________ create ________________________________ */ |
| 149 |
|
| 150 |
MDJVU_IMPLEMENT mdjvu_image_t mdjvu_image_create(int32 width, int32 height) |
| 151 |
{ |
| 152 |
int i; |
| 153 |
Image *image = (Image *) malloc(sizeof(Image)); |
| 154 |
|
| 155 |
mdjvu_init(); |
| 156 |
|
| 157 |
image->width = width; |
| 158 |
image->height = height; |
| 159 |
|
| 160 |
image->bitmaps_allocated = 16; |
| 161 |
image->bitmaps = (mdjvu_bitmap_t *) |
| 162 |
malloc(image->bitmaps_allocated * sizeof(mdjvu_bitmap_t)); |
| 163 |
image->bitmaps_count = 0; |
| 164 |
|
| 165 |
image->blits_allocated = 32; |
| 166 |
image->x = (int32 *) malloc(image->blits_allocated * sizeof(int32)); |
| 167 |
image->y = (int32 *) malloc(image->blits_allocated * sizeof(int32)); |
| 168 |
image->blits = (mdjvu_bitmap_t *) |
| 169 |
malloc(image->blits_allocated * sizeof(mdjvu_bitmap_t)); |
| 170 |
image->blits_count = 0; |
| 171 |
|
| 172 |
image->dictionary = NULL; |
| 173 |
image->resolution = 0; |
| 174 |
|
| 175 |
for (i = 0; i < mdjvu_artifacts_count; i++) |
| 176 |
image->artifacts[i] = NULL; |
| 177 |
|
| 178 |
return (mdjvu_image_t) image; |
| 179 |
} |
| 180 |
|
| 181 |
/* ______________________________ destroy _______________________________ */ |
| 182 |
|
| 183 |
MDJVU_IMPLEMENT void mdjvu_image_destroy(mdjvu_image_t image) |
| 184 |
{ |
| 185 |
int32 i; |
| 186 |
int k; |
| 187 |
free(IMG->blits); |
| 188 |
free(IMG->x); |
| 189 |
free(IMG->y); |
| 190 |
for (k = 0; k < mdjvu_artifacts_count; k++) |
| 191 |
{ |
| 192 |
mdjvu_image_disable_artifact(image, (mdjvu_artifact_type_enum) k); |
| 193 |
} |
| 194 |
for (i = 0; i < IMG->bitmaps_count; i++) |
| 195 |
mdjvu_bitmap_destroy(IMG->bitmaps[i]); |
| 196 |
free(IMG->bitmaps); |
| 197 |
free(IMG); |
| 198 |
} |
| 199 |
|
| 200 |
/* ______________________________ get/set ______________________________ */ |
| 201 |
|
| 202 |
MDJVU_IMPLEMENT int32 mdjvu_image_get_width(mdjvu_image_t image) |
| 203 |
{ return IMG->width; } |
| 204 |
|
| 205 |
MDJVU_IMPLEMENT int32 mdjvu_image_get_height(mdjvu_image_t image) |
| 206 |
{ return IMG->height; } |
| 207 |
|
| 208 |
MDJVU_IMPLEMENT int32 mdjvu_image_get_bitmap_count(mdjvu_image_t image) |
| 209 |
{ return IMG->bitmaps_count; } |
| 210 |
|
| 211 |
MDJVU_IMPLEMENT int32 mdjvu_image_get_blit_count(mdjvu_image_t image) |
| 212 |
{ return IMG->blits_count; } |
| 213 |
|
| 214 |
MDJVU_IMPLEMENT void mdjvu_image_set_dictionary(mdjvu_image_t image, mdjvu_image_t dict) |
| 215 |
{ |
| 216 |
IMG->dictionary = dict; |
| 217 |
} |
| 218 |
|
| 219 |
MDJVU_IMPLEMENT mdjvu_image_t mdjvu_image_get_dictionary(mdjvu_image_t image) |
| 220 |
{ |
| 221 |
return IMG->dictionary; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
/* ______________________________ bitmaps ______________________________ */ |
| 226 |
|
| 227 |
MDJVU_IMPLEMENT int32 mdjvu_image_add_bitmap(mdjvu_image_t image, mdjvu_bitmap_t bmp) |
| 228 |
{ |
| 229 |
if (IMG->bitmaps_count == IMG->bitmaps_allocated) |
| 230 |
{ |
| 231 |
int i; |
| 232 |
IMG->bitmaps_allocated <<= 1; |
| 233 |
IMG->bitmaps = (mdjvu_bitmap_t *) realloc(IMG->bitmaps, |
| 234 |
IMG->bitmaps_allocated * sizeof(mdjvu_bitmap_t)); |
| 235 |
for (i = 0; i < mdjvu_artifacts_count; i++) |
| 236 |
{ |
| 237 |
if (IMG->artifacts[i]) |
| 238 |
{ |
| 239 |
IMG->artifacts[i] = realloc(IMG->artifacts[i], |
| 240 |
IMG->bitmaps_allocated * artifact_sizes[i]); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
IMG->bitmaps[IMG->bitmaps_count] = bmp; |
| 245 |
mdjvu_bitmap_set_index(bmp, IMG->bitmaps_count); |
| 246 |
initialize_artifacts(IMG->artifacts, bmp); |
| 247 |
return IMG->bitmaps_count++; |
| 248 |
} |
| 249 |
|
| 250 |
MDJVU_IMPLEMENT int mdjvu_image_has_bitmap(mdjvu_image_t image, mdjvu_bitmap_t bitmap) |
| 251 |
{ |
| 252 |
int32 i = mdjvu_bitmap_get_index(bitmap); |
| 253 |
if (i >= IMG->bitmaps_count) return 0; |
| 254 |
return bitmap == IMG->bitmaps[i]; |
| 255 |
} |
| 256 |
|
| 257 |
MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_image_get_bitmap(mdjvu_image_t image, int32 i) |
| 258 |
{ |
| 259 |
return IMG->bitmaps[i]; |
| 260 |
} |
| 261 |
|
| 262 |
MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_image_new_bitmap(mdjvu_image_t image, int32 w, int32 h) |
| 263 |
{ |
| 264 |
mdjvu_bitmap_t bmp = mdjvu_bitmap_create(w, h); |
| 265 |
mdjvu_image_add_bitmap(image, bmp); |
| 266 |
return bmp; |
| 267 |
} |
| 268 |
|
| 269 |
MDJVU_IMPLEMENT void mdjvu_image_exchange_bitmaps |
| 270 |
(mdjvu_image_t image, int32 i1, int32 i2) |
| 271 |
{ |
| 272 |
int a; |
| 273 |
mdjvu_bitmap_t b1 = IMG->bitmaps[i1], b2 = IMG->bitmaps[i2]; |
| 274 |
if (i1 == i2) return; |
| 275 |
|
| 276 |
IMG->bitmaps[i1] = b2; |
| 277 |
IMG->bitmaps[i2] = b1; |
| 278 |
mdjvu_bitmap_set_index(b1, i2); |
| 279 |
mdjvu_bitmap_set_index(b2, i1); |
| 280 |
|
| 281 |
for (a = 0; a < mdjvu_artifacts_count; a++) |
| 282 |
{ |
| 283 |
if (IMG->artifacts[a]) |
| 284 |
{ |
| 285 |
char buf[MAX_ARTIFACT_SIZE]; |
| 286 |
memcpy(buf, |
| 287 |
((char *) IMG->artifacts[a]) + i1 * artifact_sizes[a], |
| 288 |
artifact_sizes[a]); |
| 289 |
memcpy(((char *) IMG->artifacts[a]) + i1 * artifact_sizes[a], |
| 290 |
((char *) IMG->artifacts[a]) + i2 * artifact_sizes[a], |
| 291 |
artifact_sizes[a]); |
| 292 |
memcpy(((char *) IMG->artifacts[a]) + i2 * artifact_sizes[a], |
| 293 |
buf, |
| 294 |
artifact_sizes[a]); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
MDJVU_IMPLEMENT int mdjvu_image_check_indices(mdjvu_image_t image) |
| 300 |
{ |
| 301 |
int32 n = IMG->bitmaps_count; |
| 302 |
int32 i; |
| 303 |
for (i = 0; i < n; i++) |
| 304 |
{ |
| 305 |
if (mdjvu_bitmap_get_index(IMG->bitmaps[i]) != i) |
| 306 |
return 0; |
| 307 |
} |
| 308 |
return 1; |
| 309 |
} |
| 310 |
|
| 311 |
MDJVU_IMPLEMENT void mdjvu_image_remove_unused_bitmaps(mdjvu_image_t image) |
| 312 |
{ |
| 313 |
int32 b = IMG->blits_count; |
| 314 |
int32 n = IMG->bitmaps_count; |
| 315 |
int32 i, filled; |
| 316 |
int32 *use_count = (int32 *) calloc(n, sizeof(int32)); |
| 317 |
mdjvu_bitmap_t *new_bitmaps; |
| 318 |
int32 new_bitmaps_count, a; |
| 319 |
void *new_artifacts[mdjvu_artifacts_count]; |
| 320 |
|
| 321 |
for (i = 0; i < b; i++) |
| 322 |
{ |
| 323 |
mdjvu_bitmap_t bmp = IMG->blits[i]; |
| 324 |
if (mdjvu_image_has_bitmap(image, bmp)) |
| 325 |
use_count[mdjvu_bitmap_get_index(bmp)]++; |
| 326 |
} |
| 327 |
|
| 328 |
new_bitmaps_count = 0; |
| 329 |
for (i = 0; i < n; i++) |
| 330 |
{ |
| 331 |
if (use_count[i]) |
| 332 |
new_bitmaps_count++; |
| 333 |
} |
| 334 |
|
| 335 |
/* create new bitmap and artifact placeholders */ |
| 336 |
new_bitmaps = (mdjvu_bitmap_t *) |
| 337 |
malloc(new_bitmaps_count * sizeof(mdjvu_bitmap_t)); |
| 338 |
for (a = 0; a < mdjvu_artifacts_count; a++) |
| 339 |
{ |
| 340 |
if (IMG->artifacts[a]) |
| 341 |
new_artifacts[a] = malloc(new_bitmaps_count * artifact_sizes[a]); |
| 342 |
else |
| 343 |
new_artifacts[a] = NULL; |
| 344 |
} |
| 345 |
|
| 346 |
filled = 0; |
| 347 |
for (i = 0; i < n; i++) |
| 348 |
{ |
| 349 |
if (use_count[i]) |
| 350 |
{ |
| 351 |
mdjvu_bitmap_set_index(IMG->bitmaps[i], filled); |
| 352 |
new_bitmaps[filled] = IMG->bitmaps[i]; |
| 353 |
for (a = 0; a < mdjvu_artifacts_count; a++) |
| 354 |
{ |
| 355 |
if (IMG->artifacts[a]) |
| 356 |
{ |
| 357 |
memcpy((char *) new_artifacts[a] + filled * artifact_sizes[a], |
| 358 |
(char *) IMG->artifacts[a] + i * artifact_sizes[a], |
| 359 |
artifact_sizes[a]); |
| 360 |
} |
| 361 |
} |
| 362 |
filled++; |
| 363 |
} |
| 364 |
else |
| 365 |
mdjvu_bitmap_destroy(IMG->bitmaps[i]); |
| 366 |
} |
| 367 |
|
| 368 |
free(use_count); |
| 369 |
free(IMG->bitmaps); |
| 370 |
IMG->bitmaps = new_bitmaps; |
| 371 |
IMG->bitmaps_count = IMG->bitmaps_allocated = new_bitmaps_count; |
| 372 |
|
| 373 |
for (a = 0; a < mdjvu_artifacts_count; a++) |
| 374 |
{ |
| 375 |
if (IMG->artifacts[a]) |
| 376 |
{ |
| 377 |
free(IMG->artifacts[a]); |
| 378 |
IMG->artifacts[a] = new_artifacts[a]; |
| 379 |
/* another place to delete artifacts that need deleting... */ |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
/* _______________________________ blits _______________________________ */ |
| 386 |
|
| 387 |
MDJVU_IMPLEMENT int32 mdjvu_image_add_blit(mdjvu_image_t image, |
| 388 |
int32 x, int32 y, |
| 389 |
mdjvu_bitmap_t bitmap) |
| 390 |
{ |
| 391 |
if (IMG->blits_count == IMG->blits_allocated) |
| 392 |
{ |
| 393 |
IMG->blits_allocated <<= 1; |
| 394 |
IMG->x = (int32 *) realloc(IMG->x, |
| 395 |
IMG->blits_allocated * sizeof(int32)); |
| 396 |
IMG->y = (int32 *) realloc(IMG->y, |
| 397 |
IMG->blits_allocated * sizeof(int32)); |
| 398 |
IMG->blits = (mdjvu_bitmap_t *) realloc(IMG->blits, |
| 399 |
IMG->blits_allocated * sizeof(mdjvu_bitmap_t)); |
| 400 |
} |
| 401 |
IMG->x[IMG->blits_count] = x; |
| 402 |
IMG->y[IMG->blits_count] = y; |
| 403 |
IMG->blits[IMG->blits_count] = bitmap; |
| 404 |
return IMG->blits_count++; |
| 405 |
} |
| 406 |
|
| 407 |
MDJVU_IMPLEMENT void mdjvu_image_remove_NULL_blits(mdjvu_image_t image) |
| 408 |
{ |
| 409 |
int32 *new_x = (int32 *) malloc(IMG->blits_count * sizeof(int32)); |
| 410 |
int32 *new_y = (int32 *) malloc(IMG->blits_count * sizeof(int32)); |
| 411 |
mdjvu_bitmap_t *new_blits = (mdjvu_bitmap_t *) |
| 412 |
malloc(IMG->blits_count * sizeof(mdjvu_bitmap_t)); |
| 413 |
int32 filled = 0, i; |
| 414 |
|
| 415 |
for (i = 0; i < IMG->blits_count; i++) |
| 416 |
{ |
| 417 |
if (IMG->blits[i]) |
| 418 |
{ |
| 419 |
new_x[filled] = IMG->x[i]; |
| 420 |
new_y[filled] = IMG->y[i]; |
| 421 |
new_blits[filled] = IMG->blits[i]; |
| 422 |
filled++; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
free(IMG->x); |
| 427 |
free(IMG->y); |
| 428 |
free(IMG->blits); |
| 429 |
IMG->x = new_x; |
| 430 |
IMG->y = new_y; |
| 431 |
IMG->blits = new_blits; |
| 432 |
IMG->blits_allocated = IMG->blits_count; |
| 433 |
IMG->blits_count = filled; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
MDJVU_IMPLEMENT int32 mdjvu_image_get_blit_x(mdjvu_image_t image, int32 i) |
| 438 |
{ |
| 439 |
assert(i >= 0 && i < IMG->blits_count); |
| 440 |
return IMG->x[i]; |
| 441 |
} |
| 442 |
|
| 443 |
MDJVU_IMPLEMENT int32 mdjvu_image_get_blit_y(mdjvu_image_t image, int32 i) |
| 444 |
{ |
| 445 |
assert(i >= 0 && i < IMG->blits_count); |
| 446 |
return IMG->y[i]; |
| 447 |
} |
| 448 |
|
| 449 |
MDJVU_IMPLEMENT void mdjvu_image_set_blit_x(mdjvu_image_t image, int32 i, int32 x) |
| 450 |
{ |
| 451 |
assert(i >= 0 && i < IMG->blits_count); |
| 452 |
IMG->x[i] = x; |
| 453 |
} |
| 454 |
|
| 455 |
MDJVU_IMPLEMENT void mdjvu_image_set_blit_y(mdjvu_image_t image, int32 i, int32 y) |
| 456 |
{ |
| 457 |
assert(i >= 0 && i < IMG->blits_count); |
| 458 |
IMG->y[i] = y; |
| 459 |
} |
| 460 |
|
| 461 |
MDJVU_IMPLEMENT mdjvu_bitmap_t |
| 462 |
mdjvu_image_get_blit_bitmap(mdjvu_image_t image, int32 i) |
| 463 |
{ |
| 464 |
assert(i >= 0 && i < IMG->blits_count); |
| 465 |
return IMG->blits[i]; |
| 466 |
} |
| 467 |
|
| 468 |
MDJVU_IMPLEMENT void mdjvu_image_set_blit_bitmap(mdjvu_image_t image, int32 i, mdjvu_bitmap_t b) |
| 469 |
{ |
| 470 |
assert(i >= 0 && i < IMG->blits_count); |
| 471 |
IMG->blits[i] = b; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
MDJVU_IMPLEMENT void mdjvu_image_exchange_blits(mdjvu_image_t image, int32 i, int32 j) |
| 476 |
{ |
| 477 |
int32 t; mdjvu_bitmap_t b; |
| 478 |
if (i == j) return; |
| 479 |
t = IMG->x[i]; IMG->x[i] = IMG->x[j]; IMG->x[j] = t; |
| 480 |
t = IMG->y[i]; IMG->y[i] = IMG->y[j]; IMG->y[j] = t; |
| 481 |
b = IMG->blits[i]; IMG->blits[i] = IMG->blits[j]; IMG->blits[j] = b; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
/* _____________________________ image info ____________________________ */ |
| 486 |
|
| 487 |
MDJVU_IMPLEMENT int32 mdjvu_image_get_resolution(mdjvu_image_t image) |
| 488 |
{ |
| 489 |
return IMG->resolution; |
| 490 |
} |
| 491 |
|
| 492 |
MDJVU_IMPLEMENT void mdjvu_image_set_resolution(mdjvu_image_t image, int32 dpi) |
| 493 |
{ |
| 494 |
IMG->resolution = dpi; |
| 495 |
} |
| 496 |
|
| 497 |
/* _____________________________ artifacts _____________________________ */ |
| 498 |
|
| 499 |
|
| 500 |
MDJVU_IMPLEMENT void mdjvu_image_enable_prototypes(mdjvu_image_t image) |
| 501 |
{ mdjvu_image_enable_artifact(image, mdjvu_artifact_prototype); } |
| 502 |
|
| 503 |
MDJVU_IMPLEMENT void mdjvu_image_enable_substitutions(mdjvu_image_t image) |
| 504 |
{ mdjvu_image_enable_artifact(image, mdjvu_artifact_substitution); } |
| 505 |
|
| 506 |
MDJVU_IMPLEMENT void mdjvu_image_enable_masses(mdjvu_image_t image) |
| 507 |
{ mdjvu_image_enable_artifact(image, mdjvu_artifact_mass); } |
| 508 |
|
| 509 |
MDJVU_IMPLEMENT void mdjvu_image_enable_dictionary_indices(mdjvu_image_t image) |
| 510 |
{ mdjvu_image_enable_artifact(image, mdjvu_artifact_dictionary_index); } |
| 511 |
|
| 512 |
MDJVU_IMPLEMENT void mdjvu_image_enable_suspiciously_big_flags(mdjvu_image_t image) |
| 513 |
{ mdjvu_image_enable_artifact(image, mdjvu_artifact_suspiciously_big_flag); } |
| 514 |
|
| 515 |
MDJVU_IMPLEMENT void mdjvu_image_enable_not_a_letter_flags(mdjvu_image_t image) |
| 516 |
{ mdjvu_image_enable_artifact(image, mdjvu_artifact_not_a_letter_flag); } |
| 517 |
|
| 518 |
MDJVU_IMPLEMENT void mdjvu_image_enable_centers(mdjvu_image_t image) |
| 519 |
{ mdjvu_image_enable_artifact(image, mdjvu_artifact_center); } |
| 520 |
|
| 521 |
|
| 522 |
MDJVU_IMPLEMENT void mdjvu_image_disable_prototypes(mdjvu_image_t image) |
| 523 |
{ mdjvu_image_disable_artifact(image, mdjvu_artifact_prototype); } |
| 524 |
|
| 525 |
MDJVU_IMPLEMENT void mdjvu_image_disable_substitutions(mdjvu_image_t image) |
| 526 |
{ mdjvu_image_disable_artifact(image, mdjvu_artifact_substitution); } |
| 527 |
|
| 528 |
MDJVU_IMPLEMENT void mdjvu_image_disable_masses(mdjvu_image_t image) |
| 529 |
{ mdjvu_image_disable_artifact(image, mdjvu_artifact_mass); } |
| 530 |
|
| 531 |
MDJVU_IMPLEMENT void mdjvu_image_disable_dictionary_indices(mdjvu_image_t image) |
| 532 |
{ mdjvu_image_disable_artifact(image, mdjvu_artifact_dictionary_index); } |
| 533 |
|
| 534 |
MDJVU_IMPLEMENT void mdjvu_image_disable_suspiciously_big_flags(mdjvu_image_t image) |
| 535 |
{ mdjvu_image_disable_artifact(image, mdjvu_artifact_suspiciously_big_flag); } |
| 536 |
|
| 537 |
MDJVU_IMPLEMENT void mdjvu_image_disable_not_a_letter_flags(mdjvu_image_t image) |
| 538 |
{ mdjvu_image_disable_artifact(image, mdjvu_artifact_not_a_letter_flag); } |
| 539 |
|
| 540 |
MDJVU_IMPLEMENT void mdjvu_image_disable_centers(mdjvu_image_t image) |
| 541 |
{ mdjvu_image_disable_artifact(image, mdjvu_artifact_center); } |
| 542 |
|
| 543 |
|
| 544 |
MDJVU_IMPLEMENT int mdjvu_image_has_prototypes(mdjvu_image_t image) |
| 545 |
{ return IMG->artifacts[mdjvu_artifact_prototype] != NULL; } |
| 546 |
|
| 547 |
MDJVU_IMPLEMENT int mdjvu_image_has_substitutions(mdjvu_image_t image) |
| 548 |
{ return IMG->artifacts[mdjvu_artifact_substitution] != NULL; } |
| 549 |
|
| 550 |
MDJVU_IMPLEMENT int mdjvu_image_has_masses(mdjvu_image_t image) |
| 551 |
{ return IMG->artifacts[mdjvu_artifact_mass] != NULL; } |
| 552 |
|
| 553 |
MDJVU_IMPLEMENT int mdjvu_image_has_dictionary_indices(mdjvu_image_t image) |
| 554 |
{ return IMG->artifacts[mdjvu_artifact_dictionary_index] != NULL; } |
| 555 |
|
| 556 |
MDJVU_IMPLEMENT int mdjvu_image_has_suspiciously_big_flags(mdjvu_image_t image) |
| 557 |
{ return IMG->artifacts[mdjvu_artifact_suspiciously_big_flag] != NULL; } |
| 558 |
|
| 559 |
MDJVU_IMPLEMENT int mdjvu_image_has_not_a_letter_flags(mdjvu_image_t image) |
| 560 |
{ return IMG->artifacts[mdjvu_artifact_not_a_letter_flag] != NULL; } |
| 561 |
|
| 562 |
MDJVU_IMPLEMENT int mdjvu_image_has_centers(mdjvu_image_t image) |
| 563 |
{ return IMG->artifacts[mdjvu_artifact_center] != NULL; } |
| 564 |
|
| 565 |
|
| 566 |
MDJVU_IMPLEMENT int mdjvu_image_get_not_a_letter_flag(mdjvu_image_t image, mdjvu_bitmap_t b) |
| 567 |
{ |
| 568 |
return ((unsigned char *) IMG->artifacts[mdjvu_artifact_not_a_letter_flag]) |
| 569 |
[mdjvu_bitmap_get_index(b)]; |
| 570 |
} |
| 571 |
|
| 572 |
MDJVU_IMPLEMENT void mdjvu_image_set_not_a_letter_flag(mdjvu_image_t image, mdjvu_bitmap_t b, int v) |
| 573 |
{ |
| 574 |
((unsigned char *) IMG->artifacts[mdjvu_artifact_not_a_letter_flag]) |
| 575 |
[mdjvu_bitmap_get_index(b)] = v ? 1 : 0; /* not to fail on 256... */ |
| 576 |
} |
| 577 |
|
| 578 |
MDJVU_IMPLEMENT int mdjvu_image_get_suspiciously_big_flag(mdjvu_image_t image, mdjvu_bitmap_t b) |
| 579 |
{ |
| 580 |
return ((unsigned char *) IMG->artifacts[mdjvu_artifact_suspiciously_big_flag]) |
| 581 |
[mdjvu_bitmap_get_index(b)]; |
| 582 |
} |
| 583 |
|
| 584 |
MDJVU_IMPLEMENT void mdjvu_image_set_suspiciously_big_flag(mdjvu_image_t image, mdjvu_bitmap_t b, int v) |
| 585 |
{ |
| 586 |
((unsigned char *) IMG->artifacts[mdjvu_artifact_suspiciously_big_flag]) |
| 587 |
[mdjvu_bitmap_get_index(b)] = v ? 1 : 0; /* not to fail on 256... */ |
| 588 |
} |
| 589 |
|
| 590 |
MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_image_get_prototype(mdjvu_image_t image, mdjvu_bitmap_t b) |
| 591 |
{ |
| 592 |
return ((mdjvu_bitmap_t *) IMG->artifacts[mdjvu_artifact_prototype]) |
| 593 |
[mdjvu_bitmap_get_index(b)]; |
| 594 |
} |
| 595 |
|
| 596 |
MDJVU_IMPLEMENT void mdjvu_image_set_prototype(mdjvu_image_t image, mdjvu_bitmap_t b, mdjvu_bitmap_t prototype) |
| 597 |
{ |
| 598 |
((mdjvu_bitmap_t *) IMG->artifacts[mdjvu_artifact_prototype]) |
| 599 |
[mdjvu_bitmap_get_index(b)] = prototype; |
| 600 |
} |
| 601 |
|
| 602 |
MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_image_get_substitution(mdjvu_image_t image, mdjvu_bitmap_t b) |
| 603 |
{ |
| 604 |
mdjvu_bitmap_t subst; |
| 605 |
if (!IMG->artifacts[mdjvu_artifact_substitution]) return b; |
| 606 |
if (!b) return b; /* special handy case */ |
| 607 |
subst = |
| 608 |
((mdjvu_bitmap_t *) IMG->artifacts[mdjvu_artifact_substitution]) |
| 609 |
[mdjvu_bitmap_get_index(b)]; |
| 610 |
return subst ? subst : b; |
| 611 |
} |
| 612 |
|
| 613 |
MDJVU_IMPLEMENT int32 mdjvu_image_get_mass(mdjvu_image_t image, mdjvu_bitmap_t b) |
| 614 |
{ |
| 615 |
return ((int32 *) IMG->artifacts[mdjvu_artifact_mass])[mdjvu_bitmap_get_index(b)]; |
| 616 |
} |
| 617 |
|
| 618 |
MDJVU_IMPLEMENT int32 mdjvu_image_get_dictionary_index(mdjvu_image_t image, mdjvu_bitmap_t b) |
| 619 |
{ |
| 620 |
return ((int32 *) IMG->artifacts[mdjvu_artifact_dictionary_index])[mdjvu_bitmap_get_index(b)]; |
| 621 |
} |
| 622 |
MDJVU_IMPLEMENT void mdjvu_image_set_dictionary_index(mdjvu_image_t image, mdjvu_bitmap_t b, int32 v) |
| 623 |
{ |
| 624 |
((int32 *) IMG->artifacts[mdjvu_artifact_dictionary_index])[mdjvu_bitmap_get_index(b)] = v; |
| 625 |
} |
| 626 |
|
| 627 |
MDJVU_IMPLEMENT void mdjvu_image_set_substitution(mdjvu_image_t image, mdjvu_bitmap_t b, mdjvu_bitmap_t s) |
| 628 |
{ |
| 629 |
if (s && b != s && mdjvu_image_has_bitmap(image, s)) |
| 630 |
s = mdjvu_image_get_substitution(image, s); |
| 631 |
|
| 632 |
((mdjvu_bitmap_t *) IMG->artifacts[mdjvu_artifact_substitution]) |
| 633 |
[mdjvu_bitmap_get_index(b)] = s; |
| 634 |
} |
| 635 |
|
| 636 |
MDJVU_IMPLEMENT void mdjvu_image_get_center(mdjvu_image_t image, mdjvu_bitmap_t b, int32 *px, int32 *py) |
| 637 |
{ |
| 638 |
Point *p = (Point *) IMG->artifacts[mdjvu_artifact_center]; |
| 639 |
int32 i = mdjvu_bitmap_get_index(b); |
| 640 |
*px = p[i].x; |
| 641 |
*py = p[i].y; |
| 642 |
} |
| 643 |
|
| 644 |
MDJVU_IMPLEMENT void mdjvu_image_set_center(mdjvu_image_t image, mdjvu_bitmap_t b, int32 x, int32 y) |
| 645 |
{ |
| 646 |
Point *p = (Point *) IMG->artifacts[mdjvu_artifact_center]; |
| 647 |
int32 i = mdjvu_bitmap_get_index(b); |
| 648 |
p[i].x = x; |
| 649 |
p[i].y = y; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
/* This function sorts bitmaps approximately according to blits. |
| 654 |
* Algorithm is simple: which shape is used earlier goes first. |
| 655 |
*/ |
| 656 |
MDJVU_IMPLEMENT void mdjvu_image_sort_bitmaps(mdjvu_image_t img) |
| 657 |
{ |
| 658 |
int32 blit_count = mdjvu_image_get_blit_count (img); |
| 659 |
int32 blits_passed, shapes_passed = 0; |
| 660 |
for (blits_passed = 0; blits_passed < blit_count; blits_passed++) |
| 661 |
{ |
| 662 |
mdjvu_bitmap_t bitmap = mdjvu_image_get_blit_bitmap(img, blits_passed); |
| 663 |
int32 i = mdjvu_bitmap_get_index(bitmap); |
| 664 |
if (i < shapes_passed) continue; |
| 665 |
mdjvu_image_exchange_bitmaps(img, shapes_passed, i); |
| 666 |
shapes_passed++; |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
/* __________________________ removing margins _________________________ */ |
| 672 |
|
| 673 |
MDJVU_IMPLEMENT void mdjvu_image_remove_bitmap_margins(mdjvu_image_t image) |
| 674 |
{ |
| 675 |
int32 *delta_x, *delta_y; |
| 676 |
int32 n = IMG->bitmaps_count; |
| 677 |
int32 b = IMG->blits_count; |
| 678 |
int32 i; |
| 679 |
|
| 680 |
delta_x = (int32 *) malloc(n * sizeof(int32)); |
| 681 |
delta_y = (int32 *) malloc(n * sizeof(int32)); |
| 682 |
|
| 683 |
for (i = 0; i < n; i++) |
| 684 |
mdjvu_bitmap_remove_margins(IMG->bitmaps[i], &delta_x[i], &delta_y[i]); |
| 685 |
|
| 686 |
for (i = 0; i < b; i++) |
| 687 |
{ |
| 688 |
int32 blit_index = mdjvu_bitmap_get_index(IMG->blits[i]); |
| 689 |
IMG->x[i] += delta_x[blit_index]; |
| 690 |
IMG->y[i] += delta_y[blit_index]; |
| 691 |
} |
| 692 |
|
| 693 |
free(delta_x); |
| 694 |
free(delta_y); |
| 695 |
} |