jpgd.cpp 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172
  1. // jpgd.cpp - C++ class for JPEG decompression.
  2. // Public domain, Rich Geldreich <richgel99@gmail.com>
  3. // Alex Evans: Linear memory allocator (taken from jpge.h).
  4. // v1.04, May. 19, 2012: Code tweaks to fix VS2008 static code analysis warnings (all looked harmless)
  5. //
  6. // Supports progressive and baseline sequential JPEG image files, and the most common chroma subsampling factors: Y, H1V1, H2V1, H1V2, and H2V2.
  7. //
  8. // Chroma upsampling quality: H2V2 is upsampled in the frequency domain, H2V1 and H1V2 are upsampled using point sampling.
  9. // Chroma upsampling reference: "Fast Scheme for Image Size Change in the Compressed Domain"
  10. // http://vision.ai.uiuc.edu/~dugad/research/dct/index.html
  11. #include "jpgd.h"
  12. #include <string.h>
  13. #include <assert.h>
  14. #define JPGD_ASSERT(x) assert(x)
  15. #ifdef _MSC_VER
  16. #pragma warning (disable : 4611) // warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable
  17. #endif
  18. // Set to 1 to enable freq. domain chroma upsampling on images using H2V2 subsampling (0=faster nearest neighbor sampling).
  19. // This is slower, but results in higher quality on images with highly saturated colors.
  20. #define JPGD_SUPPORT_FREQ_DOMAIN_UPSAMPLING 1
  21. #define JPGD_TRUE (1)
  22. #define JPGD_FALSE (0)
  23. #define JPGD_MAX(a,b) (((a)>(b)) ? (a) : (b))
  24. #define JPGD_MIN(a,b) (((a)<(b)) ? (a) : (b))
  25. namespace jpgd {
  26. static inline void *jpgd_malloc(size_t nSize) { return malloc(nSize); }
  27. static inline void jpgd_free(void *p) { free(p); }
  28. // DCT coefficients are stored in this sequence.
  29. static int g_ZAG[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 };
  30. enum JPEG_MARKER
  31. {
  32. M_SOF0 = 0xC0, M_SOF1 = 0xC1, M_SOF2 = 0xC2, M_SOF3 = 0xC3, M_SOF5 = 0xC5, M_SOF6 = 0xC6, M_SOF7 = 0xC7, M_JPG = 0xC8,
  33. M_SOF9 = 0xC9, M_SOF10 = 0xCA, M_SOF11 = 0xCB, M_SOF13 = 0xCD, M_SOF14 = 0xCE, M_SOF15 = 0xCF, M_DHT = 0xC4, M_DAC = 0xCC,
  34. M_RST0 = 0xD0, M_RST1 = 0xD1, M_RST2 = 0xD2, M_RST3 = 0xD3, M_RST4 = 0xD4, M_RST5 = 0xD5, M_RST6 = 0xD6, M_RST7 = 0xD7,
  35. M_SOI = 0xD8, M_EOI = 0xD9, M_SOS = 0xDA, M_DQT = 0xDB, M_DNL = 0xDC, M_DRI = 0xDD, M_DHP = 0xDE, M_EXP = 0xDF,
  36. M_APP0 = 0xE0, M_APP15 = 0xEF, M_JPG0 = 0xF0, M_JPG13 = 0xFD, M_COM = 0xFE, M_TEM = 0x01, M_ERROR = 0x100, RST0 = 0xD0
  37. };
  38. enum JPEG_SUBSAMPLING { JPGD_GRAYSCALE = 0, JPGD_YH1V1, JPGD_YH2V1, JPGD_YH1V2, JPGD_YH2V2 };
  39. #define CONST_BITS 13
  40. #define PASS1_BITS 2
  41. #define SCALEDONE ((int32)1)
  42. #define FIX_0_298631336 ((int32)2446) /* FIX(0.298631336) */
  43. #define FIX_0_390180644 ((int32)3196) /* FIX(0.390180644) */
  44. #define FIX_0_541196100 ((int32)4433) /* FIX(0.541196100) */
  45. #define FIX_0_765366865 ((int32)6270) /* FIX(0.765366865) */
  46. #define FIX_0_899976223 ((int32)7373) /* FIX(0.899976223) */
  47. #define FIX_1_175875602 ((int32)9633) /* FIX(1.175875602) */
  48. #define FIX_1_501321110 ((int32)12299) /* FIX(1.501321110) */
  49. #define FIX_1_847759065 ((int32)15137) /* FIX(1.847759065) */
  50. #define FIX_1_961570560 ((int32)16069) /* FIX(1.961570560) */
  51. #define FIX_2_053119869 ((int32)16819) /* FIX(2.053119869) */
  52. #define FIX_2_562915447 ((int32)20995) /* FIX(2.562915447) */
  53. #define FIX_3_072711026 ((int32)25172) /* FIX(3.072711026) */
  54. #define DESCALE(x,n) (((x) + (SCALEDONE << ((n)-1))) >> (n))
  55. #define DESCALE_ZEROSHIFT(x,n) (((x) + (128 << (n)) + (SCALEDONE << ((n)-1))) >> (n))
  56. #define MULTIPLY(var, cnst) ((var) * (cnst))
  57. #define CLAMP(i) ((static_cast<uint>(i) > 255) ? (((~i) >> 31) & 0xFF) : (i))
  58. // Compiler creates a fast path 1D IDCT for X non-zero columns
  59. template <int NONZERO_COLS>
  60. struct Row
  61. {
  62. static void idct(int* pTemp, const jpgd_block_t* pSrc)
  63. {
  64. // ACCESS_COL() will be optimized at compile time to either an array access, or 0.
  65. #define ACCESS_COL(x) (((x) < NONZERO_COLS) ? (int)pSrc[x] : 0)
  66. const int z2 = ACCESS_COL(2), z3 = ACCESS_COL(6);
  67. const int z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  68. const int tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  69. const int tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  70. const int tmp0 = (ACCESS_COL(0) + ACCESS_COL(4)) << CONST_BITS;
  71. const int tmp1 = (ACCESS_COL(0) - ACCESS_COL(4)) << CONST_BITS;
  72. const int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2;
  73. const int atmp0 = ACCESS_COL(7), atmp1 = ACCESS_COL(5), atmp2 = ACCESS_COL(3), atmp3 = ACCESS_COL(1);
  74. const int bz1 = atmp0 + atmp3, bz2 = atmp1 + atmp2, bz3 = atmp0 + atmp2, bz4 = atmp1 + atmp3;
  75. const int bz5 = MULTIPLY(bz3 + bz4, FIX_1_175875602);
  76. const int az1 = MULTIPLY(bz1, - FIX_0_899976223);
  77. const int az2 = MULTIPLY(bz2, - FIX_2_562915447);
  78. const int az3 = MULTIPLY(bz3, - FIX_1_961570560) + bz5;
  79. const int az4 = MULTIPLY(bz4, - FIX_0_390180644) + bz5;
  80. const int btmp0 = MULTIPLY(atmp0, FIX_0_298631336) + az1 + az3;
  81. const int btmp1 = MULTIPLY(atmp1, FIX_2_053119869) + az2 + az4;
  82. const int btmp2 = MULTIPLY(atmp2, FIX_3_072711026) + az2 + az3;
  83. const int btmp3 = MULTIPLY(atmp3, FIX_1_501321110) + az1 + az4;
  84. pTemp[0] = DESCALE(tmp10 + btmp3, CONST_BITS-PASS1_BITS);
  85. pTemp[7] = DESCALE(tmp10 - btmp3, CONST_BITS-PASS1_BITS);
  86. pTemp[1] = DESCALE(tmp11 + btmp2, CONST_BITS-PASS1_BITS);
  87. pTemp[6] = DESCALE(tmp11 - btmp2, CONST_BITS-PASS1_BITS);
  88. pTemp[2] = DESCALE(tmp12 + btmp1, CONST_BITS-PASS1_BITS);
  89. pTemp[5] = DESCALE(tmp12 - btmp1, CONST_BITS-PASS1_BITS);
  90. pTemp[3] = DESCALE(tmp13 + btmp0, CONST_BITS-PASS1_BITS);
  91. pTemp[4] = DESCALE(tmp13 - btmp0, CONST_BITS-PASS1_BITS);
  92. }
  93. };
  94. template <>
  95. struct Row<0>
  96. {
  97. static void idct(int* pTemp, const jpgd_block_t* pSrc)
  98. {
  99. #ifdef _MSC_VER
  100. pTemp; pSrc;
  101. #endif
  102. }
  103. };
  104. template <>
  105. struct Row<1>
  106. {
  107. static void idct(int* pTemp, const jpgd_block_t* pSrc)
  108. {
  109. const int dcval = (pSrc[0] << PASS1_BITS);
  110. pTemp[0] = dcval;
  111. pTemp[1] = dcval;
  112. pTemp[2] = dcval;
  113. pTemp[3] = dcval;
  114. pTemp[4] = dcval;
  115. pTemp[5] = dcval;
  116. pTemp[6] = dcval;
  117. pTemp[7] = dcval;
  118. }
  119. };
  120. // Compiler creates a fast path 1D IDCT for X non-zero rows
  121. template <int NONZERO_ROWS>
  122. struct Col
  123. {
  124. static void idct(uint8* pDst_ptr, const int* pTemp)
  125. {
  126. // ACCESS_ROW() will be optimized at compile time to either an array access, or 0.
  127. #define ACCESS_ROW(x) (((x) < NONZERO_ROWS) ? pTemp[x * 8] : 0)
  128. const int z2 = ACCESS_ROW(2);
  129. const int z3 = ACCESS_ROW(6);
  130. const int z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  131. const int tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  132. const int tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  133. const int tmp0 = (ACCESS_ROW(0) + ACCESS_ROW(4)) << CONST_BITS;
  134. const int tmp1 = (ACCESS_ROW(0) - ACCESS_ROW(4)) << CONST_BITS;
  135. const int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2;
  136. const int atmp0 = ACCESS_ROW(7), atmp1 = ACCESS_ROW(5), atmp2 = ACCESS_ROW(3), atmp3 = ACCESS_ROW(1);
  137. const int bz1 = atmp0 + atmp3, bz2 = atmp1 + atmp2, bz3 = atmp0 + atmp2, bz4 = atmp1 + atmp3;
  138. const int bz5 = MULTIPLY(bz3 + bz4, FIX_1_175875602);
  139. const int az1 = MULTIPLY(bz1, - FIX_0_899976223);
  140. const int az2 = MULTIPLY(bz2, - FIX_2_562915447);
  141. const int az3 = MULTIPLY(bz3, - FIX_1_961570560) + bz5;
  142. const int az4 = MULTIPLY(bz4, - FIX_0_390180644) + bz5;
  143. const int btmp0 = MULTIPLY(atmp0, FIX_0_298631336) + az1 + az3;
  144. const int btmp1 = MULTIPLY(atmp1, FIX_2_053119869) + az2 + az4;
  145. const int btmp2 = MULTIPLY(atmp2, FIX_3_072711026) + az2 + az3;
  146. const int btmp3 = MULTIPLY(atmp3, FIX_1_501321110) + az1 + az4;
  147. int i = DESCALE_ZEROSHIFT(tmp10 + btmp3, CONST_BITS+PASS1_BITS+3);
  148. pDst_ptr[8*0] = (uint8)CLAMP(i);
  149. i = DESCALE_ZEROSHIFT(tmp10 - btmp3, CONST_BITS+PASS1_BITS+3);
  150. pDst_ptr[8*7] = (uint8)CLAMP(i);
  151. i = DESCALE_ZEROSHIFT(tmp11 + btmp2, CONST_BITS+PASS1_BITS+3);
  152. pDst_ptr[8*1] = (uint8)CLAMP(i);
  153. i = DESCALE_ZEROSHIFT(tmp11 - btmp2, CONST_BITS+PASS1_BITS+3);
  154. pDst_ptr[8*6] = (uint8)CLAMP(i);
  155. i = DESCALE_ZEROSHIFT(tmp12 + btmp1, CONST_BITS+PASS1_BITS+3);
  156. pDst_ptr[8*2] = (uint8)CLAMP(i);
  157. i = DESCALE_ZEROSHIFT(tmp12 - btmp1, CONST_BITS+PASS1_BITS+3);
  158. pDst_ptr[8*5] = (uint8)CLAMP(i);
  159. i = DESCALE_ZEROSHIFT(tmp13 + btmp0, CONST_BITS+PASS1_BITS+3);
  160. pDst_ptr[8*3] = (uint8)CLAMP(i);
  161. i = DESCALE_ZEROSHIFT(tmp13 - btmp0, CONST_BITS+PASS1_BITS+3);
  162. pDst_ptr[8*4] = (uint8)CLAMP(i);
  163. }
  164. };
  165. template <>
  166. struct Col<1>
  167. {
  168. static void idct(uint8* pDst_ptr, const int* pTemp)
  169. {
  170. int dcval = DESCALE_ZEROSHIFT(pTemp[0], PASS1_BITS+3);
  171. const uint8 dcval_clamped = (uint8)CLAMP(dcval);
  172. pDst_ptr[0*8] = dcval_clamped;
  173. pDst_ptr[1*8] = dcval_clamped;
  174. pDst_ptr[2*8] = dcval_clamped;
  175. pDst_ptr[3*8] = dcval_clamped;
  176. pDst_ptr[4*8] = dcval_clamped;
  177. pDst_ptr[5*8] = dcval_clamped;
  178. pDst_ptr[6*8] = dcval_clamped;
  179. pDst_ptr[7*8] = dcval_clamped;
  180. }
  181. };
  182. static const uint8 s_idct_row_table[] =
  183. {
  184. 1,0,0,0,0,0,0,0, 2,0,0,0,0,0,0,0, 2,1,0,0,0,0,0,0, 2,1,1,0,0,0,0,0, 2,2,1,0,0,0,0,0, 3,2,1,0,0,0,0,0, 4,2,1,0,0,0,0,0, 4,3,1,0,0,0,0,0,
  185. 4,3,2,0,0,0,0,0, 4,3,2,1,0,0,0,0, 4,3,2,1,1,0,0,0, 4,3,2,2,1,0,0,0, 4,3,3,2,1,0,0,0, 4,4,3,2,1,0,0,0, 5,4,3,2,1,0,0,0, 6,4,3,2,1,0,0,0,
  186. 6,5,3,2,1,0,0,0, 6,5,4,2,1,0,0,0, 6,5,4,3,1,0,0,0, 6,5,4,3,2,0,0,0, 6,5,4,3,2,1,0,0, 6,5,4,3,2,1,1,0, 6,5,4,3,2,2,1,0, 6,5,4,3,3,2,1,0,
  187. 6,5,4,4,3,2,1,0, 6,5,5,4,3,2,1,0, 6,6,5,4,3,2,1,0, 7,6,5,4,3,2,1,0, 8,6,5,4,3,2,1,0, 8,7,5,4,3,2,1,0, 8,7,6,4,3,2,1,0, 8,7,6,5,3,2,1,0,
  188. 8,7,6,5,4,2,1,0, 8,7,6,5,4,3,1,0, 8,7,6,5,4,3,2,0, 8,7,6,5,4,3,2,1, 8,7,6,5,4,3,2,2, 8,7,6,5,4,3,3,2, 8,7,6,5,4,4,3,2, 8,7,6,5,5,4,3,2,
  189. 8,7,6,6,5,4,3,2, 8,7,7,6,5,4,3,2, 8,8,7,6,5,4,3,2, 8,8,8,6,5,4,3,2, 8,8,8,7,5,4,3,2, 8,8,8,7,6,4,3,2, 8,8,8,7,6,5,3,2, 8,8,8,7,6,5,4,2,
  190. 8,8,8,7,6,5,4,3, 8,8,8,7,6,5,4,4, 8,8,8,7,6,5,5,4, 8,8,8,7,6,6,5,4, 8,8,8,7,7,6,5,4, 8,8,8,8,7,6,5,4, 8,8,8,8,8,6,5,4, 8,8,8,8,8,7,5,4,
  191. 8,8,8,8,8,7,6,4, 8,8,8,8,8,7,6,5, 8,8,8,8,8,7,6,6, 8,8,8,8,8,7,7,6, 8,8,8,8,8,8,7,6, 8,8,8,8,8,8,8,6, 8,8,8,8,8,8,8,7, 8,8,8,8,8,8,8,8,
  192. };
  193. static const uint8 s_idct_col_table[] = { 1, 1, 2, 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 };
  194. void idct(const jpgd_block_t* pSrc_ptr, uint8* pDst_ptr, int block_max_zag)
  195. {
  196. JPGD_ASSERT(block_max_zag >= 1);
  197. JPGD_ASSERT(block_max_zag <= 64);
  198. if (block_max_zag <= 1)
  199. {
  200. int k = ((pSrc_ptr[0] + 4) >> 3) + 128;
  201. k = CLAMP(k);
  202. k = k | (k<<8);
  203. k = k | (k<<16);
  204. for (int i = 8; i > 0; i--)
  205. {
  206. *(int*)&pDst_ptr[0] = k;
  207. *(int*)&pDst_ptr[4] = k;
  208. pDst_ptr += 8;
  209. }
  210. return;
  211. }
  212. int temp[64];
  213. const jpgd_block_t* pSrc = pSrc_ptr;
  214. int* pTemp = temp;
  215. const uint8* pRow_tab = &s_idct_row_table[(block_max_zag - 1) * 8];
  216. int i;
  217. for (i = 8; i > 0; i--, pRow_tab++)
  218. {
  219. switch (*pRow_tab)
  220. {
  221. case 0: Row<0>::idct(pTemp, pSrc); break;
  222. case 1: Row<1>::idct(pTemp, pSrc); break;
  223. case 2: Row<2>::idct(pTemp, pSrc); break;
  224. case 3: Row<3>::idct(pTemp, pSrc); break;
  225. case 4: Row<4>::idct(pTemp, pSrc); break;
  226. case 5: Row<5>::idct(pTemp, pSrc); break;
  227. case 6: Row<6>::idct(pTemp, pSrc); break;
  228. case 7: Row<7>::idct(pTemp, pSrc); break;
  229. case 8: Row<8>::idct(pTemp, pSrc); break;
  230. }
  231. pSrc += 8;
  232. pTemp += 8;
  233. }
  234. pTemp = temp;
  235. const int nonzero_rows = s_idct_col_table[block_max_zag - 1];
  236. for (i = 8; i > 0; i--)
  237. {
  238. switch (nonzero_rows)
  239. {
  240. case 1: Col<1>::idct(pDst_ptr, pTemp); break;
  241. case 2: Col<2>::idct(pDst_ptr, pTemp); break;
  242. case 3: Col<3>::idct(pDst_ptr, pTemp); break;
  243. case 4: Col<4>::idct(pDst_ptr, pTemp); break;
  244. case 5: Col<5>::idct(pDst_ptr, pTemp); break;
  245. case 6: Col<6>::idct(pDst_ptr, pTemp); break;
  246. case 7: Col<7>::idct(pDst_ptr, pTemp); break;
  247. case 8: Col<8>::idct(pDst_ptr, pTemp); break;
  248. }
  249. pTemp++;
  250. pDst_ptr++;
  251. }
  252. }
  253. void idct_4x4(const jpgd_block_t* pSrc_ptr, uint8* pDst_ptr)
  254. {
  255. int temp[64];
  256. int* pTemp = temp;
  257. const jpgd_block_t* pSrc = pSrc_ptr;
  258. for (int i = 4; i > 0; i--)
  259. {
  260. Row<4>::idct(pTemp, pSrc);
  261. pSrc += 8;
  262. pTemp += 8;
  263. }
  264. pTemp = temp;
  265. for (int i = 8; i > 0; i--)
  266. {
  267. Col<4>::idct(pDst_ptr, pTemp);
  268. pTemp++;
  269. pDst_ptr++;
  270. }
  271. }
  272. // Retrieve one character from the input stream.
  273. inline uint jpeg_decoder::get_char()
  274. {
  275. // Any bytes remaining in buffer?
  276. if (!m_in_buf_left)
  277. {
  278. // Try to get more bytes.
  279. prep_in_buffer();
  280. // Still nothing to get?
  281. if (!m_in_buf_left)
  282. {
  283. // Pad the end of the stream with 0xFF 0xD9 (EOI marker)
  284. int t = m_tem_flag;
  285. m_tem_flag ^= 1;
  286. if (t)
  287. return 0xD9;
  288. else
  289. return 0xFF;
  290. }
  291. }
  292. uint c = *m_pIn_buf_ofs++;
  293. m_in_buf_left--;
  294. return c;
  295. }
  296. // Same as previous method, except can indicate if the character is a pad character or not.
  297. inline uint jpeg_decoder::get_char(bool *pPadding_flag)
  298. {
  299. if (!m_in_buf_left)
  300. {
  301. prep_in_buffer();
  302. if (!m_in_buf_left)
  303. {
  304. *pPadding_flag = true;
  305. int t = m_tem_flag;
  306. m_tem_flag ^= 1;
  307. if (t)
  308. return 0xD9;
  309. else
  310. return 0xFF;
  311. }
  312. }
  313. *pPadding_flag = false;
  314. uint c = *m_pIn_buf_ofs++;
  315. m_in_buf_left--;
  316. return c;
  317. }
  318. // Inserts a previously retrieved character back into the input buffer.
  319. inline void jpeg_decoder::stuff_char(uint8 q)
  320. {
  321. *(--m_pIn_buf_ofs) = q;
  322. m_in_buf_left++;
  323. }
  324. // Retrieves one character from the input stream, but does not read past markers. Will continue to return 0xFF when a marker is encountered.
  325. inline uint8 jpeg_decoder::get_octet()
  326. {
  327. bool padding_flag;
  328. int c = get_char(&padding_flag);
  329. if (c == 0xFF)
  330. {
  331. if (padding_flag)
  332. return 0xFF;
  333. c = get_char(&padding_flag);
  334. if (padding_flag)
  335. {
  336. stuff_char(0xFF);
  337. return 0xFF;
  338. }
  339. if (c == 0x00)
  340. return 0xFF;
  341. else
  342. {
  343. stuff_char(static_cast<uint8>(c));
  344. stuff_char(0xFF);
  345. return 0xFF;
  346. }
  347. }
  348. return static_cast<uint8>(c);
  349. }
  350. // Retrieves a variable number of bits from the input stream. Does not recognize markers.
  351. inline uint jpeg_decoder::get_bits(int num_bits)
  352. {
  353. if (!num_bits)
  354. return 0;
  355. uint i = m_bit_buf >> (32 - num_bits);
  356. if ((m_bits_left -= num_bits) <= 0)
  357. {
  358. m_bit_buf <<= (num_bits += m_bits_left);
  359. uint c1 = get_char();
  360. uint c2 = get_char();
  361. m_bit_buf = (m_bit_buf & 0xFFFF0000) | (c1 << 8) | c2;
  362. m_bit_buf <<= -m_bits_left;
  363. m_bits_left += 16;
  364. JPGD_ASSERT(m_bits_left >= 0);
  365. }
  366. else
  367. m_bit_buf <<= num_bits;
  368. return i;
  369. }
  370. // Retrieves a variable number of bits from the input stream. Markers will not be read into the input bit buffer. Instead, an infinite number of all 1's will be returned when a marker is encountered.
  371. inline uint jpeg_decoder::get_bits_no_markers(int num_bits)
  372. {
  373. if (!num_bits)
  374. return 0;
  375. uint i = m_bit_buf >> (32 - num_bits);
  376. if ((m_bits_left -= num_bits) <= 0)
  377. {
  378. m_bit_buf <<= (num_bits += m_bits_left);
  379. if ((m_in_buf_left < 2) || (m_pIn_buf_ofs[0] == 0xFF) || (m_pIn_buf_ofs[1] == 0xFF))
  380. {
  381. uint c1 = get_octet();
  382. uint c2 = get_octet();
  383. m_bit_buf |= (c1 << 8) | c2;
  384. }
  385. else
  386. {
  387. m_bit_buf |= ((uint)m_pIn_buf_ofs[0] << 8) | m_pIn_buf_ofs[1];
  388. m_in_buf_left -= 2;
  389. m_pIn_buf_ofs += 2;
  390. }
  391. m_bit_buf <<= -m_bits_left;
  392. m_bits_left += 16;
  393. JPGD_ASSERT(m_bits_left >= 0);
  394. }
  395. else
  396. m_bit_buf <<= num_bits;
  397. return i;
  398. }
  399. // Decodes a Huffman encoded symbol.
  400. inline int jpeg_decoder::huff_decode(huff_tables *pH)
  401. {
  402. int symbol;
  403. // Check first 8-bits: do we have a complete symbol?
  404. if ((symbol = pH->look_up[m_bit_buf >> 24]) < 0)
  405. {
  406. // Decode more bits, use a tree traversal to find symbol.
  407. int ofs = 23;
  408. do
  409. {
  410. symbol = pH->tree[-(int)(symbol + ((m_bit_buf >> ofs) & 1))];
  411. ofs--;
  412. } while (symbol < 0);
  413. get_bits_no_markers(8 + (23 - ofs));
  414. }
  415. else
  416. get_bits_no_markers(pH->code_size[symbol]);
  417. return symbol;
  418. }
  419. // Decodes a Huffman encoded symbol.
  420. inline int jpeg_decoder::huff_decode(huff_tables *pH, int& extra_bits)
  421. {
  422. int symbol;
  423. // Check first 8-bits: do we have a complete symbol?
  424. if ((symbol = pH->look_up2[m_bit_buf >> 24]) < 0)
  425. {
  426. // Use a tree traversal to find symbol.
  427. int ofs = 23;
  428. do
  429. {
  430. symbol = pH->tree[-(int)(symbol + ((m_bit_buf >> ofs) & 1))];
  431. ofs--;
  432. } while (symbol < 0);
  433. get_bits_no_markers(8 + (23 - ofs));
  434. extra_bits = get_bits_no_markers(symbol & 0xF);
  435. }
  436. else
  437. {
  438. JPGD_ASSERT(((symbol >> 8) & 31) == pH->code_size[symbol & 255] + ((symbol & 0x8000) ? (symbol & 15) : 0));
  439. if (symbol & 0x8000)
  440. {
  441. get_bits_no_markers((symbol >> 8) & 31);
  442. extra_bits = symbol >> 16;
  443. }
  444. else
  445. {
  446. int code_size = (symbol >> 8) & 31;
  447. int num_extra_bits = symbol & 0xF;
  448. int bits = code_size + num_extra_bits;
  449. if (bits <= (m_bits_left + 16))
  450. extra_bits = get_bits_no_markers(bits) & ((1 << num_extra_bits) - 1);
  451. else
  452. {
  453. get_bits_no_markers(code_size);
  454. extra_bits = get_bits_no_markers(num_extra_bits);
  455. }
  456. }
  457. symbol &= 0xFF;
  458. }
  459. return symbol;
  460. }
  461. // Tables and macro used to fully decode the DPCM differences.
  462. static const int s_extend_test[16] = { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
  463. static const int s_extend_offset[16] = { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
  464. static const int s_extend_mask[] = { 0, (1<<0), (1<<1), (1<<2), (1<<3), (1<<4), (1<<5), (1<<6), (1<<7), (1<<8), (1<<9), (1<<10), (1<<11), (1<<12), (1<<13), (1<<14), (1<<15), (1<<16) };
  465. // The logical AND's in this macro are to shut up static code analysis (aren't really necessary - couldn't find another way to do this)
  466. #define JPGD_HUFF_EXTEND(x, s) (((x) < s_extend_test[s & 15]) ? ((x) + s_extend_offset[s & 15]) : (x))
  467. // Clamps a value between 0-255.
  468. inline uint8 jpeg_decoder::clamp(int i)
  469. {
  470. if (static_cast<uint>(i) > 255)
  471. i = (((~i) >> 31) & 0xFF);
  472. return static_cast<uint8>(i);
  473. }
  474. namespace DCT_Upsample
  475. {
  476. struct Matrix44
  477. {
  478. typedef int Element_Type;
  479. enum { NUM_ROWS = 4, NUM_COLS = 4 };
  480. Element_Type v[NUM_ROWS][NUM_COLS];
  481. inline int rows() const { return NUM_ROWS; }
  482. inline int cols() const { return NUM_COLS; }
  483. inline const Element_Type & at(int r, int c) const { return v[r][c]; }
  484. inline Element_Type & at(int r, int c) { return v[r][c]; }
  485. inline Matrix44() { }
  486. inline Matrix44& operator += (const Matrix44& a)
  487. {
  488. for (int r = 0; r < NUM_ROWS; r++)
  489. {
  490. at(r, 0) += a.at(r, 0);
  491. at(r, 1) += a.at(r, 1);
  492. at(r, 2) += a.at(r, 2);
  493. at(r, 3) += a.at(r, 3);
  494. }
  495. return *this;
  496. }
  497. inline Matrix44& operator -= (const Matrix44& a)
  498. {
  499. for (int r = 0; r < NUM_ROWS; r++)
  500. {
  501. at(r, 0) -= a.at(r, 0);
  502. at(r, 1) -= a.at(r, 1);
  503. at(r, 2) -= a.at(r, 2);
  504. at(r, 3) -= a.at(r, 3);
  505. }
  506. return *this;
  507. }
  508. friend inline Matrix44 operator + (const Matrix44& a, const Matrix44& b)
  509. {
  510. Matrix44 ret;
  511. for (int r = 0; r < NUM_ROWS; r++)
  512. {
  513. ret.at(r, 0) = a.at(r, 0) + b.at(r, 0);
  514. ret.at(r, 1) = a.at(r, 1) + b.at(r, 1);
  515. ret.at(r, 2) = a.at(r, 2) + b.at(r, 2);
  516. ret.at(r, 3) = a.at(r, 3) + b.at(r, 3);
  517. }
  518. return ret;
  519. }
  520. friend inline Matrix44 operator - (const Matrix44& a, const Matrix44& b)
  521. {
  522. Matrix44 ret;
  523. for (int r = 0; r < NUM_ROWS; r++)
  524. {
  525. ret.at(r, 0) = a.at(r, 0) - b.at(r, 0);
  526. ret.at(r, 1) = a.at(r, 1) - b.at(r, 1);
  527. ret.at(r, 2) = a.at(r, 2) - b.at(r, 2);
  528. ret.at(r, 3) = a.at(r, 3) - b.at(r, 3);
  529. }
  530. return ret;
  531. }
  532. static inline void add_and_store(jpgd_block_t* pDst, const Matrix44& a, const Matrix44& b)
  533. {
  534. for (int r = 0; r < 4; r++)
  535. {
  536. pDst[0*8 + r] = static_cast<jpgd_block_t>(a.at(r, 0) + b.at(r, 0));
  537. pDst[1*8 + r] = static_cast<jpgd_block_t>(a.at(r, 1) + b.at(r, 1));
  538. pDst[2*8 + r] = static_cast<jpgd_block_t>(a.at(r, 2) + b.at(r, 2));
  539. pDst[3*8 + r] = static_cast<jpgd_block_t>(a.at(r, 3) + b.at(r, 3));
  540. }
  541. }
  542. static inline void sub_and_store(jpgd_block_t* pDst, const Matrix44& a, const Matrix44& b)
  543. {
  544. for (int r = 0; r < 4; r++)
  545. {
  546. pDst[0*8 + r] = static_cast<jpgd_block_t>(a.at(r, 0) - b.at(r, 0));
  547. pDst[1*8 + r] = static_cast<jpgd_block_t>(a.at(r, 1) - b.at(r, 1));
  548. pDst[2*8 + r] = static_cast<jpgd_block_t>(a.at(r, 2) - b.at(r, 2));
  549. pDst[3*8 + r] = static_cast<jpgd_block_t>(a.at(r, 3) - b.at(r, 3));
  550. }
  551. }
  552. };
  553. const int FRACT_BITS = 10;
  554. const int SCALE = 1 << FRACT_BITS;
  555. typedef int Temp_Type;
  556. #define D(i) (((i) + (SCALE >> 1)) >> FRACT_BITS)
  557. #define F(i) ((int)((i) * SCALE + .5f))
  558. // Any decent C++ compiler will optimize this at compile time to a 0, or an array access.
  559. #define AT(c, r) ((((c)>=NUM_COLS)||((r)>=NUM_ROWS)) ? 0 : pSrc[(c)+(r)*8])
  560. // NUM_ROWS/NUM_COLS = # of non-zero rows/cols in input matrix
  561. template<int NUM_ROWS, int NUM_COLS>
  562. struct P_Q
  563. {
  564. static void calc(Matrix44& P, Matrix44& Q, const jpgd_block_t* pSrc)
  565. {
  566. // 4x8 = 4x8 times 8x8, matrix 0 is constant
  567. const Temp_Type X000 = AT(0, 0);
  568. const Temp_Type X001 = AT(0, 1);
  569. const Temp_Type X002 = AT(0, 2);
  570. const Temp_Type X003 = AT(0, 3);
  571. const Temp_Type X004 = AT(0, 4);
  572. const Temp_Type X005 = AT(0, 5);
  573. const Temp_Type X006 = AT(0, 6);
  574. const Temp_Type X007 = AT(0, 7);
  575. const Temp_Type X010 = D(F(0.415735f) * AT(1, 0) + F(0.791065f) * AT(3, 0) + F(-0.352443f) * AT(5, 0) + F(0.277785f) * AT(7, 0));
  576. const Temp_Type X011 = D(F(0.415735f) * AT(1, 1) + F(0.791065f) * AT(3, 1) + F(-0.352443f) * AT(5, 1) + F(0.277785f) * AT(7, 1));
  577. const Temp_Type X012 = D(F(0.415735f) * AT(1, 2) + F(0.791065f) * AT(3, 2) + F(-0.352443f) * AT(5, 2) + F(0.277785f) * AT(7, 2));
  578. const Temp_Type X013 = D(F(0.415735f) * AT(1, 3) + F(0.791065f) * AT(3, 3) + F(-0.352443f) * AT(5, 3) + F(0.277785f) * AT(7, 3));
  579. const Temp_Type X014 = D(F(0.415735f) * AT(1, 4) + F(0.791065f) * AT(3, 4) + F(-0.352443f) * AT(5, 4) + F(0.277785f) * AT(7, 4));
  580. const Temp_Type X015 = D(F(0.415735f) * AT(1, 5) + F(0.791065f) * AT(3, 5) + F(-0.352443f) * AT(5, 5) + F(0.277785f) * AT(7, 5));
  581. const Temp_Type X016 = D(F(0.415735f) * AT(1, 6) + F(0.791065f) * AT(3, 6) + F(-0.352443f) * AT(5, 6) + F(0.277785f) * AT(7, 6));
  582. const Temp_Type X017 = D(F(0.415735f) * AT(1, 7) + F(0.791065f) * AT(3, 7) + F(-0.352443f) * AT(5, 7) + F(0.277785f) * AT(7, 7));
  583. const Temp_Type X020 = AT(4, 0);
  584. const Temp_Type X021 = AT(4, 1);
  585. const Temp_Type X022 = AT(4, 2);
  586. const Temp_Type X023 = AT(4, 3);
  587. const Temp_Type X024 = AT(4, 4);
  588. const Temp_Type X025 = AT(4, 5);
  589. const Temp_Type X026 = AT(4, 6);
  590. const Temp_Type X027 = AT(4, 7);
  591. const Temp_Type X030 = D(F(0.022887f) * AT(1, 0) + F(-0.097545f) * AT(3, 0) + F(0.490393f) * AT(5, 0) + F(0.865723f) * AT(7, 0));
  592. const Temp_Type X031 = D(F(0.022887f) * AT(1, 1) + F(-0.097545f) * AT(3, 1) + F(0.490393f) * AT(5, 1) + F(0.865723f) * AT(7, 1));
  593. const Temp_Type X032 = D(F(0.022887f) * AT(1, 2) + F(-0.097545f) * AT(3, 2) + F(0.490393f) * AT(5, 2) + F(0.865723f) * AT(7, 2));
  594. const Temp_Type X033 = D(F(0.022887f) * AT(1, 3) + F(-0.097545f) * AT(3, 3) + F(0.490393f) * AT(5, 3) + F(0.865723f) * AT(7, 3));
  595. const Temp_Type X034 = D(F(0.022887f) * AT(1, 4) + F(-0.097545f) * AT(3, 4) + F(0.490393f) * AT(5, 4) + F(0.865723f) * AT(7, 4));
  596. const Temp_Type X035 = D(F(0.022887f) * AT(1, 5) + F(-0.097545f) * AT(3, 5) + F(0.490393f) * AT(5, 5) + F(0.865723f) * AT(7, 5));
  597. const Temp_Type X036 = D(F(0.022887f) * AT(1, 6) + F(-0.097545f) * AT(3, 6) + F(0.490393f) * AT(5, 6) + F(0.865723f) * AT(7, 6));
  598. const Temp_Type X037 = D(F(0.022887f) * AT(1, 7) + F(-0.097545f) * AT(3, 7) + F(0.490393f) * AT(5, 7) + F(0.865723f) * AT(7, 7));
  599. // 4x4 = 4x8 times 8x4, matrix 1 is constant
  600. P.at(0, 0) = X000;
  601. P.at(0, 1) = D(X001 * F(0.415735f) + X003 * F(0.791065f) + X005 * F(-0.352443f) + X007 * F(0.277785f));
  602. P.at(0, 2) = X004;
  603. P.at(0, 3) = D(X001 * F(0.022887f) + X003 * F(-0.097545f) + X005 * F(0.490393f) + X007 * F(0.865723f));
  604. P.at(1, 0) = X010;
  605. P.at(1, 1) = D(X011 * F(0.415735f) + X013 * F(0.791065f) + X015 * F(-0.352443f) + X017 * F(0.277785f));
  606. P.at(1, 2) = X014;
  607. P.at(1, 3) = D(X011 * F(0.022887f) + X013 * F(-0.097545f) + X015 * F(0.490393f) + X017 * F(0.865723f));
  608. P.at(2, 0) = X020;
  609. P.at(2, 1) = D(X021 * F(0.415735f) + X023 * F(0.791065f) + X025 * F(-0.352443f) + X027 * F(0.277785f));
  610. P.at(2, 2) = X024;
  611. P.at(2, 3) = D(X021 * F(0.022887f) + X023 * F(-0.097545f) + X025 * F(0.490393f) + X027 * F(0.865723f));
  612. P.at(3, 0) = X030;
  613. P.at(3, 1) = D(X031 * F(0.415735f) + X033 * F(0.791065f) + X035 * F(-0.352443f) + X037 * F(0.277785f));
  614. P.at(3, 2) = X034;
  615. P.at(3, 3) = D(X031 * F(0.022887f) + X033 * F(-0.097545f) + X035 * F(0.490393f) + X037 * F(0.865723f));
  616. // 40 muls 24 adds
  617. // 4x4 = 4x8 times 8x4, matrix 1 is constant
  618. Q.at(0, 0) = D(X001 * F(0.906127f) + X003 * F(-0.318190f) + X005 * F(0.212608f) + X007 * F(-0.180240f));
  619. Q.at(0, 1) = X002;
  620. Q.at(0, 2) = D(X001 * F(-0.074658f) + X003 * F(0.513280f) + X005 * F(0.768178f) + X007 * F(-0.375330f));
  621. Q.at(0, 3) = X006;
  622. Q.at(1, 0) = D(X011 * F(0.906127f) + X013 * F(-0.318190f) + X015 * F(0.212608f) + X017 * F(-0.180240f));
  623. Q.at(1, 1) = X012;
  624. Q.at(1, 2) = D(X011 * F(-0.074658f) + X013 * F(0.513280f) + X015 * F(0.768178f) + X017 * F(-0.375330f));
  625. Q.at(1, 3) = X016;
  626. Q.at(2, 0) = D(X021 * F(0.906127f) + X023 * F(-0.318190f) + X025 * F(0.212608f) + X027 * F(-0.180240f));
  627. Q.at(2, 1) = X022;
  628. Q.at(2, 2) = D(X021 * F(-0.074658f) + X023 * F(0.513280f) + X025 * F(0.768178f) + X027 * F(-0.375330f));
  629. Q.at(2, 3) = X026;
  630. Q.at(3, 0) = D(X031 * F(0.906127f) + X033 * F(-0.318190f) + X035 * F(0.212608f) + X037 * F(-0.180240f));
  631. Q.at(3, 1) = X032;
  632. Q.at(3, 2) = D(X031 * F(-0.074658f) + X033 * F(0.513280f) + X035 * F(0.768178f) + X037 * F(-0.375330f));
  633. Q.at(3, 3) = X036;
  634. // 40 muls 24 adds
  635. }
  636. };
  637. template<int NUM_ROWS, int NUM_COLS>
  638. struct R_S
  639. {
  640. static void calc(Matrix44& R, Matrix44& S, const jpgd_block_t* pSrc)
  641. {
  642. // 4x8 = 4x8 times 8x8, matrix 0 is constant
  643. const Temp_Type X100 = D(F(0.906127f) * AT(1, 0) + F(-0.318190f) * AT(3, 0) + F(0.212608f) * AT(5, 0) + F(-0.180240f) * AT(7, 0));
  644. const Temp_Type X101 = D(F(0.906127f) * AT(1, 1) + F(-0.318190f) * AT(3, 1) + F(0.212608f) * AT(5, 1) + F(-0.180240f) * AT(7, 1));
  645. const Temp_Type X102 = D(F(0.906127f) * AT(1, 2) + F(-0.318190f) * AT(3, 2) + F(0.212608f) * AT(5, 2) + F(-0.180240f) * AT(7, 2));
  646. const Temp_Type X103 = D(F(0.906127f) * AT(1, 3) + F(-0.318190f) * AT(3, 3) + F(0.212608f) * AT(5, 3) + F(-0.180240f) * AT(7, 3));
  647. const Temp_Type X104 = D(F(0.906127f) * AT(1, 4) + F(-0.318190f) * AT(3, 4) + F(0.212608f) * AT(5, 4) + F(-0.180240f) * AT(7, 4));
  648. const Temp_Type X105 = D(F(0.906127f) * AT(1, 5) + F(-0.318190f) * AT(3, 5) + F(0.212608f) * AT(5, 5) + F(-0.180240f) * AT(7, 5));
  649. const Temp_Type X106 = D(F(0.906127f) * AT(1, 6) + F(-0.318190f) * AT(3, 6) + F(0.212608f) * AT(5, 6) + F(-0.180240f) * AT(7, 6));
  650. const Temp_Type X107 = D(F(0.906127f) * AT(1, 7) + F(-0.318190f) * AT(3, 7) + F(0.212608f) * AT(5, 7) + F(-0.180240f) * AT(7, 7));
  651. const Temp_Type X110 = AT(2, 0);
  652. const Temp_Type X111 = AT(2, 1);
  653. const Temp_Type X112 = AT(2, 2);
  654. const Temp_Type X113 = AT(2, 3);
  655. const Temp_Type X114 = AT(2, 4);
  656. const Temp_Type X115 = AT(2, 5);
  657. const Temp_Type X116 = AT(2, 6);
  658. const Temp_Type X117 = AT(2, 7);
  659. const Temp_Type X120 = D(F(-0.074658f) * AT(1, 0) + F(0.513280f) * AT(3, 0) + F(0.768178f) * AT(5, 0) + F(-0.375330f) * AT(7, 0));
  660. const Temp_Type X121 = D(F(-0.074658f) * AT(1, 1) + F(0.513280f) * AT(3, 1) + F(0.768178f) * AT(5, 1) + F(-0.375330f) * AT(7, 1));
  661. const Temp_Type X122 = D(F(-0.074658f) * AT(1, 2) + F(0.513280f) * AT(3, 2) + F(0.768178f) * AT(5, 2) + F(-0.375330f) * AT(7, 2));
  662. const Temp_Type X123 = D(F(-0.074658f) * AT(1, 3) + F(0.513280f) * AT(3, 3) + F(0.768178f) * AT(5, 3) + F(-0.375330f) * AT(7, 3));
  663. const Temp_Type X124 = D(F(-0.074658f) * AT(1, 4) + F(0.513280f) * AT(3, 4) + F(0.768178f) * AT(5, 4) + F(-0.375330f) * AT(7, 4));
  664. const Temp_Type X125 = D(F(-0.074658f) * AT(1, 5) + F(0.513280f) * AT(3, 5) + F(0.768178f) * AT(5, 5) + F(-0.375330f) * AT(7, 5));
  665. const Temp_Type X126 = D(F(-0.074658f) * AT(1, 6) + F(0.513280f) * AT(3, 6) + F(0.768178f) * AT(5, 6) + F(-0.375330f) * AT(7, 6));
  666. const Temp_Type X127 = D(F(-0.074658f) * AT(1, 7) + F(0.513280f) * AT(3, 7) + F(0.768178f) * AT(5, 7) + F(-0.375330f) * AT(7, 7));
  667. const Temp_Type X130 = AT(6, 0);
  668. const Temp_Type X131 = AT(6, 1);
  669. const Temp_Type X132 = AT(6, 2);
  670. const Temp_Type X133 = AT(6, 3);
  671. const Temp_Type X134 = AT(6, 4);
  672. const Temp_Type X135 = AT(6, 5);
  673. const Temp_Type X136 = AT(6, 6);
  674. const Temp_Type X137 = AT(6, 7);
  675. // 80 muls 48 adds
  676. // 4x4 = 4x8 times 8x4, matrix 1 is constant
  677. R.at(0, 0) = X100;
  678. R.at(0, 1) = D(X101 * F(0.415735f) + X103 * F(0.791065f) + X105 * F(-0.352443f) + X107 * F(0.277785f));
  679. R.at(0, 2) = X104;
  680. R.at(0, 3) = D(X101 * F(0.022887f) + X103 * F(-0.097545f) + X105 * F(0.490393f) + X107 * F(0.865723f));
  681. R.at(1, 0) = X110;
  682. R.at(1, 1) = D(X111 * F(0.415735f) + X113 * F(0.791065f) + X115 * F(-0.352443f) + X117 * F(0.277785f));
  683. R.at(1, 2) = X114;
  684. R.at(1, 3) = D(X111 * F(0.022887f) + X113 * F(-0.097545f) + X115 * F(0.490393f) + X117 * F(0.865723f));
  685. R.at(2, 0) = X120;
  686. R.at(2, 1) = D(X121 * F(0.415735f) + X123 * F(0.791065f) + X125 * F(-0.352443f) + X127 * F(0.277785f));
  687. R.at(2, 2) = X124;
  688. R.at(2, 3) = D(X121 * F(0.022887f) + X123 * F(-0.097545f) + X125 * F(0.490393f) + X127 * F(0.865723f));
  689. R.at(3, 0) = X130;
  690. R.at(3, 1) = D(X131 * F(0.415735f) + X133 * F(0.791065f) + X135 * F(-0.352443f) + X137 * F(0.277785f));
  691. R.at(3, 2) = X134;
  692. R.at(3, 3) = D(X131 * F(0.022887f) + X133 * F(-0.097545f) + X135 * F(0.490393f) + X137 * F(0.865723f));
  693. // 40 muls 24 adds
  694. // 4x4 = 4x8 times 8x4, matrix 1 is constant
  695. S.at(0, 0) = D(X101 * F(0.906127f) + X103 * F(-0.318190f) + X105 * F(0.212608f) + X107 * F(-0.180240f));
  696. S.at(0, 1) = X102;
  697. S.at(0, 2) = D(X101 * F(-0.074658f) + X103 * F(0.513280f) + X105 * F(0.768178f) + X107 * F(-0.375330f));
  698. S.at(0, 3) = X106;
  699. S.at(1, 0) = D(X111 * F(0.906127f) + X113 * F(-0.318190f) + X115 * F(0.212608f) + X117 * F(-0.180240f));
  700. S.at(1, 1) = X112;
  701. S.at(1, 2) = D(X111 * F(-0.074658f) + X113 * F(0.513280f) + X115 * F(0.768178f) + X117 * F(-0.375330f));
  702. S.at(1, 3) = X116;
  703. S.at(2, 0) = D(X121 * F(0.906127f) + X123 * F(-0.318190f) + X125 * F(0.212608f) + X127 * F(-0.180240f));
  704. S.at(2, 1) = X122;
  705. S.at(2, 2) = D(X121 * F(-0.074658f) + X123 * F(0.513280f) + X125 * F(0.768178f) + X127 * F(-0.375330f));
  706. S.at(2, 3) = X126;
  707. S.at(3, 0) = D(X131 * F(0.906127f) + X133 * F(-0.318190f) + X135 * F(0.212608f) + X137 * F(-0.180240f));
  708. S.at(3, 1) = X132;
  709. S.at(3, 2) = D(X131 * F(-0.074658f) + X133 * F(0.513280f) + X135 * F(0.768178f) + X137 * F(-0.375330f));
  710. S.at(3, 3) = X136;
  711. // 40 muls 24 adds
  712. }
  713. };
  714. } // end namespace DCT_Upsample
  715. // Unconditionally frees all allocated m_blocks.
  716. void jpeg_decoder::free_all_blocks()
  717. {
  718. m_pStream = NULL;
  719. for (mem_block *b = m_pMem_blocks; b; )
  720. {
  721. mem_block *n = b->m_pNext;
  722. jpgd_free(b);
  723. b = n;
  724. }
  725. m_pMem_blocks = NULL;
  726. }
  727. // This method handles all errors. It will never return.
  728. // It could easily be changed to use C++ exceptions.
  729. JPGD_NORETURN void jpeg_decoder::stop_decoding(jpgd_status status)
  730. {
  731. m_error_code = status;
  732. free_all_blocks();
  733. longjmp(m_jmp_state, status);
  734. }
  735. void *jpeg_decoder::alloc(size_t nSize, bool zero)
  736. {
  737. nSize = (JPGD_MAX(nSize, 1) + 3) & ~3;
  738. char *rv = NULL;
  739. for (mem_block *b = m_pMem_blocks; b; b = b->m_pNext)
  740. {
  741. if ((b->m_used_count + nSize) <= b->m_size)
  742. {
  743. rv = b->m_data + b->m_used_count;
  744. b->m_used_count += nSize;
  745. break;
  746. }
  747. }
  748. if (!rv)
  749. {
  750. int capacity = JPGD_MAX(32768 - 256, (nSize + 2047) & ~2047);
  751. mem_block *b = (mem_block*)jpgd_malloc(sizeof(mem_block) + capacity);
  752. if (!b) { stop_decoding(JPGD_NOTENOUGHMEM); }
  753. b->m_pNext = m_pMem_blocks; m_pMem_blocks = b;
  754. b->m_used_count = nSize;
  755. b->m_size = capacity;
  756. rv = b->m_data;
  757. }
  758. if (zero) memset(rv, 0, nSize);
  759. return rv;
  760. }
  761. void jpeg_decoder::word_clear(void *p, uint16 c, uint n)
  762. {
  763. uint8 *pD = (uint8*)p;
  764. const uint8 l = c & 0xFF, h = (c >> 8) & 0xFF;
  765. while (n)
  766. {
  767. pD[0] = l; pD[1] = h; pD += 2;
  768. n--;
  769. }
  770. }
  771. // Refill the input buffer.
  772. // This method will sit in a loop until (A) the buffer is full or (B)
  773. // the stream's read() method reports and end of file condition.
  774. void jpeg_decoder::prep_in_buffer()
  775. {
  776. m_in_buf_left = 0;
  777. m_pIn_buf_ofs = m_in_buf;
  778. if (m_eof_flag)
  779. return;
  780. do
  781. {
  782. int bytes_read = m_pStream->read(m_in_buf + m_in_buf_left, JPGD_IN_BUF_SIZE - m_in_buf_left, &m_eof_flag);
  783. if (bytes_read == -1)
  784. stop_decoding(JPGD_STREAM_READ);
  785. m_in_buf_left += bytes_read;
  786. } while ((m_in_buf_left < JPGD_IN_BUF_SIZE) && (!m_eof_flag));
  787. m_total_bytes_read += m_in_buf_left;
  788. // Pad the end of the block with M_EOI (prevents the decompressor from going off the rails if the stream is invalid).
  789. // (This dates way back to when this decompressor was written in C/asm, and the all-asm Huffman decoder did some fancy things to increase perf.)
  790. word_clear(m_pIn_buf_ofs + m_in_buf_left, 0xD9FF, 64);
  791. }
  792. // Read a Huffman code table.
  793. void jpeg_decoder::read_dht_marker()
  794. {
  795. int i, index, count;
  796. uint8 huff_num[17];
  797. uint8 huff_val[256];
  798. uint num_left = get_bits(16);
  799. if (num_left < 2)
  800. stop_decoding(JPGD_BAD_DHT_MARKER);
  801. num_left -= 2;
  802. while (num_left)
  803. {
  804. index = get_bits(8);
  805. huff_num[0] = 0;
  806. count = 0;
  807. for (i = 1; i <= 16; i++)
  808. {
  809. huff_num[i] = static_cast<uint8>(get_bits(8));
  810. count += huff_num[i];
  811. }
  812. if (count > 255)
  813. stop_decoding(JPGD_BAD_DHT_COUNTS);
  814. for (i = 0; i < count; i++)
  815. huff_val[i] = static_cast<uint8>(get_bits(8));
  816. i = 1 + 16 + count;
  817. if (num_left < (uint)i)
  818. stop_decoding(JPGD_BAD_DHT_MARKER);
  819. num_left -= i;
  820. if ((index & 0x10) > 0x10)
  821. stop_decoding(JPGD_BAD_DHT_INDEX);
  822. index = (index & 0x0F) + ((index & 0x10) >> 4) * (JPGD_MAX_HUFF_TABLES >> 1);
  823. if (index >= JPGD_MAX_HUFF_TABLES)
  824. stop_decoding(JPGD_BAD_DHT_INDEX);
  825. if (!m_huff_num[index])
  826. m_huff_num[index] = (uint8 *)alloc(17);
  827. if (!m_huff_val[index])
  828. m_huff_val[index] = (uint8 *)alloc(256);
  829. m_huff_ac[index] = (index & 0x10) != 0;
  830. memcpy(m_huff_num[index], huff_num, 17);
  831. memcpy(m_huff_val[index], huff_val, 256);
  832. }
  833. }
  834. // Read a quantization table.
  835. void jpeg_decoder::read_dqt_marker()
  836. {
  837. int n, i, prec;
  838. uint num_left;
  839. uint temp;
  840. num_left = get_bits(16);
  841. if (num_left < 2)
  842. stop_decoding(JPGD_BAD_DQT_MARKER);
  843. num_left -= 2;
  844. while (num_left)
  845. {
  846. n = get_bits(8);
  847. prec = n >> 4;
  848. n &= 0x0F;
  849. if (n >= JPGD_MAX_QUANT_TABLES)
  850. stop_decoding(JPGD_BAD_DQT_TABLE);
  851. if (!m_quant[n])
  852. m_quant[n] = (jpgd_quant_t *)alloc(64 * sizeof(jpgd_quant_t));
  853. // read quantization entries, in zag order
  854. for (i = 0; i < 64; i++)
  855. {
  856. temp = get_bits(8);
  857. if (prec)
  858. temp = (temp << 8) + get_bits(8);
  859. m_quant[n][i] = static_cast<jpgd_quant_t>(temp);
  860. }
  861. i = 64 + 1;
  862. if (prec)
  863. i += 64;
  864. if (num_left < (uint)i)
  865. stop_decoding(JPGD_BAD_DQT_LENGTH);
  866. num_left -= i;
  867. }
  868. }
  869. // Read the start of frame (SOF) marker.
  870. void jpeg_decoder::read_sof_marker()
  871. {
  872. int i;
  873. uint num_left;
  874. num_left = get_bits(16);
  875. if (get_bits(8) != 8) /* precision: sorry, only 8-bit precision is supported right now */
  876. stop_decoding(JPGD_BAD_PRECISION);
  877. m_image_y_size = get_bits(16);
  878. if ((m_image_y_size < 1) || (m_image_y_size > JPGD_MAX_HEIGHT))
  879. stop_decoding(JPGD_BAD_HEIGHT);
  880. m_image_x_size = get_bits(16);
  881. if ((m_image_x_size < 1) || (m_image_x_size > JPGD_MAX_WIDTH))
  882. stop_decoding(JPGD_BAD_WIDTH);
  883. m_comps_in_frame = get_bits(8);
  884. if (m_comps_in_frame > JPGD_MAX_COMPONENTS)
  885. stop_decoding(JPGD_TOO_MANY_COMPONENTS);
  886. if (num_left != (uint)(m_comps_in_frame * 3 + 8))
  887. stop_decoding(JPGD_BAD_SOF_LENGTH);
  888. for (i = 0; i < m_comps_in_frame; i++)
  889. {
  890. m_comp_ident[i] = get_bits(8);
  891. m_comp_h_samp[i] = get_bits(4);
  892. m_comp_v_samp[i] = get_bits(4);
  893. m_comp_quant[i] = get_bits(8);
  894. }
  895. }
  896. // Used to skip unrecognized markers.
  897. void jpeg_decoder::skip_variable_marker()
  898. {
  899. uint num_left;
  900. num_left = get_bits(16);
  901. if (num_left < 2)
  902. stop_decoding(JPGD_BAD_VARIABLE_MARKER);
  903. num_left -= 2;
  904. while (num_left)
  905. {
  906. get_bits(8);
  907. num_left--;
  908. }
  909. }
  910. // Read a define restart interval (DRI) marker.
  911. void jpeg_decoder::read_dri_marker()
  912. {
  913. if (get_bits(16) != 4)
  914. stop_decoding(JPGD_BAD_DRI_LENGTH);
  915. m_restart_interval = get_bits(16);
  916. }
  917. // Read a start of scan (SOS) marker.
  918. void jpeg_decoder::read_sos_marker()
  919. {
  920. uint num_left;
  921. int i, ci, n, c, cc;
  922. num_left = get_bits(16);
  923. n = get_bits(8);
  924. m_comps_in_scan = n;
  925. num_left -= 3;
  926. if ( (num_left != (uint)(n * 2 + 3)) || (n < 1) || (n > JPGD_MAX_COMPS_IN_SCAN) )
  927. stop_decoding(JPGD_BAD_SOS_LENGTH);
  928. for (i = 0; i < n; i++)
  929. {
  930. cc = get_bits(8);
  931. c = get_bits(8);
  932. num_left -= 2;
  933. for (ci = 0; ci < m_comps_in_frame; ci++)
  934. if (cc == m_comp_ident[ci])
  935. break;
  936. if (ci >= m_comps_in_frame)
  937. stop_decoding(JPGD_BAD_SOS_COMP_ID);
  938. m_comp_list[i] = ci;
  939. m_comp_dc_tab[ci] = (c >> 4) & 15;
  940. m_comp_ac_tab[ci] = (c & 15) + (JPGD_MAX_HUFF_TABLES >> 1);
  941. }
  942. m_spectral_start = get_bits(8);
  943. m_spectral_end = get_bits(8);
  944. m_successive_high = get_bits(4);
  945. m_successive_low = get_bits(4);
  946. if (!m_progressive_flag)
  947. {
  948. m_spectral_start = 0;
  949. m_spectral_end = 63;
  950. }
  951. num_left -= 3;
  952. while (num_left) /* read past whatever is num_left */
  953. {
  954. get_bits(8);
  955. num_left--;
  956. }
  957. }
  958. // Finds the next marker.
  959. int jpeg_decoder::next_marker()
  960. {
  961. uint c, bytes;
  962. bytes = 0;
  963. do
  964. {
  965. do
  966. {
  967. bytes++;
  968. c = get_bits(8);
  969. } while (c != 0xFF);
  970. do
  971. {
  972. c = get_bits(8);
  973. } while (c == 0xFF);
  974. } while (c == 0);
  975. // If bytes > 0 here, there where extra bytes before the marker (not good).
  976. return c;
  977. }
  978. // Process markers. Returns when an SOFx, SOI, EOI, or SOS marker is
  979. // encountered.
  980. int jpeg_decoder::process_markers()
  981. {
  982. int c;
  983. for ( ; ; )
  984. {
  985. c = next_marker();
  986. switch (c)
  987. {
  988. case M_SOF0:
  989. case M_SOF1:
  990. case M_SOF2:
  991. case M_SOF3:
  992. case M_SOF5:
  993. case M_SOF6:
  994. case M_SOF7:
  995. // case M_JPG:
  996. case M_SOF9:
  997. case M_SOF10:
  998. case M_SOF11:
  999. case M_SOF13:
  1000. case M_SOF14:
  1001. case M_SOF15:
  1002. case M_SOI:
  1003. case M_EOI:
  1004. case M_SOS:
  1005. {
  1006. return c;
  1007. }
  1008. case M_DHT:
  1009. {
  1010. read_dht_marker();
  1011. break;
  1012. }
  1013. // No arithmitic support - dumb patents!
  1014. case M_DAC:
  1015. {
  1016. stop_decoding(JPGD_NO_ARITHMITIC_SUPPORT);
  1017. break;
  1018. }
  1019. case M_DQT:
  1020. {
  1021. read_dqt_marker();
  1022. break;
  1023. }
  1024. case M_DRI:
  1025. {
  1026. read_dri_marker();
  1027. break;
  1028. }
  1029. //case M_APP0: /* no need to read the JFIF marker */
  1030. case M_JPG:
  1031. case M_RST0: /* no parameters */
  1032. case M_RST1:
  1033. case M_RST2:
  1034. case M_RST3:
  1035. case M_RST4:
  1036. case M_RST5:
  1037. case M_RST6:
  1038. case M_RST7:
  1039. case M_TEM:
  1040. {
  1041. stop_decoding(JPGD_UNEXPECTED_MARKER);
  1042. break;
  1043. }
  1044. default: /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn or APP0 */
  1045. {
  1046. skip_variable_marker();
  1047. break;
  1048. }
  1049. }
  1050. }
  1051. }
  1052. // Finds the start of image (SOI) marker.
  1053. // This code is rather defensive: it only checks the first 512 bytes to avoid
  1054. // false positives.
  1055. void jpeg_decoder::locate_soi_marker()
  1056. {
  1057. uint lastchar, thischar;
  1058. uint bytesleft;
  1059. lastchar = get_bits(8);
  1060. thischar = get_bits(8);
  1061. /* ok if it's a normal JPEG file without a special header */
  1062. if ((lastchar == 0xFF) && (thischar == M_SOI))
  1063. return;
  1064. bytesleft = 4096; //512;
  1065. for ( ; ; )
  1066. {
  1067. if (--bytesleft == 0)
  1068. stop_decoding(JPGD_NOT_JPEG);
  1069. lastchar = thischar;
  1070. thischar = get_bits(8);
  1071. if (lastchar == 0xFF)
  1072. {
  1073. if (thischar == M_SOI)
  1074. break;
  1075. else if (thischar == M_EOI) // get_bits will keep returning M_EOI if we read past the end
  1076. stop_decoding(JPGD_NOT_JPEG);
  1077. }
  1078. }
  1079. // Check the next character after marker: if it's not 0xFF, it can't be the start of the next marker, so the file is bad.
  1080. thischar = (m_bit_buf >> 24) & 0xFF;
  1081. if (thischar != 0xFF)
  1082. stop_decoding(JPGD_NOT_JPEG);
  1083. }
  1084. // Find a start of frame (SOF) marker.
  1085. void jpeg_decoder::locate_sof_marker()
  1086. {
  1087. locate_soi_marker();
  1088. int c = process_markers();
  1089. switch (c)
  1090. {
  1091. case M_SOF2:
  1092. m_progressive_flag = JPGD_TRUE;
  1093. case M_SOF0: /* baseline DCT */
  1094. case M_SOF1: /* extended sequential DCT */
  1095. {
  1096. read_sof_marker();
  1097. break;
  1098. }
  1099. case M_SOF9: /* Arithmitic coding */
  1100. {
  1101. stop_decoding(JPGD_NO_ARITHMITIC_SUPPORT);
  1102. break;
  1103. }
  1104. default:
  1105. {
  1106. stop_decoding(JPGD_UNSUPPORTED_MARKER);
  1107. break;
  1108. }
  1109. }
  1110. }
  1111. // Find a start of scan (SOS) marker.
  1112. int jpeg_decoder::locate_sos_marker()
  1113. {
  1114. int c;
  1115. c = process_markers();
  1116. if (c == M_EOI)
  1117. return JPGD_FALSE;
  1118. else if (c != M_SOS)
  1119. stop_decoding(JPGD_UNEXPECTED_MARKER);
  1120. read_sos_marker();
  1121. return JPGD_TRUE;
  1122. }
  1123. // Reset everything to default/uninitialized state.
  1124. void jpeg_decoder::init(jpeg_decoder_stream *pStream)
  1125. {
  1126. m_pMem_blocks = NULL;
  1127. m_error_code = JPGD_SUCCESS;
  1128. m_ready_flag = false;
  1129. m_image_x_size = m_image_y_size = 0;
  1130. m_pStream = pStream;
  1131. m_progressive_flag = JPGD_FALSE;
  1132. memset(m_huff_ac, 0, sizeof(m_huff_ac));
  1133. memset(m_huff_num, 0, sizeof(m_huff_num));
  1134. memset(m_huff_val, 0, sizeof(m_huff_val));
  1135. memset(m_quant, 0, sizeof(m_quant));
  1136. m_scan_type = 0;
  1137. m_comps_in_frame = 0;
  1138. memset(m_comp_h_samp, 0, sizeof(m_comp_h_samp));
  1139. memset(m_comp_v_samp, 0, sizeof(m_comp_v_samp));
  1140. memset(m_comp_quant, 0, sizeof(m_comp_quant));
  1141. memset(m_comp_ident, 0, sizeof(m_comp_ident));
  1142. memset(m_comp_h_blocks, 0, sizeof(m_comp_h_blocks));
  1143. memset(m_comp_v_blocks, 0, sizeof(m_comp_v_blocks));
  1144. m_comps_in_scan = 0;
  1145. memset(m_comp_list, 0, sizeof(m_comp_list));
  1146. memset(m_comp_dc_tab, 0, sizeof(m_comp_dc_tab));
  1147. memset(m_comp_ac_tab, 0, sizeof(m_comp_ac_tab));
  1148. m_spectral_start = 0;
  1149. m_spectral_end = 0;
  1150. m_successive_low = 0;
  1151. m_successive_high = 0;
  1152. m_max_mcu_x_size = 0;
  1153. m_max_mcu_y_size = 0;
  1154. m_blocks_per_mcu = 0;
  1155. m_max_blocks_per_row = 0;
  1156. m_mcus_per_row = 0;
  1157. m_mcus_per_col = 0;
  1158. m_expanded_blocks_per_component = 0;
  1159. m_expanded_blocks_per_mcu = 0;
  1160. m_expanded_blocks_per_row = 0;
  1161. m_freq_domain_chroma_upsample = false;
  1162. memset(m_mcu_org, 0, sizeof(m_mcu_org));
  1163. m_total_lines_left = 0;
  1164. m_mcu_lines_left = 0;
  1165. m_real_dest_bytes_per_scan_line = 0;
  1166. m_dest_bytes_per_scan_line = 0;
  1167. m_dest_bytes_per_pixel = 0;
  1168. memset(m_pHuff_tabs, 0, sizeof(m_pHuff_tabs));
  1169. memset(m_dc_coeffs, 0, sizeof(m_dc_coeffs));
  1170. memset(m_ac_coeffs, 0, sizeof(m_ac_coeffs));
  1171. memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
  1172. m_eob_run = 0;
  1173. memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
  1174. m_pIn_buf_ofs = m_in_buf;
  1175. m_in_buf_left = 0;
  1176. m_eof_flag = false;
  1177. m_tem_flag = 0;
  1178. memset(m_in_buf_pad_start, 0, sizeof(m_in_buf_pad_start));
  1179. memset(m_in_buf, 0, sizeof(m_in_buf));
  1180. memset(m_in_buf_pad_end, 0, sizeof(m_in_buf_pad_end));
  1181. m_restart_interval = 0;
  1182. m_restarts_left = 0;
  1183. m_next_restart_num = 0;
  1184. m_max_mcus_per_row = 0;
  1185. m_max_blocks_per_mcu = 0;
  1186. m_max_mcus_per_col = 0;
  1187. memset(m_last_dc_val, 0, sizeof(m_last_dc_val));
  1188. m_pMCU_coefficients = NULL;
  1189. m_pSample_buf = NULL;
  1190. m_total_bytes_read = 0;
  1191. m_pScan_line_0 = NULL;
  1192. m_pScan_line_1 = NULL;
  1193. // Ready the input buffer.
  1194. prep_in_buffer();
  1195. // Prime the bit buffer.
  1196. m_bits_left = 16;
  1197. m_bit_buf = 0;
  1198. get_bits(16);
  1199. get_bits(16);
  1200. for (int i = 0; i < JPGD_MAX_BLOCKS_PER_MCU; i++)
  1201. m_mcu_block_max_zag[i] = 64;
  1202. }
  1203. #define SCALEBITS 16
  1204. #define ONE_HALF ((int) 1 << (SCALEBITS-1))
  1205. #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5f))
  1206. // Create a few tables that allow us to quickly convert YCbCr to RGB.
  1207. void jpeg_decoder::create_look_ups()
  1208. {
  1209. for (int i = 0; i <= 255; i++)
  1210. {
  1211. int k = i - 128;
  1212. m_crr[i] = ( FIX(1.40200f) * k + ONE_HALF) >> SCALEBITS;
  1213. m_cbb[i] = ( FIX(1.77200f) * k + ONE_HALF) >> SCALEBITS;
  1214. m_crg[i] = (-FIX(0.71414f)) * k;
  1215. m_cbg[i] = (-FIX(0.34414f)) * k + ONE_HALF;
  1216. }
  1217. }
  1218. // This method throws back into the stream any bytes that where read
  1219. // into the bit buffer during initial marker scanning.
  1220. void jpeg_decoder::fix_in_buffer()
  1221. {
  1222. // In case any 0xFF's where pulled into the buffer during marker scanning.
  1223. JPGD_ASSERT((m_bits_left & 7) == 0);
  1224. if (m_bits_left == 16)
  1225. stuff_char( (uint8)(m_bit_buf & 0xFF));
  1226. if (m_bits_left >= 8)
  1227. stuff_char( (uint8)((m_bit_buf >> 8) & 0xFF));
  1228. stuff_char((uint8)((m_bit_buf >> 16) & 0xFF));
  1229. stuff_char((uint8)((m_bit_buf >> 24) & 0xFF));
  1230. m_bits_left = 16;
  1231. get_bits_no_markers(16);
  1232. get_bits_no_markers(16);
  1233. }
  1234. void jpeg_decoder::transform_mcu(int mcu_row)
  1235. {
  1236. jpgd_block_t* pSrc_ptr = m_pMCU_coefficients;
  1237. uint8* pDst_ptr = m_pSample_buf + mcu_row * m_blocks_per_mcu * 64;
  1238. for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++)
  1239. {
  1240. idct(pSrc_ptr, pDst_ptr, m_mcu_block_max_zag[mcu_block]);
  1241. pSrc_ptr += 64;
  1242. pDst_ptr += 64;
  1243. }
  1244. }
  1245. static const uint8 s_max_rc[64] =
  1246. {
  1247. 17, 18, 34, 50, 50, 51, 52, 52, 52, 68, 84, 84, 84, 84, 85, 86, 86, 86, 86, 86,
  1248. 102, 118, 118, 118, 118, 118, 118, 119, 120, 120, 120, 120, 120, 120, 120, 136,
  1249. 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136,
  1250. 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136
  1251. };
  1252. void jpeg_decoder::transform_mcu_expand(int mcu_row)
  1253. {
  1254. jpgd_block_t* pSrc_ptr = m_pMCU_coefficients;
  1255. uint8* pDst_ptr = m_pSample_buf + mcu_row * m_expanded_blocks_per_mcu * 64;
  1256. // Y IDCT
  1257. int mcu_block;
  1258. for (mcu_block = 0; mcu_block < m_expanded_blocks_per_component; mcu_block++)
  1259. {
  1260. idct(pSrc_ptr, pDst_ptr, m_mcu_block_max_zag[mcu_block]);
  1261. pSrc_ptr += 64;
  1262. pDst_ptr += 64;
  1263. }
  1264. // Chroma IDCT, with upsampling
  1265. jpgd_block_t temp_block[64];
  1266. for (int i = 0; i < 2; i++)
  1267. {
  1268. DCT_Upsample::Matrix44 P, Q, R, S;
  1269. JPGD_ASSERT(m_mcu_block_max_zag[mcu_block] >= 1);
  1270. JPGD_ASSERT(m_mcu_block_max_zag[mcu_block] <= 64);
  1271. int max_zag = m_mcu_block_max_zag[mcu_block++] - 1;
  1272. if (max_zag <= 0) max_zag = 0; // should never happen, only here to shut up static analysis
  1273. switch (s_max_rc[max_zag])
  1274. {
  1275. case 1*16+1:
  1276. DCT_Upsample::P_Q<1, 1>::calc(P, Q, pSrc_ptr);
  1277. DCT_Upsample::R_S<1, 1>::calc(R, S, pSrc_ptr);
  1278. break;
  1279. case 1*16+2:
  1280. DCT_Upsample::P_Q<1, 2>::calc(P, Q, pSrc_ptr);
  1281. DCT_Upsample::R_S<1, 2>::calc(R, S, pSrc_ptr);
  1282. break;
  1283. case 2*16+2:
  1284. DCT_Upsample::P_Q<2, 2>::calc(P, Q, pSrc_ptr);
  1285. DCT_Upsample::R_S<2, 2>::calc(R, S, pSrc_ptr);
  1286. break;
  1287. case 3*16+2:
  1288. DCT_Upsample::P_Q<3, 2>::calc(P, Q, pSrc_ptr);
  1289. DCT_Upsample::R_S<3, 2>::calc(R, S, pSrc_ptr);
  1290. break;
  1291. case 3*16+3:
  1292. DCT_Upsample::P_Q<3, 3>::calc(P, Q, pSrc_ptr);
  1293. DCT_Upsample::R_S<3, 3>::calc(R, S, pSrc_ptr);
  1294. break;
  1295. case 3*16+4:
  1296. DCT_Upsample::P_Q<3, 4>::calc(P, Q, pSrc_ptr);
  1297. DCT_Upsample::R_S<3, 4>::calc(R, S, pSrc_ptr);
  1298. break;
  1299. case 4*16+4:
  1300. DCT_Upsample::P_Q<4, 4>::calc(P, Q, pSrc_ptr);
  1301. DCT_Upsample::R_S<4, 4>::calc(R, S, pSrc_ptr);
  1302. break;
  1303. case 5*16+4:
  1304. DCT_Upsample::P_Q<5, 4>::calc(P, Q, pSrc_ptr);
  1305. DCT_Upsample::R_S<5, 4>::calc(R, S, pSrc_ptr);
  1306. break;
  1307. case 5*16+5:
  1308. DCT_Upsample::P_Q<5, 5>::calc(P, Q, pSrc_ptr);
  1309. DCT_Upsample::R_S<5, 5>::calc(R, S, pSrc_ptr);
  1310. break;
  1311. case 5*16+6:
  1312. DCT_Upsample::P_Q<5, 6>::calc(P, Q, pSrc_ptr);
  1313. DCT_Upsample::R_S<5, 6>::calc(R, S, pSrc_ptr);
  1314. break;
  1315. case 6*16+6:
  1316. DCT_Upsample::P_Q<6, 6>::calc(P, Q, pSrc_ptr);
  1317. DCT_Upsample::R_S<6, 6>::calc(R, S, pSrc_ptr);
  1318. break;
  1319. case 7*16+6:
  1320. DCT_Upsample::P_Q<7, 6>::calc(P, Q, pSrc_ptr);
  1321. DCT_Upsample::R_S<7, 6>::calc(R, S, pSrc_ptr);
  1322. break;
  1323. case 7*16+7:
  1324. DCT_Upsample::P_Q<7, 7>::calc(P, Q, pSrc_ptr);
  1325. DCT_Upsample::R_S<7, 7>::calc(R, S, pSrc_ptr);
  1326. break;
  1327. case 7*16+8:
  1328. DCT_Upsample::P_Q<7, 8>::calc(P, Q, pSrc_ptr);
  1329. DCT_Upsample::R_S<7, 8>::calc(R, S, pSrc_ptr);
  1330. break;
  1331. case 8*16+8:
  1332. DCT_Upsample::P_Q<8, 8>::calc(P, Q, pSrc_ptr);
  1333. DCT_Upsample::R_S<8, 8>::calc(R, S, pSrc_ptr);
  1334. break;
  1335. default:
  1336. JPGD_ASSERT(false);
  1337. }
  1338. DCT_Upsample::Matrix44 a(P + Q); P -= Q;
  1339. DCT_Upsample::Matrix44& b = P;
  1340. DCT_Upsample::Matrix44 c(R + S); R -= S;
  1341. DCT_Upsample::Matrix44& d = R;
  1342. DCT_Upsample::Matrix44::add_and_store(temp_block, a, c);
  1343. idct_4x4(temp_block, pDst_ptr);
  1344. pDst_ptr += 64;
  1345. DCT_Upsample::Matrix44::sub_and_store(temp_block, a, c);
  1346. idct_4x4(temp_block, pDst_ptr);
  1347. pDst_ptr += 64;
  1348. DCT_Upsample::Matrix44::add_and_store(temp_block, b, d);
  1349. idct_4x4(temp_block, pDst_ptr);
  1350. pDst_ptr += 64;
  1351. DCT_Upsample::Matrix44::sub_and_store(temp_block, b, d);
  1352. idct_4x4(temp_block, pDst_ptr);
  1353. pDst_ptr += 64;
  1354. pSrc_ptr += 64;
  1355. }
  1356. }
  1357. // Loads and dequantizes the next row of (already decoded) coefficients.
  1358. // Progressive images only.
  1359. void jpeg_decoder::load_next_row()
  1360. {
  1361. int i;
  1362. jpgd_block_t *p;
  1363. jpgd_quant_t *q;
  1364. int mcu_row, mcu_block, row_block = 0;
  1365. int component_num, component_id;
  1366. int block_x_mcu[JPGD_MAX_COMPONENTS];
  1367. memset(block_x_mcu, 0, JPGD_MAX_COMPONENTS * sizeof(int));
  1368. for (mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++)
  1369. {
  1370. int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
  1371. for (mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++)
  1372. {
  1373. component_id = m_mcu_org[mcu_block];
  1374. q = m_quant[m_comp_quant[component_id]];
  1375. p = m_pMCU_coefficients + 64 * mcu_block;
  1376. jpgd_block_t* pAC = coeff_buf_getp(m_ac_coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
  1377. jpgd_block_t* pDC = coeff_buf_getp(m_dc_coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
  1378. p[0] = pDC[0];
  1379. memcpy(&p[1], &pAC[1], 63 * sizeof(jpgd_block_t));
  1380. for (i = 63; i > 0; i--)
  1381. if (p[g_ZAG[i]])
  1382. break;
  1383. m_mcu_block_max_zag[mcu_block] = i + 1;
  1384. for ( ; i >= 0; i--)
  1385. if (p[g_ZAG[i]])
  1386. p[g_ZAG[i]] = static_cast<jpgd_block_t>(p[g_ZAG[i]] * q[i]);
  1387. row_block++;
  1388. if (m_comps_in_scan == 1)
  1389. block_x_mcu[component_id]++;
  1390. else
  1391. {
  1392. if (++block_x_mcu_ofs == m_comp_h_samp[component_id])
  1393. {
  1394. block_x_mcu_ofs = 0;
  1395. if (++block_y_mcu_ofs == m_comp_v_samp[component_id])
  1396. {
  1397. block_y_mcu_ofs = 0;
  1398. block_x_mcu[component_id] += m_comp_h_samp[component_id];
  1399. }
  1400. }
  1401. }
  1402. }
  1403. if (m_freq_domain_chroma_upsample)
  1404. transform_mcu_expand(mcu_row);
  1405. else
  1406. transform_mcu(mcu_row);
  1407. }
  1408. if (m_comps_in_scan == 1)
  1409. m_block_y_mcu[m_comp_list[0]]++;
  1410. else
  1411. {
  1412. for (component_num = 0; component_num < m_comps_in_scan; component_num++)
  1413. {
  1414. component_id = m_comp_list[component_num];
  1415. m_block_y_mcu[component_id] += m_comp_v_samp[component_id];
  1416. }
  1417. }
  1418. }
  1419. // Restart interval processing.
  1420. void jpeg_decoder::process_restart()
  1421. {
  1422. int i;
  1423. int c = 0;
  1424. // Align to a byte boundry
  1425. // FIXME: Is this really necessary? get_bits_no_markers() never reads in markers!
  1426. //get_bits_no_markers(m_bits_left & 7);
  1427. // Let's scan a little bit to find the marker, but not _too_ far.
  1428. // 1536 is a "fudge factor" that determines how much to scan.
  1429. for (i = 1536; i > 0; i--)
  1430. if (get_char() == 0xFF)
  1431. break;
  1432. if (i == 0)
  1433. stop_decoding(JPGD_BAD_RESTART_MARKER);
  1434. for ( ; i > 0; i--)
  1435. if ((c = get_char()) != 0xFF)
  1436. break;
  1437. if (i == 0)
  1438. stop_decoding(JPGD_BAD_RESTART_MARKER);
  1439. // Is it the expected marker? If not, something bad happened.
  1440. if (c != (m_next_restart_num + M_RST0))
  1441. stop_decoding(JPGD_BAD_RESTART_MARKER);
  1442. // Reset each component's DC prediction values.
  1443. memset(&m_last_dc_val, 0, m_comps_in_frame * sizeof(uint));
  1444. m_eob_run = 0;
  1445. m_restarts_left = m_restart_interval;
  1446. m_next_restart_num = (m_next_restart_num + 1) & 7;
  1447. // Get the bit buffer going again...
  1448. m_bits_left = 16;
  1449. get_bits_no_markers(16);
  1450. get_bits_no_markers(16);
  1451. }
  1452. static inline int dequantize_ac(int c, int q) { c *= q; return c; }
  1453. // Decodes and dequantizes the next row of coefficients.
  1454. void jpeg_decoder::decode_next_row()
  1455. {
  1456. int row_block = 0;
  1457. for (int mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++)
  1458. {
  1459. if ((m_restart_interval) && (m_restarts_left == 0))
  1460. process_restart();
  1461. jpgd_block_t* p = m_pMCU_coefficients;
  1462. for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++, p += 64)
  1463. {
  1464. int component_id = m_mcu_org[mcu_block];
  1465. jpgd_quant_t* q = m_quant[m_comp_quant[component_id]];
  1466. int r, s;
  1467. s = huff_decode(m_pHuff_tabs[m_comp_dc_tab[component_id]], r);
  1468. s = JPGD_HUFF_EXTEND(r, s);
  1469. m_last_dc_val[component_id] = (s += m_last_dc_val[component_id]);
  1470. p[0] = static_cast<jpgd_block_t>(s * q[0]);
  1471. int prev_num_set = m_mcu_block_max_zag[mcu_block];
  1472. huff_tables *pH = m_pHuff_tabs[m_comp_ac_tab[component_id]];
  1473. int k;
  1474. for (k = 1; k < 64; k++)
  1475. {
  1476. int extra_bits;
  1477. s = huff_decode(pH, extra_bits);
  1478. r = s >> 4;
  1479. s &= 15;
  1480. if (s)
  1481. {
  1482. if (r)
  1483. {
  1484. if ((k + r) > 63)
  1485. stop_decoding(JPGD_DECODE_ERROR);
  1486. if (k < prev_num_set)
  1487. {
  1488. int n = JPGD_MIN(r, prev_num_set - k);
  1489. int kt = k;
  1490. while (n--)
  1491. p[g_ZAG[kt++]] = 0;
  1492. }
  1493. k += r;
  1494. }
  1495. s = JPGD_HUFF_EXTEND(extra_bits, s);
  1496. JPGD_ASSERT(k < 64);
  1497. p[g_ZAG[k]] = static_cast<jpgd_block_t>(dequantize_ac(s, q[k])); //s * q[k];
  1498. }
  1499. else
  1500. {
  1501. if (r == 15)
  1502. {
  1503. if ((k + 16) > 64)
  1504. stop_decoding(JPGD_DECODE_ERROR);
  1505. if (k < prev_num_set)
  1506. {
  1507. int n = JPGD_MIN(16, prev_num_set - k);
  1508. int kt = k;
  1509. while (n--)
  1510. {
  1511. JPGD_ASSERT(kt <= 63);
  1512. p[g_ZAG[kt++]] = 0;
  1513. }
  1514. }
  1515. k += 16 - 1; // - 1 because the loop counter is k
  1516. JPGD_ASSERT(p[g_ZAG[k]] == 0);
  1517. }
  1518. else
  1519. break;
  1520. }
  1521. }
  1522. if (k < prev_num_set)
  1523. {
  1524. int kt = k;
  1525. while (kt < prev_num_set)
  1526. p[g_ZAG[kt++]] = 0;
  1527. }
  1528. m_mcu_block_max_zag[mcu_block] = k;
  1529. row_block++;
  1530. }
  1531. if (m_freq_domain_chroma_upsample)
  1532. transform_mcu_expand(mcu_row);
  1533. else
  1534. transform_mcu(mcu_row);
  1535. m_restarts_left--;
  1536. }
  1537. }
  1538. // YCbCr H1V1 (1x1:1:1, 3 m_blocks per MCU) to RGB
  1539. void jpeg_decoder::H1V1Convert()
  1540. {
  1541. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1542. uint8 *d = m_pScan_line_0;
  1543. uint8 *s = m_pSample_buf + row * 8;
  1544. for (int i = m_max_mcus_per_row; i > 0; i--)
  1545. {
  1546. for (int j = 0; j < 8; j++)
  1547. {
  1548. int y = s[j];
  1549. int cb = s[64+j];
  1550. int cr = s[128+j];
  1551. d[0] = clamp(y + m_crr[cr]);
  1552. d[1] = clamp(y + ((m_crg[cr] + m_cbg[cb]) >> 16));
  1553. d[2] = clamp(y + m_cbb[cb]);
  1554. d[3] = 255;
  1555. d += 4;
  1556. }
  1557. s += 64*3;
  1558. }
  1559. }
  1560. // YCbCr H2V1 (2x1:1:1, 4 m_blocks per MCU) to RGB
  1561. void jpeg_decoder::H2V1Convert()
  1562. {
  1563. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1564. uint8 *d0 = m_pScan_line_0;
  1565. uint8 *y = m_pSample_buf + row * 8;
  1566. uint8 *c = m_pSample_buf + 2*64 + row * 8;
  1567. for (int i = m_max_mcus_per_row; i > 0; i--)
  1568. {
  1569. for (int l = 0; l < 2; l++)
  1570. {
  1571. for (int j = 0; j < 4; j++)
  1572. {
  1573. int cb = c[0];
  1574. int cr = c[64];
  1575. int rc = m_crr[cr];
  1576. int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
  1577. int bc = m_cbb[cb];
  1578. int yy = y[j<<1];
  1579. d0[0] = clamp(yy+rc);
  1580. d0[1] = clamp(yy+gc);
  1581. d0[2] = clamp(yy+bc);
  1582. d0[3] = 255;
  1583. yy = y[(j<<1)+1];
  1584. d0[4] = clamp(yy+rc);
  1585. d0[5] = clamp(yy+gc);
  1586. d0[6] = clamp(yy+bc);
  1587. d0[7] = 255;
  1588. d0 += 8;
  1589. c++;
  1590. }
  1591. y += 64;
  1592. }
  1593. y += 64*4 - 64*2;
  1594. c += 64*4 - 8;
  1595. }
  1596. }
  1597. // YCbCr H2V1 (1x2:1:1, 4 m_blocks per MCU) to RGB
  1598. void jpeg_decoder::H1V2Convert()
  1599. {
  1600. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1601. uint8 *d0 = m_pScan_line_0;
  1602. uint8 *d1 = m_pScan_line_1;
  1603. uint8 *y;
  1604. uint8 *c;
  1605. if (row < 8)
  1606. y = m_pSample_buf + row * 8;
  1607. else
  1608. y = m_pSample_buf + 64*1 + (row & 7) * 8;
  1609. c = m_pSample_buf + 64*2 + (row >> 1) * 8;
  1610. for (int i = m_max_mcus_per_row; i > 0; i--)
  1611. {
  1612. for (int j = 0; j < 8; j++)
  1613. {
  1614. int cb = c[0+j];
  1615. int cr = c[64+j];
  1616. int rc = m_crr[cr];
  1617. int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
  1618. int bc = m_cbb[cb];
  1619. int yy = y[j];
  1620. d0[0] = clamp(yy+rc);
  1621. d0[1] = clamp(yy+gc);
  1622. d0[2] = clamp(yy+bc);
  1623. d0[3] = 255;
  1624. yy = y[8+j];
  1625. d1[0] = clamp(yy+rc);
  1626. d1[1] = clamp(yy+gc);
  1627. d1[2] = clamp(yy+bc);
  1628. d1[3] = 255;
  1629. d0 += 4;
  1630. d1 += 4;
  1631. }
  1632. y += 64*4;
  1633. c += 64*4;
  1634. }
  1635. }
  1636. // YCbCr H2V2 (2x2:1:1, 6 m_blocks per MCU) to RGB
  1637. void jpeg_decoder::H2V2Convert()
  1638. {
  1639. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1640. uint8 *d0 = m_pScan_line_0;
  1641. uint8 *d1 = m_pScan_line_1;
  1642. uint8 *y;
  1643. uint8 *c;
  1644. if (row < 8)
  1645. y = m_pSample_buf + row * 8;
  1646. else
  1647. y = m_pSample_buf + 64*2 + (row & 7) * 8;
  1648. c = m_pSample_buf + 64*4 + (row >> 1) * 8;
  1649. for (int i = m_max_mcus_per_row; i > 0; i--)
  1650. {
  1651. for (int l = 0; l < 2; l++)
  1652. {
  1653. for (int j = 0; j < 8; j += 2)
  1654. {
  1655. int cb = c[0];
  1656. int cr = c[64];
  1657. int rc = m_crr[cr];
  1658. int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
  1659. int bc = m_cbb[cb];
  1660. int yy = y[j];
  1661. d0[0] = clamp(yy+rc);
  1662. d0[1] = clamp(yy+gc);
  1663. d0[2] = clamp(yy+bc);
  1664. d0[3] = 255;
  1665. yy = y[j+1];
  1666. d0[4] = clamp(yy+rc);
  1667. d0[5] = clamp(yy+gc);
  1668. d0[6] = clamp(yy+bc);
  1669. d0[7] = 255;
  1670. yy = y[j+8];
  1671. d1[0] = clamp(yy+rc);
  1672. d1[1] = clamp(yy+gc);
  1673. d1[2] = clamp(yy+bc);
  1674. d1[3] = 255;
  1675. yy = y[j+8+1];
  1676. d1[4] = clamp(yy+rc);
  1677. d1[5] = clamp(yy+gc);
  1678. d1[6] = clamp(yy+bc);
  1679. d1[7] = 255;
  1680. d0 += 8;
  1681. d1 += 8;
  1682. c++;
  1683. }
  1684. y += 64;
  1685. }
  1686. y += 64*6 - 64*2;
  1687. c += 64*6 - 8;
  1688. }
  1689. }
  1690. // Y (1 block per MCU) to 8-bit grayscale
  1691. void jpeg_decoder::gray_convert()
  1692. {
  1693. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1694. uint8 *d = m_pScan_line_0;
  1695. uint8 *s = m_pSample_buf + row * 8;
  1696. for (int i = m_max_mcus_per_row; i > 0; i--)
  1697. {
  1698. *(uint *)d = *(uint *)s;
  1699. *(uint *)(&d[4]) = *(uint *)(&s[4]);
  1700. s += 64;
  1701. d += 8;
  1702. }
  1703. }
  1704. void jpeg_decoder::expanded_convert()
  1705. {
  1706. int row = m_max_mcu_y_size - m_mcu_lines_left;
  1707. uint8* Py = m_pSample_buf + (row / 8) * 64 * m_comp_h_samp[0] + (row & 7) * 8;
  1708. uint8* d = m_pScan_line_0;
  1709. for (int i = m_max_mcus_per_row; i > 0; i--)
  1710. {
  1711. for (int k = 0; k < m_max_mcu_x_size; k += 8)
  1712. {
  1713. const int Y_ofs = k * 8;
  1714. const int Cb_ofs = Y_ofs + 64 * m_expanded_blocks_per_component;
  1715. const int Cr_ofs = Y_ofs + 64 * m_expanded_blocks_per_component * 2;
  1716. for (int j = 0; j < 8; j++)
  1717. {
  1718. int y = Py[Y_ofs + j];
  1719. int cb = Py[Cb_ofs + j];
  1720. int cr = Py[Cr_ofs + j];
  1721. d[0] = clamp(y + m_crr[cr]);
  1722. d[1] = clamp(y + ((m_crg[cr] + m_cbg[cb]) >> 16));
  1723. d[2] = clamp(y + m_cbb[cb]);
  1724. d[3] = 255;
  1725. d += 4;
  1726. }
  1727. }
  1728. Py += 64 * m_expanded_blocks_per_mcu;
  1729. }
  1730. }
  1731. // Find end of image (EOI) marker, so we can return to the user the exact size of the input stream.
  1732. void jpeg_decoder::find_eoi()
  1733. {
  1734. if (!m_progressive_flag)
  1735. {
  1736. // Attempt to read the EOI marker.
  1737. //get_bits_no_markers(m_bits_left & 7);
  1738. // Prime the bit buffer
  1739. m_bits_left = 16;
  1740. get_bits(16);
  1741. get_bits(16);
  1742. // The next marker _should_ be EOI
  1743. process_markers();
  1744. }
  1745. m_total_bytes_read -= m_in_buf_left;
  1746. }
  1747. int jpeg_decoder::decode(const void** pScan_line, uint* pScan_line_len)
  1748. {
  1749. if ((m_error_code) || (!m_ready_flag))
  1750. return JPGD_FAILED;
  1751. if (m_total_lines_left == 0)
  1752. return JPGD_DONE;
  1753. if (m_mcu_lines_left == 0)
  1754. {
  1755. if (setjmp(m_jmp_state))
  1756. return JPGD_FAILED;
  1757. if (m_progressive_flag)
  1758. load_next_row();
  1759. else
  1760. decode_next_row();
  1761. // Find the EOI marker if that was the last row.
  1762. if (m_total_lines_left <= m_max_mcu_y_size)
  1763. find_eoi();
  1764. m_mcu_lines_left = m_max_mcu_y_size;
  1765. }
  1766. if (m_freq_domain_chroma_upsample)
  1767. {
  1768. expanded_convert();
  1769. *pScan_line = m_pScan_line_0;
  1770. }
  1771. else
  1772. {
  1773. switch (m_scan_type)
  1774. {
  1775. case JPGD_YH2V2:
  1776. {
  1777. if ((m_mcu_lines_left & 1) == 0)
  1778. {
  1779. H2V2Convert();
  1780. *pScan_line = m_pScan_line_0;
  1781. }
  1782. else
  1783. *pScan_line = m_pScan_line_1;
  1784. break;
  1785. }
  1786. case JPGD_YH2V1:
  1787. {
  1788. H2V1Convert();
  1789. *pScan_line = m_pScan_line_0;
  1790. break;
  1791. }
  1792. case JPGD_YH1V2:
  1793. {
  1794. if ((m_mcu_lines_left & 1) == 0)
  1795. {
  1796. H1V2Convert();
  1797. *pScan_line = m_pScan_line_0;
  1798. }
  1799. else
  1800. *pScan_line = m_pScan_line_1;
  1801. break;
  1802. }
  1803. case JPGD_YH1V1:
  1804. {
  1805. H1V1Convert();
  1806. *pScan_line = m_pScan_line_0;
  1807. break;
  1808. }
  1809. case JPGD_GRAYSCALE:
  1810. {
  1811. gray_convert();
  1812. *pScan_line = m_pScan_line_0;
  1813. break;
  1814. }
  1815. }
  1816. }
  1817. *pScan_line_len = m_real_dest_bytes_per_scan_line;
  1818. m_mcu_lines_left--;
  1819. m_total_lines_left--;
  1820. return JPGD_SUCCESS;
  1821. }
  1822. // Creates the tables needed for efficient Huffman decoding.
  1823. void jpeg_decoder::make_huff_table(int index, huff_tables *pH)
  1824. {
  1825. int p, i, l, si;
  1826. uint8 huffsize[257];
  1827. uint huffcode[257];
  1828. uint code;
  1829. uint subtree;
  1830. int code_size;
  1831. int lastp;
  1832. int nextfreeentry;
  1833. int currententry;
  1834. pH->ac_table = m_huff_ac[index] != 0;
  1835. p = 0;
  1836. for (l = 1; l <= 16; l++)
  1837. {
  1838. for (i = 1; i <= m_huff_num[index][l]; i++)
  1839. huffsize[p++] = static_cast<uint8>(l);
  1840. }
  1841. huffsize[p] = 0;
  1842. lastp = p;
  1843. code = 0;
  1844. si = huffsize[0];
  1845. p = 0;
  1846. while (huffsize[p])
  1847. {
  1848. while (huffsize[p] == si)
  1849. {
  1850. huffcode[p++] = code;
  1851. code++;
  1852. }
  1853. code <<= 1;
  1854. si++;
  1855. }
  1856. memset(pH->look_up, 0, sizeof(pH->look_up));
  1857. memset(pH->look_up2, 0, sizeof(pH->look_up2));
  1858. memset(pH->tree, 0, sizeof(pH->tree));
  1859. memset(pH->code_size, 0, sizeof(pH->code_size));
  1860. nextfreeentry = -1;
  1861. p = 0;
  1862. while (p < lastp)
  1863. {
  1864. i = m_huff_val[index][p];
  1865. code = huffcode[p];
  1866. code_size = huffsize[p];
  1867. pH->code_size[i] = static_cast<uint8>(code_size);
  1868. if (code_size <= 8)
  1869. {
  1870. code <<= (8 - code_size);
  1871. for (l = 1 << (8 - code_size); l > 0; l--)
  1872. {
  1873. JPGD_ASSERT(i < 256);
  1874. pH->look_up[code] = i;
  1875. bool has_extrabits = false;
  1876. int extra_bits = 0;
  1877. int num_extra_bits = i & 15;
  1878. int bits_to_fetch = code_size;
  1879. if (num_extra_bits)
  1880. {
  1881. int total_codesize = code_size + num_extra_bits;
  1882. if (total_codesize <= 8)
  1883. {
  1884. has_extrabits = true;
  1885. extra_bits = ((1 << num_extra_bits) - 1) & (code >> (8 - total_codesize));
  1886. JPGD_ASSERT(extra_bits <= 0x7FFF);
  1887. bits_to_fetch += num_extra_bits;
  1888. }
  1889. }
  1890. if (!has_extrabits)
  1891. pH->look_up2[code] = i | (bits_to_fetch << 8);
  1892. else
  1893. pH->look_up2[code] = i | 0x8000 | (extra_bits << 16) | (bits_to_fetch << 8);
  1894. code++;
  1895. }
  1896. }
  1897. else
  1898. {
  1899. subtree = (code >> (code_size - 8)) & 0xFF;
  1900. currententry = pH->look_up[subtree];
  1901. if (currententry == 0)
  1902. {
  1903. pH->look_up[subtree] = currententry = nextfreeentry;
  1904. pH->look_up2[subtree] = currententry = nextfreeentry;
  1905. nextfreeentry -= 2;
  1906. }
  1907. code <<= (16 - (code_size - 8));
  1908. for (l = code_size; l > 9; l--)
  1909. {
  1910. if ((code & 0x8000) == 0)
  1911. currententry--;
  1912. if (pH->tree[-currententry - 1] == 0)
  1913. {
  1914. pH->tree[-currententry - 1] = nextfreeentry;
  1915. currententry = nextfreeentry;
  1916. nextfreeentry -= 2;
  1917. }
  1918. else
  1919. currententry = pH->tree[-currententry - 1];
  1920. code <<= 1;
  1921. }
  1922. if ((code & 0x8000) == 0)
  1923. currententry--;
  1924. pH->tree[-currententry - 1] = i;
  1925. }
  1926. p++;
  1927. }
  1928. }
  1929. // Verifies the quantization tables needed for this scan are available.
  1930. void jpeg_decoder::check_quant_tables()
  1931. {
  1932. for (int i = 0; i < m_comps_in_scan; i++)
  1933. if (m_quant[m_comp_quant[m_comp_list[i]]] == NULL)
  1934. stop_decoding(JPGD_UNDEFINED_QUANT_TABLE);
  1935. }
  1936. // Verifies that all the Huffman tables needed for this scan are available.
  1937. void jpeg_decoder::check_huff_tables()
  1938. {
  1939. for (int i = 0; i < m_comps_in_scan; i++)
  1940. {
  1941. if ((m_spectral_start == 0) && (m_huff_num[m_comp_dc_tab[m_comp_list[i]]] == NULL))
  1942. stop_decoding(JPGD_UNDEFINED_HUFF_TABLE);
  1943. if ((m_spectral_end > 0) && (m_huff_num[m_comp_ac_tab[m_comp_list[i]]] == NULL))
  1944. stop_decoding(JPGD_UNDEFINED_HUFF_TABLE);
  1945. }
  1946. for (int i = 0; i < JPGD_MAX_HUFF_TABLES; i++)
  1947. if (m_huff_num[i])
  1948. {
  1949. if (!m_pHuff_tabs[i])
  1950. m_pHuff_tabs[i] = (huff_tables *)alloc(sizeof(huff_tables));
  1951. make_huff_table(i, m_pHuff_tabs[i]);
  1952. }
  1953. }
  1954. // Determines the component order inside each MCU.
  1955. // Also calcs how many MCU's are on each row, etc.
  1956. void jpeg_decoder::calc_mcu_block_order()
  1957. {
  1958. int component_num, component_id;
  1959. int max_h_samp = 0, max_v_samp = 0;
  1960. for (component_id = 0; component_id < m_comps_in_frame; component_id++)
  1961. {
  1962. if (m_comp_h_samp[component_id] > max_h_samp)
  1963. max_h_samp = m_comp_h_samp[component_id];
  1964. if (m_comp_v_samp[component_id] > max_v_samp)
  1965. max_v_samp = m_comp_v_samp[component_id];
  1966. }
  1967. for (component_id = 0; component_id < m_comps_in_frame; component_id++)
  1968. {
  1969. m_comp_h_blocks[component_id] = ((((m_image_x_size * m_comp_h_samp[component_id]) + (max_h_samp - 1)) / max_h_samp) + 7) / 8;
  1970. m_comp_v_blocks[component_id] = ((((m_image_y_size * m_comp_v_samp[component_id]) + (max_v_samp - 1)) / max_v_samp) + 7) / 8;
  1971. }
  1972. if (m_comps_in_scan == 1)
  1973. {
  1974. m_mcus_per_row = m_comp_h_blocks[m_comp_list[0]];
  1975. m_mcus_per_col = m_comp_v_blocks[m_comp_list[0]];
  1976. }
  1977. else
  1978. {
  1979. m_mcus_per_row = (((m_image_x_size + 7) / 8) + (max_h_samp - 1)) / max_h_samp;
  1980. m_mcus_per_col = (((m_image_y_size + 7) / 8) + (max_v_samp - 1)) / max_v_samp;
  1981. }
  1982. if (m_comps_in_scan == 1)
  1983. {
  1984. m_mcu_org[0] = m_comp_list[0];
  1985. m_blocks_per_mcu = 1;
  1986. }
  1987. else
  1988. {
  1989. m_blocks_per_mcu = 0;
  1990. for (component_num = 0; component_num < m_comps_in_scan; component_num++)
  1991. {
  1992. int num_blocks;
  1993. component_id = m_comp_list[component_num];
  1994. num_blocks = m_comp_h_samp[component_id] * m_comp_v_samp[component_id];
  1995. while (num_blocks--)
  1996. m_mcu_org[m_blocks_per_mcu++] = component_id;
  1997. }
  1998. }
  1999. }
  2000. // Starts a new scan.
  2001. int jpeg_decoder::init_scan()
  2002. {
  2003. if (!locate_sos_marker())
  2004. return JPGD_FALSE;
  2005. calc_mcu_block_order();
  2006. check_huff_tables();
  2007. check_quant_tables();
  2008. memset(m_last_dc_val, 0, m_comps_in_frame * sizeof(uint));
  2009. m_eob_run = 0;
  2010. if (m_restart_interval)
  2011. {
  2012. m_restarts_left = m_restart_interval;
  2013. m_next_restart_num = 0;
  2014. }
  2015. fix_in_buffer();
  2016. return JPGD_TRUE;
  2017. }
  2018. // Starts a frame. Determines if the number of components or sampling factors
  2019. // are supported.
  2020. void jpeg_decoder::init_frame()
  2021. {
  2022. int i;
  2023. if (m_comps_in_frame == 1)
  2024. {
  2025. if ((m_comp_h_samp[0] != 1) || (m_comp_v_samp[0] != 1))
  2026. stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
  2027. m_scan_type = JPGD_GRAYSCALE;
  2028. m_max_blocks_per_mcu = 1;
  2029. m_max_mcu_x_size = 8;
  2030. m_max_mcu_y_size = 8;
  2031. }
  2032. else if (m_comps_in_frame == 3)
  2033. {
  2034. if ( ((m_comp_h_samp[1] != 1) || (m_comp_v_samp[1] != 1)) ||
  2035. ((m_comp_h_samp[2] != 1) || (m_comp_v_samp[2] != 1)) )
  2036. stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
  2037. if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 1))
  2038. {
  2039. m_scan_type = JPGD_YH1V1;
  2040. m_max_blocks_per_mcu = 3;
  2041. m_max_mcu_x_size = 8;
  2042. m_max_mcu_y_size = 8;
  2043. }
  2044. else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 1))
  2045. {
  2046. m_scan_type = JPGD_YH2V1;
  2047. m_max_blocks_per_mcu = 4;
  2048. m_max_mcu_x_size = 16;
  2049. m_max_mcu_y_size = 8;
  2050. }
  2051. else if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 2))
  2052. {
  2053. m_scan_type = JPGD_YH1V2;
  2054. m_max_blocks_per_mcu = 4;
  2055. m_max_mcu_x_size = 8;
  2056. m_max_mcu_y_size = 16;
  2057. }
  2058. else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 2))
  2059. {
  2060. m_scan_type = JPGD_YH2V2;
  2061. m_max_blocks_per_mcu = 6;
  2062. m_max_mcu_x_size = 16;
  2063. m_max_mcu_y_size = 16;
  2064. }
  2065. else
  2066. stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
  2067. }
  2068. else
  2069. stop_decoding(JPGD_UNSUPPORTED_COLORSPACE);
  2070. m_max_mcus_per_row = (m_image_x_size + (m_max_mcu_x_size - 1)) / m_max_mcu_x_size;
  2071. m_max_mcus_per_col = (m_image_y_size + (m_max_mcu_y_size - 1)) / m_max_mcu_y_size;
  2072. // These values are for the *destination* pixels: after conversion.
  2073. if (m_scan_type == JPGD_GRAYSCALE)
  2074. m_dest_bytes_per_pixel = 1;
  2075. else
  2076. m_dest_bytes_per_pixel = 4;
  2077. m_dest_bytes_per_scan_line = ((m_image_x_size + 15) & 0xFFF0) * m_dest_bytes_per_pixel;
  2078. m_real_dest_bytes_per_scan_line = (m_image_x_size * m_dest_bytes_per_pixel);
  2079. // Initialize two scan line buffers.
  2080. m_pScan_line_0 = (uint8 *)alloc(m_dest_bytes_per_scan_line, true);
  2081. if ((m_scan_type == JPGD_YH1V2) || (m_scan_type == JPGD_YH2V2))
  2082. m_pScan_line_1 = (uint8 *)alloc(m_dest_bytes_per_scan_line, true);
  2083. m_max_blocks_per_row = m_max_mcus_per_row * m_max_blocks_per_mcu;
  2084. // Should never happen
  2085. if (m_max_blocks_per_row > JPGD_MAX_BLOCKS_PER_ROW)
  2086. stop_decoding(JPGD_ASSERTION_ERROR);
  2087. // Allocate the coefficient buffer, enough for one MCU
  2088. m_pMCU_coefficients = (jpgd_block_t*)alloc(m_max_blocks_per_mcu * 64 * sizeof(jpgd_block_t));
  2089. for (i = 0; i < m_max_blocks_per_mcu; i++)
  2090. m_mcu_block_max_zag[i] = 64;
  2091. m_expanded_blocks_per_component = m_comp_h_samp[0] * m_comp_v_samp[0];
  2092. m_expanded_blocks_per_mcu = m_expanded_blocks_per_component * m_comps_in_frame;
  2093. m_expanded_blocks_per_row = m_max_mcus_per_row * m_expanded_blocks_per_mcu;
  2094. // Freq. domain chroma upsampling is only supported for H2V2 subsampling factor (the most common one I've seen).
  2095. m_freq_domain_chroma_upsample = false;
  2096. #if JPGD_SUPPORT_FREQ_DOMAIN_UPSAMPLING
  2097. m_freq_domain_chroma_upsample = (m_expanded_blocks_per_mcu == 4*3);
  2098. #endif
  2099. if (m_freq_domain_chroma_upsample)
  2100. m_pSample_buf = (uint8 *)alloc(m_expanded_blocks_per_row * 64);
  2101. else
  2102. m_pSample_buf = (uint8 *)alloc(m_max_blocks_per_row * 64);
  2103. m_total_lines_left = m_image_y_size;
  2104. m_mcu_lines_left = 0;
  2105. create_look_ups();
  2106. }
  2107. // The coeff_buf series of methods originally stored the coefficients
  2108. // into a "virtual" file which was located in EMS, XMS, or a disk file. A cache
  2109. // was used to make this process more efficient. Now, we can store the entire
  2110. // thing in RAM.
  2111. jpeg_decoder::coeff_buf* jpeg_decoder::coeff_buf_open(int block_num_x, int block_num_y, int block_len_x, int block_len_y)
  2112. {
  2113. coeff_buf* cb = (coeff_buf*)alloc(sizeof(coeff_buf));
  2114. cb->block_num_x = block_num_x;
  2115. cb->block_num_y = block_num_y;
  2116. cb->block_len_x = block_len_x;
  2117. cb->block_len_y = block_len_y;
  2118. cb->block_size = (block_len_x * block_len_y) * sizeof(jpgd_block_t);
  2119. cb->pData = (uint8 *)alloc(cb->block_size * block_num_x * block_num_y, true);
  2120. return cb;
  2121. }
  2122. inline jpgd_block_t *jpeg_decoder::coeff_buf_getp(coeff_buf *cb, int block_x, int block_y)
  2123. {
  2124. JPGD_ASSERT((block_x < cb->block_num_x) && (block_y < cb->block_num_y));
  2125. return (jpgd_block_t *)(cb->pData + block_x * cb->block_size + block_y * (cb->block_size * cb->block_num_x));
  2126. }
  2127. // The following methods decode the various types of m_blocks encountered
  2128. // in progressively encoded images.
  2129. void jpeg_decoder::decode_block_dc_first(jpeg_decoder *pD, int component_id, int block_x, int block_y)
  2130. {
  2131. int s, r;
  2132. jpgd_block_t *p = pD->coeff_buf_getp(pD->m_dc_coeffs[component_id], block_x, block_y);
  2133. if ((s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_dc_tab[component_id]])) != 0)
  2134. {
  2135. r = pD->get_bits_no_markers(s);
  2136. s = JPGD_HUFF_EXTEND(r, s);
  2137. }
  2138. pD->m_last_dc_val[component_id] = (s += pD->m_last_dc_val[component_id]);
  2139. p[0] = static_cast<jpgd_block_t>(s << pD->m_successive_low);
  2140. }
  2141. void jpeg_decoder::decode_block_dc_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y)
  2142. {
  2143. if (pD->get_bits_no_markers(1))
  2144. {
  2145. jpgd_block_t *p = pD->coeff_buf_getp(pD->m_dc_coeffs[component_id], block_x, block_y);
  2146. p[0] |= (1 << pD->m_successive_low);
  2147. }
  2148. }
  2149. void jpeg_decoder::decode_block_ac_first(jpeg_decoder *pD, int component_id, int block_x, int block_y)
  2150. {
  2151. int k, s, r;
  2152. if (pD->m_eob_run)
  2153. {
  2154. pD->m_eob_run--;
  2155. return;
  2156. }
  2157. jpgd_block_t *p = pD->coeff_buf_getp(pD->m_ac_coeffs[component_id], block_x, block_y);
  2158. for (k = pD->m_spectral_start; k <= pD->m_spectral_end; k++)
  2159. {
  2160. s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_ac_tab[component_id]]);
  2161. r = s >> 4;
  2162. s &= 15;
  2163. if (s)
  2164. {
  2165. if ((k += r) > 63)
  2166. pD->stop_decoding(JPGD_DECODE_ERROR);
  2167. r = pD->get_bits_no_markers(s);
  2168. s = JPGD_HUFF_EXTEND(r, s);
  2169. p[g_ZAG[k]] = static_cast<jpgd_block_t>(s << pD->m_successive_low);
  2170. }
  2171. else
  2172. {
  2173. if (r == 15)
  2174. {
  2175. if ((k += 15) > 63)
  2176. pD->stop_decoding(JPGD_DECODE_ERROR);
  2177. }
  2178. else
  2179. {
  2180. pD->m_eob_run = 1 << r;
  2181. if (r)
  2182. pD->m_eob_run += pD->get_bits_no_markers(r);
  2183. pD->m_eob_run--;
  2184. break;
  2185. }
  2186. }
  2187. }
  2188. }
  2189. void jpeg_decoder::decode_block_ac_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y)
  2190. {
  2191. int s, k, r;
  2192. int p1 = 1 << pD->m_successive_low;
  2193. int m1 = (-1) << pD->m_successive_low;
  2194. jpgd_block_t *p = pD->coeff_buf_getp(pD->m_ac_coeffs[component_id], block_x, block_y);
  2195. JPGD_ASSERT(pD->m_spectral_end <= 63);
  2196. k = pD->m_spectral_start;
  2197. if (pD->m_eob_run == 0)
  2198. {
  2199. for ( ; k <= pD->m_spectral_end; k++)
  2200. {
  2201. s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_ac_tab[component_id]]);
  2202. r = s >> 4;
  2203. s &= 15;
  2204. if (s)
  2205. {
  2206. if (s != 1)
  2207. pD->stop_decoding(JPGD_DECODE_ERROR);
  2208. if (pD->get_bits_no_markers(1))
  2209. s = p1;
  2210. else
  2211. s = m1;
  2212. }
  2213. else
  2214. {
  2215. if (r != 15)
  2216. {
  2217. pD->m_eob_run = 1 << r;
  2218. if (r)
  2219. pD->m_eob_run += pD->get_bits_no_markers(r);
  2220. break;
  2221. }
  2222. }
  2223. do
  2224. {
  2225. jpgd_block_t *this_coef = p + g_ZAG[k & 63];
  2226. if (*this_coef != 0)
  2227. {
  2228. if (pD->get_bits_no_markers(1))
  2229. {
  2230. if ((*this_coef & p1) == 0)
  2231. {
  2232. if (*this_coef >= 0)
  2233. *this_coef = static_cast<jpgd_block_t>(*this_coef + p1);
  2234. else
  2235. *this_coef = static_cast<jpgd_block_t>(*this_coef + m1);
  2236. }
  2237. }
  2238. }
  2239. else
  2240. {
  2241. if (--r < 0)
  2242. break;
  2243. }
  2244. k++;
  2245. } while (k <= pD->m_spectral_end);
  2246. if ((s) && (k < 64))
  2247. {
  2248. p[g_ZAG[k]] = static_cast<jpgd_block_t>(s);
  2249. }
  2250. }
  2251. }
  2252. if (pD->m_eob_run > 0)
  2253. {
  2254. for ( ; k <= pD->m_spectral_end; k++)
  2255. {
  2256. jpgd_block_t *this_coef = p + g_ZAG[k & 63]; // logical AND to shut up static code analysis
  2257. if (*this_coef != 0)
  2258. {
  2259. if (pD->get_bits_no_markers(1))
  2260. {
  2261. if ((*this_coef & p1) == 0)
  2262. {
  2263. if (*this_coef >= 0)
  2264. *this_coef = static_cast<jpgd_block_t>(*this_coef + p1);
  2265. else
  2266. *this_coef = static_cast<jpgd_block_t>(*this_coef + m1);
  2267. }
  2268. }
  2269. }
  2270. }
  2271. pD->m_eob_run--;
  2272. }
  2273. }
  2274. // Decode a scan in a progressively encoded image.
  2275. void jpeg_decoder::decode_scan(pDecode_block_func decode_block_func)
  2276. {
  2277. int mcu_row, mcu_col, mcu_block;
  2278. int block_x_mcu[JPGD_MAX_COMPONENTS], m_block_y_mcu[JPGD_MAX_COMPONENTS];
  2279. memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
  2280. for (mcu_col = 0; mcu_col < m_mcus_per_col; mcu_col++)
  2281. {
  2282. int component_num, component_id;
  2283. memset(block_x_mcu, 0, sizeof(block_x_mcu));
  2284. for (mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++)
  2285. {
  2286. int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
  2287. if ((m_restart_interval) && (m_restarts_left == 0))
  2288. process_restart();
  2289. for (mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++)
  2290. {
  2291. component_id = m_mcu_org[mcu_block];
  2292. decode_block_func(this, component_id, block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
  2293. if (m_comps_in_scan == 1)
  2294. block_x_mcu[component_id]++;
  2295. else
  2296. {
  2297. if (++block_x_mcu_ofs == m_comp_h_samp[component_id])
  2298. {
  2299. block_x_mcu_ofs = 0;
  2300. if (++block_y_mcu_ofs == m_comp_v_samp[component_id])
  2301. {
  2302. block_y_mcu_ofs = 0;
  2303. block_x_mcu[component_id] += m_comp_h_samp[component_id];
  2304. }
  2305. }
  2306. }
  2307. }
  2308. m_restarts_left--;
  2309. }
  2310. if (m_comps_in_scan == 1)
  2311. m_block_y_mcu[m_comp_list[0]]++;
  2312. else
  2313. {
  2314. for (component_num = 0; component_num < m_comps_in_scan; component_num++)
  2315. {
  2316. component_id = m_comp_list[component_num];
  2317. m_block_y_mcu[component_id] += m_comp_v_samp[component_id];
  2318. }
  2319. }
  2320. }
  2321. }
  2322. // Decode a progressively encoded image.
  2323. void jpeg_decoder::init_progressive()
  2324. {
  2325. int i;
  2326. if (m_comps_in_frame == 4)
  2327. stop_decoding(JPGD_UNSUPPORTED_COLORSPACE);
  2328. // Allocate the coefficient buffers.
  2329. for (i = 0; i < m_comps_in_frame; i++)
  2330. {
  2331. m_dc_coeffs[i] = coeff_buf_open(m_max_mcus_per_row * m_comp_h_samp[i], m_max_mcus_per_col * m_comp_v_samp[i], 1, 1);
  2332. m_ac_coeffs[i] = coeff_buf_open(m_max_mcus_per_row * m_comp_h_samp[i], m_max_mcus_per_col * m_comp_v_samp[i], 8, 8);
  2333. }
  2334. for ( ; ; )
  2335. {
  2336. int dc_only_scan, refinement_scan;
  2337. pDecode_block_func decode_block_func;
  2338. if (!init_scan())
  2339. break;
  2340. dc_only_scan = (m_spectral_start == 0);
  2341. refinement_scan = (m_successive_high != 0);
  2342. if ((m_spectral_start > m_spectral_end) || (m_spectral_end > 63))
  2343. stop_decoding(JPGD_BAD_SOS_SPECTRAL);
  2344. if (dc_only_scan)
  2345. {
  2346. if (m_spectral_end)
  2347. stop_decoding(JPGD_BAD_SOS_SPECTRAL);
  2348. }
  2349. else if (m_comps_in_scan != 1) /* AC scans can only contain one component */
  2350. stop_decoding(JPGD_BAD_SOS_SPECTRAL);
  2351. if ((refinement_scan) && (m_successive_low != m_successive_high - 1))
  2352. stop_decoding(JPGD_BAD_SOS_SUCCESSIVE);
  2353. if (dc_only_scan)
  2354. {
  2355. if (refinement_scan)
  2356. decode_block_func = decode_block_dc_refine;
  2357. else
  2358. decode_block_func = decode_block_dc_first;
  2359. }
  2360. else
  2361. {
  2362. if (refinement_scan)
  2363. decode_block_func = decode_block_ac_refine;
  2364. else
  2365. decode_block_func = decode_block_ac_first;
  2366. }
  2367. decode_scan(decode_block_func);
  2368. m_bits_left = 16;
  2369. get_bits(16);
  2370. get_bits(16);
  2371. }
  2372. m_comps_in_scan = m_comps_in_frame;
  2373. for (i = 0; i < m_comps_in_frame; i++)
  2374. m_comp_list[i] = i;
  2375. calc_mcu_block_order();
  2376. }
  2377. void jpeg_decoder::init_sequential()
  2378. {
  2379. if (!init_scan())
  2380. stop_decoding(JPGD_UNEXPECTED_MARKER);
  2381. }
  2382. void jpeg_decoder::decode_start()
  2383. {
  2384. init_frame();
  2385. if (m_progressive_flag)
  2386. init_progressive();
  2387. else
  2388. init_sequential();
  2389. }
  2390. void jpeg_decoder::decode_init(jpeg_decoder_stream *pStream)
  2391. {
  2392. init(pStream);
  2393. locate_sof_marker();
  2394. }
  2395. jpeg_decoder::jpeg_decoder(jpeg_decoder_stream *pStream)
  2396. {
  2397. if (setjmp(m_jmp_state))
  2398. return;
  2399. decode_init(pStream);
  2400. }
  2401. int jpeg_decoder::begin_decoding()
  2402. {
  2403. if (m_ready_flag)
  2404. return JPGD_SUCCESS;
  2405. if (m_error_code)
  2406. return JPGD_FAILED;
  2407. if (setjmp(m_jmp_state))
  2408. return JPGD_FAILED;
  2409. decode_start();
  2410. m_ready_flag = true;
  2411. return JPGD_SUCCESS;
  2412. }
  2413. jpeg_decoder::~jpeg_decoder()
  2414. {
  2415. free_all_blocks();
  2416. }
  2417. jpeg_decoder_file_stream::jpeg_decoder_file_stream()
  2418. {
  2419. m_pFile = NULL;
  2420. m_eof_flag = false;
  2421. m_error_flag = false;
  2422. }
  2423. void jpeg_decoder_file_stream::close()
  2424. {
  2425. if (m_pFile)
  2426. {
  2427. fclose(m_pFile);
  2428. m_pFile = NULL;
  2429. }
  2430. m_eof_flag = false;
  2431. m_error_flag = false;
  2432. }
  2433. jpeg_decoder_file_stream::~jpeg_decoder_file_stream()
  2434. {
  2435. close();
  2436. }
  2437. bool jpeg_decoder_file_stream::open(const char *Pfilename)
  2438. {
  2439. close();
  2440. m_eof_flag = false;
  2441. m_error_flag = false;
  2442. #if defined(_MSC_VER)
  2443. m_pFile = NULL;
  2444. fopen_s(&m_pFile, Pfilename, "rb");
  2445. #else
  2446. m_pFile = fopen(Pfilename, "rb");
  2447. #endif
  2448. return m_pFile != NULL;
  2449. }
  2450. int jpeg_decoder_file_stream::read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag)
  2451. {
  2452. if (!m_pFile)
  2453. return -1;
  2454. if (m_eof_flag)
  2455. {
  2456. *pEOF_flag = true;
  2457. return 0;
  2458. }
  2459. if (m_error_flag)
  2460. return -1;
  2461. int bytes_read = static_cast<int>(fread(pBuf, 1, max_bytes_to_read, m_pFile));
  2462. if (bytes_read < max_bytes_to_read)
  2463. {
  2464. if (ferror(m_pFile))
  2465. {
  2466. m_error_flag = true;
  2467. return -1;
  2468. }
  2469. m_eof_flag = true;
  2470. *pEOF_flag = true;
  2471. }
  2472. return bytes_read;
  2473. }
  2474. bool jpeg_decoder_mem_stream::open(const uint8 *pSrc_data, uint size)
  2475. {
  2476. close();
  2477. m_pSrc_data = pSrc_data;
  2478. m_ofs = 0;
  2479. m_size = size;
  2480. return true;
  2481. }
  2482. int jpeg_decoder_mem_stream::read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag)
  2483. {
  2484. *pEOF_flag = false;
  2485. if (!m_pSrc_data)
  2486. return -1;
  2487. uint bytes_remaining = m_size - m_ofs;
  2488. if ((uint)max_bytes_to_read > bytes_remaining)
  2489. {
  2490. max_bytes_to_read = bytes_remaining;
  2491. *pEOF_flag = true;
  2492. }
  2493. memcpy(pBuf, m_pSrc_data + m_ofs, max_bytes_to_read);
  2494. m_ofs += max_bytes_to_read;
  2495. return max_bytes_to_read;
  2496. }
  2497. unsigned char *decompress_jpeg_image_from_stream(jpeg_decoder_stream *pStream, int *width, int *height, int *actual_comps, int req_comps)
  2498. {
  2499. if (!actual_comps)
  2500. return NULL;
  2501. *actual_comps = 0;
  2502. if ((!pStream) || (!width) || (!height) || (!req_comps))
  2503. return NULL;
  2504. if ((req_comps != 1) && (req_comps != 3) && (req_comps != 4))
  2505. return NULL;
  2506. jpeg_decoder decoder(pStream);
  2507. if (decoder.get_error_code() != JPGD_SUCCESS)
  2508. return NULL;
  2509. const int image_width = decoder.get_width(), image_height = decoder.get_height();
  2510. *width = image_width;
  2511. *height = image_height;
  2512. *actual_comps = decoder.get_num_components();
  2513. if (decoder.begin_decoding() != JPGD_SUCCESS)
  2514. return NULL;
  2515. const int dst_bpl = image_width * req_comps;
  2516. uint8 *pImage_data = (uint8*)jpgd_malloc(dst_bpl * image_height);
  2517. if (!pImage_data)
  2518. return NULL;
  2519. for (int y = 0; y < image_height; y++)
  2520. {
  2521. const uint8* pScan_line;
  2522. uint scan_line_len;
  2523. if (decoder.decode((const void**)&pScan_line, &scan_line_len) != JPGD_SUCCESS)
  2524. {
  2525. jpgd_free(pImage_data);
  2526. return NULL;
  2527. }
  2528. uint8 *pDst = pImage_data + y * dst_bpl;
  2529. if (((req_comps == 1) && (decoder.get_num_components() == 1)) || ((req_comps == 4) && (decoder.get_num_components() == 3)))
  2530. memcpy(pDst, pScan_line, dst_bpl);
  2531. else if (decoder.get_num_components() == 1)
  2532. {
  2533. if (req_comps == 3)
  2534. {
  2535. for (int x = 0; x < image_width; x++)
  2536. {
  2537. uint8 luma = pScan_line[x];
  2538. pDst[0] = luma;
  2539. pDst[1] = luma;
  2540. pDst[2] = luma;
  2541. pDst += 3;
  2542. }
  2543. }
  2544. else
  2545. {
  2546. for (int x = 0; x < image_width; x++)
  2547. {
  2548. uint8 luma = pScan_line[x];
  2549. pDst[0] = luma;
  2550. pDst[1] = luma;
  2551. pDst[2] = luma;
  2552. pDst[3] = 255;
  2553. pDst += 4;
  2554. }
  2555. }
  2556. }
  2557. else if (decoder.get_num_components() == 3)
  2558. {
  2559. if (req_comps == 1)
  2560. {
  2561. const int YR = 19595, YG = 38470, YB = 7471;
  2562. for (int x = 0; x < image_width; x++)
  2563. {
  2564. int r = pScan_line[x*4+0];
  2565. int g = pScan_line[x*4+1];
  2566. int b = pScan_line[x*4+2];
  2567. *pDst++ = static_cast<uint8>((r * YR + g * YG + b * YB + 32768) >> 16);
  2568. }
  2569. }
  2570. else
  2571. {
  2572. for (int x = 0; x < image_width; x++)
  2573. {
  2574. pDst[0] = pScan_line[x*4+0];
  2575. pDst[1] = pScan_line[x*4+1];
  2576. pDst[2] = pScan_line[x*4+2];
  2577. pDst += 3;
  2578. }
  2579. }
  2580. }
  2581. }
  2582. return pImage_data;
  2583. }
  2584. unsigned char *decompress_jpeg_image_from_memory(const unsigned char *pSrc_data, int src_data_size, int *width, int *height, int *actual_comps, int req_comps)
  2585. {
  2586. jpgd::jpeg_decoder_mem_stream mem_stream(pSrc_data, src_data_size);
  2587. return decompress_jpeg_image_from_stream(&mem_stream, width, height, actual_comps, req_comps);
  2588. }
  2589. unsigned char *decompress_jpeg_image_from_file(const char *pSrc_filename, int *width, int *height, int *actual_comps, int req_comps)
  2590. {
  2591. jpgd::jpeg_decoder_file_stream file_stream;
  2592. if (!file_stream.open(pSrc_filename))
  2593. return NULL;
  2594. return decompress_jpeg_image_from_stream(&file_stream, width, height, actual_comps, req_comps);
  2595. }
  2596. } // namespace jpgd