readjpg.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <draw.h>
  5. #include "imagefile.h"
  6. enum {
  7. /* Constants, all preceded by byte 0xFF */
  8. SOF =0xC0, /* Start of Frame */
  9. SOF2=0xC2, /* Start of Frame; progressive Huffman */
  10. JPG =0xC8, /* Reserved for JPEG extensions */
  11. DHT =0xC4, /* Define Huffman Tables */
  12. DAC =0xCC, /* Arithmetic coding conditioning */
  13. RST =0xD0, /* Restart interval termination */
  14. RST7 =0xD7, /* Restart interval termination (highest value) */
  15. SOI =0xD8, /* Start of Image */
  16. EOI =0xD9, /* End of Image */
  17. SOS =0xDA, /* Start of Scan */
  18. DQT =0xDB, /* Define quantization tables */
  19. DNL =0xDC, /* Define number of lines */
  20. DRI =0xDD, /* Define restart interval */
  21. DHP =0xDE, /* Define hierarchical progression */
  22. EXP =0xDF, /* Expand reference components */
  23. APPn =0xE0, /* Reserved for application segments */
  24. JPGn =0xF0, /* Reserved for JPEG extensions */
  25. COM =0xFE, /* Comment */
  26. CLAMPOFF = 300,
  27. NCLAMP = CLAMPOFF+700
  28. };
  29. typedef struct Framecomp Framecomp;
  30. typedef struct Header Header;
  31. typedef struct Huffman Huffman;
  32. struct Framecomp /* Frame component specifier from SOF marker */
  33. {
  34. int C;
  35. int H;
  36. int V;
  37. int Tq;
  38. };
  39. struct Huffman
  40. {
  41. int *size; /* malloc'ed */
  42. int *code; /* malloc'ed */
  43. int *val; /* malloc'ed */
  44. int mincode[17];
  45. int maxcode[17];
  46. int valptr[17];
  47. /* fast lookup */
  48. int value[256];
  49. int shift[256];
  50. };
  51. struct Header
  52. {
  53. Biobuf *fd;
  54. char err[256];
  55. jmp_buf errlab;
  56. /* variables in i/o routines */
  57. int sr; /* shift register, right aligned */
  58. int cnt; /* # bits in right part of sr */
  59. uchar *buf;
  60. int nbuf;
  61. int peek;
  62. int Nf;
  63. Framecomp comp[3];
  64. uchar mode;
  65. int X;
  66. int Y;
  67. int qt[4][64]; /* quantization tables */
  68. Huffman dcht[4];
  69. Huffman acht[4];
  70. int **data[3];
  71. int ndata[3];
  72. uchar *sf; /* start of frame; do better later */
  73. uchar *ss; /* start of scan; do better later */
  74. int ri; /* restart interval */
  75. /* progressive scan */
  76. Rawimage *image;
  77. Rawimage **array;
  78. int *dccoeff[3];
  79. int **accoeff[3]; /* only need 8 bits plus quantization */
  80. int naccoeff[3];
  81. int nblock[3];
  82. int nacross;
  83. int ndown;
  84. int Hmax;
  85. int Vmax;
  86. };
  87. static uchar clamp[NCLAMP];
  88. static Rawimage *readslave(Header*, int);
  89. static int readsegment(Header*, int*);
  90. static void quanttables(Header*, uchar*, int);
  91. static void huffmantables(Header*, uchar*, int);
  92. static void soiheader(Header*);
  93. static int nextbyte(Header*, int);
  94. static int int2(uchar*, int);
  95. static void nibbles(int, int*, int*);
  96. static int receive(Header*, int);
  97. static int receiveEOB(Header*, int);
  98. static int receivebit(Header*);
  99. static void restart(Header*, int);
  100. static int decode(Header*, Huffman*);
  101. static Rawimage* baselinescan(Header*, int);
  102. static void progressivescan(Header*, int);
  103. static Rawimage* progressiveIDCT(Header*, int);
  104. static void idct(int*);
  105. static void colormap1(Header*, int, Rawimage*, int*, int, int);
  106. static void colormapall1(Header*, int, Rawimage*, int*, int*, int*, int, int);
  107. static void colormap(Header*, int, Rawimage*, int**, int**, int**, int, int, int, int, int*, int*);
  108. static void jpgerror(Header*, char*, ...);
  109. static char readerr[] = "ReadJPG: read error: %r";
  110. static char memerr[] = "ReadJPG: malloc failed: %r";
  111. static int zig[64] = {
  112. 0, 1, 8, 16, 9, 2, 3, 10, 17, /* 0-7 */
  113. 24, 32, 25, 18, 11, 4, 5, /* 8-15 */
  114. 12, 19, 26, 33, 40, 48, 41, 34, /* 16-23 */
  115. 27, 20, 13, 6, 7, 14, 21, 28, /* 24-31 */
  116. 35, 42, 49, 56, 57, 50, 43, 36, /* 32-39 */
  117. 29, 22, 15, 23, 30, 37, 44, 51, /* 40-47 */
  118. 58, 59, 52, 45, 38, 31, 39, 46, /* 48-55 */
  119. 53, 60, 61, 54, 47, 55, 62, 63 /* 56-63 */
  120. };
  121. static
  122. void
  123. jpginit(void)
  124. {
  125. int k;
  126. static int inited;
  127. if(inited)
  128. return;
  129. inited = 1;
  130. for(k=0; k<CLAMPOFF; k++)
  131. clamp[k] = 0;
  132. for(; k<CLAMPOFF+256; k++)
  133. clamp[k] = k-CLAMPOFF;
  134. for(; k<NCLAMP; k++)
  135. clamp[k] = 255;
  136. }
  137. static
  138. void*
  139. jpgmalloc(Header *h, int n, int clear)
  140. {
  141. void *p;
  142. p = malloc(n);
  143. if(p == nil)
  144. jpgerror(h, memerr);
  145. if(clear)
  146. memset(p, 0, n);
  147. return p;
  148. }
  149. static
  150. void
  151. clear(void **p)
  152. {
  153. if(*p){
  154. free(*p);
  155. *p = nil;
  156. }
  157. }
  158. static
  159. void
  160. jpgfreeall(Header *h, int freeimage)
  161. {
  162. int i, j;
  163. clear(&h->buf);
  164. if(h->dccoeff[0])
  165. for(i=0; i<3; i++)
  166. clear(&h->dccoeff[i]);
  167. if(h->accoeff[0])
  168. for(i=0; i<3; i++){
  169. if(h->accoeff[i])
  170. for(j=0; j<h->naccoeff[i]; j++)
  171. clear(&h->accoeff[i][j]);
  172. clear(&h->accoeff[i]);
  173. }
  174. for(i=0; i<4; i++){
  175. clear(&h->dcht[i].size);
  176. clear(&h->acht[i].size);
  177. clear(&h->dcht[i].code);
  178. clear(&h->acht[i].code);
  179. clear(&h->dcht[i].val);
  180. clear(&h->acht[i].val);
  181. }
  182. if(h->data[0])
  183. for(i=0; i<3; i++){
  184. if(h->data[i])
  185. for(j=0; j<h->ndata[i]; j++)
  186. clear(&h->data[i][j]);
  187. clear(&h->data[i]);
  188. }
  189. if(freeimage && h->image!=nil){
  190. clear(&h->array);
  191. clear(&h->image->cmap);
  192. for(i=0; i<3; i++)
  193. clear(&h->image->chans[i]);
  194. clear(&h->image);
  195. }
  196. }
  197. static
  198. void
  199. jpgerror(Header *h, char *fmt, ...)
  200. {
  201. va_list arg;
  202. va_start(arg, fmt);
  203. vseprint(h->err, h->err+sizeof h->err, fmt, arg);
  204. va_end(arg);
  205. werrstr(h->err);
  206. jpgfreeall(h, 1);
  207. longjmp(h->errlab, 1);
  208. }
  209. Rawimage**
  210. Breadjpg(Biobuf *b, int colorspace)
  211. {
  212. Rawimage *r, **array;
  213. Header *h;
  214. char buf[ERRMAX];
  215. buf[0] = '\0';
  216. if(colorspace!=CYCbCr && colorspace!=CRGB){
  217. errstr(buf, sizeof buf); /* throw it away */
  218. werrstr("ReadJPG: unknown color space");
  219. return nil;
  220. }
  221. jpginit();
  222. h = malloc(sizeof(Header));
  223. array = malloc(sizeof(Header));
  224. if(h==nil || array==nil){
  225. free(h);
  226. free(array);
  227. return nil;
  228. }
  229. h->array = array;
  230. memset(h, 0, sizeof(Header));
  231. h->fd = b;
  232. errstr(buf, sizeof buf); /* throw it away */
  233. if(setjmp(h->errlab))
  234. r = nil;
  235. else
  236. r = readslave(h, colorspace);
  237. jpgfreeall(h, 0);
  238. free(h);
  239. array[0] = r;
  240. array[1] = nil;
  241. return array;
  242. }
  243. Rawimage**
  244. readjpg(int fd, int colorspace)
  245. {
  246. Rawimage** a;
  247. Biobuf b;
  248. if(Binit(&b, fd, OREAD) < 0)
  249. return nil;
  250. a = Breadjpg(&b, colorspace);
  251. Bterm(&b);
  252. return a;
  253. }
  254. static
  255. Rawimage*
  256. readslave(Header *header, int colorspace)
  257. {
  258. Rawimage *image;
  259. int nseg, i, H, V, m, n;
  260. uchar *b;
  261. soiheader(header);
  262. nseg = 0;
  263. image = nil;
  264. header->buf = jpgmalloc(header, 4096, 0);
  265. header->nbuf = 4096;
  266. while(header->err[0] == '\0'){
  267. nseg++;
  268. n = readsegment(header, &m);
  269. b = header->buf;
  270. switch(m){
  271. case -1:
  272. return image;
  273. case APPn+0:
  274. if(nseg==1 && strncmp((char*)b, "JFIF", 4)==0) /* JFIF header; check version */
  275. if(b[5]>1 || b[6]>2)
  276. sprint(header->err, "ReadJPG: can't handle JFIF version %d.%2d", b[5], b[6]);
  277. break;
  278. case APPn+1: case APPn+2: case APPn+3: case APPn+4: case APPn+5:
  279. case APPn+6: case APPn+7: case APPn+8: case APPn+9: case APPn+10:
  280. case APPn+11: case APPn+12: case APPn+13: case APPn+14: case APPn+15:
  281. break;
  282. case DQT:
  283. quanttables(header, b, n);
  284. break;
  285. case SOF:
  286. case SOF2:
  287. header->Y = int2(b, 1);
  288. header->X = int2(b, 3);
  289. header->Nf =b[5];
  290. for(i=0; i<header->Nf; i++){
  291. header->comp[i].C = b[6+3*i+0];
  292. nibbles(b[6+3*i+1], &H, &V);
  293. if(H<=0 || V<=0)
  294. jpgerror(header, "non-positive sampling factor (Hsamp or Vsamp)");
  295. header->comp[i].H = H;
  296. header->comp[i].V = V;
  297. header->comp[i].Tq = b[6+3*i+2];
  298. }
  299. header->mode = m;
  300. header->sf = b;
  301. break;
  302. case SOS:
  303. header->ss = b;
  304. switch(header->mode){
  305. case SOF:
  306. image = baselinescan(header, colorspace);
  307. break;
  308. case SOF2:
  309. progressivescan(header, colorspace);
  310. break;
  311. default:
  312. sprint(header->err, "unrecognized or unspecified encoding %d", header->mode);
  313. break;
  314. }
  315. break;
  316. case DHT:
  317. huffmantables(header, b, n);
  318. break;
  319. case DRI:
  320. header->ri = int2(b, 0);
  321. break;
  322. case COM:
  323. break;
  324. case EOI:
  325. if(header->mode == SOF2)
  326. image = progressiveIDCT(header, colorspace);
  327. return image;
  328. default:
  329. sprint(header->err, "ReadJPG: unknown marker %.2x", m);
  330. break;
  331. }
  332. }
  333. return image;
  334. }
  335. /* readsegment is called after reading scan, which can have */
  336. /* read ahead a byte. so we must check peek here */
  337. static
  338. int
  339. readbyte(Header *h)
  340. {
  341. uchar x;
  342. if(h->peek >= 0){
  343. x = h->peek;
  344. h->peek = -1;
  345. }else if(Bread(h->fd, &x, 1) != 1)
  346. jpgerror(h, readerr);
  347. return x;
  348. }
  349. static
  350. int
  351. marker(Header *h)
  352. {
  353. int c;
  354. while((c=readbyte(h)) == 0)
  355. fprint(2, "ReadJPG: skipping zero byte at offset %lld\n", Boffset(h->fd));
  356. if(c != 0xFF)
  357. jpgerror(h, "ReadJPG: expecting marker; found 0x%x at offset %lld\n", c, Boffset(h->fd));
  358. while(c == 0xFF)
  359. c = readbyte(h);
  360. return c;
  361. }
  362. static
  363. int
  364. int2(uchar *buf, int n)
  365. {
  366. return (buf[n]<<8) + buf[n+1];
  367. }
  368. static
  369. void
  370. nibbles(int b, int *p0, int *p1)
  371. {
  372. *p0 = (b>>4) & 0xF;
  373. *p1 = b & 0xF;
  374. }
  375. static
  376. void
  377. soiheader(Header *h)
  378. {
  379. h->peek = -1;
  380. if(marker(h) != SOI)
  381. jpgerror(h, "ReadJPG: unrecognized marker in header");
  382. h->err[0] = '\0';
  383. h->mode = 0;
  384. h->ri = 0;
  385. }
  386. static
  387. int
  388. readsegment(Header *h, int *markerp)
  389. {
  390. int m, n;
  391. uchar tmp[2];
  392. m = marker(h);
  393. switch(m){
  394. case EOI:
  395. *markerp = m;
  396. return 0;
  397. case 0:
  398. jpgerror(h, "ReadJPG: expecting marker; saw %.2x at offset %lld", m, Boffset(h->fd));
  399. }
  400. if(Bread(h->fd, tmp, 2) != 2)
  401. Readerr:
  402. jpgerror(h, readerr);
  403. n = int2(tmp, 0);
  404. if(n < 2)
  405. goto Readerr;
  406. n -= 2;
  407. if(n > h->nbuf){
  408. free(h->buf);
  409. h->buf = jpgmalloc(h, n+1, 0); /* +1 for sentinel */
  410. h->nbuf = n;
  411. }
  412. if(Bread(h->fd, h->buf, n) != n)
  413. goto Readerr;
  414. *markerp = m;
  415. return n;
  416. }
  417. static
  418. int
  419. huffmantable(Header *h, uchar *b)
  420. {
  421. Huffman *t;
  422. int Tc, th, n, nsize, i, j, k, v, cnt, code, si, sr, m;
  423. int *maxcode;
  424. nibbles(b[0], &Tc, &th);
  425. if(Tc > 1)
  426. jpgerror(h, "ReadJPG: unknown Huffman table class %d", Tc);
  427. if(th>3 || (h->mode==SOF && th>1))
  428. jpgerror(h, "ReadJPG: unknown Huffman table index %d", th);
  429. if(Tc == 0)
  430. t = &h->dcht[th];
  431. else
  432. t = &h->acht[th];
  433. /* flow chart C-2 */
  434. nsize = 0;
  435. for(i=0; i<16; i++)
  436. nsize += b[1+i];
  437. t->size = jpgmalloc(h, (nsize+1)*sizeof(int), 1);
  438. k = 0;
  439. for(i=1; i<=16; i++){
  440. n = b[i];
  441. for(j=0; j<n; j++)
  442. t->size[k++] = i;
  443. }
  444. t->size[k] = 0;
  445. /* initialize HUFFVAL */
  446. t->val = jpgmalloc(h, nsize*sizeof(int), 1);
  447. for(i=0; i<nsize; i++)
  448. t->val[i] = b[17+i];
  449. /* flow chart C-3 */
  450. t->code = jpgmalloc(h, (nsize+1)*sizeof(int), 1);
  451. k = 0;
  452. code = 0;
  453. si = t->size[0];
  454. for(;;){
  455. do
  456. t->code[k++] = code++;
  457. while(t->size[k] == si);
  458. if(t->size[k] == 0)
  459. break;
  460. do{
  461. code <<= 1;
  462. si++;
  463. }while(t->size[k] != si);
  464. }
  465. /* flow chart F-25 */
  466. i = 0;
  467. j = 0;
  468. for(;;){
  469. for(;;){
  470. i++;
  471. if(i > 16)
  472. goto outF25;
  473. if(b[i] != 0)
  474. break;
  475. t->maxcode[i] = -1;
  476. }
  477. t->valptr[i] = j;
  478. t->mincode[i] = t->code[j];
  479. j += b[i]-1;
  480. t->maxcode[i] = t->code[j];
  481. j++;
  482. }
  483. outF25:
  484. /* create byte-indexed fast path tables */
  485. maxcode = t->maxcode;
  486. /* stupid startup algorithm: just run machine for each byte value */
  487. for(v=0; v<256; ){
  488. cnt = 7;
  489. m = 1<<7;
  490. code = 0;
  491. sr = v;
  492. i = 1;
  493. for(;;i++){
  494. if(sr & m)
  495. code |= 1;
  496. if(code <= maxcode[i])
  497. break;
  498. code <<= 1;
  499. m >>= 1;
  500. if(m == 0){
  501. t->shift[v] = 0;
  502. t->value[v] = -1;
  503. goto continueBytes;
  504. }
  505. cnt--;
  506. }
  507. t->shift[v] = 8-cnt;
  508. t->value[v] = t->val[t->valptr[i]+(code-t->mincode[i])];
  509. continueBytes:
  510. v++;
  511. }
  512. return nsize;
  513. }
  514. static
  515. void
  516. huffmantables(Header *h, uchar *b, int n)
  517. {
  518. int l, mt;
  519. for(l=0; l<n; l+=17+mt)
  520. mt = huffmantable(h, &b[l]);
  521. }
  522. static
  523. int
  524. quanttable(Header *h, uchar *b)
  525. {
  526. int i, pq, tq, *q;
  527. nibbles(b[0], &pq, &tq);
  528. if(pq > 1)
  529. jpgerror(h, "ReadJPG: unknown quantization table class %d", pq);
  530. if(tq > 3)
  531. jpgerror(h, "ReadJPG: unknown quantization table index %d", tq);
  532. q = h->qt[tq];
  533. for(i=0; i<64; i++){
  534. if(pq == 0)
  535. q[i] = b[1+i];
  536. else
  537. q[i] = int2(b, 1+2*i);
  538. }
  539. return 64*(1+pq);
  540. }
  541. static
  542. void
  543. quanttables(Header *h, uchar *b, int n)
  544. {
  545. int l, m;
  546. for(l=0; l<n; l+=1+m)
  547. m = quanttable(h, &b[l]);
  548. }
  549. static
  550. Rawimage*
  551. baselinescan(Header *h, int colorspace)
  552. {
  553. int Ns, z, k, m, Hmax, Vmax, comp;
  554. int allHV1, nblock, ri, mcu, nacross, nmcu;
  555. Huffman *dcht, *acht;
  556. int block, t, diff, *qt;
  557. uchar *ss;
  558. Rawimage *image;
  559. int Td[3], Ta[3], H[3], V[3], DC[3];
  560. int ***data, *zz;
  561. ss = h->ss;
  562. Ns = ss[0];
  563. if((Ns!=3 && Ns!=1) || Ns!=h->Nf)
  564. jpgerror(h, "ReadJPG: can't handle scan not 3 components");
  565. image = jpgmalloc(h, sizeof(Rawimage), 1);
  566. h->image = image;
  567. image->r = Rect(0, 0, h->X, h->Y);
  568. image->cmap = nil;
  569. image->cmaplen = 0;
  570. image->chanlen = h->X*h->Y;
  571. image->fields = 0;
  572. image->gifflags = 0;
  573. image->gifdelay = 0;
  574. image->giftrindex = 0;
  575. if(Ns == 3)
  576. image->chandesc = colorspace;
  577. else
  578. image->chandesc = CY;
  579. image->nchans = h->Nf;
  580. for(k=0; k<h->Nf; k++)
  581. image->chans[k] = jpgmalloc(h, h->X*h->Y, 0);
  582. /* compute maximum H and V */
  583. Hmax = 0;
  584. Vmax = 0;
  585. for(comp=0; comp<Ns; comp++){
  586. if(h->comp[comp].H > Hmax)
  587. Hmax = h->comp[comp].H;
  588. if(h->comp[comp].V > Vmax)
  589. Vmax = h->comp[comp].V;
  590. }
  591. /* initialize data structures */
  592. allHV1 = 1;
  593. data = h->data;
  594. for(comp=0; comp<Ns; comp++){
  595. /* JPEG requires scan components to be in same order as in frame, */
  596. /* so if both have 3 we know scan is Y Cb Cr and there's no need to */
  597. /* reorder */
  598. nibbles(ss[2+2*comp], &Td[comp], &Ta[comp]);
  599. H[comp] = h->comp[comp].H;
  600. V[comp] = h->comp[comp].V;
  601. nblock = H[comp]*V[comp];
  602. if(nblock != 1)
  603. allHV1 = 0;
  604. data[comp] = jpgmalloc(h, nblock*sizeof(int*), 0);
  605. h->ndata[comp] = nblock;
  606. DC[comp] = 0;
  607. for(m=0; m<nblock; m++)
  608. data[comp][m] = jpgmalloc(h, 8*8*sizeof(int), 0);
  609. }
  610. ri = h->ri;
  611. h->cnt = 0;
  612. h->sr = 0;
  613. h->peek = -1;
  614. nacross = ((h->X+(8*Hmax-1))/(8*Hmax));
  615. nmcu = ((h->Y+(8*Vmax-1))/(8*Vmax))*nacross;
  616. for(mcu=0; mcu<nmcu; ){
  617. for(comp=0; comp<Ns; comp++){
  618. dcht = &h->dcht[Td[comp]];
  619. acht = &h->acht[Ta[comp]];
  620. qt = h->qt[h->comp[comp].Tq];
  621. for(block=0; block<H[comp]*V[comp]; block++){
  622. /* F-22 */
  623. t = decode(h, dcht);
  624. diff = receive(h, t);
  625. DC[comp] += diff;
  626. /* F-23 */
  627. zz = data[comp][block];
  628. memset(zz, 0, 8*8*sizeof(int));
  629. zz[0] = qt[0]*DC[comp];
  630. k = 1;
  631. for(;;){
  632. t = decode(h, acht);
  633. if((t&0x0F) == 0){
  634. if((t&0xF0) != 0xF0)
  635. break;
  636. k += 16;
  637. }else{
  638. k += t>>4;
  639. z = receive(h, t&0xF);
  640. zz[zig[k]] = z*qt[k];
  641. if(k == 63)
  642. break;
  643. k++;
  644. }
  645. }
  646. idct(zz);
  647. }
  648. }
  649. /* rotate colors to RGB and assign to bytes */
  650. if(Ns == 1) /* very easy */
  651. colormap1(h, colorspace, image, data[0][0], mcu, nacross);
  652. else if(allHV1) /* fairly easy */
  653. colormapall1(h, colorspace, image, data[0][0], data[1][0], data[2][0], mcu, nacross);
  654. else /* miserable general case */
  655. colormap(h, colorspace, image, data[0], data[1], data[2], mcu, nacross, Hmax, Vmax, H, V);
  656. /* process restart marker, if present */
  657. mcu++;
  658. if(ri>0 && mcu<nmcu && mcu%ri==0){
  659. restart(h, mcu);
  660. for(comp=0; comp<Ns; comp++)
  661. DC[comp] = 0;
  662. }
  663. }
  664. return image;
  665. }
  666. static
  667. void
  668. restart(Header *h, int mcu)
  669. {
  670. int rest, rst, nskip;
  671. rest = mcu/h->ri-1;
  672. nskip = 0;
  673. do{
  674. do{
  675. rst = nextbyte(h, 1);
  676. nskip++;
  677. }while(rst>=0 && rst!=0xFF);
  678. if(rst == 0xFF){
  679. rst = nextbyte(h, 1);
  680. nskip++;
  681. }
  682. }while(rst>=0 && (rst&~7)!=RST);
  683. if(nskip != 2)
  684. sprint(h->err, "ReadJPG: skipped %d bytes at restart %d\n", nskip-2, rest);
  685. if(rst < 0)
  686. jpgerror(h, readerr);
  687. if((rst&7) != (rest&7))
  688. jpgerror(h, "ReadJPG: expected RST%d got %d", rest&7, rst&7);
  689. h->cnt = 0;
  690. h->sr = 0;
  691. }
  692. static
  693. Rawimage*
  694. progressiveIDCT(Header *h, int colorspace)
  695. {
  696. int k, m, comp, block, Nf, bn;
  697. int allHV1, nblock, mcu, nmcu;
  698. int H[3], V[3], blockno[3];
  699. int *dccoeff, **accoeff;
  700. int ***data, *zz;
  701. Nf = h->Nf;
  702. allHV1 = 1;
  703. data = h->data;
  704. for(comp=0; comp<Nf; comp++){
  705. H[comp] = h->comp[comp].H;
  706. V[comp] = h->comp[comp].V;
  707. nblock = h->nblock[comp];
  708. if(nblock != 1)
  709. allHV1 = 0;
  710. h->ndata[comp] = nblock;
  711. data[comp] = jpgmalloc(h, nblock*sizeof(int*), 0);
  712. for(m=0; m<nblock; m++)
  713. data[comp][m] = jpgmalloc(h, 8*8*sizeof(int), 0);
  714. }
  715. memset(blockno, 0, sizeof blockno);
  716. nmcu = h->nacross*h->ndown;
  717. for(mcu=0; mcu<nmcu; mcu++){
  718. for(comp=0; comp<Nf; comp++){
  719. dccoeff = h->dccoeff[comp];
  720. accoeff = h->accoeff[comp];
  721. bn = blockno[comp];
  722. for(block=0; block<h->nblock[comp]; block++){
  723. zz = data[comp][block];
  724. memset(zz, 0, 8*8*sizeof(int));
  725. zz[0] = dccoeff[bn];
  726. for(k=1; k<64; k++)
  727. zz[zig[k]] = accoeff[bn][k];
  728. idct(zz);
  729. bn++;
  730. }
  731. blockno[comp] = bn;
  732. }
  733. /* rotate colors to RGB and assign to bytes */
  734. if(Nf == 1) /* very easy */
  735. colormap1(h, colorspace, h->image, data[0][0], mcu, h->nacross);
  736. else if(allHV1) /* fairly easy */
  737. colormapall1(h, colorspace, h->image, data[0][0], data[1][0], data[2][0], mcu, h->nacross);
  738. else /* miserable general case */
  739. colormap(h, colorspace, h->image, data[0], data[1], data[2], mcu, h->nacross, h->Hmax, h->Vmax, H, V);
  740. }
  741. return h->image;
  742. }
  743. static
  744. void
  745. progressiveinit(Header *h, int colorspace)
  746. {
  747. int Nf, Ns, j, k, nmcu, comp;
  748. uchar *ss;
  749. Rawimage *image;
  750. ss = h->ss;
  751. Ns = ss[0];
  752. Nf = h->Nf;
  753. if((Ns!=3 && Ns!=1) || Ns!=Nf)
  754. jpgerror(h, "ReadJPG: image must have 1 or 3 components");
  755. image = jpgmalloc(h, sizeof(Rawimage), 1);
  756. h->image = image;
  757. image->r = Rect(0, 0, h->X, h->Y);
  758. image->cmap = nil;
  759. image->cmaplen = 0;
  760. image->chanlen = h->X*h->Y;
  761. image->fields = 0;
  762. image->gifflags = 0;
  763. image->gifdelay = 0;
  764. image->giftrindex = 0;
  765. if(Nf == 3)
  766. image->chandesc = colorspace;
  767. else
  768. image->chandesc = CY;
  769. image->nchans = h->Nf;
  770. for(k=0; k<Nf; k++){
  771. image->chans[k] = jpgmalloc(h, h->X*h->Y, 0);
  772. h->nblock[k] = h->comp[k].H*h->comp[k].V;
  773. }
  774. /* compute maximum H and V */
  775. h->Hmax = 0;
  776. h->Vmax = 0;
  777. for(comp=0; comp<Nf; comp++){
  778. if(h->comp[comp].H > h->Hmax)
  779. h->Hmax = h->comp[comp].H;
  780. if(h->comp[comp].V > h->Vmax)
  781. h->Vmax = h->comp[comp].V;
  782. }
  783. h->nacross = ((h->X+(8*h->Hmax-1))/(8*h->Hmax));
  784. h->ndown = ((h->Y+(8*h->Vmax-1))/(8*h->Vmax));
  785. nmcu = h->nacross*h->ndown;
  786. for(k=0; k<Nf; k++){
  787. h->dccoeff[k] = jpgmalloc(h, h->nblock[k]*nmcu * sizeof(int), 1);
  788. h->accoeff[k] = jpgmalloc(h, h->nblock[k]*nmcu * sizeof(int*), 1);
  789. h->naccoeff[k] = h->nblock[k]*nmcu;
  790. for(j=0; j<h->nblock[k]*nmcu; j++)
  791. h->accoeff[k][j] = jpgmalloc(h, 64*sizeof(int), 1);
  792. }
  793. }
  794. static
  795. void
  796. progressivedc(Header *h, int comp, int Ah, int Al)
  797. {
  798. int Ns, z, ri, mcu, nmcu;
  799. int block, t, diff, qt, *dc, bn;
  800. Huffman *dcht;
  801. uchar *ss;
  802. int Td[3], DC[3], blockno[3];
  803. ss= h->ss;
  804. Ns = ss[0];
  805. if(Ns!=h->Nf)
  806. jpgerror(h, "ReadJPG: can't handle progressive with Nf!=Ns in DC scan");
  807. /* initialize data structures */
  808. h->cnt = 0;
  809. h->sr = 0;
  810. h->peek = -1;
  811. for(comp=0; comp<Ns; comp++){
  812. /*
  813. * JPEG requires scan components to be in same order as in frame,
  814. * so if both have 3 we know scan is Y Cb Cr and there's no need to
  815. * reorder
  816. */
  817. nibbles(ss[2+2*comp], &Td[comp], &z); /* z is ignored */
  818. DC[comp] = 0;
  819. }
  820. ri = h->ri;
  821. nmcu = h->nacross*h->ndown;
  822. memset(blockno, 0, sizeof blockno);
  823. for(mcu=0; mcu<nmcu; ){
  824. for(comp=0; comp<Ns; comp++){
  825. dcht = &h->dcht[Td[comp]];
  826. qt = h->qt[h->comp[comp].Tq][0];
  827. dc = h->dccoeff[comp];
  828. bn = blockno[comp];
  829. for(block=0; block<h->nblock[comp]; block++){
  830. if(Ah == 0){
  831. t = decode(h, dcht);
  832. diff = receive(h, t);
  833. DC[comp] += diff;
  834. dc[bn] = qt*DC[comp]<<Al;
  835. }else
  836. dc[bn] |= qt*receivebit(h)<<Al;
  837. bn++;
  838. }
  839. blockno[comp] = bn;
  840. }
  841. /* process restart marker, if present */
  842. mcu++;
  843. if(ri>0 && mcu<nmcu && mcu%ri==0){
  844. restart(h, mcu);
  845. for(comp=0; comp<Ns; comp++)
  846. DC[comp] = 0;
  847. }
  848. }
  849. }
  850. static
  851. void
  852. progressiveac(Header *h, int comp, int Al)
  853. {
  854. int Ns, Ss, Se, z, k, eobrun, x, y, nver, tmcu, blockno, *acc, rs;
  855. int ri, mcu, nacross, ndown, nmcu, nhor;
  856. Huffman *acht;
  857. int *qt, rrrr, ssss, q;
  858. uchar *ss;
  859. int Ta, H, V;
  860. ss = h->ss;
  861. Ns = ss[0];
  862. if(Ns != 1)
  863. jpgerror(h, "ReadJPG: illegal Ns>1 in progressive AC scan");
  864. Ss = ss[1+2];
  865. Se = ss[2+2];
  866. H = h->comp[comp].H;
  867. V = h->comp[comp].V;
  868. nacross = h->nacross*H;
  869. ndown = h->ndown*V;
  870. q = 8*h->Hmax/H;
  871. nhor = (h->X+q-1)/q;
  872. q = 8*h->Vmax/V;
  873. nver = (h->Y+q-1)/q;
  874. /* initialize data structures */
  875. h->cnt = 0;
  876. h->sr = 0;
  877. h->peek = -1;
  878. nibbles(ss[1+1], &z, &Ta); /* z is thrown away */
  879. ri = h->ri;
  880. eobrun = 0;
  881. acht = &h->acht[Ta];
  882. qt = h->qt[h->comp[comp].Tq];
  883. nmcu = nacross*ndown;
  884. mcu = 0;
  885. for(y=0; y<nver; y++){
  886. for(x=0; x<nhor; x++){
  887. /* Figure G-3 */
  888. if(eobrun > 0){
  889. --eobrun;
  890. continue;
  891. }
  892. /* arrange blockno to be in same sequence as original scan calculation. */
  893. tmcu = x/H + (nacross/H)*(y/V);
  894. blockno = tmcu*H*V + H*(y%V) + x%H;
  895. acc = h->accoeff[comp][blockno];
  896. k = Ss;
  897. for(;;){
  898. rs = decode(h, acht);
  899. /* XXX remove rrrr ssss as in baselinescan */
  900. nibbles(rs, &rrrr, &ssss);
  901. if(ssss == 0){
  902. if(rrrr < 15){
  903. eobrun = 0;
  904. if(rrrr > 0)
  905. eobrun = receiveEOB(h, rrrr)-1;
  906. break;
  907. }
  908. k += 16;
  909. }else{
  910. k += rrrr;
  911. z = receive(h, ssss);
  912. acc[k] = z*qt[k]<<Al;
  913. if(k == Se)
  914. break;
  915. k++;
  916. }
  917. }
  918. }
  919. /* process restart marker, if present */
  920. mcu++;
  921. if(ri>0 && mcu<nmcu && mcu%ri==0){
  922. restart(h, mcu);
  923. eobrun = 0;
  924. }
  925. }
  926. }
  927. static
  928. void
  929. increment(Header *h, int acc[], int k, int Pt)
  930. {
  931. if(acc[k] == 0)
  932. return;
  933. if(receivebit(h) != 0)
  934. if(acc[k] < 0)
  935. acc[k] -= Pt;
  936. else
  937. acc[k] += Pt;
  938. }
  939. static
  940. void
  941. progressiveacinc(Header *h, int comp, int Al)
  942. {
  943. int Ns, i, z, k, Ss, Se, Ta, **ac, H, V;
  944. int ri, mcu, nacross, ndown, nhor, nver, eobrun, nzeros, pending, x, y, tmcu, blockno, q, nmcu;
  945. Huffman *acht;
  946. int *qt, rrrr, ssss, *acc, rs;
  947. uchar *ss;
  948. ss = h->ss;
  949. Ns = ss[0];
  950. if(Ns != 1)
  951. jpgerror(h, "ReadJPG: illegal Ns>1 in progressive AC scan");
  952. Ss = ss[1+2];
  953. Se = ss[2+2];
  954. H = h->comp[comp].H;
  955. V = h->comp[comp].V;
  956. nacross = h->nacross*H;
  957. ndown = h->ndown*V;
  958. q = 8*h->Hmax/H;
  959. nhor = (h->X+q-1)/q;
  960. q = 8*h->Vmax/V;
  961. nver = (h->Y+q-1)/q;
  962. /* initialize data structures */
  963. h->cnt = 0;
  964. h->sr = 0;
  965. h->peek = -1;
  966. nibbles(ss[1+1], &z, &Ta); /* z is thrown away */
  967. ri = h->ri;
  968. eobrun = 0;
  969. ac = h->accoeff[comp];
  970. acht = &h->acht[Ta];
  971. qt = h->qt[h->comp[comp].Tq];
  972. nmcu = nacross*ndown;
  973. mcu = 0;
  974. pending = 0;
  975. nzeros = -1;
  976. for(y=0; y<nver; y++){
  977. for(x=0; x<nhor; x++){
  978. /* Figure G-7 */
  979. /* arrange blockno to be in same sequence as original scan calculation. */
  980. tmcu = x/H + (nacross/H)*(y/V);
  981. blockno = tmcu*H*V + H*(y%V) + x%H;
  982. acc = ac[blockno];
  983. if(eobrun > 0){
  984. if(nzeros > 0)
  985. jpgerror(h, "ReadJPG: zeros pending at block start");
  986. for(k=Ss; k<=Se; k++)
  987. increment(h, acc, k, qt[k]<<Al);
  988. --eobrun;
  989. continue;
  990. }
  991. for(k=Ss; k<=Se; ){
  992. if(nzeros >= 0){
  993. if(acc[k] != 0)
  994. increment(h, acc, k, qt[k]<<Al);
  995. else if(nzeros-- == 0)
  996. acc[k] = pending;
  997. k++;
  998. continue;
  999. }
  1000. rs = decode(h, acht);
  1001. nibbles(rs, &rrrr, &ssss);
  1002. if(ssss == 0){
  1003. if(rrrr < 15){
  1004. eobrun = 0;
  1005. if(rrrr > 0)
  1006. eobrun = receiveEOB(h, rrrr)-1;
  1007. while(k <= Se){
  1008. increment(h, acc, k, qt[k]<<Al);
  1009. k++;
  1010. }
  1011. break;
  1012. }
  1013. for(i=0; i<16; k++){
  1014. increment(h, acc, k, qt[k]<<Al);
  1015. if(acc[k] == 0)
  1016. i++;
  1017. }
  1018. continue;
  1019. }else if(ssss != 1)
  1020. jpgerror(h, "ReadJPG: ssss!=1 in progressive increment");
  1021. nzeros = rrrr;
  1022. pending = receivebit(h);
  1023. if(pending == 0)
  1024. pending = -1;
  1025. pending *= qt[k]<<Al;
  1026. }
  1027. }
  1028. /* process restart marker, if present */
  1029. mcu++;
  1030. if(ri>0 && mcu<nmcu && mcu%ri==0){
  1031. restart(h, mcu);
  1032. eobrun = 0;
  1033. nzeros = -1;
  1034. }
  1035. }
  1036. }
  1037. static
  1038. void
  1039. progressivescan(Header *h, int colorspace)
  1040. {
  1041. uchar *ss;
  1042. int Ns, Ss, Ah, Al, c, comp, i;
  1043. if(h->dccoeff[0] == nil)
  1044. progressiveinit(h, colorspace);
  1045. ss = h->ss;
  1046. Ns = ss[0];
  1047. Ss = ss[1+2*Ns];
  1048. nibbles(ss[3+2*Ns], &Ah, &Al);
  1049. c = ss[1];
  1050. comp = -1;
  1051. for(i=0; i<h->Nf; i++)
  1052. if(h->comp[i].C == c)
  1053. comp = i;
  1054. if(comp == -1)
  1055. jpgerror(h, "ReadJPG: bad component index in scan header");
  1056. if(Ss == 0){
  1057. progressivedc(h, comp, Ah, Al);
  1058. return;
  1059. }
  1060. if(Ah == 0){
  1061. progressiveac(h, comp, Al);
  1062. return;
  1063. }
  1064. progressiveacinc(h, comp, Al);
  1065. }
  1066. enum {
  1067. c1 = 2871, /* 1.402 * 2048 */
  1068. c2 = 705, /* 0.34414 * 2048 */
  1069. c3 = 1463, /* 0.71414 * 2048 */
  1070. c4 = 3629, /* 1.772 * 2048 */
  1071. };
  1072. static
  1073. void
  1074. colormap1(Header *h, int colorspace, Rawimage *image, int data[8*8], int mcu, int nacross)
  1075. {
  1076. uchar *pic;
  1077. int x, y, dx, dy, minx, miny;
  1078. int r, k, pici;
  1079. USED(colorspace);
  1080. pic = image->chans[0];
  1081. minx = 8*(mcu%nacross);
  1082. dx = 8;
  1083. if(minx+dx > h->X)
  1084. dx = h->X-minx;
  1085. miny = 8*(mcu/nacross);
  1086. dy = 8;
  1087. if(miny+dy > h->Y)
  1088. dy = h->Y-miny;
  1089. pici = miny*h->X+minx;
  1090. k = 0;
  1091. for(y=0; y<dy; y++){
  1092. for(x=0; x<dx; x++){
  1093. r = clamp[(data[k+x]+128)+CLAMPOFF];
  1094. pic[pici+x] = r;
  1095. }
  1096. pici += h->X;
  1097. k += 8;
  1098. }
  1099. }
  1100. static
  1101. void
  1102. colormapall1(Header *h, int colorspace, Rawimage *image, int data0[8*8], int data1[8*8], int data2[8*8], int mcu, int nacross)
  1103. {
  1104. uchar *rpic, *gpic, *bpic, *rp, *gp, *bp;
  1105. int *p0, *p1, *p2;
  1106. int x, y, dx, dy, minx, miny;
  1107. int r, g, b, k, pici;
  1108. int Y, Cr, Cb;
  1109. rpic = image->chans[0];
  1110. gpic = image->chans[1];
  1111. bpic = image->chans[2];
  1112. minx = 8*(mcu%nacross);
  1113. dx = 8;
  1114. if(minx+dx > h->X)
  1115. dx = h->X-minx;
  1116. miny = 8*(mcu/nacross);
  1117. dy = 8;
  1118. if(miny+dy > h->Y)
  1119. dy = h->Y-miny;
  1120. pici = miny*h->X+minx;
  1121. k = 0;
  1122. for(y=0; y<dy; y++){
  1123. p0 = data0+k;
  1124. p1 = data1+k;
  1125. p2 = data2+k;
  1126. rp = rpic+pici;
  1127. gp = gpic+pici;
  1128. bp = bpic+pici;
  1129. if(colorspace == CYCbCr)
  1130. for(x=0; x<dx; x++){
  1131. *rp++ = clamp[*p0++ + 128 + CLAMPOFF];
  1132. *gp++ = clamp[*p1++ + 128 + CLAMPOFF];
  1133. *bp++ = clamp[*p2++ + 128 + CLAMPOFF];
  1134. }
  1135. else
  1136. for(x=0; x<dx; x++){
  1137. Y = (*p0++ + 128) << 11;
  1138. Cb = *p1++;
  1139. Cr = *p2++;
  1140. r = Y+c1*Cr;
  1141. g = Y-c2*Cb-c3*Cr;
  1142. b = Y+c4*Cb;
  1143. *rp++ = clamp[(r>>11)+CLAMPOFF];
  1144. *gp++ = clamp[(g>>11)+CLAMPOFF];
  1145. *bp++ = clamp[(b>>11)+CLAMPOFF];
  1146. }
  1147. pici += h->X;
  1148. k += 8;
  1149. }
  1150. }
  1151. static
  1152. void
  1153. colormap(Header *h, int colorspace, Rawimage *image, int *data0[8*8], int *data1[8*8], int *data2[8*8], int mcu, int nacross, int Hmax, int Vmax, int *H, int *V)
  1154. {
  1155. uchar *rpic, *gpic, *bpic;
  1156. int x, y, dx, dy, minx, miny;
  1157. int r, g, b, pici, H0, H1, H2;
  1158. int t, b0, b1, b2, y0, y1, y2, x0, x1, x2;
  1159. int Y, Cr, Cb;
  1160. rpic = image->chans[0];
  1161. gpic = image->chans[1];
  1162. bpic = image->chans[2];
  1163. minx = 8*Hmax*(mcu%nacross);
  1164. dx = 8*Hmax;
  1165. if(minx+dx > h->X)
  1166. dx = h->X-minx;
  1167. miny = 8*Vmax*(mcu/nacross);
  1168. dy = 8*Vmax;
  1169. if(miny+dy > h->Y)
  1170. dy = h->Y-miny;
  1171. pici = miny*h->X+minx;
  1172. H0 = H[0];
  1173. H1 = H[1];
  1174. H2 = H[2];
  1175. for(y=0; y<dy; y++){
  1176. t = y*V[0];
  1177. b0 = H0*(t/(8*Vmax));
  1178. y0 = 8*((t/Vmax)&7);
  1179. t = y*V[1];
  1180. b1 = H1*(t/(8*Vmax));
  1181. y1 = 8*((t/Vmax)&7);
  1182. t = y*V[2];
  1183. b2 = H2*(t/(8*Vmax));
  1184. y2 = 8*((t/Vmax)&7);
  1185. x0 = 0;
  1186. x1 = 0;
  1187. x2 = 0;
  1188. for(x=0; x<dx; x++){
  1189. if(colorspace == CYCbCr){
  1190. rpic[pici+x] = clamp[data0[b0][y0+x0++*H0/Hmax] + 128 + CLAMPOFF];
  1191. gpic[pici+x] = clamp[data1[b1][y1+x1++*H1/Hmax] + 128 + CLAMPOFF];
  1192. bpic[pici+x] = clamp[data2[b2][y2+x2++*H2/Hmax] + 128 + CLAMPOFF];
  1193. }else{
  1194. Y = (data0[b0][y0+x0++*H0/Hmax]+128)<<11;
  1195. Cb = data1[b1][y1+x1++*H1/Hmax];
  1196. Cr = data2[b2][y2+x2++*H2/Hmax];
  1197. r = Y+c1*Cr;
  1198. g = Y-c2*Cb-c3*Cr;
  1199. b = Y+c4*Cb;
  1200. rpic[pici+x] = clamp[(r>>11)+CLAMPOFF];
  1201. gpic[pici+x] = clamp[(g>>11)+CLAMPOFF];
  1202. bpic[pici+x] = clamp[(b>>11)+CLAMPOFF];
  1203. }
  1204. if(x0*H0/Hmax >= 8){
  1205. x0 = 0;
  1206. b0++;
  1207. }
  1208. if(x1*H1/Hmax >= 8){
  1209. x1 = 0;
  1210. b1++;
  1211. }
  1212. if(x2*H2/Hmax >= 8){
  1213. x2 = 0;
  1214. b2++;
  1215. }
  1216. }
  1217. pici += h->X;
  1218. }
  1219. }
  1220. /*
  1221. * decode next 8-bit value from entropy-coded input. chart F-26
  1222. */
  1223. static
  1224. int
  1225. decode(Header *h, Huffman *t)
  1226. {
  1227. int code, v, cnt, m, sr, i;
  1228. int *maxcode;
  1229. static int badcode;
  1230. maxcode = t->maxcode;
  1231. if(h->cnt < 8)
  1232. nextbyte(h, 0);
  1233. /* fast lookup */
  1234. code = (h->sr>>(h->cnt-8))&0xFF;
  1235. v = t->value[code];
  1236. if(v >= 0){
  1237. h->cnt -= t->shift[code];
  1238. return v;
  1239. }
  1240. h->cnt -= 8;
  1241. if(h->cnt == 0)
  1242. nextbyte(h, 0);
  1243. h->cnt--;
  1244. cnt = h->cnt;
  1245. m = 1<<cnt;
  1246. sr = h->sr;
  1247. code <<= 1;
  1248. i = 9;
  1249. for(;;i++){
  1250. if(sr & m)
  1251. code |= 1;
  1252. if(code <= maxcode[i])
  1253. break;
  1254. code <<= 1;
  1255. m >>= 1;
  1256. if(m == 0){
  1257. sr = nextbyte(h, 0);
  1258. m = 0x80;
  1259. cnt = 8;
  1260. }
  1261. cnt--;
  1262. }
  1263. if(i >= 17){
  1264. if(badcode == 0)
  1265. fprint(2, "badly encoded %dx%d JPEG file; ignoring bad value\n", h->X, h->Y);
  1266. badcode = 1;
  1267. i = 0;
  1268. }
  1269. h->cnt = cnt;
  1270. return t->val[t->valptr[i]+(code-t->mincode[i])];
  1271. }
  1272. /*
  1273. * load next byte of input
  1274. */
  1275. static
  1276. int
  1277. nextbyte(Header *h, int marker)
  1278. {
  1279. int b, b2;
  1280. if(h->peek >= 0){
  1281. b = h->peek;
  1282. h->peek = -1;
  1283. }else{
  1284. b = Bgetc(h->fd);
  1285. if(b == Beof)
  1286. jpgerror(h, "truncated file");
  1287. b &= 0xFF;
  1288. }
  1289. if(b == 0xFF){
  1290. if(marker)
  1291. return b;
  1292. b2 = Bgetc(h->fd);
  1293. if(b2 != 0){
  1294. if(b2 == Beof)
  1295. jpgerror(h, "truncated file");
  1296. b2 &= 0xFF;
  1297. if(b2 == DNL)
  1298. jpgerror(h, "ReadJPG: DNL marker unimplemented");
  1299. /* decoder is reading into marker; satisfy it and restore state */
  1300. Bungetc(h->fd);
  1301. h->peek = b;
  1302. }
  1303. }
  1304. h->cnt += 8;
  1305. h->sr = (h->sr<<8) | b;
  1306. return b;
  1307. }
  1308. /*
  1309. * return next s bits of input, MSB first, and level shift it
  1310. */
  1311. static
  1312. int
  1313. receive(Header *h, int s)
  1314. {
  1315. int v, m;
  1316. while(h->cnt < s)
  1317. nextbyte(h, 0);
  1318. h->cnt -= s;
  1319. v = h->sr >> h->cnt;
  1320. m = (1<<s);
  1321. v &= m-1;
  1322. /* level shift */
  1323. if(v < (m>>1))
  1324. v += ~(m-1)+1;
  1325. return v;
  1326. }
  1327. /*
  1328. * return next s bits of input, decode as EOB
  1329. */
  1330. static
  1331. int
  1332. receiveEOB(Header *h, int s)
  1333. {
  1334. int v, m;
  1335. while(h->cnt < s)
  1336. nextbyte(h, 0);
  1337. h->cnt -= s;
  1338. v = h->sr >> h->cnt;
  1339. m = (1<<s);
  1340. v &= m-1;
  1341. /* level shift */
  1342. v += m;
  1343. return v;
  1344. }
  1345. /*
  1346. * return next bit of input
  1347. */
  1348. static
  1349. int
  1350. receivebit(Header *h)
  1351. {
  1352. if(h->cnt < 1)
  1353. nextbyte(h, 0);
  1354. h->cnt--;
  1355. return (h->sr >> h->cnt) & 1;
  1356. }
  1357. /*
  1358. * Scaled integer implementation.
  1359. * inverse two dimensional DCT, Chen-Wang algorithm
  1360. * (IEEE ASSP-32, pp. 803-816, Aug. 1984)
  1361. * 32-bit integer arithmetic (8 bit coefficients)
  1362. * 11 mults, 29 adds per DCT
  1363. *
  1364. * coefficients extended to 12 bit for IEEE1180-1990 compliance
  1365. */
  1366. enum {
  1367. W1 = 2841, /* 2048*sqrt(2)*cos(1*pi/16)*/
  1368. W2 = 2676, /* 2048*sqrt(2)*cos(2*pi/16)*/
  1369. W3 = 2408, /* 2048*sqrt(2)*cos(3*pi/16)*/
  1370. W5 = 1609, /* 2048*sqrt(2)*cos(5*pi/16)*/
  1371. W6 = 1108, /* 2048*sqrt(2)*cos(6*pi/16)*/
  1372. W7 = 565, /* 2048*sqrt(2)*cos(7*pi/16)*/
  1373. W1pW7 = 3406, /* W1+W7*/
  1374. W1mW7 = 2276, /* W1-W7*/
  1375. W3pW5 = 4017, /* W3+W5*/
  1376. W3mW5 = 799, /* W3-W5*/
  1377. W2pW6 = 3784, /* W2+W6*/
  1378. W2mW6 = 1567, /* W2-W6*/
  1379. R2 = 181 /* 256/sqrt(2)*/
  1380. };
  1381. static
  1382. void
  1383. idct(int b[8*8])
  1384. {
  1385. int x, y, eighty, v;
  1386. int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  1387. int *p;
  1388. /* transform horizontally*/
  1389. for(y=0; y<8; y++){
  1390. eighty = y<<3;
  1391. /* if all non-DC components are zero, just propagate the DC term*/
  1392. p = b+eighty;
  1393. if(p[1]==0)
  1394. if(p[2]==0 && p[3]==0)
  1395. if(p[4]==0 && p[5]==0)
  1396. if(p[6]==0 && p[7]==0){
  1397. v = p[0]<<3;
  1398. p[0] = v;
  1399. p[1] = v;
  1400. p[2] = v;
  1401. p[3] = v;
  1402. p[4] = v;
  1403. p[5] = v;
  1404. p[6] = v;
  1405. p[7] = v;
  1406. continue;
  1407. }
  1408. /* prescale*/
  1409. x0 = (p[0]<<11)+128;
  1410. x1 = p[4]<<11;
  1411. x2 = p[6];
  1412. x3 = p[2];
  1413. x4 = p[1];
  1414. x5 = p[7];
  1415. x6 = p[5];
  1416. x7 = p[3];
  1417. /* first stage*/
  1418. x8 = W7*(x4+x5);
  1419. x4 = x8 + W1mW7*x4;
  1420. x5 = x8 - W1pW7*x5;
  1421. x8 = W3*(x6+x7);
  1422. x6 = x8 - W3mW5*x6;
  1423. x7 = x8 - W3pW5*x7;
  1424. /* second stage*/
  1425. x8 = x0 + x1;
  1426. x0 -= x1;
  1427. x1 = W6*(x3+x2);
  1428. x2 = x1 - W2pW6*x2;
  1429. x3 = x1 + W2mW6*x3;
  1430. x1 = x4 + x6;
  1431. x4 -= x6;
  1432. x6 = x5 + x7;
  1433. x5 -= x7;
  1434. /* third stage*/
  1435. x7 = x8 + x3;
  1436. x8 -= x3;
  1437. x3 = x0 + x2;
  1438. x0 -= x2;
  1439. x2 = (R2*(x4+x5)+128)>>8;
  1440. x4 = (R2*(x4-x5)+128)>>8;
  1441. /* fourth stage*/
  1442. p[0] = (x7+x1)>>8;
  1443. p[1] = (x3+x2)>>8;
  1444. p[2] = (x0+x4)>>8;
  1445. p[3] = (x8+x6)>>8;
  1446. p[4] = (x8-x6)>>8;
  1447. p[5] = (x0-x4)>>8;
  1448. p[6] = (x3-x2)>>8;
  1449. p[7] = (x7-x1)>>8;
  1450. }
  1451. /* transform vertically*/
  1452. for(x=0; x<8; x++){
  1453. /* if all non-DC components are zero, just propagate the DC term*/
  1454. p = b+x;
  1455. if(p[8*1]==0)
  1456. if(p[8*2]==0 && p[8*3]==0)
  1457. if(p[8*4]==0 && p[8*5]==0)
  1458. if(p[8*6]==0 && p[8*7]==0){
  1459. v = (p[8*0]+32)>>6;
  1460. p[8*0] = v;
  1461. p[8*1] = v;
  1462. p[8*2] = v;
  1463. p[8*3] = v;
  1464. p[8*4] = v;
  1465. p[8*5] = v;
  1466. p[8*6] = v;
  1467. p[8*7] = v;
  1468. continue;
  1469. }
  1470. /* prescale*/
  1471. x0 = (p[8*0]<<8)+8192;
  1472. x1 = p[8*4]<<8;
  1473. x2 = p[8*6];
  1474. x3 = p[8*2];
  1475. x4 = p[8*1];
  1476. x5 = p[8*7];
  1477. x6 = p[8*5];
  1478. x7 = p[8*3];
  1479. /* first stage*/
  1480. x8 = W7*(x4+x5) + 4;
  1481. x4 = (x8+W1mW7*x4)>>3;
  1482. x5 = (x8-W1pW7*x5)>>3;
  1483. x8 = W3*(x6+x7) + 4;
  1484. x6 = (x8-W3mW5*x6)>>3;
  1485. x7 = (x8-W3pW5*x7)>>3;
  1486. /* second stage*/
  1487. x8 = x0 + x1;
  1488. x0 -= x1;
  1489. x1 = W6*(x3+x2) + 4;
  1490. x2 = (x1-W2pW6*x2)>>3;
  1491. x3 = (x1+W2mW6*x3)>>3;
  1492. x1 = x4 + x6;
  1493. x4 -= x6;
  1494. x6 = x5 + x7;
  1495. x5 -= x7;
  1496. /* third stage*/
  1497. x7 = x8 + x3;
  1498. x8 -= x3;
  1499. x3 = x0 + x2;
  1500. x0 -= x2;
  1501. x2 = (R2*(x4+x5)+128)>>8;
  1502. x4 = (R2*(x4-x5)+128)>>8;
  1503. /* fourth stage*/
  1504. p[8*0] = (x7+x1)>>14;
  1505. p[8*1] = (x3+x2)>>14;
  1506. p[8*2] = (x0+x4)>>14;
  1507. p[8*3] = (x8+x6)>>14;
  1508. p[8*4] = (x8-x6)>>14;
  1509. p[8*5] = (x0-x4)>>14;
  1510. p[8*6] = (x3-x2)>>14;
  1511. p[8*7] = (x7-x1)>>14;
  1512. }
  1513. }