img_jpeg.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /* JPEG: baseline, extended-sequential and progressive 8-bit Huffman JPEG.
  2. *
  3. * Arithmetic-coded JPEG and CMYK are rejected with IMG_ERR_UNSUPPORTED.
  4. * Baseline scans decode straight into component planes; progressive scans
  5. * keep quantized coefficients until all scans have arrived, then dequantize
  6. * and IDCT into the same planes. EXIF orientation is applied while writing
  7. * the RGB565 output. */
  8. #include "img_internal.h"
  9. static const unsigned char zigzag[64] = {
  10. 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
  11. 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
  12. 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
  13. 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
  14. };
  15. #define CONST_BITS 13
  16. #define PASS1_BITS 2
  17. #define FIX_0_298631336 2446
  18. #define FIX_0_390180644 3196
  19. #define FIX_0_541196100 4433
  20. #define FIX_0_765366865 6270
  21. #define FIX_0_899976223 7373
  22. #define FIX_1_175875602 9633
  23. #define FIX_1_501321110 12299
  24. #define FIX_1_847759065 15137
  25. #define FIX_1_961570560 16069
  26. #define FIX_2_053119869 16819
  27. #define FIX_2_562915447 20995
  28. #define FIX_3_072711026 25172
  29. static int descale(int x, int n)
  30. {
  31. return (x + (1 << (n - 1))) >> n;
  32. }
  33. static void idct_block(const int *in, unsigned char *out)
  34. {
  35. int ws[64];
  36. int i, n;
  37. for (i = 0; i < 8; i++) {
  38. if (in[8 + i] == 0 && in[16 + i] == 0 && in[24 + i] == 0 &&
  39. in[32 + i] == 0 && in[40 + i] == 0 && in[48 + i] == 0 &&
  40. in[56 + i] == 0) {
  41. int dc = in[i] * (1 << PASS1_BITS);
  42. for (n = 0; n < 8; n++)
  43. ws[i + n * 8] = dc;
  44. continue;
  45. }
  46. {
  47. int z1, z2, z3, z4, z5;
  48. int tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
  49. z2 = in[16 + i];
  50. z3 = in[48 + i];
  51. z1 = (z2 + z3) * FIX_0_541196100;
  52. tmp2 = z1 + z3 * -FIX_1_847759065;
  53. tmp3 = z1 + z2 * FIX_0_765366865;
  54. z2 = in[i];
  55. z3 = in[32 + i];
  56. tmp0 = (z2 + z3) * (1 << CONST_BITS);
  57. tmp1 = (z2 - z3) * (1 << CONST_BITS);
  58. tmp10 = tmp0 + tmp3;
  59. tmp13 = tmp0 - tmp3;
  60. tmp11 = tmp1 + tmp2;
  61. tmp12 = tmp1 - tmp2;
  62. tmp0 = in[56 + i];
  63. tmp1 = in[40 + i];
  64. tmp2 = in[24 + i];
  65. tmp3 = in[8 + i];
  66. z1 = tmp0 + tmp3;
  67. z2 = tmp1 + tmp2;
  68. z3 = tmp0 + tmp2;
  69. z4 = tmp1 + tmp3;
  70. z5 = (z3 + z4) * FIX_1_175875602;
  71. tmp0 *= FIX_0_298631336;
  72. tmp1 *= FIX_2_053119869;
  73. tmp2 *= FIX_3_072711026;
  74. tmp3 *= FIX_1_501321110;
  75. z1 *= -FIX_0_899976223;
  76. z2 *= -FIX_2_562915447;
  77. z3 *= -FIX_1_961570560;
  78. z4 *= -FIX_0_390180644;
  79. z3 += z5;
  80. z4 += z5;
  81. tmp0 += z1 + z3;
  82. tmp1 += z2 + z4;
  83. tmp2 += z2 + z3;
  84. tmp3 += z1 + z4;
  85. ws[i + 0 * 8] = descale(tmp10 + tmp3, CONST_BITS - PASS1_BITS);
  86. ws[i + 7 * 8] = descale(tmp10 - tmp3, CONST_BITS - PASS1_BITS);
  87. ws[i + 1 * 8] = descale(tmp11 + tmp2, CONST_BITS - PASS1_BITS);
  88. ws[i + 6 * 8] = descale(tmp11 - tmp2, CONST_BITS - PASS1_BITS);
  89. ws[i + 2 * 8] = descale(tmp12 + tmp1, CONST_BITS - PASS1_BITS);
  90. ws[i + 5 * 8] = descale(tmp12 - tmp1, CONST_BITS - PASS1_BITS);
  91. ws[i + 3 * 8] = descale(tmp13 + tmp0, CONST_BITS - PASS1_BITS);
  92. ws[i + 4 * 8] = descale(tmp13 - tmp0, CONST_BITS - PASS1_BITS);
  93. }
  94. }
  95. for (i = 0; i < 8; i++) {
  96. int *w = ws + i * 8;
  97. if (w[1] == 0 && w[2] == 0 && w[3] == 0 && w[4] == 0 &&
  98. w[5] == 0 && w[6] == 0 && w[7] == 0) {
  99. int dc = descale(w[0], PASS1_BITS + 3) + 128;
  100. if (dc < 0) dc = 0;
  101. if (dc > 255) dc = 255;
  102. for (n = 0; n < 8; n++)
  103. out[i * 8 + n] = (unsigned char)dc;
  104. continue;
  105. }
  106. {
  107. int z1, z2, z3, z4, z5;
  108. int tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
  109. z2 = w[2];
  110. z3 = w[6];
  111. z1 = (z2 + z3) * FIX_0_541196100;
  112. tmp2 = z1 + z3 * -FIX_1_847759065;
  113. tmp3 = z1 + z2 * FIX_0_765366865;
  114. tmp0 = (w[0] + w[4]) * (1 << CONST_BITS);
  115. tmp1 = (w[0] - w[4]) * (1 << CONST_BITS);
  116. tmp10 = tmp0 + tmp3;
  117. tmp13 = tmp0 - tmp3;
  118. tmp11 = tmp1 + tmp2;
  119. tmp12 = tmp1 - tmp2;
  120. tmp0 = w[7];
  121. tmp1 = w[5];
  122. tmp2 = w[3];
  123. tmp3 = w[1];
  124. z1 = tmp0 + tmp3;
  125. z2 = tmp1 + tmp2;
  126. z3 = tmp0 + tmp2;
  127. z4 = tmp1 + tmp3;
  128. z5 = (z3 + z4) * FIX_1_175875602;
  129. tmp0 *= FIX_0_298631336;
  130. tmp1 *= FIX_2_053119869;
  131. tmp2 *= FIX_3_072711026;
  132. tmp3 *= FIX_1_501321110;
  133. z1 *= -FIX_0_899976223;
  134. z2 *= -FIX_2_562915447;
  135. z3 *= -FIX_1_961570560;
  136. z4 *= -FIX_0_390180644;
  137. z3 += z5;
  138. z4 += z5;
  139. tmp0 += z1 + z3;
  140. tmp1 += z2 + z4;
  141. tmp2 += z2 + z3;
  142. tmp3 += z1 + z4;
  143. #define PUT(idx, val) do { \
  144. int v = descale((val), CONST_BITS + PASS1_BITS + 3) + 128; \
  145. if (v < 0) v = 0; \
  146. if (v > 255) v = 255; \
  147. out[i * 8 + (idx)] = (unsigned char)v; \
  148. } while (0)
  149. PUT(0, tmp10 + tmp3);
  150. PUT(7, tmp10 - tmp3);
  151. PUT(1, tmp11 + tmp2);
  152. PUT(6, tmp11 - tmp2);
  153. PUT(2, tmp12 + tmp1);
  154. PUT(5, tmp12 - tmp1);
  155. PUT(3, tmp13 + tmp0);
  156. PUT(4, tmp13 - tmp0);
  157. #undef PUT
  158. }
  159. }
  160. }
  161. struct jpeg_huff {
  162. unsigned char bits[17];
  163. unsigned char vals[256];
  164. int mincode[17];
  165. int maxcode[18];
  166. int valptr[17];
  167. int total;
  168. };
  169. struct jpeg_comp {
  170. unsigned char id, h, v, tq;
  171. int td, ta;
  172. unsigned int bw, bh; /* padded block grid (8x8 blocks) */
  173. unsigned int sw, sh; /* actual block grid in a scan */
  174. unsigned int pw, ph; /* padded plane size in pixels */
  175. short *coef; /* progressive coefficients, zigzag order */
  176. unsigned char *plane;
  177. int dc;
  178. };
  179. struct jpeg {
  180. const unsigned char *d;
  181. unsigned int n, pos;
  182. unsigned int w, h; /* raw dimensions */
  183. unsigned int out_w, out_h;/* after EXIF orientation */
  184. unsigned int ncomp;
  185. struct jpeg_comp comp[3];
  186. unsigned int max_h, max_v;
  187. unsigned int mcus_x, mcus_y;
  188. unsigned char qt[4][64]; /* zigzag order */
  189. struct jpeg_huff dc[4], ac[4];
  190. unsigned int restart;
  191. unsigned int rst_next;
  192. unsigned int bitbuf;
  193. int bitcnt;
  194. unsigned int marker;
  195. int progressive;
  196. unsigned int orientation;
  197. };
  198. static int jpeg_u8(struct jpeg *j, unsigned int *v)
  199. {
  200. if (j->pos >= j->n)
  201. return -1;
  202. *v = j->d[j->pos++];
  203. return 0;
  204. }
  205. static int jpeg_u16(struct jpeg *j, unsigned int *v)
  206. {
  207. unsigned int a, b;
  208. if (jpeg_u8(j, &a) || jpeg_u8(j, &b))
  209. return -1;
  210. *v = (a << 8) | b;
  211. return 0;
  212. }
  213. static int jpeg_marker(struct jpeg *j)
  214. {
  215. if (j->pos >= j->n || j->d[j->pos++] != 0xFF)
  216. return -1;
  217. while (j->pos < j->n && j->d[j->pos] == 0xFF)
  218. j->pos++;
  219. if (j->pos >= j->n)
  220. return -1;
  221. return (int)j->d[j->pos++];
  222. }
  223. static int huff_build(struct jpeg_huff *h, const unsigned int *counts,
  224. const unsigned char *vals)
  225. {
  226. int code = 0, k = 0, l, total = 0;
  227. h->total = 0;
  228. for (l = 1; l <= 16; l++) {
  229. h->bits[l] = (unsigned char)counts[l - 1];
  230. total += (int)counts[l - 1];
  231. }
  232. if (total > 256)
  233. return -1;
  234. for (l = 0; l < total; l++)
  235. h->vals[l] = vals[l];
  236. {
  237. int left = 1;
  238. for (l = 1; l <= 16; l++) {
  239. left <<= 1;
  240. left -= (int)counts[l - 1];
  241. if (left < 0)
  242. return -1;
  243. }
  244. }
  245. k = 0;
  246. for (l = 1; l <= 16; l++) {
  247. if (h->bits[l]) {
  248. h->valptr[l] = k;
  249. h->mincode[l] = code;
  250. code += h->bits[l];
  251. h->maxcode[l] = code - 1;
  252. k += h->bits[l];
  253. } else {
  254. h->valptr[l] = 0;
  255. h->mincode[l] = 0;
  256. h->maxcode[l] = -1;
  257. }
  258. code <<= 1;
  259. }
  260. h->maxcode[17] = 0x7FFFFFFF;
  261. h->total = total;
  262. return 0;
  263. }
  264. static int huff_decode(struct jpeg *j, const struct jpeg_huff *h)
  265. {
  266. int code = 0, l;
  267. if (h->total <= 0)
  268. return -1;
  269. for (l = 1; l <= 16; l++) {
  270. int b;
  271. if (j->bitcnt == 0) {
  272. unsigned int v;
  273. if (j->pos >= j->n)
  274. return -1;
  275. v = j->d[j->pos++];
  276. if (v == 0xFF) {
  277. unsigned int v2;
  278. if (j->pos >= j->n)
  279. return -1;
  280. v2 = j->d[j->pos++];
  281. if (v2 != 0x00) {
  282. j->marker = 0xFF00 | v2;
  283. j->pos -= 2;
  284. return -1;
  285. }
  286. v = 0xFF;
  287. }
  288. j->bitbuf = v;
  289. j->bitcnt = 8;
  290. }
  291. b = (int)((j->bitbuf >> (j->bitcnt - 1)) & 1);
  292. j->bitcnt--;
  293. code = (code << 1) | b;
  294. if (h->maxcode[l] >= 0 && code >= h->mincode[l] &&
  295. code <= h->maxcode[l]) {
  296. int idx = h->valptr[l] + code - h->mincode[l];
  297. if (idx < 0 || idx >= h->total)
  298. return -1;
  299. return h->vals[idx];
  300. }
  301. }
  302. return -1;
  303. }
  304. static int jpeg_bits(struct jpeg *j, int n)
  305. {
  306. int v = 0;
  307. while (n--) {
  308. int b;
  309. if (j->bitcnt == 0) {
  310. unsigned int x;
  311. if (j->pos >= j->n)
  312. return -1;
  313. x = j->d[j->pos++];
  314. if (x == 0xFF) {
  315. unsigned int x2;
  316. if (j->pos >= j->n)
  317. return -1;
  318. x2 = j->d[j->pos++];
  319. if (x2 != 0x00) {
  320. j->marker = 0xFF00 | x2;
  321. j->pos -= 2;
  322. return -1;
  323. }
  324. x = 0xFF;
  325. }
  326. j->bitbuf = x;
  327. j->bitcnt = 8;
  328. }
  329. b = (int)((j->bitbuf >> (j->bitcnt - 1)) & 1);
  330. j->bitcnt--;
  331. v = (v << 1) | b;
  332. }
  333. return v;
  334. }
  335. static int jpeg_extend(int v, int n)
  336. {
  337. if (n == 0)
  338. return 0;
  339. return (v < (1 << (n - 1))) ? v - (1 << n) + 1 : v;
  340. }
  341. static int jpeg_restart(struct jpeg *j)
  342. {
  343. j->bitcnt = 0;
  344. while (j->pos + 1 < j->n) {
  345. if (j->d[j->pos] == 0xFF && j->d[j->pos + 1] != 0x00) {
  346. unsigned int m = j->d[j->pos + 1];
  347. j->pos += 2;
  348. if (m == 0xD0 + (j->rst_next & 7)) {
  349. j->rst_next++;
  350. return 0;
  351. }
  352. return -1;
  353. }
  354. j->pos++;
  355. }
  356. return -1;
  357. }
  358. /* Skip to the next real marker after the entropy-coded scan. */
  359. static void jpeg_scan_end(struct jpeg *j)
  360. {
  361. j->bitcnt = 0;
  362. while (j->pos + 1 < j->n) {
  363. unsigned int b = j->d[j->pos];
  364. unsigned int m;
  365. if (b != 0xFF) {
  366. j->pos++;
  367. continue;
  368. }
  369. m = j->d[j->pos + 1];
  370. if (m == 0x00) {
  371. j->pos += 2;
  372. continue;
  373. }
  374. if (m >= 0xD0 && m <= 0xD7) {
  375. j->pos += 2;
  376. continue;
  377. }
  378. return;
  379. }
  380. }
  381. /* Bilinear chroma sample at a fixed-point position (16.16). */
  382. static int jpeg_chroma(const struct jpeg_comp *c, int posx, int posy)
  383. {
  384. int x0, y0, x1, y1, fx, fy;
  385. int v00, v10, v01, v11;
  386. if (posx < 0) {
  387. x0 = x1 = 0;
  388. fx = 0;
  389. } else {
  390. x0 = posx >> 16;
  391. fx = posx & 65535;
  392. if (x0 >= (int)c->pw) {
  393. x0 = x1 = (int)c->pw - 1;
  394. fx = 0;
  395. } else {
  396. x1 = x0 + 1;
  397. if (x1 >= (int)c->pw)
  398. x1 = (int)c->pw - 1;
  399. }
  400. }
  401. if (posy < 0) {
  402. y0 = y1 = 0;
  403. fy = 0;
  404. } else {
  405. y0 = posy >> 16;
  406. fy = posy & 65535;
  407. if (y0 >= (int)c->ph) {
  408. y0 = y1 = (int)c->ph - 1;
  409. fy = 0;
  410. } else {
  411. y1 = y0 + 1;
  412. if (y1 >= (int)c->ph)
  413. y1 = (int)c->ph - 1;
  414. }
  415. }
  416. v00 = c->plane[y0 * c->pw + x0];
  417. v10 = c->plane[y0 * c->pw + x1];
  418. v01 = c->plane[y1 * c->pw + x0];
  419. v11 = c->plane[y1 * c->pw + x1];
  420. return ((v00 * (65536 - fx) + v10 * fx) >> 16) * (65536 - fy) / 65536 +
  421. ((v01 * (65536 - fx) + v11 * fx) >> 16) * fy / 65536;
  422. }
  423. /* Baseline sequential block: dequantize directly in natural order. */
  424. static int jpeg_block(struct jpeg *j, struct jpeg_comp *c, int *blk)
  425. {
  426. const unsigned char *q = j->qt[c->tq];
  427. int k, t;
  428. for (k = 0; k < 64; k++)
  429. blk[k] = 0;
  430. t = huff_decode(j, &j->dc[c->td]);
  431. if (t < 0 || t > 15)
  432. return -1;
  433. if (t) {
  434. int v = jpeg_bits(j, t);
  435. if (v < 0)
  436. return -1;
  437. c->dc += jpeg_extend(v, t);
  438. }
  439. blk[0] = c->dc * q[0];
  440. for (k = 1; k < 64; ) {
  441. int rs = huff_decode(j, &j->ac[c->ta]);
  442. int r, s;
  443. if (rs < 0)
  444. return -1;
  445. r = rs >> 4;
  446. s = rs & 15;
  447. if (s == 0) {
  448. if (r == 15) {
  449. k += 16;
  450. continue;
  451. }
  452. break;
  453. }
  454. k += r;
  455. if (k >= 64)
  456. return -1;
  457. {
  458. int v = jpeg_bits(j, s);
  459. if (v < 0)
  460. return -1;
  461. blk[zigzag[k]] = jpeg_extend(v, s) * q[k];
  462. }
  463. k++;
  464. }
  465. return 0;
  466. }
  467. static int jpeg_refine_bit(struct jpeg *j, short *v, int Al)
  468. {
  469. int bit = jpeg_bits(j, 1);
  470. int p1 = 1 << Al;
  471. if (bit < 0)
  472. return -1;
  473. if (bit && ((*v & p1) == 0)) {
  474. if (*v >= 0)
  475. *v = (short)(*v + p1);
  476. else
  477. *v = (short)(*v - p1);
  478. }
  479. return 0;
  480. }
  481. /* One 8x8 block of a progressive scan. Coefficients are kept in zigzag
  482. * order; the first scan may use a nonzero Al bit position. */
  483. static int jpeg_prog_block(struct jpeg *j, struct jpeg_comp *c, short *blk,
  484. int Ss, int Se, int Ah, int Al, int *eobrun)
  485. {
  486. int k, rs, r, s;
  487. if (Ss == 0) {
  488. if (Ah == 0) {
  489. int t = huff_decode(j, &j->dc[c->td]);
  490. int diff = 0;
  491. if (t < 0 || t > 15)
  492. return -1;
  493. if (t) {
  494. int v = jpeg_bits(j, t);
  495. if (v < 0)
  496. return -1;
  497. diff = jpeg_extend(v, t);
  498. }
  499. c->dc += diff;
  500. blk[0] = (short)(c->dc << Al);
  501. } else {
  502. int bit = jpeg_bits(j, 1);
  503. if (bit < 0)
  504. return -1;
  505. blk[0] = (short)(blk[0] | (bit << Al));
  506. }
  507. return 0;
  508. }
  509. if (Ah == 0) {
  510. if (*eobrun > 0) {
  511. (*eobrun)--;
  512. return 0;
  513. }
  514. k = Ss;
  515. while (k <= Se) {
  516. rs = huff_decode(j, &j->ac[c->ta]);
  517. if (rs < 0)
  518. return -1;
  519. r = rs >> 4;
  520. s = rs & 15;
  521. if (s == 0) {
  522. if (r == 15) {
  523. k += 16;
  524. continue;
  525. }
  526. *eobrun = (1 << r) - 1;
  527. if (r) {
  528. int v = jpeg_bits(j, r);
  529. if (v < 0)
  530. return -1;
  531. *eobrun += v;
  532. }
  533. break;
  534. }
  535. k += r;
  536. if (k > Se)
  537. return -1;
  538. {
  539. int v = jpeg_bits(j, s);
  540. if (v < 0)
  541. return -1;
  542. blk[k] = (short)(jpeg_extend(v, s) << Al);
  543. }
  544. k++;
  545. }
  546. return 0;
  547. }
  548. /* AC successive-approximation refinement. */
  549. k = Ss;
  550. if (*eobrun == 0) {
  551. for (; k <= Se; k++) {
  552. int newval = 0;
  553. int p1 = 1 << Al;
  554. rs = huff_decode(j, &j->ac[c->ta]);
  555. if (rs < 0)
  556. return -1;
  557. r = rs >> 4;
  558. s = rs & 15;
  559. if (s) {
  560. int bit;
  561. if (s != 1)
  562. return -1;
  563. bit = jpeg_bits(j, 1);
  564. if (bit < 0)
  565. return -1;
  566. newval = bit ? p1 : -p1;
  567. } else {
  568. if (r != 15) {
  569. *eobrun = 1 << r;
  570. if (r) {
  571. int v = jpeg_bits(j, r);
  572. if (v < 0)
  573. return -1;
  574. *eobrun += v;
  575. }
  576. break;
  577. }
  578. }
  579. do {
  580. if (blk[k]) {
  581. if (jpeg_refine_bit(j, &blk[k], Al))
  582. return -1;
  583. } else {
  584. if (--r < 0)
  585. break;
  586. }
  587. k++;
  588. } while (k <= Se);
  589. if (newval) {
  590. if (k > Se)
  591. return -1;
  592. blk[k] = (short)newval;
  593. }
  594. }
  595. }
  596. if (*eobrun > 0) {
  597. for (; k <= Se; k++)
  598. if (blk[k])
  599. if (jpeg_refine_bit(j, &blk[k], Al))
  600. return -1;
  601. (*eobrun)--;
  602. }
  603. return 0;
  604. }
  605. /* Decode one SOS scan into planes (baseline) or coefficients (progressive). */
  606. static int jpeg_scan(struct jpeg *j, unsigned int ns, const unsigned int *sc,
  607. int Ss, int Se, int Ah, int Al)
  608. {
  609. unsigned int k, mcu = 0, total;
  610. int eobrun = 0;
  611. int interleaved = (ns > 1) || !j->progressive;
  612. for (k = 0; k < ns; k++)
  613. j->comp[sc[k]].dc = 0;
  614. j->rst_next = 0;
  615. if (interleaved) {
  616. unsigned int mx, my;
  617. total = j->mcus_x * j->mcus_y;
  618. for (my = 0; my < j->mcus_y; my++) {
  619. for (mx = 0; mx < j->mcus_x; mx++) {
  620. for (k = 0; k < ns; k++) {
  621. struct jpeg_comp *c = &j->comp[sc[k]];
  622. unsigned int by, bx;
  623. for (by = 0; by < c->v; by++) {
  624. for (bx = 0; bx < c->h; bx++) {
  625. unsigned int index =
  626. (my * c->v + by) * c->bw +
  627. (mx * c->h + bx);
  628. if (j->progressive) {
  629. if (jpeg_prog_block(j, c,
  630. c->coef + index * 64,
  631. Ss, Se, Ah, Al,
  632. &eobrun))
  633. return -1;
  634. } else {
  635. int blk[64];
  636. unsigned char p[64];
  637. unsigned int xx, yy;
  638. if (jpeg_block(j, c, blk))
  639. return -1;
  640. idct_block(blk, p);
  641. for (yy = 0; yy < 8; yy++)
  642. for (xx = 0; xx < 8; xx++) {
  643. unsigned int px = mx * c->h * 8 + bx * 8 + xx;
  644. unsigned int py = my * c->v * 8 + by * 8 + yy;
  645. c->plane[py * c->pw + px] =
  646. p[yy * 8 + xx];
  647. }
  648. }
  649. }
  650. }
  651. }
  652. mcu++;
  653. if (j->restart && (mcu % j->restart) == 0 &&
  654. mcu < total) {
  655. for (k = 0; k < ns; k++)
  656. j->comp[sc[k]].dc = 0;
  657. eobrun = 0;
  658. if (jpeg_restart(j))
  659. return -1;
  660. }
  661. }
  662. }
  663. } else {
  664. struct jpeg_comp *c = &j->comp[sc[0]];
  665. unsigned int bx, by;
  666. total = c->sw * c->sh;
  667. for (by = 0; by < c->sh; by++) {
  668. for (bx = 0; bx < c->sw; bx++) {
  669. unsigned int index = by * c->bw + bx;
  670. if (jpeg_prog_block(j, c, c->coef + index * 64,
  671. Ss, Se, Ah, Al, &eobrun))
  672. return -1;
  673. mcu++;
  674. if (j->restart && (mcu % j->restart) == 0 &&
  675. mcu < total) {
  676. c->dc = 0;
  677. eobrun = 0;
  678. if (jpeg_restart(j))
  679. return -1;
  680. }
  681. }
  682. }
  683. }
  684. return 0;
  685. }
  686. static int jpeg_sof(struct jpeg *j, unsigned int marker, int *error)
  687. {
  688. unsigned int len, prec, i;
  689. *error = IMG_ERR_CORRUPT;
  690. if (jpeg_u16(j, &len) || len < 8)
  691. return -1;
  692. if (jpeg_u8(j, &prec) || prec != 8) {
  693. *error = IMG_ERR_UNSUPPORTED;
  694. return -1;
  695. }
  696. if (jpeg_u16(j, &j->h) || jpeg_u16(j, &j->w))
  697. return -1;
  698. if (j->w == 0 || j->h == 0 || j->w > 32768 || j->h > 32768) {
  699. *error = IMG_ERR_TOOBIG;
  700. return -1;
  701. }
  702. if (jpeg_u8(j, &j->ncomp) ||
  703. (j->ncomp != 1 && j->ncomp != 3)) {
  704. *error = IMG_ERR_UNSUPPORTED;
  705. return -1;
  706. }
  707. if (len < 8 + j->ncomp * 3)
  708. return -1;
  709. if (marker != 0xC0 && marker != 0xC1 && marker != 0xC2) {
  710. *error = IMG_ERR_UNSUPPORTED;
  711. return -1;
  712. }
  713. j->progressive = (marker == 0xC2);
  714. j->max_h = j->max_v = 1;
  715. for (i = 0; i < j->ncomp; i++) {
  716. unsigned int id, hv, tq;
  717. if (jpeg_u8(j, &id) || jpeg_u8(j, &hv) || jpeg_u8(j, &tq))
  718. return -1;
  719. j->comp[i].id = (unsigned char)id;
  720. j->comp[i].h = (unsigned char)(hv >> 4);
  721. j->comp[i].v = (unsigned char)(hv & 15);
  722. j->comp[i].tq = (unsigned char)tq;
  723. if (j->comp[i].h == 0 || j->comp[i].v == 0 ||
  724. j->comp[i].h > 4 || j->comp[i].v > 4 || tq > 3) {
  725. *error = IMG_ERR_UNSUPPORTED;
  726. return -1;
  727. }
  728. if (j->comp[i].h > j->max_h)
  729. j->max_h = j->comp[i].h;
  730. if (j->comp[i].v > j->max_v)
  731. j->max_v = j->comp[i].v;
  732. }
  733. j->mcus_x = (j->w + 8 * j->max_h - 1) / (8 * j->max_h);
  734. j->mcus_y = (j->h + 8 * j->max_v - 1) / (8 * j->max_v);
  735. for (i = 0; i < j->ncomp; i++) {
  736. j->comp[i].bw = j->mcus_x * j->comp[i].h;
  737. j->comp[i].bh = j->mcus_y * j->comp[i].v;
  738. j->comp[i].sw = (j->w * j->comp[i].h + 8 * j->max_h - 1) /
  739. (8 * j->max_h);
  740. j->comp[i].sh = (j->h * j->comp[i].v + 8 * j->max_v - 1) /
  741. (8 * j->max_v);
  742. j->comp[i].pw = j->comp[i].bw * 8;
  743. j->comp[i].ph = j->comp[i].bh * 8;
  744. }
  745. j->out_w = j->w;
  746. j->out_h = j->h;
  747. if (j->orientation >= 5) {
  748. j->out_w = j->h;
  749. j->out_h = j->w;
  750. }
  751. return 0;
  752. }
  753. static int jpeg_setup(struct jpeg *j, struct img_mem *m,
  754. struct img_image *out, unsigned int *image_end)
  755. {
  756. unsigned int i;
  757. int r;
  758. r = img_alloc(m, j->out_w, j->out_h, IMG_FMT_JPEG, 0, out);
  759. if (r)
  760. return r;
  761. *image_end = img_mem_mark(m);
  762. for (i = 0; i < j->ncomp; i++) {
  763. struct jpeg_comp *c = &j->comp[i];
  764. unsigned int n;
  765. c->plane = (unsigned char *)img_mem_alloc(m, c->pw * c->ph, 16);
  766. if (!c->plane)
  767. return IMG_ERR_NOMEM;
  768. if (j->progressive) {
  769. n = c->bw * c->bh * 64;
  770. c->coef = (short *)img_mem_alloc(m, n * sizeof(short), 16);
  771. if (!c->coef)
  772. return IMG_ERR_NOMEM;
  773. for (n = 0; n < c->bw * c->bh * 64; n++)
  774. c->coef[n] = 0;
  775. }
  776. c->dc = 0;
  777. }
  778. return 0;
  779. }
  780. static void jpeg_reconstruct(struct jpeg *j)
  781. {
  782. unsigned int ci, bx, by, k;
  783. for (ci = 0; ci < j->ncomp; ci++) {
  784. struct jpeg_comp *c = &j->comp[ci];
  785. const unsigned char *q = j->qt[c->tq];
  786. for (by = 0; by < c->bh; by++) {
  787. for (bx = 0; bx < c->bw; bx++) {
  788. short *coef = c->coef + (by * c->bw + bx) * 64;
  789. int blk[64];
  790. unsigned char p[64];
  791. unsigned int xx, yy;
  792. for (k = 0; k < 64; k++)
  793. blk[zigzag[k]] = (int)coef[k] * q[k];
  794. idct_block(blk, p);
  795. for (yy = 0; yy < 8; yy++)
  796. for (xx = 0; xx < 8; xx++)
  797. c->plane[(by * 8 + yy) * c->pw +
  798. bx * 8 + xx] =
  799. p[yy * 8 + xx];
  800. }
  801. }
  802. }
  803. }
  804. static void jpeg_map_pixel(unsigned int o, unsigned int u, unsigned int v,
  805. unsigned int rw, unsigned int rh,
  806. unsigned int *x, unsigned int *y)
  807. {
  808. switch (o) {
  809. case 2: *x = rw - 1 - u; *y = v; break;
  810. case 3: *x = rw - 1 - u; *y = rh - 1 - v; break;
  811. case 4: *x = u; *y = rh - 1 - v; break;
  812. case 5: *x = v; *y = u; break;
  813. case 6: *x = v; *y = rh - 1 - u; break;
  814. case 7: *x = rw - 1 - v; *y = rh - 1 - u; break;
  815. case 8: *x = rw - 1 - v; *y = u; break;
  816. default: *x = u; *y = v; break;
  817. }
  818. }
  819. static void jpeg_output(struct jpeg *j, struct img_image *out)
  820. {
  821. unsigned int oy, ox;
  822. for (oy = 0; oy < j->out_h; oy++) {
  823. for (ox = 0; ox < j->out_w; ox++) {
  824. unsigned int x, y, yy, r, g, b;
  825. jpeg_map_pixel(j->orientation, ox, oy, j->w, j->h,
  826. &x, &y);
  827. yy = j->comp[0].plane[y * j->comp[0].pw + x];
  828. if (j->ncomp == 1) {
  829. r = g = b = yy;
  830. } else {
  831. struct jpeg_comp *cb = &j->comp[1];
  832. struct jpeg_comp *cr = &j->comp[2];
  833. unsigned int cstep_x, cstep_y, crstep_x, crstep_y;
  834. int cpos_x, cpos_y, crpos_x, crpos_y;
  835. int cbv, crv, yv;
  836. int rr, gg, bb;
  837. cstep_x = ((unsigned int)cb->h << 16) / j->max_h;
  838. cstep_y = ((unsigned int)cb->v << 16) / j->max_v;
  839. crstep_x = ((unsigned int)cr->h << 16) / j->max_h;
  840. crstep_y = ((unsigned int)cr->v << 16) / j->max_v;
  841. cpos_x = (int)(x * cstep_x) +
  842. (int)(((unsigned int)cb->h * 32768u) / j->max_h) -
  843. 32768;
  844. cpos_y = (int)(y * cstep_y) +
  845. (int)(((unsigned int)cb->v * 32768u) / j->max_v) -
  846. 32768;
  847. crpos_x = (int)(x * crstep_x) +
  848. (int)(((unsigned int)cr->h * 32768u) / j->max_h) -
  849. 32768;
  850. crpos_y = (int)(y * crstep_y) +
  851. (int)(((unsigned int)cr->v * 32768u) / j->max_v) -
  852. 32768;
  853. cbv = jpeg_chroma(cb, cpos_x, cpos_y) - 128;
  854. crv = jpeg_chroma(cr, crpos_x, crpos_y) - 128;
  855. yv = (int)yy;
  856. rr = yv + ((91881 * crv) >> 16);
  857. gg = yv - ((22554 * cbv + 46802 * crv) >> 16);
  858. bb = yv + ((116130 * cbv) >> 16);
  859. if (rr < 0) rr = 0;
  860. if (gg < 0) gg = 0;
  861. if (bb < 0) bb = 0;
  862. if (rr > 255) rr = 255;
  863. if (gg > 255) gg = 255;
  864. if (bb > 255) bb = 255;
  865. r = (unsigned int)rr;
  866. g = (unsigned int)gg;
  867. b = (unsigned int)bb;
  868. }
  869. out->px[oy * j->out_w + ox] = img_rgb565(r, g, b);
  870. }
  871. }
  872. }
  873. /* EXIF orientation tag (IFD0, tag 0x0112); 1 = normal. */
  874. static unsigned int jpeg_exif_orientation(const unsigned char *d, unsigned int n)
  875. {
  876. unsigned int pos = 2;
  877. while (pos + 4 <= n) {
  878. unsigned int m;
  879. if (d[pos] != 0xFF) {
  880. pos++;
  881. continue;
  882. }
  883. while (pos < n && d[pos] == 0xFF)
  884. pos++;
  885. if (pos >= n)
  886. break;
  887. m = d[pos++];
  888. if (m == 0xD8 || m == 0xD9 || (m >= 0xD0 && m <= 0xD7))
  889. continue;
  890. if (pos + 2 > n)
  891. break;
  892. {
  893. unsigned int len = img_be16(d + pos);
  894. if (len < 2 || pos + len > n)
  895. break;
  896. if (m == 0xE1 && len >= 8 && pos + 8 <= n &&
  897. d[pos + 2] == 'E' && d[pos + 3] == 'x' &&
  898. d[pos + 4] == 'i' && d[pos + 5] == 'f' &&
  899. d[pos + 6] == 0 && d[pos + 7] == 0) {
  900. const unsigned char *t = d + pos + 8;
  901. unsigned int avail = len - 8;
  902. int le;
  903. unsigned int magic, ifd;
  904. if (avail < 8)
  905. return 1;
  906. le = (t[0] == 'I' && t[1] == 'I');
  907. magic = le ? (t[2] | (t[3] << 8)) :
  908. ((t[2] << 8) | t[3]);
  909. if (magic != 42)
  910. return 1;
  911. ifd = le ? img_le32(t + 4) : img_be32(t + 4);
  912. if (ifd + 2 > avail)
  913. return 1;
  914. {
  915. const unsigned char *p = t + ifd;
  916. unsigned int cnt = le ? img_le16(p) :
  917. img_be16(p);
  918. unsigned int i;
  919. p += 2;
  920. for (i = 0; i < cnt; i++) {
  921. unsigned int tag, type, count, v;
  922. if ((unsigned int)(p - t) + 12 > avail)
  923. break;
  924. tag = le ? img_le16(p) : img_be16(p);
  925. type = le ? img_le16(p + 2) :
  926. img_be16(p + 2);
  927. count = le ? img_le32(p + 4) :
  928. img_be32(p + 4);
  929. if (tag == 0x0112 && type == 3 &&
  930. count == 1) {
  931. v = le ? img_le16(p + 8) :
  932. img_be16(p + 8);
  933. if (v >= 1 && v <= 8)
  934. return v;
  935. }
  936. p += 12;
  937. }
  938. }
  939. return 1;
  940. }
  941. pos += len;
  942. }
  943. }
  944. return 1;
  945. }
  946. int img_sniff_jpeg(const unsigned char *d, unsigned int n, struct img_info *out)
  947. {
  948. unsigned int pos = 2;
  949. unsigned int orientation = jpeg_exif_orientation(d, n);
  950. if (n < 4 || d[0] != 0xFF || d[1] != 0xD8)
  951. return IMG_FMT_UNKNOWN;
  952. while (pos + 4 <= n) {
  953. unsigned int m;
  954. if (d[pos] != 0xFF) {
  955. pos++;
  956. continue;
  957. }
  958. while (pos < n && d[pos] == 0xFF)
  959. pos++;
  960. if (pos >= n)
  961. break;
  962. m = d[pos++];
  963. if (m == 0xD8 || m == 0xD9 || (m >= 0xD0 && m <= 0xD7))
  964. continue;
  965. if (pos + 2 > n)
  966. break;
  967. {
  968. unsigned int len = img_be16(d + pos);
  969. if (len < 2 || pos + len > n)
  970. break;
  971. if ((m >= 0xC0 && m <= 0xCF) && m != 0xC4 &&
  972. m != 0xC8 && m != 0xCC) {
  973. unsigned int h = img_be16(d + pos + 3);
  974. unsigned int w = img_be16(d + pos + 5);
  975. unsigned int nc = d[pos + 7];
  976. unsigned int mh = 1, mv = 1, ci;
  977. if (nc <= 4 && pos + 8 + 3 * nc <= pos + len) {
  978. for (ci = 0; ci < nc; ci++) {
  979. unsigned int hv = d[pos + 9 + ci * 3];
  980. if ((hv >> 4) > mh) mh = hv >> 4;
  981. if ((hv & 15) > mv) mv = hv & 15;
  982. }
  983. out->components = nc;
  984. out->max_h = mh ? mh : 1;
  985. out->max_v = mv ? mv : 1;
  986. }
  987. out->format = IMG_FMT_JPEG;
  988. out->height = (orientation >= 5) ? w : h;
  989. out->width = (orientation >= 5) ? h : w;
  990. out->has_alpha = 0;
  991. out->progressive = (m == 0xC2);
  992. return IMG_FMT_JPEG;
  993. }
  994. pos += len;
  995. }
  996. }
  997. return IMG_FMT_UNKNOWN;
  998. }
  999. int img_decode_jpeg(const unsigned char *d, unsigned int n,
  1000. const struct img_options *opt, struct img_mem *m,
  1001. struct img_image *out)
  1002. {
  1003. struct jpeg *j;
  1004. unsigned int mark, image_end = 0, i;
  1005. int rcode, setup = 0, scans = 0;
  1006. mark = img_mem_mark(m);
  1007. j = (struct jpeg *)img_mem_alloc(m, sizeof(*j), 16);
  1008. if (!j)
  1009. return IMG_ERR_NOMEM;
  1010. for (i = 0; i < sizeof(*j); i++)
  1011. ((unsigned char *)j)[i] = 0;
  1012. j->d = d;
  1013. j->n = n;
  1014. j->pos = 2;
  1015. j->rst_next = 0;
  1016. j->orientation = jpeg_exif_orientation(d, n);
  1017. while (j->pos < n) {
  1018. int mk = jpeg_marker(j);
  1019. if (mk < 0)
  1020. goto bad;
  1021. if (mk == 0xD9)
  1022. break;
  1023. if (mk == 0xDA) {
  1024. unsigned int len, ns, ss, se, ahal, k;
  1025. unsigned int sc[3], td[3], ta[3];
  1026. unsigned int ah, al;
  1027. if (jpeg_u16(j, &len) || len < 6)
  1028. goto bad;
  1029. if (jpeg_u8(j, &ns) || ns < 1 || ns > j->ncomp)
  1030. goto bad;
  1031. if (len < 6 + 2 * ns)
  1032. goto bad;
  1033. for (k = 0; k < ns; k++) {
  1034. unsigned int id, tabs, c;
  1035. if (jpeg_u8(j, &id) || jpeg_u8(j, &tabs))
  1036. goto bad;
  1037. if ((tabs >> 4) > 3 || (tabs & 15) > 3)
  1038. goto bad;
  1039. for (c = 0; c < j->ncomp; c++)
  1040. if (j->comp[c].id == id)
  1041. break;
  1042. if (c == j->ncomp)
  1043. goto bad;
  1044. sc[k] = c;
  1045. td[k] = tabs >> 4;
  1046. ta[k] = tabs & 15;
  1047. }
  1048. if (jpeg_u8(j, &ss) || jpeg_u8(j, &se) ||
  1049. jpeg_u8(j, &ahal))
  1050. goto bad;
  1051. ah = ahal >> 4;
  1052. al = ahal & 15;
  1053. if (len > 6 + 2 * ns)
  1054. j->pos += len - (6 + 2 * ns);
  1055. if (!j->progressive) {
  1056. if (ns != j->ncomp || ss != 0 || se != 63 ||
  1057. ah != 0 || al != 0)
  1058. goto unsupported;
  1059. } else {
  1060. if (ss == 0) {
  1061. if (se != 0)
  1062. goto bad;
  1063. } else if (ns != 1 || ss > se || se > 63) {
  1064. goto bad;
  1065. }
  1066. if (ah > 13 || al > 13)
  1067. goto bad;
  1068. }
  1069. if (!setup)
  1070. goto bad;
  1071. for (k = 0; k < ns; k++) {
  1072. j->comp[sc[k]].td = (int)td[k];
  1073. j->comp[sc[k]].ta = (int)ta[k];
  1074. }
  1075. if (jpeg_scan(j, ns, sc, (int)ss, (int)se,
  1076. (int)ah, (int)al))
  1077. goto entropy_bad;
  1078. scans++;
  1079. if (!j->progressive)
  1080. break;
  1081. jpeg_scan_end(j);
  1082. continue;
  1083. }
  1084. if (mk == 0xC0 || mk == 0xC1 || mk == 0xC2) {
  1085. int err = IMG_ERR_CORRUPT;
  1086. if (setup)
  1087. goto bad;
  1088. if (jpeg_sof(j, (unsigned int)mk, &err)) {
  1089. rcode = err;
  1090. goto fail;
  1091. }
  1092. if (!img_size_ok(j->w, j->h, opt))
  1093. goto too_big;
  1094. rcode = jpeg_setup(j, m, out, &image_end);
  1095. if (rcode)
  1096. goto fail;
  1097. setup = 1;
  1098. continue;
  1099. }
  1100. if (mk >= 0xC0 && mk <= 0xCF && mk != 0xC4 &&
  1101. mk != 0xC8 && mk != 0xCC) {
  1102. rcode = IMG_ERR_UNSUPPORTED;
  1103. goto fail;
  1104. }
  1105. {
  1106. unsigned int len, k;
  1107. if (jpeg_u16(j, &len) || len < 2 || j->pos + len - 2 > n)
  1108. goto bad;
  1109. if (mk == 0xDB) { /* DQT */
  1110. unsigned int end = j->pos + len - 2;
  1111. while (j->pos < end) {
  1112. unsigned int pqtq, pq, tq;
  1113. if (jpeg_u8(j, &pqtq))
  1114. goto bad;
  1115. pq = pqtq >> 4;
  1116. tq = pqtq & 15;
  1117. if (pq > 1 || tq > 3)
  1118. goto bad;
  1119. for (k = 0; k < 64; k++) {
  1120. unsigned int v;
  1121. if (pq) {
  1122. if (jpeg_u16(j, &v))
  1123. goto bad;
  1124. j->qt[tq][k] = (unsigned char)(v >> 8);
  1125. } else {
  1126. if (jpeg_u8(j, &v))
  1127. goto bad;
  1128. j->qt[tq][k] = (unsigned char)v;
  1129. }
  1130. }
  1131. }
  1132. } else if (mk == 0xC4) { /* DHT */
  1133. unsigned int end = j->pos + len - 2;
  1134. while (j->pos < end) {
  1135. unsigned int tcth, tc, th;
  1136. unsigned int counts[16], total = 0;
  1137. if (jpeg_u8(j, &tcth))
  1138. goto bad;
  1139. tc = tcth >> 4;
  1140. th = tcth & 15;
  1141. if (tc > 1 || th > 3)
  1142. goto bad;
  1143. for (k = 0; k < 16; k++) {
  1144. unsigned int v;
  1145. if (jpeg_u8(j, &v))
  1146. goto bad;
  1147. counts[k] = v;
  1148. total += v;
  1149. }
  1150. {
  1151. unsigned char vals[256];
  1152. if (total > 256)
  1153. goto bad;
  1154. for (k = 0; k < total; k++) {
  1155. unsigned int v;
  1156. if (jpeg_u8(j, &v))
  1157. goto bad;
  1158. vals[k] = (unsigned char)v;
  1159. }
  1160. if (tc)
  1161. huff_build(&j->ac[th], counts, vals);
  1162. else
  1163. huff_build(&j->dc[th], counts, vals);
  1164. }
  1165. }
  1166. } else if (mk == 0xDD) { /* DRI */
  1167. if (jpeg_u16(j, &j->restart))
  1168. goto bad;
  1169. j->rst_next = 0;
  1170. } else {
  1171. j->pos += len - 2;
  1172. }
  1173. }
  1174. }
  1175. if (!setup || scans == 0)
  1176. goto bad;
  1177. if (j->progressive)
  1178. jpeg_reconstruct(j);
  1179. jpeg_output(j, out);
  1180. img_mem_rewind(m, image_end);
  1181. return 0;
  1182. entropy_bad:
  1183. img_mem_rewind(m, mark);
  1184. rcode = IMG_ERR_CORRUPT;
  1185. goto fail;
  1186. too_big:
  1187. rcode = IMG_ERR_TOOBIG;
  1188. goto fail;
  1189. unsupported:
  1190. rcode = IMG_ERR_UNSUPPORTED;
  1191. goto fail;
  1192. bad:
  1193. rcode = IMG_ERR_CORRUPT;
  1194. fail:
  1195. img_mem_rewind(m, mark);
  1196. return rcode;
  1197. }