bv7_jpeg_encoder_b

array -> jpeg

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/39428/258126/bv7_jpeg_encoder_b.js

  1. // ==UserScript==
  2. // @name bv7_jpeg_encoder_b
  3. // @namespace bv7
  4. // @version 0.1
  5. // @description array -> jpeg
  6. // @author bv7
  7. // ==/UserScript==
  8.  
  9. // include file:///D:/projects/JSProjects/bv7bbc/bv7_bbc_dark/bv_dev_canvas*.html
  10. // require https://gf.qytechs.cn/scripts/38665-bv7-jpeg2array-b/code/bv7_jpeg2array_b.user.js
  11. // require https://gf.qytechs.cn/scripts/39257-bv7-canvas-b/code/bv7_canvas_b.js
  12. // require https://raw.githubusercontent.com/owencm/javascript-jpeg-encoder/master/jpeg_encoder_basic.js
  13. // run-at document-idle
  14. // grant GM_xmlhttpRequest
  15.  
  16. class JpegEncoder {
  17. constructor() {
  18. this.zigZag = [
  19. 0x00, 0x01, 0x05, 0x06, 0x0E, 0x0F, 0x1B, 0x1C,
  20. 0x02, 0x04, 0x07, 0x0D, 0x10, 0x1A, 0x1D, 0x2A,
  21. 0x03, 0x08, 0x0C, 0x11, 0x19, 0x1E, 0x29, 0x2B,
  22. 0x09, 0x0B, 0x12, 0x18, 0x1F, 0x28, 0x2C, 0x35,
  23. 0x0A, 0x13, 0x17, 0x20, 0x27, 0x2D, 0x34, 0x36,
  24. 0x14, 0x16, 0x21, 0x26, 0x2E, 0x33, 0x37, 0x3C,
  25. 0x15, 0x22, 0x25, 0x2F, 0x32, 0x38, 0x3B, 0x3D,
  26. 0x23, 0x24, 0x30, 0x31, 0x39, 0x3A, 0x3E, 0x3F
  27. ];
  28. this.std_dc_luminance_nrcodes = [
  29. 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
  30. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  31. ];
  32. this.std_dc_luminance_values = [
  33. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  34. 0x08, 0x09, 0x0A, 0x0B
  35. ];
  36. this.std_ac_luminance_nrcodes = [
  37. 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
  38. 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D
  39. ];
  40. this.std_ac_luminance_values = [
  41. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  42. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  43. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08,
  44. 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0,
  45. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16,
  46. 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
  47. 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  48. 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  49. 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  50. 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  51. 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  52. 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  53. 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  54. 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
  55. 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
  56. 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5,
  57. 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4,
  58. 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
  59. 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA,
  60. 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
  61. 0xF9, 0xFA
  62. ];
  63. this.std_dc_chrominance_nrcodes = [
  64. 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  65. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
  66. ];
  67. this.std_dc_chrominance_values = [
  68. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  69. 0x08, 0x09, 0x0A, 0x0B
  70. ];
  71. this.std_ac_chrominance_nrcodes = [
  72. 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
  73. 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77
  74. ];
  75. this.std_ac_chrominance_values = [
  76. 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  77. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  78. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  79. 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0,
  80. 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34,
  81. 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
  82. 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38,
  83. 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  84. 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  85. 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  86. 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  87. 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  88. 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
  89. 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
  90. 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4,
  91. 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
  92. 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,
  93. 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
  94. 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9,
  95. 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
  96. 0xF9, 0xFA
  97. ];
  98. this.YDC_HT = new Uint8Array(0x0B);
  99. this.UVDC_HT = new Uint8Array(0x0B);
  100. this.YAC_HT = new Int8Array(0xFA);
  101. this.UVAC_HT = new Int8Array(0xFA);
  102. this.YDC_HT2 = new Uint8Array(0x0B);
  103. this.UVDC_HT2 = new Uint8Array(0x0B);
  104. this.YAC_HT2 = new Uint8Array(0xFA);
  105. this.UVAC_HT2 = new Uint8Array(0xFA);
  106. this.YTable = new Uint8Array(0x40 );
  107. this.UVTable = new Uint8Array(0x40 );
  108. this.fdtbl_Y = [];
  109. this.fdtbl_UV = new Float32Array(0x08);
  110. this.outputfDCTQuant = new Uint16Array(0x40 );
  111. this.DU = new Int16Array(0x40 );
  112. this.category = new Uint8Array(0xFFFE);
  113. this.bitcode = new Uint16Array(0xFFFE);
  114. this.RGB_YUV_TABLE = new Uint32Array(0x0800);
  115. this.YDU = new Int16Array(0x40 );
  116. this.UDU = new Int16Array(0x40 );
  117. this.VDU = new Int16Array(0x40 );
  118. this.initHuffmanTbl();
  119. this.initCategoryNumber();
  120. this.initRGBYUVTable();
  121. }
  122. // IO functions
  123. writeBits(value, posval) {
  124. while (posval >= 0) {
  125. if (value & (1 << posval)) this.bytenew |= (1 << this.bytepos);
  126. posval--;
  127. this.bytepos--;
  128. if (this.bytepos < 0) {
  129. this.writeByte(this.bytenew);
  130. if (this.bytenew == 0xFF) this.writeByte(0x00);
  131. this.bytepos = 7;
  132. this.bytenew = 0;
  133. }
  134. }
  135. }
  136. writeByte(value) {
  137. this.byteout.push(String.fromCharCode(value)); // write char directly instead of converting later
  138. }
  139. writeWord(value) {
  140. this.writeByte((value >> 8) & 0xFF);
  141. this.writeByte((value ) & 0xFF);
  142. }
  143. writeAPP0() {
  144. this.writeWord(0xFFE0); // marker
  145. this.writeWord(0x0010); // length
  146. this.writeByte(0x4A ); // J
  147. this.writeByte(0x46 ); // F
  148. this.writeByte(0x49 ); // I
  149. this.writeByte(0x46 ); // F
  150. this.writeByte(0x00 ); // = "JFIF",'\0'
  151. this.writeByte(0x01 ); // versionhi
  152. this.writeByte(0x01 ); // versionlo
  153. this.writeByte(0x00 ); // xyunits
  154. this.writeWord(0x0001); // xdensity
  155. this.writeWord(0x0001); // ydensity
  156. this.writeByte(0x00 ); // thumbnwidth
  157. this.writeByte(0x00 ); // thumbnheight
  158. }
  159. writeDQT() {
  160. this.writeWord(0xFFDB); // marker
  161. this.writeWord(0x0084); // length
  162. this.writeByte(0x00 );
  163. this.YTable.forEach((v) => this.writeByte(v));
  164. this.writeByte(0x01 );
  165. this.UVTable.forEach((v) => this.writeByte(v));
  166. }
  167. writeSOF0(width, height) {
  168. this.writeWord(0xFFC0); // marker
  169. this.writeWord(0x0011); // length, truecolor YUV JPG
  170. this.writeByte(0x08 ); // precision
  171. this.writeWord(height);
  172. this.writeWord(width );
  173. this.writeByte(0x03 ); // nrofcomponents
  174. this.writeByte(0x01 ); // IdY
  175. this.writeByte(0x11 ); // HVY
  176. this.writeByte(0x00 ); // QTY
  177. this.writeByte(0x02 ); // IdU
  178. this.writeByte(0x11 ); // HVU
  179. this.writeByte(0x01 ); // QTU
  180. this.writeByte(0x03 ); // IdV
  181. this.writeByte(0x11 ); // HVV
  182. this.writeByte(0x01 ); // QTV
  183. }
  184. writeDHT() {
  185. this.writeWord(0xFFC4); // marker
  186. this.writeWord(0x01A2); // length
  187. this.writeByte(0x00 ); // HTYDCinfo
  188. this.std_dc_luminance_nrcodes.forEach((v) => this.writeByte(v));
  189. this.std_dc_luminance_values.forEach((v) => this.writeByte(v));
  190. this.writeByte(0x10 ); // HTYACinfo
  191. this.std_ac_luminance_nrcodes.forEach((v) => this.writeByte(v));
  192. this.std_ac_luminance_values.forEach((v) => this.writeByte(v));
  193. this.writeByte(0x01 ); // HTUDCinfo
  194. this.std_dc_chrominance_nrcodes.forEach((v) => this.writeByte(v));
  195. this.std_dc_chrominance_values.forEach((v) => this.writeByte(v));
  196. this.writeByte(0x11 ); // HTUACinfo
  197. this.std_ac_chrominance_nrcodes.forEach((v) => this.writeByte(v));
  198. this.std_ac_chrominance_values.forEach((v) => this.writeByte(v));
  199. }
  200. writeSOS() {
  201. this.writeWord(0xFFDA); // marker
  202. this.writeWord(0x000C); // length
  203. this.writeByte(0x03 ); // nrofcomponents
  204. this.writeByte(0x01 ); // IdY
  205. this.writeByte(0x00 ); // HTY
  206. this.writeByte(0x02 ); // IdU
  207. this.writeByte(0x11 ); // HTU
  208. this.writeByte(0x03 ); // IdV
  209. this.writeByte(0x11 ); // HTV
  210. this.writeByte(0x00 ); // Ss
  211. this.writeByte(0x3F ); // Se
  212. this.writeByte(0x00 ); // Bf
  213. }
  214. computeHuffmanTbl(nrcodes, std_table, HT, HT2) {
  215. let a, j;
  216. let codevalue = 0;
  217. let pos_in_table = 0;
  218. nrcodes.forEach((nrcodesK, k) => {
  219. for (j = 0; j < nrcodesK; j++) {
  220. HT[a = std_table[pos_in_table++]] = codevalue++;
  221. HT2[a ] = k;
  222. }
  223. codevalue *= 2;
  224. });
  225. }
  226. initHuffmanTbl() {
  227. this.computeHuffmanTbl(this.std_dc_luminance_nrcodes , this.std_dc_luminance_values , this.YDC_HT , this.YDC_HT2 );
  228. this.computeHuffmanTbl(this.std_dc_chrominance_nrcodes, this.std_dc_chrominance_values, this.UVDC_HT, this.UVDC_HT2);
  229. this.computeHuffmanTbl(this.std_ac_luminance_nrcodes , this.std_ac_luminance_values , this.YAC_HT , this.YAC_HT2 );
  230. this.computeHuffmanTbl(this.std_ac_chrominance_nrcodes, this.std_ac_chrominance_values, this.UVAC_HT, this.UVAC_HT2);
  231. }
  232. initCategoryNumber() {
  233. let cat, nr, nrlower, nrupper, nrn;
  234. for (cat = 0, nrlower = 1, nrupper = 2; cat < 15; cat++, nrlower = nrupper, nrupper <<= 1) {
  235. for (nr = nrlower, nrn = nrlower - 1; nr < nrupper; nr++, nrn--) { //Positive & Negative numbers
  236. this.category[0x7FFF + nr] = this.category[0x7FFF - nr] = cat;
  237. this.bitcode[ 0x7FFF + nr] = nr;
  238. this.bitcode[ 0x7FFF - nr] = nrn;
  239. }
  240. }
  241. }
  242. initRGBYUVTable() {
  243. for(let i = 0x0000; i < 0x0100; i++) {
  244. this.RGB_YUV_TABLE[i ] = 0x004C8B * i ;
  245. this.RGB_YUV_TABLE[i + 0x0100] = 0x009646 * i ;
  246. this.RGB_YUV_TABLE[i + 0x0200] = 0x001D2F * i + 0x008000;
  247. this.RGB_YUV_TABLE[i + 0x0300] = - 0x002B33 * i ;
  248. this.RGB_YUV_TABLE[i + 0x0400] = - 0x0054CD * i ;
  249. this.RGB_YUV_TABLE[i + 0x0500] = 0x008000 * i + 0x807FFF;
  250. this.RGB_YUV_TABLE[i + 0x0600] = - 0x006B2F * i ;
  251. this.RGB_YUV_TABLE[i + 0x0700] = - 0x0014D1 * i ;
  252. }
  253. }
  254. initQuantTables(sf) {
  255. let i, t;
  256. let row;
  257. let col;
  258. [
  259. 0x10, 0x0B, 0x0A, 0x10, 0x18, 0x28, 0x33, 0x3D,
  260. 0x0C, 0x0C, 0x0E, 0x13, 0x1A, 0x3A, 0x3C, 0x37,
  261. 0x0E, 0x0D, 0x10, 0x18, 0x28, 0x39, 0x45, 0x38,
  262. 0x0E, 0x11, 0x16, 0x1D, 0x33, 0x57, 0x50, 0x3E,
  263. 0x12, 0x16, 0x25, 0x38, 0x44, 0x6D, 0x67, 0x4D,
  264. 0x18, 0x23, 0x37, 0x40, 0x51, 0x68, 0x71, 0x5C,
  265. 0x31, 0x40, 0x4E, 0x57, 0x67, 0x79, 0x78, 0x65,
  266. 0x48, 0x5C, 0x5F, 0x62, 0x70, 0x64, 0x67, 0x63
  267. ].forEach((v, i) => this.YTable[this.zigZag[i]] = (t = 0 | ((v * sf + 50) / 100)) < 1 ? 1 : t > 0xFF ? 0xFF : t);
  268. [
  269. 0x11, 0x12, 0x18, 0x2F, 0x63, 0x63, 0x63, 0x63,
  270. 0x12, 0x15, 0x1A, 0x42, 0x63, 0x63, 0x63, 0x63,
  271. 0x18, 0x1A, 0x38, 0x63, 0x63, 0x63, 0x63, 0x63,
  272. 0x2F, 0x42, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  273. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  274. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  275. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  276. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
  277. ].forEach((v, i) => this.UVTable[this.zigZag[i]] = (t = 0 | ((v * sf + 50) / 100)) < 1 ? 1 : t > 0xFF ? 0xFF : t);
  278. let aasf = [
  279. 1.0, 1.387039845, 1.306562965, 1.175875602,
  280. 1.0, 0.785694958, 0.541196100, 0.275899379
  281. ];
  282. for (i = 0, row = 0; row < 8; row++) for (col = 0; col < 8; col++, i++) {
  283. this.fdtbl_Y[i] = 1.0 / (this.YTable [this.zigZag[i]] * aasf[row] * aasf[col] * 8.0);
  284. this.fdtbl_UV[i] = 1.0 / (this.UVTable[this.zigZag[i]] * aasf[row] * aasf[col] * 8.0);
  285. }
  286. }
  287. fDCTQuant(data, fdtbl) { // DCT & quantization core
  288. let d0, d1, d2, d3, d4, d5, d6, d7;
  289. let t0, t1, t2, t3, t4, t5, t6, t7;
  290. // Pass 1: process rows.
  291. let i, p;
  292. for (i = p = 0; i < 8; ++i, p += 8) { // advance pointer to next row
  293. d0 = data[p ];
  294. d1 = data[p + 1];
  295. d2 = data[p + 2];
  296. d3 = data[p + 3];
  297. d4 = data[p + 4];
  298. d5 = data[p + 5];
  299. d6 = data[p + 6];
  300. d7 = data[p + 7];
  301. t0 = d0 + d7;
  302. t7 = d0 - d7;
  303. t1 = d1 + d6;
  304. t6 = d1 - d6;
  305. t2 = d2 + d5;
  306. t5 = d2 - d5;
  307. t3 = d3 + d4;
  308. t4 = d3 - d4;
  309. // Even part
  310. d0 = t0 + t3; // phase 2
  311. d1 = t1 + t2;
  312. d2 = t1 - t2;
  313. d3 = t0 - t3;
  314. data[p ] = d0 + d1; // phase 3
  315. data[p + 4] = d0 - d1;
  316. d0 = (d2 + d3) * 0.707106781; // c4
  317. data[p + 2] = d3 + d0; // phase 5
  318. data[p + 6] = d3 - d0;
  319. // Odd part
  320. d0 = t4 + t5; // phase 2
  321. d1 = t5 + t6;
  322. d2 = t6 + t7;
  323. // The rotator is modified from fig 4-8 to avoid extra negations.
  324. d3 = 0.382683433 * (d0 - d2); // c6
  325. t2 = 0.541196100 * d0 + d3; // c2-c6
  326. t4 = 1.306562965 * d2 + d3; // c2+c6
  327. t3 = 0.707106781 * d1 ; // c4
  328. d1 = t7 + t3; // phase 5
  329. d3 = t7 - t3;
  330. data[p + 1] = d1 + t4; // phase 6
  331. data[p + 3] = d3 - t2;
  332. data[p + 5] = d3 + t2;
  333. data[p + 7] = d1 - t4;
  334. }
  335. // Pass 2: process columns.
  336. for (i = p = 0; i < 8; ++i, p++) { // advance pointer to next column
  337. d0 = data[p ];
  338. d1 = data[p + 0x08];
  339. d2 = data[p + 0x10];
  340. d3 = data[p + 0x18];
  341. d4 = data[p + 0x20];
  342. d5 = data[p + 0x28];
  343. d6 = data[p + 0x30];
  344. d7 = data[p + 0x38];
  345. t0 = d0 + d7;
  346. t1 = d1 + d6;
  347. t2 = d2 + d5;
  348. t3 = d3 + d4;
  349. t4 = d3 - d4;
  350. t5 = d2 - d5;
  351. t6 = d1 - d6;
  352. t7 = d0 - d7;
  353. // Even part
  354. d0 = t0 + t3; // phase 2
  355. d1 = t1 + t2;
  356. d2 = t1 - t2;
  357. d3 = t0 - t3;
  358. data[p ] = d0 + d1; // phase 3
  359. data[p + 0x20] = d0 - d1;
  360. d0 = (d2 + d3) * 0.707106781; // c4
  361. data[p + 0x10] = d3 + d0; // phase 5
  362. data[p + 0x30] = d3 - d0;
  363. // Odd part
  364. d0 = t4 + t5; // phase 2
  365. d1 = t5 + t6;
  366. d2 = t6 + t7;
  367. // The rotator is modified from fig 4-8 to avoid extra negations.
  368. d3 = 0.382683433 * (d0 - d2); // c6
  369. t2 = 0.541196100 * d0 + d3; // c2-c6
  370. t4 = 1.306562965 * d2 + d3; // c2+c6
  371. t3 = 0.707106781 * d1; // c4
  372. d1 = t7 + t3; // phase 5
  373. d3 = t7 - t3;
  374. data[p + 0x08] = d1 + t4; // phase 6
  375. data[p + 0x18] = d3 - t2;
  376. data[p + 0x28] = d3 + t2;
  377. data[p + 0x38] = d1 - t4;
  378. }
  379. // Quantize/descale the coefficients
  380. // Apply the quantization and scaling factor & Round to nearest integer
  381. for (i = 0; i < 64; ++i) this.outputfDCTQuant[i] = ((d0 = data[i] * fdtbl[i]) > 0.0) ? ((d0 + 0.5)|0) : ((d0 - 0.5)|0); //outputfDCTQuant[i] = fround(d0);
  382. return this.outputfDCTQuant;
  383. }
  384. processDU(CDU, fdtbl, DC, HTDC, HTAC, HTDC2, HTAC2) {
  385. let pos, i, a;
  386. let EOB = HTAC[0x00];
  387. let EOB2 = HTAC2[0x00];
  388. let M16zeroes = HTAC[0xF0];
  389. let M16zeroes2 = HTAC2[0xF0];
  390. let DU_DCT = this.fDCTQuant(CDU, fdtbl);
  391. //ZigZag reorder
  392. for (i = 0; i < 64; ++i) this.DU[this.zigZag[i]] = DU_DCT[i];
  393. let Diff = this.DU[0] - DC;
  394. DC = this.DU[0];
  395. //Encode DC
  396. if (Diff == 0) this.writeBits(HTDC[0], HTDC2[0]); // Diff might be 0
  397. else {
  398. pos = 0x7FFF + Diff;
  399. this.writeBits(HTDC[a = this.category[pos] + 1], HTDC2[a]);
  400. this.writeBits(this.bitcode[pos], this.category[pos]);
  401. }
  402. //Encode ACs
  403. let end0pos = 0x3F; // was const... which is crazy
  404. while (end0pos > 0 && this.DU[end0pos] == 0) end0pos--;
  405. if (end0pos == 0) this.writeBits(EOB, EOB2); //end0pos = first element in reverse order !=0
  406. else {
  407. let lng, startpos, nrzeroes, nrmarker;
  408. end0pos++;
  409. for (i = 1; i < end0pos; i++) {
  410. startpos = i;
  411. while (this.DU[i] == 0 && i < end0pos) ++i;
  412. nrzeroes = i - startpos;
  413. if (nrzeroes > 0x0F) {
  414. lng = nrzeroes >> 4;
  415. for (nrmarker = 0; nrmarker < lng; ++nrmarker) this.writeBits(M16zeroes, M16zeroes2);
  416. nrzeroes = nrzeroes & 0x0F;
  417. }
  418. this.writeBits(HTAC[a = (nrzeroes << 4) + this.category[pos = 0x7FFF + this.DU[i]] + 1], HTAC2[a]);
  419. this.writeBits(this.bitcode[pos], this.category[pos]);
  420. }
  421. if (end0pos != 0x40) this.writeBits(EOB, EOB2);
  422. }
  423. return DC;
  424. }
  425. encode(imageData, quality = 0.92) { // image data object
  426. this.byteout = []; // Initialize bit writer
  427. this.bytenew = 0;
  428. this.bytepos = 7;
  429. let newSf = Math.floor(quality < 0.5 ? 50 / quality : 200 * (1 - quality));
  430. if ((typeof this.sf) == 'undefined' || newSf !== this.sf) this.initQuantTables(this.sf = newSf);
  431. // Add JPEG headers
  432. this.writeWord(0xFFD8); // SOI
  433. this.writeAPP0();
  434. this.writeDQT();
  435. this.writeSOF0(imageData.width, imageData.height);
  436. this.writeDHT();
  437. this.writeSOS();
  438. let y1, y2, y3, pY3, pY1;
  439. let x1, x2, x3, pX3, pX1;
  440. let r, g, b;
  441. let pos;
  442. let quadWidth = imageData.width * 4;
  443. let width32 = imageData.width * 32;
  444. // Encode 8x8 macroblocks
  445. let DCY = 0;
  446. let DCU = 0;
  447. let DCV = 0;
  448. for (y1 = 0, pY1 = 0; y1 < imageData.height; y1 += 8, pY1 += width32) {
  449. for (x1 = 0, pX1 = pY1; x1 < imageData.width; x1 += 8, pX1 += 32) {
  450. for (y2 = pos = 0, y3 = y1, pY3 = pX1; y2 < 8; y2++, y3++, pY3 += quadWidth) {
  451. for (x2 = 0, x3 = x1, pX3 = pY3; x2 < 8; x2++, x3++, pos++) {
  452. if (y3 < imageData.height && x3 < imageData.width) {
  453. r = imageData.data[pX3++];
  454. g = imageData.data[pX3++];
  455. b = imageData.data[pX3++];
  456. pX3++;
  457. } else r = g = b = 0; // padding
  458. this.YDU[pos] = ((this.RGB_YUV_TABLE[r ] + this.RGB_YUV_TABLE[g + 0x0100] + this.RGB_YUV_TABLE[b + 0x0200]) >> 16) - 0x80;
  459. this.UDU[pos] = ((this.RGB_YUV_TABLE[r + 0x0300] + this.RGB_YUV_TABLE[g + 0x0400] + this.RGB_YUV_TABLE[b + 0x0500]) >> 16) - 0x80;
  460. this.VDU[pos] = ((this.RGB_YUV_TABLE[r + 0x0500] + this.RGB_YUV_TABLE[g + 0x0600] + this.RGB_YUV_TABLE[b + 0x0700]) >> 16) - 0x80;
  461. }
  462. }
  463. DCY = this.processDU(this.YDU, this.fdtbl_Y , DCY, this.YDC_HT, this.YAC_HT, this.YDC_HT2, this.YAC_HT2);
  464. DCU = this.processDU(this.UDU, this.fdtbl_UV, DCU, this.UVDC_HT, this.UVAC_HT, this.UVDC_HT2, this.UVAC_HT2);
  465. DCV = this.processDU(this.VDU, this.fdtbl_UV, DCV, this.UVDC_HT, this.UVAC_HT, this.UVDC_HT2, this.UVAC_HT2);
  466. }
  467. }
  468. ////////////////////////////////////////////////////////////////
  469. // Do the bit alignment of the EOI marker
  470. if (this.bytepos >= 0) this.writeBits((1 << (this.bytepos + 1)) - 1, this.bytepos);
  471. this.writeWord(0xFFD9); //EOI
  472. let res = 'data:image/jpeg;base64,' + btoa(this.byteout.join(''));
  473. this.byteout = [];
  474. return res;
  475. }
  476. }
  477. /*
  478. let canvas = new Canvas();
  479. canvas.width = 500;
  480. canvas.height = 500;
  481. canvas.getContext('2d').drawImage(document.getElementById('img1'), -20, 20, 600, 500, 20, -10, 300, 400, () => {
  482. let jpegEncoder = new JpegEncoder();
  483. let b64 = jpegEncoder.encode(canvas.context.imageData);
  484. console.log('JpegEncode:', b64);
  485. console.log('canvas.context.imageData =', canvas.context.imageData);
  486. document.getElementById('imgBase64').src = b64;
  487. });
  488. document.body.appendChild(canvas.domCanvas);
  489. */

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址