halftone.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. // Copyright (C) 2004-2025 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 <assert.h>
  24. struct fz_halftone
  25. {
  26. int refs;
  27. int n;
  28. fz_pixmap *comp[1];
  29. };
  30. static fz_halftone *
  31. fz_new_halftone(fz_context *ctx, int comps)
  32. {
  33. fz_halftone *ht;
  34. int i;
  35. ht = Memento_label(fz_malloc(ctx, sizeof(fz_halftone) + (comps-1)*sizeof(fz_pixmap *)), "fz_halftone");
  36. ht->refs = 1;
  37. ht->n = comps;
  38. for (i = 0; i < comps; i++)
  39. ht->comp[i] = NULL;
  40. return ht;
  41. }
  42. fz_halftone *
  43. fz_keep_halftone(fz_context *ctx, fz_halftone *ht)
  44. {
  45. return fz_keep_imp(ctx, ht, &ht->refs);
  46. }
  47. void
  48. fz_drop_halftone(fz_context *ctx, fz_halftone *ht)
  49. {
  50. int i;
  51. if (fz_drop_imp(ctx, ht, &ht->refs))
  52. {
  53. for (i = 0; i < ht->n; i++)
  54. fz_drop_pixmap(ctx, ht->comp[i]);
  55. fz_free(ctx, ht);
  56. }
  57. }
  58. /* Default mono halftone, lifted from Ghostscript. */
  59. /* The 0x00 entry has been changed to 0x01 to avoid problems with white
  60. * pixels appearing in the output; as we use < 0 should not appear in the
  61. * array. I think that gs scales this slightly and hence never actually uses
  62. * the raw values here. */
  63. static unsigned char mono_ht[] =
  64. {
  65. 0x0E, 0x8E, 0x2E, 0xAE, 0x06, 0x86, 0x26, 0xA6, 0x0C, 0x8C, 0x2C, 0xAC, 0x04, 0x84, 0x24, 0xA4,
  66. 0xCE, 0x4E, 0xEE, 0x6E, 0xC6, 0x46, 0xE6, 0x66, 0xCC, 0x4C, 0xEC, 0x6C, 0xC4, 0x44, 0xE4, 0x64,
  67. 0x3E, 0xBE, 0x1E, 0x9E, 0x36, 0xB6, 0x16, 0x96, 0x3C, 0xBC, 0x1C, 0x9C, 0x34, 0xB4, 0x14, 0x94,
  68. 0xFE, 0x7E, 0xDE, 0x5E, 0xF6, 0x76, 0xD6, 0x56, 0xFC, 0x7C, 0xDC, 0x5C, 0xF4, 0x74, 0xD4, 0x54,
  69. 0x01, 0x81, 0x21, 0xA1, 0x09, 0x89, 0x29, 0xA9, 0x03, 0x83, 0x23, 0xA3, 0x0B, 0x8B, 0x2B, 0xAB,
  70. 0xC1, 0x41, 0xE1, 0x61, 0xC9, 0x49, 0xE9, 0x69, 0xC3, 0x43, 0xE3, 0x63, 0xCB, 0x4B, 0xEB, 0x6B,
  71. 0x31, 0xB1, 0x11, 0x91, 0x39, 0xB9, 0x19, 0x99, 0x33, 0xB3, 0x13, 0x93, 0x3B, 0xBB, 0x1B, 0x9B,
  72. 0xF1, 0x71, 0xD1, 0x51, 0xF9, 0x79, 0xD9, 0x59, 0xF3, 0x73, 0xD3, 0x53, 0xFB, 0x7B, 0xDB, 0x5B,
  73. 0x0D, 0x8D, 0x2D, 0xAD, 0x05, 0x85, 0x25, 0xA5, 0x0F, 0x8F, 0x2F, 0xAF, 0x07, 0x87, 0x27, 0xA7,
  74. 0xCD, 0x4D, 0xED, 0x6D, 0xC5, 0x45, 0xE5, 0x65, 0xCF, 0x4F, 0xEF, 0x6F, 0xC7, 0x47, 0xE7, 0x67,
  75. 0x3D, 0xBD, 0x1D, 0x9D, 0x35, 0xB5, 0x15, 0x95, 0x3F, 0xBF, 0x1F, 0x9F, 0x37, 0xB7, 0x17, 0x97,
  76. 0xFD, 0x7D, 0xDD, 0x5D, 0xF5, 0x75, 0xD5, 0x55, 0xFF, 0x7F, 0xDF, 0x5F, 0xF7, 0x77, 0xD7, 0x57,
  77. 0x02, 0x82, 0x22, 0xA2, 0x0A, 0x8A, 0x2A, 0xAA, 0x01 /*0x00*/, 0x80, 0x20, 0xA0, 0x08, 0x88, 0x28, 0xA8,
  78. 0xC2, 0x42, 0xE2, 0x62, 0xCA, 0x4A, 0xEA, 0x6A, 0xC0, 0x40, 0xE0, 0x60, 0xC8, 0x48, 0xE8, 0x68,
  79. 0x32, 0xB2, 0x12, 0x92, 0x3A, 0xBA, 0x1A, 0x9A, 0x30, 0xB0, 0x10, 0x90, 0x38, 0xB8, 0x18, 0x98,
  80. 0xF2, 0x72, 0xD2, 0x52, 0xFA, 0x7A, 0xDA, 0x5A, 0xF0, 0x70, 0xD0, 0x50, 0xF8, 0x78, 0xD8, 0x58
  81. };
  82. fz_halftone *fz_default_halftone(fz_context *ctx, int num_comps)
  83. {
  84. fz_halftone *ht = fz_new_halftone(ctx, num_comps);
  85. fz_try(ctx)
  86. {
  87. int i;
  88. for (i = 0; i < num_comps; i++)
  89. ht->comp[i] = fz_new_pixmap_with_data(ctx, NULL, 16, 16, NULL, 1, 16, mono_ht);
  90. }
  91. fz_catch(ctx)
  92. {
  93. fz_drop_halftone(ctx, ht);
  94. fz_rethrow(ctx);
  95. }
  96. return ht;
  97. }
  98. /* Finally, code to actually perform halftoning. */
  99. static void make_ht_line(unsigned char *buf, fz_halftone *ht, int x, int y, int w)
  100. {
  101. int k, n;
  102. n = ht->n;
  103. for (k = 0; k < n; k++)
  104. {
  105. fz_pixmap *tile = ht->comp[k];
  106. unsigned char *b = buf++;
  107. unsigned char *t;
  108. unsigned char *tbase;
  109. int px = x + tile->x;
  110. int py = y + tile->y;
  111. int tw = tile->w;
  112. int th = tile->h;
  113. int w2 = w;
  114. int len;
  115. px = px % tw;
  116. if (px < 0)
  117. px += tw;
  118. py = py % th;
  119. if (py < 0)
  120. py += th;
  121. assert(tile->n == 1);
  122. /* Left hand section; from x to tile width */
  123. tbase = tile->samples + (unsigned int)(py * tw);
  124. t = tbase + px;
  125. len = tw - px;
  126. if (len > w2)
  127. len = w2;
  128. w2 -= len;
  129. while (len--)
  130. {
  131. *b = *t++;
  132. b += n;
  133. }
  134. /* Centre section - complete copies */
  135. w2 -= tw;
  136. while (w2 >= 0)
  137. {
  138. len = tw;
  139. t = tbase;
  140. while (len--)
  141. {
  142. *b = *t++;
  143. b += n;
  144. }
  145. w2 -= tw;
  146. }
  147. w2 += tw;
  148. /* Right hand section - stragglers */
  149. t = tbase;
  150. while (w2--)
  151. {
  152. *b = *t++;
  153. b += n;
  154. }
  155. }
  156. }
  157. /* Inner mono thresholding code */
  158. typedef void (threshold_fn)(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len);
  159. #ifdef ARCH_ARM
  160. static void
  161. do_threshold_1(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len)
  162. __attribute__((naked));
  163. static void
  164. do_threshold_1(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len)
  165. {
  166. asm volatile(
  167. ENTER_ARM
  168. // Store one more reg that required to keep double stack alignment
  169. ".syntax unified\n"
  170. "stmfd r13!,{r4-r7,r9,r14} \n"
  171. "@ r0 = ht_line \n"
  172. "@ r1 = pixmap \n"
  173. "@ r2 = out \n"
  174. "@ r3 = w \n"
  175. "@ <> = ht_len \n"
  176. "ldr r9, [r13,#6*4] @ r9 = ht_len \n"
  177. "subs r3, r3, #7 @ r3 = w -= 7 \n"
  178. "ble 2f @ while (w > 0) { \n"
  179. "mov r12,r9 @ r12= l = ht_len \n"
  180. "b 1f \n"
  181. "9: \n"
  182. "strb r14,[r2], #1 @ *out++ = 0 \n"
  183. "subs r12,r12,#8 @ r12 = l -= 8 \n"
  184. "moveq r12,r9 @ if(l==0) l = ht_len \n"
  185. "subeq r0, r0, r9 @ ht_line -= l \n"
  186. "subs r3, r3, #8 @ w -= 8 \n"
  187. "ble 2f @ } \n"
  188. "1: \n"
  189. "ldr r14,[r1], #4 @ r14= pixmap[0..3] \n"
  190. "ldr r5, [r1], #4 @ r5 = pixmap[4..7] \n"
  191. "ldrb r4, [r0], #8 @ r0 = ht_line += 8 \n"
  192. "adds r14,r14,#1 @ set eq iff r14=-1 \n"
  193. "addseq r5, r5, #1 @ set eq iff r14=r5=-1 \n"
  194. "beq 9b @ white \n"
  195. "ldrb r5, [r1, #-8] @ r5 = pixmap[0] \n"
  196. "ldrb r6, [r0, #-7] @ r6 = ht_line[1] \n"
  197. "ldrb r7, [r1, #-7] @ r7 = pixmap[1] \n"
  198. "mov r14,#0 @ r14= h = 0 \n"
  199. "cmp r5, r4 @ if (r5 < r4) \n"
  200. "orrlt r14,r14,#0x80 @ h |= 0x80 \n"
  201. "ldrb r4, [r0, #-6] @ r4 = ht_line[2] \n"
  202. "ldrb r5, [r1, #-6] @ r5 = pixmap[2] \n"
  203. "cmp r7, r6 @ if (r7 < r6) \n"
  204. "orrlt r14,r14,#0x40 @ h |= 0x40 \n"
  205. "ldrb r6, [r0, #-5] @ r6 = ht_line[3] \n"
  206. "ldrb r7, [r1, #-5] @ r7 = pixmap[3] \n"
  207. "cmp r5, r4 @ if (r5 < r4) \n"
  208. "orrlt r14,r14,#0x20 @ h |= 0x20 \n"
  209. "ldrb r4, [r0, #-4] @ r4 = ht_line[4] \n"
  210. "ldrb r5, [r1, #-4] @ r5 = pixmap[4] \n"
  211. "cmp r7, r6 @ if (r7 < r6) \n"
  212. "orrlt r14,r14,#0x10 @ h |= 0x10 \n"
  213. "ldrb r6, [r0, #-3] @ r6 = ht_line[5] \n"
  214. "ldrb r7, [r1, #-3] @ r7 = pixmap[5] \n"
  215. "cmp r5, r4 @ if (r5 < r4) \n"
  216. "orrlt r14,r14,#0x08 @ h |= 0x08 \n"
  217. "ldrb r4, [r0, #-2] @ r4 = ht_line[6] \n"
  218. "ldrb r5, [r1, #-2] @ r5 = pixmap[6] \n"
  219. "cmp r7, r6 @ if (r7 < r6) \n"
  220. "orrlt r14,r14,#0x04 @ h |= 0x04 \n"
  221. "ldrb r6, [r0, #-1] @ r6 = ht_line[7] \n"
  222. "ldrb r7, [r1, #-1] @ r7 = pixmap[7] \n"
  223. "cmp r5, r4 @ if (r5 < r4) \n"
  224. "orrlt r14,r14,#0x02 @ h |= 0x02 \n"
  225. "cmp r7, r6 @ if (r7 < r6) \n"
  226. "orrlt r14,r14,#0x01 @ h |= 0x01 \n"
  227. "subs r12,r12,#8 @ r12 = l -= 8 \n"
  228. "strb r14,[r2], #1 @ *out++ = h \n"
  229. "moveq r12,r9 @ if(l==0) l = ht_len \n"
  230. "subeq r0, r0, r9 @ ht_line -= l \n"
  231. "subs r3, r3, #8 @ w -= 8 \n"
  232. "bgt 1b @ } \n"
  233. "2: \n"
  234. "adds r3, r3, #7 @ w += 7 \n"
  235. "ble 4f @ if (w >= 0) { \n"
  236. "ldrb r4, [r0], #1 @ r4 = ht_line[0] \n"
  237. "ldrb r5, [r1], #1 @ r5 = pixmap[0] \n"
  238. "mov r14, #0 @ r14= h = 0 \n"
  239. "cmp r5, r4 @ if (r5 < r4) \n"
  240. "orrlt r14,r14,#0x80 @ h |= 0x80 \n"
  241. "cmp r3, #1 @ \n"
  242. "ldrbgt r4, [r0], #1 @ r6 = ht_line[1] \n"
  243. "ldrbgt r5, [r1], #1 @ r7 = pixmap[1] \n"
  244. "ble 3f @ \n"
  245. "cmp r5, r4 @ if (r5 < r4) \n"
  246. "orrlt r14,r14,#0x40 @ h |= 0x40 \n"
  247. "cmp r3, #2 @ \n"
  248. "ldrbgt r4, [r0], #1 @ r6 = ht_line[2] \n"
  249. "ldrbgt r5, [r1], #1 @ r7 = pixmap[2] \n"
  250. "ble 3f @ \n"
  251. "cmp r5, r4 @ if (r5 < r4) \n"
  252. "orrlt r14,r14,#0x20 @ h |= 0x20 \n"
  253. "cmp r3, #3 @ \n"
  254. "ldrbgt r4, [r0], #1 @ r6 = ht_line[3] \n"
  255. "ldrbgt r5, [r1], #1 @ r7 = pixmap[3] \n"
  256. "ble 3f @ \n"
  257. "cmp r5, r4 @ if (r5 < r4) \n"
  258. "orrlt r14,r14,#0x10 @ h |= 0x10 \n"
  259. "cmp r3, #4 @ \n"
  260. "ldrbgt r4, [r0], #1 @ r6 = ht_line[4] \n"
  261. "ldrbgt r5, [r1], #1 @ r7 = pixmap[4] \n"
  262. "ble 3f @ \n"
  263. "cmp r5, r4 @ if (r5 < r4) \n"
  264. "orrlt r14,r14,#0x08 @ h |= 0x08 \n"
  265. "cmp r3, #5 @ \n"
  266. "ldrbgt r4, [r0], #1 @ r6 = ht_line[5] \n"
  267. "ldrbgt r5, [r1], #1 @ r7 = pixmap[5] \n"
  268. "ble 3f @ \n"
  269. "cmp r5, r4 @ if (r5 < r4) \n"
  270. "orrlt r14,r14,#0x04 @ h |= 0x04 \n"
  271. "cmp r3, #6 @ \n"
  272. "ldrbgt r4, [r0], #1 @ r6 = ht_line[6] \n"
  273. "ldrbgt r5, [r1], #1 @ r7 = pixmap[6] \n"
  274. "ble 3f @ \n"
  275. "cmp r5, r4 @ if (r5 < r4) \n"
  276. "orrlt r14,r14,#0x02 @ h |= 0x02 \n"
  277. "3: \n"
  278. "strb r14,[r2] @ *out = h \n"
  279. "4: \n"
  280. "ldmfd r13!,{r4-r7,r9,PC} @ pop, return to thumb \n"
  281. ENTER_THUMB
  282. );
  283. }
  284. #else
  285. static void do_threshold_1(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len)
  286. {
  287. int h;
  288. int l = ht_len;
  289. w -= 7;
  290. while (w > 0)
  291. {
  292. h = 0;
  293. if (pixmap[0] < ht_line[0])
  294. h |= 0x80;
  295. if (pixmap[1] < ht_line[1])
  296. h |= 0x40;
  297. if (pixmap[2] < ht_line[2])
  298. h |= 0x20;
  299. if (pixmap[3] < ht_line[3])
  300. h |= 0x10;
  301. if (pixmap[4] < ht_line[4])
  302. h |= 0x08;
  303. if (pixmap[5] < ht_line[5])
  304. h |= 0x04;
  305. if (pixmap[6] < ht_line[6])
  306. h |= 0x02;
  307. if (pixmap[7] < ht_line[7])
  308. h |= 0x01;
  309. pixmap += 8;
  310. ht_line += 8;
  311. l -= 8;
  312. if (l == 0)
  313. {
  314. l = ht_len;
  315. ht_line -= ht_len;
  316. }
  317. *out++ = h;
  318. w -= 8;
  319. }
  320. if (w > -7)
  321. {
  322. h = 0;
  323. if (pixmap[0] < ht_line[0])
  324. h |= 0x80;
  325. if (w > -6 && pixmap[1] < ht_line[1])
  326. h |= 0x40;
  327. if (w > -5 && pixmap[2] < ht_line[2])
  328. h |= 0x20;
  329. if (w > -4 && pixmap[3] < ht_line[3])
  330. h |= 0x10;
  331. if (w > -3 && pixmap[4] < ht_line[4])
  332. h |= 0x08;
  333. if (w > -2 && pixmap[5] < ht_line[5])
  334. h |= 0x04;
  335. if (w > -1 && pixmap[6] < ht_line[6])
  336. h |= 0x02;
  337. *out++ = h;
  338. }
  339. }
  340. #endif
  341. /*
  342. Note that the tests in do_threshold_4 are inverted compared to those
  343. in do_threshold_1. This is to allow for the fact that the CMYK
  344. contone renderings have white = 0, whereas rgb, and greyscale have
  345. white = 0xFF. Reversing these tests enables us to maintain that
  346. BlackIs1 in bitmaps.
  347. */
  348. #ifdef ARCH_ARM
  349. static void
  350. do_threshold_4(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len)
  351. __attribute__((naked));
  352. static void
  353. do_threshold_4(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len)
  354. {
  355. asm volatile(
  356. ENTER_ARM
  357. // Store one more reg that required to keep double stack alignment
  358. "stmfd r13!,{r4-r7,r9,r14} \n"
  359. "@ r0 = ht_line \n"
  360. "@ r1 = pixmap \n"
  361. "@ r2 = out \n"
  362. "@ r3 = w \n"
  363. "@ <> = ht_len \n"
  364. "ldr r9, [r13,#6*4] @ r9 = ht_len \n"
  365. "subs r3, r3, #1 @ r3 = w -= 1 \n"
  366. "ble 2f @ while (w > 0) { \n"
  367. "mov r12,r9 @ r12= l = ht_len \n"
  368. "b 1f @ \n"
  369. "9: @ \n"
  370. "strb r14,[r2], #1 @ *out++ = h \n"
  371. "subs r12,r12,#2 @ r12 = l -= 2 \n"
  372. "moveq r12,r9 @ if(l==0) l = ht_len \n"
  373. "subeq r0, r0, r9, LSL #2 @ ht_line -= l \n"
  374. "subs r3, r3, #2 @ w -= 2 \n"
  375. "beq 2f @ } \n"
  376. "blt 3f @ \n"
  377. "1: \n"
  378. "ldr r5, [r1], #4 @ r5 = pixmap[0..3] \n"
  379. "ldr r7, [r1], #4 @ r7 = pixmap[4..7] \n"
  380. "add r0, r0, #8 @ r0 = ht_line += 8 \n"
  381. "mov r14,#0 @ r14= h = 0 \n"
  382. "orrs r5, r5, r7 @ if (r5 | r7 == 0) \n"
  383. "beq 9b @ white \n"
  384. "ldrb r4, [r0, #-8] @ r4 = ht_line[0] \n"
  385. "ldrb r5, [r1, #-8] @ r5 = pixmap[0] \n"
  386. "ldrb r6, [r0, #-7] @ r6 = ht_line[1] \n"
  387. "ldrb r7, [r1, #-7] @ r7 = pixmap[1] \n"
  388. "cmp r4, r5 @ if (r4 < r5) \n"
  389. "orrle r14,r14,#0x80 @ h |= 0x80 \n"
  390. "ldrb r4, [r0, #-6] @ r4 = ht_line[2] \n"
  391. "ldrb r5, [r1, #-6] @ r5 = pixmap[2] \n"
  392. "cmp r6, r7 @ if (r6 < r7) \n"
  393. "orrle r14,r14,#0x40 @ h |= 0x40 \n"
  394. "ldrb r6, [r0, #-5] @ r6 = ht_line[3] \n"
  395. "ldrb r7, [r1, #-5] @ r7 = pixmap[3] \n"
  396. "cmp r4, r5 @ if (r4 < r5) \n"
  397. "orrle r14,r14,#0x20 @ h |= 0x20 \n"
  398. "ldrb r4, [r0, #-4] @ r4 = ht_line[4] \n"
  399. "ldrb r5, [r1, #-4] @ r5 = pixmap[4] \n"
  400. "cmp r6, r7 @ if (r6 < r7) \n"
  401. "orrle r14,r14,#0x10 @ h |= 0x10 \n"
  402. "ldrb r6, [r0, #-3] @ r6 = ht_line[5] \n"
  403. "ldrb r7, [r1, #-3] @ r7 = pixmap[5] \n"
  404. "cmp r4, r5 @ if (r4 < r5) \n"
  405. "orrle r14,r14,#0x08 @ h |= 0x08 \n"
  406. "ldrb r4, [r0, #-2] @ r4 = ht_line[6] \n"
  407. "ldrb r5, [r1, #-2] @ r5 = pixmap[6] \n"
  408. "cmp r6, r7 @ if (r6 < r7) \n"
  409. "orrle r14,r14,#0x04 @ h |= 0x04 \n"
  410. "ldrb r6, [r0, #-1] @ r6 = ht_line[7] \n"
  411. "ldrb r7, [r1, #-1] @ r7 = pixmap[7] \n"
  412. "cmp r4, r5 @ if (r4 < r5) \n"
  413. "orrle r14,r14,#0x02 @ h |= 0x02 \n"
  414. "cmp r6, r7 @ if (r7 < r6) \n"
  415. "orrle r14,r14,#0x01 @ h |= 0x01 \n"
  416. "subs r12,r12,#2 @ r12 = l -= 2 \n"
  417. "strb r14,[r2], #1 @ *out++ = h \n"
  418. "moveq r12,r9 @ if(l==0) l = ht_len \n"
  419. "subeq r0, r0, r9, LSL #2 @ ht_line -= l \n"
  420. "subs r3, r3, #2 @ w -= 2 \n"
  421. "bgt 1b @ } \n"
  422. "blt 3f @ \n"
  423. "2: \n"
  424. "ldrb r4, [r0], #1 @ r4 = ht_line[0] \n"
  425. "ldrb r5, [r1], #1 @ r5 = pixmap[0] \n"
  426. "mov r14, #0 @ r14= h = 0 \n"
  427. "ldrb r6, [r0], #1 @ r6 = ht_line[1] \n"
  428. "ldrb r7, [r1], #1 @ r7 = pixmap[1] \n"
  429. "cmp r4, r5 @ if (r4 < r5) \n"
  430. "orrle r14,r14,#0x80 @ h |= 0x80 \n"
  431. "ldrb r4, [r0], #1 @ r6 = ht_line[2] \n"
  432. "ldrb r5, [r1], #1 @ r7 = pixmap[2] \n"
  433. "cmp r6, r7 @ if (r6 < r7) \n"
  434. "orrle r14,r14,#0x40 @ h |= 0x40 \n"
  435. "ldrb r6, [r0], #1 @ r6 = ht_line[1] \n"
  436. "ldrb r7, [r1], #1 @ r7 = pixmap[3] \n"
  437. "cmp r4, r5 @ if (r4 < r5) \n"
  438. "orrle r14,r14,#0x20 @ h |= 0x20 \n"
  439. "cmp r6, r7 @ if (r6 < r7) \n"
  440. "orrle r14,r14,#0x10 @ h |= 0x10 \n"
  441. "strb r14,[r2] @ *out = h \n"
  442. "3: \n"
  443. "ldmfd r13!,{r4-r7,r9,PC} @ pop, return to thumb \n"
  444. ENTER_THUMB
  445. );
  446. }
  447. #else
  448. static void do_threshold_4(const unsigned char * FZ_RESTRICT ht_line, const unsigned char * FZ_RESTRICT pixmap, unsigned char * FZ_RESTRICT out, int w, int ht_len)
  449. {
  450. int l = ht_len;
  451. w--;
  452. while (w > 0)
  453. {
  454. int h = 0;
  455. if (pixmap[0] >= ht_line[0])
  456. h |= 0x80;
  457. if (pixmap[1] >= ht_line[1])
  458. h |= 0x40;
  459. if (pixmap[2] >= ht_line[2])
  460. h |= 0x20;
  461. if (pixmap[3] >= ht_line[3])
  462. h |= 0x10;
  463. if (pixmap[4] >= ht_line[4])
  464. h |= 0x08;
  465. if (pixmap[5] >= ht_line[5])
  466. h |= 0x04;
  467. if (pixmap[6] >= ht_line[6])
  468. h |= 0x02;
  469. if (pixmap[7] >= ht_line[7])
  470. h |= 0x01;
  471. *out++ = h;
  472. l -= 2;
  473. if (l == 0)
  474. {
  475. l = ht_len;
  476. ht_line -= ht_len<<2;
  477. }
  478. pixmap += 8;
  479. ht_line += 8;
  480. w -= 2;
  481. }
  482. if (w == 0)
  483. {
  484. int h = 0;
  485. if (pixmap[0] >= ht_line[0])
  486. h |= 0x80;
  487. if (pixmap[1] >= ht_line[1])
  488. h |= 0x40;
  489. if (pixmap[2] >= ht_line[2])
  490. h |= 0x20;
  491. if (pixmap[3] >= ht_line[3])
  492. h |= 0x10;
  493. *out = h;
  494. }
  495. }
  496. #endif
  497. fz_bitmap *fz_new_bitmap_from_pixmap(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht)
  498. {
  499. return fz_new_bitmap_from_pixmap_band(ctx, pix, ht, 0);
  500. }
  501. fz_bitmap *fz_new_bitmap_from_image(fz_context *ctx, fz_image *img, fz_halftone *ht)
  502. {
  503. fz_pixmap *pix = fz_get_pixmap_from_image(ctx, img, NULL, NULL, NULL, NULL);
  504. fz_bitmap *bitmap;
  505. fz_try(ctx)
  506. bitmap = fz_new_bitmap_from_pixmap_band(ctx, pix, ht, 0);
  507. fz_always(ctx)
  508. fz_drop_pixmap(ctx, pix);
  509. fz_catch(ctx)
  510. fz_rethrow(ctx);
  511. return bitmap;
  512. }
  513. void fz_invert_bitmap(fz_context *ctx, fz_bitmap *bmp)
  514. {
  515. unsigned char *s = bmp->samples;
  516. int w, h, w2 = (bmp->w+7)>>3;
  517. for (h = bmp->h; h > 0; h--)
  518. {
  519. unsigned char *t = s;
  520. for (w = w2; w > 0; w--)
  521. *t++ ^= 255;
  522. s += bmp->stride;
  523. }
  524. }
  525. /* TAOCP, vol 2, p337 */
  526. static int gcd(int u, int v)
  527. {
  528. int r;
  529. do
  530. {
  531. if (v == 0)
  532. return u;
  533. r = u % v;
  534. u = v;
  535. v = r;
  536. }
  537. while (1);
  538. }
  539. fz_bitmap *fz_new_bitmap_from_pixmap_band(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht, int band_start)
  540. {
  541. fz_bitmap *out = NULL;
  542. unsigned char *ht_line = NULL;
  543. unsigned char *o, *p;
  544. int w, h, x, y, n, pstride, ostride, lcm, i, alpha;
  545. fz_halftone *ht_ = NULL;
  546. threshold_fn *thresh;
  547. fz_var(ht_line);
  548. if (!pix)
  549. return NULL;
  550. n = pix->n;
  551. alpha = pix->alpha;
  552. /* Treat alpha only as greyscale */
  553. if (n == 1 && alpha)
  554. alpha = 0;
  555. n -= alpha;
  556. if (alpha != 0)
  557. fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap may not have alpha channel to convert to bitmap");
  558. switch(n)
  559. {
  560. case 1:
  561. thresh = do_threshold_1;
  562. break;
  563. case 4:
  564. thresh = do_threshold_4;
  565. break;
  566. default:
  567. fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap must be grayscale or CMYK to convert to bitmap");
  568. }
  569. if (ht == NULL)
  570. ht_ = ht = fz_default_halftone(ctx, n);
  571. /* Find the minimum length for the halftone line. This
  572. * is the LCM of the halftone lengths and 8. (We need a
  573. * multiple of 8 for the unrolled threshold routines - if
  574. * we ever use SSE, we may need longer.) We use the fact
  575. * that LCM(a,b) = a * b / GCD(a,b) and use euclids
  576. * algorithm.
  577. */
  578. lcm = 8;
  579. for (i = 0; i < ht->n; i++)
  580. {
  581. w = ht->comp[i]->w;
  582. lcm = lcm / gcd(lcm, w) * w;
  583. }
  584. fz_try(ctx)
  585. {
  586. ht_line = fz_malloc(ctx, lcm * (size_t)n);
  587. out = fz_new_bitmap(ctx, pix->w, pix->h, n, pix->xres, pix->yres);
  588. o = out->samples;
  589. p = pix->samples;
  590. h = pix->h;
  591. x = pix->x;
  592. y = pix->y + band_start;
  593. w = pix->w;
  594. ostride = out->stride;
  595. pstride = pix->stride;
  596. while (h--)
  597. {
  598. make_ht_line(ht_line, ht, x, y++, lcm);
  599. thresh(ht_line, p, o, w, lcm);
  600. o += ostride;
  601. p += pstride;
  602. }
  603. }
  604. fz_always(ctx)
  605. {
  606. fz_drop_halftone(ctx, ht_);
  607. fz_free(ctx, ht_line);
  608. }
  609. fz_catch(ctx)
  610. fz_rethrow(ctx);
  611. return out;
  612. }