File: frames.cpp

package info (click to toggle)
djvulibre 3.5.27.1-10
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,664 kB
  • sloc: cpp: 65,465; xml: 5,723; ansic: 4,936; sh: 865; makefile: 316
file content (416 lines) | stat: -rw-r--r-- 12,807 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* minidjvu - library for handling bilevel images with DjVuBitonal support
 *
 * frames.c - extracting frameworks and calculating "importance rating"
 *
 * Copyright (C) 2005  Ilya Mezhirov
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 
 * minidjvu is derived from DjVuLibre (http://djvu.sourceforge.net)
 * All over DjVuLibre there is a patent alert from LizardTech
 * which I guess I should reproduce (don't ask me what does this mean):
 * 
 *  ------------------------------------------------------------------
 * | DjVu (r) Reference Library (v. 3.5)
 * | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
 * | The DjVu Reference Library is protected by U.S. Pat. No.
 * | 6,058,214 and patents pending.
 * |
 * | This software is subject to, and may be distributed under, the
 * | GNU General Public License, either Version 2 of the license,
 * | or (at your option) any later version. The license should have
 * | accompanied the software or you may obtain a copy of the license
 * | from the Free Software Foundation at http://www.fsf.org .
 * |
 * | The computer code originally released by LizardTech under this
 * | license and unmodified by other parties is deemed "the LIZARDTECH
 * | ORIGINAL CODE."  Subject to any third party intellectual property
 * | claims, LizardTech grants recipient a worldwide, royalty-free, 
 * | non-exclusive license to make, use, sell, or otherwise dispose of 
 * | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
 * | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
 * | General Public License.   This grant only confers the right to 
 * | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
 * | the extent such infringement is reasonably necessary to enable 
 * | recipient to make, have made, practice, sell, or otherwise dispose 
 * | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
 * | any greater extent that may be necessary to utilize further 
 * | modifications or combinations.
 * |
 * | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
 * | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
 * | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 * +------------------------------------------------------------------
 */

/* Frameworks are funny things...
 * The algorithm is to be commented yet.
 * Here's a picture illustrating what is a frame
 * (view with monospace font):
 *
 *  Original letter:        Its framework:
 *
  .....@@@@@@@@........ .....................
  ...@@@@@@@@@@@@...... ......@@@@@..........
  ..@@@@@@@@@@@@@@..... .....@@...@@@........
  ..@@@@@...@@@@@@@.... ....@@......@@.......
  ..@@@@.....@@@@@@.... ....@........@.......
  .@@@@@.....@@@@@@.... ....@........@.......
  .@@@@@.....@@@@@@.... ....@........@.......
  ..@@@@.....@@@@@@.... ....@........@.......
  ..........@@@@@@@.... .............@.......
  .......@@@@@@@@@@.... .............@.......
  .....@@@@@@@@@@@@.... ........@@@@@@.......
  ...@@@@@@@@@@@@@@.... ......@@@....@.......
  ..@@@@@@@..@@@@@@.... .....@@......@.......
  .@@@@@@....@@@@@@.... ...@@@.......@.......
  .@@@@@.....@@@@@@.... ...@.........@.......
  @@@@@......@@@@@@.... ..@@.........@.......
  @@@@@......@@@@@@.... ..@..........@.......
  @@@@@.....@@@@@@@.... ..@..........@.......
  @@@@@@....@@@@@@@.@@@ ..@@.........@.......
  .@@@@@@@@@@@@@@@@@@@@ ...@@.....@@@@@......
  .@@@@@@@@@@@@@@@@@@@@ ....@@..@@@...@@@@@..
  ..@@@@@@@@@.@@@@@@@@. .....@@@@............
  ....@@@@@....@@@@@... .....................

 * A letter is converted into grayshades,
 *   and a frame is the set of its purely black pixels after the transformation.
 * In the grayshade version of a letter,
 *   all pixels that were white remain absolutely white,
 *   the frame is black and the blackness falls down from it to the border.
 */

/* Offtopic: I wonder if this thing could help OCRing because frameworks
 * perfectly retain readability while becoming essentially 1-dimensional.
 */

#include "mdjvucfg.h"
#include "minidjvu.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <math.h>

/* Stuff for not using malloc in C++
 * (made by Leon Bottou; has no use in minidjvu,
 * but left here for potential DjVuLibre compatibility)
 */
#ifdef __cplusplus
# define MALLOC(Type)    new Type
# define FREE(p)         delete p
# define MALLOCV(Type,n) new Type[n]
# define FREEV(p)        delete [] p
#else
# define MALLOC(Type)    ((Type*)malloc(sizeof(Type)))
# define FREE(p)         do{if(p)free(p);}while(0)
# define MALLOCV(Type,n) ((Type*)malloc(sizeof(Type)*(n)))
# define FREEV(p)        do{if(p)free(p);}while(0)
#endif


/* This determines the gray level of the border (ratio of black).
 * Setting it to 1 will effectively eliminate grayshading.
 */
#define BORDER_FALLOFF .7 /* this is the main constant in all the matcher... */

typedef unsigned char byte;

static int donut_connectivity_test(byte *upper, byte *row, byte *lower)/*{{{*/
{
    /*(on the pictures below 0 is white, 1 is black or gray)
     *
     * 01.
     * 1 . -> 1
     * ...
     *
     * .0.
     * 1 1 ->  1
     * .0.
     *
     * all others -> 0
     */

    int sum, l, u, d, r;

    sum = (u = *upper ? 1 : 0) + (d = *lower ? 1 : 0) +
          (l = row[-1] ? 1 : 0) + (r = row[1] ? 1 : 0);

    switch(sum)
    {
        case 3:/*{{{*/
        {
            int x = 6 - (u + (l << 1) + d + (d << 1));
            switch(x)
            {
                case 0: /* l */
                    return upper[-1] && lower[-1] ? 0 : 1;
                case 1: /* d */
                    return lower[-1] && lower[1] ? 0 : 1;
                case 2: /* r */
                    return upper[1] && lower[1] ? 0 : 1;
                case 3: /* u */
                    return upper[-1] && upper[1] ? 0 : 1;
                default: assert(0); return 0;
            }
        }
        break;/*}}}*/
        case 2:/*{{{*/
        {
            int s = l + r;
            if (s & 1)
            {
                /*   A1.
                 *   1 0 - should be !A (2x2 square extermination)
                 *   .0.
                 */
                if (l)
                {
                    if (u)
                        return upper[-1] ? 0 : 1;
                    else
                        return lower[-1] ? 0 : 1;
                }
                else /* r */
                {
                    if (u)
                        return upper[1] ? 0 : 1;
                    else
                        return lower[1] ? 0 : 1;
                }
            }
            else
            {
                /*   .0.
                 *   1 1 - surely should be 1 to preserve connection
                 *   .0.
                 */
                return 1;
            }
        }
        break;/*}}}*/
        case 0: case 4:
            return 1;
        case 1:
            return 0;
        default: assert(0); return 0;
    }
    assert(0); return 0;
}/*}}}*/
static byte donut_transform_pixel(byte *upper, byte *row, byte *lower)/*{{{*/
{
    /* (center pixel should be gray in order for this to work)
     * (on the pictures below 0 is white, 1 is black or gray)
     *
     * 01.
     * 1 . -> center will become 1
     * ...
     *
     * .0.
     * 1 1 -> center will become 1
     * .0.
     *
     * 00.
     * 1 0 -> center will become 1
     * .0.
     *
     * 1..
     * 1 0 -> center will become 0
     * 1..
     *
     * 11.
     * 1 0 -> center will become 0
     * .0.
     *
     * .A.
     * A A -> center will become 1
     * .A.
     */

    int sum, l, u, d, r;
    if (!*row) return 0;

    sum = (u = *upper ? 1 : 0) + (d = *lower ? 1 : 0) +
          (l = row[-1] ? 1 : 0) + (r = row[1] ? 1 : 0);

    switch(sum)
    {
        case 1: case 3:/*{{{*/
        {
            int x = u + (l << 1) + d + (d << 1);
            if (sum == 3) x = (6 - x) ^ 2;
            switch(x)
            {
                case 0: /* r */
                    return upper[1] && lower[1] ? 0 : 1;
                case 1: /* u */
                    return upper[-1] && upper[1] ? 0 : 1;
                case 2: /* l */
                    return upper[-1] && lower[-1] ? 0 : 1;
                case 3: /* d */
                    return lower[-1] && lower[1] ? 0 : 1;
                default: assert(0); return 0;
            }
        }
        break;/*}}}*/
        case 2:/*{{{*/
        {
            int s = l + r;
            if (s & 1)
            {
                /*   A1.
                 *   1 0 - should be !A (2x2 square extermination)
                 *   .0.
                 */
                if (l)
                {
                    if (u)
                        return upper[-1] ? 0 : 1;
                    else
                        return lower[-1] ? 0 : 1;
                }
                else /* r */
                {
                    if (u)
                        return upper[1] ? 0 : 1;
                    else
                        return lower[1] ? 0 : 1;
                }
            }
            else
            {
                /*   .0.
                 *   1 1 - surely should be 1 to preserve connection
                 *   .0.
                 */
                return 1;
            }
        }
        break;/*}}}*/
        case 0: case 4:
            return 1; /* lone pixels are NOT omitted */
        default: assert(0); return 0;
    }
    assert(0); return 0;
}/*}}}*/

/* `pixels' should have a margin of 1 pixel at each side
 * returns true if the image was changed
 */
static int flay(byte **pixels, int w, int h, int rank, int **ranks)
{
    int i, j, result = 0;

    byte *buf = MALLOCV(byte, w * h);

    assert(pixels);
    assert(w);
    assert(h);

    for (i = 0; i < h; i++) for (j = 0; j < w; j++)
    {
        buf[w * i + j] =
            donut_transform_pixel(pixels[i-1] + j, pixels[i] + j, pixels[i+1] + j);
    }

    for (i = 0; i < h; i++)
    {
        byte *up = pixels[i-1], *row = pixels[i], *dn = pixels[i+1];
        byte *buf_row = buf + w * i;
        int *rank_row = NULL;
        if (ranks) rank_row = ranks[i];
        for (j = 0; j < w; j++)
        {
            if (row[j] && !buf_row[j])
            {
                if (!donut_connectivity_test(up + j, row + j, dn + j))
                {
                    row[j] = buf_row[j];
                    if (rank) rank_row[j] = rank;
                    result = 1;
                }
            }
            else
                row[j] = buf_row[j];
        }
    }

    FREEV(buf);
    return result;
}

/* TODO: use less temporary buffers and silly copyings */
MDJVU_IMPLEMENT void mdjvu_soften_pattern(byte **result, byte **pixels, int32 w, int32 h)/*{{{*/
{
    byte *r = MALLOCV(byte, (w + 2) * (h + 2));
    byte **pointers = MALLOCV(byte *, h + 2);
    int *ranks_buf = MALLOCV(int, w * h);
    int **ranks = MALLOCV(int *, h);

    int i, j, passes = 1;
    double level = 1, falloff;
    byte *colors;

    memset(r, 0, (w + 2) * (h + 2));
    memset(ranks_buf, 0, w * h * sizeof(int));

    for (i = 0; i < h + 2; i++)
        pointers[i] = r + (w + 2) * i + 1;

    for (i = 0; i < h; i++)
        memcpy(pointers[i+1], pixels[i], w);

    for (i = 0; i < h; i++)
        ranks[i] = ranks_buf + w * i;

    while(flay(pointers + 1, w, h, passes, ranks)) passes++;

    colors = MALLOCV(byte, passes + 1);

    falloff = pow(BORDER_FALLOFF, 1./passes);

    for (i = 0; i < passes; i++)
    {
        colors[i] = (byte) (level * 255);
        level *= falloff;
    }
    /* TODO: test the next line */
    /* colors[passes - 1] = 50; pay less attention to border pixels */
    colors[passes] = 0;

    pointers++;
    for (i = 0; i < h; i++)
    {
        for (j = 0; j < w; j++)
        {
            if (pointers[i][j])
            {
                result[i][j] = 255;
            }
            else
            {
                result[i][j] = colors[passes - ranks[i][j]];
            }
        }
    }
    pointers--;

    FREEV(colors);
    FREEV(ranks);
    FREEV(ranks_buf);
    FREEV(r);
    FREEV(pointers);
}/*}}}*/