img_jpeg.c 35 KB

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