| 1 |
/* |
| 2 |
* delegate.c - choosing a representative over a class of letters |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "../base/mdjvucfg.h" |
| 6 |
#include <minidjvu/minidjvu.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <stdio.h> |
| 10 |
|
| 11 |
MDJVU_IMPLEMENT void mdjvu_multipage_choose_representatives |
| 12 |
(int32 npages, |
| 13 |
mdjvu_image_t *pages, |
| 14 |
int32 max_tag, |
| 15 |
int32 *tags, |
| 16 |
mdjvu_bitmap_t *representatives) |
| 17 |
{ |
| 18 |
int page_number; |
| 19 |
int32 total_bitmaps_passed = 0; |
| 20 |
memset(representatives, 0, (max_tag + 1) * sizeof(mdjvu_bitmap_t)); |
| 21 |
for (page_number = 0; page_number < npages; page_number++) |
| 22 |
{ |
| 23 |
mdjvu_image_t page = pages[page_number]; |
| 24 |
int32 bitmap_count = mdjvu_image_get_bitmap_count(page); |
| 25 |
int32 i; /* index of bitmap in a page */ |
| 26 |
|
| 27 |
for (i = 0; i < bitmap_count; i++) |
| 28 |
{ |
| 29 |
int32 tag = tags[total_bitmaps_passed++]; |
| 30 |
if (!tag) continue; /* skip non-substitutable bitmaps */ |
| 31 |
if (!representatives[tag]) |
| 32 |
representatives[tag] = mdjvu_image_get_bitmap(page, i); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
MDJVU_IMPLEMENT mdjvu_image_t mdjvu_multipage_choose_average_representatives |
| 38 |
(int32 npages, |
| 39 |
mdjvu_image_t *pages, |
| 40 |
int32 total_count, |
| 41 |
int32 max_tag, |
| 42 |
int32 *tags, |
| 43 |
mdjvu_bitmap_t *representatives, |
| 44 |
unsigned char *dictionary_flags) |
| 45 |
{ |
| 46 |
int32 page_number, tag; |
| 47 |
mdjvu_bitmap_t *sources; |
| 48 |
int32 *cx, *cy; |
| 49 |
mdjvu_image_t dictionary = mdjvu_image_create(0,0); /* 0 x 0 image */ |
| 50 |
|
| 51 |
memset(representatives, 0, (max_tag + 1) * sizeof(mdjvu_bitmap_t)); |
| 52 |
|
| 53 |
sources = (mdjvu_bitmap_t *) malloc(total_count * sizeof(mdjvu_bitmap_t)); |
| 54 |
cx = (int32 *) malloc(total_count * sizeof(int32)); |
| 55 |
cy = (int32 *) malloc(total_count * sizeof(int32)); |
| 56 |
|
| 57 |
for (tag = 1; tag < max_tag; tag++) |
| 58 |
{ |
| 59 |
int32 sources_found = 0, total_bitmaps_passed = 0; |
| 60 |
mdjvu_bitmap_t rep; |
| 61 |
if (!dictionary_flags[tag]) continue; |
| 62 |
|
| 63 |
memset(sources, 0, total_count * sizeof(mdjvu_bitmap_t)); |
| 64 |
memset(cx, 0, total_count * sizeof(int32)); |
| 65 |
memset(cy, 0, total_count * sizeof(int32)); |
| 66 |
for (page_number = 0; page_number < npages; page_number++) |
| 67 |
{ |
| 68 |
mdjvu_image_t page = pages[page_number]; |
| 69 |
int32 bitmap_count = mdjvu_image_get_bitmap_count(page); |
| 70 |
int32 i; /* index of bitmap in a page */ |
| 71 |
|
| 72 |
for (i = 0; i < bitmap_count; i++) |
| 73 |
{ |
| 74 |
if (tags[total_bitmaps_passed++] == tag) |
| 75 |
{ |
| 76 |
sources[sources_found] = mdjvu_image_get_bitmap(page, i); |
| 77 |
mdjvu_image_get_center(page, sources[sources_found], &cx[sources_found], &cy[sources_found]); |
| 78 |
sources_found++; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if (sources_found) |
| 84 |
{ |
| 85 |
rep = mdjvu_average(sources, sources_found, cx, cy); |
| 86 |
representatives[tag] = rep; |
| 87 |
} |
| 88 |
} |
| 89 |
free(cx); |
| 90 |
free(cy); |
| 91 |
free(sources); |
| 92 |
|
| 93 |
for (page_number = 0; page_number < npages; page_number++) |
| 94 |
mdjvu_image_disable_centers(pages[page_number]); |
| 95 |
|
| 96 |
for (tag = 1; tag <= max_tag; tag++) |
| 97 |
{ |
| 98 |
mdjvu_bitmap_t rep; |
| 99 |
|
| 100 |
if (!dictionary_flags[tag] || (rep = representatives[tag]) == NULL) continue; |
| 101 |
mdjvu_image_add_bitmap(dictionary, rep); |
| 102 |
} |
| 103 |
return dictionary; |
| 104 |
} |