KJpegDecode.c
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:6k
源码类别:

模拟服务器

开发平台:

C/C++

  1. //---------------------------------------------------------------------------
  2. // Sword3 Engine (c) 1999-2000 by Kingsoft
  3. //
  4. // File: KJpegDeocode.cpp
  5. // Date: 2000.08.08
  6. // Code: Daniel Wang
  7. // Desc: Jpeg DU Decode
  8. // From: Cloud Wu's JPEG Decoder
  9. //---------------------------------------------------------------------------
  10. #include <windows.h>
  11. #include "KJpegLib.h"
  12. /****************************************************************************
  13. DU 的解码
  14. 每个 DU 里的数据按下面次序(倒序)重排列过, 得到原始次序
  15. BYTE jpeg_zigzag[64] = {
  16. 0, 63,62,55,47,54,61,60,
  17. 53,46,39,31,38,45,52,59,
  18. 58,51,44,37,30,23,15,22,
  19. 29,36,43,50,57,56,49,42,
  20. 35,28,21,14,7, 6, 13,20,
  21. 27,34,41,48,40,33,26,19,
  22. 12,5, 4,11,18,25,32,24,
  23. 17,10,3,2, 9, 16,8, 1 
  24. };
  25. ****************************************************************************/
  26. // ANN 算法需要转置矩阵
  27. BYTE jpeg_zigzag[64] = {
  28. 0,63,55,62,61,54,47,39,
  29. 46,53,60,59,52,45,38,31,
  30. 23,30,37,44,51,58,57,50,
  31. 43,36,29,22,15,7,14,21,
  32. 28,35,42,49,56,48,41,34,
  33. 27,20,13,6,5,12,19,26,
  34. 33,40,32,25,18,11,4,3,
  35. 10,17,24,16,9,2,1,8
  36. };
  37. //---------------------------------------------------------------------------
  38. // 函数: jpeg_Preprocess
  39. // 功能: 预处理, 去掉 0xFF
  40. // 参数: stream JPEG数据流
  41. // 返回: void
  42. //---------------------------------------------------------------------------
  43. void jpeg_preprocess(LPBYTE stream)
  44. {
  45. __asm
  46. {
  47. mov esi,stream;
  48. mov edi,stream;
  49. cld;
  50. ALIGN 4
  51. _loop:
  52. lodsb;
  53. cmp al,0xff;
  54. jz _getFF;
  55. stosb;
  56. jmp _loop;
  57. _getFF:
  58. lodsb;
  59. test al,al;
  60. jnz _not0;
  61. mov byte ptr [edi],0xff;
  62. inc edi;
  63. jmp _loop;
  64. _not0:
  65. cmp al,0xd9; // 结束?
  66. jz _end;
  67. cmp al,0xff;
  68. jz _getFF;
  69. _end:
  70. }
  71. }
  72. //---------------------------------------------------------------------------
  73. // 函数: jpeg_decode_DU
  74. // 功能: 从数据流 jpeg_stream 解码一个 DU 到 buf
  75. // 参数: buf 解码后的缓存
  76. // com jpeg componet
  77. // 返回: void
  78. //---------------------------------------------------------------------------
  79. void jpeg_decode_DU(short *buf, int com)
  80. {
  81. short* QTB = jpeg_qtable[jpeg_head.component[com].qtb];
  82. JPEG_HTABLE* ACT = &jpeg_htable[jpeg_head.component[com].act];
  83. JPEG_HTABLE* DCT = &jpeg_htable[jpeg_head.component[com].dct];
  84. short JpegDC = jpeg_DC[com];
  85. // DC 的解码
  86. __asm
  87. {
  88. mov edx,DCT;
  89. mov esi,jpeg_stream;
  90. mov edi,QTB;
  91. cld;
  92. mov ch,byte ptr[edx+JPEG_HTABLE.num];
  93. mov edx,[edx]JPEG_HTABLE.htb;
  94. mov cl,jpeg_bit;
  95. lodsd;
  96. mov ebx,eax;
  97. mov eax,[esi];
  98. bswap ebx;
  99. bswap eax;
  100. shld ebx,eax,cl;
  101. _loop_dct:
  102. mov cl,[edx]JPEG_HCODE.len;
  103. xor eax,eax;
  104. shld eax,ebx,cl;
  105. cmp ax,[edx]JPEG_HCODE.code;
  106. jz _decode_dct;
  107. add edx,4;
  108. dec ch;
  109. jmp _loop_dct;
  110. _decode_dct:
  111. mov ch,cl;
  112. mov cl,jpeg_bit; // 刚才用掉 cl 个bit
  113. shr ebx,cl;
  114. add cl,ch; // 一共用掉 bit 数
  115. cmp cl,32; // 用完 4 字节?
  116. jl _lt4byte_dc;
  117. sub cl,32;
  118. lodsd;
  119. bswap eax;
  120. mov ebx,eax;
  121. _lt4byte_dc:
  122. mov eax,[esi];
  123. bswap eax;
  124. shld ebx,eax,cl;
  125. mov ch,cl;
  126. mov cl,[edx]JPEG_HCODE.num;
  127. xor eax,eax;
  128. shld eax,ebx,cl;
  129. xchg ch,cl;
  130. shr ebx,cl;
  131. add cl,ch;
  132. xchg ch,cl;
  133. dec cl;
  134. bt eax,cl;
  135. jc _get_diff;
  136. inc cl;
  137. mov edx,1;
  138. inc eax;
  139. shl edx,cl;
  140. sub eax,edx;
  141. _get_diff:
  142. add ax,JpegDC;
  143. mov edx,com;
  144. mov cl,ch;
  145. push edi;
  146. lea edi,jpeg_DC;
  147. mov [edi + edx * 2],ax;
  148. pop edi;
  149. imul word ptr[edi];
  150. mov edx,buf;
  151. add edi,2;
  152. mov [edx],ax;
  153. // AC 的解码. cl 是前面被使用掉的 bit 数, ebx 是上4byte 
  154. cmp cl,32; // 用完 4 字节?
  155. jl _lt4byte_dc0;
  156. sub cl,32;
  157. lodsd;
  158. bswap eax;
  159. mov ebx,eax;
  160. _lt4byte_dc0:
  161. mov eax,[esi];
  162. bswap eax;
  163. shld ebx,eax,cl;
  164. mov jpeg_bit,cl; //用掉 cl 个 bit
  165. mov cx,63; // 处理 63 个 AC
  166. _loop_decode_act:
  167. mov edx,ACT;
  168. bswap ecx;
  169. mov ch,byte ptr[edx+JPEG_HTABLE.num];
  170. mov edx,[edx]JPEG_HTABLE.htb;
  171. _loop_act:
  172. mov cl,[edx]JPEG_HCODE.len;
  173. xor eax,eax;
  174. shld eax,ebx,cl;
  175. cmp ax,[edx]JPEG_HCODE.code;
  176. jz _decode_act;
  177. add edx,4;
  178. dec ch;
  179. jmp _loop_act;
  180. _decode_act:
  181. // [edx]JPEG_HCODE.num 是接出来的码
  182. mov ch,cl;
  183. mov cl,jpeg_bit; // 刚才用掉 cl 个bit
  184. shr ebx,cl;
  185. add cl,ch; // 一共用掉 bit 数
  186. cmp cl,32; // 用完 4 字节?
  187. jl _lt4byte;
  188. sub cl,32;
  189. lodsd;
  190. bswap eax;
  191. mov ebx,eax;
  192. _lt4byte:
  193. mov eax,[esi];
  194. bswap eax;
  195. shld ebx,eax,cl;
  196. mov jpeg_bit,cl;//使用了下4byte的 cl bit
  197. movzx eax,byte ptr[edx]JPEG_HCODE.num;
  198. bswap ecx;
  199. test ax,ax;
  200. jz _decode_end;
  201. ror eax,4;
  202. cmp cx,ax;
  203. jz _decode_end;
  204. sub cx,ax;
  205. rol eax,4;
  206. bswap ecx;
  207. mov cl,al;
  208. mov ch,al;
  209. and cl,0xf;
  210. shr ch,4;
  211. jz _skip_set0;
  212. movzx eax,ch;
  213. bswap ecx;
  214. lea edi,[edi+2*eax];
  215. add ax,cx;
  216. bswap ecx;
  217. _set0:
  218. movzx edx,[eax+jpeg_zigzag];
  219. add edx,edx;
  220. add edx,buf;
  221. dec eax;
  222. dec ch;
  223. mov word ptr[edx],0;
  224. jnz _set0;
  225. _skip_set0:
  226. xor eax,eax;
  227. shld eax,ebx,cl;
  228. dec cl;
  229. bt eax,cl;
  230. jc _get_ac;
  231. mov edx,2;
  232. inc eax;
  233. shl edx,cl;
  234. sub eax,edx;
  235. _get_ac:
  236. inc cl;
  237. imul word ptr[edi];
  238. // dx==0 时, 正数,无进位
  239. test dx,dx;
  240. jz _2byte;
  241. // dx==0xffff 时, 负数,无进位
  242. inc dx;
  243. jz _2byte;
  244. mov ax,0x7fff;
  245. bt dx,15;
  246. adc ax,0;
  247. _2byte:
  248. bswap ecx;
  249. movzx edx,cx;
  250. movzx edx,[edx+jpeg_zigzag];
  251. add edx,edx
  252. add edx,buf;
  253. add edi,2;
  254. mov [edx],ax;
  255. bswap ecx;
  256. mov ch,cl;
  257. mov cl,jpeg_bit;
  258. shr ebx,cl;
  259. add cl,ch; // 累计使用 bit 数
  260. cmp cl,32; // 用完 4 字节?
  261. jl _lt4byte_2;
  262. sub cl,32;
  263. lodsd;
  264. bswap eax;
  265. mov ebx,eax;
  266. _lt4byte_2:
  267. mov eax,[esi];
  268. bswap eax;
  269. shld ebx,eax,cl;
  270. mov jpeg_bit,cl;//使用了下4byte的 cl bit
  271. bswap ecx;
  272. dec cx;
  273. jnz _loop_decode_act;
  274. jmp _end;
  275. _decode_end:
  276. movzx ecx,cx;
  277. xor eax,eax;
  278. mov edi,buf;
  279. _end_loop:
  280. movzx edx,[ecx+jpeg_zigzag];
  281. dec ecx;
  282. mov [edi+edx*2],ax;
  283. jnz _end_loop;
  284. _end:
  285. sub esi,4;
  286. mov jpeg_stream,esi;
  287. }
  288. // iDCT 解码
  289. jpeg_IDCT(buf);
  290. }
  291. //---------------------------------------------------------------------------