jpgd.cpp 80 KB

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