dewarp.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*====================================================================*
  2. - Copyright (C) 2001 Leptonica. All rights reserved.
  3. -
  4. - Redistribution and use in source and binary forms, with or without
  5. - modification, are permitted provided that the following conditions
  6. - are met:
  7. - 1. Redistributions of source code must retain the above copyright
  8. - notice, this list of conditions and the following disclaimer.
  9. - 2. Redistributions in binary form must reproduce the above
  10. - copyright notice, this list of conditions and the following
  11. - disclaimer in the documentation and/or other materials
  12. - provided with the distribution.
  13. -
  14. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
  18. - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *====================================================================*/
  26. #ifndef LEPTONICA_DEWARP_H
  27. #define LEPTONICA_DEWARP_H
  28. /*!
  29. * \file dewarp.h
  30. *
  31. * <pre>
  32. * Data structure to hold arrays and results for generating
  33. * horizontal and vertical disparity arrays based on textlines.
  34. * Each disparity array is two-dimensional. The vertical disparity
  35. * array gives a vertical displacement, relative to the lowest point
  36. * in the textlines. The horizontal disparty array gives a horizontal
  37. * displacement, relative to the minimum values (for even pages)
  38. * or maximum values (for odd pages) of the left and right ends of
  39. * full textlines. Horizontal alignment always involves translations
  40. * away from the book gutter.
  41. *
  42. * We have intentionally separated the process of building models
  43. * from the rendering process that uses the models. For any page,
  44. * the building operation either creates an actual model (that is,
  45. * a model with at least the vertical disparity being computed, and
  46. * for which the 'success' flag is set) or fails to create a model.
  47. * However, at rendering time, a page can have one of two different
  48. * types of models.
  49. * (1) A valid model is an actual model that meets the rendering
  50. * constraints, which are limits on model curvature parameters.
  51. * See dewarpaTestForValidModel() for details.
  52. * Valid models are identified by dewarpaInsertRefModels(),
  53. * which sets the 'vvalid' and 'hvalid' fields. Only valid
  54. * models are used for rendering.
  55. * (2) A reference model is used by a page that doesn't have
  56. * a valid model, but has a nearby valid model of the same
  57. * parity (even/odd page) that it can use. The range in pages
  58. * to search for a valid model is given by the 'maxdist' field.
  59. *
  60. * At the rendering stage, vertical and horizontal disparities are
  61. * treated differently. It is somewhat more robust to generate
  62. * vertical disparity models (VDM) than horizontal disparity
  63. * models (HDM). A valid VDM is required for any correction to
  64. * be made; if a valid VDM is not available, just use the input
  65. * image. Otherwise, assuming it is available, the use of the
  66. * HDM is controlled by two fields: 'useboth' and 'check_columns'.
  67. * (a) With useboth == 0, we use only the VDM.
  68. * (b) With useboth == 1, we require using the VDM and, if a valid
  69. * horizontal disparity model (HDM) is available, we also use it.
  70. * (c) With check_columns == 1, check for multiple columns and if
  71. * true, only use the VDM, even if a valid HDM is available.
  72. * Note that 'check_columns' takes precedence over 'useboth'
  73. * when there is more than 1 column of text. By default,
  74. * check_columns == 0.
  75. *
  76. * The 'maxdist' parameter is input when the dewarpa is created.
  77. * The other rendering parameters have default values given in dewarp1.c.
  78. * All parameters used by rendering can be set (or reset) using accessors.
  79. *
  80. * After dewarping, use of the VDM will cause all points on each
  81. * altered curve to have a y-value equal to the minimum. Use of
  82. * the HDA will cause the left and right edges of the textlines
  83. * to be vertically aligned if they had been typeset flush-left
  84. * and flush-right, respectively.
  85. *
  86. * The sampled disparity arrays are expanded to full resolution,
  87. * using linear interpolation, and this is further expanded
  88. * by slope continuation to the right and below if the image
  89. * is larger than the full resolution disparity arrays. Then
  90. * the disparity correction can be applied to the input image.
  91. * If the input pix are 2x reduced, the expansion from sampled
  92. * to full res uses the product of (sampling) * (redfactor).
  93. *
  94. * The most accurate results are produced at full resolution, and
  95. * this is generally recommended.
  96. * </pre>
  97. */
  98. /*! Dewarp version for serialization
  99. * <pre>
  100. * Note on versioning of the serialization of this data structure:
  101. * The dewarping utility and the stored data can be expected to change.
  102. * In most situations, the serialized version is ephemeral -- it is
  103. * not needed after being used. No functions will be provided to
  104. * convert between different versions.
  105. * </pre>
  106. */
  107. #define DEWARP_VERSION_NUMBER 4
  108. /*! Data structure to hold a number of Dewarp */
  109. struct L_Dewarpa
  110. {
  111. l_int32 nalloc; /*!< size of dewarp ptr array */
  112. l_int32 maxpage; /*!< maximum page number in array */
  113. struct L_Dewarp **dewarp; /*!< array of ptrs to page dewarp */
  114. struct L_Dewarp **dewarpcache; /*!< array of ptrs to cached dewarps */
  115. struct Numa *namodels; /*!< list of page numbers for pages */
  116. /*!< with page models */
  117. struct Numa *napages; /*!< list of page numbers with either */
  118. /*!< page models or ref page models */
  119. l_int32 redfactor; /*!< reduction factor of input: 1 or 2 */
  120. l_int32 sampling; /*!< disparity arrays sampling factor */
  121. l_int32 minlines; /*!< min number of long lines required */
  122. l_int32 maxdist; /*!< max distance for getting ref page */
  123. l_int32 max_linecurv; /*!< maximum abs line curvature, */
  124. /*!< in micro-units */
  125. l_int32 min_diff_linecurv; /*!< minimum abs diff line */
  126. /*!< curvature in micro-units */
  127. l_int32 max_diff_linecurv; /*!< maximum abs diff line */
  128. /*!< curvature in micro-units */
  129. l_int32 max_edgeslope; /*!< maximum abs left or right edge */
  130. /*!< slope, in milli-units */
  131. l_int32 max_edgecurv; /*!< maximum abs left or right edge */
  132. /*!< curvature, in micro-units */
  133. l_int32 max_diff_edgecurv; /*!< maximum abs diff left-right */
  134. /*!< edge curvature, in micro-units */
  135. l_int32 useboth; /*!< use both disparity arrays if */
  136. /*!< available; only vertical otherwise */
  137. l_int32 check_columns; /*!< if there are multiple columns, */
  138. /*!< only use the vertical disparity */
  139. /*!< array */
  140. l_int32 modelsready; /*!< invalid models have been removed */
  141. /*!< and refs built against valid set */
  142. };
  143. typedef struct L_Dewarpa L_DEWARPA;
  144. /*! Data structure for a single dewarp */
  145. struct L_Dewarp
  146. {
  147. struct L_Dewarpa *dewa; /*!< ptr to parent (not owned) */
  148. struct Pix *pixs; /*!< source pix, 1 bpp */
  149. struct FPix *sampvdispar; /*!< sampled vert disparity array */
  150. struct FPix *samphdispar; /*!< sampled horiz disparity array */
  151. struct FPix *sampydispar; /*!< sampled slope h-disparity array */
  152. struct FPix *fullvdispar; /*!< full vert disparity array */
  153. struct FPix *fullhdispar; /*!< full horiz disparity array */
  154. struct FPix *fullydispar; /*!< full slope h-disparity array */
  155. struct Numa *namidys; /*!< sorted y val of midpoint each line */
  156. struct Numa *nacurves; /*!< sorted curvature of each line */
  157. l_int32 w; /*!< width of source image */
  158. l_int32 h; /*!< height of source image */
  159. l_int32 pageno; /*!< page number; important for reuse */
  160. l_int32 sampling; /*!< sampling factor of disparity arrays */
  161. l_int32 redfactor; /*!< reduction factor of pixs: 1 or 2 */
  162. l_int32 minlines; /*!< min number of long lines required */
  163. l_int32 nlines; /*!< number of long lines found */
  164. l_int32 mincurv; /*!< min line curvature in micro-units */
  165. l_int32 maxcurv; /*!< max line curvature in micro-units */
  166. l_int32 leftslope; /*!< left edge slope in milli-units */
  167. l_int32 rightslope; /*!< right edge slope in milli-units */
  168. l_int32 leftcurv; /*!< left edge curvature in micro-units */
  169. l_int32 rightcurv; /*!< right edge curvature in micro-units*/
  170. l_int32 nx; /*!< number of sampling pts in x-dir */
  171. l_int32 ny; /*!< number of sampling pts in y-dir */
  172. l_int32 hasref; /*!< 0 if normal; 1 if has a refpage */
  173. l_int32 refpage; /*!< page with disparity model to use */
  174. l_int32 vsuccess; /*!< sets to 1 if vert disparity builds */
  175. l_int32 hsuccess; /*!< sets to 1 if horiz disparity builds */
  176. l_int32 ysuccess; /*!< sets to 1 if slope disparity builds */
  177. l_int32 vvalid; /*!< sets to 1 if valid vert disparity */
  178. l_int32 hvalid; /*!< sets to 1 if valid horiz disparity */
  179. l_int32 skip_horiz; /*!< if 1, skip horiz disparity */
  180. /*!< correction */
  181. l_int32 debug; /*!< set to 1 if debug output requested */
  182. };
  183. typedef struct L_Dewarp L_DEWARP;
  184. #endif /* LEPTONICA_DEWARP_H */