| 1 |
/* |
| 2 |
* adjust_y.c - adjust y coordinates of blits so the text won't look bumpy |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "../base/mdjvucfg.h" |
| 6 |
#include <minidjvu/minidjvu.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <assert.h> |
| 9 |
|
| 10 |
|
| 11 |
#define DO_NOT_ADJUST -10000 |
| 12 |
|
| 13 |
|
| 14 |
/* |
| 15 |
* This algorithm was made by Leon Bottou. |
| 16 |
* I remember, I had another (simpler) version |
| 17 |
* and I couldn't see the difference between results, |
| 18 |
* but he said this version is cooler. |
| 19 |
*/ |
| 20 |
|
| 21 |
|
| 22 |
/* Estimate position of baseline. |
| 23 |
* The baseline position is measured in quarter pixels. |
| 24 |
* Using fractional pixels makes a big improvement. |
| 25 |
*/ |
| 26 |
static int32 compute_baseline(mdjvu_bitmap_t bitmap) |
| 27 |
{ |
| 28 |
int32 w = mdjvu_bitmap_get_width(bitmap); |
| 29 |
int32 h = mdjvu_bitmap_get_height(bitmap); |
| 30 |
int32 *mass = (int32 *) malloc(h * sizeof(int32)); |
| 31 |
unsigned char *row = (unsigned char *) malloc(w); |
| 32 |
int32 i, j, m; |
| 33 |
int32 tm = 0; |
| 34 |
for (i = 0; i < h; i++) |
| 35 |
{ |
| 36 |
mdjvu_bitmap_unpack_row(bitmap, row, i); |
| 37 |
for (j = 0; j < w; j++) |
| 38 |
{ |
| 39 |
if (row[j]) |
| 40 |
break; |
| 41 |
} |
| 42 |
for (m = w - j; m > 0; m--) |
| 43 |
{ |
| 44 |
if (row[j + m - 1]) |
| 45 |
break; |
| 46 |
} |
| 47 |
mass[h - i - 1] = m; |
| 48 |
tm += m; |
| 49 |
} |
| 50 |
|
| 51 |
m = 0; |
| 52 |
i = 0; |
| 53 |
|
| 54 |
while (m * 6 < tm * 4) |
| 55 |
{ |
| 56 |
m += mass[i/4]; |
| 57 |
i += 1; |
| 58 |
} |
| 59 |
|
| 60 |
free(row); |
| 61 |
free(mass); |
| 62 |
|
| 63 |
return 4 * (h - 1) - i; |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* Set blits to their substitutes using given x and y adjustments. |
| 68 |
* Before this, blits may not use dictionary bitmaps. |
| 69 |
*/ |
| 70 |
static void update_blits(mdjvu_image_t image, int32 *x_adjust, int32 *y_adjust) |
| 71 |
{ |
| 72 |
int32 b = mdjvu_image_get_blit_count(image); |
| 73 |
int32 i; |
| 74 |
for (i = 0; i < b; i++) |
| 75 |
{ |
| 76 |
int32 x = mdjvu_image_get_blit_x(image, i); |
| 77 |
int32 y = mdjvu_image_get_blit_y(image, i); |
| 78 |
mdjvu_bitmap_t bitmap = mdjvu_image_get_blit_bitmap(image, i); |
| 79 |
mdjvu_bitmap_t subst = mdjvu_image_get_substitution(image, bitmap); |
| 80 |
int32 k = mdjvu_bitmap_get_index(bitmap); |
| 81 |
|
| 82 |
mdjvu_image_set_blit_x(image, i, x + x_adjust[k]); |
| 83 |
mdjvu_image_set_blit_y(image, i, y + y_adjust[k]); |
| 84 |
mdjvu_image_set_blit_bitmap(image, i, subst); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* Return the baselines array. To be MDJVU_FREEV'ed later. |
| 90 |
*/ |
| 91 |
static int32 *get_baselines(mdjvu_image_t image) |
| 92 |
{ |
| 93 |
int32 i; |
| 94 |
int32 n = mdjvu_image_get_bitmap_count(image); |
| 95 |
int32 *baseline = MDJVU_MALLOCV(int32, n); |
| 96 |
for (i = 0; i < n; i++) |
| 97 |
{ |
| 98 |
mdjvu_bitmap_t bitmap = mdjvu_image_get_bitmap(image, i); |
| 99 |
if (mdjvu_image_has_not_a_letter_flags(image) && !mdjvu_image_get_not_a_letter_flag(image, bitmap)) |
| 100 |
baseline[i] = compute_baseline(bitmap); |
| 101 |
else |
| 102 |
baseline[i] = DO_NOT_ADJUST; |
| 103 |
} |
| 104 |
return baseline; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
/* |
| 109 |
* Computes x and y adjustsments. |
| 110 |
* bitmap != subst. |
| 111 |
*/ |
| 112 |
static void compute_adjustments(mdjvu_bitmap_t bitmap, mdjvu_bitmap_t subst, |
| 113 |
int32 b_base, int32 s_base, |
| 114 |
int32 *dx, int32 *dy) |
| 115 |
{ |
| 116 |
int32 w, h, subst_w, subst_h, adjust; |
| 117 |
|
| 118 |
/* Compute coordinate adjustment */ |
| 119 |
w = mdjvu_bitmap_get_width(bitmap); |
| 120 |
h = mdjvu_bitmap_get_height(bitmap); |
| 121 |
subst_w = mdjvu_bitmap_get_width(subst); |
| 122 |
subst_h = mdjvu_bitmap_get_height(subst); |
| 123 |
*dx = (w - subst_w) / 2; |
| 124 |
*dy = (h - subst_h) / 2; |
| 125 |
|
| 126 |
/* Refine vertical adjustment */ |
| 127 |
adjust = b_base - s_base; |
| 128 |
|
| 129 |
if (adjust < 0) |
| 130 |
adjust = - (2 - adjust) / 4; |
| 131 |
else |
| 132 |
adjust = (2 + adjust) / 4; |
| 133 |
|
| 134 |
if (abs(adjust - *dy) <= 1 + w/16 ) |
| 135 |
*dy = adjust; |
| 136 |
} |
| 137 |
|
| 138 |
MDJVU_IMPLEMENT void mdjvu_adjust(mdjvu_image_t image) |
| 139 |
{ |
| 140 |
int32 b = mdjvu_image_get_blit_count(image); |
| 141 |
int32 n = mdjvu_image_get_bitmap_count(image); |
| 142 |
int32 *baseline = get_baselines(image); |
| 143 |
|
| 144 |
int32 i; |
| 145 |
int32 *x_adjust = (int32 *) calloc(n, sizeof(int32)); |
| 146 |
int32 *y_adjust = (int32 *) calloc(n, sizeof(int32)); |
| 147 |
|
| 148 |
for (i = 0; i < b; i++) |
| 149 |
{ |
| 150 |
mdjvu_bitmap_t bitmap = mdjvu_image_get_blit_bitmap(image, i); |
| 151 |
mdjvu_bitmap_t subst = mdjvu_image_get_substitution(image, bitmap); |
| 152 |
assert(subst); |
| 153 |
|
| 154 |
if (subst == bitmap) continue; |
| 155 |
|
| 156 |
compute_adjustments(bitmap, subst, |
| 157 |
baseline[i], |
| 158 |
baseline[mdjvu_bitmap_get_index(subst)], |
| 159 |
&x_adjust[i], &y_adjust[i]); |
| 160 |
} |
| 161 |
|
| 162 |
MDJVU_FREEV(baseline); |
| 163 |
|
| 164 |
update_blits(image, x_adjust, y_adjust); |
| 165 |
|
| 166 |
free(x_adjust); |
| 167 |
free(y_adjust); |
| 168 |
} |
| 169 |
|
| 170 |
static void adjust_page(mdjvu_image_t dict, |
| 171 |
int32 *dict_baselines, |
| 172 |
mdjvu_image_t image) |
| 173 |
{ |
| 174 |
int32 b = mdjvu_image_get_blit_count(image); |
| 175 |
int32 n = mdjvu_image_get_bitmap_count(image); |
| 176 |
int32 *baseline = get_baselines(image); |
| 177 |
|
| 178 |
int32 i; |
| 179 |
int32 *x_adjust = (int32 *) calloc(n, sizeof(int32)); |
| 180 |
int32 *y_adjust = (int32 *) calloc(n, sizeof(int32)); |
| 181 |
|
| 182 |
for (i = 0; i < b; i++) |
| 183 |
{ |
| 184 |
mdjvu_bitmap_t bitmap = mdjvu_image_get_blit_bitmap(image, i); |
| 185 |
mdjvu_bitmap_t subst = mdjvu_image_get_substitution(image, bitmap); |
| 186 |
int32 subst_baseline; |
| 187 |
|
| 188 |
if (subst == bitmap || baseline[i] == DO_NOT_ADJUST) continue; |
| 189 |
|
| 190 |
if (mdjvu_image_has_bitmap(dict, subst)) |
| 191 |
subst_baseline = dict_baselines[mdjvu_bitmap_get_index(subst)]; |
| 192 |
else |
| 193 |
{ |
| 194 |
assert(mdjvu_image_has_bitmap(image, subst)); |
| 195 |
subst_baseline = baseline[mdjvu_bitmap_get_index(subst)]; |
| 196 |
} |
| 197 |
|
| 198 |
compute_adjustments(bitmap, subst, |
| 199 |
baseline[i], |
| 200 |
subst_baseline, |
| 201 |
&x_adjust[i], &y_adjust[i]); |
| 202 |
} |
| 203 |
|
| 204 |
MDJVU_FREEV(baseline); |
| 205 |
|
| 206 |
update_blits(image, x_adjust, y_adjust); |
| 207 |
|
| 208 |
free(x_adjust); |
| 209 |
free(y_adjust); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
MDJVU_FUNCTION void mdjvu_multipage_adjust(mdjvu_image_t dict, |
| 214 |
int32 npages, |
| 215 |
mdjvu_image_t *pages) |
| 216 |
{ |
| 217 |
int32 *dict_baselines = get_baselines(dict); |
| 218 |
int32 i; |
| 219 |
for (i = 0; i < npages; i++) |
| 220 |
adjust_page(dict, dict_baselines, pages[i]); |
| 221 |
MDJVU_FREEV(dict_baselines); |
| 222 |
} |