| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571 |
- /* JPEG: baseline, extended-sequential and progressive 8-bit Huffman JPEG.
- *
- * Arithmetic-coded JPEG and CMYK are rejected with IMG_ERR_UNSUPPORTED.
- * Baseline scans decode straight into component planes; progressive scans
- * keep quantized coefficients until all scans have arrived, then dequantize
- * and IDCT into the same planes. EXIF orientation is applied while writing
- * the RGB565 output. */
- #include "img_internal.h"
- static const unsigned char zigzag[64] = {
- 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
- 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
- 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
- 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
- };
- #define CONST_BITS 13
- #define PASS1_BITS 2
- #define FIX_0_298631336 2446
- #define FIX_0_390180644 3196
- #define FIX_0_541196100 4433
- #define FIX_0_765366865 6270
- #define FIX_0_899976223 7373
- #define FIX_1_175875602 9633
- #define FIX_1_501321110 12299
- #define FIX_1_847759065 15137
- #define FIX_1_961570560 16069
- #define FIX_2_053119869 16819
- #define FIX_2_562915447 20995
- #define FIX_3_072711026 25172
- /* Scaled (4x4/2x2) IDCT constants, IJG/Loeffler. */
- #define FIX_0_211164243 1730
- #define FIX_0_509795579 4176
- #define FIX_0_601344887 4926
- #define FIX_0_720959822 5906
- #define FIX_0_850430095 6967
- #define FIX_1_061594337 8697
- #define FIX_1_272758580 10426
- #define FIX_1_451774981 11893
- #define FIX_2_172734803 17799
- #define FIX_3_624509785 29692
- static int descale(int x, int n)
- {
- return (x + (1 << (n - 1))) >> n;
- }
- static void idct_block(const int *in, unsigned char *out)
- {
- int ws[64];
- int i, n;
- for (i = 0; i < 8; i++) {
- if (in[8 + i] == 0 && in[16 + i] == 0 && in[24 + i] == 0 &&
- in[32 + i] == 0 && in[40 + i] == 0 && in[48 + i] == 0 &&
- in[56 + i] == 0) {
- int dc = in[i] * (1 << PASS1_BITS);
- for (n = 0; n < 8; n++)
- ws[i + n * 8] = dc;
- continue;
- }
- {
- int z1, z2, z3, z4, z5;
- int tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
- z2 = in[16 + i];
- z3 = in[48 + i];
- z1 = (z2 + z3) * FIX_0_541196100;
- tmp2 = z1 + z3 * -FIX_1_847759065;
- tmp3 = z1 + z2 * FIX_0_765366865;
- z2 = in[i];
- z3 = in[32 + i];
- tmp0 = (z2 + z3) * (1 << CONST_BITS);
- tmp1 = (z2 - z3) * (1 << CONST_BITS);
- tmp10 = tmp0 + tmp3;
- tmp13 = tmp0 - tmp3;
- tmp11 = tmp1 + tmp2;
- tmp12 = tmp1 - tmp2;
- tmp0 = in[56 + i];
- tmp1 = in[40 + i];
- tmp2 = in[24 + i];
- tmp3 = in[8 + i];
- z1 = tmp0 + tmp3;
- z2 = tmp1 + tmp2;
- z3 = tmp0 + tmp2;
- z4 = tmp1 + tmp3;
- z5 = (z3 + z4) * FIX_1_175875602;
- tmp0 *= FIX_0_298631336;
- tmp1 *= FIX_2_053119869;
- tmp2 *= FIX_3_072711026;
- tmp3 *= FIX_1_501321110;
- z1 *= -FIX_0_899976223;
- z2 *= -FIX_2_562915447;
- z3 *= -FIX_1_961570560;
- z4 *= -FIX_0_390180644;
- z3 += z5;
- z4 += z5;
- tmp0 += z1 + z3;
- tmp1 += z2 + z4;
- tmp2 += z2 + z3;
- tmp3 += z1 + z4;
- ws[i + 0 * 8] = descale(tmp10 + tmp3, CONST_BITS - PASS1_BITS);
- ws[i + 7 * 8] = descale(tmp10 - tmp3, CONST_BITS - PASS1_BITS);
- ws[i + 1 * 8] = descale(tmp11 + tmp2, CONST_BITS - PASS1_BITS);
- ws[i + 6 * 8] = descale(tmp11 - tmp2, CONST_BITS - PASS1_BITS);
- ws[i + 2 * 8] = descale(tmp12 + tmp1, CONST_BITS - PASS1_BITS);
- ws[i + 5 * 8] = descale(tmp12 - tmp1, CONST_BITS - PASS1_BITS);
- ws[i + 3 * 8] = descale(tmp13 + tmp0, CONST_BITS - PASS1_BITS);
- ws[i + 4 * 8] = descale(tmp13 - tmp0, CONST_BITS - PASS1_BITS);
- }
- }
- for (i = 0; i < 8; i++) {
- int *w = ws + i * 8;
- if (w[1] == 0 && w[2] == 0 && w[3] == 0 && w[4] == 0 &&
- w[5] == 0 && w[6] == 0 && w[7] == 0) {
- int dc = descale(w[0], PASS1_BITS + 3) + 128;
- if (dc < 0) dc = 0;
- if (dc > 255) dc = 255;
- for (n = 0; n < 8; n++)
- out[i * 8 + n] = (unsigned char)dc;
- continue;
- }
- {
- int z1, z2, z3, z4, z5;
- int tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
- z2 = w[2];
- z3 = w[6];
- z1 = (z2 + z3) * FIX_0_541196100;
- tmp2 = z1 + z3 * -FIX_1_847759065;
- tmp3 = z1 + z2 * FIX_0_765366865;
- tmp0 = (w[0] + w[4]) * (1 << CONST_BITS);
- tmp1 = (w[0] - w[4]) * (1 << CONST_BITS);
- tmp10 = tmp0 + tmp3;
- tmp13 = tmp0 - tmp3;
- tmp11 = tmp1 + tmp2;
- tmp12 = tmp1 - tmp2;
- tmp0 = w[7];
- tmp1 = w[5];
- tmp2 = w[3];
- tmp3 = w[1];
- z1 = tmp0 + tmp3;
- z2 = tmp1 + tmp2;
- z3 = tmp0 + tmp2;
- z4 = tmp1 + tmp3;
- z5 = (z3 + z4) * FIX_1_175875602;
- tmp0 *= FIX_0_298631336;
- tmp1 *= FIX_2_053119869;
- tmp2 *= FIX_3_072711026;
- tmp3 *= FIX_1_501321110;
- z1 *= -FIX_0_899976223;
- z2 *= -FIX_2_562915447;
- z3 *= -FIX_1_961570560;
- z4 *= -FIX_0_390180644;
- z3 += z5;
- z4 += z5;
- tmp0 += z1 + z3;
- tmp1 += z2 + z4;
- tmp2 += z2 + z3;
- tmp3 += z1 + z4;
- #define PUT(idx, val) do { \
- int v = descale((val), CONST_BITS + PASS1_BITS + 3) + 128; \
- if (v < 0) v = 0; \
- if (v > 255) v = 255; \
- out[i * 8 + (idx)] = (unsigned char)v; \
- } while (0)
- PUT(0, tmp10 + tmp3);
- PUT(7, tmp10 - tmp3);
- PUT(1, tmp11 + tmp2);
- PUT(6, tmp11 - tmp2);
- PUT(2, tmp12 + tmp1);
- PUT(5, tmp12 - tmp1);
- PUT(3, tmp13 + tmp0);
- PUT(4, tmp13 - tmp0);
- #undef PUT
- }
- }
- }
- #define SCALED_OUT(v, n) do { \
- int _v = descale((v), (n)) + 128; \
- if (_v < 0) _v = 0; \
- if (_v > 255) _v = 255; \
- *out++ = (unsigned char)_v; \
- } while (0)
- /* Reduced 4x4 output from the low-frequency coefficients. This is the
- * Loeffler/IGJ scaled IDCT: an 8-point column pass and a 4-point row pass,
- * ignoring the coefficients the 4x4 output cannot represent. It is the
- * same result as decoding to 8x8 and box-filtering, at roughly a quarter
- * of the work. */
- static void idct_block_4x4(const int *in, unsigned char *out)
- {
- int ws[8 * 4];
- int ctr;
- for (ctr = 0; ctr < 8; ctr++) {
- int *w = ws + ctr;
- if (ctr == 4)
- continue;
- if (in[1 * 8 + ctr] == 0 && in[2 * 8 + ctr] == 0 &&
- in[3 * 8 + ctr] == 0 && in[5 * 8 + ctr] == 0 &&
- in[6 * 8 + ctr] == 0 && in[7 * 8 + ctr] == 0) {
- int dc = in[ctr] << PASS1_BITS;
- w[0 * 8] = w[1 * 8] = w[2 * 8] = w[3 * 8] = dc;
- continue;
- }
- {
- int tmp0, tmp2, tmp10, tmp12;
- int z1, z2, z3, z4;
- tmp0 = in[0 * 8 + ctr] << (CONST_BITS + 1);
- z2 = in[2 * 8 + ctr];
- z3 = in[6 * 8 + ctr];
- tmp2 = z2 * FIX_1_847759065 +
- z3 * -FIX_0_765366865;
- tmp10 = tmp0 + tmp2;
- tmp12 = tmp0 - tmp2;
- z1 = in[7 * 8 + ctr];
- z2 = in[5 * 8 + ctr];
- z3 = in[3 * 8 + ctr];
- z4 = in[1 * 8 + ctr];
- tmp0 = z1 * -FIX_0_211164243 +
- z2 * FIX_1_451774981 +
- z3 * -FIX_2_172734803 +
- z4 * FIX_1_061594337;
- tmp2 = z1 * -FIX_0_509795579 +
- z2 * -FIX_0_601344887 +
- z3 * FIX_0_899976223 +
- z4 * FIX_2_562915447;
- w[0 * 8] = descale(tmp10 + tmp2,
- CONST_BITS - PASS1_BITS + 1);
- w[3 * 8] = descale(tmp10 - tmp2,
- CONST_BITS - PASS1_BITS + 1);
- w[1 * 8] = descale(tmp12 + tmp0,
- CONST_BITS - PASS1_BITS + 1);
- w[2 * 8] = descale(tmp12 - tmp0,
- CONST_BITS - PASS1_BITS + 1);
- }
- }
- for (ctr = 0; ctr < 4; ctr++) {
- int *w = ws + ctr * 8;
- if (w[1] == 0 && w[2] == 0 && w[3] == 0 &&
- w[5] == 0 && w[6] == 0 && w[7] == 0) {
- int dc = descale(w[0], PASS1_BITS + 3) + 128;
- if (dc < 0) dc = 0;
- if (dc > 255) dc = 255;
- out[0] = out[1] = out[2] = out[3] = (unsigned char)dc;
- out += 4;
- continue;
- }
- {
- int tmp0, tmp2, tmp10, tmp12;
- int z1, z2, z3, z4;
- tmp0 = w[0] << (CONST_BITS + 1);
- tmp2 = w[2] * FIX_1_847759065 +
- w[6] * -FIX_0_765366865;
- tmp10 = tmp0 + tmp2;
- tmp12 = tmp0 - tmp2;
- z1 = w[7];
- z2 = w[5];
- z3 = w[3];
- z4 = w[1];
- tmp0 = z1 * -FIX_0_211164243 +
- z2 * FIX_1_451774981 +
- z3 * -FIX_2_172734803 +
- z4 * FIX_1_061594337;
- tmp2 = z1 * -FIX_0_509795579 +
- z2 * -FIX_0_601344887 +
- z3 * FIX_0_899976223 +
- z4 * FIX_2_562915447;
- SCALED_OUT(tmp10 + tmp2,
- CONST_BITS + PASS1_BITS + 3 + 1);
- SCALED_OUT(tmp12 + tmp0,
- CONST_BITS + PASS1_BITS + 3 + 1);
- SCALED_OUT(tmp12 - tmp0,
- CONST_BITS + PASS1_BITS + 3 + 1);
- SCALED_OUT(tmp10 - tmp2,
- CONST_BITS + PASS1_BITS + 3 + 1);
- }
- }
- }
- /* 2x2 output (1/4 scale). */
- static void idct_block_2x2(const int *in, unsigned char *out)
- {
- int ws[8 * 2];
- int ctr;
- for (ctr = 0; ctr < 8; ctr++) {
- int *w = ws + ctr;
- int tmp0, tmp10;
- if (ctr == 2 || ctr == 4 || ctr == 6)
- continue;
- if (in[1 * 8 + ctr] == 0 && in[3 * 8 + ctr] == 0 &&
- in[5 * 8 + ctr] == 0 && in[7 * 8 + ctr] == 0) {
- int dc = in[ctr] << PASS1_BITS;
- w[0] = w[8] = dc;
- continue;
- }
- tmp10 = in[0 * 8 + ctr] << (CONST_BITS + 2);
- tmp0 = in[7 * 8 + ctr] * -FIX_0_720959822 +
- in[5 * 8 + ctr] * FIX_0_850430095 +
- in[3 * 8 + ctr] * -FIX_1_272758580 +
- in[1 * 8 + ctr] * FIX_3_624509785;
- w[0] = descale(tmp10 + tmp0, CONST_BITS - PASS1_BITS + 2);
- w[8] = descale(tmp10 - tmp0, CONST_BITS - PASS1_BITS + 2);
- }
- for (ctr = 0; ctr < 2; ctr++) {
- int *w = ws + ctr * 8;
- if (w[1] == 0 && w[3] == 0 && w[5] == 0 && w[7] == 0) {
- int dc = descale(w[0], PASS1_BITS + 3) + 128;
- if (dc < 0) dc = 0;
- if (dc > 255) dc = 255;
- out[0] = out[1] = (unsigned char)dc;
- out += 2;
- continue;
- }
- {
- int tmp0, tmp10;
- tmp10 = w[0] << (CONST_BITS + 2);
- tmp0 = w[7] * -FIX_0_720959822 +
- w[5] * FIX_0_850430095 +
- w[3] * -FIX_1_272758580 +
- w[1] * FIX_3_624509785;
- SCALED_OUT(tmp10 + tmp0,
- CONST_BITS + PASS1_BITS + 3 + 2);
- SCALED_OUT(tmp10 - tmp0,
- CONST_BITS + PASS1_BITS + 3 + 2);
- }
- }
- }
- /* 1x1 output (1/8 scale): just the DC average. */
- static void idct_block_1x1(const int *in, unsigned char *out)
- {
- int dc = descale(in[0], 3) + 128;
- if (dc < 0) dc = 0;
- if (dc > 255) dc = 255;
- out[0] = (unsigned char)dc;
- }
- static void idct_block_out(unsigned int scale, const int *blk,
- unsigned char *p)
- {
- if (scale == 1)
- idct_block(blk, p);
- else if (scale == 2)
- idct_block_4x4(blk, p);
- else if (scale == 4)
- idct_block_2x2(blk, p);
- else
- idct_block_1x1(blk, p);
- }
- struct jpeg_huff {
- unsigned char bits[17];
- unsigned char vals[256];
- int mincode[17];
- int maxcode[18];
- int valptr[17];
- int total;
- };
- struct jpeg_comp {
- unsigned char id, h, v, tq;
- int td, ta;
- unsigned int bw, bh; /* padded block grid (8x8 blocks) */
- unsigned int sw, sh; /* actual block grid in a scan */
- unsigned int pw, ph; /* padded plane size in pixels */
- short *coef; /* progressive coefficients, zigzag order */
- unsigned char *plane;
- int dc;
- };
- struct jpeg {
- const unsigned char *d;
- unsigned int n, pos;
- unsigned int w, h; /* raw dimensions */
- unsigned int sw, sh; /* raw dimensions after the IDCT scale */
- unsigned int out_w, out_h;/* after EXIF orientation */
- unsigned int scale; /* 1, 2, 4 or 8: output is w/scale */
- unsigned int want_w, want_h; /* caller's fit bounds, 0 = none */
- unsigned int ncomp;
- struct jpeg_comp comp[3];
- unsigned int max_h, max_v;
- unsigned int mcus_x, mcus_y;
- unsigned char qt[4][64]; /* zigzag order */
- struct jpeg_huff dc[4], ac[4];
- unsigned int restart;
- unsigned int rst_next;
- unsigned int bitbuf;
- int bitcnt;
- unsigned int marker;
- int progressive;
- unsigned int orientation;
- };
- static int jpeg_u8(struct jpeg *j, unsigned int *v)
- {
- if (j->pos >= j->n)
- return -1;
- *v = j->d[j->pos++];
- return 0;
- }
- static int jpeg_u16(struct jpeg *j, unsigned int *v)
- {
- unsigned int a, b;
- if (jpeg_u8(j, &a) || jpeg_u8(j, &b))
- return -1;
- *v = (a << 8) | b;
- return 0;
- }
- static int jpeg_marker(struct jpeg *j)
- {
- if (j->pos >= j->n || j->d[j->pos++] != 0xFF)
- return -1;
- while (j->pos < j->n && j->d[j->pos] == 0xFF)
- j->pos++;
- if (j->pos >= j->n)
- return -1;
- return (int)j->d[j->pos++];
- }
- static int huff_build(struct jpeg_huff *h, const unsigned int *counts,
- const unsigned char *vals)
- {
- int code = 0, k = 0, l, total = 0;
- h->total = 0;
- for (l = 1; l <= 16; l++) {
- h->bits[l] = (unsigned char)counts[l - 1];
- total += (int)counts[l - 1];
- }
- if (total > 256)
- return -1;
- for (l = 0; l < total; l++)
- h->vals[l] = vals[l];
- {
- int left = 1;
- for (l = 1; l <= 16; l++) {
- left <<= 1;
- left -= (int)counts[l - 1];
- if (left < 0)
- return -1;
- }
- }
- k = 0;
- for (l = 1; l <= 16; l++) {
- if (h->bits[l]) {
- h->valptr[l] = k;
- h->mincode[l] = code;
- code += h->bits[l];
- h->maxcode[l] = code - 1;
- k += h->bits[l];
- } else {
- h->valptr[l] = 0;
- h->mincode[l] = 0;
- h->maxcode[l] = -1;
- }
- code <<= 1;
- }
- h->maxcode[17] = 0x7FFFFFFF;
- h->total = total;
- return 0;
- }
- static int huff_decode(struct jpeg *j, const struct jpeg_huff *h)
- {
- int code = 0, l;
- if (h->total <= 0)
- return -1;
- for (l = 1; l <= 16; l++) {
- int b;
- if (j->bitcnt == 0) {
- unsigned int v;
- if (j->pos >= j->n)
- return -1;
- v = j->d[j->pos++];
- if (v == 0xFF) {
- unsigned int v2;
- if (j->pos >= j->n)
- return -1;
- v2 = j->d[j->pos++];
- if (v2 != 0x00) {
- j->marker = 0xFF00 | v2;
- j->pos -= 2;
- return -1;
- }
- v = 0xFF;
- }
- j->bitbuf = v;
- j->bitcnt = 8;
- }
- b = (int)((j->bitbuf >> (j->bitcnt - 1)) & 1);
- j->bitcnt--;
- code = (code << 1) | b;
- if (h->maxcode[l] >= 0 && code >= h->mincode[l] &&
- code <= h->maxcode[l]) {
- int idx = h->valptr[l] + code - h->mincode[l];
- if (idx < 0 || idx >= h->total)
- return -1;
- return h->vals[idx];
- }
- }
- return -1;
- }
- static int jpeg_bits(struct jpeg *j, int n)
- {
- int v = 0;
- while (n--) {
- int b;
- if (j->bitcnt == 0) {
- unsigned int x;
- if (j->pos >= j->n)
- return -1;
- x = j->d[j->pos++];
- if (x == 0xFF) {
- unsigned int x2;
- if (j->pos >= j->n)
- return -1;
- x2 = j->d[j->pos++];
- if (x2 != 0x00) {
- j->marker = 0xFF00 | x2;
- j->pos -= 2;
- return -1;
- }
- x = 0xFF;
- }
- j->bitbuf = x;
- j->bitcnt = 8;
- }
- b = (int)((j->bitbuf >> (j->bitcnt - 1)) & 1);
- j->bitcnt--;
- v = (v << 1) | b;
- }
- return v;
- }
- static int jpeg_extend(int v, int n)
- {
- if (n == 0)
- return 0;
- return (v < (1 << (n - 1))) ? v - (1 << n) + 1 : v;
- }
- static int jpeg_restart(struct jpeg *j)
- {
- j->bitcnt = 0;
- while (j->pos + 1 < j->n) {
- if (j->d[j->pos] == 0xFF && j->d[j->pos + 1] != 0x00) {
- unsigned int m = j->d[j->pos + 1];
- j->pos += 2;
- if (m == 0xD0 + (j->rst_next & 7)) {
- j->rst_next++;
- return 0;
- }
- return -1;
- }
- j->pos++;
- }
- return -1;
- }
- /* Skip to the next real marker after the entropy-coded scan. */
- static void jpeg_scan_end(struct jpeg *j)
- {
- j->bitcnt = 0;
- while (j->pos + 1 < j->n) {
- unsigned int b = j->d[j->pos];
- unsigned int m;
- if (b != 0xFF) {
- j->pos++;
- continue;
- }
- m = j->d[j->pos + 1];
- if (m == 0x00) {
- j->pos += 2;
- continue;
- }
- if (m >= 0xD0 && m <= 0xD7) {
- j->pos += 2;
- continue;
- }
- return;
- }
- }
- /* Bilinear chroma sample at a fixed-point position (16.16). */
- static int jpeg_chroma(const struct jpeg_comp *c, int posx, int posy)
- {
- int x0, y0, x1, y1, fx, fy;
- int v00, v10, v01, v11;
- if (posx < 0) {
- x0 = x1 = 0;
- fx = 0;
- } else {
- x0 = posx >> 16;
- fx = posx & 65535;
- if (x0 >= (int)c->pw) {
- x0 = x1 = (int)c->pw - 1;
- fx = 0;
- } else {
- x1 = x0 + 1;
- if (x1 >= (int)c->pw)
- x1 = (int)c->pw - 1;
- }
- }
- if (posy < 0) {
- y0 = y1 = 0;
- fy = 0;
- } else {
- y0 = posy >> 16;
- fy = posy & 65535;
- if (y0 >= (int)c->ph) {
- y0 = y1 = (int)c->ph - 1;
- fy = 0;
- } else {
- y1 = y0 + 1;
- if (y1 >= (int)c->ph)
- y1 = (int)c->ph - 1;
- }
- }
- v00 = c->plane[y0 * c->pw + x0];
- v10 = c->plane[y0 * c->pw + x1];
- v01 = c->plane[y1 * c->pw + x0];
- v11 = c->plane[y1 * c->pw + x1];
- return ((v00 * (65536 - fx) + v10 * fx) >> 16) * (65536 - fy) / 65536 +
- ((v01 * (65536 - fx) + v11 * fx) >> 16) * fy / 65536;
- }
- /* Baseline sequential block: dequantize directly in natural order. */
- static int jpeg_block(struct jpeg *j, struct jpeg_comp *c, int *blk)
- {
- const unsigned char *q = j->qt[c->tq];
- int k, t;
- for (k = 0; k < 64; k++)
- blk[k] = 0;
- t = huff_decode(j, &j->dc[c->td]);
- if (t < 0 || t > 15)
- return -1;
- if (t) {
- int v = jpeg_bits(j, t);
- if (v < 0)
- return -1;
- c->dc += jpeg_extend(v, t);
- }
- blk[0] = c->dc * q[0];
- for (k = 1; k < 64; ) {
- int rs = huff_decode(j, &j->ac[c->ta]);
- int r, s;
- if (rs < 0)
- return -1;
- r = rs >> 4;
- s = rs & 15;
- if (s == 0) {
- if (r == 15) {
- k += 16;
- continue;
- }
- break;
- }
- k += r;
- if (k >= 64)
- return -1;
- {
- int v = jpeg_bits(j, s);
- if (v < 0)
- return -1;
- blk[zigzag[k]] = jpeg_extend(v, s) * q[k];
- }
- k++;
- }
- return 0;
- }
- static int jpeg_refine_bit(struct jpeg *j, short *v, int Al)
- {
- int bit = jpeg_bits(j, 1);
- int p1 = 1 << Al;
- if (bit < 0)
- return -1;
- if (bit && ((*v & p1) == 0)) {
- if (*v >= 0)
- *v = (short)(*v + p1);
- else
- *v = (short)(*v - p1);
- }
- return 0;
- }
- /* One 8x8 block of a progressive scan. Coefficients are kept in zigzag
- * order; the first scan may use a nonzero Al bit position. */
- static int jpeg_prog_block(struct jpeg *j, struct jpeg_comp *c, short *blk,
- int Ss, int Se, int Ah, int Al, int *eobrun)
- {
- int k, rs, r, s;
- if (Ss == 0) {
- if (Ah == 0) {
- int t = huff_decode(j, &j->dc[c->td]);
- int diff = 0;
- if (t < 0 || t > 15)
- return -1;
- if (t) {
- int v = jpeg_bits(j, t);
- if (v < 0)
- return -1;
- diff = jpeg_extend(v, t);
- }
- c->dc += diff;
- blk[0] = (short)(c->dc << Al);
- } else {
- int bit = jpeg_bits(j, 1);
- if (bit < 0)
- return -1;
- blk[0] = (short)(blk[0] | (bit << Al));
- }
- return 0;
- }
- if (Ah == 0) {
- if (*eobrun > 0) {
- (*eobrun)--;
- return 0;
- }
- k = Ss;
- while (k <= Se) {
- rs = huff_decode(j, &j->ac[c->ta]);
- if (rs < 0)
- return -1;
- r = rs >> 4;
- s = rs & 15;
- if (s == 0) {
- if (r == 15) {
- k += 16;
- continue;
- }
- *eobrun = (1 << r) - 1;
- if (r) {
- int v = jpeg_bits(j, r);
- if (v < 0)
- return -1;
- *eobrun += v;
- }
- break;
- }
- k += r;
- if (k > Se)
- return -1;
- {
- int v = jpeg_bits(j, s);
- if (v < 0)
- return -1;
- blk[k] = (short)(jpeg_extend(v, s) << Al);
- }
- k++;
- }
- return 0;
- }
- /* AC successive-approximation refinement. */
- k = Ss;
- if (*eobrun == 0) {
- for (; k <= Se; k++) {
- int newval = 0;
- int p1 = 1 << Al;
- rs = huff_decode(j, &j->ac[c->ta]);
- if (rs < 0)
- return -1;
- r = rs >> 4;
- s = rs & 15;
- if (s) {
- int bit;
- if (s != 1)
- return -1;
- bit = jpeg_bits(j, 1);
- if (bit < 0)
- return -1;
- newval = bit ? p1 : -p1;
- } else {
- if (r != 15) {
- *eobrun = 1 << r;
- if (r) {
- int v = jpeg_bits(j, r);
- if (v < 0)
- return -1;
- *eobrun += v;
- }
- break;
- }
- }
- do {
- if (blk[k]) {
- if (jpeg_refine_bit(j, &blk[k], Al))
- return -1;
- } else {
- if (--r < 0)
- break;
- }
- k++;
- } while (k <= Se);
- if (newval) {
- if (k > Se)
- return -1;
- blk[k] = (short)newval;
- }
- }
- }
- if (*eobrun > 0) {
- for (; k <= Se; k++)
- if (blk[k])
- if (jpeg_refine_bit(j, &blk[k], Al))
- return -1;
- (*eobrun)--;
- }
- return 0;
- }
- /* Decode one SOS scan into planes (baseline) or coefficients (progressive). */
- static int jpeg_scan(struct jpeg *j, unsigned int ns, const unsigned int *sc,
- int Ss, int Se, int Ah, int Al)
- {
- unsigned int k, mcu = 0, total;
- int eobrun = 0;
- int interleaved = (ns > 1) || !j->progressive;
- for (k = 0; k < ns; k++)
- j->comp[sc[k]].dc = 0;
- j->rst_next = 0;
- if (interleaved) {
- unsigned int mx, my;
- total = j->mcus_x * j->mcus_y;
- for (my = 0; my < j->mcus_y; my++) {
- for (mx = 0; mx < j->mcus_x; mx++) {
- for (k = 0; k < ns; k++) {
- struct jpeg_comp *c = &j->comp[sc[k]];
- unsigned int by, bx;
- for (by = 0; by < c->v; by++) {
- for (bx = 0; bx < c->h; bx++) {
- unsigned int index =
- (my * c->v + by) * c->bw +
- (mx * c->h + bx);
- if (j->progressive) {
- if (jpeg_prog_block(j, c,
- c->coef + index * 64,
- Ss, Se, Ah, Al,
- &eobrun))
- return -1;
- } else {
- int blk[64];
- unsigned char p[64];
- unsigned int xx, yy;
- unsigned int s =
- 8 / j->scale;
- if (jpeg_block(j, c, blk))
- return -1;
- idct_block_out(j->scale,
- blk, p);
- for (yy = 0; yy < s; yy++)
- for (xx = 0; xx < s; xx++) {
- unsigned int px = mx * c->h * s + bx * s + xx;
- unsigned int py = my * c->v * s + by * s + yy;
- c->plane[py * c->pw + px] =
- p[yy * s + xx];
- }
- }
- }
- }
- }
- mcu++;
- if (j->restart && (mcu % j->restart) == 0 &&
- mcu < total) {
- for (k = 0; k < ns; k++)
- j->comp[sc[k]].dc = 0;
- eobrun = 0;
- if (jpeg_restart(j))
- return -1;
- }
- }
- }
- } else {
- struct jpeg_comp *c = &j->comp[sc[0]];
- unsigned int bx, by;
- total = c->sw * c->sh;
- for (by = 0; by < c->sh; by++) {
- for (bx = 0; bx < c->sw; bx++) {
- unsigned int index = by * c->bw + bx;
- if (jpeg_prog_block(j, c, c->coef + index * 64,
- Ss, Se, Ah, Al, &eobrun))
- return -1;
- mcu++;
- if (j->restart && (mcu % j->restart) == 0 &&
- mcu < total) {
- c->dc = 0;
- eobrun = 0;
- if (jpeg_restart(j))
- return -1;
- }
- }
- }
- }
- return 0;
- }
- static int jpeg_sof(struct jpeg *j, unsigned int marker, int *error)
- {
- unsigned int len, prec, i;
- *error = IMG_ERR_CORRUPT;
- if (jpeg_u16(j, &len) || len < 8)
- return -1;
- if (jpeg_u8(j, &prec) || prec != 8) {
- *error = IMG_ERR_UNSUPPORTED;
- return -1;
- }
- if (jpeg_u16(j, &j->h) || jpeg_u16(j, &j->w))
- return -1;
- if (j->w == 0 || j->h == 0 || j->w > 32768 || j->h > 32768) {
- *error = IMG_ERR_TOOBIG;
- return -1;
- }
- if (jpeg_u8(j, &j->ncomp) ||
- (j->ncomp != 1 && j->ncomp != 3)) {
- *error = IMG_ERR_UNSUPPORTED;
- return -1;
- }
- if (len < 8 + j->ncomp * 3)
- return -1;
- if (marker != 0xC0 && marker != 0xC1 && marker != 0xC2) {
- *error = IMG_ERR_UNSUPPORTED;
- return -1;
- }
- j->progressive = (marker == 0xC2);
- j->max_h = j->max_v = 1;
- for (i = 0; i < j->ncomp; i++) {
- unsigned int id, hv, tq;
- if (jpeg_u8(j, &id) || jpeg_u8(j, &hv) || jpeg_u8(j, &tq))
- return -1;
- j->comp[i].id = (unsigned char)id;
- j->comp[i].h = (unsigned char)(hv >> 4);
- j->comp[i].v = (unsigned char)(hv & 15);
- j->comp[i].tq = (unsigned char)tq;
- if (j->comp[i].h == 0 || j->comp[i].v == 0 ||
- j->comp[i].h > 4 || j->comp[i].v > 4 || tq > 3) {
- *error = IMG_ERR_UNSUPPORTED;
- return -1;
- }
- if (j->comp[i].h > j->max_h)
- j->max_h = j->comp[i].h;
- if (j->comp[i].v > j->max_v)
- j->max_v = j->comp[i].v;
- }
- j->mcus_x = (j->w + 8 * j->max_h - 1) / (8 * j->max_h);
- j->mcus_y = (j->h + 8 * j->max_v - 1) / (8 * j->max_v);
- {
- unsigned int d = 1;
- if (j->want_w || j->want_h) {
- unsigned int dmax = 8;
- if (j->want_w) {
- unsigned int q = j->w / j->want_w;
- if (q < dmax)
- dmax = q;
- }
- if (j->want_h) {
- unsigned int q = j->h / j->want_h;
- if (q < dmax)
- dmax = q;
- }
- if (dmax >= 8)
- d = 8;
- else if (dmax >= 4)
- d = 4;
- else if (dmax >= 2)
- d = 2;
- }
- j->scale = d;
- }
- for (i = 0; i < j->ncomp; i++) {
- j->comp[i].bw = j->mcus_x * j->comp[i].h;
- j->comp[i].bh = j->mcus_y * j->comp[i].v;
- j->comp[i].sw = (j->w * j->comp[i].h + 8 * j->max_h - 1) /
- (8 * j->max_h);
- j->comp[i].sh = (j->h * j->comp[i].v + 8 * j->max_v - 1) /
- (8 * j->max_v);
- j->comp[i].pw = j->comp[i].bw * (8 / j->scale);
- j->comp[i].ph = j->comp[i].bh * (8 / j->scale);
- }
- j->sw = (j->w + j->scale - 1) / j->scale;
- j->sh = (j->h + j->scale - 1) / j->scale;
- j->out_w = j->sw;
- j->out_h = j->sh;
- if (j->orientation >= 5) {
- j->out_w = j->sh;
- j->out_h = j->sw;
- }
- return 0;
- }
- static int jpeg_setup(struct jpeg *j, struct img_mem *m,
- struct img_image *out, unsigned int *image_end)
- {
- unsigned int i;
- int r;
- r = img_alloc(m, j->out_w, j->out_h, IMG_FMT_JPEG, 0, out);
- if (r)
- return r;
- *image_end = img_mem_mark(m);
- for (i = 0; i < j->ncomp; i++) {
- struct jpeg_comp *c = &j->comp[i];
- unsigned int n;
- c->plane = (unsigned char *)img_mem_alloc(m, c->pw * c->ph, 16);
- if (!c->plane)
- return IMG_ERR_NOMEM;
- if (j->progressive) {
- n = c->bw * c->bh * 64;
- c->coef = (short *)img_mem_alloc(m, n * sizeof(short), 16);
- if (!c->coef)
- return IMG_ERR_NOMEM;
- for (n = 0; n < c->bw * c->bh * 64; n++)
- c->coef[n] = 0;
- }
- c->dc = 0;
- }
- return 0;
- }
- static void jpeg_reconstruct(struct jpeg *j)
- {
- unsigned int ci, bx, by, k;
- for (ci = 0; ci < j->ncomp; ci++) {
- struct jpeg_comp *c = &j->comp[ci];
- const unsigned char *q = j->qt[c->tq];
- unsigned int s = 8 / j->scale;
- for (by = 0; by < c->bh; by++) {
- for (bx = 0; bx < c->bw; bx++) {
- short *coef = c->coef + (by * c->bw + bx) * 64;
- int blk[64];
- unsigned char p[64];
- unsigned int xx, yy;
- for (k = 0; k < 64; k++)
- blk[zigzag[k]] = (int)coef[k] * q[k];
- idct_block_out(j->scale, blk, p);
- for (yy = 0; yy < s; yy++)
- for (xx = 0; xx < s; xx++)
- c->plane[(by * s + yy) * c->pw +
- bx * s + xx] =
- p[yy * s + xx];
- }
- }
- }
- }
- static void jpeg_map_pixel(unsigned int o, unsigned int u, unsigned int v,
- unsigned int rw, unsigned int rh,
- unsigned int *x, unsigned int *y)
- {
- switch (o) {
- case 2: *x = rw - 1 - u; *y = v; break;
- case 3: *x = rw - 1 - u; *y = rh - 1 - v; break;
- case 4: *x = u; *y = rh - 1 - v; break;
- case 5: *x = v; *y = u; break;
- case 6: *x = v; *y = rh - 1 - u; break;
- case 7: *x = rw - 1 - v; *y = rh - 1 - u; break;
- case 8: *x = rw - 1 - v; *y = u; break;
- default: *x = u; *y = v; break;
- }
- }
- static void jpeg_output(struct jpeg *j, struct img_image *out)
- {
- unsigned int oy, ox;
- for (oy = 0; oy < j->out_h; oy++) {
- for (ox = 0; ox < j->out_w; ox++) {
- unsigned int x, y, yy, r, g, b;
- jpeg_map_pixel(j->orientation, ox, oy, j->sw, j->sh,
- &x, &y);
- yy = j->comp[0].plane[y * j->comp[0].pw + x];
- if (j->ncomp == 1) {
- r = g = b = yy;
- } else {
- struct jpeg_comp *cb = &j->comp[1];
- struct jpeg_comp *cr = &j->comp[2];
- unsigned int cstep_x, cstep_y, crstep_x, crstep_y;
- int cpos_x, cpos_y, crpos_x, crpos_y;
- int cbv, crv, yv;
- int rr, gg, bb;
- cstep_x = ((unsigned int)cb->h << 16) / j->max_h;
- cstep_y = ((unsigned int)cb->v << 16) / j->max_v;
- crstep_x = ((unsigned int)cr->h << 16) / j->max_h;
- crstep_y = ((unsigned int)cr->v << 16) / j->max_v;
- cpos_x = (int)(x * cstep_x) +
- (int)(((unsigned int)cb->h * 32768u) / j->max_h) -
- 32768;
- cpos_y = (int)(y * cstep_y) +
- (int)(((unsigned int)cb->v * 32768u) / j->max_v) -
- 32768;
- crpos_x = (int)(x * crstep_x) +
- (int)(((unsigned int)cr->h * 32768u) / j->max_h) -
- 32768;
- crpos_y = (int)(y * crstep_y) +
- (int)(((unsigned int)cr->v * 32768u) / j->max_v) -
- 32768;
- cbv = jpeg_chroma(cb, cpos_x, cpos_y) - 128;
- crv = jpeg_chroma(cr, crpos_x, crpos_y) - 128;
- yv = (int)yy;
- rr = yv + ((91881 * crv) >> 16);
- gg = yv - ((22554 * cbv + 46802 * crv) >> 16);
- bb = yv + ((116130 * cbv) >> 16);
- if (rr < 0) rr = 0;
- if (gg < 0) gg = 0;
- if (bb < 0) bb = 0;
- if (rr > 255) rr = 255;
- if (gg > 255) gg = 255;
- if (bb > 255) bb = 255;
- r = (unsigned int)rr;
- g = (unsigned int)gg;
- b = (unsigned int)bb;
- }
- out->px[oy * j->out_w + ox] = img_rgb565(r, g, b);
- }
- }
- }
- /* EXIF orientation tag (IFD0, tag 0x0112); 1 = normal. */
- static unsigned int jpeg_exif_orientation(const unsigned char *d, unsigned int n)
- {
- unsigned int pos = 2;
- while (pos + 4 <= n) {
- unsigned int m;
- if (d[pos] != 0xFF) {
- pos++;
- continue;
- }
- while (pos < n && d[pos] == 0xFF)
- pos++;
- if (pos >= n)
- break;
- m = d[pos++];
- if (m == 0xD8 || m == 0xD9 || (m >= 0xD0 && m <= 0xD7))
- continue;
- if (pos + 2 > n)
- break;
- {
- unsigned int len = img_be16(d + pos);
- if (len < 2 || pos + len > n)
- break;
- if (m == 0xE1 && len >= 8 && pos + 8 <= n &&
- d[pos + 2] == 'E' && d[pos + 3] == 'x' &&
- d[pos + 4] == 'i' && d[pos + 5] == 'f' &&
- d[pos + 6] == 0 && d[pos + 7] == 0) {
- const unsigned char *t = d + pos + 8;
- unsigned int avail = len - 8;
- int le;
- unsigned int magic, ifd;
- if (avail < 8)
- return 1;
- le = (t[0] == 'I' && t[1] == 'I');
- magic = le ? (t[2] | (t[3] << 8)) :
- ((t[2] << 8) | t[3]);
- if (magic != 42)
- return 1;
- ifd = le ? img_le32(t + 4) : img_be32(t + 4);
- if (ifd + 2 > avail)
- return 1;
- {
- const unsigned char *p = t + ifd;
- unsigned int cnt = le ? img_le16(p) :
- img_be16(p);
- unsigned int i;
- p += 2;
- for (i = 0; i < cnt; i++) {
- unsigned int tag, type, count, v;
- if ((unsigned int)(p - t) + 12 > avail)
- break;
- tag = le ? img_le16(p) : img_be16(p);
- type = le ? img_le16(p + 2) :
- img_be16(p + 2);
- count = le ? img_le32(p + 4) :
- img_be32(p + 4);
- if (tag == 0x0112 && type == 3 &&
- count == 1) {
- v = le ? img_le16(p + 8) :
- img_be16(p + 8);
- if (v >= 1 && v <= 8)
- return v;
- }
- p += 12;
- }
- }
- return 1;
- }
- pos += len;
- }
- }
- return 1;
- }
- int img_sniff_jpeg(const unsigned char *d, unsigned int n, struct img_info *out)
- {
- unsigned int pos = 2;
- unsigned int orientation = jpeg_exif_orientation(d, n);
- if (n < 4 || d[0] != 0xFF || d[1] != 0xD8)
- return IMG_FMT_UNKNOWN;
- while (pos + 4 <= n) {
- unsigned int m;
- if (d[pos] != 0xFF) {
- pos++;
- continue;
- }
- while (pos < n && d[pos] == 0xFF)
- pos++;
- if (pos >= n)
- break;
- m = d[pos++];
- if (m == 0xD8 || m == 0xD9 || (m >= 0xD0 && m <= 0xD7))
- continue;
- if (pos + 2 > n)
- break;
- {
- unsigned int len = img_be16(d + pos);
- if (len < 2 || pos + len > n)
- break;
- if ((m >= 0xC0 && m <= 0xCF) && m != 0xC4 &&
- m != 0xC8 && m != 0xCC) {
- unsigned int h = img_be16(d + pos + 3);
- unsigned int w = img_be16(d + pos + 5);
- unsigned int nc = d[pos + 7];
- unsigned int mh = 1, mv = 1, ci;
- if (nc <= 4 && pos + 8 + 3 * nc <= pos + len) {
- for (ci = 0; ci < nc; ci++) {
- unsigned int hv = d[pos + 9 + ci * 3];
- if ((hv >> 4) > mh) mh = hv >> 4;
- if ((hv & 15) > mv) mv = hv & 15;
- }
- out->components = nc;
- out->max_h = mh ? mh : 1;
- out->max_v = mv ? mv : 1;
- }
- out->format = IMG_FMT_JPEG;
- out->height = (orientation >= 5) ? w : h;
- out->width = (orientation >= 5) ? h : w;
- out->has_alpha = 0;
- out->progressive = (m == 0xC2);
- return IMG_FMT_JPEG;
- }
- pos += len;
- }
- }
- return IMG_FMT_UNKNOWN;
- }
- int img_decode_jpeg(const unsigned char *d, unsigned int n,
- const struct img_options *opt, struct img_mem *m,
- struct img_image *out)
- {
- struct jpeg *j;
- unsigned int mark, image_end = 0, i;
- int rcode, setup = 0, scans = 0;
- mark = img_mem_mark(m);
- j = (struct jpeg *)img_mem_alloc(m, sizeof(*j), 16);
- if (!j)
- return IMG_ERR_NOMEM;
- for (i = 0; i < sizeof(*j); i++)
- ((unsigned char *)j)[i] = 0;
- j->d = d;
- j->n = n;
- j->pos = 2;
- j->rst_next = 0;
- j->orientation = jpeg_exif_orientation(d, n);
- j->want_w = opt ? opt->max_w : 0;
- j->want_h = opt ? opt->max_h : 0;
- while (j->pos < n) {
- int mk = jpeg_marker(j);
- if (mk < 0)
- goto bad;
- if (mk == 0xD9)
- break;
- if (mk == 0xDA) {
- unsigned int len, ns, ss, se, ahal, k;
- unsigned int sc[3], td[3], ta[3];
- unsigned int ah, al;
- if (jpeg_u16(j, &len) || len < 6)
- goto bad;
- if (jpeg_u8(j, &ns) || ns < 1 || ns > j->ncomp)
- goto bad;
- if (len < 6 + 2 * ns)
- goto bad;
- for (k = 0; k < ns; k++) {
- unsigned int id, tabs, c;
- if (jpeg_u8(j, &id) || jpeg_u8(j, &tabs))
- goto bad;
- if ((tabs >> 4) > 3 || (tabs & 15) > 3)
- goto bad;
- for (c = 0; c < j->ncomp; c++)
- if (j->comp[c].id == id)
- break;
- if (c == j->ncomp)
- goto bad;
- sc[k] = c;
- td[k] = tabs >> 4;
- ta[k] = tabs & 15;
- }
- if (jpeg_u8(j, &ss) || jpeg_u8(j, &se) ||
- jpeg_u8(j, &ahal))
- goto bad;
- ah = ahal >> 4;
- al = ahal & 15;
- if (len > 6 + 2 * ns)
- j->pos += len - (6 + 2 * ns);
- if (!j->progressive) {
- if (ns != j->ncomp || ss != 0 || se != 63 ||
- ah != 0 || al != 0)
- goto unsupported;
- } else {
- if (ss == 0) {
- if (se != 0)
- goto bad;
- } else if (ns != 1 || ss > se || se > 63) {
- goto bad;
- }
- if (ah > 13 || al > 13)
- goto bad;
- }
- if (!setup)
- goto bad;
- for (k = 0; k < ns; k++) {
- j->comp[sc[k]].td = (int)td[k];
- j->comp[sc[k]].ta = (int)ta[k];
- }
- if (jpeg_scan(j, ns, sc, (int)ss, (int)se,
- (int)ah, (int)al))
- goto entropy_bad;
- scans++;
- if (!j->progressive)
- break;
- jpeg_scan_end(j);
- continue;
- }
- if (mk == 0xC0 || mk == 0xC1 || mk == 0xC2) {
- int err = IMG_ERR_CORRUPT;
- if (setup)
- goto bad;
- if (jpeg_sof(j, (unsigned int)mk, &err)) {
- rcode = err;
- goto fail;
- }
- if (!img_size_ok(j->w, j->h, opt))
- goto too_big;
- rcode = jpeg_setup(j, m, out, &image_end);
- if (rcode)
- goto fail;
- setup = 1;
- continue;
- }
- if (mk >= 0xC0 && mk <= 0xCF && mk != 0xC4 &&
- mk != 0xC8 && mk != 0xCC) {
- rcode = IMG_ERR_UNSUPPORTED;
- goto fail;
- }
- {
- unsigned int len, k;
- if (jpeg_u16(j, &len) || len < 2 || j->pos + len - 2 > n)
- goto bad;
- if (mk == 0xDB) { /* DQT */
- unsigned int end = j->pos + len - 2;
- while (j->pos < end) {
- unsigned int pqtq, pq, tq;
- if (jpeg_u8(j, &pqtq))
- goto bad;
- pq = pqtq >> 4;
- tq = pqtq & 15;
- if (pq > 1 || tq > 3)
- goto bad;
- for (k = 0; k < 64; k++) {
- unsigned int v;
- if (pq) {
- if (jpeg_u16(j, &v))
- goto bad;
- j->qt[tq][k] = (unsigned char)(v >> 8);
- } else {
- if (jpeg_u8(j, &v))
- goto bad;
- j->qt[tq][k] = (unsigned char)v;
- }
- }
- }
- } else if (mk == 0xC4) { /* DHT */
- unsigned int end = j->pos + len - 2;
- while (j->pos < end) {
- unsigned int tcth, tc, th;
- unsigned int counts[16], total = 0;
- if (jpeg_u8(j, &tcth))
- goto bad;
- tc = tcth >> 4;
- th = tcth & 15;
- if (tc > 1 || th > 3)
- goto bad;
- for (k = 0; k < 16; k++) {
- unsigned int v;
- if (jpeg_u8(j, &v))
- goto bad;
- counts[k] = v;
- total += v;
- }
- {
- unsigned char vals[256];
- if (total > 256)
- goto bad;
- for (k = 0; k < total; k++) {
- unsigned int v;
- if (jpeg_u8(j, &v))
- goto bad;
- vals[k] = (unsigned char)v;
- }
- if (tc)
- huff_build(&j->ac[th], counts, vals);
- else
- huff_build(&j->dc[th], counts, vals);
- }
- }
- } else if (mk == 0xDD) { /* DRI */
- if (jpeg_u16(j, &j->restart))
- goto bad;
- j->rst_next = 0;
- } else {
- j->pos += len - 2;
- }
- }
- }
- if (!setup || scans == 0)
- goto bad;
- if (j->progressive)
- jpeg_reconstruct(j);
- jpeg_output(j, out);
- img_mem_rewind(m, image_end);
- return 0;
- entropy_bad:
- img_mem_rewind(m, mark);
- rcode = IMG_ERR_CORRUPT;
- goto fail;
- too_big:
- rcode = IMG_ERR_TOOBIG;
- goto fail;
- unsupported:
- rcode = IMG_ERR_UNSUPPORTED;
- goto fail;
- bad:
- rcode = IMG_ERR_CORRUPT;
- fail:
- img_mem_rewind(m, mark);
- return rcode;
- }
|