vnc-enc-zywrle.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE 'ZYWRLE' VNC CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A FOLLOWING BSD-STYLE SOURCE LICENSE. *
  7. * PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE 'ZYWRLE' VNC CODEC SOURCE CODE IS (C) COPYRIGHT 2006 *
  10. * BY Hitachi Systems & Services, Ltd. *
  11. * (Noriaki Yamazaki, Research & Development Center) *
  12. * *
  13. * *
  14. ********************************************************************
  15. Redistribution and use in source and binary forms, with or without
  16. modification, are permitted provided that the following conditions
  17. are met:
  18. - Redistributions of source code must retain the above copyright
  19. notice, this list of conditions and the following disclaimer.
  20. - Redistributions in binary form must reproduce the above copyright
  21. notice, this list of conditions and the following disclaimer in the
  22. documentation and/or other materials provided with the distribution.
  23. - Neither the name of the Hitachi Systems & Services, Ltd. nor
  24. the names of its contributors may be used to endorse or promote
  25. products derived from this software without specific prior written
  26. permission.
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
  31. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. ********************************************************************/
  39. #ifndef VNC_ENC_ZYWRLE_H
  40. #define VNC_ENC_ZYWRLE_H
  41. /* Tables for Coefficients filtering. */
  42. #ifndef ZYWRLE_QUANTIZE
  43. /* Type A:lower bit omitting of EZW style. */
  44. static const unsigned int zywrle_param[3][3]={
  45. {0x0000F000, 0x00000000, 0x00000000},
  46. {0x0000C000, 0x00F0F0F0, 0x00000000},
  47. {0x0000C000, 0x00C0C0C0, 0x00F0F0F0},
  48. /* {0x0000FF00, 0x00000000, 0x00000000},
  49. {0x0000FF00, 0x00FFFFFF, 0x00000000},
  50. {0x0000FF00, 0x00FFFFFF, 0x00FFFFFF}, */
  51. };
  52. #else
  53. /* Type B:Non liner quantization filter. */
  54. static const int8_t zywrle_conv[4][256]={
  55. { /* bi=5, bo=5 r=0.0:PSNR=24.849 */
  56. 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0, 0, 0, 0,
  61. 0, 0, 0, 0, 0, 0, 0, 0,
  62. 0, 0, 0, 0, 0, 0, 0, 0,
  63. 0, 0, 0, 0, 0, 0, 0, 0,
  64. 0, 0, 0, 0, 0, 0, 0, 0,
  65. 0, 0, 0, 0, 0, 0, 0, 0,
  66. 0, 0, 0, 0, 0, 0, 0, 0,
  67. 0, 0, 0, 0, 0, 0, 0, 0,
  68. 0, 0, 0, 0, 0, 0, 0, 0,
  69. 0, 0, 0, 0, 0, 0, 0, 0,
  70. 0, 0, 0, 0, 0, 0, 0, 0,
  71. 0, 0, 0, 0, 0, 0, 0, 0,
  72. 0, 0, 0, 0, 0, 0, 0, 0,
  73. 0, 0, 0, 0, 0, 0, 0, 0,
  74. 0, 0, 0, 0, 0, 0, 0, 0,
  75. 0, 0, 0, 0, 0, 0, 0, 0,
  76. 0, 0, 0, 0, 0, 0, 0, 0,
  77. 0, 0, 0, 0, 0, 0, 0, 0,
  78. 0, 0, 0, 0, 0, 0, 0, 0,
  79. 0, 0, 0, 0, 0, 0, 0, 0,
  80. 0, 0, 0, 0, 0, 0, 0, 0,
  81. 0, 0, 0, 0, 0, 0, 0, 0,
  82. 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0,
  86. 0, 0, 0, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0,
  88. },
  89. { /* bi=5, bo=5 r=2.0:PSNR=74.031 */
  90. 0, 0, 0, 0, 0, 0, 0, 0,
  91. 0, 0, 0, 0, 0, 0, 0, 0,
  92. 0, 0, 0, 0, 0, 0, 0, 32,
  93. 32, 32, 32, 32, 32, 32, 32, 32,
  94. 32, 32, 32, 32, 32, 32, 32, 32,
  95. 48, 48, 48, 48, 48, 48, 48, 48,
  96. 48, 48, 48, 56, 56, 56, 56, 56,
  97. 56, 56, 56, 56, 64, 64, 64, 64,
  98. 64, 64, 64, 64, 72, 72, 72, 72,
  99. 72, 72, 72, 72, 80, 80, 80, 80,
  100. 80, 80, 88, 88, 88, 88, 88, 88,
  101. 88, 88, 88, 88, 88, 88, 96, 96,
  102. 96, 96, 96, 104, 104, 104, 104, 104,
  103. 104, 104, 104, 104, 104, 112, 112, 112,
  104. 112, 112, 112, 112, 112, 112, 120, 120,
  105. 120, 120, 120, 120, 120, 120, 120, 120,
  106. 0, -120, -120, -120, -120, -120, -120, -120,
  107. -120, -120, -120, -112, -112, -112, -112, -112,
  108. -112, -112, -112, -112, -104, -104, -104, -104,
  109. -104, -104, -104, -104, -104, -104, -96, -96,
  110. -96, -96, -96, -88, -88, -88, -88, -88,
  111. -88, -88, -88, -88, -88, -88, -88, -80,
  112. -80, -80, -80, -80, -80, -72, -72, -72,
  113. -72, -72, -72, -72, -72, -64, -64, -64,
  114. -64, -64, -64, -64, -64, -56, -56, -56,
  115. -56, -56, -56, -56, -56, -56, -48, -48,
  116. -48, -48, -48, -48, -48, -48, -48, -48,
  117. -48, -32, -32, -32, -32, -32, -32, -32,
  118. -32, -32, -32, -32, -32, -32, -32, -32,
  119. -32, -32, 0, 0, 0, 0, 0, 0,
  120. 0, 0, 0, 0, 0, 0, 0, 0,
  121. 0, 0, 0, 0, 0, 0, 0, 0,
  122. },
  123. { /* bi=5, bo=4 r=2.0:PSNR=64.441 */
  124. 0, 0, 0, 0, 0, 0, 0, 0,
  125. 0, 0, 0, 0, 0, 0, 0, 0,
  126. 0, 0, 0, 0, 0, 0, 0, 0,
  127. 0, 0, 0, 0, 0, 0, 0, 0,
  128. 48, 48, 48, 48, 48, 48, 48, 48,
  129. 48, 48, 48, 48, 48, 48, 48, 48,
  130. 48, 48, 48, 48, 48, 48, 48, 48,
  131. 64, 64, 64, 64, 64, 64, 64, 64,
  132. 64, 64, 64, 64, 64, 64, 64, 64,
  133. 80, 80, 80, 80, 80, 80, 80, 80,
  134. 80, 80, 80, 80, 80, 88, 88, 88,
  135. 88, 88, 88, 88, 88, 88, 88, 88,
  136. 104, 104, 104, 104, 104, 104, 104, 104,
  137. 104, 104, 104, 112, 112, 112, 112, 112,
  138. 112, 112, 112, 112, 120, 120, 120, 120,
  139. 120, 120, 120, 120, 120, 120, 120, 120,
  140. 0, -120, -120, -120, -120, -120, -120, -120,
  141. -120, -120, -120, -120, -120, -112, -112, -112,
  142. -112, -112, -112, -112, -112, -112, -104, -104,
  143. -104, -104, -104, -104, -104, -104, -104, -104,
  144. -104, -88, -88, -88, -88, -88, -88, -88,
  145. -88, -88, -88, -88, -80, -80, -80, -80,
  146. -80, -80, -80, -80, -80, -80, -80, -80,
  147. -80, -64, -64, -64, -64, -64, -64, -64,
  148. -64, -64, -64, -64, -64, -64, -64, -64,
  149. -64, -48, -48, -48, -48, -48, -48, -48,
  150. -48, -48, -48, -48, -48, -48, -48, -48,
  151. -48, -48, -48, -48, -48, -48, -48, -48,
  152. -48, 0, 0, 0, 0, 0, 0, 0,
  153. 0, 0, 0, 0, 0, 0, 0, 0,
  154. 0, 0, 0, 0, 0, 0, 0, 0,
  155. 0, 0, 0, 0, 0, 0, 0, 0,
  156. },
  157. { /* bi=5, bo=2 r=2.0:PSNR=43.175 */
  158. 0, 0, 0, 0, 0, 0, 0, 0,
  159. 0, 0, 0, 0, 0, 0, 0, 0,
  160. 0, 0, 0, 0, 0, 0, 0, 0,
  161. 0, 0, 0, 0, 0, 0, 0, 0,
  162. 0, 0, 0, 0, 0, 0, 0, 0,
  163. 0, 0, 0, 0, 0, 0, 0, 0,
  164. 0, 0, 0, 0, 0, 0, 0, 0,
  165. 0, 0, 0, 0, 0, 0, 0, 0,
  166. 88, 88, 88, 88, 88, 88, 88, 88,
  167. 88, 88, 88, 88, 88, 88, 88, 88,
  168. 88, 88, 88, 88, 88, 88, 88, 88,
  169. 88, 88, 88, 88, 88, 88, 88, 88,
  170. 88, 88, 88, 88, 88, 88, 88, 88,
  171. 88, 88, 88, 88, 88, 88, 88, 88,
  172. 88, 88, 88, 88, 88, 88, 88, 88,
  173. 88, 88, 88, 88, 88, 88, 88, 88,
  174. 0, -88, -88, -88, -88, -88, -88, -88,
  175. -88, -88, -88, -88, -88, -88, -88, -88,
  176. -88, -88, -88, -88, -88, -88, -88, -88,
  177. -88, -88, -88, -88, -88, -88, -88, -88,
  178. -88, -88, -88, -88, -88, -88, -88, -88,
  179. -88, -88, -88, -88, -88, -88, -88, -88,
  180. -88, -88, -88, -88, -88, -88, -88, -88,
  181. -88, -88, -88, -88, -88, -88, -88, -88,
  182. -88, 0, 0, 0, 0, 0, 0, 0,
  183. 0, 0, 0, 0, 0, 0, 0, 0,
  184. 0, 0, 0, 0, 0, 0, 0, 0,
  185. 0, 0, 0, 0, 0, 0, 0, 0,
  186. 0, 0, 0, 0, 0, 0, 0, 0,
  187. 0, 0, 0, 0, 0, 0, 0, 0,
  188. 0, 0, 0, 0, 0, 0, 0, 0,
  189. 0, 0, 0, 0, 0, 0, 0, 0,
  190. }
  191. };
  192. static const int8_t *zywrle_param[3][3][3]={
  193. {{zywrle_conv[0], zywrle_conv[2], zywrle_conv[0]},
  194. {zywrle_conv[0], zywrle_conv[0], zywrle_conv[0]},
  195. {zywrle_conv[0], zywrle_conv[0], zywrle_conv[0]}},
  196. {{zywrle_conv[0], zywrle_conv[3], zywrle_conv[0]},
  197. {zywrle_conv[1], zywrle_conv[1], zywrle_conv[1]},
  198. {zywrle_conv[0], zywrle_conv[0], zywrle_conv[0]}},
  199. {{zywrle_conv[0], zywrle_conv[3], zywrle_conv[0]},
  200. {zywrle_conv[2], zywrle_conv[2], zywrle_conv[2]},
  201. {zywrle_conv[1], zywrle_conv[1], zywrle_conv[1]}},
  202. };
  203. #endif
  204. /* Load/Save pixel stuffs. */
  205. #define ZYWRLE_YMASK15 0xFFFFFFF8
  206. #define ZYWRLE_UVMASK15 0xFFFFFFF8
  207. #define ZYWRLE_LOAD_PIXEL15(src, r, g, b) \
  208. do { \
  209. r = (((uint8_t*)src)[S_1]<< 1)& 0xF8; \
  210. g = (((uint8_t*)src)[S_1]<< 6) | (((uint8_t*)src)[S_0]>> 2); \
  211. g &= 0xF8; \
  212. b = (((uint8_t*)src)[S_0]<< 3)& 0xF8; \
  213. } while (0)
  214. #define ZYWRLE_SAVE_PIXEL15(dst, r, g, b) \
  215. do { \
  216. r &= 0xF8; \
  217. g &= 0xF8; \
  218. b &= 0xF8; \
  219. ((uint8_t*)dst)[S_1] = (uint8_t)((r >> 1)|(g >> 6)); \
  220. ((uint8_t*)dst)[S_0] = (uint8_t)(((b >> 3)|(g << 2))& 0xFF); \
  221. } while (0)
  222. #define ZYWRLE_YMASK16 0xFFFFFFFC
  223. #define ZYWRLE_UVMASK16 0xFFFFFFF8
  224. #define ZYWRLE_LOAD_PIXEL16(src, r, g, b) \
  225. do { \
  226. r = ((uint8_t*)src)[S_1] & 0xF8; \
  227. g = (((uint8_t*)src)[S_1]<< 5) | (((uint8_t*)src)[S_0] >> 3); \
  228. g &= 0xFC; \
  229. b = (((uint8_t*)src)[S_0]<< 3) & 0xF8; \
  230. } while (0)
  231. #define ZYWRLE_SAVE_PIXEL16(dst, r, g,b) \
  232. do { \
  233. r &= 0xF8; \
  234. g &= 0xFC; \
  235. b &= 0xF8; \
  236. ((uint8_t*)dst)[S_1] = (uint8_t)(r | (g >> 5)); \
  237. ((uint8_t*)dst)[S_0] = (uint8_t)(((b >> 3)|(g << 3)) & 0xFF); \
  238. } while (0)
  239. #define ZYWRLE_YMASK32 0xFFFFFFFF
  240. #define ZYWRLE_UVMASK32 0xFFFFFFFF
  241. #define ZYWRLE_LOAD_PIXEL32(src, r, g, b) \
  242. do { \
  243. r = ((uint8_t*)src)[L_2]; \
  244. g = ((uint8_t*)src)[L_1]; \
  245. b = ((uint8_t*)src)[L_0]; \
  246. } while (0)
  247. #define ZYWRLE_SAVE_PIXEL32(dst, r, g, b) \
  248. do { \
  249. ((uint8_t*)dst)[L_2] = (uint8_t)r; \
  250. ((uint8_t*)dst)[L_1] = (uint8_t)g; \
  251. ((uint8_t*)dst)[L_0] = (uint8_t)b; \
  252. } while (0)
  253. static inline void harr(int8_t *px0, int8_t *px1)
  254. {
  255. /* Piecewise-Linear Harr(PLHarr) */
  256. int x0 = (int)*px0, x1 = (int)*px1;
  257. int orgx0 = x0, orgx1 = x1;
  258. if ((x0 ^ x1) & 0x80) {
  259. /* differ sign */
  260. x1 += x0;
  261. if (((x1 ^ orgx1) & 0x80) == 0) {
  262. /* |x1| > |x0| */
  263. x0 -= x1; /* H = -B */
  264. }
  265. } else {
  266. /* same sign */
  267. x0 -= x1;
  268. if (((x0 ^ orgx0) & 0x80) == 0) {
  269. /* |x0| > |x1| */
  270. x1 += x0; /* L = A */
  271. }
  272. }
  273. *px0 = (int8_t)x1;
  274. *px1 = (int8_t)x0;
  275. }
  276. /*
  277. 1D-Wavelet transform.
  278. In coefficients array, the famous 'pyramid' decomposition is well used.
  279. 1D Model:
  280. |L0L0L0L0|L0L0L0L0|H0H0H0H0|H0H0H0H0| : level 0
  281. |L1L1L1L1|H1H1H1H1|H0H0H0H0|H0H0H0H0| : level 1
  282. But this method needs line buffer because H/L is different position from X0/X1.
  283. So, I used 'interleave' decomposition instead of it.
  284. 1D Model:
  285. |L0H0L0H0|L0H0L0H0|L0H0L0H0|L0H0L0H0| : level 0
  286. |L1H0H1H0|L1H0H1H0|L1H0H1H0|L1H0H1H0| : level 1
  287. In this method, H/L and X0/X1 is always same position.
  288. This leads us to more speed and less memory.
  289. Of cause, the result of both method is quite same
  290. because it's only difference that coefficient position.
  291. */
  292. static inline void wavelet_level(int *data, int size, int l, int skip_pixel)
  293. {
  294. int s, ofs;
  295. int8_t *px0;
  296. int8_t *end;
  297. px0 = (int8_t*)data;
  298. s = (8 << l) * skip_pixel;
  299. end = px0 + (size >> (l + 1)) * s;
  300. s -= 2;
  301. ofs = (4 << l) * skip_pixel;
  302. while (px0 < end) {
  303. harr(px0, px0 + ofs);
  304. px0++;
  305. harr(px0, px0 + ofs);
  306. px0++;
  307. harr(px0, px0 + ofs);
  308. px0 += s;
  309. }
  310. }
  311. #ifndef ZYWRLE_QUANTIZE
  312. /* Type A:lower bit omitting of EZW style. */
  313. static inline void filter_wavelet_square(int *buf, int width, int height,
  314. int level, int l)
  315. {
  316. int r, s;
  317. int x, y;
  318. int *h;
  319. const unsigned int *m;
  320. m = &(zywrle_param[level - 1][l]);
  321. s = 2 << l;
  322. for (r = 1; r < 4; r++) {
  323. h = buf;
  324. if (r & 0x01) {
  325. h += s >> 1;
  326. }
  327. if (r & 0x02) {
  328. h += (s >> 1) * width;
  329. }
  330. for (y = 0; y < height / s; y++) {
  331. for (x = 0; x < width / s; x++) {
  332. /*
  333. these are same following code.
  334. h[x] = h[x] / (~m[x]+1) * (~m[x]+1);
  335. ( round h[x] with m[x] bit )
  336. '&' operator isn't 'round' but is 'floor'.
  337. So, we must offset when h[x] is negative.
  338. */
  339. if (((int8_t*)h)[0] & 0x80) {
  340. ((int8_t*)h)[0] += ~((int8_t*)m)[0];
  341. }
  342. if (((int8_t*)h)[1] & 0x80) {
  343. ((int8_t*)h)[1] += ~((int8_t*)m)[1];
  344. }
  345. if (((int8_t*)h)[2] & 0x80) {
  346. ((int8_t*)h)[2] += ~((int8_t*)m)[2];
  347. }
  348. *h &= *m;
  349. h += s;
  350. }
  351. h += (s-1)*width;
  352. }
  353. }
  354. }
  355. #else
  356. /*
  357. Type B:Non liner quantization filter.
  358. Coefficients have Gaussian curve and smaller value which is
  359. large part of coefficients isn't more important than larger value.
  360. So, I use filter of Non liner quantize/dequantize table.
  361. In general, Non liner quantize formula is explained as following.
  362. y=f(x) = sign(x)*round( ((abs(x)/(2^7))^ r )* 2^(bo-1) )*2^(8-bo)
  363. x=f-1(y) = sign(y)*round( ((abs(y)/(2^7))^(1/r))* 2^(bi-1) )*2^(8-bi)
  364. ( r:power coefficient bi:effective MSB in input bo:effective MSB in output )
  365. r < 1.0 : Smaller value is more important than larger value.
  366. r > 1.0 : Larger value is more important than smaller value.
  367. r = 1.0 : Liner quantization which is same with EZW style.
  368. r = 0.75 is famous non liner quantization used in MP3 audio codec.
  369. In contrast to audio data, larger value is important in wavelet coefficients.
  370. So, I select r = 2.0 table( quantize is x^2, dequantize sqrt(x) ).
  371. As compared with EZW style liner quantization, this filter tended to be
  372. more sharp edge and be more compression rate but be more blocking noise and be
  373. less quality. Especially, the surface of graphic objects has distinguishable
  374. noise in middle quality mode.
  375. We need only quantized-dequantized(filtered) value rather than quantized value
  376. itself because all values are packed or palette-lized in later ZRLE section.
  377. This lead us not to need to modify client decoder when we change
  378. the filtering procedure in future.
  379. Client only decodes coefficients given by encoder.
  380. */
  381. static inline void filter_wavelet_square(int *buf, int width, int height,
  382. int level, int l)
  383. {
  384. int r, s;
  385. int x, y;
  386. int *h;
  387. const int8_t **m;
  388. m = zywrle_param[level - 1][l];
  389. s = 2 << l;
  390. for (r = 1; r < 4; r++) {
  391. h = buf;
  392. if (r & 0x01) {
  393. h += s >> 1;
  394. }
  395. if (r & 0x02) {
  396. h += (s >> 1) * width;
  397. }
  398. for (y = 0; y < height / s; y++) {
  399. for (x = 0; x < width / s; x++) {
  400. ((int8_t*)h)[0] = m[0][((uint8_t*)h)[0]];
  401. ((int8_t*)h)[1] = m[1][((uint8_t*)h)[1]];
  402. ((int8_t*)h)[2] = m[2][((uint8_t*)h)[2]];
  403. h += s;
  404. }
  405. h += (s - 1) * width;
  406. }
  407. }
  408. }
  409. #endif
  410. static inline void wavelet(int *buf, int width, int height, int level)
  411. {
  412. int l, s;
  413. int *top;
  414. int *end;
  415. for (l = 0; l < level; l++) {
  416. top = buf;
  417. end = buf + height * width;
  418. s = width << l;
  419. while (top < end) {
  420. wavelet_level(top, width, l, 1);
  421. top += s;
  422. }
  423. top = buf;
  424. end = buf + width;
  425. s = 1<<l;
  426. while (top < end) {
  427. wavelet_level(top, height, l, width);
  428. top += s;
  429. }
  430. filter_wavelet_square(buf, width, height, level, l);
  431. }
  432. }
  433. /* Load/Save coefficients stuffs.
  434. Coefficients manages as 24 bits little-endian pixel. */
  435. #define ZYWRLE_LOAD_COEFF(src, r, g, b) \
  436. do { \
  437. r = ((int8_t*)src)[2]; \
  438. g = ((int8_t*)src)[1]; \
  439. b = ((int8_t*)src)[0]; \
  440. } while (0)
  441. #define ZYWRLE_SAVE_COEFF(dst, r, g, b) \
  442. do { \
  443. ((int8_t*)dst)[2] = (int8_t)r; \
  444. ((int8_t*)dst)[1] = (int8_t)g; \
  445. ((int8_t*)dst)[0] = (int8_t)b; \
  446. } while (0)
  447. /*
  448. RGB <=> YUV conversion stuffs.
  449. YUV conversion is explained as following formula in strict meaning:
  450. Y = 0.299R + 0.587G + 0.114B ( 0<=Y<=255)
  451. U = -0.169R - 0.331G + 0.500B (-128<=U<=127)
  452. V = 0.500R - 0.419G - 0.081B (-128<=V<=127)
  453. I use simple conversion RCT(reversible color transform) which is described
  454. in JPEG-2000 specification.
  455. Y = (R + 2G + B)/4 ( 0<=Y<=255)
  456. U = B-G (-256<=U<=255)
  457. V = R-G (-256<=V<=255)
  458. */
  459. /* RCT is N-bit RGB to N-bit Y and N+1-bit UV.
  460. For make Same N-bit, UV is lossy.
  461. More exact PLHarr, we reduce to odd range(-127<=x<=127). */
  462. #define ZYWRLE_RGBYUV_(r, g, b, y, u, v, ymask, uvmask) \
  463. do { \
  464. y = (r + (g << 1) + b) >> 2; \
  465. u = b - g; \
  466. v = r - g; \
  467. y -= 128; \
  468. u >>= 1; \
  469. v >>= 1; \
  470. y &= ymask; \
  471. u &= uvmask; \
  472. v &= uvmask; \
  473. if (y == -128) { \
  474. y += (0xFFFFFFFF - ymask + 1); \
  475. } \
  476. if (u == -128) { \
  477. u += (0xFFFFFFFF - uvmask + 1); \
  478. } \
  479. if (v == -128) { \
  480. v += (0xFFFFFFFF - uvmask + 1); \
  481. } \
  482. } while (0)
  483. /*
  484. coefficient packing/unpacking stuffs.
  485. Wavelet transform makes 4 sub coefficient image from 1 original image.
  486. model with pyramid decomposition:
  487. +------+------+
  488. | | |
  489. | L | Hx |
  490. | | |
  491. +------+------+
  492. | | |
  493. | H | Hxy |
  494. | | |
  495. +------+------+
  496. So, we must transfer each sub images individually in strict meaning.
  497. But at least ZRLE meaning, following one decomposition image is same as
  498. avobe individual sub image. I use this format.
  499. (Strictly saying, transfer order is reverse(Hxy->Hy->Hx->L)
  500. for simplified procedure for any wavelet level.)
  501. +------+------+
  502. | L |
  503. +------+------+
  504. | Hx |
  505. +------+------+
  506. | Hy |
  507. +------+------+
  508. | Hxy |
  509. +------+------+
  510. */
  511. #define ZYWRLE_INC_PTR(data) \
  512. do { \
  513. data++; \
  514. if( data - p >= (w + uw) ) { \
  515. data += scanline-(w + uw); \
  516. p = data; \
  517. } \
  518. } while (0)
  519. #define ZYWRLE_TRANSFER_COEFF(buf, data, t, w, h, scanline, level, TRANS) \
  520. do { \
  521. ph = buf; \
  522. s = 2 << level; \
  523. if (t & 0x01) { \
  524. ph += s >> 1; \
  525. } \
  526. if (t & 0x02) { \
  527. ph += (s >> 1) * w; \
  528. } \
  529. end = ph + h * w; \
  530. while (ph < end) { \
  531. line = ph + w; \
  532. while (ph < line) { \
  533. TRANS \
  534. ZYWRLE_INC_PTR(data); \
  535. ph += s; \
  536. } \
  537. ph += (s - 1) * w; \
  538. } \
  539. } while (0)
  540. #define ZYWRLE_PACK_COEFF(buf, data, t, width, height, scanline, level) \
  541. ZYWRLE_TRANSFER_COEFF(buf, data, t, width, height, scanline, level, \
  542. ZYWRLE_LOAD_COEFF(ph, r, g, b); \
  543. ZYWRLE_SAVE_PIXEL(data, r, g, b);)
  544. #define ZYWRLE_UNPACK_COEFF(buf, data, t, width, height, scanline, level) \
  545. ZYWRLE_TRANSFER_COEFF(buf, data, t, width, height, scanline, level, \
  546. ZYWRLE_LOAD_PIXEL(data, r, g, b); \
  547. ZYWRLE_SAVE_COEFF(ph, r, g, b);)
  548. #define ZYWRLE_SAVE_UNALIGN(data, TRANS) \
  549. do { \
  550. top = buf + w * h; \
  551. end = buf + (w + uw) * (h + uh); \
  552. while (top < end) { \
  553. TRANS \
  554. ZYWRLE_INC_PTR(data); \
  555. top++; \
  556. } \
  557. } while (0)
  558. #define ZYWRLE_LOAD_UNALIGN(data,TRANS) \
  559. do { \
  560. top = buf + w * h; \
  561. if (uw) { \
  562. p = data + w; \
  563. end = (int*)(p + h * scanline); \
  564. while (p < (ZRLE_PIXEL*)end) { \
  565. line = (int*)(p + uw); \
  566. while (p < (ZRLE_PIXEL*)line) { \
  567. TRANS \
  568. p++; \
  569. top++; \
  570. } \
  571. p += scanline - uw; \
  572. } \
  573. } \
  574. if (uh) { \
  575. p = data + h * scanline; \
  576. end = (int*)(p + uh * scanline); \
  577. while (p < (ZRLE_PIXEL*)end) { \
  578. line = (int*)(p + w); \
  579. while (p < (ZRLE_PIXEL*)line) { \
  580. TRANS \
  581. p++; \
  582. top++; \
  583. } \
  584. p += scanline - w; \
  585. } \
  586. } \
  587. if (uw && uh) { \
  588. p= data + w + h * scanline; \
  589. end = (int*)(p + uh * scanline); \
  590. while (p < (ZRLE_PIXEL*)end) { \
  591. line = (int*)(p + uw); \
  592. while (p < (ZRLE_PIXEL*)line) { \
  593. TRANS \
  594. p++; \
  595. top++; \
  596. } \
  597. p += scanline-uw; \
  598. } \
  599. } \
  600. } while (0)
  601. static inline void zywrle_calc_size(int *w, int *h, int level)
  602. {
  603. *w &= ~((1 << level) - 1);
  604. *h &= ~((1 << level) - 1);
  605. }
  606. #endif