decoder.c 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597
  1. //
  2. // JPEG Decoder
  3. //
  4. // written by Larry Bank
  5. // bitbank@pobox.com
  6. // Arduino port started 8/2/2020
  7. // Original JPEG code written 26+ years ago :)
  8. // The goal of this code is to decode baseline JPEG images
  9. // using no more than 18K of RAM (if sent directly to an LCD display)
  10. //
  11. // Copyright 2020 BitBank Software, Inc. All Rights Reserved.
  12. // Licensed under the Apache License, Version 2.0 (the "License");
  13. // you may not use this file except in compliance with the License.
  14. // You may obtain a copy of the License at
  15. // http://www.apache.org/licenses/LICENSE-2.0
  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. //
  23. #include "ImC/decoder.h"
  24. #if defined(ARM_MATH_CM4) || defined(ARM_MATH_CM7)
  25. #define HAS_SIMD
  26. #endif
  27. // forward references
  28. static int JPEGInit(JPEGIMAGE *pJPEG);
  29. static int JPEGParseInfo(JPEGIMAGE *pPage, int bExtractThumb);
  30. static void JPEGGetMoreData(JPEGIMAGE *pPage);
  31. static int DecodeJPEG(JPEGIMAGE *pImage);
  32. static int32_t readRAM(JPEGFILE *pFile, uint8_t *pBuf, int32_t iLen);
  33. static int32_t seekMem(JPEGFILE *pFile, int32_t iPosition);
  34. //static int32_t readFile(JPEGFILE *pFile, uint8_t *pBuf, int32_t iLen);
  35. //static int32_t seekFile(JPEGFILE *pFile, int32_t iPosition);
  36. //static void closeFile(void *handle);
  37. static void JPEGDither(JPEGIMAGE *pJPEG, int iWidth, int iHeight);
  38. /* JPEG tables */
  39. // zigzag ordering of DCT coefficients
  40. static const unsigned char cZigZag[64] = {0,1,5,6,14,15,27,28,
  41. 2,4,7,13,16,26,29,42,
  42. 3,8,12,17,25,30,41,43,
  43. 9,11,18,24,31,40,44,53,
  44. 10,19,23,32,39,45,52,54,
  45. 20,22,33,38,46,51,55,60,
  46. 21,34,37,47,50,56,59,61,
  47. 35,36,48,49,57,58,62,63};
  48. // un-zigzag ordering
  49. static const unsigned char cZigZag2[64] = {0,1,8,16,9,2,3,10,
  50. 17,24,32,25,18,11,4,5,
  51. 12,19,26,33,40,48,41,34,
  52. 27,20,13,6,7,14,21,28,
  53. 35,42,49,56,57,50,43,36,
  54. 29,22,15,23,30,37,44,51,
  55. 58,59,52,45,38,31,39,46,
  56. 53,60,61,54,47,55,62,63};
  57. // For AA&N IDCT method, multipliers are equal to quantization
  58. // coefficients scaled by scalefactor[row]*scalefactor[col], where
  59. // scalefactor[0] = 1
  60. // scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  61. // For integer operation, the multiplier table is to be scaled by
  62. // IFAST_SCALE_BITS.
  63. static const int iScaleBits[64] = {16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  64. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  65. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  66. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  67. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  68. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  69. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  70. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247};
  71. uint8_t (*dst_image)[120][160][3];
  72. float scale;
  73. int32_t zero_point;
  74. /////////////////////////////////////BILINEAR INTERPOLATION////////////////////////////////////////////////
  75. //#define floor(x) (int)x
  76. //#define ceil(x) (int)(x+0.995)
  77. //void resize_image(uint32_t* src, uint32_t* dst, int image_width, int image_height, int new_width, int new_height){
  78. //
  79. //
  80. //
  81. // float ratio_x = (float) (image_width - 1) / (new_width -1);
  82. // float ratio_y = (float) (image_height -1) / (new_height-1);
  83. //
  84. // for(int i = 0; i < new_height; i++){
  85. // for(int j = 0; j < new_width; j++){
  86. // int xl = floor(ratio_x * j);
  87. // int yl = floor(ratio_y * i);
  88. // int xh = ceil(ratio_x * j);
  89. // int yh = ceil(ratio_y * i);
  90. //
  91. // float x_weight = (ratio_x * j) - xl;
  92. // float y_weight = (ratio_y * i) - yl;
  93. //
  94. // uint32_t a = src[yl * image_width + xl];
  95. // uint32_t b = src[yl * image_width + xh];
  96. // uint32_t c = src[yh * image_width + xl];
  97. // uint32_t d = src[yh * image_width + xh];
  98. //
  99. // uint32_t pixel = a * (1 - x_weight) * (1 - y_weight) +
  100. // b * x_weight * ( 1- y_weight) +
  101. // c * y_weight * ( 1 - x_weight) +
  102. // d * x_weight * y_weight;
  103. //
  104. //
  105. // dst[i * new_width + j] = pixel;
  106. //
  107. // }
  108. // }
  109. //
  110. //}
  111. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  112. int getImage(JPEGDRAW *pdraw){
  113. // printf("Being called for drawing\r\n");
  114. // int iCount;
  115. // iCount = pdraw->iWidth * pdraw->iHeight;
  116. // printf("Depth of the pixel %d \r\n", pdraw->iBpp);
  117. for(int i = 0, ii=pdraw->y; i < pdraw->iHeight; i++,ii++){
  118. for(int j = 0; j < pdraw->iWidth; j++){
  119. uint32_t rgb = (uint32_t)pdraw->pPixels[i * pdraw->iWidth + j];
  120. uint32_t R5 = ((((rgb >> 11) & 0x1F) * 527) + 23) >> 6;
  121. uint32_t G6 = ((((rgb >> 5) & 0x3F) * 259) + 33) >> 6;
  122. uint32_t B5 = (((rgb & 0x1F) * 527) + 23) >> 6;
  123. (*dst_image)[ii][j][0] = (int8_t) ( (R5 / 255.0) / scale + zero_point);
  124. (*dst_image)[ii][j][1] = (int8_t) ( (G6 / 255.0) / scale + zero_point);
  125. (*dst_image)[ii][j][2] = (int8_t) ( (B5 / 255.0) / scale + zero_point);
  126. }
  127. }
  128. return 1;
  129. }
  130. //void decodeMyImage(uint8_t* _pdata, int iDataSize, int x, int y, int iOptions, JPEG_DRAW_CALLBACK *pfnDraw, int convert_to_grayscale){
  131. int32_t decodeMyImage(uint8_t* _pdata, int iDataSize, int x, int y, int iOptions, JPEG_DRAW_CALLBACK *pfnDraw, int convert_to_grayscale){
  132. // printf("[DEC] init ...\r\n");
  133. JPEGIMAGE _jpeg;
  134. memset(&_jpeg, 0, sizeof(JPEGIMAGE));
  135. _jpeg.ucMemType = JPEG_MEM_RAM;
  136. _jpeg.pfnRead = readRAM;
  137. _jpeg.pfnSeek = seekMem;
  138. _jpeg.pfnDraw = getImage;
  139. _jpeg.pfnOpen = NULL;
  140. _jpeg.pfnClose = NULL;
  141. _jpeg.JPEGFile.iSize = iDataSize;
  142. _jpeg.JPEGFile.pData = _pdata;
  143. _jpeg.iMaxMCUs = 1000; // set to an unnaturally high value to start
  144. if(JPEGInit(&_jpeg))
  145. printf("[DEC] OK init\r\n");
  146. else {
  147. printf("[DEC] FAILED init\r\n");
  148. return DEC_EINI;
  149. }
  150. // printf("Image size: %d x %d, orientation: %d, bpp: %d\r\n", _jpeg.iWidth,
  151. // _jpeg.iHeight, (int)_jpeg.ucOrientation, (int)_jpeg.ucBpp);
  152. _jpeg.iXOffset = x;
  153. _jpeg.iYOffset = y;
  154. _jpeg.iOptions = iOptions;
  155. //START_AGAIN:
  156. if(DecodeJPEG(&_jpeg)){
  157. printf("[DEC] OK decode\r\n");
  158. return DEC_ENON;
  159. }
  160. else{
  161. printf("[DEC] FAILED decode\r\n");
  162. // goto START_AGAIN;
  163. return DEC_EDEC;
  164. }
  165. // printf("dump memory D:\\temp %p %p \r\n", dst_image, (uint8_t*)dst_image + (120*160*3));
  166. // resize_image(dst_image, resized_image, 160, 120, 50, 50);
  167. // printf("%p\r\n", resized_image);
  168. // printf("Done\r\n");
  169. }
  170. //
  171. // Range clip and shift for RGB565 output
  172. // input value is 0 to 255, then another 256 for overflow to FF, then 512 more for negative values wrapping around
  173. // Trims a few instructions off the final output stage
  174. //
  175. static const uint8_t ucRangeTable[] = {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
  176. 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
  177. 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
  178. 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
  179. 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
  180. 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
  181. 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
  182. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff,
  183. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  184. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  185. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  186. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  187. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  188. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  189. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  190. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  191. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  192. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  193. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  194. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  195. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  196. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  197. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  198. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  199. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  200. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  201. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  202. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  203. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  204. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  205. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  206. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  207. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  208. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  209. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  210. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  211. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  212. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  213. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  214. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  215. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  216. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  217. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  218. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  219. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  220. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  221. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  222. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  223. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  224. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  225. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  226. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  227. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  228. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  229. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  230. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  231. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
  232. 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
  233. 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
  234. 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
  235. 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
  236. 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
  237. 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
  238. 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f};
  239. //
  240. // Convert 8-bit grayscale into RGB565
  241. //
  242. static const uint16_t usGrayTo565[] = {0x0000,0x0000,0x0000,0x0000,0x0020,0x0020,0x0020,0x0020, // 0
  243. 0x0841,0x0841,0x0841,0x0841,0x0861,0x0861,0x0861,0x0861,
  244. 0x1082,0x1082,0x1082,0x1082,0x10a2,0x10a2,0x10a2,0x10a2,
  245. 0x18c3,0x18c3,0x18c3,0x18c3,0x18e3,0x18e3,0x18e3,0x18e3,
  246. 0x2104,0x2104,0x2104,0x2104,0x2124,0x2124,0x2124,0x2124,
  247. 0x2945,0x2945,0x2945,0x2945,0x2965,0x2965,0x2965,0x2965,
  248. 0x3186,0x3186,0x3186,0x3186,0x31a6,0x31a6,0x31a6,0x31a6,
  249. 0x39c7,0x39c7,0x39c7,0x39c7,0x39e7,0x39e7,0x39e7,0x39e7,
  250. 0x4208,0x4208,0x4208,0x4208,0x4228,0x4228,0x4228,0x4228,
  251. 0x4a49,0x4a49,0x4a49,0x4a49,0x4a69,0x4a69,0x4a69,0x4a69,
  252. 0x528a,0x528a,0x528a,0x528a,0x52aa,0x52aa,0x52aa,0x52aa,
  253. 0x5acb,0x5acb,0x5acb,0x5acb,0x5aeb,0x5aeb,0x5aeb,0x5aeb,
  254. 0x630c,0x630c,0x630c,0x630c,0x632c,0x632c,0x632c,0x632c,
  255. 0x6b4d,0x6b4d,0x6b4d,0x6b4d,0x6b6d,0x6b6d,0x6b6d,0x6b6d,
  256. 0x738e,0x738e,0x738e,0x738e,0x73ae,0x73ae,0x73ae,0x73ae,
  257. 0x7bcf,0x7bcf,0x7bcf,0x7bcf,0x7bef,0x7bef,0x7bef,0x7bef,
  258. 0x8410,0x8410,0x8410,0x8410,0x8430,0x8430,0x8430,0x8430,
  259. 0x8c51,0x8c51,0x8c51,0x8c51,0x8c71,0x8c71,0x8c71,0x8c71,
  260. 0x9492,0x9492,0x9492,0x9492,0x94b2,0x94b2,0x94b2,0x94b2,
  261. 0x9cd3,0x9cd3,0x9cd3,0x9cd3,0x9cf3,0x9cf3,0x9cf3,0x9cf3,
  262. 0xa514,0xa514,0xa514,0xa514,0xa534,0xa534,0xa534,0xa534,
  263. 0xad55,0xad55,0xad55,0xad55,0xad75,0xad75,0xad75,0xad75,
  264. 0xb596,0xb596,0xb596,0xb596,0xb5b6,0xb5b6,0xb5b6,0xb5b6,
  265. 0xbdd7,0xbdd7,0xbdd7,0xbdd7,0xbdf7,0xbdf7,0xbdf7,0xbdf7,
  266. 0xc618,0xc618,0xc618,0xc618,0xc638,0xc638,0xc638,0xc638,
  267. 0xce59,0xce59,0xce59,0xce59,0xce79,0xce79,0xce79,0xce79,
  268. 0xd69a,0xd69a,0xd69a,0xd69a,0xd6ba,0xd6ba,0xd6ba,0xd6ba,
  269. 0xdedb,0xdedb,0xdedb,0xdedb,0xdefb,0xdefb,0xdefb,0xdefb,
  270. 0xe71c,0xe71c,0xe71c,0xe71c,0xe73c,0xe73c,0xe73c,0xe73c,
  271. 0xef5d,0xef5d,0xef5d,0xef5d,0xef7d,0xef7d,0xef7d,0xef7d,
  272. 0xf79e,0xf79e,0xf79e,0xf79e,0xf7be,0xf7be,0xf7be,0xf7be,
  273. 0xffdf,0xffdf,0xffdf,0xffdf,0xffff,0xffff,0xffff,0xffff};
  274. //
  275. // Clip and convert red value into 5-bits for RGB565
  276. //
  277. static const uint16_t usRangeTableR[] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // 0
  278. 0x0800,0x0800,0x0800,0x0800,0x0800,0x0800,0x0800,0x0800,
  279. 0x1000,0x1000,0x1000,0x1000,0x1000,0x1000,0x1000,0x1000,
  280. 0x1800,0x1800,0x1800,0x1800,0x1800,0x1800,0x1800,0x1800,
  281. 0x2000,0x2000,0x2000,0x2000,0x2000,0x2000,0x2000,0x2000,
  282. 0x2800,0x2800,0x2800,0x2800,0x2800,0x2800,0x2800,0x2800,
  283. 0x3000,0x3000,0x3000,0x3000,0x3000,0x3000,0x3000,0x3000,
  284. 0x3800,0x3800,0x3800,0x3800,0x3800,0x3800,0x3800,0x3800,
  285. 0x4000,0x4000,0x4000,0x4000,0x4000,0x4000,0x4000,0x4000,
  286. 0x4800,0x4800,0x4800,0x4800,0x4800,0x4800,0x4800,0x4800,
  287. 0x5000,0x5000,0x5000,0x5000,0x5000,0x5000,0x5000,0x5000,
  288. 0x5800,0x5800,0x5800,0x5800,0x5800,0x5800,0x5800,0x5800,
  289. 0x6000,0x6000,0x6000,0x6000,0x6000,0x6000,0x6000,0x6000,
  290. 0x6800,0x6800,0x6800,0x6800,0x6800,0x6800,0x6800,0x6800,
  291. 0x7000,0x7000,0x7000,0x7000,0x7000,0x7000,0x7000,0x7000,
  292. 0x7800,0x7800,0x7800,0x7800,0x7800,0x7800,0x7800,0x7800,
  293. 0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
  294. 0x8800,0x8800,0x8800,0x8800,0x8800,0x8800,0x8800,0x8800,
  295. 0x9000,0x9000,0x9000,0x9000,0x9000,0x9000,0x9000,0x9000,
  296. 0x9800,0x9800,0x9800,0x9800,0x9800,0x9800,0x9800,0x9800,
  297. 0xa000,0xa000,0xa000,0xa000,0xa000,0xa000,0xa000,0xa000,
  298. 0xa800,0xa800,0xa800,0xa800,0xa800,0xa800,0xa800,0xa800,
  299. 0xb000,0xb000,0xb000,0xb000,0xb000,0xb000,0xb000,0xb000,
  300. 0xb800,0xb800,0xb800,0xb800,0xb800,0xb800,0xb800,0xb800,
  301. 0xc000,0xc000,0xc000,0xc000,0xc000,0xc000,0xc000,0xc000,
  302. 0xc800,0xc800,0xc800,0xc800,0xc800,0xc800,0xc800,0xc800,
  303. 0xd000,0xd000,0xd000,0xd000,0xd000,0xd000,0xd000,0xd000,
  304. 0xd800,0xd800,0xd800,0xd800,0xd800,0xd800,0xd800,0xd800,
  305. 0xe000,0xe000,0xe000,0xe000,0xe000,0xe000,0xe000,0xe000,
  306. 0xe800,0xe800,0xe800,0xe800,0xe800,0xe800,0xe800,0xe800,
  307. 0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,0xf000,
  308. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  309. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800, // 256
  310. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  311. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  312. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  313. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  314. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  315. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  316. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  317. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  318. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  319. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  320. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  321. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  322. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  323. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  324. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  325. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  326. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  327. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  328. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  329. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  330. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  331. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  332. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  333. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  334. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  335. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  336. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  337. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  338. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  339. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  340. 0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,0xf800,
  341. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 512
  342. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  343. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  344. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  345. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  346. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  347. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  348. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  349. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  350. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  351. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  352. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  353. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  354. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  355. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  356. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  357. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 768
  358. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  359. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  360. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  361. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  362. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  363. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  364. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  365. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  366. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  367. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  368. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  369. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  370. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  371. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  372. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  373. //
  374. // Clip and convert green value into 5-bits for RGB565
  375. //
  376. static const uint16_t usRangeTableG[] = {0x0000,0x0000,0x0000,0x0000,0x0020,0x0020,0x0020,0x0020, // 0
  377. 0x0040,0x0040,0x0040,0x0040,0x0060,0x0060,0x0060,0x0060,
  378. 0x0080,0x0080,0x0080,0x0080,0x00a0,0x00a0,0x00a0,0x00a0,
  379. 0x00c0,0x00c0,0x00c0,0x00c0,0x00e0,0x00e0,0x00e0,0x00e0,
  380. 0x0100,0x0100,0x0100,0x0100,0x0120,0x0120,0x0120,0x0120,
  381. 0x0140,0x0140,0x0140,0x0140,0x0160,0x0160,0x0160,0x0160,
  382. 0x0180,0x0180,0x0180,0x0180,0x01a0,0x01a0,0x01a0,0x01a0,
  383. 0x01c0,0x01c0,0x01c0,0x01c0,0x01e0,0x01e0,0x01e0,0x01e0,
  384. 0x0200,0x0200,0x0200,0x0200,0x0220,0x0220,0x0220,0x0220,
  385. 0x0240,0x0240,0x0240,0x0240,0x0260,0x0260,0x0260,0x0260,
  386. 0x0280,0x0280,0x0280,0x0280,0x02a0,0x02a0,0x02a0,0x02a0,
  387. 0x02c0,0x02c0,0x02c0,0x02c0,0x02e0,0x02e0,0x02e0,0x02e0,
  388. 0x0300,0x0300,0x0300,0x0300,0x0320,0x0320,0x0320,0x0320,
  389. 0x0340,0x0340,0x0340,0x0340,0x0360,0x0360,0x0360,0x0360,
  390. 0x0380,0x0380,0x0380,0x0380,0x03a0,0x03a0,0x03a0,0x03a0,
  391. 0x03c0,0x03c0,0x03c0,0x03c0,0x03e0,0x03e0,0x03e0,0x03e0,
  392. 0x0400,0x0400,0x0400,0x0400,0x0420,0x0420,0x0420,0x0420,
  393. 0x0440,0x0440,0x0440,0x0440,0x0460,0x0460,0x0460,0x0460,
  394. 0x0480,0x0480,0x0480,0x0480,0x04a0,0x04a0,0x04a0,0x04a0,
  395. 0x04c0,0x04c0,0x04c0,0x04c0,0x04e0,0x04e0,0x04e0,0x04e0,
  396. 0x0500,0x0500,0x0500,0x0500,0x0520,0x0520,0x0520,0x0520,
  397. 0x0540,0x0540,0x0540,0x0540,0x0560,0x0560,0x0560,0x0560,
  398. 0x0580,0x0580,0x0580,0x0580,0x05a0,0x05a0,0x05a0,0x05a0,
  399. 0x05c0,0x05c0,0x05c0,0x05c0,0x05e0,0x05e0,0x05e0,0x05e0,
  400. 0x0600,0x0600,0x0600,0x0600,0x0620,0x0620,0x0620,0x0620,
  401. 0x0640,0x0640,0x0640,0x0640,0x0660,0x0660,0x0660,0x0660,
  402. 0x0680,0x0680,0x0680,0x0680,0x06a0,0x06a0,0x06a0,0x06a0,
  403. 0x06c0,0x06c0,0x06c0,0x06c0,0x06e0,0x06e0,0x06e0,0x06e0,
  404. 0x0700,0x0700,0x0700,0x0700,0x0720,0x0720,0x0720,0x0720,
  405. 0x0740,0x0740,0x0740,0x0740,0x0760,0x0760,0x0760,0x0760,
  406. 0x0780,0x0780,0x0780,0x0780,0x07a0,0x07a0,0x07a0,0x07a0,
  407. 0x07c0,0x07c0,0x07c0,0x07c0,0x07e0,0x07e0,0x07e0,0x07e0,
  408. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0, // 256
  409. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  410. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  411. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  412. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  413. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  414. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  415. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  416. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  417. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  418. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  419. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  420. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  421. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  422. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  423. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  424. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  425. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  426. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  427. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  428. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  429. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  430. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  431. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  432. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  433. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  434. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  435. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  436. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  437. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  438. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  439. 0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,0x07e0,
  440. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 512
  441. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  442. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  443. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  444. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  445. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  446. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  447. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  448. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  449. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  450. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  451. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  452. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  453. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  454. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  455. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  456. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 768
  457. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  458. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  459. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  460. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  461. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  462. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  463. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  464. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  465. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  466. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  467. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  468. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  469. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  470. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  471. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  472. //
  473. // Clip and convert blue value into 5-bits for RGB565
  474. //
  475. static const uint16_t usRangeTableB[] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // 0
  476. 0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,
  477. 0x0002,0x0002,0x0002,0x0002,0x0002,0x0002,0x0002,0x0002,
  478. 0x0003,0x0003,0x0003,0x0003,0x0003,0x0003,0x0003,0x0003,
  479. 0x0004,0x0004,0x0004,0x0004,0x0004,0x0004,0x0004,0x0004,
  480. 0x0005,0x0005,0x0005,0x0005,0x0005,0x0005,0x0005,0x0005,
  481. 0x0006,0x0006,0x0006,0x0006,0x0006,0x0006,0x0006,0x0006,
  482. 0x0007,0x0007,0x0007,0x0007,0x0007,0x0007,0x0007,0x0007,
  483. 0x0008,0x0008,0x0008,0x0008,0x0008,0x0008,0x0008,0x0008,
  484. 0x0009,0x0009,0x0009,0x0009,0x0009,0x0009,0x0009,0x0009,
  485. 0x000a,0x000a,0x000a,0x000a,0x000a,0x000a,0x000a,0x000a,
  486. 0x000b,0x000b,0x000b,0x000b,0x000b,0x000b,0x000b,0x000b,
  487. 0x000c,0x000c,0x000c,0x000c,0x000c,0x000c,0x000c,0x000c,
  488. 0x000d,0x000d,0x000d,0x000d,0x000d,0x000d,0x000d,0x000d,
  489. 0x000e,0x000e,0x000e,0x000e,0x000e,0x000e,0x000e,0x000e,
  490. 0x000f,0x000f,0x000f,0x000f,0x000f,0x000f,0x000f,0x000f,
  491. 0x0010,0x0010,0x0010,0x0010,0x0010,0x0010,0x0010,0x0010,
  492. 0x0011,0x0011,0x0011,0x0011,0x0011,0x0011,0x0011,0x0011,
  493. 0x0012,0x0012,0x0012,0x0012,0x0012,0x0012,0x0012,0x0012,
  494. 0x0013,0x0013,0x0013,0x0013,0x0013,0x0013,0x0013,0x0013,
  495. 0x0014,0x0014,0x0014,0x0014,0x0014,0x0014,0x0014,0x0014,
  496. 0x0015,0x0015,0x0015,0x0015,0x0015,0x0015,0x0015,0x0015,
  497. 0x0016,0x0016,0x0016,0x0016,0x0016,0x0016,0x0016,0x0016,
  498. 0x0017,0x0017,0x0017,0x0017,0x0017,0x0017,0x0017,0x0017,
  499. 0x0018,0x0018,0x0018,0x0018,0x0018,0x0018,0x0018,0x0018,
  500. 0x0019,0x0019,0x0019,0x0019,0x0019,0x0019,0x0019,0x0019,
  501. 0x001a,0x001a,0x001a,0x001a,0x001a,0x001a,0x001a,0x001a,
  502. 0x001b,0x001b,0x001b,0x001b,0x001b,0x001b,0x001b,0x001b,
  503. 0x001c,0x001c,0x001c,0x001c,0x001c,0x001c,0x001c,0x001c,
  504. 0x001d,0x001d,0x001d,0x001d,0x001d,0x001d,0x001d,0x001d,
  505. 0x001e,0x001e,0x001e,0x001e,0x001e,0x001e,0x001e,0x001e,
  506. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  507. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f, // 256
  508. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  509. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  510. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  511. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  512. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  513. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  514. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  515. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  516. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  517. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  518. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  519. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  520. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  521. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  522. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  523. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  524. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  525. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  526. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  527. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  528. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  529. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  530. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  531. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  532. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  533. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  534. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  535. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  536. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  537. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  538. 0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
  539. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 512
  540. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  541. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  542. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  543. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  544. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  545. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  546. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  547. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  548. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  549. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  550. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  551. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  552. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  553. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  554. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  555. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 768
  556. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  557. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  558. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  559. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  560. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  561. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  562. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  563. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  564. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  565. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  566. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  567. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  568. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  569. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  570. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  571. #if defined (__MACH__) || defined( __LINUX__ ) || defined( __MCUXPRESSO )
  572. //
  573. // API for C
  574. //
  575. //
  576. // Memory initialization
  577. //
  578. int JPEG_openRAM(JPEGIMAGE *pJPEG, uint8_t *pData, int iDataSize, JPEG_DRAW_CALLBACK *pfnDraw)
  579. {
  580. memset(pJPEG, 0, sizeof(JPEGIMAGE));
  581. pJPEG->ucMemType = JPEG_MEM_RAM;
  582. pJPEG->pfnRead = readRAM;
  583. pJPEG->pfnSeek = seekMem;
  584. pJPEG->pfnDraw = pfnDraw;
  585. pJPEG->pfnOpen = NULL;
  586. pJPEG->pfnClose = NULL;
  587. pJPEG->JPEGFile.iSize = iDataSize;
  588. pJPEG->JPEGFile.pData = pData;
  589. pJPEG->iMaxMCUs = 1000; // set to an unnaturally high value to start
  590. return JPEGInit(pJPEG);
  591. } /* JPEG_openRAM() */
  592. //
  593. // File initialization
  594. //
  595. int JPEG_openFile(JPEGIMAGE *pJPEG, const char *szFilename, JPEG_DRAW_CALLBACK *pfnDraw)
  596. {
  597. memset(pJPEG, 0, sizeof(JPEGIMAGE));
  598. pJPEG->ucMemType = JPEG_MEM_RAM;
  599. pJPEG->pfnRead = readFile;
  600. pJPEG->pfnSeek = seekFile;
  601. pJPEG->pfnDraw = pfnDraw;
  602. pJPEG->pfnOpen = NULL;
  603. pJPEG->pfnClose = closeFile;
  604. pJPEG->iMaxMCUs = 1000; // set to an unnaturally high value to start
  605. pJPEG->JPEGFile.fHandle = fopen(szFilename, "r+b");
  606. if (pJPEG->JPEGFile.fHandle == NULL)
  607. return 0;
  608. fseek((FILE *)pJPEG->JPEGFile.fHandle, 0, SEEK_END);
  609. pJPEG->JPEGFile.iSize = (int)ftell((FILE *)pJPEG->JPEGFile.fHandle);
  610. fseek((FILE *)pJPEG->JPEGFile.fHandle, 0, SEEK_SET);
  611. return JPEGInit(pJPEG);
  612. } /* JPEG_openFile() */
  613. int JPEG_getLastError(JPEGIMAGE *pJPEG)
  614. {
  615. return pJPEG->iError;
  616. } /* JPEG_getLastError() */
  617. int JPEG_getWidth(JPEGIMAGE *pJPEG)
  618. {
  619. return pJPEG->iWidth;
  620. } /* JPEG_getWidth() */
  621. int JPEG_getHeight(JPEGIMAGE *pJPEG)
  622. {
  623. return pJPEG->iHeight;
  624. } /* JPEG_getHeight() */
  625. int JPEG_getOrientation(JPEGIMAGE *pJPEG)
  626. {
  627. return (int)pJPEG->ucOrientation;
  628. } /* JPEG_getOrientation() */
  629. int JPEG_getBpp(JPEGIMAGE *pJPEG)
  630. {
  631. return (int)pJPEG->ucBpp;
  632. } /* JPEG_getBpp() */
  633. int JPEG_getSubSample(JPEGIMAGE *pJPEG)
  634. {
  635. return (int)pJPEG->ucSubSample;
  636. } /* JPEG_getSubSample() */
  637. int JPEG_hasThumb(JPEGIMAGE *pJPEG)
  638. {
  639. return (int)pJPEG->ucHasThumb;
  640. } /* JPEG_hasThumb() */
  641. int JPEG_getThumbWidth(JPEGIMAGE *pJPEG)
  642. {
  643. return pJPEG->iThumbWidth;
  644. } /* JPEG_getThumbWidth() */
  645. int JPEG_getThumbHeight(JPEGIMAGE *pJPEG)
  646. {
  647. return pJPEG->iThumbHeight;
  648. } /* JPEG_getThumbHeight() */
  649. void JPEG_setPixelType(JPEGIMAGE *pJPEG, int iType)
  650. {
  651. pJPEG->ucPixelType = (uint8_t)iType;
  652. } /* JPEG_setPixelType() */
  653. void JPEG_setMaxOutputSize(JPEGIMAGE *pJPEG, int iMaxMCUs)
  654. {
  655. if (iMaxMCUs < 1)
  656. iMaxMCUs = 1; // don't allow invalid value
  657. pJPEG->iMaxMCUs = iMaxMCUs;
  658. } /* JPEG_setMaxOutputSize() */
  659. int JPEG_decode(JPEGIMAGE *pJPEG, int x, int y, int iOptions)
  660. {
  661. pJPEG->iXOffset = x;
  662. pJPEG->iYOffset = y;
  663. pJPEG->iOptions = iOptions;
  664. return DecodeJPEG(pJPEG);
  665. } /* JPEG_decode() */
  666. int JPEG_decodeDither(JPEGIMAGE *pJPEG, uint8_t *pDither, int iOptions)
  667. {
  668. pJPEG->iOptions = iOptions;
  669. pJPEG->pDitherBuffer = pDither;
  670. return DecodeJPEG(pJPEG);
  671. } /* JPEG_decodeDither() */
  672. void JPEG_close(JPEGIMAGE *pJPEG)
  673. {
  674. if (pJPEG->pfnClose)
  675. (*pJPEG->pfnClose)(pJPEG->JPEGFile.fHandle);
  676. } /* JPEG_close() */
  677. #endif // !__cplusplus
  678. //
  679. // Helper functions for memory based images
  680. //
  681. static int32_t readRAM(JPEGFILE *pFile, uint8_t *pBuf, int32_t iLen)
  682. {
  683. int32_t iBytesRead;
  684. iBytesRead = iLen;
  685. if ((pFile->iSize - pFile->iPos) < iLen)
  686. iBytesRead = pFile->iSize - pFile->iPos;
  687. if (iBytesRead <= 0)
  688. return 0;
  689. memcpy(pBuf, &pFile->pData[pFile->iPos], iBytesRead);
  690. pFile->iPos += iBytesRead;
  691. return iBytesRead;
  692. } /* readRAM() */
  693. static int32_t seekMem(JPEGFILE *pFile, int32_t iPosition)
  694. {
  695. if (iPosition < 0) iPosition = 0;
  696. else if (iPosition >= pFile->iSize) iPosition = pFile->iSize-1;
  697. pFile->iPos = iPosition;
  698. return iPosition;
  699. } /* seekMem() */
  700. #if defined (__MACH__) || defined( __LINUX__ ) || defined( __MCUXPRESSO )
  701. static void closeFile(void *handle)
  702. {
  703. fclose((FILE *)handle);
  704. } /* closeFile() */
  705. static int32_t seekFile(JPEGFILE *pFile, int32_t iPosition)
  706. {
  707. if (iPosition < 0) iPosition = 0;
  708. else if (iPosition >= pFile->iSize) iPosition = pFile->iSize-1;
  709. pFile->iPos = iPosition;
  710. fseek((FILE *)pFile->fHandle, iPosition, SEEK_SET);
  711. return iPosition;
  712. } /* seekFile() */
  713. static int32_t readFile(JPEGFILE *pFile, uint8_t *pBuf, int32_t iLen)
  714. {
  715. int32_t iBytesRead;
  716. iBytesRead = iLen;
  717. if ((pFile->iSize - pFile->iPos) < iLen)
  718. iBytesRead = pFile->iSize - pFile->iPos;
  719. if (iBytesRead <= 0)
  720. return 0;
  721. iBytesRead = (int)fread(pBuf, 1, iBytesRead, (FILE *)pFile->fHandle);
  722. pFile->iPos += iBytesRead;
  723. return iBytesRead;
  724. } /* readFile() */
  725. #endif // __LINUX__
  726. //
  727. // The following functions are written in plain C and have no
  728. // 3rd party dependencies, not even the C runtime library
  729. //
  730. //
  731. // Initialize a JPEG file and callback access from a file on SD or memory
  732. // returns 1 for success, 0 for failure
  733. // Fills in the basic image info fields of the JPEGIMAGE structure
  734. //
  735. static int JPEGInit(JPEGIMAGE *pJPEG)
  736. {
  737. return JPEGParseInfo(pJPEG, 0); // gather info for image
  738. } /* JPEGInit() */
  739. //
  740. // Unpack the Huffman tables
  741. //
  742. static int JPEGGetHuffTables(uint8_t *pBuf, int iLen, JPEGIMAGE *pJPEG)
  743. {
  744. int i, j, iOffset, iTableOffset;
  745. uint8_t ucTable, *pHuffVals;
  746. iOffset = 0;
  747. pHuffVals = (uint8_t *)pJPEG->usPixels; // temp holding area to save RAM
  748. while (iLen > 17) // while there are tables to copy (we may have combined more than 1 table together)
  749. {
  750. ucTable = pBuf[iOffset++]; // get table index
  751. if (ucTable & 0x10) // convert AC offset of 0x10 into offset of 4
  752. ucTable ^= 0x14;
  753. pJPEG->ucHuffTableUsed |= (1 << ucTable); // mark this table as being defined
  754. if (ucTable <= 7) // tables are 0-3, AC+DC
  755. {
  756. iTableOffset = ucTable * HUFF_TABLEN;
  757. j = 0; // total bits
  758. for (i=0; i<16; i++)
  759. {
  760. j += pBuf[iOffset];
  761. pHuffVals[iTableOffset+i] = pBuf[iOffset++];
  762. }
  763. iLen -= 17; // subtract length of bit lengths
  764. if (j == 0 || j > 256 || j > iLen) // bogus bit lengths
  765. {
  766. return -1;
  767. }
  768. iTableOffset += 16;
  769. for (i=0; i<j; i++)
  770. { // copy huffman table
  771. pHuffVals[iTableOffset+i] = pBuf[iOffset++];
  772. }
  773. iLen -= j;
  774. }
  775. }
  776. return 0;
  777. } /* JPEGGetHuffTables() */
  778. #ifdef FUTURE
  779. //
  780. // Create 11-bit lookup tables for some images where it doesn't work
  781. // for 10-bit tables
  782. //
  783. static int JPEGMakeHuffTables_Slow(JPEGIMAGE *pJPEG, int bThumbnail)
  784. {
  785. int code, repeat, count, codestart;
  786. int j;
  787. int iLen, iTable;
  788. unsigned short *pTable, *pShort, *pLong;
  789. unsigned char *pucTable, *pucShort, *pucLong;
  790. uint32_t ul, *pLongTable;
  791. int iBitNum; // current code bit length
  792. int cc; // code
  793. unsigned char *p, *pBits, ucCode;
  794. int iMaxLength, iMaxMask;
  795. pJPEG->b11Bit = 1; // indicate we're using the bigger A/C decode tables
  796. // first do DC components (up to 4 tables of 12-bit codes)
  797. // we can save time and memory for the DC codes by knowing that there exist short codes (<= 6 bits)
  798. // and long codes (>6 bits, but the first 5 bits are 1's). This allows us to create 2 tables: a 6-bit and 7 or 8-bit
  799. // to handle any DC codes
  800. iMaxLength = 12; // assume DC codes can be 12-bits
  801. iMaxMask = 0x7f; // lower 7 bits after truncate 5 leading 1's
  802. if (pJPEG->ucMode == 0xc3) // create 13-bit tables for lossless mode
  803. {
  804. iMaxLength = 13;
  805. iMaxMask = 0xff;
  806. }
  807. for (iTable = 0; iTable < 2; iTable++)
  808. {
  809. if (pJPEG->ucHuffTableUsed & (1<<iTable))
  810. {
  811. // pJPEG->huffdcFast[iTable] = (int *)PILIOAlloc(0x180); // short table = 128 bytes, long table = 256 bytes
  812. pucShort = (unsigned char *)&pJPEG->ucHuffDC[iTable*DC_TABLE_SIZE];
  813. // pJPEG->huffdc[iTable] = pJPEG->huffdcFast[iTable] + 0x20; // 0x20 longs = 128 bytes
  814. pucLong = (unsigned char *)&pJPEG->ucHuffDC[iTable*DC_TABLE_SIZE + 128];
  815. pBits = &pJPEG->ucHuffVals[iTable * HUFF_TABLEN];
  816. p = pBits;
  817. p += 16; // point to bit data
  818. cc = 0; // start with a code of 0
  819. for (iBitNum = 1; iBitNum <= 16; iBitNum++)
  820. {
  821. iLen = *pBits++; // get number of codes for this bit length
  822. if (iBitNum > iMaxLength && iLen > 0) // we can't handle codes longer a certain length
  823. {
  824. return -1;
  825. }
  826. while (iLen)
  827. {
  828. // if (iBitNum > 6) // do long table
  829. if ((cc >> (iBitNum-5)) == 0x1f) // first 5 bits are 1 - use long table
  830. {
  831. count = iMaxLength - iBitNum;
  832. codestart = cc << count;
  833. pucTable = &pucLong[codestart & iMaxMask]; // use lower 7/8 bits of code
  834. }
  835. else // do short table
  836. {
  837. count = 6 - iBitNum;
  838. if (count < 0)
  839. return -1; // DEBUG - something went wrong
  840. codestart = cc << count;
  841. pucTable = &pucShort[codestart];
  842. }
  843. ucCode = *p++; // get actual huffman code
  844. if (ucCode == 16 && pJPEG->ucMode == 0xc3) // lossless mode
  845. {
  846. // in lossless mode, this code won't fit in 4 bits, so save it's length in the next slot
  847. ucCode = 255;
  848. pucLong[256] = (unsigned char)iBitNum;
  849. }
  850. // does precalculating the DC value save time on ARM?
  851. #ifndef USE_ARM_ASM
  852. if (ucCode != 0 && (ucCode + iBitNum) <= 6 && pJPEG->ucMode != 0xc2) // we can fit the magnitude value in the code lookup (not for progressive)
  853. {
  854. int k, iLoop;
  855. unsigned char ucCoeff;
  856. unsigned char *d = &pucTable[512];
  857. unsigned char ucMag = ucCode;
  858. ucCode |= ((iBitNum+ucCode) << 4); // add magnitude bits to length
  859. repeat = 1<<ucMag;
  860. iLoop = 1<<(count-ucMag);
  861. for (j=0; j<repeat; j++)
  862. { // calcuate the magnitude coeff already
  863. if (j & 1<<(ucMag-1)) // positive number
  864. ucCoeff = (unsigned char)j;
  865. else // negative number
  866. ucCoeff = (unsigned char)(j - ((1<<ucMag)-1));
  867. for (k=0; k<iLoop; k++)
  868. {
  869. *d++ = ucCoeff;
  870. } // for k
  871. } // for j
  872. }
  873. #endif
  874. else
  875. {
  876. ucCode |= (iBitNum << 4);
  877. }
  878. if (count) // do it as dwords to save time
  879. {
  880. repeat = (1<<count);
  881. memset(pucTable, ucCode, repeat);
  882. // pLongTable = (uint32_t *)pTable;
  883. // repeat = 1 << (count-2); // store as dwords (/4)
  884. // ul = code | (code << 16);
  885. // for (j=0; j<repeat; j++)
  886. // *pLongTable++ = ul;
  887. }
  888. else
  889. {
  890. pucTable[0] = ucCode;
  891. }
  892. cc++;
  893. iLen--;
  894. }
  895. cc <<= 1;
  896. }
  897. } // if table defined
  898. }
  899. // now do AC components (up to 2 tables of 16-bit codes)
  900. // We split the codes into a short table (9 bits or less) and a long table (first 5 bits are 1)
  901. for (iTable = 0; iTable < 2; iTable++)
  902. {
  903. if (pJPEG->ucHuffTableUsed & (1<<(iTable+4))) // if this table is defined
  904. {
  905. pBits = &pJPEG->ucHuffVals[(iTable+4) * HUFF_TABLEN];
  906. p = pBits;
  907. p += 16; // point to bit data
  908. pShort = &pJPEG->usHuffAC[iTable*HUFF11SIZE];
  909. pLong = &pJPEG->usHuffAC[iTable*HUFF11SIZE + 1024]; // long codes start here
  910. cc = 0; // start with a code of 0
  911. // construct the decode table
  912. for (iBitNum = 1; iBitNum <= 16; iBitNum++)
  913. {
  914. iLen = *pBits++; // get number of codes for this bit length
  915. while (iLen)
  916. {
  917. if ((cc >> (iBitNum-4)) == 0xf) // first 4 bits are 1 - use long table
  918. {
  919. count = 16 - iBitNum;
  920. codestart = cc << count;
  921. pTable = &pLong[codestart & 0xfff]; // use lower 12 bits of code
  922. }
  923. else
  924. {
  925. count = 12 - iBitNum;
  926. if (count < 0) // a 13-bit? code - that doesn't fit our optimized scheme, see if we can do a bigger table version
  927. {
  928. return -1; // DEBUG - fatal error, we currently don't support it
  929. }
  930. codestart = cc << count;
  931. pTable = &pShort[codestart]; // 11 bits or shorter
  932. }
  933. code = *p++; // get actual huffman code
  934. if (bThumbnail && code != 0) // add "extra" bits to code length since we skip these codes
  935. {
  936. // get rid of extra bits in code and add increment (1) for AC index
  937. code = ((iBitNum+(code & 0xf)) << 8) | ((code >> 4)+1);
  938. }
  939. else
  940. {
  941. code |= (iBitNum << 8);
  942. }
  943. if (count) // do it as dwords to save time
  944. {
  945. repeat = 1 << (count-1); // store as dwords (/2)
  946. ul = code | (code << 16);
  947. pLongTable = (uint32_t *)pTable;
  948. for (j=0; j<repeat; j++)
  949. *pLongTable++ = ul;
  950. }
  951. else
  952. {
  953. pTable[0] = (unsigned short)code;
  954. }
  955. cc++;
  956. iLen--;
  957. }
  958. cc <<= 1;
  959. } // for each bit length
  960. } // if table defined
  961. }
  962. return 0;
  963. } /* JPEGMakeHuffTables_Slow() */
  964. #endif // FUTURE
  965. //
  966. // Expand the Huffman tables for fast decoding
  967. // returns 1 for success, 0 for failure
  968. //
  969. static int JPEGMakeHuffTables(JPEGIMAGE *pJPEG, int bThumbnail)
  970. {
  971. int code, repeat, count, codestart;
  972. int j;
  973. int iLen, iTable;
  974. uint16_t *pTable, *pShort, *pLong;
  975. uint8_t *pHuffVals, *pucTable, *pucShort, *pucLong;
  976. uint32_t ul, *pLongTable;
  977. int iBitNum; // current code bit length
  978. int cc; // code
  979. uint8_t *p, *pBits, ucCode;
  980. int iMaxLength, iMaxMask;
  981. int iTablesUsed;
  982. iTablesUsed = 0;
  983. pHuffVals = (uint8_t *)pJPEG->usPixels;
  984. for (j=0; j<4; j++)
  985. {
  986. if (pJPEG->ucHuffTableUsed & (1 << j))
  987. iTablesUsed++;
  988. }
  989. // first do DC components (up to 4 tables of 12-bit codes)
  990. // we can save time and memory for the DC codes by knowing that there exist short codes (<= 6 bits)
  991. // and long codes (>6 bits, but the first 5 bits are 1's). This allows us to create 2 tables: a 6-bit and 7 or 8-bit
  992. // to handle any DC codes
  993. iMaxLength = 12; // assume DC codes can be 12-bits
  994. iMaxMask = 0x7f; // lower 7 bits after truncate 5 leading 1's
  995. for (iTable = 0; iTable < 4; iTable++)
  996. {
  997. if (pJPEG->ucHuffTableUsed & (1 << iTable))
  998. {
  999. // pJPEG->huffdcFast[iTable] = (int *)PILIOAlloc(0x180); // short table = 128 bytes, long table = 256 bytes
  1000. pucShort = &pJPEG->ucHuffDC[iTable*DC_TABLE_SIZE];
  1001. // pJPEG->huffdc[iTable] = pJPEG->huffdcFast[iTable] + 0x20; // 0x20 longs = 128 bytes
  1002. pucLong = &pJPEG->ucHuffDC[iTable*DC_TABLE_SIZE + 128];
  1003. pBits = &pHuffVals[iTable * HUFF_TABLEN];
  1004. p = pBits;
  1005. p += 16; // point to bit data
  1006. cc = 0; // start with a code of 0
  1007. for (iBitNum = 1; iBitNum <= 16; iBitNum++)
  1008. {
  1009. iLen = *pBits++; // get number of codes for this bit length
  1010. if (iBitNum > iMaxLength && iLen > 0) // we can't handle codes longer a certain length
  1011. {
  1012. return 0;
  1013. }
  1014. while (iLen)
  1015. {
  1016. // if (iBitNum > 6) // do long table
  1017. if ((cc >> (iBitNum-5)) == 0x1f) // first 5 bits are 1 - use long table
  1018. {
  1019. count = iMaxLength - iBitNum;
  1020. codestart = cc << count;
  1021. pucTable = &pucLong[codestart & iMaxMask]; // use lower 7/8 bits of code
  1022. }
  1023. else // do short table
  1024. {
  1025. count = 6 - iBitNum;
  1026. if (count < 0)
  1027. return 0; // DEBUG - something went wrong
  1028. codestart = cc << count;
  1029. pucTable = &pucShort[codestart];
  1030. }
  1031. ucCode = *p++; // get actual huffman code
  1032. // does precalculating the DC value save time on ARM?
  1033. #ifndef USE_ARM_ASM
  1034. if (ucCode != 0 && (ucCode + iBitNum) <= 6 && pJPEG->ucMode != 0xc2) // we can fit the magnitude value in the code lookup (not for progressive)
  1035. {
  1036. int k, iLoop;
  1037. unsigned char ucCoeff;
  1038. unsigned char *d = &pucTable[512];
  1039. unsigned char ucMag = ucCode;
  1040. ucCode |= ((iBitNum+ucCode) << 4); // add magnitude bits to length
  1041. repeat = 1<<ucMag;
  1042. iLoop = 1<<(count-ucMag);
  1043. for (j=0; j<repeat; j++)
  1044. { // calcuate the magnitude coeff already
  1045. if (j & 1<<(ucMag-1)) // positive number
  1046. ucCoeff = (unsigned char)j;
  1047. else // negative number
  1048. ucCoeff = (unsigned char)(j - ((1<<ucMag)-1));
  1049. for (k=0; k<iLoop; k++)
  1050. {
  1051. *d++ = ucCoeff;
  1052. } // for k
  1053. } // for j
  1054. }
  1055. #endif
  1056. else
  1057. {
  1058. ucCode |= (iBitNum << 4);
  1059. }
  1060. if (count) // do it as dwords to save time
  1061. {
  1062. repeat = (1<<count);
  1063. memset(pucTable, ucCode, repeat);
  1064. // pLongTable = (uint32_t *)pTable;
  1065. // repeat = 1 << (count-2); // store as dwords (/4)
  1066. // ul = code | (code << 16);
  1067. // for (j=0; j<repeat; j++)
  1068. // *pLongTable++ = ul;
  1069. }
  1070. else
  1071. {
  1072. pucTable[0] = ucCode;
  1073. }
  1074. cc++;
  1075. iLen--;
  1076. }
  1077. cc <<= 1;
  1078. }
  1079. } // if table defined
  1080. }
  1081. // now do AC components (up to 4 tables of 16-bit codes)
  1082. // We split the codes into a short table (9 bits or less) and a long table (first 5 bits are 1)
  1083. for (iTable = 0; iTable < 4; iTable++)
  1084. {
  1085. if (pJPEG->ucHuffTableUsed & (1 << (iTable+4))) // if this table is defined
  1086. {
  1087. pBits = &pHuffVals[(iTable+4) * HUFF_TABLEN];
  1088. p = pBits;
  1089. p += 16; // point to bit data
  1090. pShort = &pJPEG->usHuffAC[iTable*HUFF11SIZE];
  1091. pLong = &pJPEG->usHuffAC[iTable*HUFF11SIZE + 1024];
  1092. cc = 0; // start with a code of 0
  1093. // construct the decode table
  1094. for (iBitNum = 1; iBitNum <= 16; iBitNum++)
  1095. {
  1096. iLen = *pBits++; // get number of codes for this bit length
  1097. while (iLen)
  1098. {
  1099. if ((cc >> (iBitNum-6)) == 0x3f) // first 6 bits are 1 - use long table
  1100. {
  1101. count = 16 - iBitNum;
  1102. codestart = cc << count;
  1103. pTable = &pLong[codestart & 0x3ff]; // use lower 10 bits of code
  1104. }
  1105. else
  1106. {
  1107. count = 10 - iBitNum;
  1108. if (count < 0) // an 11/12-bit? code - that doesn't fit our optimized scheme, see if we can do a bigger table version
  1109. {
  1110. if (count == -1 && iTablesUsed <= 4) // we need to create "slow" tables
  1111. { // DEBUG
  1112. // j = JPEGMakeHuffTables_Slow(pJPEG, bThumbnail);
  1113. return 0;
  1114. }
  1115. else
  1116. return 0; // DEBUG - fatal error, more than 2 big tables we currently don't support
  1117. }
  1118. codestart = cc << count;
  1119. pTable = &pShort[codestart]; // 10 bits or shorter
  1120. }
  1121. code = *p++; // get actual huffman code
  1122. if (bThumbnail && code != 0) // add "extra" bits to code length since we skip these codes
  1123. {
  1124. // get rid of extra bits in code and add increment (1) for AC index
  1125. code = ((iBitNum+(code & 0xf)) << 8) | ((code >> 4)+1);
  1126. }
  1127. #ifdef BOGUS // precalculating the AC coeff makes it run slightly slower
  1128. else if ((code & 0xf) != 0 && (code + iBitNum) <= 10) // we can fit the magnitude value + huffman code in a single read
  1129. {
  1130. int k, iLoop;
  1131. unsigned short usCoeff;
  1132. unsigned short *d = &pTable[4096]; // use unused table slots 2+3 for extra coeff data
  1133. unsigned char ucMag = (unsigned char)(code & 0xf);
  1134. code |= ((iBitNum + (code & 0xf)) << 8); // add magnitude bits to length
  1135. repeat = 1<<ucMag;
  1136. iLoop = 1<<(count-ucMag);
  1137. for (j=0; j<repeat; j++)
  1138. { // calcuate the magnitude coeff already
  1139. if (j & 1<<(ucMag-1)) // positive number
  1140. usCoeff = (unsigned short)j;
  1141. else // negative number
  1142. usCoeff = (unsigned short)(j - ((1<<ucMag)-1));
  1143. for (k=0; k<iLoop; k++)
  1144. {
  1145. *d++ = usCoeff;
  1146. } // for k
  1147. } // for j
  1148. }
  1149. #endif
  1150. else
  1151. {
  1152. code |= (iBitNum << 8);
  1153. }
  1154. if (count) // do it as dwords to save time
  1155. {
  1156. repeat = 1 << (count-1); // store as dwords (/2)
  1157. ul = code | (code << 16);
  1158. pLongTable = (uint32_t *)pTable;
  1159. for (j=0; j<repeat; j++)
  1160. *pLongTable++ = ul;
  1161. }
  1162. else
  1163. {
  1164. pTable[0] = (unsigned short)code;
  1165. }
  1166. cc++;
  1167. iLen--;
  1168. }
  1169. cc <<= 1;
  1170. } // for each bit length
  1171. } // if table defined
  1172. }
  1173. return 1;
  1174. } /* JPEGMakeHuffTables() */
  1175. //
  1176. // TIFFSHORT
  1177. // read a 16-bit unsigned integer from the given pointer
  1178. // and interpret the data as big endian (Motorola) or little endian (Intel)
  1179. //
  1180. static uint16_t TIFFSHORT(unsigned char *p, int bMotorola)
  1181. {
  1182. unsigned short s;
  1183. if (bMotorola)
  1184. s = *p * 0x100 + *(p+1); // big endian (AKA Motorola byte order)
  1185. else
  1186. s = *p + *(p+1)*0x100; // little endian (AKA Intel byte order)
  1187. return s;
  1188. } /* TIFFSHORT() */
  1189. //
  1190. // TIFFLONG
  1191. // read a 32-bit unsigned integer from the given pointer
  1192. // and interpret the data as big endian (Motorola) or little endian (Intel)
  1193. //
  1194. static uint32_t TIFFLONG(unsigned char *p, int bMotorola)
  1195. {
  1196. uint32_t l;
  1197. if (bMotorola)
  1198. l = *p * 0x1000000 + *(p+1) * 0x10000 + *(p+2) * 0x100 + *(p+3); // big endian
  1199. else
  1200. l = *p + *(p+1) * 0x100 + *(p+2) * 0x10000 + *(p+3) * 0x1000000; // little endian
  1201. return l;
  1202. } /* TIFFLONG() */
  1203. //
  1204. // TIFFVALUE
  1205. // read an integer value encoded in a TIFF TAG (12-byte structure)
  1206. // and interpret the data as big endian (Motorola) or little endian (Intel)
  1207. //
  1208. static int TIFFVALUE(unsigned char *p, int bMotorola)
  1209. {
  1210. int i, iType;
  1211. iType = TIFFSHORT(p+2, bMotorola);
  1212. /* If pointer to a list of items, must be a long */
  1213. if (TIFFSHORT(p+4, bMotorola) > 1)
  1214. {
  1215. iType = 4;
  1216. }
  1217. switch (iType)
  1218. {
  1219. case 3: /* Short */
  1220. i = TIFFSHORT(p+8, bMotorola);
  1221. break;
  1222. case 4: /* Long */
  1223. case 7: // undefined (treat it as a long since it's usually a multibyte buffer)
  1224. i = TIFFLONG(p+8, bMotorola);
  1225. break;
  1226. case 6: // signed byte
  1227. i = (signed char)p[8];
  1228. break;
  1229. case 2: /* ASCII */
  1230. case 5: /* Unsigned Rational */
  1231. case 10: /* Signed Rational */
  1232. i = TIFFLONG(p+8, bMotorola);
  1233. break;
  1234. default: /* to suppress compiler warning */
  1235. i = 0;
  1236. break;
  1237. }
  1238. return i;
  1239. } /* TIFFVALUE() */
  1240. static void GetTIFFInfo(JPEGIMAGE *pPage, int bMotorola, int iOffset)
  1241. {
  1242. int iTag, iTagCount, i;
  1243. uint8_t *cBuf = pPage->ucFileBuf;
  1244. iTagCount = TIFFSHORT(&cBuf[iOffset], bMotorola); /* Number of tags in this dir */
  1245. if (iTagCount < 1 || iTagCount > 256) // invalid tag count
  1246. return; /* Bad header info */
  1247. /*--- Search the TIFF tags ---*/
  1248. for (i=0; i<iTagCount; i++)
  1249. {
  1250. unsigned char *p = &cBuf[iOffset + (i*12) +2];
  1251. iTag = TIFFSHORT(p, bMotorola); /* current tag value */
  1252. if (iTag == 274) // orientation tag
  1253. {
  1254. pPage->ucOrientation = TIFFVALUE(p, bMotorola);
  1255. }
  1256. else if (iTag == 256) // width of thumbnail
  1257. {
  1258. pPage->iThumbWidth = TIFFVALUE(p, bMotorola);
  1259. }
  1260. else if (iTag == 257) // height of thumbnail
  1261. {
  1262. pPage->iThumbHeight = TIFFVALUE(p, bMotorola);
  1263. }
  1264. else if (iTag == 513) // offset to JPEG data
  1265. {
  1266. pPage->iThumbData = TIFFVALUE(p, bMotorola);
  1267. }
  1268. }
  1269. } /* GetTIFFInfo() */
  1270. static int JPEGGetSOS(JPEGIMAGE *pJPEG, int *iOff)
  1271. {
  1272. int16_t sLen;
  1273. int iOffset = *iOff;
  1274. int i, j;
  1275. uint8_t uc,c,cc;
  1276. uint8_t *buf = pJPEG->ucFileBuf;
  1277. sLen = MOTOSHORT(&buf[iOffset]);
  1278. iOffset += 2;
  1279. // Assume no components in this scan
  1280. for (i=0; i<4; i++)
  1281. pJPEG->JPCI[i].component_needed = 0;
  1282. uc = buf[iOffset++]; // get number of components
  1283. pJPEG->ucComponentsInScan = uc;
  1284. sLen -= 3;
  1285. if (uc < 1 || uc > MAX_COMPS_IN_SCAN || sLen != (uc*2+3)) // check length of data packet
  1286. return 1; // error
  1287. for (i=0; i<uc; i++)
  1288. {
  1289. cc = buf[iOffset++];
  1290. c = buf[iOffset++];
  1291. sLen -= 2;
  1292. for (j=0; j<4; j++) // search for component id
  1293. {
  1294. if (pJPEG->JPCI[j].component_id == cc)
  1295. break;
  1296. }
  1297. if (j == 4) // error, not found
  1298. return 1;
  1299. if ((c & 0xf) > 3 || (c & 0xf0) > 0x30)
  1300. return 1; // bogus table numbers
  1301. pJPEG->JPCI[j].dc_tbl_no = c >> 4;
  1302. pJPEG->JPCI[j].ac_tbl_no = c & 0xf;
  1303. pJPEG->JPCI[j].component_needed = 1; // mark this component as being included in the scan
  1304. }
  1305. pJPEG->iScanStart = buf[iOffset++]; // Get the scan start (or lossless predictor) for this scan
  1306. pJPEG->iScanEnd = buf[iOffset++]; // Get the scan end for this scan
  1307. c = buf[iOffset++]; // successive approximation bits
  1308. pJPEG->cApproxBitsLow = c & 0xf; // also point transform in lossless mode
  1309. pJPEG->cApproxBitsHigh = c >> 4;
  1310. *iOff = iOffset;
  1311. return 0;
  1312. } /* JPEGGetSOS() */
  1313. //
  1314. // Remove markers from the data stream to allow faster decode
  1315. // Stuffed zeros and restart interval markers aren't needed to properly decode
  1316. // the data, but they make reading VLC data slower, so I pull them out first
  1317. //
  1318. static int JPEGFilter(uint8_t *pBuf, uint8_t *d, int iLen, uint8_t *bFF)
  1319. {
  1320. // since we have the entire jpeg buffer in memory already, we can just change it in place
  1321. unsigned char c, *s, *pEnd, *pStart;
  1322. pStart = d;
  1323. s = pBuf;
  1324. pEnd = &s[iLen-1]; // stop just shy of the end to not miss a final marker/stuffed 0
  1325. if (*bFF) // last byte was a FF, check the next one
  1326. {
  1327. if (s[0] == 0) // stuffed 0, keep the FF
  1328. *d++ = 0xff;
  1329. s++;
  1330. *bFF = 0;
  1331. }
  1332. while (s < pEnd)
  1333. {
  1334. c = *d++ = *s++;
  1335. if (c == 0xff) // marker or stuffed zeros?
  1336. {
  1337. if (s[0] != 0) // it's a marker, skip both
  1338. {
  1339. d--;
  1340. }
  1341. s++; // for stuffed 0's, store the FF, skip the 00
  1342. }
  1343. }
  1344. if (s == pEnd) // need to test the last byte
  1345. {
  1346. c = s[0];
  1347. if (c == 0xff) // last byte is FF, take care of it next time through
  1348. *bFF = 1; // take care of it next time through
  1349. else
  1350. *d++ = c; // nope, just store it
  1351. }
  1352. return (int)(d-pStart); // filtered output length
  1353. } /* JPEGFilter() */
  1354. //
  1355. // Read and filter more VLC data for decoding
  1356. //
  1357. static void JPEGGetMoreData(JPEGIMAGE *pPage)
  1358. {
  1359. int iDelta = pPage->iVLCSize - pPage->iVLCOff;
  1360. // printf("Getting more data...size=%d, off=%d\n", pPage->iVLCSize, pPage->iVLCOff);
  1361. // move any existing data down
  1362. if (iDelta >= (JPEG_FILE_BUF_SIZE-64) || iDelta < 0)
  1363. return; // buffer is already full; no need to read more data
  1364. if (pPage->iVLCOff != 0)
  1365. {
  1366. memcpy(pPage->ucFileBuf, &pPage->ucFileBuf[pPage->iVLCOff], pPage->iVLCSize - pPage->iVLCOff);
  1367. pPage->iVLCSize -= pPage->iVLCOff;
  1368. pPage->iVLCOff = 0;
  1369. pPage->bb.pBuf = pPage->ucFileBuf; // reset VLC source pointer too
  1370. }
  1371. if (pPage->JPEGFile.iPos < pPage->JPEGFile.iSize && pPage->iVLCSize < JPEG_FILE_BUF_SIZE-64)
  1372. {
  1373. int i;
  1374. // Try to read enough to fill the buffer
  1375. i = (*pPage->pfnRead)(&pPage->JPEGFile, &pPage->ucFileBuf[pPage->iVLCSize], JPEG_FILE_BUF_SIZE - pPage->iVLCSize); // max length we can read
  1376. // Filter out the markers
  1377. pPage->iVLCSize += JPEGFilter(&pPage->ucFileBuf[pPage->iVLCSize], &pPage->ucFileBuf[pPage->iVLCSize], i, &pPage->ucFF);
  1378. }
  1379. } /* JPEGGetMoreData() */
  1380. //
  1381. // Parse the JPEG header, gather necessary info to decode the image
  1382. // Returns 1 for success, 0 for failure
  1383. //
  1384. static int JPEGParseInfo(JPEGIMAGE *pPage, int bExtractThumb)
  1385. {
  1386. int iBytesRead;
  1387. int i, iOffset, iTableOffset;
  1388. uint8_t ucTable, *s = pPage->ucFileBuf;
  1389. uint16_t usMarker, usLen = 0;
  1390. int iFilePos = 0;
  1391. if (bExtractThumb) // seek to the start of the thumbnail image
  1392. {
  1393. iFilePos = pPage->iThumbData;
  1394. (*pPage->pfnSeek)(&pPage->JPEGFile, iFilePos);
  1395. }
  1396. iBytesRead = (*pPage->pfnRead)(&pPage->JPEGFile, s, JPEG_FILE_BUF_SIZE);
  1397. if (iBytesRead < 256) // a JPEG file this tiny? probably bad
  1398. {
  1399. pPage->iError = JPEG_INVALID_FILE;
  1400. return 0;
  1401. }
  1402. iFilePos += iBytesRead;
  1403. if (MOTOSHORT(pPage->ucFileBuf) != 0xffd8)
  1404. {
  1405. pPage->iError = JPEG_INVALID_FILE;
  1406. return 0; // not a JPEG file
  1407. }
  1408. iOffset = 2; /* Start at offset of first marker */
  1409. usMarker = 0; /* Search for SOFx (start of frame) marker */
  1410. while (usMarker != 0xffda && iOffset < pPage->JPEGFile.iSize)
  1411. {
  1412. if (iOffset >= JPEG_FILE_BUF_SIZE/2) // too close to the end, read more data
  1413. {
  1414. // Do we need to seek first?
  1415. if (iOffset >= JPEG_FILE_BUF_SIZE)
  1416. {
  1417. iFilePos += (iOffset - iBytesRead);
  1418. iOffset = 0;
  1419. (*pPage->pfnSeek)(&pPage->JPEGFile, iFilePos);
  1420. iBytesRead = 0; // throw away any old data
  1421. }
  1422. // move existing bytes down
  1423. if (iOffset)
  1424. {
  1425. memcpy(pPage->ucFileBuf, &pPage->ucFileBuf[iOffset], iBytesRead - iOffset);
  1426. iBytesRead -= iOffset;
  1427. iOffset = 0;
  1428. }
  1429. i = (*pPage->pfnRead)(&pPage->JPEGFile, &pPage->ucFileBuf[iBytesRead], JPEG_FILE_BUF_SIZE-iBytesRead);
  1430. iFilePos += i;
  1431. iBytesRead += i;
  1432. }
  1433. usMarker = MOTOSHORT(&s[iOffset]);
  1434. iOffset += 2;
  1435. usLen = MOTOSHORT(&s[iOffset]); // marker length
  1436. if (usMarker < 0xffc0 || usMarker == 0xffff) // invalid marker, could be generated by "Arles Image Web Page Creator" or Accusoft
  1437. {
  1438. iOffset++;
  1439. continue; // skip 1 byte and try to resync
  1440. }
  1441. switch (usMarker)
  1442. {
  1443. case 0xffc1:
  1444. case 0xffc2:
  1445. case 0xffc3:
  1446. pPage->iError = JPEG_UNSUPPORTED_FEATURE;
  1447. return 0; // currently unsupported modes
  1448. case 0xffe1: // App1 (EXIF?)
  1449. if (s[iOffset+2] == 'E' && s[iOffset+3] == 'x' && (s[iOffset+8] == 'M' || s[iOffset+8] == 'I')) // the EXIF data we want
  1450. {
  1451. int bMotorola, IFD, iTagCount;
  1452. pPage->iEXIF = iFilePos - iBytesRead + iOffset + 8; // start of TIFF file
  1453. // Get the orientation value (if present)
  1454. bMotorola = (s[iOffset+8] == 'M');
  1455. IFD = TIFFLONG(&s[iOffset+12], bMotorola);
  1456. iTagCount = TIFFSHORT(&s[iOffset+16], bMotorola);
  1457. GetTIFFInfo(pPage, bMotorola, IFD+iOffset+8);
  1458. // The second IFD defines the thumbnail (if present)
  1459. if (iTagCount >= 1 && iTagCount < 32) // valid number of tags for EXIF data 'page'
  1460. {
  1461. // point to next IFD
  1462. IFD += (12 * iTagCount) + 2;
  1463. IFD = TIFFLONG(&s[IFD + iOffset + 8], bMotorola);
  1464. if (IFD != 0) // Thumbnail present?
  1465. {
  1466. pPage->ucHasThumb = 1;
  1467. GetTIFFInfo(pPage, bMotorola, IFD+iOffset+8); // info for second 'page' of TIFF
  1468. pPage->iThumbData += iOffset + 8; // absolute offset in the file
  1469. }
  1470. }
  1471. }
  1472. break;
  1473. case 0xffc0: // SOFx - start of frame
  1474. pPage->ucMode = (uint8_t)usMarker;
  1475. pPage->ucBpp = s[iOffset+2]; // bits per sample
  1476. pPage->iHeight = MOTOSHORT(&s[iOffset+3]);
  1477. pPage->iWidth = MOTOSHORT(&s[iOffset+5]);
  1478. pPage->ucNumComponents = s[iOffset+7];
  1479. pPage->ucBpp = pPage->ucBpp * pPage->ucNumComponents; /* Bpp = number of components * bits per sample */
  1480. if (pPage->ucNumComponents == 1)
  1481. pPage->ucSubSample = 0; // use this to differentiate from color 1:1
  1482. else
  1483. {
  1484. usLen -= 8;
  1485. iOffset += 8;
  1486. // pPage->ucSubSample = s[iOffset+9]; // subsampling option for the second color component
  1487. for (i=0; i<pPage->ucNumComponents; i++)
  1488. {
  1489. uint8_t ucSamp;
  1490. pPage->JPCI[i].component_id = s[iOffset++];
  1491. pPage->JPCI[i].component_index = (unsigned char)i;
  1492. ucSamp = s[iOffset++]; // get the h+v sampling factor
  1493. if (i == 0) // Y component?
  1494. pPage->ucSubSample = ucSamp;
  1495. // pPage->JPCI[i].h_samp_factor = ucSamp >> 4;
  1496. // pPage->JPCI[i].v_samp_factor = ucSamp & 0xf;
  1497. pPage->JPCI[i].quant_tbl_no = s[iOffset++]; // quantization table number
  1498. usLen -= 3;
  1499. }
  1500. }
  1501. break;
  1502. case 0xffdd: // Restart Interval
  1503. if (usLen == 4)
  1504. pPage->iResInterval = MOTOSHORT(&s[iOffset+2]);
  1505. break;
  1506. case 0xffc4: /* M_DHT */ // get Huffman tables
  1507. iOffset += 2; // skip length
  1508. usLen -= 2; // subtract length length
  1509. if (JPEGGetHuffTables(&s[iOffset], usLen, pPage) != 0) // bad tables?
  1510. {
  1511. pPage->iError = JPEG_DECODE_ERROR;
  1512. return 0; // error
  1513. }
  1514. break;
  1515. case 0xffdb: /* M_DQT */
  1516. /* Get the quantization tables */
  1517. /* first byte has PPPPNNNN where P = precision and N = table number 0-3 */
  1518. iOffset += 2; // skip length
  1519. usLen -= 2; // subtract length length
  1520. while (usLen > 0)
  1521. {
  1522. ucTable = s[iOffset++]; // table number
  1523. if ((ucTable & 0xf) > 3) // invalid table number
  1524. {
  1525. pPage->iError = JPEG_DECODE_ERROR;
  1526. return 0;
  1527. }
  1528. iTableOffset = (ucTable & 0xf) * DCTSIZE;
  1529. if (ucTable & 0xf0) // if word precision
  1530. {
  1531. for (i=0; i<DCTSIZE; i++)
  1532. {
  1533. pPage->sQuantTable[i+iTableOffset] = MOTOSHORT(&s[iOffset]);
  1534. iOffset += 2;
  1535. }
  1536. usLen -= (DCTSIZE*2 + 1);
  1537. }
  1538. else // byte precision
  1539. {
  1540. for (i=0; i<DCTSIZE; i++)
  1541. {
  1542. pPage->sQuantTable[i+iTableOffset] = (unsigned short)s[iOffset++];
  1543. }
  1544. usLen -= (DCTSIZE + 1);
  1545. }
  1546. }
  1547. break;
  1548. } // switch on JPEG marker
  1549. iOffset += usLen;
  1550. } // while
  1551. if (usMarker == 0xffda) // start of image
  1552. {
  1553. if (pPage->ucBpp != 8) // need to match up table IDs
  1554. {
  1555. iOffset -= usLen;
  1556. JPEGGetSOS(pPage, &iOffset); // get Start-Of-Scan info for decoding
  1557. }
  1558. if (!JPEGMakeHuffTables(pPage, 0)) //int bThumbnail) DEBUG
  1559. {
  1560. pPage->iError = JPEG_UNSUPPORTED_FEATURE;
  1561. return 0;
  1562. }
  1563. // Now the offset points to the start of compressed data
  1564. i = JPEGFilter(&pPage->ucFileBuf[iOffset], pPage->ucFileBuf, iBytesRead-iOffset, &pPage->ucFF);
  1565. pPage->iVLCOff = 0;
  1566. pPage->iVLCSize = i;
  1567. JPEGGetMoreData(pPage); // read more VLC data
  1568. return 1;
  1569. }
  1570. pPage->iError = JPEG_DECODE_ERROR;
  1571. return 0;
  1572. } /* JPEGParseInfo() */
  1573. //
  1574. // Fix and reorder the quantization table for faster decoding.*
  1575. //
  1576. static void JPEGFixQuantD(JPEGIMAGE *pJPEG)
  1577. {
  1578. int iTable, iTableOffset;
  1579. signed short sTemp[DCTSIZE];
  1580. int i;
  1581. uint16_t *p;
  1582. for (iTable=0; iTable<pJPEG->ucNumComponents; iTable++)
  1583. {
  1584. iTableOffset = iTable * DCTSIZE;
  1585. p = (uint16_t *)&pJPEG->sQuantTable[iTableOffset];
  1586. for (i=0; i<DCTSIZE; i++)
  1587. sTemp[i] = p[cZigZag[i]];
  1588. memcpy(&pJPEG->sQuantTable[iTableOffset], sTemp, DCTSIZE*sizeof(short)); // copy back to original spot
  1589. // Prescale for DCT multiplication
  1590. p = (uint16_t *)&pJPEG->sQuantTable[iTableOffset];
  1591. for (i=0; i<DCTSIZE; i++)
  1592. {
  1593. p[i] = (uint16_t)((p[i] * iScaleBits[i]) >> 12);
  1594. }
  1595. }
  1596. } /* JPEGFixQuantD() */
  1597. //
  1598. // Decode the 64 coefficients of the current DCT block
  1599. //
  1600. static int JPEGDecodeMCU(JPEGIMAGE *pJPEG, int iMCU, int *iDCPredictor)
  1601. {
  1602. uint32_t ulCode, ulTemp;
  1603. uint8_t *pZig;
  1604. signed char cCoeff;
  1605. unsigned short *pFast;
  1606. unsigned char ucHuff, *pucFast;
  1607. uint32_t usHuff; // this prevents an unnecessary & 65535 for shorts
  1608. uint32_t ulBitOff, ulBits; // local copies to allow compiler to use register vars
  1609. uint8_t *pBuf, *pEnd, *pEnd2;
  1610. signed short *pMCU = &pJPEG->sMCUs[iMCU];
  1611. uint8_t ucMaxACCol, ucMaxACRow;
  1612. #define MIN_DCT_THRESHOLD 8
  1613. ulBitOff = pJPEG->bb.ulBitOff;
  1614. ulBits = pJPEG->bb.ulBits;
  1615. pBuf = pJPEG->bb.pBuf;
  1616. pZig = (unsigned char *)&cZigZag2[1];
  1617. pEnd = (unsigned char *)&cZigZag2[64];
  1618. if (ulBitOff > (REGISTER_WIDTH-17)) // need to get more data
  1619. {
  1620. pBuf += (ulBitOff >> 3);
  1621. ulBitOff &= 7;
  1622. ulBits = MOTOLONG(pBuf);
  1623. }
  1624. if (pJPEG->iOptions & (JPEG_SCALE_QUARTER | JPEG_SCALE_EIGHTH)) // reduced size DCT
  1625. {
  1626. pMCU[1] = pMCU[8] = pMCU[9] = 0;
  1627. pEnd2 = (uint8_t *)&cZigZag2[5]; // we only need to store the 4 elements we care about
  1628. }
  1629. else
  1630. {
  1631. memset(pMCU, 0, 64*sizeof(short)); // pre-fill with zero since we may skip coefficients
  1632. pEnd2 = (uint8_t *)&cZigZag2[64];
  1633. }
  1634. ucMaxACCol = ucMaxACRow = 0;
  1635. pZig = (unsigned char *)&cZigZag2[1];
  1636. pEnd = (unsigned char *)&cZigZag2[64];
  1637. // get the DC component
  1638. pucFast = &pJPEG->ucHuffDC[pJPEG->ucDCTable * DC_TABLE_SIZE];
  1639. ulCode = (ulBits >> (REGISTER_WIDTH - 12 - ulBitOff)) & 0xfff; // get as lower 12 bits
  1640. if (ulCode >= 0xf80) // it's a long code
  1641. ulCode = (ulCode & 0xff); // point to long table and trim to 7-bits + 0x80 offset into long table
  1642. else
  1643. ulCode >>= 6; // it's a short code, use first 6 bits only
  1644. ucHuff = pucFast[ulCode];
  1645. cCoeff = (signed char)pucFast[ulCode+512]; // get pre-calculated extra bits for "small" values
  1646. if (ucHuff == 0) // invalid code
  1647. return -1;
  1648. ulBitOff += (ucHuff >> 4); // add the Huffman length
  1649. ucHuff &= 0xf; // get the actual code (SSSS)
  1650. if (ucHuff) // if there is a change to the DC value
  1651. { // get the 'extra' bits
  1652. if (cCoeff)
  1653. {
  1654. (*iDCPredictor) += cCoeff;
  1655. }
  1656. else
  1657. {
  1658. if (ulBitOff > (REGISTER_WIDTH - 17)) // need to get more data
  1659. {
  1660. pBuf += (ulBitOff >> 3);
  1661. ulBitOff &= 7;
  1662. ulBits = MOTOLONG(pBuf);
  1663. }
  1664. ulCode = ulBits << ulBitOff;
  1665. ulTemp = ~(uint32_t)(((int32_t)ulCode)>>31); // slide sign bit across other 31 bits
  1666. ulCode >>= (REGISTER_WIDTH - ucHuff);
  1667. ulCode -= ulTemp>>(REGISTER_WIDTH-ucHuff);
  1668. ulBitOff += ucHuff; // add bit length
  1669. (*iDCPredictor) += (int)ulCode;
  1670. }
  1671. }
  1672. pMCU[0] = (short)*iDCPredictor; // store in MCU[0]
  1673. // Now get the other 63 AC coefficients
  1674. pFast = &pJPEG->usHuffAC[pJPEG->ucACTable * HUFF11SIZE];
  1675. if (pJPEG->b11Bit) // 11-bit "slow" tables used
  1676. {
  1677. // if (pJPEG->pHuffACFast == pJPEG->huffacFast[1]) // second table
  1678. // pFast = &pJPEG->ucAltHuff[0];
  1679. while (pZig < pEnd)
  1680. {
  1681. if (ulBitOff >(REGISTER_WIDTH - 17)) // need to get more data
  1682. {
  1683. pBuf += (ulBitOff >> 3);
  1684. ulBitOff &= 7;
  1685. ulBits = MOTOLONG(pBuf);
  1686. }
  1687. ulCode = (ulBits >> (REGISTER_WIDTH - 16 - ulBitOff)) & 0xffff; // get as lower 16 bits
  1688. if (ulCode >= 0xf000) // first 4 bits = 1, use long table
  1689. ulCode = (ulCode & 0x1fff);
  1690. else
  1691. ulCode >>= 4; // use lower 12 bits (short table)
  1692. usHuff = pFast[ulCode];
  1693. if (usHuff == 0) // invalid code
  1694. return -1;
  1695. ulBitOff += (usHuff >> 8); // add length
  1696. usHuff &= 0xff; // get code (RRRR/SSSS)
  1697. if (usHuff == 0) // no more AC components
  1698. {
  1699. goto mcu_done;
  1700. }
  1701. if (ulBitOff > (REGISTER_WIDTH - 17)) // need to get more data
  1702. {
  1703. pBuf += (ulBitOff >> 3);
  1704. ulBitOff &= 7;
  1705. ulBits = MOTOLONG(pBuf);
  1706. }
  1707. pZig += (usHuff >> 4); // get the skip amount (RRRR)
  1708. usHuff &= 0xf; // get (SSSS) - extra length
  1709. if (pZig < pEnd && usHuff) // && piHisto)
  1710. {
  1711. ulCode = ulBits << ulBitOff;
  1712. ulTemp = ~(uint32_t) (((int32_t) ulCode) >> (REGISTER_WIDTH-1)); // slide sign bit across other 63 bits
  1713. ulCode >>= (REGISTER_WIDTH - usHuff);
  1714. ulCode -= ulTemp >> (REGISTER_WIDTH - usHuff);
  1715. ucMaxACCol |= 1<<(*pZig & 7); // keep track of occupied columns
  1716. if (*pZig >= 0x20) // if more than 4 rows used in a col, mark it
  1717. ucMaxACRow |= 1<<(*pZig & 7); // keep track of the max AC term row
  1718. pMCU[*pZig] = (signed short)ulCode; // store AC coefficient (already reordered)
  1719. }
  1720. ulBitOff += usHuff; // add (SSSS) extra length
  1721. pZig++;
  1722. } // while
  1723. }
  1724. else // 10-bit "fast" tables used
  1725. {
  1726. while (pZig < pEnd)
  1727. {
  1728. if (ulBitOff >(REGISTER_WIDTH - 17)) // need to get more data
  1729. {
  1730. pBuf += (ulBitOff >> 3);
  1731. ulBitOff &= 7;
  1732. ulBits = MOTOLONG(pBuf);
  1733. }
  1734. ulCode = (ulBits >> (REGISTER_WIDTH - 16 - ulBitOff)) & 0xffff; // get as lower 16 bits
  1735. if (ulCode >= 0xfc00) // first 6 bits = 1, use long table
  1736. ulCode = (ulCode & 0x7ff); // (ulCode & 0x3ff) + 0x400;
  1737. else
  1738. ulCode >>= 6; // use lower 10 bits (short table)
  1739. usHuff = pFast[ulCode];
  1740. if (usHuff == 0) // invalid code
  1741. return -1;
  1742. ulBitOff += (usHuff >> 8); // add length
  1743. usHuff &= 0xff; // get code (RRRR/SSSS)
  1744. if (usHuff == 0) // no more AC components
  1745. {
  1746. goto mcu_done;
  1747. }
  1748. if (ulBitOff >(REGISTER_WIDTH - 17)) // need to get more data
  1749. {
  1750. pBuf += (ulBitOff >> 3);
  1751. ulBitOff &= 7;
  1752. ulBits = MOTOLONG(pBuf);
  1753. }
  1754. pZig += (usHuff >> 4); // get the skip amount (RRRR)
  1755. usHuff &= 0xf; // get (SSSS) - extra length
  1756. if (pZig < pEnd2 && usHuff)
  1757. {
  1758. ulCode = ulBits << ulBitOff;
  1759. ulTemp = ~(uint32_t) (((int32_t) ulCode) >> (REGISTER_WIDTH-1)); // slide sign bit across other 63 bits
  1760. ulCode >>= (REGISTER_WIDTH - usHuff);
  1761. ulCode -= ulTemp >> (REGISTER_WIDTH - usHuff);
  1762. ucMaxACCol |= 1<<(*pZig & 7); // keep track of occupied columns
  1763. if (*pZig >= 0x20) // if more than 4 rows used in a col, mark it
  1764. ucMaxACRow |= 1<<(*pZig & 7); // keep track of the max AC term row
  1765. pMCU[*pZig] = (signed short)ulCode; // store AC coefficient (already reordered)
  1766. }
  1767. ulBitOff += usHuff; // add (SSSS) extra length
  1768. pZig++;
  1769. } // while
  1770. } // 10-bit tables
  1771. mcu_done:
  1772. pJPEG->bb.pBuf = pBuf;
  1773. pJPEG->iVLCOff = (int)(pBuf - pJPEG->ucFileBuf);
  1774. pJPEG->bb.ulBitOff = ulBitOff;
  1775. pJPEG->bb.ulBits = ulBits;
  1776. pJPEG->ucMaxACCol = ucMaxACCol;
  1777. pJPEG->ucMaxACRow = ucMaxACRow; // DEBUG
  1778. return 0;
  1779. } /* JPEGDecodeMCU() */
  1780. //
  1781. // Inverse DCT
  1782. //
  1783. static void JPEGIDCT(JPEGIMAGE *pJPEG, int iMCUOffset, int iQuantTable, int iACFlags)
  1784. {
  1785. int iRow;
  1786. unsigned char ucColMask;
  1787. int iCol;
  1788. signed int tmp6,tmp7,tmp10,tmp11,tmp12,tmp13;
  1789. signed int z5,z10,z11,z12,z13;
  1790. signed int tmp0,tmp1,tmp2,tmp3,tmp4,tmp5;
  1791. signed short *pQuant;
  1792. unsigned char *pOutput;
  1793. unsigned char ucMaxACRow, ucMaxACCol;
  1794. int16_t *pMCUSrc = &pJPEG->sMCUs[iMCUOffset];
  1795. ucMaxACRow = (unsigned char)(iACFlags >> 8);
  1796. ucMaxACCol = iACFlags & 0xff;
  1797. // my shortcut method appears to violate patent 20020080052
  1798. // but the patent is invalidated by prior art:
  1799. // http://netilium.org/~mad/dtj/DTJ/DTJK04/
  1800. pQuant = &pJPEG->sQuantTable[iQuantTable * DCTSIZE];
  1801. if (pJPEG->iOptions & JPEG_SCALE_QUARTER) // special case
  1802. {
  1803. /* Column 0 */
  1804. tmp4 = pMCUSrc[0] * pQuant[0];
  1805. tmp5 = pMCUSrc[8] * pQuant[8];
  1806. tmp0 = tmp4 + tmp5;
  1807. tmp2 = tmp4 - tmp5;
  1808. /* Column 1 */
  1809. tmp4 = pMCUSrc[1] * pQuant[1];
  1810. tmp5 = pMCUSrc[9] * pQuant[9];
  1811. tmp1 = tmp4 + tmp5;
  1812. tmp3 = tmp4 - tmp5;
  1813. /* Pass 2: process 2 rows, store into output array. */
  1814. /* Row 0 */
  1815. pOutput = (unsigned char *)pMCUSrc; // store output pixels back into MCU
  1816. pOutput[0] = ucRangeTable[(((tmp0 + tmp1)>>5) & 0x3ff)];
  1817. pOutput[1] = ucRangeTable[(((tmp0 - tmp1)>>5) & 0x3ff)];
  1818. /* Row 1 */
  1819. pOutput[2] = ucRangeTable[(((tmp2 + tmp3)>>5) & 0x3ff)];
  1820. pOutput[3] = ucRangeTable[(((tmp2 - tmp3)>>5) & 0x3ff)];
  1821. return;
  1822. }
  1823. // do columns first
  1824. ucColMask = ucMaxACCol | 1; // column 0 must always be calculated
  1825. for (iCol = 0; iCol < 8 && ucColMask; iCol++)
  1826. {
  1827. if (ucColMask & (1<<iCol)) // column has data in it
  1828. {
  1829. ucColMask &= ~(1<<iCol); // unmark this col after use
  1830. if (!(ucMaxACRow & (1<<iCol))) // simpler calculations if only half populated
  1831. {
  1832. // even part
  1833. tmp10 = pMCUSrc[iCol] * pQuant[iCol];
  1834. tmp1 = pMCUSrc[iCol+16] * pQuant[iCol+16]; // get 2nd row
  1835. tmp12 = ((tmp1*106)>>8); // used to be 362 - 1 (256)
  1836. tmp0 = tmp10 + tmp1;
  1837. tmp3 = tmp10 - tmp1;
  1838. tmp1 = tmp10 + tmp12;
  1839. tmp2 = tmp10 - tmp12;
  1840. // odd part
  1841. tmp4 = pMCUSrc[iCol+8] * pQuant[iCol+8]; // get 1st row
  1842. tmp5 = pMCUSrc[iCol+24];
  1843. if (tmp5) // this value is usually 0
  1844. {
  1845. tmp5 *= pQuant[iCol+24]; // get 3rd row
  1846. tmp7 = tmp4 + tmp5;
  1847. tmp11 = (((tmp4 - tmp5) * 362) >> 8); // 362>>8 = 1.414213562
  1848. z5 = (((tmp4-tmp5) * 473) >> 8); // 473>>8 = 1.8477
  1849. tmp12 = ((-tmp5 * -669)>>8) + z5; // -669>>8 = -2.6131259
  1850. tmp6 = tmp12 - tmp7;
  1851. tmp5 = tmp11 - tmp6;
  1852. tmp10 = ((tmp4 * 277)>>8) - z5; // 277>>8 = 1.08239
  1853. tmp4 = tmp10 + tmp5;
  1854. }
  1855. else // simpler case when we only have 1 odd row to calculate
  1856. {
  1857. tmp7 = tmp4;
  1858. tmp5 = (145*tmp4) >> 8;
  1859. tmp6 = (217*tmp4) >> 8;
  1860. tmp4 = (-51*tmp4) >> 8;
  1861. }
  1862. pMCUSrc[iCol] = (short)(tmp0 + tmp7); // row0
  1863. pMCUSrc[iCol+8] = (short)(tmp1 + tmp6); // row 1
  1864. pMCUSrc[iCol+16] = (short)(tmp2 + tmp5); // row 2
  1865. pMCUSrc[iCol+24] = (short)(tmp3 - tmp4); // row 3
  1866. pMCUSrc[iCol+32] = (short)(tmp3 + tmp4); // row 4
  1867. pMCUSrc[iCol+40] = (short)(tmp2 - tmp5); // row 5
  1868. pMCUSrc[iCol+48] = (short)(tmp1 - tmp6); // row 6
  1869. pMCUSrc[iCol+56] = (short)(tmp0 - tmp7); // row 7
  1870. }
  1871. else // need to do full column calculation
  1872. {
  1873. // even part
  1874. tmp0 = pMCUSrc[iCol] * pQuant[iCol];
  1875. tmp2 = pMCUSrc[iCol+32]; // get 4th row
  1876. if (tmp2) // 4th row is most likely 0
  1877. {
  1878. tmp2 = tmp2 * pQuant[iCol+32];
  1879. tmp10 = tmp0 + tmp2;
  1880. tmp11 = tmp0 - tmp2;
  1881. }
  1882. else
  1883. {
  1884. tmp10 = tmp11 = tmp0;
  1885. }
  1886. tmp1 = pMCUSrc[iCol+16] * pQuant[iCol+16]; // get 2nd row
  1887. tmp3 = pMCUSrc[iCol+48]; // get 6th row
  1888. if (tmp3) // 6th row is most likely 0
  1889. {
  1890. tmp3 = tmp3 * pQuant[iCol+48];
  1891. tmp13 = tmp1 + tmp3;
  1892. tmp12 = (((tmp1 - tmp3) * 362) >> 8) - tmp13; // 362>>8 = 1.414213562
  1893. }
  1894. else
  1895. {
  1896. tmp13 = tmp1;
  1897. tmp12 = ((tmp1*362)>>8) - tmp1;
  1898. }
  1899. tmp0 = tmp10 + tmp13;
  1900. tmp3 = tmp10 - tmp13;
  1901. tmp1 = tmp11 + tmp12;
  1902. tmp2 = tmp11 - tmp12;
  1903. // odd part
  1904. tmp5 = pMCUSrc[iCol+24] * pQuant[iCol+24]; // get 3rd row
  1905. tmp6 = pMCUSrc[iCol+40]; // get 5th row
  1906. if (tmp6) // very likely that row 5 = 0
  1907. {
  1908. tmp6 = tmp6 * pQuant[iCol+40];
  1909. z13 = tmp6 + tmp5;
  1910. z10 = tmp6 - tmp5;
  1911. }
  1912. else
  1913. {
  1914. z13 = tmp5;
  1915. z10 = -tmp5;
  1916. }
  1917. tmp4 = pMCUSrc[iCol+8] * pQuant[iCol+8]; // get 1st row
  1918. tmp7 = pMCUSrc[iCol+56]; // get 7th row
  1919. if (tmp7) // very likely that row 7 = 0
  1920. {
  1921. tmp7 = tmp7 * pQuant[iCol+56];
  1922. z11 = tmp4 + tmp7;
  1923. z12 = tmp4 - tmp7;
  1924. }
  1925. else
  1926. {
  1927. z11 = z12 = tmp4;
  1928. }
  1929. tmp7 = z11 + z13;
  1930. tmp11 = (((z11 - z13) * 362) >> 8); // 362>>8 = 1.414213562
  1931. z5 = (((z10 + z12) * 473) >> 8); // 473>>8 = 1.8477
  1932. tmp12 = ((z10 * -669)>>8) + z5; // -669>>8 = -2.6131259
  1933. tmp6 = tmp12 - tmp7;
  1934. tmp5 = tmp11 - tmp6;
  1935. tmp10 = ((z12 * 277)>>8) - z5; // 277>>8 = 1.08239
  1936. tmp4 = tmp10 + tmp5;
  1937. pMCUSrc[iCol] = (short)(tmp0 + tmp7); // row0
  1938. pMCUSrc[iCol+8] = (short)(tmp1 + tmp6); // row 1
  1939. pMCUSrc[iCol+16] = (short)(tmp2 + tmp5); // row 2
  1940. pMCUSrc[iCol+24] = (short)(tmp3 - tmp4); // row 3
  1941. pMCUSrc[iCol+32] = (short)(tmp3 + tmp4); // row 4
  1942. pMCUSrc[iCol+40] = (short)(tmp2 - tmp5); // row 5
  1943. pMCUSrc[iCol+48] = (short)(tmp1 - tmp6); // row 6
  1944. pMCUSrc[iCol+56] = (short)(tmp0 - tmp7); // row 7
  1945. } // full calculation needed
  1946. } // if column has data in it
  1947. } // for each column
  1948. // now do rows
  1949. pOutput = (unsigned char *)pMCUSrc; // store output pixels back into MCU
  1950. for (iRow=0; iRow<64; iRow+=8) // all rows must be calculated
  1951. {
  1952. // even part
  1953. if (ucMaxACCol < 0x10) // quick and dirty calculation (right 4 columns are all 0's)
  1954. {
  1955. if (ucMaxACCol < 0x04) // very likely case (1 or 2 columns occupied)
  1956. {
  1957. // even part
  1958. tmp0 = tmp1 = tmp2 = tmp3 = pMCUSrc[iRow+0];
  1959. // odd part
  1960. tmp7 = pMCUSrc[iRow+1];
  1961. tmp6 = (tmp7 * 217)>>8; // * 0.8477
  1962. tmp5 = (tmp7 * 145)>>8; // * 0.5663
  1963. tmp4 = -((tmp7 * 51)>>8); // * -0.199
  1964. }
  1965. else
  1966. {
  1967. tmp10 = pMCUSrc[iRow+0];
  1968. tmp13 = pMCUSrc[iRow+2];
  1969. tmp12 = ((tmp13 * 106)>>8); // 2-6 * 1.414
  1970. tmp0 = tmp10 + tmp13;
  1971. tmp3 = tmp10 - tmp13;
  1972. tmp1 = tmp10 + tmp12;
  1973. tmp2 = tmp10 - tmp12;
  1974. // odd part
  1975. z13 = pMCUSrc[iRow+3];
  1976. z11 = pMCUSrc[iRow+1];
  1977. tmp7 = z11 + z13;
  1978. tmp11 = ((z11 - z13)*362)>>8; // * 1.414
  1979. z5 = ((z11 - z13)*473)>>8; // * 1.8477
  1980. tmp10 = ((z11*277)>>8) - z5; // * 1.08239
  1981. tmp12 = ((z13*669)>>8) + z5; // * 2.61312
  1982. tmp6 = tmp12 - tmp7;
  1983. tmp5 = tmp11 - tmp6;
  1984. tmp4 = tmp10 + tmp5;
  1985. }
  1986. }
  1987. else // need to do the full calculation
  1988. {
  1989. tmp10 = pMCUSrc[iRow+0] + pMCUSrc[iRow+4];
  1990. tmp11 = pMCUSrc[iRow+0] - pMCUSrc[iRow+4];
  1991. tmp13 = pMCUSrc[iRow+2] + pMCUSrc[iRow+6];
  1992. tmp12 = (((pMCUSrc[iRow+2] - pMCUSrc[iRow+6]) * 362)>>8) - tmp13; // 2-6 * 1.414
  1993. tmp0 = tmp10 + tmp13;
  1994. tmp3 = tmp10 - tmp13;
  1995. tmp1 = tmp11 + tmp12;
  1996. tmp2 = tmp11 - tmp12;
  1997. // odd part
  1998. z13 = pMCUSrc[iRow+5] + pMCUSrc[iRow+3];
  1999. z10 = pMCUSrc[iRow+5] - pMCUSrc[iRow+3];
  2000. z11 = pMCUSrc[iRow+1] + pMCUSrc[iRow+7];
  2001. z12 = pMCUSrc[iRow+1] - pMCUSrc[iRow+7];
  2002. tmp7 = z11 + z13;
  2003. tmp11 = ((z11 - z13)*362)>>8; // * 1.414
  2004. z5 = ((z10 + z12)*473)>>8; // * 1.8477
  2005. tmp10 = ((z12*277)>>8) - z5; // * 1.08239
  2006. tmp12 = ((z10*-669)>>8) + z5; // * 2.61312
  2007. tmp6 = tmp12 - tmp7;
  2008. tmp5 = tmp11 - tmp6;
  2009. tmp4 = tmp10 + tmp5;
  2010. }
  2011. // final output stage - scale down and range limit
  2012. pOutput[0] = ucRangeTable[(((tmp0 + tmp7)>>5) & 0x3ff)];
  2013. pOutput[1] = ucRangeTable[(((tmp1 + tmp6)>>5) & 0x3ff)];
  2014. pOutput[2] = ucRangeTable[(((tmp2 + tmp5)>>5) & 0x3ff)];
  2015. pOutput[3] = ucRangeTable[(((tmp3 - tmp4)>>5) & 0x3ff)];
  2016. pOutput[4] = ucRangeTable[(((tmp3 + tmp4)>>5) & 0x3ff)];
  2017. pOutput[5] = ucRangeTable[(((tmp2 - tmp5)>>5) & 0x3ff)];
  2018. pOutput[6] = ucRangeTable[(((tmp1 - tmp6)>>5) & 0x3ff)];
  2019. pOutput[7] = ucRangeTable[(((tmp0 - tmp7)>>5) & 0x3ff)];
  2020. pOutput += 8;
  2021. } // for each row
  2022. } /* JPEGIDCT() */
  2023. static void JPEGPutMCU8BitGray(JPEGIMAGE *pJPEG, int x, int iPitch)
  2024. {
  2025. int i, j, xcount, ycount;
  2026. uint8_t *pDest, *pSrc = (uint8_t *)&pJPEG->sMCUs[0];
  2027. if (pJPEG->pDitherBuffer)
  2028. pDest = &pJPEG->pDitherBuffer[x];
  2029. else
  2030. pDest = (uint8_t *)&pJPEG->usPixels[x/2];
  2031. if (pJPEG->ucSubSample <= 0x11) // single Y
  2032. {
  2033. if (pJPEG->iOptions & JPEG_SCALE_HALF) // special handling of 1/2 size (pixel averaging)
  2034. {
  2035. int pix;
  2036. for (i=0; i<4; i++)
  2037. {
  2038. for (j=0; j<4; j++)
  2039. {
  2040. pix = (pSrc[0] + pSrc[1] + pSrc[8] + pSrc[9] + 2) >> 2; // average 2x2 block
  2041. pDest[j] = (uint8_t)pix;
  2042. pSrc += 2;
  2043. }
  2044. pSrc += 8; // skip extra line
  2045. pDest += iPitch;
  2046. }
  2047. return;
  2048. }
  2049. xcount = ycount = 8; // debug
  2050. if (pJPEG->iOptions & JPEG_SCALE_QUARTER)
  2051. xcount = ycount = 2;
  2052. else if (pJPEG->iOptions & JPEG_SCALE_EIGHTH)
  2053. xcount = ycount = 1;
  2054. for (i=0; i<ycount; i++) // do up to 8 rows
  2055. {
  2056. for (j=0; j<xcount; j++)
  2057. *pDest++ = *pSrc++;
  2058. pDest -= xcount;
  2059. pDest += iPitch; // next line
  2060. }
  2061. return;
  2062. } // single Y source
  2063. if (pJPEG->ucSubSample == 0x21) // stacked horizontally
  2064. {
  2065. if (pJPEG->iOptions & JPEG_SCALE_EIGHTH)
  2066. {
  2067. // only 2 pixels emitted
  2068. pDest[0] = pSrc[0];
  2069. pDest[1] = pSrc[128];
  2070. return;
  2071. } /* 1/8 */
  2072. if (pJPEG->iOptions & JPEG_SCALE_HALF)
  2073. {
  2074. for (i=0; i<4; i++)
  2075. {
  2076. for (j=0; j<4; j++)
  2077. {
  2078. int pix;
  2079. pix = (pSrc[j*2] + pSrc[j*2+1] + pSrc[j*2 + 8] + pSrc[j*2 + 9] + 2) >> 2;
  2080. pDest[j] = (uint8_t)pix;
  2081. pix = (pSrc[j*2 + 128] + pSrc[j*2+129] + pSrc[j*2 + 136] + pSrc[j*2 + 137] + 2) >> 2;
  2082. pDest[j+4] = (uint8_t)pix;
  2083. }
  2084. pSrc += 16;
  2085. pDest += iPitch;
  2086. }
  2087. return;
  2088. }
  2089. if (pJPEG->iOptions & JPEG_SCALE_QUARTER)
  2090. {
  2091. // each MCU contributes a 2x2 block
  2092. pDest[0] = pSrc[0]; // Y0
  2093. pDest[1] = pSrc[1];
  2094. pDest[iPitch] = pSrc[2];
  2095. pDest[iPitch+1] = pSrc[3];
  2096. pDest[2] = pSrc[128]; // Y`
  2097. pDest[3] = pSrc[129];
  2098. pDest[iPitch+2] = pSrc[130];
  2099. pDest[iPitch+3] = pSrc[131];
  2100. return;
  2101. }
  2102. for (i=0; i<8; i++)
  2103. {
  2104. for (j=0; j<8; j++)
  2105. {
  2106. pDest[j] = pSrc[j];
  2107. pDest[j+8] = pSrc[128 + j];
  2108. }
  2109. pSrc += 8;
  2110. pDest += iPitch;
  2111. }
  2112. } // 0x21
  2113. if (pJPEG->ucSubSample == 0x12) // stacked vertically
  2114. {
  2115. if (pJPEG->iOptions & JPEG_SCALE_EIGHTH)
  2116. {
  2117. // only 2 pixels emitted
  2118. pDest[0] = pSrc[0];
  2119. pDest[iPitch] = pSrc[128];
  2120. return;
  2121. } /* 1/8 */
  2122. if (pJPEG->iOptions & JPEG_SCALE_HALF)
  2123. {
  2124. for (i=0; i<4; i++)
  2125. {
  2126. for (j=0; j<4; j++)
  2127. {
  2128. int pix;
  2129. pix = (pSrc[j*2] + pSrc[j*2+1] + pSrc[j*2 + 8] + pSrc[j*2 + 9] + 2) >> 2;
  2130. pDest[j] = (uint8_t)pix;
  2131. pix = (pSrc[j*2 + 128] + pSrc[j*2+129] + pSrc[j*2 + 136] + pSrc[j*2 + 137] + 2) >> 2;
  2132. pDest[4*iPitch+j] = (uint8_t)pix;
  2133. }
  2134. pSrc += 16;
  2135. pDest += iPitch;
  2136. }
  2137. return;
  2138. }
  2139. if (pJPEG->iOptions & JPEG_SCALE_QUARTER)
  2140. {
  2141. // each MCU contributes a 2x2 block
  2142. pDest[0] = pSrc[0]; // Y0
  2143. pDest[1] = pSrc[1];
  2144. pDest[iPitch] = pSrc[2];
  2145. pDest[iPitch+1] = pSrc[3];
  2146. pDest[iPitch*2] = pSrc[128]; // Y`
  2147. pDest[iPitch*2+1] = pSrc[129];
  2148. pDest[iPitch*3] = pSrc[130];
  2149. pDest[iPitch*3+1] = pSrc[131];
  2150. return;
  2151. }
  2152. for (i=0; i<8; i++)
  2153. {
  2154. for (j=0; j<8; j++)
  2155. {
  2156. pDest[j] = pSrc[j];
  2157. pDest[8*iPitch + j] = pSrc[128 + j];
  2158. }
  2159. pSrc += 8;
  2160. pDest += iPitch;
  2161. }
  2162. } // 0x12
  2163. if (pJPEG->ucSubSample == 0x22)
  2164. {
  2165. if (pJPEG->iOptions & JPEG_SCALE_EIGHTH)
  2166. {
  2167. // each MCU contributes 1 pixel
  2168. pDest[0] = pSrc[0]; // Y0
  2169. pDest[1] = pSrc[128]; // Y1
  2170. pDest[iPitch] = pSrc[256]; // Y2
  2171. pDest[iPitch + 1] = pSrc[384]; // Y3
  2172. return;
  2173. }
  2174. if (pJPEG->iOptions & JPEG_SCALE_QUARTER)
  2175. {
  2176. // each MCU contributes 2x2 pixels
  2177. pDest[0] = pSrc[0]; // Y0
  2178. pDest[1] = pSrc[1];
  2179. pDest[iPitch] = pSrc[2];
  2180. pDest[iPitch+1] = pSrc[3];
  2181. pDest[2] = pSrc[128]; // Y1
  2182. pDest[3] = pSrc[129];
  2183. pDest[iPitch+2] = pSrc[130];
  2184. pDest[iPitch+3] = pSrc[131];
  2185. pDest[iPitch*2] = pSrc[256]; // Y2
  2186. pDest[iPitch*2+1] = pSrc[257];
  2187. pDest[iPitch*3] = pSrc[258];
  2188. pDest[iPitch*3+1] = pSrc[259];
  2189. pDest[iPitch*2+2] = pSrc[384]; // Y3
  2190. pDest[iPitch*2+3] = pSrc[385];
  2191. pDest[iPitch*3+2] = pSrc[386];
  2192. pDest[iPitch*3+3] = pSrc[387];
  2193. return;
  2194. }
  2195. if (pJPEG->iOptions & JPEG_SCALE_HALF)
  2196. {
  2197. for (i=0; i<4; i++)
  2198. {
  2199. for (j=0; j<4; j++)
  2200. {
  2201. int pix;
  2202. pix = (pSrc[j*2] + pSrc[j*2+1] + pSrc[j*2 + 8] + pSrc[j*2 + 9] + 2) >> 2;
  2203. pDest[j] = (uint8_t)pix; // Y0
  2204. pix = (pSrc[j*2+128] + pSrc[j*2+129] + pSrc[j*2 + 136] + pSrc[j*2 + 137] + 2) >> 2;
  2205. pDest[j+4] = (uint8_t)pix; // Y1
  2206. pix = (pSrc[j*2+256] + pSrc[j*2+257] + pSrc[j*2 + 264] + pSrc[j*2 + 265] + 2) >> 2;
  2207. pDest[iPitch*4 + j] = (uint8_t)pix; // Y2
  2208. pix = (pSrc[j*2+384] + pSrc[j*2+385] + pSrc[j*2 + 392] + pSrc[j*2 + 393] + 2) >> 2;
  2209. pDest[iPitch*4 + j + 4] = (uint8_t)pix; // Y3
  2210. }
  2211. pSrc += 16;
  2212. pDest += iPitch;
  2213. }
  2214. return;
  2215. }
  2216. for (i=0; i<8; i++)
  2217. {
  2218. for (j=0; j<8; j++)
  2219. {
  2220. pDest[j] = pSrc[j]; // Y0
  2221. pDest[j+8] = pSrc[j+128]; // Y1
  2222. pDest[iPitch*8 + j] = pSrc[j+256]; // Y2
  2223. pDest[iPitch*8 + j + 8] = pSrc[j + 384]; // Y3
  2224. }
  2225. pSrc += 8;
  2226. pDest += iPitch;
  2227. }
  2228. } // 0x22
  2229. } /* JPEGMPutMCU8BitGray() */
  2230. static void JPEGPutMCUGray(JPEGIMAGE *pJPEG, int x, int iPitch)
  2231. {
  2232. uint16_t *usDest = (uint16_t *)&pJPEG->usPixels[x];
  2233. int i, j, xcount, ycount;
  2234. uint8_t *pSrc = (uint8_t *)&pJPEG->sMCUs[0];
  2235. if (pJPEG->iOptions & JPEG_SCALE_HALF) // special handling of 1/2 size (pixel averaging)
  2236. {
  2237. int pix;
  2238. for (i=0; i<4; i++)
  2239. {
  2240. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2241. {
  2242. for (j=0; j<4; j++)
  2243. {
  2244. pix = (pSrc[0] + pSrc[1] + pSrc[8] + pSrc[9] + 2) >> 2; // average 2x2 block
  2245. usDest[j] = usGrayTo565[pix];
  2246. pSrc += 2;
  2247. }
  2248. }
  2249. else
  2250. {
  2251. for (j=0; j<4; j++)
  2252. {
  2253. pix = (pSrc[0] + pSrc[1] + pSrc[8] + pSrc[9] + 2) >> 2; // average 2x2 block
  2254. usDest[j] = __builtin_bswap16(usGrayTo565[pix]);
  2255. pSrc += 2;
  2256. }
  2257. }
  2258. pSrc += 8; // skip extra line
  2259. usDest += iPitch;
  2260. }
  2261. return;
  2262. }
  2263. xcount = ycount = 8; // debug
  2264. if (pJPEG->iOptions & JPEG_SCALE_QUARTER)
  2265. xcount = ycount = 2;
  2266. else if (pJPEG->iOptions & JPEG_SCALE_EIGHTH)
  2267. xcount = ycount = 1;
  2268. for (i=0; i<ycount; i++) // do up to 8 rows
  2269. {
  2270. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2271. {
  2272. for (j=0; j<xcount; j++)
  2273. *usDest++ = usGrayTo565[*pSrc++];
  2274. }
  2275. else
  2276. {
  2277. for (j=0; j<xcount; j++)
  2278. *usDest++ = __builtin_bswap16(usGrayTo565[*pSrc++]);
  2279. }
  2280. usDest -= xcount;
  2281. usDest += iPitch; // next line
  2282. }
  2283. } /* JPEGPutMCUGray() */
  2284. static void JPEGPixelLE(uint16_t *pDest, int iY, int iCb, int iCr)
  2285. {
  2286. //
  2287. // Cortex-M4/M7 has some SIMD instructions which can shave a few cycles
  2288. // off of this function (e.g. Teensy, Arduino Nano 33 BLE, Portenta, etc)
  2289. //
  2290. #ifdef HAS_SIMD
  2291. uint32_t ulPixel;
  2292. uint32_t ulCbCr = (iCb | (iCr << 16));
  2293. uint32_t ulTmp = -1409 | (-2925 << 16); // for green calc
  2294. ulCbCr = __SSUB16(ulCbCr, 0x00800080); // dual 16-bit subtraction
  2295. ulPixel = __SMLAD(ulCbCr, ulTmp, iY) >> 14; // G
  2296. ulPixel = __USAT16(ulPixel, 6) << 5; // range limit to 6 bits
  2297. ulTmp = __SMLAD(7258, ulCbCr, iY) >> 15; // Blue
  2298. ulTmp = __USAT16(ulTmp, 5); // range limit to 5 bits
  2299. ulPixel |= ulTmp; // now we have G + B
  2300. ulTmp = __SMLAD(5742, ulCbCr >> 16, iY) >> 15; // Red
  2301. ulTmp = __USAT16(ulTmp, 5); // range limit to 5 bits
  2302. ulPixel |= (ulTmp << 11); // now we have R + G + B
  2303. pDest[0] = (uint16_t)ulPixel;
  2304. #else
  2305. int iCBB, iCBG, iCRG, iCRR;
  2306. unsigned short usPixel;
  2307. iCBB = 7258 * (iCb-0x80);
  2308. iCBG = -1409 * (iCb-0x80);
  2309. iCRG = -2925 * (iCr-0x80);
  2310. iCRR = 5742 * (iCr-0x80);
  2311. usPixel = usRangeTableB[((iCBB + iY) >> 12) & 0x3ff]; // blue pixel
  2312. usPixel |= usRangeTableG[((iCBG + iCRG + iY) >> 12) & 0x3ff]; // green pixel
  2313. usPixel |= usRangeTableR[((iCRR + iY) >> 12) & 0x3ff]; // red pixel
  2314. pDest[0] = usPixel;
  2315. #endif
  2316. } /* JPEGPixelLE() */
  2317. static void JPEGPixelBE(uint16_t *pDest, int iY, int iCb, int iCr)
  2318. {
  2319. int iCBB, iCBG, iCRG, iCRR;
  2320. unsigned short usPixel;
  2321. iCBB = 7258 * (iCb-0x80);
  2322. iCBG = -1409 * (iCb-0x80);
  2323. iCRG = -2925 * (iCr-0x80);
  2324. iCRR = 5742 * (iCr-0x80);
  2325. usPixel = usRangeTableB[((iCBB + iY) >> 12) & 0x3ff]; // blue pixel
  2326. usPixel |= usRangeTableG[((iCBG + iCRG + iY) >> 12) & 0x3ff]; // green pixel
  2327. usPixel |= usRangeTableR[((iCRR + iY) >> 12) & 0x3ff]; // red pixel
  2328. pDest[0] = __builtin_bswap16(usPixel);
  2329. } /* JPEGPixelBE() */
  2330. static void JPEGPixel2LE(uint16_t *pDest, int iY1, int iY2, int iCb, int iCr)
  2331. {
  2332. uint32_t ulPixel1, ulPixel2;
  2333. //
  2334. // Cortex-M4/M7 has some SIMD instructions which can shave a few cycles
  2335. // off of this function (e.g. Teensy, Arduino Nano 33 BLE, Portenta, etc)
  2336. //
  2337. #ifdef HAS_SIMD
  2338. uint32_t ulCbCr = (iCb | (iCr << 16));
  2339. uint32_t ulTmp2, ulTmp = -1409 | (-2925 << 16); // for green calc
  2340. ulCbCr = __SSUB16(ulCbCr, 0x00800080); // dual 16-bit subtraction
  2341. ulPixel1 = __SMLAD(ulCbCr, ulTmp, iY1) >> 14; // G for pixel 1
  2342. ulPixel2 = __SMLAD(ulCbCr, ulTmp, iY2) >> 14; // G for pixel 2
  2343. ulPixel1 |= (ulPixel2 << 16);
  2344. ulPixel1 = __USAT16(ulPixel1, 6) << 5; // range limit both to 6 bits
  2345. ulTmp = __SMLAD(7258, ulCbCr, iY1) >> 15; // Blue 1
  2346. ulTmp2 = __SMLAD(7258, ulCbCr, iY2) >> 15; // Blue 2
  2347. ulTmp = __USAT16(ulTmp | (ulTmp2 << 16), 5); // range limit both to 5 bits
  2348. ulPixel1 |= ulTmp; // now we have G + B
  2349. ulTmp = __SMLAD(5742, ulCbCr >> 16, iY1) >> 15; // Red 1
  2350. ulTmp2 = __SMLAD(5742, ulCbCr >> 16, iY2) >> 15; // Red 2
  2351. ulTmp = __USAT16(ulTmp | (ulTmp2 << 16), 5); // range limit both to 5 bits
  2352. ulPixel1 |= (ulTmp << 11); // now we have R + G + B
  2353. *(uint32_t *)&pDest[0] = ulPixel1;
  2354. #else
  2355. int iCBB, iCBG, iCRG, iCRR;
  2356. iCBB = 7258 * (iCb-0x80);
  2357. iCBG = -1409 * (iCb-0x80);
  2358. iCRG = -2925 * (iCr-0x80);
  2359. iCRR = 5742 * (iCr-0x80);
  2360. ulPixel1 = usRangeTableB[((iCBB + iY1) >> 12) & 0x3ff]; // blue pixel
  2361. ulPixel1 |= usRangeTableG[((iCBG + iCRG + iY1) >> 12) & 0x3ff]; // green pixel
  2362. ulPixel1 |= usRangeTableR[((iCRR + iY1) >> 12) & 0x3ff]; // red pixel
  2363. ulPixel2 = usRangeTableB[((iCBB + iY2) >> 12) & 0x3ff]; // blue pixel
  2364. ulPixel2 |= usRangeTableG[((iCBG + iCRG + iY2) >> 12) & 0x3ff]; // green pixel
  2365. ulPixel2 |= usRangeTableR[((iCRR + iY2) >> 12) & 0x3ff]; // red pixel
  2366. *(uint32_t *)&pDest[0] = (ulPixel1 | (ulPixel2<<16));
  2367. #endif
  2368. } /* JPEGPixel2LE() */
  2369. static void JPEGPixel2BE(uint16_t *pDest, int32_t iY1, int32_t iY2, int32_t iCb, int32_t iCr)
  2370. {
  2371. int32_t iCBB, iCBG, iCRG, iCRR;
  2372. uint32_t ulPixel1, ulPixel2;
  2373. iCBB = 7258L * (iCb-0x80);
  2374. iCBG = -1409L * (iCb-0x80);
  2375. iCRG = -2925L * (iCr-0x80);
  2376. iCRR = 5742L * (iCr-0x80);
  2377. ulPixel1 = usRangeTableB[((iCBB + iY1) >> 12) & 0x3ff]; // blue pixel
  2378. ulPixel1 |= usRangeTableG[((iCBG + iCRG + iY1) >> 12) & 0x3ff]; // green pixel
  2379. ulPixel1 |= usRangeTableR[((iCRR + iY1) >> 12) & 0x3ff]; // red pixel
  2380. ulPixel2 = usRangeTableB[((iCBB + iY2) >> 12) & 0x3ff]; // blue pixel
  2381. ulPixel2 |= usRangeTableG[((iCBG + iCRG + iY2) >> 12) & 0x3ff]; // green pixel
  2382. ulPixel2 |= usRangeTableR[((iCRR + iY2) >> 12) & 0x3ff]; // red pixel
  2383. *(uint32_t *)&pDest[0] = __builtin_bswap16(ulPixel1) | ((uint32_t)__builtin_bswap16(ulPixel2)<<16);
  2384. } /* JPEGPixel2BE() */
  2385. static void JPEGPutMCU11(JPEGIMAGE *pJPEG, int x, int iPitch)
  2386. {
  2387. int iCr, iCb;
  2388. signed int Y;
  2389. int iCol;
  2390. int iRow;
  2391. uint8_t *pY, *pCr, *pCb;
  2392. uint16_t *pOutput = &pJPEG->usPixels[x];
  2393. pY = (unsigned char *)&pJPEG->sMCUs[0*DCTSIZE];
  2394. pCb = (unsigned char *)&pJPEG->sMCUs[1*DCTSIZE];
  2395. pCr = (unsigned char *)&pJPEG->sMCUs[2*DCTSIZE];
  2396. if (pJPEG->iOptions & JPEG_SCALE_HALF)
  2397. {
  2398. for (iRow=0; iRow<4; iRow++) // up to 8 rows to do
  2399. {
  2400. for (iCol=0; iCol<4; iCol++) // up to 4x2 cols to do
  2401. {
  2402. iCr = (pCr[0] + pCr[1] + pCr[8] + pCr[9] + 2) >> 2;
  2403. iCb = (pCb[0] + pCb[1] + pCb[8] + pCb[9] + 2) >> 2;
  2404. Y = (pY[0] + pY[1] + pY[8] + pY[9]) << 10;
  2405. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2406. JPEGPixelLE(pOutput+iCol, Y, iCb, iCr);
  2407. else
  2408. JPEGPixelBE(pOutput+iCol, Y, iCb, iCr);
  2409. pCr += 2;
  2410. pCb += 2;
  2411. pY += 2;
  2412. } // for col
  2413. pCr += 8;
  2414. pCb += 8;
  2415. pY += 8;
  2416. pOutput += iPitch;
  2417. } // for row
  2418. return;
  2419. }
  2420. if (pJPEG->iOptions & JPEG_SCALE_EIGHTH) // special case for 1/8 scaling
  2421. {
  2422. // only 4 pixels to draw, so no looping needed
  2423. iCr = pCr[0];
  2424. iCb = pCb[0];
  2425. Y = (int)(pY[0]) << 12;
  2426. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2427. JPEGPixelLE(pOutput, Y, iCb, iCr);
  2428. else
  2429. JPEGPixelBE(pOutput, Y, iCb, iCr);
  2430. return;
  2431. }
  2432. if (pJPEG->iOptions & JPEG_SCALE_QUARTER) // special case for 1/4 scaling
  2433. {
  2434. // only 4 pixels to draw, so no looping needed
  2435. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2436. {
  2437. iCr = *pCr++;
  2438. iCb = *pCb++;
  2439. Y = (int)(*pY++) << 12;
  2440. JPEGPixelLE(pOutput, Y, iCb, iCr);
  2441. iCr = *pCr++;
  2442. iCb = *pCb++;
  2443. Y = (int)(*pY++) << 12;
  2444. JPEGPixelLE(pOutput+1, Y, iCb, iCr);
  2445. iCr = *pCr++;
  2446. iCb = *pCb++;
  2447. Y = (int)(*pY++) << 12;
  2448. JPEGPixelLE(pOutput+iPitch, Y, iCb, iCr);
  2449. iCr = *pCr++;
  2450. iCb = *pCb++;
  2451. Y = (int)(*pY++) << 12;
  2452. JPEGPixelLE(pOutput+1+iPitch, Y, iCb, iCr);
  2453. }
  2454. else
  2455. {
  2456. iCr = *pCr++;
  2457. iCb = *pCb++;
  2458. Y = (int)(*pY++) << 12;
  2459. JPEGPixelBE(pOutput, Y, iCb, iCr);
  2460. iCr = *pCr++;
  2461. iCb = *pCb++;
  2462. Y = (int)(*pY++) << 12;
  2463. JPEGPixelBE(pOutput+1, Y, iCb, iCr);
  2464. iCr = *pCr++;
  2465. iCb = *pCb++;
  2466. Y = (int)(*pY++) << 12;
  2467. JPEGPixelBE(pOutput+iPitch, Y, iCb, iCr);
  2468. iCr = *pCr++;
  2469. iCb = *pCb++;
  2470. Y = (int)(*pY++) << 12;
  2471. JPEGPixelBE(pOutput+1+iPitch, Y, iCb, iCr);
  2472. }
  2473. return;
  2474. }
  2475. for (iRow=0; iRow<8; iRow++) // up to 8 rows to do
  2476. {
  2477. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2478. {
  2479. for (iCol=0; iCol<8; iCol++) // up to 4x2 cols to do
  2480. {
  2481. iCr = *pCr++;
  2482. iCb = *pCb++;
  2483. Y = (int)(*pY++) << 12;
  2484. JPEGPixelLE(pOutput+iCol, Y, iCb, iCr);
  2485. } // for col
  2486. }
  2487. else
  2488. {
  2489. for (iCol=0; iCol<8; iCol++) // up to 4x2 cols to do
  2490. {
  2491. iCr = *pCr++;
  2492. iCb = *pCb++;
  2493. Y = (int)(*pY++) << 12;
  2494. JPEGPixelBE(pOutput+iCol, Y, iCb, iCr);
  2495. } // for col
  2496. }
  2497. pOutput += iPitch;
  2498. } // for row
  2499. } /* JPEGPutMCU11() */
  2500. static void JPEGPutMCU22(JPEGIMAGE *pJPEG, int x, int iPitch)
  2501. {
  2502. uint32_t Cr,Cb;
  2503. signed int Y1, Y2, Y3, Y4;
  2504. int iRow, iCol, iXCount1, iXCount2, iYCount;
  2505. unsigned char *pY, *pCr, *pCb;
  2506. int bUseOdd1, bUseOdd2; // special case where 24bpp odd sized image can clobber first column
  2507. uint16_t *pOutput = &pJPEG->usPixels[x];
  2508. pY = (unsigned char *)&pJPEG->sMCUs[0*DCTSIZE];
  2509. pCb = (unsigned char *)&pJPEG->sMCUs[4*DCTSIZE];
  2510. pCr = (unsigned char *)&pJPEG->sMCUs[5*DCTSIZE];
  2511. if (pJPEG->iOptions & JPEG_SCALE_HALF) // special handling of 1/2 size (pixel averaging)
  2512. {
  2513. for (iRow=0; iRow<4; iRow++) // 16x16 becomes 8x8 of 2x2 pixels
  2514. {
  2515. for (iCol=0; iCol<4; iCol++)
  2516. {
  2517. Y1 = (pY[iCol*2] + pY[iCol*2+1] + pY[iCol*2+8] + pY[iCol*2+9]) << 10;
  2518. Cb = pCb[iCol];
  2519. Cr = pCr[iCol];
  2520. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2521. JPEGPixelLE(pOutput+iCol, Y1, Cb, Cr); // top left
  2522. else
  2523. JPEGPixelBE(pOutput+iCol, Y1, Cb, Cr);
  2524. Y1 = (pY[iCol*2+(DCTSIZE*2)] + pY[iCol*2+1+(DCTSIZE*2)] + pY[iCol*2+8+(DCTSIZE*2)] + pY[iCol*2+9+(DCTSIZE*2)]) << 10;
  2525. Cb = pCb[iCol+4];
  2526. Cr = pCr[iCol+4];
  2527. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2528. JPEGPixelLE(pOutput+iCol+4, Y1, Cb, Cr); // top right
  2529. else
  2530. JPEGPixelBE(pOutput+iCol+4, Y1, Cb, Cr);
  2531. Y1 = (pY[iCol*2+(DCTSIZE*4)] + pY[iCol*2+1+(DCTSIZE*4)] + pY[iCol*2+8+(DCTSIZE*4)] + pY[iCol*2+9+(DCTSIZE*4)]) << 10;
  2532. Cb = pCb[iCol+32];
  2533. Cr = pCr[iCol+32];
  2534. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2535. JPEGPixelLE(pOutput+iCol+iPitch*4, Y1, Cb, Cr); // bottom left
  2536. else
  2537. JPEGPixelBE(pOutput+iCol+iPitch*4, Y1, Cb, Cr);
  2538. Y1 = (pY[iCol*2+(DCTSIZE*6)] + pY[iCol*2+1+(DCTSIZE*6)] + pY[iCol*2+8+(DCTSIZE*6)] + pY[iCol*2+9+(DCTSIZE*6)]) << 10;
  2539. Cb = pCb[iCol+32+4];
  2540. Cr = pCr[iCol+32+4];
  2541. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2542. JPEGPixelLE(pOutput+iCol+4+iPitch*4, Y1, Cb, Cr); // bottom right
  2543. else
  2544. JPEGPixelBE(pOutput+iCol+4+iPitch*4, Y1, Cb, Cr);
  2545. }
  2546. pY += 16;
  2547. pCb += 8;
  2548. pCr += 8;
  2549. pOutput += iPitch;
  2550. }
  2551. return;
  2552. }
  2553. if (pJPEG->iOptions & JPEG_SCALE_EIGHTH)
  2554. {
  2555. Y1 = pY[0] << 12; // scale to level of conversion table
  2556. Cb = pCb[0];
  2557. Cr = pCr[0];
  2558. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2559. JPEGPixelLE(pOutput, Y1, Cb, Cr);
  2560. else
  2561. JPEGPixelBE(pOutput, Y1, Cb, Cr);
  2562. // top right block
  2563. Y1 = pY[DCTSIZE*2] << 12; // scale to level of conversion table
  2564. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2565. JPEGPixelLE(pOutput + 1, Y1, Cb, Cr);
  2566. else
  2567. JPEGPixelBE(pOutput + 1, Y1, Cb, Cr);
  2568. // bottom left block
  2569. Y1 = pY[DCTSIZE*4] << 12; // scale to level of conversion table
  2570. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2571. JPEGPixelLE(pOutput+iPitch, Y1, Cb, Cr);
  2572. else
  2573. JPEGPixelBE(pOutput+iPitch, Y1, Cb, Cr);
  2574. // bottom right block
  2575. Y1 = pY[DCTSIZE*6] << 12; // scale to level of conversion table
  2576. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2577. JPEGPixelLE(pOutput+ 1 + iPitch, Y1, Cb, Cr);
  2578. else
  2579. JPEGPixelBE(pOutput+ 1 + iPitch, Y1, Cb, Cr);
  2580. return;
  2581. }
  2582. if (pJPEG->iOptions & JPEG_SCALE_QUARTER) // special case of 1/4
  2583. {
  2584. for (iRow=0; iRow<2; iRow++)
  2585. {
  2586. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2587. {
  2588. for (iCol=0; iCol<2; iCol++)
  2589. {
  2590. // top left block
  2591. Y1 = pY[iCol] << 12; // scale to level of conversion table
  2592. Cb = pCb[0];
  2593. Cr = pCr[0];
  2594. JPEGPixelLE(pOutput + iCol, Y1, Cb, Cr);
  2595. // top right block
  2596. Y1 = pY[iCol+(DCTSIZE*2)] << 12; // scale to level of conversion table
  2597. Cb = pCb[1];
  2598. Cr = pCr[1];
  2599. JPEGPixelLE(pOutput + 2+iCol, Y1, Cb, Cr);
  2600. // bottom left block
  2601. Y1 = pY[iCol+DCTSIZE*4] << 12; // scale to level of conversion table
  2602. Cb = pCb[2];
  2603. Cr = pCr[2];
  2604. JPEGPixelLE(pOutput+iPitch*2 + iCol, Y1, Cb, Cr);
  2605. // bottom right block
  2606. Y1 = pY[iCol+DCTSIZE*6] << 12; // scale to level of conversion table
  2607. Cb = pCb[3];
  2608. Cr = pCr[3];
  2609. JPEGPixelLE(pOutput+iPitch*2 + 2+iCol, Y1, Cb, Cr);
  2610. } // for each column
  2611. }
  2612. else
  2613. {
  2614. for (iCol=0; iCol<2; iCol++)
  2615. {
  2616. // top left block
  2617. Y1 = pY[iCol] << 12; // scale to level of conversion table
  2618. Cb = pCb[0];
  2619. Cr = pCr[0];
  2620. JPEGPixelBE(pOutput + iCol, Y1, Cb, Cr);
  2621. // top right block
  2622. Y1 = pY[iCol+(DCTSIZE*2)] << 12; // scale to level of conversion table
  2623. Cb = pCb[1];
  2624. Cr = pCr[1];
  2625. JPEGPixelBE(pOutput + 2+iCol, Y1, Cb, Cr);
  2626. // bottom left block
  2627. Y1 = pY[iCol+DCTSIZE*4] << 12; // scale to level of conversion table
  2628. Cb = pCb[2];
  2629. Cr = pCr[2];
  2630. JPEGPixelBE(pOutput+iPitch*2 + iCol, Y1, Cb, Cr);
  2631. // bottom right block
  2632. Y1 = pY[iCol+DCTSIZE*6] << 12; // scale to level of conversion table
  2633. Cb = pCb[3];
  2634. Cr = pCr[3];
  2635. JPEGPixelBE(pOutput+iPitch*2 + 2+iCol, Y1, Cb, Cr);
  2636. } // for each column
  2637. }
  2638. pY += 2; // skip 1 line of source pixels
  2639. pOutput += iPitch;
  2640. }
  2641. return;
  2642. }
  2643. /* Convert YCC pixels into RGB pixels and store in output image */
  2644. iYCount = 4;
  2645. bUseOdd1 = bUseOdd2 = 1; // assume odd column can be used
  2646. if ((x+15) >= pJPEG->iWidth)
  2647. {
  2648. iCol = (((pJPEG->iWidth & 15)+1) >> 1);
  2649. if (iCol >= 4)
  2650. {
  2651. iXCount1 = 4;
  2652. iXCount2 = iCol-4;
  2653. if (pJPEG->iWidth & 1 && (iXCount2 * 2) + 8 + (x * 16) > pJPEG->iWidth)
  2654. bUseOdd2 = 0;
  2655. }
  2656. else
  2657. {
  2658. iXCount1 = iCol;
  2659. iXCount2 = 0;
  2660. if (pJPEG->iWidth & 1 && (iXCount1 * 2) + (x * 16) > pJPEG->iWidth)
  2661. bUseOdd1 = 0;
  2662. }
  2663. }
  2664. else
  2665. iXCount1 = iXCount2 = 4;
  2666. for (iRow=0; iRow<iYCount; iRow++) // up to 4 rows to do
  2667. {
  2668. for (iCol=0; iCol<iXCount1; iCol++) // up to 4 cols to do
  2669. {
  2670. // for top left block
  2671. Y1 = pY[iCol*2];
  2672. Y2 = pY[iCol*2+1];
  2673. Y3 = pY[iCol*2+8];
  2674. Y4 = pY[iCol*2+9];
  2675. Y1 <<= 12; // scale to level of conversion table
  2676. Y2 <<= 12;
  2677. Y3 <<= 12;
  2678. Y4 <<= 12;
  2679. Cb = pCb[iCol];
  2680. Cr = pCr[iCol];
  2681. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2682. {
  2683. if (bUseOdd1 || iCol != (iXCount1-1)) // only render if it won't go off the right edge
  2684. {
  2685. JPEGPixel2LE(pOutput + (iCol<<1), Y1, Y2, Cb, Cr);
  2686. JPEGPixel2LE(pOutput+iPitch + (iCol<<1), Y3, Y4, Cb, Cr);
  2687. }
  2688. else
  2689. {
  2690. JPEGPixelLE(pOutput + (iCol<<1), Y1, Cb, Cr);
  2691. JPEGPixelLE(pOutput+iPitch + (iCol<<1), Y3, Cb, Cr);
  2692. }
  2693. }
  2694. else
  2695. {
  2696. if (bUseOdd1 || iCol != (iXCount1-1)) // only render if it won't go off the right edge
  2697. {
  2698. JPEGPixel2BE(pOutput + (iCol<<1), Y1, Y2, Cb, Cr);
  2699. JPEGPixel2BE(pOutput+iPitch + (iCol<<1), Y3, Y4, Cb, Cr);
  2700. }
  2701. else
  2702. {
  2703. JPEGPixelBE(pOutput + (iCol<<1), Y1, Cb, Cr);
  2704. JPEGPixelBE(pOutput+iPitch + (iCol<<1), Y3, Cb, Cr);
  2705. }
  2706. }
  2707. // for top right block
  2708. if (iCol < iXCount2)
  2709. {
  2710. Y1 = pY[iCol*2+DCTSIZE*2];
  2711. Y2 = pY[iCol*2+1+DCTSIZE*2];
  2712. Y3 = pY[iCol*2+8+DCTSIZE*2];
  2713. Y4 = pY[iCol*2+9+DCTSIZE*2];
  2714. Y1 <<= 12; // scale to level of conversion table
  2715. Y2 <<= 12;
  2716. Y3 <<= 12;
  2717. Y4 <<= 12;
  2718. Cb = pCb[iCol+4];
  2719. Cr = pCr[iCol+4];
  2720. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2721. {
  2722. if (bUseOdd2 || iCol != (iXCount2-1)) // only render if it won't go off the right edge
  2723. {
  2724. JPEGPixel2LE(pOutput + 8+(iCol<<1), Y1, Y2, Cb, Cr);
  2725. JPEGPixel2LE(pOutput+iPitch + 8+(iCol<<1), Y3, Y4, Cb, Cr);
  2726. }
  2727. else
  2728. {
  2729. JPEGPixelLE(pOutput+ 8+(iCol<<1), Y1, Cb, Cr);
  2730. JPEGPixelLE(pOutput+iPitch+ 8+(iCol<<1), Y3, Cb, Cr);
  2731. }
  2732. }
  2733. else
  2734. {
  2735. if (bUseOdd2 || iCol != (iXCount2-1)) // only render if it won't go off the right edge
  2736. {
  2737. JPEGPixel2BE(pOutput + 8+(iCol<<1), Y1, Y2, Cb, Cr);
  2738. JPEGPixel2BE(pOutput+iPitch + 8+(iCol<<1), Y3, Y4, Cb, Cr);
  2739. }
  2740. else
  2741. {
  2742. JPEGPixelBE(pOutput+ 8+(iCol<<1), Y1, Cb, Cr);
  2743. JPEGPixelBE(pOutput+iPitch+ 8+(iCol<<1), Y3, Cb, Cr);
  2744. }
  2745. }
  2746. }
  2747. // for bottom left block
  2748. Y1 = pY[iCol*2+DCTSIZE*4];
  2749. Y2 = pY[iCol*2+1+DCTSIZE*4];
  2750. Y3 = pY[iCol*2+8+DCTSIZE*4];
  2751. Y4 = pY[iCol*2+9+DCTSIZE*4];
  2752. Y1 <<= 12; // scale to level of conversion table
  2753. Y2 <<= 12;
  2754. Y3 <<= 12;
  2755. Y4 <<= 12;
  2756. Cb = pCb[iCol+32];
  2757. Cr = pCr[iCol+32];
  2758. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2759. {
  2760. if (bUseOdd1 || iCol != (iXCount1-1)) // only render if it won't go off the right edge
  2761. {
  2762. JPEGPixel2LE(pOutput+iPitch*8+ (iCol<<1), Y1, Y2, Cb, Cr);
  2763. JPEGPixel2LE(pOutput+iPitch*9+ (iCol<<1), Y3, Y4, Cb, Cr);
  2764. }
  2765. else
  2766. {
  2767. JPEGPixelLE(pOutput+iPitch*8+ (iCol<<1), Y1, Cb, Cr);
  2768. JPEGPixelLE(pOutput+iPitch*9+ (iCol<<1), Y3, Cb, Cr);
  2769. }
  2770. }
  2771. else
  2772. {
  2773. if (bUseOdd1 || iCol != (iXCount1-1)) // only render if it won't go off the right edge
  2774. {
  2775. JPEGPixel2BE(pOutput+iPitch*8+ (iCol<<1), Y1, Y2, Cb, Cr);
  2776. JPEGPixel2BE(pOutput+iPitch*9+ (iCol<<1), Y3, Y4, Cb, Cr);
  2777. }
  2778. else
  2779. {
  2780. JPEGPixelBE(pOutput+iPitch*8+ (iCol<<1), Y1, Cb, Cr);
  2781. JPEGPixelBE(pOutput+iPitch*9+ (iCol<<1), Y3, Cb, Cr);
  2782. }
  2783. }
  2784. // for bottom right block
  2785. if (iCol < iXCount2)
  2786. {
  2787. Y1 = pY[iCol*2+DCTSIZE*6];
  2788. Y2 = pY[iCol*2+1+DCTSIZE*6];
  2789. Y3 = pY[iCol*2+8+DCTSIZE*6];
  2790. Y4 = pY[iCol*2+9+DCTSIZE*6];
  2791. Y1 <<= 12; // scale to level of conversion table
  2792. Y2 <<= 12;
  2793. Y3 <<= 12;
  2794. Y4 <<= 12;
  2795. Cb = pCb[iCol+36];
  2796. Cr = pCr[iCol+36];
  2797. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2798. {
  2799. if (bUseOdd2 || iCol != (iXCount2-1)) // only render if it won't go off the right edge
  2800. {
  2801. JPEGPixel2LE(pOutput+iPitch*8+ 8+(iCol<<1), Y1, Y2, Cb, Cr);
  2802. JPEGPixel2LE(pOutput+iPitch*9+ 8+(iCol<<1), Y3, Y4, Cb, Cr);
  2803. }
  2804. else
  2805. {
  2806. JPEGPixelLE(pOutput+iPitch*8+ 8+(iCol<<1), Y1, Cb, Cr);
  2807. JPEGPixelLE(pOutput+iPitch*9+ 8+(iCol<<1), Y3, Cb, Cr);
  2808. }
  2809. }
  2810. else
  2811. {
  2812. if (bUseOdd2 || iCol != (iXCount2-1)) // only render if it won't go off the right edge
  2813. {
  2814. JPEGPixel2BE(pOutput+iPitch*8+ 8+(iCol<<1), Y1, Y2, Cb, Cr);
  2815. JPEGPixel2BE(pOutput+iPitch*9+ 8+(iCol<<1), Y3, Y4, Cb, Cr);
  2816. }
  2817. else
  2818. {
  2819. JPEGPixelBE(pOutput+iPitch*8+ 8+(iCol<<1), Y1, Cb, Cr);
  2820. JPEGPixelBE(pOutput+iPitch*9+ 8+(iCol<<1), Y3, Cb, Cr);
  2821. }
  2822. }
  2823. }
  2824. } // for each column
  2825. pY += 16; // skip to next line of source pixels
  2826. pCb += 8;
  2827. pCr += 8;
  2828. pOutput += iPitch*2;
  2829. }
  2830. } /* JPEGPutMCU22() */
  2831. static void JPEGPutMCU12(JPEGIMAGE *pJPEG, int x, int iPitch)
  2832. {
  2833. uint32_t Cr,Cb;
  2834. signed int Y1, Y2;
  2835. int iRow, iCol, iXCount, iYCount;
  2836. uint8_t *pY, *pCr, *pCb;
  2837. uint16_t *pOutput = &pJPEG->usPixels[x];
  2838. pY = (uint8_t *)&pJPEG->sMCUs[0*DCTSIZE];
  2839. pCb = (uint8_t *)&pJPEG->sMCUs[2*DCTSIZE];
  2840. pCr = (uint8_t *)&pJPEG->sMCUs[3*DCTSIZE];
  2841. if (pJPEG->iOptions & JPEG_SCALE_HALF)
  2842. {
  2843. for (iRow=0; iRow<4; iRow++)
  2844. {
  2845. for (iCol=0; iCol<4; iCol++)
  2846. {
  2847. Y1 = (pY[0] + pY[1] + pY[8] + pY[9]) << 10;
  2848. Cb = (pCb[0] + pCb[1] + 1) >> 1;
  2849. Cr = (pCr[0] + pCr[1] + 1) >> 1;
  2850. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2851. JPEGPixelLE(pOutput+iCol, Y1, Cb, Cr);
  2852. else
  2853. JPEGPixelBE(pOutput+iCol, Y1, Cb, Cr);
  2854. Y1 = (pY[DCTSIZE*2] + pY[DCTSIZE*2+1] + pY[DCTSIZE*2+8] + pY[DCTSIZE*2+9]) << 10;
  2855. Cb = (pCb[32] + pCb[33] + 1) >> 1;
  2856. Cr = (pCr[32] + pCr[33] + 1) >> 1;
  2857. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2858. JPEGPixelLE(pOutput+iCol+iPitch, Y1, Cb, Cr);
  2859. else
  2860. JPEGPixelBE(pOutput+iCol+iPitch, Y1, Cb, Cr);
  2861. pCb += 2;
  2862. pCr += 2;
  2863. pY += 2;
  2864. }
  2865. pY += 8;
  2866. pOutput += iPitch*2;
  2867. }
  2868. return;
  2869. }
  2870. if (pJPEG->iOptions & JPEG_SCALE_EIGHTH)
  2871. {
  2872. Y1 = pY[0] << 12;
  2873. Y2 = pY[DCTSIZE*2] << 12;
  2874. Cb = pCb[0];
  2875. Cr = pCr[0];
  2876. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2877. {
  2878. JPEGPixelLE(pOutput, Y1, Cb, Cr);
  2879. JPEGPixelLE(pOutput + iPitch, Y2, Cb, Cr);
  2880. }
  2881. else
  2882. {
  2883. JPEGPixelBE(pOutput, Y1, Cb, Cr);
  2884. JPEGPixelBE(pOutput + iPitch, Y2, Cb, Cr);
  2885. }
  2886. return;
  2887. }
  2888. if (pJPEG->iOptions & JPEG_SCALE_QUARTER)
  2889. { // draw a 2x4 block
  2890. Y1 = pY[0] << 12;
  2891. Y2 = pY[2] << 12;
  2892. Cb = pCb[0];
  2893. Cr = pCr[0];
  2894. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2895. {
  2896. JPEGPixelLE(pOutput, Y1, Cb, Cr);
  2897. JPEGPixelLE(pOutput + iPitch, Y2, Cb, Cr);
  2898. }
  2899. else
  2900. {
  2901. JPEGPixelBE(pOutput, Y1, Cb, Cr);
  2902. JPEGPixelBE(pOutput + iPitch, Y2, Cb, Cr);
  2903. }
  2904. Y1 = pY[1] << 12;
  2905. Y2 = pY[3] << 12;
  2906. Cb = pCb[1];
  2907. Cr = pCr[1];
  2908. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2909. {
  2910. JPEGPixelLE(pOutput + 1, Y1, Cb, Cr);
  2911. JPEGPixelLE(pOutput + 1 + iPitch, Y2, Cb, Cr);
  2912. }
  2913. else
  2914. {
  2915. JPEGPixelBE(pOutput + 1, Y1, Cb, Cr);
  2916. JPEGPixelBE(pOutput + 1 + iPitch, Y2, Cb, Cr);
  2917. }
  2918. pY += DCTSIZE*2; // next Y block below
  2919. Y1 = pY[0] << 12;
  2920. Y2 = pY[2] << 12;
  2921. Cb = pCb[2];
  2922. Cr = pCr[2];
  2923. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2924. {
  2925. JPEGPixelLE(pOutput + iPitch*2, Y1, Cb, Cr);
  2926. JPEGPixelLE(pOutput + iPitch*3, Y2, Cb, Cr);
  2927. }
  2928. else
  2929. {
  2930. JPEGPixelBE(pOutput + iPitch*2, Y1, Cb, Cr);
  2931. JPEGPixelBE(pOutput + iPitch*3, Y2, Cb, Cr);
  2932. }
  2933. Y1 = pY[1] << 12;
  2934. Y2 = pY[3] << 12;
  2935. Cb = pCb[3];
  2936. Cr = pCr[3];
  2937. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2938. {
  2939. JPEGPixelLE(pOutput + 1 + iPitch*2, Y1, Cb, Cr);
  2940. JPEGPixelLE(pOutput + 1 + iPitch*3, Y2, Cb, Cr);
  2941. }
  2942. else
  2943. {
  2944. JPEGPixelBE(pOutput + 1 + iPitch*2, Y1, Cb, Cr);
  2945. JPEGPixelBE(pOutput + 1 + iPitch*3, Y2, Cb, Cr);
  2946. }
  2947. return;
  2948. }
  2949. /* Convert YCC pixels into RGB pixels and store in output image */
  2950. iYCount = 16;
  2951. iXCount = 8;
  2952. for (iRow=0; iRow<iYCount; iRow+=2) // up to 16 rows to do
  2953. {
  2954. for (iCol=0; iCol<iXCount; iCol++) // up to 8 cols to do
  2955. {
  2956. Y1 = pY[iCol];
  2957. Y2 = pY[iCol+8];
  2958. Y1 <<= 12; // scale to level of conversion table
  2959. Y2 <<= 12;
  2960. Cb = pCb[iCol];
  2961. Cr = pCr[iCol];
  2962. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  2963. {
  2964. JPEGPixelLE(pOutput + iCol, Y1, Cb, Cr);
  2965. JPEGPixelLE(pOutput + iPitch + iCol, Y2, Cb, Cr);
  2966. }
  2967. else
  2968. {
  2969. JPEGPixelBE(pOutput + iCol, Y1, Cb, Cr);
  2970. JPEGPixelBE(pOutput + iPitch + iCol, Y2, Cb, Cr);
  2971. }
  2972. }
  2973. pY += 16; // skip to next 2 lines of source pixels
  2974. if (iRow == 6) // next MCU block, skip ahead to correct spot
  2975. pY += (128-64);
  2976. pCb += 8;
  2977. pCr += 8;
  2978. pOutput += iPitch*2; // next 2 lines of dest pixels
  2979. }
  2980. } /* JPEGPutMCU12() */
  2981. static void JPEGPutMCU21(JPEGIMAGE *pJPEG, int x, int iPitch)
  2982. {
  2983. int iCr, iCb;
  2984. signed int Y1, Y2;
  2985. int iCol;
  2986. int iRow;
  2987. uint8_t *pY, *pCr, *pCb;
  2988. uint16_t *pOutput = &pJPEG->usPixels[x];
  2989. pY = (uint8_t *)&pJPEG->sMCUs[0*DCTSIZE];
  2990. pCb = (uint8_t *)&pJPEG->sMCUs[2*DCTSIZE];
  2991. pCr = (uint8_t *)&pJPEG->sMCUs[3*DCTSIZE];
  2992. if (pJPEG->iOptions & JPEG_SCALE_HALF)
  2993. {
  2994. for (iRow=0; iRow<4; iRow++)
  2995. {
  2996. for (iCol=0; iCol<4; iCol++)
  2997. { // left block
  2998. iCr = (pCr[0] + pCr[8] + 1) >> 1;
  2999. iCb = (pCb[0] + pCb[8] + 1) >> 1;
  3000. Y1 = (signed int)(pY[0] + pY[1] + pY[8] + pY[9]) << 10;
  3001. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  3002. JPEGPixelLE(pOutput+iCol, Y1, iCb, iCr);
  3003. else
  3004. JPEGPixelBE(pOutput+iCol, Y1, iCb, iCr);
  3005. // right block
  3006. iCr = (pCr[4] + pCr[12] + 1) >> 1;
  3007. iCb = (pCb[4] + pCb[12] + 1) >> 1;
  3008. Y1 = (signed int)(pY[128] + pY[129] + pY[136] + pY[137]) << 10;
  3009. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  3010. JPEGPixelLE(pOutput+iCol+4, Y1, iCb, iCr);
  3011. else
  3012. JPEGPixelBE(pOutput+iCol+4, Y1, iCb, iCr);
  3013. pCb++;
  3014. pCr++;
  3015. pY += 2;
  3016. }
  3017. pCb += 12;
  3018. pCr += 12;
  3019. pY += 8;
  3020. pOutput += iPitch;
  3021. }
  3022. return;
  3023. }
  3024. if (pJPEG->iOptions & JPEG_SCALE_EIGHTH)
  3025. { // draw 2 pixels
  3026. iCr = pCr[0];
  3027. iCb = pCb[0];
  3028. Y1 = (signed int)(pY[0]) << 12;
  3029. Y2 = (signed int)(pY[DCTSIZE*2]) << 12;
  3030. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  3031. JPEGPixel2LE(pOutput, Y1, Y2, iCb, iCr);
  3032. else
  3033. JPEGPixel2BE(pOutput, Y1, Y2, iCb, iCr);
  3034. return;
  3035. }
  3036. if (pJPEG->iOptions & JPEG_SCALE_QUARTER)
  3037. { // draw 4x2 pixels
  3038. // top left
  3039. iCr = pCr[0];
  3040. iCb = pCb[0];
  3041. Y1 = (signed int)(pY[0]) << 12;
  3042. Y2 = (signed int)(pY[1]) << 12;
  3043. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  3044. JPEGPixel2LE(pOutput, Y1, Y2, iCb, iCr);
  3045. else
  3046. JPEGPixel2BE(pOutput, Y1, Y2, iCb, iCr);
  3047. // top right
  3048. iCr = pCr[1];
  3049. iCb = pCb[1];
  3050. Y1 = (signed int)pY[DCTSIZE*2] << 12;
  3051. Y2 = (signed int)pY[DCTSIZE*2+1] << 12;
  3052. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  3053. JPEGPixel2LE(pOutput + 2, Y1, Y2, iCb, iCr);
  3054. else
  3055. JPEGPixel2BE(pOutput + 2, Y1, Y2, iCb, iCr);
  3056. // bottom left
  3057. iCr = pCr[2];
  3058. iCb = pCb[2];
  3059. Y1 = (signed int)(pY[2]) << 12;
  3060. Y2 = (signed int)(pY[3]) << 12;
  3061. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  3062. JPEGPixel2LE(pOutput + iPitch, Y1, Y2, iCb, iCr);
  3063. else
  3064. JPEGPixel2BE(pOutput + iPitch, Y1, Y2, iCb, iCr);
  3065. // bottom right
  3066. iCr = pCr[3];
  3067. iCb = pCb[3];
  3068. Y1 = (signed int)pY[DCTSIZE*2+2] << 12;
  3069. Y2 = (signed int)pY[DCTSIZE*2+3] << 12;
  3070. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  3071. JPEGPixel2LE(pOutput + iPitch + 2, Y1, Y2, iCb, iCr);
  3072. else
  3073. JPEGPixel2BE(pOutput + iPitch + 2, Y1, Y2, iCb, iCr);
  3074. return;
  3075. }
  3076. /* Convert YCC pixels into RGB pixels and store in output image */
  3077. for (iRow=0; iRow<8; iRow++) // up to 8 rows to do
  3078. {
  3079. for (iCol=0; iCol<4; iCol++) // up to 4x2 cols to do
  3080. { // left block
  3081. iCr = *pCr++;
  3082. iCb = *pCb++;
  3083. Y1 = (signed int)(*pY++) << 12;
  3084. Y2 = (signed int)(*pY++) << 12;
  3085. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  3086. JPEGPixel2LE(pOutput + iCol*2, Y1, Y2, iCb, iCr);
  3087. else
  3088. JPEGPixel2BE(pOutput + iCol*2, Y1, Y2, iCb, iCr);
  3089. // right block
  3090. iCr = pCr[3];
  3091. iCb = pCb[3];
  3092. Y1 = (signed int)pY[126] << 12;
  3093. Y2 = (signed int)pY[127] << 12;
  3094. if (pJPEG->ucPixelType == RGB565_LITTLE_ENDIAN)
  3095. JPEGPixel2LE(pOutput + 8 + iCol*2, Y1, Y2, iCb, iCr);
  3096. else
  3097. JPEGPixel2BE(pOutput + 8 + iCol*2, Y1, Y2, iCb, iCr);
  3098. } // for col
  3099. pCb += 4;
  3100. pCr += 4;
  3101. pOutput += iPitch;
  3102. } // for row
  3103. } /* JPEGPutMCU21() */
  3104. // Dither the 8-bit gray pixels into 1, 2, or 4-bit gray
  3105. static void JPEGDither(JPEGIMAGE *pJPEG, int iWidth, int iHeight)
  3106. {
  3107. int x, y, xmask=0, iDestPitch=0;
  3108. int32_t cNew, lFErr, v=0, h;
  3109. int32_t e1,e2,e3,e4;
  3110. uint8_t cOut, ucPixelType; // forward errors for gray
  3111. uint8_t *pSrc, *pDest, *errors, *pErrors=NULL, *d, *pPixels; // destination 8bpp image
  3112. uint8_t pixelmask=0, shift=0;
  3113. ucPixelType = pJPEG->ucPixelType;
  3114. errors = (uint8_t *)pJPEG->usPixels; // plenty of space here
  3115. errors[0] = errors[1] = errors[2] = 0;
  3116. pDest = pSrc = pJPEG->pDitherBuffer; // write the new pixels over the original
  3117. switch (ucPixelType)
  3118. {
  3119. case FOUR_BIT_DITHERED:
  3120. iDestPitch = (iWidth+1)/2;
  3121. pixelmask = 0xf0;
  3122. shift = 4;
  3123. xmask = 1;
  3124. break;
  3125. case TWO_BIT_DITHERED:
  3126. iDestPitch = (iWidth+3)/4;
  3127. pixelmask = 0xc0;
  3128. shift = 2;
  3129. xmask = 3;
  3130. break;
  3131. case ONE_BIT_DITHERED:
  3132. iDestPitch = (iWidth+7)/8;
  3133. pixelmask = 0x80;
  3134. shift = 1;
  3135. xmask = 7;
  3136. break;
  3137. }
  3138. for (y=0; y<iHeight; y++)
  3139. {
  3140. pPixels = &pSrc[y * iWidth];
  3141. d = &pDest[y * iDestPitch];
  3142. pErrors = &errors[1]; // point to second pixel to avoid boundary check
  3143. lFErr = 0;
  3144. cOut = 0;
  3145. for (x=0; x<iWidth; x++)
  3146. {
  3147. cNew = *pPixels++; // get grayscale uint8_t pixel
  3148. // add forward error
  3149. cNew += lFErr;
  3150. if (cNew > 255) cNew = 255; // clip to uint8_t
  3151. cOut <<= shift; // pack new pixels into a byte
  3152. cOut |= (cNew >> (8-shift)); // keep top N bits
  3153. if ((x & xmask) == xmask) // store it when the byte is full
  3154. {
  3155. *d++ = cOut;
  3156. cOut = 0;
  3157. }
  3158. // calculate the Floyd-Steinberg error for this pixel
  3159. v = cNew - (cNew & pixelmask); // new error for N-bit gray output (always positive)
  3160. h = v >> 1;
  3161. e1 = (7*h)>>3; // 7/16
  3162. e2 = h - e1; // 1/16
  3163. e3 = (5*h) >> 3; // 5/16
  3164. e4 = h - e3; // 3/16
  3165. // distribute error to neighbors
  3166. lFErr = e1 + pErrors[1];
  3167. pErrors[1] = (uint8_t)e2;
  3168. pErrors[0] += e3;
  3169. pErrors[-1] += e4;
  3170. pErrors++;
  3171. } // for x
  3172. } // for y
  3173. } /* JPEGDither() */
  3174. //
  3175. // Decode the image
  3176. // returns 0 for error, 1 for success
  3177. //
  3178. static int DecodeJPEG(JPEGIMAGE *pJPEG)
  3179. {
  3180. int cx, cy, x, y, mcuCX, mcuCY;
  3181. int iLum0, iLum1, iLum2, iLum3, iCr, iCb;
  3182. signed int iDCPred0, iDCPred1, iDCPred2;
  3183. int i, iQuant1, iQuant2, iQuant3, iErr;
  3184. uint8_t c;
  3185. int iMCUCount, xoff, iPitch, bThumbnail = 0;
  3186. int bContinue = 1; // early exit if the DRAW callback wants to stop
  3187. uint32_t l, *pl;
  3188. unsigned char cDCTable0, cACTable0, cDCTable1, cACTable1, cDCTable2, cACTable2;
  3189. JPEGDRAW jd;
  3190. int iMaxFill = 16, iScaleShift = 0;
  3191. // Requested the Exif thumbnail
  3192. if (pJPEG->iOptions & JPEG_EXIF_THUMBNAIL)
  3193. {
  3194. if (pJPEG->iThumbData == 0 || pJPEG->iThumbWidth == 0) // doesn't exist
  3195. {
  3196. pJPEG->iError = JPEG_INVALID_PARAMETER;
  3197. return 0;
  3198. }
  3199. if (!JPEGParseInfo(pJPEG, 1)) // parse the embedded thumbnail file header
  3200. return 0; // something went wrong
  3201. }
  3202. // Fast downscaling options
  3203. if (pJPEG->iOptions & JPEG_SCALE_HALF)
  3204. iScaleShift = 1;
  3205. else if (pJPEG->iOptions & JPEG_SCALE_QUARTER)
  3206. {
  3207. iScaleShift = 2;
  3208. iMaxFill = 1;
  3209. }
  3210. else if (pJPEG->iOptions & JPEG_SCALE_EIGHTH)
  3211. {
  3212. iScaleShift = 3;
  3213. iMaxFill = 1;
  3214. bThumbnail = 1;
  3215. }
  3216. // reorder and fix the quantization table for decoding
  3217. JPEGFixQuantD(pJPEG);
  3218. pJPEG->bb.ulBits = MOTOLONG(&pJPEG->ucFileBuf[0]); // preload first 4 bytes
  3219. pJPEG->bb.pBuf = pJPEG->ucFileBuf;
  3220. pJPEG->bb.ulBitOff = 0;
  3221. cDCTable0 = pJPEG->JPCI[0].dc_tbl_no;
  3222. cACTable0 = pJPEG->JPCI[0].ac_tbl_no;
  3223. cDCTable1 = pJPEG->JPCI[1].dc_tbl_no;
  3224. cACTable1 = pJPEG->JPCI[1].ac_tbl_no;
  3225. cDCTable2 = pJPEG->JPCI[2].dc_tbl_no;
  3226. cACTable2 = pJPEG->JPCI[2].ac_tbl_no;
  3227. iDCPred0 = iDCPred1 = iDCPred2 = mcuCX = mcuCY = 0;
  3228. switch (pJPEG->ucSubSample) // set up the parameters for the different subsampling options
  3229. {
  3230. case 0x00: // fake value to handle grayscale
  3231. case 0x01: // fake value to handle sRGB/CMYK
  3232. case 0x11:
  3233. cx = (pJPEG->iWidth + 7) >> 3; // number of MCU blocks
  3234. cy = (pJPEG->iHeight + 7) >> 3;
  3235. iCr = MCU1;
  3236. iCb = MCU2;
  3237. mcuCX = mcuCY = 8;
  3238. break;
  3239. case 0x12:
  3240. cx = (pJPEG->iWidth + 7) >> 3; // number of MCU blocks
  3241. cy = (pJPEG->iHeight + 15) >> 4;
  3242. iCr = MCU2;
  3243. iCb = MCU3;
  3244. mcuCX = 8;
  3245. mcuCY = 16;
  3246. break;
  3247. case 0x21:
  3248. cx = (pJPEG->iWidth + 15) >> 4; // number of MCU blocks
  3249. cy = (pJPEG->iHeight + 7) >> 3;
  3250. iCr = MCU2;
  3251. iCb = MCU3;
  3252. mcuCX = 16;
  3253. mcuCY = 8;
  3254. break;
  3255. case 0x22:
  3256. cx = (pJPEG->iWidth + 15) >> 4; // number of MCU blocks
  3257. cy = (pJPEG->iHeight + 15) >> 4;
  3258. iCr = MCU4;
  3259. iCb = MCU5;
  3260. mcuCX = mcuCY = 16;
  3261. break;
  3262. default: // to suppress compiler warning
  3263. cx = cy = 0;
  3264. iCr = iCb = 0;
  3265. break;
  3266. }
  3267. // Scale down the MCUs by the requested amount
  3268. mcuCX >>= iScaleShift;
  3269. mcuCY >>= iScaleShift;
  3270. iQuant1 = pJPEG->sQuantTable[pJPEG->JPCI[0].quant_tbl_no*DCTSIZE]; // DC quant values
  3271. iQuant2 = pJPEG->sQuantTable[pJPEG->JPCI[1].quant_tbl_no*DCTSIZE];
  3272. iQuant3 = pJPEG->sQuantTable[pJPEG->JPCI[2].quant_tbl_no*DCTSIZE];
  3273. // luminance values are always in these positions
  3274. iLum0 = MCU0;
  3275. iLum1 = MCU1;
  3276. iLum2 = MCU2;
  3277. iLum3 = MCU3;
  3278. iErr = 0;
  3279. pJPEG->iResCount = pJPEG->iResInterval;
  3280. // Calculate how many MCUs we can fit in the pixel buffer to maximize LCD drawing speed
  3281. iMCUCount = MAX_BUFFERED_PIXELS / (mcuCX * mcuCY);
  3282. if (pJPEG->ucPixelType == EIGHT_BIT_GRAYSCALE)
  3283. iMCUCount *= 2; // each pixel is only 1 byte
  3284. if (iMCUCount > cx)
  3285. iMCUCount = cx; // don't go wider than the image
  3286. if (iMCUCount > pJPEG->iMaxMCUs) // did the user set an upper bound on how many pixels per JPEGDraw callback?
  3287. iMCUCount = pJPEG->iMaxMCUs;
  3288. if (pJPEG->ucPixelType > EIGHT_BIT_GRAYSCALE) // dithered, override the max MCU count
  3289. iMCUCount = cx; // do the whole row
  3290. jd.iBpp = 16;
  3291. switch (pJPEG->ucPixelType)
  3292. {
  3293. case EIGHT_BIT_GRAYSCALE:
  3294. jd.iBpp = 8;
  3295. break;
  3296. case FOUR_BIT_DITHERED:
  3297. jd.iBpp = 4;
  3298. break;
  3299. case TWO_BIT_DITHERED:
  3300. jd.iBpp = 2;
  3301. break;
  3302. case ONE_BIT_DITHERED:
  3303. jd.iBpp = 1;
  3304. break;
  3305. }
  3306. if (pJPEG->ucPixelType > EIGHT_BIT_GRAYSCALE)
  3307. jd.pPixels = (uint16_t *)pJPEG->pDitherBuffer;
  3308. else
  3309. jd.pPixels = pJPEG->usPixels;
  3310. jd.iHeight = mcuCY;
  3311. jd.y = pJPEG->iYOffset;
  3312. for (y = 0; y < cy && bContinue && iErr == 0; y++, jd.y += mcuCY)
  3313. {
  3314. jd.x = pJPEG->iXOffset;
  3315. xoff = 0; // start of new LCD output group
  3316. iPitch = iMCUCount * mcuCX; // pixels per line of LCD buffer
  3317. for (x = 0; x < cx && bContinue && iErr == 0; x++)
  3318. {
  3319. pJPEG->ucACTable = cACTable0;
  3320. pJPEG->ucDCTable = cDCTable0;
  3321. // do the first luminance component
  3322. iErr = JPEGDecodeMCU(pJPEG, iLum0, &iDCPred0);
  3323. if (pJPEG->ucMaxACCol == 0 || bThumbnail) // no AC components, save some time
  3324. {
  3325. pl = (uint32_t *)&pJPEG->sMCUs[iLum0];
  3326. c = ucRangeTable[((iDCPred0 * iQuant1) >> 5) & 0x3ff];
  3327. l = c | ((uint32_t) c << 8) | ((uint32_t) c << 16) | ((uint32_t) c << 24);
  3328. // dct stores byte values
  3329. for (i = 0; i<iMaxFill; i++) // 8x8 bytes = 16 longs
  3330. pl[i] = l;
  3331. }
  3332. else
  3333. {
  3334. JPEGIDCT(pJPEG, iLum0, pJPEG->JPCI[0].quant_tbl_no, (pJPEG->ucMaxACCol | (pJPEG->ucMaxACRow << 8))); // first quantization table
  3335. }
  3336. // do the second luminance component
  3337. if (pJPEG->ucSubSample > 0x11) // subsampling
  3338. {
  3339. iErr |= JPEGDecodeMCU(pJPEG, iLum1, &iDCPred0);
  3340. if (pJPEG->ucMaxACCol == 0 || bThumbnail) // no AC components, save some time
  3341. {
  3342. c = ucRangeTable[((iDCPred0 * iQuant1) >> 5) & 0x3ff];
  3343. l = c | ((uint32_t) c << 8) | ((uint32_t) c << 16) | ((uint32_t) c << 24);
  3344. // dct stores byte values
  3345. pl = (uint32_t *)&pJPEG->sMCUs[iLum1];
  3346. for (i = 0; i<iMaxFill; i++) // 8x8 bytes = 16 longs
  3347. pl[i] = l;
  3348. }
  3349. else
  3350. {
  3351. JPEGIDCT(pJPEG, iLum1, pJPEG->JPCI[0].quant_tbl_no, (pJPEG->ucMaxACCol | (pJPEG->ucMaxACRow << 8))); // first quantization table
  3352. }
  3353. if (pJPEG->ucSubSample == 0x22)
  3354. {
  3355. iErr |= JPEGDecodeMCU(pJPEG, iLum2, &iDCPred0);
  3356. if (pJPEG->ucMaxACCol == 0 || bThumbnail) // no AC components, save some time
  3357. {
  3358. c = ucRangeTable[((iDCPred0 * iQuant1) >> 5) & 0x3ff];
  3359. l = c | ((uint32_t) c << 8) | ((uint32_t) c << 16) | ((uint32_t) c << 24);
  3360. // dct stores byte values
  3361. pl = (uint32_t *)&pJPEG->sMCUs[iLum2];
  3362. for (i = 0; i<iMaxFill; i++) // 8x8 bytes = 16 longs
  3363. pl[i] = l;
  3364. }
  3365. else
  3366. {
  3367. JPEGIDCT(pJPEG, iLum2, pJPEG->JPCI[0].quant_tbl_no, (pJPEG->ucMaxACCol | (pJPEG->ucMaxACRow << 8))); // first quantization table
  3368. }
  3369. iErr |= JPEGDecodeMCU(pJPEG, iLum3, &iDCPred0);
  3370. if (pJPEG->ucMaxACCol == 0 || bThumbnail) // no AC components, save some time
  3371. {
  3372. c = ucRangeTable[((iDCPred0 * iQuant1) >> 5) & 0x3ff];
  3373. l = c | ((uint32_t) c << 8) | ((uint32_t) c << 16) | ((uint32_t) c << 24);
  3374. // dct stores byte values
  3375. pl = (uint32_t *)&pJPEG->sMCUs[iLum3];
  3376. for (i = 0; i<iMaxFill; i++) // 8x8 bytes = 16 longs
  3377. pl[i] = l;
  3378. }
  3379. else
  3380. {
  3381. JPEGIDCT(pJPEG, iLum3, pJPEG->JPCI[0].quant_tbl_no, (pJPEG->ucMaxACCol | (pJPEG->ucMaxACRow << 8))); // first quantization table
  3382. }
  3383. } // if 2:2 subsampling
  3384. } // if subsampling used
  3385. if (pJPEG->ucSubSample && pJPEG->ucNumComponents == 3) // if color (not CMYK)
  3386. {
  3387. // first chroma
  3388. pJPEG->ucACTable = cACTable1;
  3389. pJPEG->ucDCTable = cDCTable1;
  3390. iErr |= JPEGDecodeMCU(pJPEG, iCr, &iDCPred1);
  3391. if (pJPEG->ucMaxACCol == 0 || bThumbnail) // no AC components, save some time
  3392. {
  3393. c = ucRangeTable[((iDCPred1 * iQuant2) >> 5) & 0x3ff];
  3394. l = c | ((uint32_t) c << 8) | ((uint32_t) c << 16) | ((uint32_t) c << 24);
  3395. // dct stores byte values
  3396. pl = (uint32_t *)&pJPEG->sMCUs[iCr];
  3397. for (i = 0; i<iMaxFill; i++) // 8x8 bytes = 16 longs
  3398. pl[i] = l;
  3399. }
  3400. else
  3401. {
  3402. JPEGIDCT(pJPEG, iCr, pJPEG->JPCI[1].quant_tbl_no, (pJPEG->ucMaxACCol | (pJPEG->ucMaxACRow << 8))); // second quantization table
  3403. }
  3404. // second chroma
  3405. pJPEG->ucACTable = cACTable2;
  3406. pJPEG->ucDCTable = cDCTable2;
  3407. iErr |= JPEGDecodeMCU(pJPEG, iCb, &iDCPred2);
  3408. if (pJPEG->ucMaxACCol == 0 || bThumbnail) // no AC components, save some time
  3409. {
  3410. c = ucRangeTable[((iDCPred2 * iQuant3) >> 5) & 0x3ff];
  3411. l = c | ((uint32_t) c << 8) | ((uint32_t) c << 16) | ((uint32_t) c << 24);
  3412. // dct stores byte values
  3413. pl = (uint32_t *)&pJPEG->sMCUs[iCb];
  3414. for (i = 0; i<iMaxFill; i++) // 8x8 bytes = 16 longs
  3415. pl[i] = l;
  3416. }
  3417. else
  3418. {
  3419. JPEGIDCT(pJPEG, iCb, pJPEG->JPCI[2].quant_tbl_no, (pJPEG->ucMaxACCol | (pJPEG->ucMaxACRow << 8)));
  3420. }
  3421. } // if color components present
  3422. if (pJPEG->ucPixelType >= EIGHT_BIT_GRAYSCALE)
  3423. {
  3424. JPEGPutMCU8BitGray(pJPEG, xoff, iPitch);
  3425. }
  3426. else
  3427. {
  3428. switch (pJPEG->ucSubSample)
  3429. {
  3430. case 0x00: // grayscale
  3431. JPEGPutMCUGray(pJPEG, xoff, iPitch);
  3432. break;
  3433. case 0x11:
  3434. JPEGPutMCU11(pJPEG, xoff, iPitch);
  3435. break;
  3436. case 0x12:
  3437. JPEGPutMCU12(pJPEG, xoff, iPitch);
  3438. break;
  3439. case 0x21:
  3440. JPEGPutMCU21(pJPEG, xoff, iPitch);
  3441. break;
  3442. case 0x22:
  3443. JPEGPutMCU22(pJPEG, xoff, iPitch);
  3444. break;
  3445. } // switch on color option
  3446. }
  3447. xoff += mcuCX;
  3448. if (xoff == iPitch || x == cx-1) // time to draw
  3449. {
  3450. xoff = 0;
  3451. jd.iWidth = iPitch; // width of each LCD block group
  3452. jd.pUser = pJPEG->pUser;
  3453. if (pJPEG->ucPixelType > EIGHT_BIT_GRAYSCALE) // dither to 4/2/1 bits
  3454. JPEGDither(pJPEG, cx * mcuCX, mcuCY);
  3455. if ((jd.y - pJPEG->iYOffset + mcuCY) > (pJPEG->iHeight>>iScaleShift)) { // last row needs to be trimmed
  3456. jd.iHeight = (pJPEG->iHeight>>iScaleShift) - (jd.y - pJPEG->iYOffset);
  3457. }
  3458. bContinue = (*pJPEG->pfnDraw)(&jd);
  3459. jd.x += iPitch;
  3460. if ((cx - 1 - x) < iMCUCount) // change pitch for the last set of MCUs on this row
  3461. iPitch = (cx - 1 - x) * mcuCX;
  3462. }
  3463. if (pJPEG->iResInterval)
  3464. {
  3465. if (--pJPEG->iResCount == 0)
  3466. {
  3467. pJPEG->iResCount = pJPEG->iResInterval;
  3468. iDCPred0 = iDCPred1 = iDCPred2 = 0; // reset DC predictors
  3469. if (pJPEG->bb.ulBitOff & 7) // need to start at the next even byte
  3470. {
  3471. pJPEG->bb.ulBitOff += (8 - (pJPEG->bb.ulBitOff & 7)); // new restart interval starts on byte boundary
  3472. }
  3473. } // if restart interval needs to reset
  3474. } // if there is a restart interval
  3475. // See if we need to feed it more data
  3476. if (pJPEG->iVLCOff >= FILE_HIGHWATER)
  3477. JPEGGetMoreData(pJPEG); // need more 'filtered' VLC data
  3478. } // for x
  3479. } // for y
  3480. if (iErr != 0)
  3481. pJPEG->iError = JPEG_DECODE_ERROR;
  3482. return (iErr == 0);
  3483. } /* DecodeJPEG() */