bv7_jpeg_encoder

array -> jpeg

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

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

QingJ © 2025

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