skew.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include <math.h>
  24. #include <assert.h>
  25. #include <limits.h>
  26. #if ARCH_HAS_SSE
  27. #include <emmintrin.h>
  28. #include <smmintrin.h>
  29. #endif
  30. /* Uncomment the following to enable Debugging printfs. */
  31. /* #define DEBUG_PRINT_WORKING */
  32. enum {
  33. NUM_SKEW_COLS = 4,
  34. SKEW_COL_OFFSET = 96,
  35. SKEW_COL_WIDTH = 96,
  36. SKEW_MAX_DIFF = 16
  37. };
  38. typedef struct
  39. {
  40. void *src;
  41. uint32_t src_w;
  42. uint32_t src_h;
  43. uint32_t y;
  44. uint32_t offsets[NUM_SKEW_COLS*2];
  45. uint16_t *tables[NUM_SKEW_COLS*2];
  46. uint32_t corr_height;
  47. } fz_skew;
  48. /* Finalise a rescaler instance. */
  49. static void
  50. fz_drop_skew(fz_context *ctx, fz_skew *skew)
  51. {
  52. if (skew == NULL)
  53. return;
  54. fz_free(ctx, skew->tables[0]);
  55. fz_free(ctx, skew);
  56. }
  57. /* Initialise a skew instance. */
  58. static fz_skew *
  59. fz_new_skew(fz_context *ctx,
  60. unsigned int src_w,
  61. unsigned int src_h)
  62. {
  63. fz_skew *skew = fz_malloc_struct(ctx, fz_skew);
  64. int i;
  65. skew->src_w = src_w;
  66. skew->src_h = src_h;
  67. skew->y = 0;
  68. skew->corr_height = src_h-100; /* FIXME */
  69. fz_try(ctx)
  70. skew->tables[0] = (uint16_t *)fz_malloc(ctx, skew->src_h * sizeof(uint16_t) * NUM_SKEW_COLS * 2);
  71. fz_catch(ctx)
  72. {
  73. fz_drop_skew(ctx, skew);
  74. fz_rethrow(ctx);
  75. }
  76. for (i = 1; i < NUM_SKEW_COLS * 2; i++)
  77. skew->tables[i] = skew->tables[0] + i*skew->src_h;
  78. for (i = 0; i < NUM_SKEW_COLS; i++)
  79. {
  80. int offset = skew->src_w * (i+1) / (NUM_SKEW_COLS+1) - SKEW_COL_WIDTH/2;
  81. skew->offsets[2*i ] = offset - SKEW_COL_OFFSET;
  82. skew->offsets[2*i+1] = offset + SKEW_COL_OFFSET;
  83. }
  84. return skew;
  85. }
  86. static int sum_c(const uint8_t *data)
  87. {
  88. int i;
  89. uint32_t sum = 0;
  90. for (i = SKEW_COL_WIDTH; i > 0; i--)
  91. sum += *data++;
  92. return sum;
  93. }
  94. #if ARCH_HAS_SSE
  95. static inline int sum_sse(const uint8_t *data)
  96. {
  97. __m128i mm0, mm1, mm2, mm3, mm4, mm5;
  98. __m128i mm_zero = _mm_set1_epi32(0);
  99. mm0 = _mm_loadu_si128((const __m128i *)(data )); // mm0 = ppoonnmmllkkjjiihhggffeeddccbbaa
  100. mm1 = _mm_loadu_si128((const __m128i *)(data+16)); // mm1 = ppoonnmmllkkjjiihhggffeeddccbbaa
  101. mm2 = _mm_loadu_si128((const __m128i *)(data+32)); // mm2 = ppoonnmmllkkjjiihhggffeeddccbbaa
  102. mm3 = _mm_loadu_si128((const __m128i *)(data+48)); // mm3 = ppoonnmmllkkjjiihhggffeeddccbbaa
  103. mm4 = _mm_loadu_si128((const __m128i *)(data+64)); // mm4 = ppoonnmmllkkjjiihhggffeeddccbbaa
  104. mm5 = _mm_loadu_si128((const __m128i *)(data+80)); // mm5 = ppoonnmmllkkjjiihhggffeeddccbbaa
  105. mm0 = _mm_sad_epu8(mm0, mm_zero); // Max value in each half is 8*255
  106. mm1 = _mm_sad_epu8(mm1, mm_zero);
  107. mm2 = _mm_sad_epu8(mm2, mm_zero);
  108. mm3 = _mm_sad_epu8(mm3, mm_zero);
  109. mm4 = _mm_sad_epu8(mm4, mm_zero);
  110. mm5 = _mm_sad_epu8(mm5, mm_zero);
  111. mm0 = _mm_add_epi64(mm0, mm1); // Max value in each half is 2*8*255
  112. mm2 = _mm_add_epi64(mm2, mm3); // Max value in each half is 2*8*255
  113. mm4 = _mm_add_epi64(mm4, mm5); // Max value in each half is 2*8*255
  114. mm0 = _mm_add_epi64(mm0, mm2); // Max value in each half is 4*8*255
  115. mm0 = _mm_add_epi64(mm0, mm4); // Max value in each half is 6*8*255
  116. mm0 = _mm_shuffle_epi32(mm0, (2<<2)+0); // Max value in each half is 10*8*255
  117. mm0 = _mm_hadd_epi32(mm0, mm0); // Max value in bottom bits is 20*8*255 - still fits in an unsigned 16bit word.
  118. return _mm_extract_epi16(mm0, 0);
  119. }
  120. #endif
  121. /* Process data: */
  122. static void
  123. fz_skew_process(fz_context *ctx, fz_skew *skew, const uint8_t *input)
  124. {
  125. int i;
  126. int off = skew->y++;
  127. #if ARCH_HAS_SSE
  128. for (i = 0; i < NUM_SKEW_COLS * 2; i++)
  129. skew->tables[i][off] = sum_sse(input + skew->offsets[i]);
  130. #else
  131. for (i = 0; i < NUM_SKEW_COLS * 2; i++)
  132. skew->tables[i][off] = sum_c(input + skew->offsets[i]);
  133. #endif
  134. /* Some debug code; if enabled this writes the summed value back
  135. * in to give us a visual indication of where we are looking for
  136. * correspondences. */
  137. #if 0
  138. for (i = 0; i < NUM_SKEW_COLS * 2; i++) {
  139. int v = (skew->tables[i][off] + (SKEW_COL_WIDTH/2) ) / SKEW_COL_WIDTH;
  140. memset(input + skew->offsets[i], v, SKEW_COL_WIDTH);
  141. }
  142. #endif
  143. }
  144. static double
  145. do_detect_skew(fz_context *ctx, fz_skew *skew)
  146. {
  147. int i, j, h, o, max_at;
  148. int64_t max_sum, corr_at, corr_sum, avg_sum;
  149. float ang;
  150. int64_t avg = SKEW_COL_WIDTH * 255/2;
  151. if (skew == NULL)
  152. return 0;
  153. h = skew->corr_height - 2 * SKEW_MAX_DIFF;
  154. corr_at = 0;
  155. corr_sum = 0;
  156. for (i = 0; i < NUM_SKEW_COLS; i++)
  157. {
  158. max_at = 9999;
  159. max_sum = 0;
  160. avg_sum = 0;
  161. for (o = -SKEW_MAX_DIFF; o <= SKEW_MAX_DIFF; o++)
  162. {
  163. uint16_t *t0 = skew->tables[2*i] + SKEW_MAX_DIFF;
  164. uint16_t *t1 = skew->tables[2*i+1] + SKEW_MAX_DIFF + o;
  165. int64_t sum = 0;
  166. for (j = 0; j < h; j++)
  167. sum += ((int64_t)t0[j]-avg) * ((int64_t)t1[j]-avg);
  168. if (max_sum < sum)
  169. max_sum = sum, max_at = o;
  170. avg_sum += sum;
  171. #ifdef DEBUG_PRINT_WORKING
  172. printf("col %d, offset %d -> %llx\n", i, o, sum);
  173. #endif
  174. }
  175. avg_sum /= (SKEW_MAX_DIFF+1)*2;
  176. #ifdef DEBUG_PRINT_WORKING
  177. ang = (180.0 / 3.1415942) * atan(max_at / (double)(SKEW_COL_OFFSET * 2));
  178. printf("col %d max: offset %d -> %llx ang=%g\n", i, max_at, max_sum, ang);
  179. #endif
  180. /* Subtract the average from the maximum; we judge the significance of a
  181. * match by how far it exceeds the average. max_sum becomes 'significance'. */
  182. max_sum -= avg_sum;
  183. #ifdef DEBUG_PRINT_WORKING
  184. printf("Significance: %llx\n", max_sum - avg_sum);
  185. #endif
  186. corr_at += max_at * max_sum;
  187. corr_sum += max_sum;
  188. }
  189. ang = (180.0 / 3.1415942) * atan(corr_at / (double)(corr_sum * SKEW_COL_OFFSET * 2));
  190. if (ang < -45 || ang > 45)
  191. return 0;
  192. return ang;
  193. }
  194. double fz_detect_skew(fz_context *ctx, fz_pixmap *pix)
  195. {
  196. fz_skew *skew = fz_new_skew(ctx, pix->w, pix->h);
  197. int y;
  198. uint8_t *ptr;
  199. ptrdiff_t stride;
  200. double angle;
  201. fz_pixmap *pix2 = NULL;
  202. fz_var(pix2);
  203. fz_try(ctx)
  204. {
  205. if (pix->n != 1)
  206. {
  207. pix2 = fz_convert_pixmap(ctx, pix, fz_device_gray(ctx), NULL, NULL, fz_default_color_params, 0);
  208. ptr = pix2->samples;
  209. stride = pix2->stride;
  210. }
  211. else
  212. {
  213. ptr = pix->samples;
  214. stride = pix->stride;
  215. }
  216. for (y = pix->h; y > 0; y--)
  217. {
  218. fz_skew_process(ctx, skew, ptr);
  219. ptr += stride;
  220. }
  221. angle = do_detect_skew(ctx, skew);
  222. }
  223. fz_always(ctx)
  224. {
  225. fz_drop_pixmap(ctx, pix2);
  226. fz_drop_skew(ctx, skew);
  227. }
  228. fz_catch(ctx)
  229. fz_rethrow(ctx);
  230. return angle;
  231. }