transition.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <string.h>
  24. /*
  25. FIXME: Currently transitions only work with alpha. Our app only
  26. uses alpha.
  27. */
  28. static int
  29. fade(fz_pixmap *tpix, const fz_pixmap *opix, const fz_pixmap *npix, int time)
  30. {
  31. unsigned char *t, *o, *n;
  32. int size;
  33. int h;
  34. int tstride, ostride, nstride;
  35. if (!tpix || !opix || !npix || tpix->w != opix->w || opix->w != npix->w || tpix->h != opix->h || opix->h != npix->h || tpix->n != opix->n || opix->n != npix->n)
  36. return 0;
  37. h = tpix->h;
  38. size = tpix->w * tpix->n;
  39. ostride = opix->stride - size;
  40. nstride = npix->stride - size;
  41. tstride = tpix->stride - size;
  42. t = tpix->samples;
  43. o = opix->samples;
  44. n = npix->samples;
  45. while (h--)
  46. {
  47. int ww = size;
  48. while (ww-- > 0)
  49. {
  50. int op = *o++;
  51. int np = *n++;
  52. *t++ = ((op<<8) + ((np-op) * time) + 0x80)>>8;
  53. }
  54. o += ostride;
  55. n += nstride;
  56. t += tstride;
  57. }
  58. return 1;
  59. }
  60. static int
  61. blind_horiz(fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time)
  62. {
  63. unsigned char *t, *o, *n;
  64. int blind_height, size, position, y;
  65. int tstride, ostride, nstride;
  66. if (!tpix || !opix || !npix || tpix->w != opix->w || opix->w != npix->w || tpix->h != opix->h || opix->h != npix->h || tpix->n != opix->n || opix->n != npix->n)
  67. return 0;
  68. size = tpix->w * tpix->n;
  69. blind_height = (tpix->h+7) / 8;
  70. position = blind_height * time / 256;
  71. ostride = opix->stride;
  72. nstride = npix->stride;
  73. tstride = tpix->stride;
  74. t = tpix->samples;
  75. o = opix->samples;
  76. n = npix->samples;
  77. for (y = 0; y < tpix->h; y++)
  78. {
  79. memcpy(t, ((y % blind_height) <= position ? n : o), size);
  80. t += tstride;
  81. o += ostride;
  82. n += nstride;
  83. }
  84. return 1;
  85. }
  86. static int
  87. blind_vertical(fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time)
  88. {
  89. unsigned char *t, *o, *n;
  90. int blind_width, size, position, y;
  91. int tstride, ostride, nstride;
  92. if (!tpix || !opix || !npix || tpix->w != opix->w || opix->w != npix->w || tpix->h != opix->h || opix->h != npix->h || tpix->n != opix->n || opix->n != npix->n)
  93. return 0;
  94. size = tpix->w * tpix->n;
  95. blind_width = (tpix->w+7) / 8;
  96. position = blind_width * time / 256;
  97. blind_width *= tpix->n;
  98. position *= tpix->n;
  99. ostride = opix->stride - size;
  100. nstride = npix->stride - size;
  101. tstride = tpix->stride - size;
  102. t = tpix->samples;
  103. o = opix->samples;
  104. n = npix->samples;
  105. for (y = 0; y < tpix->h; y++)
  106. {
  107. int w, x;
  108. x = 0;
  109. while ((w = size - x) > 0)
  110. {
  111. int p;
  112. if (w > blind_width)
  113. w = blind_width;
  114. p = position;
  115. if (p > w)
  116. p = w;
  117. memcpy(t, n, p);
  118. memcpy(t+position, o+position, w - p);
  119. x += blind_width;
  120. t += w;
  121. o += w;
  122. n += w;
  123. }
  124. o += ostride;
  125. n += nstride;
  126. t += tstride;
  127. }
  128. return 1;
  129. }
  130. static int
  131. wipe_tb(fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time)
  132. {
  133. unsigned char *t, *o, *n;
  134. int size, position, y;
  135. int tstride, ostride, nstride;
  136. if (!tpix || !opix || !npix || tpix->w != opix->w || opix->w != npix->w || tpix->h != opix->h || opix->h != npix->h || tpix->n != opix->n || opix->n != npix->n)
  137. return 0;
  138. size = tpix->w * tpix->n;
  139. position = tpix->h * time / 256;
  140. ostride = opix->stride;
  141. nstride = npix->stride;
  142. tstride = tpix->stride;
  143. t = tpix->samples;
  144. o = opix->samples;
  145. n = npix->samples;
  146. for (y = 0; y < position; y++)
  147. {
  148. memcpy(t, n, size);
  149. t += tstride;
  150. o += ostride;
  151. n += nstride;
  152. }
  153. for (; y < tpix->h; y++)
  154. {
  155. memcpy(t, o, size);
  156. t += tstride;
  157. o += ostride;
  158. n += nstride;
  159. }
  160. return 1;
  161. }
  162. static int
  163. wipe_lr(fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time)
  164. {
  165. unsigned char *t, *o, *n;
  166. int size, position, y;
  167. int tstride, ostride, nstride;
  168. if (!tpix || !opix || !npix || tpix->w != opix->w || opix->w != npix->w || tpix->h != opix->h || opix->h != npix->h || tpix->n != opix->n || opix->n != npix->n)
  169. return 0;
  170. size = tpix->w * tpix->n;
  171. position = tpix->w * time / 256;
  172. position *= tpix->n;
  173. ostride = opix->stride;
  174. nstride = npix->stride;
  175. tstride = tpix->stride;
  176. t = tpix->samples;
  177. o = opix->samples + position;
  178. n = npix->samples;
  179. for (y = 0; y < tpix->h; y++)
  180. {
  181. memcpy(t, n, position);
  182. memcpy(t+position, o, size-position);
  183. t += tstride;
  184. o += ostride;
  185. n += nstride;
  186. }
  187. return 1;
  188. }
  189. int fz_generate_transition(fz_context *ctx, fz_pixmap *tpix, fz_pixmap *opix, fz_pixmap *npix, int time, fz_transition *trans)
  190. {
  191. switch (trans->type)
  192. {
  193. default:
  194. case FZ_TRANSITION_FADE:
  195. return fade(tpix, opix, npix, time);
  196. case FZ_TRANSITION_BLINDS:
  197. if (trans->vertical)
  198. return blind_vertical(tpix, opix, npix, time);
  199. else
  200. return blind_horiz(tpix, opix, npix, time);
  201. case FZ_TRANSITION_WIPE:
  202. switch (((trans->direction + 45 + 360) % 360) / 90)
  203. {
  204. default:
  205. case 0: return wipe_lr(tpix, opix, npix, time);
  206. case 1: return wipe_tb(tpix, npix, opix, 256-time);
  207. case 2: return wipe_lr(tpix, npix, opix, 256-time);
  208. case 3: return wipe_tb(tpix, opix, npix, time);
  209. }
  210. }
  211. }