pngread.c 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226
  1. /* pngread.c - read a PNG file
  2. *
  3. * Copyright (c) 2018-2019 Cosmin Truta
  4. * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
  5. * Copyright (c) 1996-1997 Andreas Dilger
  6. * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file contains routines that an application calls directly to
  13. * read a PNG file or stream.
  14. */
  15. #include "pngpriv.h"
  16. #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
  17. # include <errno.h>
  18. #endif
  19. #ifdef PNG_READ_SUPPORTED
  20. /* Create a PNG structure for reading, and allocate any memory needed. */
  21. PNG_FUNCTION(png_structp,PNGAPI
  22. png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
  23. png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
  24. {
  25. #ifndef PNG_USER_MEM_SUPPORTED
  26. png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
  27. error_fn, warn_fn, NULL, NULL, NULL);
  28. #else
  29. return png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
  30. warn_fn, NULL, NULL, NULL);
  31. }
  32. /* Alternate create PNG structure for reading, and allocate any memory
  33. * needed.
  34. */
  35. PNG_FUNCTION(png_structp,PNGAPI
  36. png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
  37. png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  38. png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
  39. {
  40. png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
  41. error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
  42. #endif /* USER_MEM */
  43. if (png_ptr != NULL)
  44. {
  45. png_ptr->mode = PNG_IS_READ_STRUCT;
  46. /* Added in libpng-1.6.0; this can be used to detect a read structure if
  47. * required (it will be zero in a write structure.)
  48. */
  49. # ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  50. png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
  51. # endif
  52. # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
  53. png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
  54. /* In stable builds only warn if an application error can be completely
  55. * handled.
  56. */
  57. # if PNG_RELEASE_BUILD
  58. png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
  59. # endif
  60. # endif
  61. /* TODO: delay this, it can be done in png_init_io (if the app doesn't
  62. * do it itself) avoiding setting the default function if it is not
  63. * required.
  64. */
  65. png_set_read_fn(png_ptr, NULL, NULL);
  66. }
  67. return png_ptr;
  68. }
  69. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  70. /* Read the information before the actual image data. This has been
  71. * changed in v0.90 to allow reading a file that already has the magic
  72. * bytes read from the stream. You can tell libpng how many bytes have
  73. * been read from the beginning of the stream (up to the maximum of 8)
  74. * via png_set_sig_bytes(), and we will only check the remaining bytes
  75. * here. The application can then have access to the signature bytes we
  76. * read if it is determined that this isn't a valid PNG file.
  77. */
  78. void PNGAPI
  79. png_read_info(png_structrp png_ptr, png_inforp info_ptr)
  80. {
  81. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  82. int keep;
  83. #endif
  84. png_debug(1, "in png_read_info");
  85. if (png_ptr == NULL || info_ptr == NULL)
  86. return;
  87. /* Read and check the PNG file signature. */
  88. png_read_sig(png_ptr, info_ptr);
  89. for (;;)
  90. {
  91. png_uint_32 length = png_read_chunk_header(png_ptr);
  92. png_uint_32 chunk_name = png_ptr->chunk_name;
  93. /* IDAT logic needs to happen here to simplify getting the two flags
  94. * right.
  95. */
  96. if (chunk_name == png_IDAT)
  97. {
  98. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  99. png_chunk_error(png_ptr, "Missing IHDR before IDAT");
  100. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  101. (png_ptr->mode & PNG_HAVE_PLTE) == 0)
  102. png_chunk_error(png_ptr, "Missing PLTE before IDAT");
  103. else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
  104. png_chunk_benign_error(png_ptr, "Too many IDATs found");
  105. png_ptr->mode |= PNG_HAVE_IDAT;
  106. }
  107. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  108. {
  109. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  110. png_ptr->mode |= PNG_AFTER_IDAT;
  111. }
  112. /* This should be a binary subdivision search or a hash for
  113. * matching the chunk name rather than a linear search.
  114. */
  115. if (chunk_name == png_IHDR)
  116. png_handle_IHDR(png_ptr, info_ptr, length);
  117. else if (chunk_name == png_IEND)
  118. png_handle_IEND(png_ptr, info_ptr, length);
  119. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  120. else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
  121. {
  122. png_handle_unknown(png_ptr, info_ptr, length, keep);
  123. if (chunk_name == png_PLTE)
  124. png_ptr->mode |= PNG_HAVE_PLTE;
  125. else if (chunk_name == png_IDAT)
  126. {
  127. png_ptr->idat_size = 0; /* It has been consumed */
  128. break;
  129. }
  130. }
  131. #endif
  132. else if (chunk_name == png_PLTE)
  133. png_handle_PLTE(png_ptr, info_ptr, length);
  134. else if (chunk_name == png_IDAT)
  135. {
  136. png_ptr->idat_size = length;
  137. break;
  138. }
  139. #ifdef PNG_READ_bKGD_SUPPORTED
  140. else if (chunk_name == png_bKGD)
  141. png_handle_bKGD(png_ptr, info_ptr, length);
  142. #endif
  143. #ifdef PNG_READ_cHRM_SUPPORTED
  144. else if (chunk_name == png_cHRM)
  145. png_handle_cHRM(png_ptr, info_ptr, length);
  146. #endif
  147. #ifdef PNG_READ_eXIf_SUPPORTED
  148. else if (chunk_name == png_eXIf)
  149. png_handle_eXIf(png_ptr, info_ptr, length);
  150. #endif
  151. #ifdef PNG_READ_gAMA_SUPPORTED
  152. else if (chunk_name == png_gAMA)
  153. png_handle_gAMA(png_ptr, info_ptr, length);
  154. #endif
  155. #ifdef PNG_READ_hIST_SUPPORTED
  156. else if (chunk_name == png_hIST)
  157. png_handle_hIST(png_ptr, info_ptr, length);
  158. #endif
  159. #ifdef PNG_READ_oFFs_SUPPORTED
  160. else if (chunk_name == png_oFFs)
  161. png_handle_oFFs(png_ptr, info_ptr, length);
  162. #endif
  163. #ifdef PNG_READ_pCAL_SUPPORTED
  164. else if (chunk_name == png_pCAL)
  165. png_handle_pCAL(png_ptr, info_ptr, length);
  166. #endif
  167. #ifdef PNG_READ_sCAL_SUPPORTED
  168. else if (chunk_name == png_sCAL)
  169. png_handle_sCAL(png_ptr, info_ptr, length);
  170. #endif
  171. #ifdef PNG_READ_pHYs_SUPPORTED
  172. else if (chunk_name == png_pHYs)
  173. png_handle_pHYs(png_ptr, info_ptr, length);
  174. #endif
  175. #ifdef PNG_READ_sBIT_SUPPORTED
  176. else if (chunk_name == png_sBIT)
  177. png_handle_sBIT(png_ptr, info_ptr, length);
  178. #endif
  179. #ifdef PNG_READ_sRGB_SUPPORTED
  180. else if (chunk_name == png_sRGB)
  181. png_handle_sRGB(png_ptr, info_ptr, length);
  182. #endif
  183. #ifdef PNG_READ_iCCP_SUPPORTED
  184. else if (chunk_name == png_iCCP)
  185. png_handle_iCCP(png_ptr, info_ptr, length);
  186. #endif
  187. #ifdef PNG_READ_sPLT_SUPPORTED
  188. else if (chunk_name == png_sPLT)
  189. png_handle_sPLT(png_ptr, info_ptr, length);
  190. #endif
  191. #ifdef PNG_READ_tEXt_SUPPORTED
  192. else if (chunk_name == png_tEXt)
  193. png_handle_tEXt(png_ptr, info_ptr, length);
  194. #endif
  195. #ifdef PNG_READ_tIME_SUPPORTED
  196. else if (chunk_name == png_tIME)
  197. png_handle_tIME(png_ptr, info_ptr, length);
  198. #endif
  199. #ifdef PNG_READ_tRNS_SUPPORTED
  200. else if (chunk_name == png_tRNS)
  201. png_handle_tRNS(png_ptr, info_ptr, length);
  202. #endif
  203. #ifdef PNG_READ_zTXt_SUPPORTED
  204. else if (chunk_name == png_zTXt)
  205. png_handle_zTXt(png_ptr, info_ptr, length);
  206. #endif
  207. #ifdef PNG_READ_iTXt_SUPPORTED
  208. else if (chunk_name == png_iTXt)
  209. png_handle_iTXt(png_ptr, info_ptr, length);
  210. #endif
  211. else
  212. png_handle_unknown(png_ptr, info_ptr, length,
  213. PNG_HANDLE_CHUNK_AS_DEFAULT);
  214. }
  215. }
  216. #endif /* SEQUENTIAL_READ */
  217. /* Optional call to update the users info_ptr structure */
  218. void PNGAPI
  219. png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
  220. {
  221. png_debug(1, "in png_read_update_info");
  222. if (png_ptr != NULL)
  223. {
  224. if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
  225. {
  226. png_read_start_row(png_ptr);
  227. # ifdef PNG_READ_TRANSFORMS_SUPPORTED
  228. png_read_transform_info(png_ptr, info_ptr);
  229. # else
  230. PNG_UNUSED(info_ptr)
  231. # endif
  232. }
  233. /* New in 1.6.0 this avoids the bug of doing the initializations twice */
  234. else
  235. png_app_error(png_ptr,
  236. "png_read_update_info/png_start_read_image: duplicate call");
  237. }
  238. }
  239. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  240. /* Initialize palette, background, etc, after transformations
  241. * are set, but before any reading takes place. This allows
  242. * the user to obtain a gamma-corrected palette, for example.
  243. * If the user doesn't call this, we will do it ourselves.
  244. */
  245. void PNGAPI
  246. png_start_read_image(png_structrp png_ptr)
  247. {
  248. png_debug(1, "in png_start_read_image");
  249. if (png_ptr != NULL)
  250. {
  251. if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
  252. png_read_start_row(png_ptr);
  253. /* New in 1.6.0 this avoids the bug of doing the initializations twice */
  254. else
  255. png_app_error(png_ptr,
  256. "png_start_read_image/png_read_update_info: duplicate call");
  257. }
  258. }
  259. #endif /* SEQUENTIAL_READ */
  260. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  261. #ifdef PNG_MNG_FEATURES_SUPPORTED
  262. /* Undoes intrapixel differencing,
  263. * NOTE: this is apparently only supported in the 'sequential' reader.
  264. */
  265. static void
  266. png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
  267. {
  268. png_debug(1, "in png_do_read_intrapixel");
  269. if (
  270. (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
  271. {
  272. int bytes_per_pixel;
  273. png_uint_32 row_width = row_info->width;
  274. if (row_info->bit_depth == 8)
  275. {
  276. png_bytep rp;
  277. png_uint_32 i;
  278. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  279. bytes_per_pixel = 3;
  280. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  281. bytes_per_pixel = 4;
  282. else
  283. return;
  284. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  285. {
  286. *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
  287. *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
  288. }
  289. }
  290. else if (row_info->bit_depth == 16)
  291. {
  292. png_bytep rp;
  293. png_uint_32 i;
  294. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  295. bytes_per_pixel = 6;
  296. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  297. bytes_per_pixel = 8;
  298. else
  299. return;
  300. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  301. {
  302. png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
  303. png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
  304. png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
  305. png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
  306. png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
  307. *(rp ) = (png_byte)((red >> 8) & 0xff);
  308. *(rp + 1) = (png_byte)(red & 0xff);
  309. *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
  310. *(rp + 5) = (png_byte)(blue & 0xff);
  311. }
  312. }
  313. }
  314. }
  315. #endif /* MNG_FEATURES */
  316. void PNGAPI
  317. png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
  318. {
  319. png_row_info row_info;
  320. if (png_ptr == NULL)
  321. return;
  322. png_debug2(1, "in png_read_row (row %lu, pass %d)",
  323. (unsigned long)png_ptr->row_number, png_ptr->pass);
  324. /* png_read_start_row sets the information (in particular iwidth) for this
  325. * interlace pass.
  326. */
  327. if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
  328. png_read_start_row(png_ptr);
  329. /* 1.5.6: row_info moved out of png_struct to a local here. */
  330. row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
  331. row_info.color_type = png_ptr->color_type;
  332. row_info.bit_depth = png_ptr->bit_depth;
  333. row_info.channels = png_ptr->channels;
  334. row_info.pixel_depth = png_ptr->pixel_depth;
  335. row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
  336. #ifdef PNG_WARNINGS_SUPPORTED
  337. if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  338. {
  339. /* Check for transforms that have been set but were defined out */
  340. #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
  341. if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
  342. png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
  343. #endif
  344. #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
  345. if ((png_ptr->transformations & PNG_FILLER) != 0)
  346. png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
  347. #endif
  348. #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
  349. !defined(PNG_READ_PACKSWAP_SUPPORTED)
  350. if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
  351. png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
  352. #endif
  353. #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
  354. if ((png_ptr->transformations & PNG_PACK) != 0)
  355. png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
  356. #endif
  357. #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
  358. if ((png_ptr->transformations & PNG_SHIFT) != 0)
  359. png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
  360. #endif
  361. #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
  362. if ((png_ptr->transformations & PNG_BGR) != 0)
  363. png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
  364. #endif
  365. #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
  366. if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
  367. png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
  368. #endif
  369. }
  370. #endif /* WARNINGS */
  371. #ifdef PNG_READ_INTERLACING_SUPPORTED
  372. /* If interlaced and we do not need a new row, combine row and return.
  373. * Notice that the pixels we have from previous rows have been transformed
  374. * already; we can only combine like with like (transformed or
  375. * untransformed) and, because of the libpng API for interlaced images, this
  376. * means we must transform before de-interlacing.
  377. */
  378. if (png_ptr->interlaced != 0 &&
  379. (png_ptr->transformations & PNG_INTERLACE) != 0)
  380. {
  381. switch (png_ptr->pass)
  382. {
  383. case 0:
  384. if (png_ptr->row_number & 0x07)
  385. {
  386. if (dsp_row != NULL)
  387. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  388. png_read_finish_row(png_ptr);
  389. return;
  390. }
  391. break;
  392. case 1:
  393. if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  394. {
  395. if (dsp_row != NULL)
  396. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  397. png_read_finish_row(png_ptr);
  398. return;
  399. }
  400. break;
  401. case 2:
  402. if ((png_ptr->row_number & 0x07) != 4)
  403. {
  404. if (dsp_row != NULL && (png_ptr->row_number & 4))
  405. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  406. png_read_finish_row(png_ptr);
  407. return;
  408. }
  409. break;
  410. case 3:
  411. if ((png_ptr->row_number & 3) || png_ptr->width < 3)
  412. {
  413. if (dsp_row != NULL)
  414. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  415. png_read_finish_row(png_ptr);
  416. return;
  417. }
  418. break;
  419. case 4:
  420. if ((png_ptr->row_number & 3) != 2)
  421. {
  422. if (dsp_row != NULL && (png_ptr->row_number & 2))
  423. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  424. png_read_finish_row(png_ptr);
  425. return;
  426. }
  427. break;
  428. case 5:
  429. if ((png_ptr->row_number & 1) || png_ptr->width < 2)
  430. {
  431. if (dsp_row != NULL)
  432. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  433. png_read_finish_row(png_ptr);
  434. return;
  435. }
  436. break;
  437. default:
  438. case 6:
  439. if ((png_ptr->row_number & 1) == 0)
  440. {
  441. png_read_finish_row(png_ptr);
  442. return;
  443. }
  444. break;
  445. }
  446. }
  447. #endif
  448. if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
  449. png_error(png_ptr, "Invalid attempt to read row data");
  450. /* Fill the row with IDAT data: */
  451. png_ptr->row_buf[0]=255; /* to force error if no data was found */
  452. png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
  453. if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
  454. {
  455. if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
  456. png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
  457. png_ptr->prev_row + 1, png_ptr->row_buf[0]);
  458. else
  459. png_error(png_ptr, "bad adaptive filter value");
  460. }
  461. /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
  462. * 1.5.6, while the buffer really is this big in current versions of libpng
  463. * it may not be in the future, so this was changed just to copy the
  464. * interlaced count:
  465. */
  466. memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
  467. #ifdef PNG_MNG_FEATURES_SUPPORTED
  468. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
  469. (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  470. {
  471. /* Intrapixel differencing */
  472. png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
  473. }
  474. #endif
  475. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  476. if (png_ptr->transformations)
  477. png_do_read_transformations(png_ptr, &row_info);
  478. #endif
  479. /* The transformed pixel depth should match the depth now in row_info. */
  480. if (png_ptr->transformed_pixel_depth == 0)
  481. {
  482. png_ptr->transformed_pixel_depth = row_info.pixel_depth;
  483. if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
  484. png_error(png_ptr, "sequential row overflow");
  485. }
  486. else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
  487. png_error(png_ptr, "internal sequential row size calculation error");
  488. #ifdef PNG_READ_INTERLACING_SUPPORTED
  489. /* Expand interlaced rows to full size */
  490. if (png_ptr->interlaced != 0 &&
  491. (png_ptr->transformations & PNG_INTERLACE) != 0)
  492. {
  493. if (png_ptr->pass < 6)
  494. png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
  495. png_ptr->transformations);
  496. if (dsp_row != NULL)
  497. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  498. if (row != NULL)
  499. png_combine_row(png_ptr, row, 0/*row*/);
  500. }
  501. else
  502. #endif
  503. {
  504. if (row != NULL)
  505. png_combine_row(png_ptr, row, -1/*ignored*/);
  506. if (dsp_row != NULL)
  507. png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
  508. }
  509. png_read_finish_row(png_ptr);
  510. if (png_ptr->read_row_fn != NULL)
  511. (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  512. }
  513. #endif /* SEQUENTIAL_READ */
  514. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  515. /* Read one or more rows of image data. If the image is interlaced,
  516. * and png_set_interlace_handling() has been called, the rows need to
  517. * contain the contents of the rows from the previous pass. If the
  518. * image has alpha or transparency, and png_handle_alpha()[*] has been
  519. * called, the rows contents must be initialized to the contents of the
  520. * screen.
  521. *
  522. * "row" holds the actual image, and pixels are placed in it
  523. * as they arrive. If the image is displayed after each pass, it will
  524. * appear to "sparkle" in. "display_row" can be used to display a
  525. * "chunky" progressive image, with finer detail added as it becomes
  526. * available. If you do not want this "chunky" display, you may pass
  527. * NULL for display_row. If you do not want the sparkle display, and
  528. * you have not called png_handle_alpha(), you may pass NULL for rows.
  529. * If you have called png_handle_alpha(), and the image has either an
  530. * alpha channel or a transparency chunk, you must provide a buffer for
  531. * rows. In this case, you do not have to provide a display_row buffer
  532. * also, but you may. If the image is not interlaced, or if you have
  533. * not called png_set_interlace_handling(), the display_row buffer will
  534. * be ignored, so pass NULL to it.
  535. *
  536. * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  537. */
  538. void PNGAPI
  539. png_read_rows(png_structrp png_ptr, png_bytepp row,
  540. png_bytepp display_row, png_uint_32 num_rows)
  541. {
  542. png_uint_32 i;
  543. png_bytepp rp;
  544. png_bytepp dp;
  545. png_debug(1, "in png_read_rows");
  546. if (png_ptr == NULL)
  547. return;
  548. rp = row;
  549. dp = display_row;
  550. if (rp != NULL && dp != NULL)
  551. for (i = 0; i < num_rows; i++)
  552. {
  553. png_bytep rptr = *rp++;
  554. png_bytep dptr = *dp++;
  555. png_read_row(png_ptr, rptr, dptr);
  556. }
  557. else if (rp != NULL)
  558. for (i = 0; i < num_rows; i++)
  559. {
  560. png_bytep rptr = *rp;
  561. png_read_row(png_ptr, rptr, NULL);
  562. rp++;
  563. }
  564. else if (dp != NULL)
  565. for (i = 0; i < num_rows; i++)
  566. {
  567. png_bytep dptr = *dp;
  568. png_read_row(png_ptr, NULL, dptr);
  569. dp++;
  570. }
  571. }
  572. #endif /* SEQUENTIAL_READ */
  573. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  574. /* Read the entire image. If the image has an alpha channel or a tRNS
  575. * chunk, and you have called png_handle_alpha()[*], you will need to
  576. * initialize the image to the current image that PNG will be overlaying.
  577. * We set the num_rows again here, in case it was incorrectly set in
  578. * png_read_start_row() by a call to png_read_update_info() or
  579. * png_start_read_image() if png_set_interlace_handling() wasn't called
  580. * prior to either of these functions like it should have been. You can
  581. * only call this function once. If you desire to have an image for
  582. * each pass of a interlaced image, use png_read_rows() instead.
  583. *
  584. * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  585. */
  586. void PNGAPI
  587. png_read_image(png_structrp png_ptr, png_bytepp image)
  588. {
  589. png_uint_32 i, image_height;
  590. int pass, j;
  591. png_bytepp rp;
  592. png_debug(1, "in png_read_image");
  593. if (png_ptr == NULL)
  594. return;
  595. #ifdef PNG_READ_INTERLACING_SUPPORTED
  596. if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
  597. {
  598. pass = png_set_interlace_handling(png_ptr);
  599. /* And make sure transforms are initialized. */
  600. png_start_read_image(png_ptr);
  601. }
  602. else
  603. {
  604. if (png_ptr->interlaced != 0 &&
  605. (png_ptr->transformations & PNG_INTERLACE) == 0)
  606. {
  607. /* Caller called png_start_read_image or png_read_update_info without
  608. * first turning on the PNG_INTERLACE transform. We can fix this here,
  609. * but the caller should do it!
  610. */
  611. png_warning(png_ptr, "Interlace handling should be turned on when "
  612. "using png_read_image");
  613. /* Make sure this is set correctly */
  614. png_ptr->num_rows = png_ptr->height;
  615. }
  616. /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
  617. * the above error case.
  618. */
  619. pass = png_set_interlace_handling(png_ptr);
  620. }
  621. #else
  622. if (png_ptr->interlaced)
  623. png_error(png_ptr,
  624. "Cannot read interlaced image -- interlace handler disabled");
  625. pass = 1;
  626. #endif
  627. image_height=png_ptr->height;
  628. for (j = 0; j < pass; j++)
  629. {
  630. rp = image;
  631. for (i = 0; i < image_height; i++)
  632. {
  633. png_read_row(png_ptr, *rp, NULL);
  634. rp++;
  635. }
  636. }
  637. }
  638. #endif /* SEQUENTIAL_READ */
  639. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  640. /* Read the end of the PNG file. Will not read past the end of the
  641. * file, will verify the end is accurate, and will read any comments
  642. * or time information at the end of the file, if info is not NULL.
  643. */
  644. void PNGAPI
  645. png_read_end(png_structrp png_ptr, png_inforp info_ptr)
  646. {
  647. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  648. int keep;
  649. #endif
  650. png_debug(1, "in png_read_end");
  651. if (png_ptr == NULL)
  652. return;
  653. /* If png_read_end is called in the middle of reading the rows there may
  654. * still be pending IDAT data and an owned zstream. Deal with this here.
  655. */
  656. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  657. if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0)
  658. #endif
  659. png_read_finish_IDAT(png_ptr);
  660. #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
  661. /* Report invalid palette index; added at libng-1.5.10 */
  662. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  663. png_ptr->num_palette_max > png_ptr->num_palette)
  664. png_benign_error(png_ptr, "Read palette index exceeding num_palette");
  665. #endif
  666. do
  667. {
  668. png_uint_32 length = png_read_chunk_header(png_ptr);
  669. png_uint_32 chunk_name = png_ptr->chunk_name;
  670. if (chunk_name != png_IDAT)
  671. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  672. if (chunk_name == png_IEND)
  673. png_handle_IEND(png_ptr, info_ptr, length);
  674. else if (chunk_name == png_IHDR)
  675. png_handle_IHDR(png_ptr, info_ptr, length);
  676. else if (info_ptr == NULL)
  677. png_crc_finish(png_ptr, length);
  678. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  679. else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
  680. {
  681. if (chunk_name == png_IDAT)
  682. {
  683. if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
  684. || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
  685. png_benign_error(png_ptr, ".Too many IDATs found");
  686. }
  687. png_handle_unknown(png_ptr, info_ptr, length, keep);
  688. if (chunk_name == png_PLTE)
  689. png_ptr->mode |= PNG_HAVE_PLTE;
  690. }
  691. #endif
  692. else if (chunk_name == png_IDAT)
  693. {
  694. /* Zero length IDATs are legal after the last IDAT has been
  695. * read, but not after other chunks have been read. 1.6 does not
  696. * always read all the deflate data; specifically it cannot be relied
  697. * upon to read the Adler32 at the end. If it doesn't ignore IDAT
  698. * chunks which are longer than zero as well:
  699. */
  700. if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
  701. || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
  702. png_benign_error(png_ptr, "..Too many IDATs found");
  703. png_crc_finish(png_ptr, length);
  704. }
  705. else if (chunk_name == png_PLTE)
  706. png_handle_PLTE(png_ptr, info_ptr, length);
  707. #ifdef PNG_READ_bKGD_SUPPORTED
  708. else if (chunk_name == png_bKGD)
  709. png_handle_bKGD(png_ptr, info_ptr, length);
  710. #endif
  711. #ifdef PNG_READ_cHRM_SUPPORTED
  712. else if (chunk_name == png_cHRM)
  713. png_handle_cHRM(png_ptr, info_ptr, length);
  714. #endif
  715. #ifdef PNG_READ_eXIf_SUPPORTED
  716. else if (chunk_name == png_eXIf)
  717. png_handle_eXIf(png_ptr, info_ptr, length);
  718. #endif
  719. #ifdef PNG_READ_gAMA_SUPPORTED
  720. else if (chunk_name == png_gAMA)
  721. png_handle_gAMA(png_ptr, info_ptr, length);
  722. #endif
  723. #ifdef PNG_READ_hIST_SUPPORTED
  724. else if (chunk_name == png_hIST)
  725. png_handle_hIST(png_ptr, info_ptr, length);
  726. #endif
  727. #ifdef PNG_READ_oFFs_SUPPORTED
  728. else if (chunk_name == png_oFFs)
  729. png_handle_oFFs(png_ptr, info_ptr, length);
  730. #endif
  731. #ifdef PNG_READ_pCAL_SUPPORTED
  732. else if (chunk_name == png_pCAL)
  733. png_handle_pCAL(png_ptr, info_ptr, length);
  734. #endif
  735. #ifdef PNG_READ_sCAL_SUPPORTED
  736. else if (chunk_name == png_sCAL)
  737. png_handle_sCAL(png_ptr, info_ptr, length);
  738. #endif
  739. #ifdef PNG_READ_pHYs_SUPPORTED
  740. else if (chunk_name == png_pHYs)
  741. png_handle_pHYs(png_ptr, info_ptr, length);
  742. #endif
  743. #ifdef PNG_READ_sBIT_SUPPORTED
  744. else if (chunk_name == png_sBIT)
  745. png_handle_sBIT(png_ptr, info_ptr, length);
  746. #endif
  747. #ifdef PNG_READ_sRGB_SUPPORTED
  748. else if (chunk_name == png_sRGB)
  749. png_handle_sRGB(png_ptr, info_ptr, length);
  750. #endif
  751. #ifdef PNG_READ_iCCP_SUPPORTED
  752. else if (chunk_name == png_iCCP)
  753. png_handle_iCCP(png_ptr, info_ptr, length);
  754. #endif
  755. #ifdef PNG_READ_sPLT_SUPPORTED
  756. else if (chunk_name == png_sPLT)
  757. png_handle_sPLT(png_ptr, info_ptr, length);
  758. #endif
  759. #ifdef PNG_READ_tEXt_SUPPORTED
  760. else if (chunk_name == png_tEXt)
  761. png_handle_tEXt(png_ptr, info_ptr, length);
  762. #endif
  763. #ifdef PNG_READ_tIME_SUPPORTED
  764. else if (chunk_name == png_tIME)
  765. png_handle_tIME(png_ptr, info_ptr, length);
  766. #endif
  767. #ifdef PNG_READ_tRNS_SUPPORTED
  768. else if (chunk_name == png_tRNS)
  769. png_handle_tRNS(png_ptr, info_ptr, length);
  770. #endif
  771. #ifdef PNG_READ_zTXt_SUPPORTED
  772. else if (chunk_name == png_zTXt)
  773. png_handle_zTXt(png_ptr, info_ptr, length);
  774. #endif
  775. #ifdef PNG_READ_iTXt_SUPPORTED
  776. else if (chunk_name == png_iTXt)
  777. png_handle_iTXt(png_ptr, info_ptr, length);
  778. #endif
  779. else
  780. png_handle_unknown(png_ptr, info_ptr, length,
  781. PNG_HANDLE_CHUNK_AS_DEFAULT);
  782. } while ((png_ptr->mode & PNG_HAVE_IEND) == 0);
  783. }
  784. #endif /* SEQUENTIAL_READ */
  785. /* Free all memory used in the read struct */
  786. static void
  787. png_read_destroy(png_structrp png_ptr)
  788. {
  789. png_debug(1, "in png_read_destroy");
  790. #ifdef PNG_READ_GAMMA_SUPPORTED
  791. png_destroy_gamma_table(png_ptr);
  792. #endif
  793. png_free(png_ptr, png_ptr->big_row_buf);
  794. png_ptr->big_row_buf = NULL;
  795. png_free(png_ptr, png_ptr->big_prev_row);
  796. png_ptr->big_prev_row = NULL;
  797. png_free(png_ptr, png_ptr->read_buffer);
  798. png_ptr->read_buffer = NULL;
  799. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  800. png_free(png_ptr, png_ptr->palette_lookup);
  801. png_ptr->palette_lookup = NULL;
  802. png_free(png_ptr, png_ptr->quantize_index);
  803. png_ptr->quantize_index = NULL;
  804. #endif
  805. if ((png_ptr->free_me & PNG_FREE_PLTE) != 0)
  806. {
  807. png_zfree(png_ptr, png_ptr->palette);
  808. png_ptr->palette = NULL;
  809. }
  810. png_ptr->free_me &= ~PNG_FREE_PLTE;
  811. #if defined(PNG_tRNS_SUPPORTED) || \
  812. defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  813. if ((png_ptr->free_me & PNG_FREE_TRNS) != 0)
  814. {
  815. png_free(png_ptr, png_ptr->trans_alpha);
  816. png_ptr->trans_alpha = NULL;
  817. }
  818. png_ptr->free_me &= ~PNG_FREE_TRNS;
  819. #endif
  820. inflateEnd(&png_ptr->zstream);
  821. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  822. png_free(png_ptr, png_ptr->save_buffer);
  823. png_ptr->save_buffer = NULL;
  824. #endif
  825. #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
  826. defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  827. png_free(png_ptr, png_ptr->unknown_chunk.data);
  828. png_ptr->unknown_chunk.data = NULL;
  829. #endif
  830. #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  831. png_free(png_ptr, png_ptr->chunk_list);
  832. png_ptr->chunk_list = NULL;
  833. #endif
  834. #if defined(PNG_READ_EXPAND_SUPPORTED) && \
  835. defined(PNG_ARM_NEON_IMPLEMENTATION)
  836. png_free(png_ptr, png_ptr->riffled_palette);
  837. png_ptr->riffled_palette = NULL;
  838. #endif
  839. /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
  840. * callbacks are still set at this point. They are required to complete the
  841. * destruction of the png_struct itself.
  842. */
  843. }
  844. /* Free all memory used by the read */
  845. void PNGAPI
  846. png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
  847. png_infopp end_info_ptr_ptr)
  848. {
  849. png_structrp png_ptr = NULL;
  850. png_debug(1, "in png_destroy_read_struct");
  851. if (png_ptr_ptr != NULL)
  852. png_ptr = *png_ptr_ptr;
  853. if (png_ptr == NULL)
  854. return;
  855. /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
  856. * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
  857. * The extra was, apparently, unnecessary yet this hides memory leak bugs.
  858. */
  859. png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
  860. png_destroy_info_struct(png_ptr, info_ptr_ptr);
  861. *png_ptr_ptr = NULL;
  862. png_read_destroy(png_ptr);
  863. png_destroy_png_struct(png_ptr);
  864. }
  865. void PNGAPI
  866. png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
  867. {
  868. if (png_ptr == NULL)
  869. return;
  870. png_ptr->read_row_fn = read_row_fn;
  871. }
  872. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  873. #ifdef PNG_INFO_IMAGE_SUPPORTED
  874. void PNGAPI
  875. png_read_png(png_structrp png_ptr, png_inforp info_ptr,
  876. int transforms, voidp params)
  877. {
  878. if (png_ptr == NULL || info_ptr == NULL)
  879. return;
  880. /* png_read_info() gives us all of the information from the
  881. * PNG file before the first IDAT (image data chunk).
  882. */
  883. png_read_info(png_ptr, info_ptr);
  884. if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
  885. png_error(png_ptr, "Image is too high to process with png_read_png()");
  886. /* -------------- image transformations start here ------------------- */
  887. /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
  888. * is not implemented. This will only happen in de-configured (non-default)
  889. * libpng builds. The results can be unexpected - png_read_png may return
  890. * short or mal-formed rows because the transform is skipped.
  891. */
  892. /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
  893. */
  894. if ((transforms & PNG_TRANSFORM_SCALE_16) != 0)
  895. /* Added at libpng-1.5.4. "strip_16" produces the same result that it
  896. * did in earlier versions, while "scale_16" is now more accurate.
  897. */
  898. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  899. png_set_scale_16(png_ptr);
  900. #else
  901. png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
  902. #endif
  903. /* If both SCALE and STRIP are required pngrtran will effectively cancel the
  904. * latter by doing SCALE first. This is ok and allows apps not to check for
  905. * which is supported to get the right answer.
  906. */
  907. if ((transforms & PNG_TRANSFORM_STRIP_16) != 0)
  908. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  909. png_set_strip_16(png_ptr);
  910. #else
  911. png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
  912. #endif
  913. /* Strip alpha bytes from the input data without combining with
  914. * the background (not recommended).
  915. */
  916. if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0)
  917. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  918. png_set_strip_alpha(png_ptr);
  919. #else
  920. png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
  921. #endif
  922. /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
  923. * byte into separate bytes (useful for paletted and grayscale images).
  924. */
  925. if ((transforms & PNG_TRANSFORM_PACKING) != 0)
  926. #ifdef PNG_READ_PACK_SUPPORTED
  927. png_set_packing(png_ptr);
  928. #else
  929. png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
  930. #endif
  931. /* Change the order of packed pixels to least significant bit first
  932. * (not useful if you are using png_set_packing).
  933. */
  934. if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
  935. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  936. png_set_packswap(png_ptr);
  937. #else
  938. png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
  939. #endif
  940. /* Expand paletted colors into true RGB triplets
  941. * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
  942. * Expand paletted or RGB images with transparency to full alpha
  943. * channels so the data will be available as RGBA quartets.
  944. */
  945. if ((transforms & PNG_TRANSFORM_EXPAND) != 0)
  946. #ifdef PNG_READ_EXPAND_SUPPORTED
  947. png_set_expand(png_ptr);
  948. #else
  949. png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
  950. #endif
  951. /* We don't handle background color or gamma transformation or quantizing.
  952. */
  953. /* Invert monochrome files to have 0 as white and 1 as black
  954. */
  955. if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
  956. #ifdef PNG_READ_INVERT_SUPPORTED
  957. png_set_invert_mono(png_ptr);
  958. #else
  959. png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
  960. #endif
  961. /* If you want to shift the pixel values from the range [0,255] or
  962. * [0,65535] to the original [0,7] or [0,31], or whatever range the
  963. * colors were originally in:
  964. */
  965. if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
  966. #ifdef PNG_READ_SHIFT_SUPPORTED
  967. if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
  968. png_set_shift(png_ptr, &info_ptr->sig_bit);
  969. #else
  970. png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
  971. #endif
  972. /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
  973. if ((transforms & PNG_TRANSFORM_BGR) != 0)
  974. #ifdef PNG_READ_BGR_SUPPORTED
  975. png_set_bgr(png_ptr);
  976. #else
  977. png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
  978. #endif
  979. /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
  980. if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
  981. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  982. png_set_swap_alpha(png_ptr);
  983. #else
  984. png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
  985. #endif
  986. /* Swap bytes of 16-bit files to least significant byte first */
  987. if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
  988. #ifdef PNG_READ_SWAP_SUPPORTED
  989. png_set_swap(png_ptr);
  990. #else
  991. png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
  992. #endif
  993. /* Added at libpng-1.2.41 */
  994. /* Invert the alpha channel from opacity to transparency */
  995. if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
  996. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  997. png_set_invert_alpha(png_ptr);
  998. #else
  999. png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
  1000. #endif
  1001. /* Added at libpng-1.2.41 */
  1002. /* Expand grayscale image to RGB */
  1003. if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0)
  1004. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1005. png_set_gray_to_rgb(png_ptr);
  1006. #else
  1007. png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
  1008. #endif
  1009. /* Added at libpng-1.5.4 */
  1010. if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0)
  1011. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  1012. png_set_expand_16(png_ptr);
  1013. #else
  1014. png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
  1015. #endif
  1016. /* We don't handle adding filler bytes */
  1017. /* We use png_read_image and rely on that for interlace handling, but we also
  1018. * call png_read_update_info therefore must turn on interlace handling now:
  1019. */
  1020. (void)png_set_interlace_handling(png_ptr);
  1021. /* Optional call to gamma correct and add the background to the palette
  1022. * and update info structure. REQUIRED if you are expecting libpng to
  1023. * update the palette for you (i.e., you selected such a transform above).
  1024. */
  1025. png_read_update_info(png_ptr, info_ptr);
  1026. /* -------------- image transformations end here ------------------- */
  1027. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1028. if (info_ptr->row_pointers == NULL)
  1029. {
  1030. png_uint_32 iptr;
  1031. info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
  1032. info_ptr->height * (sizeof (png_bytep))));
  1033. for (iptr=0; iptr<info_ptr->height; iptr++)
  1034. info_ptr->row_pointers[iptr] = NULL;
  1035. info_ptr->free_me |= PNG_FREE_ROWS;
  1036. for (iptr = 0; iptr < info_ptr->height; iptr++)
  1037. info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
  1038. png_malloc(png_ptr, info_ptr->rowbytes));
  1039. }
  1040. png_read_image(png_ptr, info_ptr->row_pointers);
  1041. info_ptr->valid |= PNG_INFO_IDAT;
  1042. /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
  1043. png_read_end(png_ptr, info_ptr);
  1044. PNG_UNUSED(params)
  1045. }
  1046. #endif /* INFO_IMAGE */
  1047. #endif /* SEQUENTIAL_READ */
  1048. #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
  1049. /* SIMPLIFIED READ
  1050. *
  1051. * This code currently relies on the sequential reader, though it could easily
  1052. * be made to work with the progressive one.
  1053. */
  1054. /* Arguments to png_image_finish_read: */
  1055. /* Encoding of PNG data (used by the color-map code) */
  1056. # define P_NOTSET 0 /* File encoding not yet known */
  1057. # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
  1058. # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
  1059. # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
  1060. # define P_LINEAR8 4 /* 8-bit linear: only from a file value */
  1061. /* Color-map processing: after libpng has run on the PNG image further
  1062. * processing may be needed to convert the data to color-map indices.
  1063. */
  1064. #define PNG_CMAP_NONE 0
  1065. #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
  1066. #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
  1067. #define PNG_CMAP_RGB 3 /* Process RGB data */
  1068. #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
  1069. /* The following document where the background is for each processing case. */
  1070. #define PNG_CMAP_NONE_BACKGROUND 256
  1071. #define PNG_CMAP_GA_BACKGROUND 231
  1072. #define PNG_CMAP_TRANS_BACKGROUND 254
  1073. #define PNG_CMAP_RGB_BACKGROUND 256
  1074. #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
  1075. typedef struct
  1076. {
  1077. /* Arguments: */
  1078. png_imagep image;
  1079. png_voidp buffer;
  1080. png_int_32 row_stride;
  1081. png_voidp colormap;
  1082. png_const_colorp background;
  1083. /* Local variables: */
  1084. png_voidp local_row;
  1085. png_voidp first_row;
  1086. ptrdiff_t row_bytes; /* step between rows */
  1087. int file_encoding; /* E_ values above */
  1088. png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
  1089. int colormap_processing; /* PNG_CMAP_ values above */
  1090. } png_image_read_control;
  1091. /* Do all the *safe* initialization - 'safe' means that png_error won't be
  1092. * called, so setting up the jmp_buf is not required. This means that anything
  1093. * called from here must *not* call png_malloc - it has to call png_malloc_warn
  1094. * instead so that control is returned safely back to this routine.
  1095. */
  1096. static int
  1097. png_image_read_init(png_imagep image)
  1098. {
  1099. if (image->opaque == NULL)
  1100. {
  1101. png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
  1102. png_safe_error, png_safe_warning);
  1103. /* And set the rest of the structure to NULL to ensure that the various
  1104. * fields are consistent.
  1105. */
  1106. memset(image, 0, (sizeof *image));
  1107. image->version = PNG_IMAGE_VERSION;
  1108. if (png_ptr != NULL)
  1109. {
  1110. png_infop info_ptr = png_create_info_struct(png_ptr);
  1111. if (info_ptr != NULL)
  1112. {
  1113. png_controlp control = png_voidcast(png_controlp,
  1114. png_malloc_warn(png_ptr, (sizeof *control)));
  1115. if (control != NULL)
  1116. {
  1117. memset(control, 0, (sizeof *control));
  1118. control->png_ptr = png_ptr;
  1119. control->info_ptr = info_ptr;
  1120. control->for_write = 0;
  1121. image->opaque = control;
  1122. return 1;
  1123. }
  1124. /* Error clean up */
  1125. png_destroy_info_struct(png_ptr, &info_ptr);
  1126. }
  1127. png_destroy_read_struct(&png_ptr, NULL, NULL);
  1128. }
  1129. return png_image_error(image, "png_image_read: out of memory");
  1130. }
  1131. return png_image_error(image, "png_image_read: opaque pointer not NULL");
  1132. }
  1133. /* Utility to find the base format of a PNG file from a png_struct. */
  1134. static png_uint_32
  1135. png_image_format(png_structrp png_ptr)
  1136. {
  1137. png_uint_32 format = 0;
  1138. if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
  1139. format |= PNG_FORMAT_FLAG_COLOR;
  1140. if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  1141. format |= PNG_FORMAT_FLAG_ALPHA;
  1142. /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
  1143. * sets the png_struct fields; that's all we are interested in here. The
  1144. * precise interaction with an app call to png_set_tRNS and PNG file reading
  1145. * is unclear.
  1146. */
  1147. else if (png_ptr->num_trans > 0)
  1148. format |= PNG_FORMAT_FLAG_ALPHA;
  1149. if (png_ptr->bit_depth == 16)
  1150. format |= PNG_FORMAT_FLAG_LINEAR;
  1151. if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0)
  1152. format |= PNG_FORMAT_FLAG_COLORMAP;
  1153. return format;
  1154. }
  1155. /* Is the given gamma significantly different from sRGB? The test is the same
  1156. * one used in pngrtran.c when deciding whether to do gamma correction. The
  1157. * arithmetic optimizes the division by using the fact that the inverse of the
  1158. * file sRGB gamma is 2.2
  1159. */
  1160. static int
  1161. png_gamma_not_sRGB(png_fixed_point g)
  1162. {
  1163. if (g < PNG_FP_1)
  1164. {
  1165. /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
  1166. if (g == 0)
  1167. return 0;
  1168. return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
  1169. }
  1170. return 1;
  1171. }
  1172. /* Do the main body of a 'png_image_begin_read' function; read the PNG file
  1173. * header and fill in all the information. This is executed in a safe context,
  1174. * unlike the init routine above.
  1175. */
  1176. static int
  1177. png_image_read_header(png_voidp argument)
  1178. {
  1179. png_imagep image = png_voidcast(png_imagep, argument);
  1180. png_structrp png_ptr = image->opaque->png_ptr;
  1181. png_inforp info_ptr = image->opaque->info_ptr;
  1182. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  1183. png_set_benign_errors(png_ptr, 1/*warn*/);
  1184. #endif
  1185. png_read_info(png_ptr, info_ptr);
  1186. /* Do this the fast way; just read directly out of png_struct. */
  1187. image->width = png_ptr->width;
  1188. image->height = png_ptr->height;
  1189. {
  1190. png_uint_32 format = png_image_format(png_ptr);
  1191. image->format = format;
  1192. #ifdef PNG_COLORSPACE_SUPPORTED
  1193. /* Does the colorspace match sRGB? If there is no color endpoint
  1194. * (colorant) information assume yes, otherwise require the
  1195. * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
  1196. * colorspace has been determined to be invalid ignore it.
  1197. */
  1198. if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
  1199. & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
  1200. PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
  1201. image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
  1202. #endif
  1203. }
  1204. /* We need the maximum number of entries regardless of the format the
  1205. * application sets here.
  1206. */
  1207. {
  1208. png_uint_32 cmap_entries;
  1209. switch (png_ptr->color_type)
  1210. {
  1211. case PNG_COLOR_TYPE_GRAY:
  1212. cmap_entries = 1U << png_ptr->bit_depth;
  1213. break;
  1214. case PNG_COLOR_TYPE_PALETTE:
  1215. cmap_entries = (png_uint_32)png_ptr->num_palette;
  1216. break;
  1217. default:
  1218. cmap_entries = 256;
  1219. break;
  1220. }
  1221. if (cmap_entries > 256)
  1222. cmap_entries = 256;
  1223. image->colormap_entries = cmap_entries;
  1224. }
  1225. return 1;
  1226. }
  1227. #ifdef PNG_STDIO_SUPPORTED
  1228. int PNGAPI
  1229. png_image_begin_read_from_stdio(png_imagep image, FILE* file)
  1230. {
  1231. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1232. {
  1233. if (file != NULL)
  1234. {
  1235. if (png_image_read_init(image) != 0)
  1236. {
  1237. /* This is slightly evil, but png_init_io doesn't do anything other
  1238. * than this and we haven't changed the standard IO functions so
  1239. * this saves a 'safe' function.
  1240. */
  1241. image->opaque->png_ptr->io_ptr = file;
  1242. return png_safe_execute(image, png_image_read_header, image);
  1243. }
  1244. }
  1245. else
  1246. return png_image_error(image,
  1247. "png_image_begin_read_from_stdio: invalid argument");
  1248. }
  1249. else if (image != NULL)
  1250. return png_image_error(image,
  1251. "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
  1252. return 0;
  1253. }
  1254. int PNGAPI
  1255. png_image_begin_read_from_file(png_imagep image, const char *file_name)
  1256. {
  1257. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1258. {
  1259. if (file_name != NULL)
  1260. {
  1261. FILE *fp = fopen(file_name, "rb");
  1262. if (fp != NULL)
  1263. {
  1264. if (png_image_read_init(image) != 0)
  1265. {
  1266. image->opaque->png_ptr->io_ptr = fp;
  1267. image->opaque->owned_file = 1;
  1268. return png_safe_execute(image, png_image_read_header, image);
  1269. }
  1270. /* Clean up: just the opened file. */
  1271. (void)fclose(fp);
  1272. }
  1273. else
  1274. return png_image_error(image, strerror(errno));
  1275. }
  1276. else
  1277. return png_image_error(image,
  1278. "png_image_begin_read_from_file: invalid argument");
  1279. }
  1280. else if (image != NULL)
  1281. return png_image_error(image,
  1282. "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
  1283. return 0;
  1284. }
  1285. #endif /* STDIO */
  1286. static void PNGCBAPI
  1287. png_image_memory_read(png_structp png_ptr, png_bytep out, size_t need)
  1288. {
  1289. if (png_ptr != NULL)
  1290. {
  1291. png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
  1292. if (image != NULL)
  1293. {
  1294. png_controlp cp = image->opaque;
  1295. if (cp != NULL)
  1296. {
  1297. png_const_bytep memory = cp->memory;
  1298. size_t size = cp->size;
  1299. if (memory != NULL && size >= need)
  1300. {
  1301. memcpy(out, memory, need);
  1302. cp->memory = memory + need;
  1303. cp->size = size - need;
  1304. return;
  1305. }
  1306. png_error(png_ptr, "read beyond end of data");
  1307. }
  1308. }
  1309. png_error(png_ptr, "invalid memory read");
  1310. }
  1311. }
  1312. int PNGAPI png_image_begin_read_from_memory(png_imagep image,
  1313. png_const_voidp memory, size_t size)
  1314. {
  1315. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1316. {
  1317. if (memory != NULL && size > 0)
  1318. {
  1319. if (png_image_read_init(image) != 0)
  1320. {
  1321. /* Now set the IO functions to read from the memory buffer and
  1322. * store it into io_ptr. Again do this in-place to avoid calling a
  1323. * libpng function that requires error handling.
  1324. */
  1325. image->opaque->memory = png_voidcast(png_const_bytep, memory);
  1326. image->opaque->size = size;
  1327. image->opaque->png_ptr->io_ptr = image;
  1328. image->opaque->png_ptr->read_data_fn = png_image_memory_read;
  1329. return png_safe_execute(image, png_image_read_header, image);
  1330. }
  1331. }
  1332. else
  1333. return png_image_error(image,
  1334. "png_image_begin_read_from_memory: invalid argument");
  1335. }
  1336. else if (image != NULL)
  1337. return png_image_error(image,
  1338. "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
  1339. return 0;
  1340. }
  1341. /* Utility function to skip chunks that are not used by the simplified image
  1342. * read functions and an appropriate macro to call it.
  1343. */
  1344. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  1345. static void
  1346. png_image_skip_unused_chunks(png_structrp png_ptr)
  1347. {
  1348. /* Prepare the reader to ignore all recognized chunks whose data will not
  1349. * be used, i.e., all chunks recognized by libpng except for those
  1350. * involved in basic image reading:
  1351. *
  1352. * IHDR, PLTE, IDAT, IEND
  1353. *
  1354. * Or image data handling:
  1355. *
  1356. * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
  1357. *
  1358. * This provides a small performance improvement and eliminates any
  1359. * potential vulnerability to security problems in the unused chunks.
  1360. *
  1361. * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
  1362. * too. This allows the simplified API to be compiled without iCCP support,
  1363. * however if the support is there the chunk is still checked to detect
  1364. * errors (which are unfortunately quite common.)
  1365. */
  1366. {
  1367. static const png_byte chunks_to_process[] = {
  1368. 98, 75, 71, 68, '\0', /* bKGD */
  1369. 99, 72, 82, 77, '\0', /* cHRM */
  1370. 103, 65, 77, 65, '\0', /* gAMA */
  1371. # ifdef PNG_READ_iCCP_SUPPORTED
  1372. 105, 67, 67, 80, '\0', /* iCCP */
  1373. # endif
  1374. 115, 66, 73, 84, '\0', /* sBIT */
  1375. 115, 82, 71, 66, '\0', /* sRGB */
  1376. };
  1377. /* Ignore unknown chunks and all other chunks except for the
  1378. * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
  1379. */
  1380. png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
  1381. NULL, -1);
  1382. /* But do not ignore image data handling chunks */
  1383. png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
  1384. chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5);
  1385. }
  1386. }
  1387. # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
  1388. #else
  1389. # define PNG_SKIP_CHUNKS(p) ((void)0)
  1390. #endif /* HANDLE_AS_UNKNOWN */
  1391. /* The following macro gives the exact rounded answer for all values in the
  1392. * range 0..255 (it actually divides by 51.2, but the rounding still generates
  1393. * the correct numbers 0..5
  1394. */
  1395. #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
  1396. /* Utility functions to make particular color-maps */
  1397. static void
  1398. set_file_encoding(png_image_read_control *display)
  1399. {
  1400. png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
  1401. if (png_gamma_significant(g) != 0)
  1402. {
  1403. if (png_gamma_not_sRGB(g) != 0)
  1404. {
  1405. display->file_encoding = P_FILE;
  1406. display->gamma_to_linear = png_reciprocal(g);
  1407. }
  1408. else
  1409. display->file_encoding = P_sRGB;
  1410. }
  1411. else
  1412. display->file_encoding = P_LINEAR8;
  1413. }
  1414. static unsigned int
  1415. decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
  1416. {
  1417. if (encoding == P_FILE) /* double check */
  1418. encoding = display->file_encoding;
  1419. if (encoding == P_NOTSET) /* must be the file encoding */
  1420. {
  1421. set_file_encoding(display);
  1422. encoding = display->file_encoding;
  1423. }
  1424. switch (encoding)
  1425. {
  1426. case P_FILE:
  1427. value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
  1428. break;
  1429. case P_sRGB:
  1430. value = png_sRGB_table[value];
  1431. break;
  1432. case P_LINEAR:
  1433. break;
  1434. case P_LINEAR8:
  1435. value *= 257;
  1436. break;
  1437. #ifdef __GNUC__
  1438. default:
  1439. png_error(display->image->opaque->png_ptr,
  1440. "unexpected encoding (internal error)");
  1441. #endif
  1442. }
  1443. return value;
  1444. }
  1445. static png_uint_32
  1446. png_colormap_compose(png_image_read_control *display,
  1447. png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
  1448. png_uint_32 background, int encoding)
  1449. {
  1450. /* The file value is composed on the background, the background has the given
  1451. * encoding and so does the result, the file is encoded with P_FILE and the
  1452. * file and alpha are 8-bit values. The (output) encoding will always be
  1453. * P_LINEAR or P_sRGB.
  1454. */
  1455. png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
  1456. png_uint_32 b = decode_gamma(display, background, encoding);
  1457. /* The alpha is always an 8-bit value (it comes from the palette), the value
  1458. * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
  1459. */
  1460. f = f * alpha + b * (255-alpha);
  1461. if (encoding == P_LINEAR)
  1462. {
  1463. /* Scale to 65535; divide by 255, approximately (in fact this is extremely
  1464. * accurate, it divides by 255.00000005937181414556, with no overflow.)
  1465. */
  1466. f *= 257; /* Now scaled by 65535 */
  1467. f += f >> 16;
  1468. f = (f+32768) >> 16;
  1469. }
  1470. else /* P_sRGB */
  1471. f = PNG_sRGB_FROM_LINEAR(f);
  1472. return f;
  1473. }
  1474. /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
  1475. * be 8-bit.
  1476. */
  1477. static void
  1478. png_create_colormap_entry(png_image_read_control *display,
  1479. png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
  1480. png_uint_32 alpha, int encoding)
  1481. {
  1482. png_imagep image = display->image;
  1483. int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
  1484. P_LINEAR : P_sRGB;
  1485. int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
  1486. (red != green || green != blue);
  1487. if (ip > 255)
  1488. png_error(image->opaque->png_ptr, "color-map index out of range");
  1489. /* Update the cache with whether the file gamma is significantly different
  1490. * from sRGB.
  1491. */
  1492. if (encoding == P_FILE)
  1493. {
  1494. if (display->file_encoding == P_NOTSET)
  1495. set_file_encoding(display);
  1496. /* Note that the cached value may be P_FILE too, but if it is then the
  1497. * gamma_to_linear member has been set.
  1498. */
  1499. encoding = display->file_encoding;
  1500. }
  1501. if (encoding == P_FILE)
  1502. {
  1503. png_fixed_point g = display->gamma_to_linear;
  1504. red = png_gamma_16bit_correct(red*257, g);
  1505. green = png_gamma_16bit_correct(green*257, g);
  1506. blue = png_gamma_16bit_correct(blue*257, g);
  1507. if (convert_to_Y != 0 || output_encoding == P_LINEAR)
  1508. {
  1509. alpha *= 257;
  1510. encoding = P_LINEAR;
  1511. }
  1512. else
  1513. {
  1514. red = PNG_sRGB_FROM_LINEAR(red * 255);
  1515. green = PNG_sRGB_FROM_LINEAR(green * 255);
  1516. blue = PNG_sRGB_FROM_LINEAR(blue * 255);
  1517. encoding = P_sRGB;
  1518. }
  1519. }
  1520. else if (encoding == P_LINEAR8)
  1521. {
  1522. /* This encoding occurs quite frequently in test cases because PngSuite
  1523. * includes a gAMA 1.0 chunk with most images.
  1524. */
  1525. red *= 257;
  1526. green *= 257;
  1527. blue *= 257;
  1528. alpha *= 257;
  1529. encoding = P_LINEAR;
  1530. }
  1531. else if (encoding == P_sRGB &&
  1532. (convert_to_Y != 0 || output_encoding == P_LINEAR))
  1533. {
  1534. /* The values are 8-bit sRGB values, but must be converted to 16-bit
  1535. * linear.
  1536. */
  1537. red = png_sRGB_table[red];
  1538. green = png_sRGB_table[green];
  1539. blue = png_sRGB_table[blue];
  1540. alpha *= 257;
  1541. encoding = P_LINEAR;
  1542. }
  1543. /* This is set if the color isn't gray but the output is. */
  1544. if (encoding == P_LINEAR)
  1545. {
  1546. if (convert_to_Y != 0)
  1547. {
  1548. /* NOTE: these values are copied from png_do_rgb_to_gray */
  1549. png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
  1550. (png_uint_32)2366 * blue;
  1551. if (output_encoding == P_LINEAR)
  1552. y = (y + 16384) >> 15;
  1553. else
  1554. {
  1555. /* y is scaled by 32768, we need it scaled by 255: */
  1556. y = (y + 128) >> 8;
  1557. y *= 255;
  1558. y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
  1559. alpha = PNG_DIV257(alpha);
  1560. encoding = P_sRGB;
  1561. }
  1562. blue = red = green = y;
  1563. }
  1564. else if (output_encoding == P_sRGB)
  1565. {
  1566. red = PNG_sRGB_FROM_LINEAR(red * 255);
  1567. green = PNG_sRGB_FROM_LINEAR(green * 255);
  1568. blue = PNG_sRGB_FROM_LINEAR(blue * 255);
  1569. alpha = PNG_DIV257(alpha);
  1570. encoding = P_sRGB;
  1571. }
  1572. }
  1573. if (encoding != output_encoding)
  1574. png_error(image->opaque->png_ptr, "bad encoding (internal error)");
  1575. /* Store the value. */
  1576. {
  1577. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  1578. int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
  1579. (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
  1580. # else
  1581. # define afirst 0
  1582. # endif
  1583. # ifdef PNG_FORMAT_BGR_SUPPORTED
  1584. int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
  1585. # else
  1586. # define bgr 0
  1587. # endif
  1588. if (output_encoding == P_LINEAR)
  1589. {
  1590. png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
  1591. entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
  1592. /* The linear 16-bit values must be pre-multiplied by the alpha channel
  1593. * value, if less than 65535 (this is, effectively, composite on black
  1594. * if the alpha channel is removed.)
  1595. */
  1596. switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
  1597. {
  1598. case 4:
  1599. entry[afirst ? 0 : 3] = (png_uint_16)alpha;
  1600. /* FALLTHROUGH */
  1601. case 3:
  1602. if (alpha < 65535)
  1603. {
  1604. if (alpha > 0)
  1605. {
  1606. blue = (blue * alpha + 32767U)/65535U;
  1607. green = (green * alpha + 32767U)/65535U;
  1608. red = (red * alpha + 32767U)/65535U;
  1609. }
  1610. else
  1611. red = green = blue = 0;
  1612. }
  1613. entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
  1614. entry[afirst + 1] = (png_uint_16)green;
  1615. entry[afirst + bgr] = (png_uint_16)red;
  1616. break;
  1617. case 2:
  1618. entry[1 ^ afirst] = (png_uint_16)alpha;
  1619. /* FALLTHROUGH */
  1620. case 1:
  1621. if (alpha < 65535)
  1622. {
  1623. if (alpha > 0)
  1624. green = (green * alpha + 32767U)/65535U;
  1625. else
  1626. green = 0;
  1627. }
  1628. entry[afirst] = (png_uint_16)green;
  1629. break;
  1630. default:
  1631. break;
  1632. }
  1633. }
  1634. else /* output encoding is P_sRGB */
  1635. {
  1636. png_bytep entry = png_voidcast(png_bytep, display->colormap);
  1637. entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
  1638. switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
  1639. {
  1640. case 4:
  1641. entry[afirst ? 0 : 3] = (png_byte)alpha;
  1642. /* FALLTHROUGH */
  1643. case 3:
  1644. entry[afirst + (2 ^ bgr)] = (png_byte)blue;
  1645. entry[afirst + 1] = (png_byte)green;
  1646. entry[afirst + bgr] = (png_byte)red;
  1647. break;
  1648. case 2:
  1649. entry[1 ^ afirst] = (png_byte)alpha;
  1650. /* FALLTHROUGH */
  1651. case 1:
  1652. entry[afirst] = (png_byte)green;
  1653. break;
  1654. default:
  1655. break;
  1656. }
  1657. }
  1658. # ifdef afirst
  1659. # undef afirst
  1660. # endif
  1661. # ifdef bgr
  1662. # undef bgr
  1663. # endif
  1664. }
  1665. }
  1666. static int
  1667. make_gray_file_colormap(png_image_read_control *display)
  1668. {
  1669. unsigned int i;
  1670. for (i=0; i<256; ++i)
  1671. png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
  1672. return (int)i;
  1673. }
  1674. static int
  1675. make_gray_colormap(png_image_read_control *display)
  1676. {
  1677. unsigned int i;
  1678. for (i=0; i<256; ++i)
  1679. png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
  1680. return (int)i;
  1681. }
  1682. #define PNG_GRAY_COLORMAP_ENTRIES 256
  1683. static int
  1684. make_ga_colormap(png_image_read_control *display)
  1685. {
  1686. unsigned int i, a;
  1687. /* Alpha is retained, the output will be a color-map with entries
  1688. * selected by six levels of alpha. One transparent entry, 6 gray
  1689. * levels for all the intermediate alpha values, leaving 230 entries
  1690. * for the opaque grays. The color-map entries are the six values
  1691. * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
  1692. * relevant entry.
  1693. *
  1694. * if (alpha > 229) // opaque
  1695. * {
  1696. * // The 231 entries are selected to make the math below work:
  1697. * base = 0;
  1698. * entry = (231 * gray + 128) >> 8;
  1699. * }
  1700. * else if (alpha < 26) // transparent
  1701. * {
  1702. * base = 231;
  1703. * entry = 0;
  1704. * }
  1705. * else // partially opaque
  1706. * {
  1707. * base = 226 + 6 * PNG_DIV51(alpha);
  1708. * entry = PNG_DIV51(gray);
  1709. * }
  1710. */
  1711. i = 0;
  1712. while (i < 231)
  1713. {
  1714. unsigned int gray = (i * 256 + 115) / 231;
  1715. png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
  1716. }
  1717. /* 255 is used here for the component values for consistency with the code
  1718. * that undoes premultiplication in pngwrite.c.
  1719. */
  1720. png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
  1721. for (a=1; a<5; ++a)
  1722. {
  1723. unsigned int g;
  1724. for (g=0; g<6; ++g)
  1725. png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
  1726. P_sRGB);
  1727. }
  1728. return (int)i;
  1729. }
  1730. #define PNG_GA_COLORMAP_ENTRIES 256
  1731. static int
  1732. make_rgb_colormap(png_image_read_control *display)
  1733. {
  1734. unsigned int i, r;
  1735. /* Build a 6x6x6 opaque RGB cube */
  1736. for (i=r=0; r<6; ++r)
  1737. {
  1738. unsigned int g;
  1739. for (g=0; g<6; ++g)
  1740. {
  1741. unsigned int b;
  1742. for (b=0; b<6; ++b)
  1743. png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
  1744. P_sRGB);
  1745. }
  1746. }
  1747. return (int)i;
  1748. }
  1749. #define PNG_RGB_COLORMAP_ENTRIES 216
  1750. /* Return a palette index to the above palette given three 8-bit sRGB values. */
  1751. #define PNG_RGB_INDEX(r,g,b) \
  1752. ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
  1753. static int
  1754. png_image_read_colormap(png_voidp argument)
  1755. {
  1756. png_image_read_control *display =
  1757. png_voidcast(png_image_read_control*, argument);
  1758. png_imagep image = display->image;
  1759. png_structrp png_ptr = image->opaque->png_ptr;
  1760. png_uint_32 output_format = image->format;
  1761. int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
  1762. P_LINEAR : P_sRGB;
  1763. unsigned int cmap_entries;
  1764. unsigned int output_processing; /* Output processing option */
  1765. unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
  1766. /* Background information; the background color and the index of this color
  1767. * in the color-map if it exists (else 256).
  1768. */
  1769. unsigned int background_index = 256;
  1770. png_uint_32 back_r, back_g, back_b;
  1771. /* Flags to accumulate things that need to be done to the input. */
  1772. int expand_tRNS = 0;
  1773. /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
  1774. * very difficult to do, the results look awful, and it is difficult to see
  1775. * what possible use it is because the application can't control the
  1776. * color-map.
  1777. */
  1778. if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
  1779. png_ptr->num_trans > 0) /* alpha in input */ &&
  1780. ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
  1781. {
  1782. if (output_encoding == P_LINEAR) /* compose on black */
  1783. back_b = back_g = back_r = 0;
  1784. else if (display->background == NULL /* no way to remove it */)
  1785. png_error(png_ptr,
  1786. "background color must be supplied to remove alpha/transparency");
  1787. /* Get a copy of the background color (this avoids repeating the checks
  1788. * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
  1789. * output format.
  1790. */
  1791. else
  1792. {
  1793. back_g = display->background->green;
  1794. if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
  1795. {
  1796. back_r = display->background->red;
  1797. back_b = display->background->blue;
  1798. }
  1799. else
  1800. back_b = back_r = back_g;
  1801. }
  1802. }
  1803. else if (output_encoding == P_LINEAR)
  1804. back_b = back_r = back_g = 65535;
  1805. else
  1806. back_b = back_r = back_g = 255;
  1807. /* Default the input file gamma if required - this is necessary because
  1808. * libpng assumes that if no gamma information is present the data is in the
  1809. * output format, but the simplified API deduces the gamma from the input
  1810. * format.
  1811. */
  1812. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0)
  1813. {
  1814. /* Do this directly, not using the png_colorspace functions, to ensure
  1815. * that it happens even if the colorspace is invalid (though probably if
  1816. * it is the setting will be ignored) Note that the same thing can be
  1817. * achieved at the application interface with png_set_gAMA.
  1818. */
  1819. if (png_ptr->bit_depth == 16 &&
  1820. (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
  1821. png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR;
  1822. else
  1823. png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE;
  1824. png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
  1825. }
  1826. /* Decide what to do based on the PNG color type of the input data. The
  1827. * utility function png_create_colormap_entry deals with most aspects of the
  1828. * output transformations; this code works out how to produce bytes of
  1829. * color-map entries from the original format.
  1830. */
  1831. switch (png_ptr->color_type)
  1832. {
  1833. case PNG_COLOR_TYPE_GRAY:
  1834. if (png_ptr->bit_depth <= 8)
  1835. {
  1836. /* There at most 256 colors in the output, regardless of
  1837. * transparency.
  1838. */
  1839. unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
  1840. cmap_entries = 1U << png_ptr->bit_depth;
  1841. if (cmap_entries > image->colormap_entries)
  1842. png_error(png_ptr, "gray[8] color-map: too few entries");
  1843. step = 255 / (cmap_entries - 1);
  1844. output_processing = PNG_CMAP_NONE;
  1845. /* If there is a tRNS chunk then this either selects a transparent
  1846. * value or, if the output has no alpha, the background color.
  1847. */
  1848. if (png_ptr->num_trans > 0)
  1849. {
  1850. trans = png_ptr->trans_color.gray;
  1851. if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
  1852. back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
  1853. }
  1854. /* png_create_colormap_entry just takes an RGBA and writes the
  1855. * corresponding color-map entry using the format from 'image',
  1856. * including the required conversion to sRGB or linear as
  1857. * appropriate. The input values are always either sRGB (if the
  1858. * gamma correction flag is 0) or 0..255 scaled file encoded values
  1859. * (if the function must gamma correct them).
  1860. */
  1861. for (i=val=0; i<cmap_entries; ++i, val += step)
  1862. {
  1863. /* 'i' is a file value. While this will result in duplicated
  1864. * entries for 8-bit non-sRGB encoded files it is necessary to
  1865. * have non-gamma corrected values to do tRNS handling.
  1866. */
  1867. if (i != trans)
  1868. png_create_colormap_entry(display, i, val, val, val, 255,
  1869. P_FILE/*8-bit with file gamma*/);
  1870. /* Else this entry is transparent. The colors don't matter if
  1871. * there is an alpha channel (back_alpha == 0), but it does no
  1872. * harm to pass them in; the values are not set above so this
  1873. * passes in white.
  1874. *
  1875. * NOTE: this preserves the full precision of the application
  1876. * supplied background color when it is used.
  1877. */
  1878. else
  1879. png_create_colormap_entry(display, i, back_r, back_g, back_b,
  1880. back_alpha, output_encoding);
  1881. }
  1882. /* We need libpng to preserve the original encoding. */
  1883. data_encoding = P_FILE;
  1884. /* The rows from libpng, while technically gray values, are now also
  1885. * color-map indices; however, they may need to be expanded to 1
  1886. * byte per pixel. This is what png_set_packing does (i.e., it
  1887. * unpacks the bit values into bytes.)
  1888. */
  1889. if (png_ptr->bit_depth < 8)
  1890. png_set_packing(png_ptr);
  1891. }
  1892. else /* bit depth is 16 */
  1893. {
  1894. /* The 16-bit input values can be converted directly to 8-bit gamma
  1895. * encoded values; however, if a tRNS chunk is present 257 color-map
  1896. * entries are required. This means that the extra entry requires
  1897. * special processing; add an alpha channel, sacrifice gray level
  1898. * 254 and convert transparent (alpha==0) entries to that.
  1899. *
  1900. * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
  1901. * same time to minimize quality loss. If a tRNS chunk is present
  1902. * this means libpng must handle it too; otherwise it is impossible
  1903. * to do the exact match on the 16-bit value.
  1904. *
  1905. * If the output has no alpha channel *and* the background color is
  1906. * gray then it is possible to let libpng handle the substitution by
  1907. * ensuring that the corresponding gray level matches the background
  1908. * color exactly.
  1909. */
  1910. data_encoding = P_sRGB;
  1911. if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
  1912. png_error(png_ptr, "gray[16] color-map: too few entries");
  1913. cmap_entries = (unsigned int)make_gray_colormap(display);
  1914. if (png_ptr->num_trans > 0)
  1915. {
  1916. unsigned int back_alpha;
  1917. if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  1918. back_alpha = 0;
  1919. else
  1920. {
  1921. if (back_r == back_g && back_g == back_b)
  1922. {
  1923. /* Background is gray; no special processing will be
  1924. * required.
  1925. */
  1926. png_color_16 c;
  1927. png_uint_32 gray = back_g;
  1928. if (output_encoding == P_LINEAR)
  1929. {
  1930. gray = PNG_sRGB_FROM_LINEAR(gray * 255);
  1931. /* And make sure the corresponding palette entry
  1932. * matches.
  1933. */
  1934. png_create_colormap_entry(display, gray, back_g, back_g,
  1935. back_g, 65535, P_LINEAR);
  1936. }
  1937. /* The background passed to libpng, however, must be the
  1938. * sRGB value.
  1939. */
  1940. c.index = 0; /*unused*/
  1941. c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
  1942. /* NOTE: does this work without expanding tRNS to alpha?
  1943. * It should be the color->gray case below apparently
  1944. * doesn't.
  1945. */
  1946. png_set_background_fixed(png_ptr, &c,
  1947. PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
  1948. 0/*gamma: not used*/);
  1949. output_processing = PNG_CMAP_NONE;
  1950. break;
  1951. }
  1952. #ifdef __COVERITY__
  1953. /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
  1954. * here.
  1955. */
  1956. back_alpha = 255;
  1957. #else
  1958. back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
  1959. #endif
  1960. }
  1961. /* output_processing means that the libpng-processed row will be
  1962. * 8-bit GA and it has to be processing to single byte color-map
  1963. * values. Entry 254 is replaced by either a completely
  1964. * transparent entry or by the background color at full
  1965. * precision (and the background color is not a simple gray
  1966. * level in this case.)
  1967. */
  1968. expand_tRNS = 1;
  1969. output_processing = PNG_CMAP_TRANS;
  1970. background_index = 254;
  1971. /* And set (overwrite) color-map entry 254 to the actual
  1972. * background color at full precision.
  1973. */
  1974. png_create_colormap_entry(display, 254, back_r, back_g, back_b,
  1975. back_alpha, output_encoding);
  1976. }
  1977. else
  1978. output_processing = PNG_CMAP_NONE;
  1979. }
  1980. break;
  1981. case PNG_COLOR_TYPE_GRAY_ALPHA:
  1982. /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
  1983. * of 65536 combinations. If, however, the alpha channel is to be
  1984. * removed there are only 256 possibilities if the background is gray.
  1985. * (Otherwise there is a subset of the 65536 possibilities defined by
  1986. * the triangle between black, white and the background color.)
  1987. *
  1988. * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
  1989. * worry about tRNS matching - tRNS is ignored if there is an alpha
  1990. * channel.
  1991. */
  1992. data_encoding = P_sRGB;
  1993. if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  1994. {
  1995. if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
  1996. png_error(png_ptr, "gray+alpha color-map: too few entries");
  1997. cmap_entries = (unsigned int)make_ga_colormap(display);
  1998. background_index = PNG_CMAP_GA_BACKGROUND;
  1999. output_processing = PNG_CMAP_GA;
  2000. }
  2001. else /* alpha is removed */
  2002. {
  2003. /* Alpha must be removed as the PNG data is processed when the
  2004. * background is a color because the G and A channels are
  2005. * independent and the vector addition (non-parallel vectors) is a
  2006. * 2-D problem.
  2007. *
  2008. * This can be reduced to the same algorithm as above by making a
  2009. * colormap containing gray levels (for the opaque grays), a
  2010. * background entry (for a transparent pixel) and a set of four six
  2011. * level color values, one set for each intermediate alpha value.
  2012. * See the comments in make_ga_colormap for how this works in the
  2013. * per-pixel processing.
  2014. *
  2015. * If the background is gray, however, we only need a 256 entry gray
  2016. * level color map. It is sufficient to make the entry generated
  2017. * for the background color be exactly the color specified.
  2018. */
  2019. if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
  2020. (back_r == back_g && back_g == back_b))
  2021. {
  2022. /* Background is gray; no special processing will be required. */
  2023. png_color_16 c;
  2024. png_uint_32 gray = back_g;
  2025. if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
  2026. png_error(png_ptr, "gray-alpha color-map: too few entries");
  2027. cmap_entries = (unsigned int)make_gray_colormap(display);
  2028. if (output_encoding == P_LINEAR)
  2029. {
  2030. gray = PNG_sRGB_FROM_LINEAR(gray * 255);
  2031. /* And make sure the corresponding palette entry matches. */
  2032. png_create_colormap_entry(display, gray, back_g, back_g,
  2033. back_g, 65535, P_LINEAR);
  2034. }
  2035. /* The background passed to libpng, however, must be the sRGB
  2036. * value.
  2037. */
  2038. c.index = 0; /*unused*/
  2039. c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
  2040. png_set_background_fixed(png_ptr, &c,
  2041. PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
  2042. 0/*gamma: not used*/);
  2043. output_processing = PNG_CMAP_NONE;
  2044. }
  2045. else
  2046. {
  2047. png_uint_32 i, a;
  2048. /* This is the same as png_make_ga_colormap, above, except that
  2049. * the entries are all opaque.
  2050. */
  2051. if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
  2052. png_error(png_ptr, "ga-alpha color-map: too few entries");
  2053. i = 0;
  2054. while (i < 231)
  2055. {
  2056. png_uint_32 gray = (i * 256 + 115) / 231;
  2057. png_create_colormap_entry(display, i++, gray, gray, gray,
  2058. 255, P_sRGB);
  2059. }
  2060. /* NOTE: this preserves the full precision of the application
  2061. * background color.
  2062. */
  2063. background_index = i;
  2064. png_create_colormap_entry(display, i++, back_r, back_g, back_b,
  2065. #ifdef __COVERITY__
  2066. /* Coverity claims that output_encoding
  2067. * cannot be 2 (P_LINEAR) here.
  2068. */ 255U,
  2069. #else
  2070. output_encoding == P_LINEAR ? 65535U : 255U,
  2071. #endif
  2072. output_encoding);
  2073. /* For non-opaque input composite on the sRGB background - this
  2074. * requires inverting the encoding for each component. The input
  2075. * is still converted to the sRGB encoding because this is a
  2076. * reasonable approximate to the logarithmic curve of human
  2077. * visual sensitivity, at least over the narrow range which PNG
  2078. * represents. Consequently 'G' is always sRGB encoded, while
  2079. * 'A' is linear. We need the linear background colors.
  2080. */
  2081. if (output_encoding == P_sRGB) /* else already linear */
  2082. {
  2083. /* This may produce a value not exactly matching the
  2084. * background, but that's ok because these numbers are only
  2085. * used when alpha != 0
  2086. */
  2087. back_r = png_sRGB_table[back_r];
  2088. back_g = png_sRGB_table[back_g];
  2089. back_b = png_sRGB_table[back_b];
  2090. }
  2091. for (a=1; a<5; ++a)
  2092. {
  2093. unsigned int g;
  2094. /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
  2095. * by an 8-bit alpha value (0..255).
  2096. */
  2097. png_uint_32 alpha = 51 * a;
  2098. png_uint_32 back_rx = (255-alpha) * back_r;
  2099. png_uint_32 back_gx = (255-alpha) * back_g;
  2100. png_uint_32 back_bx = (255-alpha) * back_b;
  2101. for (g=0; g<6; ++g)
  2102. {
  2103. png_uint_32 gray = png_sRGB_table[g*51] * alpha;
  2104. png_create_colormap_entry(display, i++,
  2105. PNG_sRGB_FROM_LINEAR(gray + back_rx),
  2106. PNG_sRGB_FROM_LINEAR(gray + back_gx),
  2107. PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
  2108. }
  2109. }
  2110. cmap_entries = i;
  2111. output_processing = PNG_CMAP_GA;
  2112. }
  2113. }
  2114. break;
  2115. case PNG_COLOR_TYPE_RGB:
  2116. case PNG_COLOR_TYPE_RGB_ALPHA:
  2117. /* Exclude the case where the output is gray; we can always handle this
  2118. * with the cases above.
  2119. */
  2120. if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
  2121. {
  2122. /* The color-map will be grayscale, so we may as well convert the
  2123. * input RGB values to a simple grayscale and use the grayscale
  2124. * code above.
  2125. *
  2126. * NOTE: calling this apparently damages the recognition of the
  2127. * transparent color in background color handling; call
  2128. * png_set_tRNS_to_alpha before png_set_background_fixed.
  2129. */
  2130. png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
  2131. -1);
  2132. data_encoding = P_sRGB;
  2133. /* The output will now be one or two 8-bit gray or gray+alpha
  2134. * channels. The more complex case arises when the input has alpha.
  2135. */
  2136. if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  2137. png_ptr->num_trans > 0) &&
  2138. (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  2139. {
  2140. /* Both input and output have an alpha channel, so no background
  2141. * processing is required; just map the GA bytes to the right
  2142. * color-map entry.
  2143. */
  2144. expand_tRNS = 1;
  2145. if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
  2146. png_error(png_ptr, "rgb[ga] color-map: too few entries");
  2147. cmap_entries = (unsigned int)make_ga_colormap(display);
  2148. background_index = PNG_CMAP_GA_BACKGROUND;
  2149. output_processing = PNG_CMAP_GA;
  2150. }
  2151. else
  2152. {
  2153. /* Either the input or the output has no alpha channel, so there
  2154. * will be no non-opaque pixels in the color-map; it will just be
  2155. * grayscale.
  2156. */
  2157. if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
  2158. png_error(png_ptr, "rgb[gray] color-map: too few entries");
  2159. /* Ideally this code would use libpng to do the gamma correction,
  2160. * but if an input alpha channel is to be removed we will hit the
  2161. * libpng bug in gamma+compose+rgb-to-gray (the double gamma
  2162. * correction bug). Fix this by dropping the gamma correction in
  2163. * this case and doing it in the palette; this will result in
  2164. * duplicate palette entries, but that's better than the
  2165. * alternative of double gamma correction.
  2166. */
  2167. if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  2168. png_ptr->num_trans > 0) &&
  2169. png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0)
  2170. {
  2171. cmap_entries = (unsigned int)make_gray_file_colormap(display);
  2172. data_encoding = P_FILE;
  2173. }
  2174. else
  2175. cmap_entries = (unsigned int)make_gray_colormap(display);
  2176. /* But if the input has alpha or transparency it must be removed
  2177. */
  2178. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  2179. png_ptr->num_trans > 0)
  2180. {
  2181. png_color_16 c;
  2182. png_uint_32 gray = back_g;
  2183. /* We need to ensure that the application background exists in
  2184. * the colormap and that completely transparent pixels map to
  2185. * it. Achieve this simply by ensuring that the entry
  2186. * selected for the background really is the background color.
  2187. */
  2188. if (data_encoding == P_FILE) /* from the fixup above */
  2189. {
  2190. /* The app supplied a gray which is in output_encoding, we
  2191. * need to convert it to a value of the input (P_FILE)
  2192. * encoding then set this palette entry to the required
  2193. * output encoding.
  2194. */
  2195. if (output_encoding == P_sRGB)
  2196. gray = png_sRGB_table[gray]; /* now P_LINEAR */
  2197. gray = PNG_DIV257(png_gamma_16bit_correct(gray,
  2198. png_ptr->colorspace.gamma)); /* now P_FILE */
  2199. /* And make sure the corresponding palette entry contains
  2200. * exactly the required sRGB value.
  2201. */
  2202. png_create_colormap_entry(display, gray, back_g, back_g,
  2203. back_g, 0/*unused*/, output_encoding);
  2204. }
  2205. else if (output_encoding == P_LINEAR)
  2206. {
  2207. gray = PNG_sRGB_FROM_LINEAR(gray * 255);
  2208. /* And make sure the corresponding palette entry matches.
  2209. */
  2210. png_create_colormap_entry(display, gray, back_g, back_g,
  2211. back_g, 0/*unused*/, P_LINEAR);
  2212. }
  2213. /* The background passed to libpng, however, must be the
  2214. * output (normally sRGB) value.
  2215. */
  2216. c.index = 0; /*unused*/
  2217. c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
  2218. /* NOTE: the following is apparently a bug in libpng. Without
  2219. * it the transparent color recognition in
  2220. * png_set_background_fixed seems to go wrong.
  2221. */
  2222. expand_tRNS = 1;
  2223. png_set_background_fixed(png_ptr, &c,
  2224. PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
  2225. 0/*gamma: not used*/);
  2226. }
  2227. output_processing = PNG_CMAP_NONE;
  2228. }
  2229. }
  2230. else /* output is color */
  2231. {
  2232. /* We could use png_quantize here so long as there is no transparent
  2233. * color or alpha; png_quantize ignores alpha. Easier overall just
  2234. * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
  2235. * Consequently we always want libpng to produce sRGB data.
  2236. */
  2237. data_encoding = P_sRGB;
  2238. /* Is there any transparency or alpha? */
  2239. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  2240. png_ptr->num_trans > 0)
  2241. {
  2242. /* Is there alpha in the output too? If so all four channels are
  2243. * processed into a special RGB cube with alpha support.
  2244. */
  2245. if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  2246. {
  2247. png_uint_32 r;
  2248. if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
  2249. png_error(png_ptr, "rgb+alpha color-map: too few entries");
  2250. cmap_entries = (unsigned int)make_rgb_colormap(display);
  2251. /* Add a transparent entry. */
  2252. png_create_colormap_entry(display, cmap_entries, 255, 255,
  2253. 255, 0, P_sRGB);
  2254. /* This is stored as the background index for the processing
  2255. * algorithm.
  2256. */
  2257. background_index = cmap_entries++;
  2258. /* Add 27 r,g,b entries each with alpha 0.5. */
  2259. for (r=0; r<256; r = (r << 1) | 0x7f)
  2260. {
  2261. png_uint_32 g;
  2262. for (g=0; g<256; g = (g << 1) | 0x7f)
  2263. {
  2264. png_uint_32 b;
  2265. /* This generates components with the values 0, 127 and
  2266. * 255
  2267. */
  2268. for (b=0; b<256; b = (b << 1) | 0x7f)
  2269. png_create_colormap_entry(display, cmap_entries++,
  2270. r, g, b, 128, P_sRGB);
  2271. }
  2272. }
  2273. expand_tRNS = 1;
  2274. output_processing = PNG_CMAP_RGB_ALPHA;
  2275. }
  2276. else
  2277. {
  2278. /* Alpha/transparency must be removed. The background must
  2279. * exist in the color map (achieved by setting adding it after
  2280. * the 666 color-map). If the standard processing code will
  2281. * pick up this entry automatically that's all that is
  2282. * required; libpng can be called to do the background
  2283. * processing.
  2284. */
  2285. unsigned int sample_size =
  2286. PNG_IMAGE_SAMPLE_SIZE(output_format);
  2287. png_uint_32 r, g, b; /* sRGB background */
  2288. if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
  2289. png_error(png_ptr, "rgb-alpha color-map: too few entries");
  2290. cmap_entries = (unsigned int)make_rgb_colormap(display);
  2291. png_create_colormap_entry(display, cmap_entries, back_r,
  2292. back_g, back_b, 0/*unused*/, output_encoding);
  2293. if (output_encoding == P_LINEAR)
  2294. {
  2295. r = PNG_sRGB_FROM_LINEAR(back_r * 255);
  2296. g = PNG_sRGB_FROM_LINEAR(back_g * 255);
  2297. b = PNG_sRGB_FROM_LINEAR(back_b * 255);
  2298. }
  2299. else
  2300. {
  2301. r = back_r;
  2302. g = back_g;
  2303. b = back_g;
  2304. }
  2305. /* Compare the newly-created color-map entry with the one the
  2306. * PNG_CMAP_RGB algorithm will use. If the two entries don't
  2307. * match, add the new one and set this as the background
  2308. * index.
  2309. */
  2310. if (memcmp((png_const_bytep)display->colormap +
  2311. sample_size * cmap_entries,
  2312. (png_const_bytep)display->colormap +
  2313. sample_size * PNG_RGB_INDEX(r,g,b),
  2314. sample_size) != 0)
  2315. {
  2316. /* The background color must be added. */
  2317. background_index = cmap_entries++;
  2318. /* Add 27 r,g,b entries each with created by composing with
  2319. * the background at alpha 0.5.
  2320. */
  2321. for (r=0; r<256; r = (r << 1) | 0x7f)
  2322. {
  2323. for (g=0; g<256; g = (g << 1) | 0x7f)
  2324. {
  2325. /* This generates components with the values 0, 127
  2326. * and 255
  2327. */
  2328. for (b=0; b<256; b = (b << 1) | 0x7f)
  2329. png_create_colormap_entry(display, cmap_entries++,
  2330. png_colormap_compose(display, r, P_sRGB, 128,
  2331. back_r, output_encoding),
  2332. png_colormap_compose(display, g, P_sRGB, 128,
  2333. back_g, output_encoding),
  2334. png_colormap_compose(display, b, P_sRGB, 128,
  2335. back_b, output_encoding),
  2336. 0/*unused*/, output_encoding);
  2337. }
  2338. }
  2339. expand_tRNS = 1;
  2340. output_processing = PNG_CMAP_RGB_ALPHA;
  2341. }
  2342. else /* background color is in the standard color-map */
  2343. {
  2344. png_color_16 c;
  2345. c.index = 0; /*unused*/
  2346. c.red = (png_uint_16)back_r;
  2347. c.gray = c.green = (png_uint_16)back_g;
  2348. c.blue = (png_uint_16)back_b;
  2349. png_set_background_fixed(png_ptr, &c,
  2350. PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
  2351. 0/*gamma: not used*/);
  2352. output_processing = PNG_CMAP_RGB;
  2353. }
  2354. }
  2355. }
  2356. else /* no alpha or transparency in the input */
  2357. {
  2358. /* Alpha in the output is irrelevant, simply map the opaque input
  2359. * pixels to the 6x6x6 color-map.
  2360. */
  2361. if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
  2362. png_error(png_ptr, "rgb color-map: too few entries");
  2363. cmap_entries = (unsigned int)make_rgb_colormap(display);
  2364. output_processing = PNG_CMAP_RGB;
  2365. }
  2366. }
  2367. break;
  2368. case PNG_COLOR_TYPE_PALETTE:
  2369. /* It's already got a color-map. It may be necessary to eliminate the
  2370. * tRNS entries though.
  2371. */
  2372. {
  2373. unsigned int num_trans = png_ptr->num_trans;
  2374. png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
  2375. png_const_colorp colormap = png_ptr->palette;
  2376. int do_background = trans != NULL &&
  2377. (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
  2378. unsigned int i;
  2379. /* Just in case: */
  2380. if (trans == NULL)
  2381. num_trans = 0;
  2382. output_processing = PNG_CMAP_NONE;
  2383. data_encoding = P_FILE; /* Don't change from color-map indices */
  2384. cmap_entries = (unsigned int)png_ptr->num_palette;
  2385. if (cmap_entries > 256)
  2386. cmap_entries = 256;
  2387. if (cmap_entries > (unsigned int)image->colormap_entries)
  2388. png_error(png_ptr, "palette color-map: too few entries");
  2389. for (i=0; i < cmap_entries; ++i)
  2390. {
  2391. if (do_background != 0 && i < num_trans && trans[i] < 255)
  2392. {
  2393. if (trans[i] == 0)
  2394. png_create_colormap_entry(display, i, back_r, back_g,
  2395. back_b, 0, output_encoding);
  2396. else
  2397. {
  2398. /* Must compose the PNG file color in the color-map entry
  2399. * on the sRGB color in 'back'.
  2400. */
  2401. png_create_colormap_entry(display, i,
  2402. png_colormap_compose(display, colormap[i].red,
  2403. P_FILE, trans[i], back_r, output_encoding),
  2404. png_colormap_compose(display, colormap[i].green,
  2405. P_FILE, trans[i], back_g, output_encoding),
  2406. png_colormap_compose(display, colormap[i].blue,
  2407. P_FILE, trans[i], back_b, output_encoding),
  2408. output_encoding == P_LINEAR ? trans[i] * 257U :
  2409. trans[i],
  2410. output_encoding);
  2411. }
  2412. }
  2413. else
  2414. png_create_colormap_entry(display, i, colormap[i].red,
  2415. colormap[i].green, colormap[i].blue,
  2416. i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
  2417. }
  2418. /* The PNG data may have indices packed in fewer than 8 bits, it
  2419. * must be expanded if so.
  2420. */
  2421. if (png_ptr->bit_depth < 8)
  2422. png_set_packing(png_ptr);
  2423. }
  2424. break;
  2425. default:
  2426. png_error(png_ptr, "invalid PNG color type");
  2427. /*NOT REACHED*/
  2428. }
  2429. /* Now deal with the output processing */
  2430. if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
  2431. (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
  2432. png_set_tRNS_to_alpha(png_ptr);
  2433. switch (data_encoding)
  2434. {
  2435. case P_sRGB:
  2436. /* Change to 8-bit sRGB */
  2437. png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
  2438. /* FALLTHROUGH */
  2439. case P_FILE:
  2440. if (png_ptr->bit_depth > 8)
  2441. png_set_scale_16(png_ptr);
  2442. break;
  2443. #ifdef __GNUC__
  2444. default:
  2445. png_error(png_ptr, "bad data option (internal error)");
  2446. #endif
  2447. }
  2448. if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
  2449. png_error(png_ptr, "color map overflow (BAD internal error)");
  2450. image->colormap_entries = cmap_entries;
  2451. /* Double check using the recorded background index */
  2452. switch (output_processing)
  2453. {
  2454. case PNG_CMAP_NONE:
  2455. if (background_index != PNG_CMAP_NONE_BACKGROUND)
  2456. goto bad_background;
  2457. break;
  2458. case PNG_CMAP_GA:
  2459. if (background_index != PNG_CMAP_GA_BACKGROUND)
  2460. goto bad_background;
  2461. break;
  2462. case PNG_CMAP_TRANS:
  2463. if (background_index >= cmap_entries ||
  2464. background_index != PNG_CMAP_TRANS_BACKGROUND)
  2465. goto bad_background;
  2466. break;
  2467. case PNG_CMAP_RGB:
  2468. if (background_index != PNG_CMAP_RGB_BACKGROUND)
  2469. goto bad_background;
  2470. break;
  2471. case PNG_CMAP_RGB_ALPHA:
  2472. if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
  2473. goto bad_background;
  2474. break;
  2475. default:
  2476. png_error(png_ptr, "bad processing option (internal error)");
  2477. bad_background:
  2478. png_error(png_ptr, "bad background index (internal error)");
  2479. }
  2480. display->colormap_processing = (int)output_processing;
  2481. return 1/*ok*/;
  2482. }
  2483. /* The final part of the color-map read called from png_image_finish_read. */
  2484. static int
  2485. png_image_read_and_map(png_voidp argument)
  2486. {
  2487. png_image_read_control *display = png_voidcast(png_image_read_control*,
  2488. argument);
  2489. png_imagep image = display->image;
  2490. png_structrp png_ptr = image->opaque->png_ptr;
  2491. int passes;
  2492. /* Called when the libpng data must be transformed into the color-mapped
  2493. * form. There is a local row buffer in display->local and this routine must
  2494. * do the interlace handling.
  2495. */
  2496. switch (png_ptr->interlaced)
  2497. {
  2498. case PNG_INTERLACE_NONE:
  2499. passes = 1;
  2500. break;
  2501. case PNG_INTERLACE_ADAM7:
  2502. passes = PNG_INTERLACE_ADAM7_PASSES;
  2503. break;
  2504. default:
  2505. png_error(png_ptr, "unknown interlace type");
  2506. }
  2507. {
  2508. png_uint_32 height = image->height;
  2509. png_uint_32 width = image->width;
  2510. int proc = display->colormap_processing;
  2511. png_bytep first_row = png_voidcast(png_bytep, display->first_row);
  2512. ptrdiff_t step_row = display->row_bytes;
  2513. int pass;
  2514. for (pass = 0; pass < passes; ++pass)
  2515. {
  2516. unsigned int startx, stepx, stepy;
  2517. png_uint_32 y;
  2518. if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
  2519. {
  2520. /* The row may be empty for a short image: */
  2521. if (PNG_PASS_COLS(width, pass) == 0)
  2522. continue;
  2523. startx = PNG_PASS_START_COL(pass);
  2524. stepx = PNG_PASS_COL_OFFSET(pass);
  2525. y = PNG_PASS_START_ROW(pass);
  2526. stepy = PNG_PASS_ROW_OFFSET(pass);
  2527. }
  2528. else
  2529. {
  2530. y = 0;
  2531. startx = 0;
  2532. stepx = stepy = 1;
  2533. }
  2534. for (; y<height; y += stepy)
  2535. {
  2536. png_bytep inrow = png_voidcast(png_bytep, display->local_row);
  2537. png_bytep outrow = first_row + y * step_row;
  2538. png_const_bytep end_row = outrow + width;
  2539. /* Read read the libpng data into the temporary buffer. */
  2540. png_read_row(png_ptr, inrow, NULL);
  2541. /* Now process the row according to the processing option, note
  2542. * that the caller verifies that the format of the libpng output
  2543. * data is as required.
  2544. */
  2545. outrow += startx;
  2546. switch (proc)
  2547. {
  2548. case PNG_CMAP_GA:
  2549. for (; outrow < end_row; outrow += stepx)
  2550. {
  2551. /* The data is always in the PNG order */
  2552. unsigned int gray = *inrow++;
  2553. unsigned int alpha = *inrow++;
  2554. unsigned int entry;
  2555. /* NOTE: this code is copied as a comment in
  2556. * make_ga_colormap above. Please update the
  2557. * comment if you change this code!
  2558. */
  2559. if (alpha > 229) /* opaque */
  2560. {
  2561. entry = (231 * gray + 128) >> 8;
  2562. }
  2563. else if (alpha < 26) /* transparent */
  2564. {
  2565. entry = 231;
  2566. }
  2567. else /* partially opaque */
  2568. {
  2569. entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
  2570. }
  2571. *outrow = (png_byte)entry;
  2572. }
  2573. break;
  2574. case PNG_CMAP_TRANS:
  2575. for (; outrow < end_row; outrow += stepx)
  2576. {
  2577. png_byte gray = *inrow++;
  2578. png_byte alpha = *inrow++;
  2579. if (alpha == 0)
  2580. *outrow = PNG_CMAP_TRANS_BACKGROUND;
  2581. else if (gray != PNG_CMAP_TRANS_BACKGROUND)
  2582. *outrow = gray;
  2583. else
  2584. *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
  2585. }
  2586. break;
  2587. case PNG_CMAP_RGB:
  2588. for (; outrow < end_row; outrow += stepx)
  2589. {
  2590. *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
  2591. inrow += 3;
  2592. }
  2593. break;
  2594. case PNG_CMAP_RGB_ALPHA:
  2595. for (; outrow < end_row; outrow += stepx)
  2596. {
  2597. unsigned int alpha = inrow[3];
  2598. /* Because the alpha entries only hold alpha==0.5 values
  2599. * split the processing at alpha==0.25 (64) and 0.75
  2600. * (196).
  2601. */
  2602. if (alpha >= 196)
  2603. *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
  2604. inrow[2]);
  2605. else if (alpha < 64)
  2606. *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
  2607. else
  2608. {
  2609. /* Likewise there are three entries for each of r, g
  2610. * and b. We could select the entry by popcount on
  2611. * the top two bits on those architectures that
  2612. * support it, this is what the code below does,
  2613. * crudely.
  2614. */
  2615. unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
  2616. /* Here are how the values map:
  2617. *
  2618. * 0x00 .. 0x3f -> 0
  2619. * 0x40 .. 0xbf -> 1
  2620. * 0xc0 .. 0xff -> 2
  2621. *
  2622. * So, as above with the explicit alpha checks, the
  2623. * breakpoints are at 64 and 196.
  2624. */
  2625. if (inrow[0] & 0x80) back_i += 9; /* red */
  2626. if (inrow[0] & 0x40) back_i += 9;
  2627. if (inrow[0] & 0x80) back_i += 3; /* green */
  2628. if (inrow[0] & 0x40) back_i += 3;
  2629. if (inrow[0] & 0x80) back_i += 1; /* blue */
  2630. if (inrow[0] & 0x40) back_i += 1;
  2631. *outrow = (png_byte)back_i;
  2632. }
  2633. inrow += 4;
  2634. }
  2635. break;
  2636. default:
  2637. break;
  2638. }
  2639. }
  2640. }
  2641. }
  2642. return 1;
  2643. }
  2644. static int
  2645. png_image_read_colormapped(png_voidp argument)
  2646. {
  2647. png_image_read_control *display = png_voidcast(png_image_read_control*,
  2648. argument);
  2649. png_imagep image = display->image;
  2650. png_controlp control = image->opaque;
  2651. png_structrp png_ptr = control->png_ptr;
  2652. png_inforp info_ptr = control->info_ptr;
  2653. int passes = 0; /* As a flag */
  2654. PNG_SKIP_CHUNKS(png_ptr);
  2655. /* Update the 'info' structure and make sure the result is as required; first
  2656. * make sure to turn on the interlace handling if it will be required
  2657. * (because it can't be turned on *after* the call to png_read_update_info!)
  2658. */
  2659. if (display->colormap_processing == PNG_CMAP_NONE)
  2660. passes = png_set_interlace_handling(png_ptr);
  2661. png_read_update_info(png_ptr, info_ptr);
  2662. /* The expected output can be deduced from the colormap_processing option. */
  2663. switch (display->colormap_processing)
  2664. {
  2665. case PNG_CMAP_NONE:
  2666. /* Output must be one channel and one byte per pixel, the output
  2667. * encoding can be anything.
  2668. */
  2669. if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
  2670. info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
  2671. info_ptr->bit_depth == 8)
  2672. break;
  2673. goto bad_output;
  2674. case PNG_CMAP_TRANS:
  2675. case PNG_CMAP_GA:
  2676. /* Output must be two channels and the 'G' one must be sRGB, the latter
  2677. * can be checked with an exact number because it should have been set
  2678. * to this number above!
  2679. */
  2680. if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  2681. info_ptr->bit_depth == 8 &&
  2682. png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
  2683. image->colormap_entries == 256)
  2684. break;
  2685. goto bad_output;
  2686. case PNG_CMAP_RGB:
  2687. /* Output must be 8-bit sRGB encoded RGB */
  2688. if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  2689. info_ptr->bit_depth == 8 &&
  2690. png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
  2691. image->colormap_entries == 216)
  2692. break;
  2693. goto bad_output;
  2694. case PNG_CMAP_RGB_ALPHA:
  2695. /* Output must be 8-bit sRGB encoded RGBA */
  2696. if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  2697. info_ptr->bit_depth == 8 &&
  2698. png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
  2699. image->colormap_entries == 244 /* 216 + 1 + 27 */)
  2700. break;
  2701. goto bad_output;
  2702. default:
  2703. bad_output:
  2704. png_error(png_ptr, "bad color-map processing (internal error)");
  2705. }
  2706. /* Now read the rows. Do this here if it is possible to read directly into
  2707. * the output buffer, otherwise allocate a local row buffer of the maximum
  2708. * size libpng requires and call the relevant processing routine safely.
  2709. */
  2710. {
  2711. png_voidp first_row = display->buffer;
  2712. ptrdiff_t row_bytes = display->row_stride;
  2713. /* The following expression is designed to work correctly whether it gives
  2714. * a signed or an unsigned result.
  2715. */
  2716. if (row_bytes < 0)
  2717. {
  2718. char *ptr = png_voidcast(char*, first_row);
  2719. ptr += (image->height-1) * (-row_bytes);
  2720. first_row = png_voidcast(png_voidp, ptr);
  2721. }
  2722. display->first_row = first_row;
  2723. display->row_bytes = row_bytes;
  2724. }
  2725. if (passes == 0)
  2726. {
  2727. int result;
  2728. png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
  2729. display->local_row = row;
  2730. result = png_safe_execute(image, png_image_read_and_map, display);
  2731. display->local_row = NULL;
  2732. png_free(png_ptr, row);
  2733. return result;
  2734. }
  2735. else
  2736. {
  2737. png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
  2738. while (--passes >= 0)
  2739. {
  2740. png_uint_32 y = image->height;
  2741. png_bytep row = png_voidcast(png_bytep, display->first_row);
  2742. for (; y > 0; --y)
  2743. {
  2744. png_read_row(png_ptr, row, NULL);
  2745. row += row_bytes;
  2746. }
  2747. }
  2748. return 1;
  2749. }
  2750. }
  2751. /* Just the row reading part of png_image_read. */
  2752. static int
  2753. png_image_read_composite(png_voidp argument)
  2754. {
  2755. png_image_read_control *display = png_voidcast(png_image_read_control*,
  2756. argument);
  2757. png_imagep image = display->image;
  2758. png_structrp png_ptr = image->opaque->png_ptr;
  2759. int passes;
  2760. switch (png_ptr->interlaced)
  2761. {
  2762. case PNG_INTERLACE_NONE:
  2763. passes = 1;
  2764. break;
  2765. case PNG_INTERLACE_ADAM7:
  2766. passes = PNG_INTERLACE_ADAM7_PASSES;
  2767. break;
  2768. default:
  2769. png_error(png_ptr, "unknown interlace type");
  2770. }
  2771. {
  2772. png_uint_32 height = image->height;
  2773. png_uint_32 width = image->width;
  2774. ptrdiff_t step_row = display->row_bytes;
  2775. unsigned int channels =
  2776. (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
  2777. int pass;
  2778. for (pass = 0; pass < passes; ++pass)
  2779. {
  2780. unsigned int startx, stepx, stepy;
  2781. png_uint_32 y;
  2782. if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
  2783. {
  2784. /* The row may be empty for a short image: */
  2785. if (PNG_PASS_COLS(width, pass) == 0)
  2786. continue;
  2787. startx = PNG_PASS_START_COL(pass) * channels;
  2788. stepx = PNG_PASS_COL_OFFSET(pass) * channels;
  2789. y = PNG_PASS_START_ROW(pass);
  2790. stepy = PNG_PASS_ROW_OFFSET(pass);
  2791. }
  2792. else
  2793. {
  2794. y = 0;
  2795. startx = 0;
  2796. stepx = channels;
  2797. stepy = 1;
  2798. }
  2799. for (; y<height; y += stepy)
  2800. {
  2801. png_bytep inrow = png_voidcast(png_bytep, display->local_row);
  2802. png_bytep outrow;
  2803. png_const_bytep end_row;
  2804. /* Read the row, which is packed: */
  2805. png_read_row(png_ptr, inrow, NULL);
  2806. outrow = png_voidcast(png_bytep, display->first_row);
  2807. outrow += y * step_row;
  2808. end_row = outrow + width * channels;
  2809. /* Now do the composition on each pixel in this row. */
  2810. outrow += startx;
  2811. for (; outrow < end_row; outrow += stepx)
  2812. {
  2813. png_byte alpha = inrow[channels];
  2814. if (alpha > 0) /* else no change to the output */
  2815. {
  2816. unsigned int c;
  2817. for (c=0; c<channels; ++c)
  2818. {
  2819. png_uint_32 component = inrow[c];
  2820. if (alpha < 255) /* else just use component */
  2821. {
  2822. /* This is PNG_OPTIMIZED_ALPHA, the component value
  2823. * is a linear 8-bit value. Combine this with the
  2824. * current outrow[c] value which is sRGB encoded.
  2825. * Arithmetic here is 16-bits to preserve the output
  2826. * values correctly.
  2827. */
  2828. component *= 257*255; /* =65535 */
  2829. component += (255-alpha)*png_sRGB_table[outrow[c]];
  2830. /* So 'component' is scaled by 255*65535 and is
  2831. * therefore appropriate for the sRGB to linear
  2832. * conversion table.
  2833. */
  2834. component = PNG_sRGB_FROM_LINEAR(component);
  2835. }
  2836. outrow[c] = (png_byte)component;
  2837. }
  2838. }
  2839. inrow += channels+1; /* components and alpha channel */
  2840. }
  2841. }
  2842. }
  2843. }
  2844. return 1;
  2845. }
  2846. /* The do_local_background case; called when all the following transforms are to
  2847. * be done:
  2848. *
  2849. * PNG_RGB_TO_GRAY
  2850. * PNG_COMPOSITE
  2851. * PNG_GAMMA
  2852. *
  2853. * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
  2854. * PNG_COMPOSITE code performs gamma correction, so we get double gamma
  2855. * correction. The fix-up is to prevent the PNG_COMPOSITE operation from
  2856. * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
  2857. * row and handles the removal or pre-multiplication of the alpha channel.
  2858. */
  2859. static int
  2860. png_image_read_background(png_voidp argument)
  2861. {
  2862. png_image_read_control *display = png_voidcast(png_image_read_control*,
  2863. argument);
  2864. png_imagep image = display->image;
  2865. png_structrp png_ptr = image->opaque->png_ptr;
  2866. png_inforp info_ptr = image->opaque->info_ptr;
  2867. png_uint_32 height = image->height;
  2868. png_uint_32 width = image->width;
  2869. int pass, passes;
  2870. /* Double check the convoluted logic below. We expect to get here with
  2871. * libpng doing rgb to gray and gamma correction but background processing
  2872. * left to the png_image_read_background function. The rows libpng produce
  2873. * might be 8 or 16-bit but should always have two channels; gray plus alpha.
  2874. */
  2875. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
  2876. png_error(png_ptr, "lost rgb to gray");
  2877. if ((png_ptr->transformations & PNG_COMPOSE) != 0)
  2878. png_error(png_ptr, "unexpected compose");
  2879. if (png_get_channels(png_ptr, info_ptr) != 2)
  2880. png_error(png_ptr, "lost/gained channels");
  2881. /* Expect the 8-bit case to always remove the alpha channel */
  2882. if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
  2883. (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
  2884. png_error(png_ptr, "unexpected 8-bit transformation");
  2885. switch (png_ptr->interlaced)
  2886. {
  2887. case PNG_INTERLACE_NONE:
  2888. passes = 1;
  2889. break;
  2890. case PNG_INTERLACE_ADAM7:
  2891. passes = PNG_INTERLACE_ADAM7_PASSES;
  2892. break;
  2893. default:
  2894. png_error(png_ptr, "unknown interlace type");
  2895. }
  2896. /* Use direct access to info_ptr here because otherwise the simplified API
  2897. * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
  2898. * checking the value after libpng expansions, not the original value in the
  2899. * PNG.
  2900. */
  2901. switch (info_ptr->bit_depth)
  2902. {
  2903. case 8:
  2904. /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
  2905. * to be removed by composing on a background: either the row if
  2906. * display->background is NULL or display->background->green if not.
  2907. * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
  2908. */
  2909. {
  2910. png_bytep first_row = png_voidcast(png_bytep, display->first_row);
  2911. ptrdiff_t step_row = display->row_bytes;
  2912. for (pass = 0; pass < passes; ++pass)
  2913. {
  2914. png_bytep row = png_voidcast(png_bytep, display->first_row);
  2915. unsigned int startx, stepx, stepy;
  2916. png_uint_32 y;
  2917. if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
  2918. {
  2919. /* The row may be empty for a short image: */
  2920. if (PNG_PASS_COLS(width, pass) == 0)
  2921. continue;
  2922. startx = PNG_PASS_START_COL(pass);
  2923. stepx = PNG_PASS_COL_OFFSET(pass);
  2924. y = PNG_PASS_START_ROW(pass);
  2925. stepy = PNG_PASS_ROW_OFFSET(pass);
  2926. }
  2927. else
  2928. {
  2929. y = 0;
  2930. startx = 0;
  2931. stepx = stepy = 1;
  2932. }
  2933. if (display->background == NULL)
  2934. {
  2935. for (; y<height; y += stepy)
  2936. {
  2937. png_bytep inrow = png_voidcast(png_bytep,
  2938. display->local_row);
  2939. png_bytep outrow = first_row + y * step_row;
  2940. png_const_bytep end_row = outrow + width;
  2941. /* Read the row, which is packed: */
  2942. png_read_row(png_ptr, inrow, NULL);
  2943. /* Now do the composition on each pixel in this row. */
  2944. outrow += startx;
  2945. for (; outrow < end_row; outrow += stepx)
  2946. {
  2947. png_byte alpha = inrow[1];
  2948. if (alpha > 0) /* else no change to the output */
  2949. {
  2950. png_uint_32 component = inrow[0];
  2951. if (alpha < 255) /* else just use component */
  2952. {
  2953. /* Since PNG_OPTIMIZED_ALPHA was not set it is
  2954. * necessary to invert the sRGB transfer
  2955. * function and multiply the alpha out.
  2956. */
  2957. component = png_sRGB_table[component] * alpha;
  2958. component += png_sRGB_table[outrow[0]] *
  2959. (255-alpha);
  2960. component = PNG_sRGB_FROM_LINEAR(component);
  2961. }
  2962. outrow[0] = (png_byte)component;
  2963. }
  2964. inrow += 2; /* gray and alpha channel */
  2965. }
  2966. }
  2967. }
  2968. else /* constant background value */
  2969. {
  2970. png_byte background8 = display->background->green;
  2971. png_uint_16 background = png_sRGB_table[background8];
  2972. for (; y<height; y += stepy)
  2973. {
  2974. png_bytep inrow = png_voidcast(png_bytep,
  2975. display->local_row);
  2976. png_bytep outrow = first_row + y * step_row;
  2977. png_const_bytep end_row = outrow + width;
  2978. /* Read the row, which is packed: */
  2979. png_read_row(png_ptr, inrow, NULL);
  2980. /* Now do the composition on each pixel in this row. */
  2981. outrow += startx;
  2982. for (; outrow < end_row; outrow += stepx)
  2983. {
  2984. png_byte alpha = inrow[1];
  2985. if (alpha > 0) /* else use background */
  2986. {
  2987. png_uint_32 component = inrow[0];
  2988. if (alpha < 255) /* else just use component */
  2989. {
  2990. component = png_sRGB_table[component] * alpha;
  2991. component += background * (255-alpha);
  2992. component = PNG_sRGB_FROM_LINEAR(component);
  2993. }
  2994. outrow[0] = (png_byte)component;
  2995. }
  2996. else
  2997. outrow[0] = background8;
  2998. inrow += 2; /* gray and alpha channel */
  2999. }
  3000. row += display->row_bytes;
  3001. }
  3002. }
  3003. }
  3004. }
  3005. break;
  3006. case 16:
  3007. /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
  3008. * still be done and, maybe, the alpha channel removed. This code also
  3009. * handles the alpha-first option.
  3010. */
  3011. {
  3012. png_uint_16p first_row = png_voidcast(png_uint_16p,
  3013. display->first_row);
  3014. /* The division by two is safe because the caller passed in a
  3015. * stride which was multiplied by 2 (below) to get row_bytes.
  3016. */
  3017. ptrdiff_t step_row = display->row_bytes / 2;
  3018. unsigned int preserve_alpha = (image->format &
  3019. PNG_FORMAT_FLAG_ALPHA) != 0;
  3020. unsigned int outchannels = 1U+preserve_alpha;
  3021. int swap_alpha = 0;
  3022. # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
  3023. if (preserve_alpha != 0 &&
  3024. (image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
  3025. swap_alpha = 1;
  3026. # endif
  3027. for (pass = 0; pass < passes; ++pass)
  3028. {
  3029. unsigned int startx, stepx, stepy;
  3030. png_uint_32 y;
  3031. /* The 'x' start and step are adjusted to output components here.
  3032. */
  3033. if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
  3034. {
  3035. /* The row may be empty for a short image: */
  3036. if (PNG_PASS_COLS(width, pass) == 0)
  3037. continue;
  3038. startx = PNG_PASS_START_COL(pass) * outchannels;
  3039. stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
  3040. y = PNG_PASS_START_ROW(pass);
  3041. stepy = PNG_PASS_ROW_OFFSET(pass);
  3042. }
  3043. else
  3044. {
  3045. y = 0;
  3046. startx = 0;
  3047. stepx = outchannels;
  3048. stepy = 1;
  3049. }
  3050. for (; y<height; y += stepy)
  3051. {
  3052. png_const_uint_16p inrow;
  3053. png_uint_16p outrow = first_row + y*step_row;
  3054. png_uint_16p end_row = outrow + width * outchannels;
  3055. /* Read the row, which is packed: */
  3056. png_read_row(png_ptr, png_voidcast(png_bytep,
  3057. display->local_row), NULL);
  3058. inrow = png_voidcast(png_const_uint_16p, display->local_row);
  3059. /* Now do the pre-multiplication on each pixel in this row.
  3060. */
  3061. outrow += startx;
  3062. for (; outrow < end_row; outrow += stepx)
  3063. {
  3064. png_uint_32 component = inrow[0];
  3065. png_uint_16 alpha = inrow[1];
  3066. if (alpha > 0) /* else 0 */
  3067. {
  3068. if (alpha < 65535) /* else just use component */
  3069. {
  3070. component *= alpha;
  3071. component += 32767;
  3072. component /= 65535;
  3073. }
  3074. }
  3075. else
  3076. component = 0;
  3077. outrow[swap_alpha] = (png_uint_16)component;
  3078. if (preserve_alpha != 0)
  3079. outrow[1 ^ swap_alpha] = alpha;
  3080. inrow += 2; /* components and alpha channel */
  3081. }
  3082. }
  3083. }
  3084. }
  3085. break;
  3086. #ifdef __GNUC__
  3087. default:
  3088. png_error(png_ptr, "unexpected bit depth");
  3089. #endif
  3090. }
  3091. return 1;
  3092. }
  3093. /* The guts of png_image_finish_read as a png_safe_execute callback. */
  3094. static int
  3095. png_image_read_direct(png_voidp argument)
  3096. {
  3097. png_image_read_control *display = png_voidcast(png_image_read_control*,
  3098. argument);
  3099. png_imagep image = display->image;
  3100. png_structrp png_ptr = image->opaque->png_ptr;
  3101. png_inforp info_ptr = image->opaque->info_ptr;
  3102. png_uint_32 format = image->format;
  3103. int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
  3104. int do_local_compose = 0;
  3105. int do_local_background = 0; /* to avoid double gamma correction bug */
  3106. int passes = 0;
  3107. /* Add transforms to ensure the correct output format is produced then check
  3108. * that the required implementation support is there. Always expand; always
  3109. * need 8 bits minimum, no palette and expanded tRNS.
  3110. */
  3111. png_set_expand(png_ptr);
  3112. /* Now check the format to see if it was modified. */
  3113. {
  3114. png_uint_32 base_format = png_image_format(png_ptr) &
  3115. ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
  3116. png_uint_32 change = format ^ base_format;
  3117. png_fixed_point output_gamma;
  3118. int mode; /* alpha mode */
  3119. /* Do this first so that we have a record if rgb to gray is happening. */
  3120. if ((change & PNG_FORMAT_FLAG_COLOR) != 0)
  3121. {
  3122. /* gray<->color transformation required. */
  3123. if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
  3124. png_set_gray_to_rgb(png_ptr);
  3125. else
  3126. {
  3127. /* libpng can't do both rgb to gray and
  3128. * background/pre-multiplication if there is also significant gamma
  3129. * correction, because both operations require linear colors and
  3130. * the code only supports one transform doing the gamma correction.
  3131. * Handle this by doing the pre-multiplication or background
  3132. * operation in this code, if necessary.
  3133. *
  3134. * TODO: fix this by rewriting pngrtran.c (!)
  3135. *
  3136. * For the moment (given that fixing this in pngrtran.c is an
  3137. * enormous change) 'do_local_background' is used to indicate that
  3138. * the problem exists.
  3139. */
  3140. if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  3141. do_local_background = 1/*maybe*/;
  3142. png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
  3143. PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
  3144. }
  3145. change &= ~PNG_FORMAT_FLAG_COLOR;
  3146. }
  3147. /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
  3148. */
  3149. {
  3150. png_fixed_point input_gamma_default;
  3151. if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 &&
  3152. (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
  3153. input_gamma_default = PNG_GAMMA_LINEAR;
  3154. else
  3155. input_gamma_default = PNG_DEFAULT_sRGB;
  3156. /* Call png_set_alpha_mode to set the default for the input gamma; the
  3157. * output gamma is set by a second call below.
  3158. */
  3159. png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
  3160. }
  3161. if (linear != 0)
  3162. {
  3163. /* If there *is* an alpha channel in the input it must be multiplied
  3164. * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
  3165. */
  3166. if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  3167. mode = PNG_ALPHA_STANDARD; /* associated alpha */
  3168. else
  3169. mode = PNG_ALPHA_PNG;
  3170. output_gamma = PNG_GAMMA_LINEAR;
  3171. }
  3172. else
  3173. {
  3174. mode = PNG_ALPHA_PNG;
  3175. output_gamma = PNG_DEFAULT_sRGB;
  3176. }
  3177. if ((change & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0)
  3178. {
  3179. mode = PNG_ALPHA_OPTIMIZED;
  3180. change &= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
  3181. }
  3182. /* If 'do_local_background' is set check for the presence of gamma
  3183. * correction; this is part of the work-round for the libpng bug
  3184. * described above.
  3185. *
  3186. * TODO: fix libpng and remove this.
  3187. */
  3188. if (do_local_background != 0)
  3189. {
  3190. png_fixed_point gtest;
  3191. /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
  3192. * gamma correction, the screen gamma hasn't been set on png_struct
  3193. * yet; it's set below. png_struct::gamma, however, is set to the
  3194. * final value.
  3195. */
  3196. if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
  3197. PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0)
  3198. do_local_background = 0;
  3199. else if (mode == PNG_ALPHA_STANDARD)
  3200. {
  3201. do_local_background = 2/*required*/;
  3202. mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
  3203. }
  3204. /* else leave as 1 for the checks below */
  3205. }
  3206. /* If the bit-depth changes then handle that here. */
  3207. if ((change & PNG_FORMAT_FLAG_LINEAR) != 0)
  3208. {
  3209. if (linear != 0 /*16-bit output*/)
  3210. png_set_expand_16(png_ptr);
  3211. else /* 8-bit output */
  3212. png_set_scale_16(png_ptr);
  3213. change &= ~PNG_FORMAT_FLAG_LINEAR;
  3214. }
  3215. /* Now the background/alpha channel changes. */
  3216. if ((change & PNG_FORMAT_FLAG_ALPHA) != 0)
  3217. {
  3218. /* Removing an alpha channel requires composition for the 8-bit
  3219. * formats; for the 16-bit it is already done, above, by the
  3220. * pre-multiplication and the channel just needs to be stripped.
  3221. */
  3222. if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  3223. {
  3224. /* If RGB->gray is happening the alpha channel must be left and the
  3225. * operation completed locally.
  3226. *
  3227. * TODO: fix libpng and remove this.
  3228. */
  3229. if (do_local_background != 0)
  3230. do_local_background = 2/*required*/;
  3231. /* 16-bit output: just remove the channel */
  3232. else if (linear != 0) /* compose on black (well, pre-multiply) */
  3233. png_set_strip_alpha(png_ptr);
  3234. /* 8-bit output: do an appropriate compose */
  3235. else if (display->background != NULL)
  3236. {
  3237. png_color_16 c;
  3238. c.index = 0; /*unused*/
  3239. c.red = display->background->red;
  3240. c.green = display->background->green;
  3241. c.blue = display->background->blue;
  3242. c.gray = display->background->green;
  3243. /* This is always an 8-bit sRGB value, using the 'green' channel
  3244. * for gray is much better than calculating the luminance here;
  3245. * we can get off-by-one errors in that calculation relative to
  3246. * the app expectations and that will show up in transparent
  3247. * pixels.
  3248. */
  3249. png_set_background_fixed(png_ptr, &c,
  3250. PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
  3251. 0/*gamma: not used*/);
  3252. }
  3253. else /* compose on row: implemented below. */
  3254. {
  3255. do_local_compose = 1;
  3256. /* This leaves the alpha channel in the output, so it has to be
  3257. * removed by the code below. Set the encoding to the 'OPTIMIZE'
  3258. * one so the code only has to hack on the pixels that require
  3259. * composition.
  3260. */
  3261. mode = PNG_ALPHA_OPTIMIZED;
  3262. }
  3263. }
  3264. else /* output needs an alpha channel */
  3265. {
  3266. /* This is tricky because it happens before the swap operation has
  3267. * been accomplished; however, the swap does *not* swap the added
  3268. * alpha channel (weird API), so it must be added in the correct
  3269. * place.
  3270. */
  3271. png_uint_32 filler; /* opaque filler */
  3272. int where;
  3273. if (linear != 0)
  3274. filler = 65535;
  3275. else
  3276. filler = 255;
  3277. #ifdef PNG_FORMAT_AFIRST_SUPPORTED
  3278. if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
  3279. {
  3280. where = PNG_FILLER_BEFORE;
  3281. change &= ~PNG_FORMAT_FLAG_AFIRST;
  3282. }
  3283. else
  3284. #endif
  3285. where = PNG_FILLER_AFTER;
  3286. png_set_add_alpha(png_ptr, filler, where);
  3287. }
  3288. /* This stops the (irrelevant) call to swap_alpha below. */
  3289. change &= ~PNG_FORMAT_FLAG_ALPHA;
  3290. }
  3291. /* Now set the alpha mode correctly; this is always done, even if there is
  3292. * no alpha channel in either the input or the output because it correctly
  3293. * sets the output gamma.
  3294. */
  3295. png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
  3296. # ifdef PNG_FORMAT_BGR_SUPPORTED
  3297. if ((change & PNG_FORMAT_FLAG_BGR) != 0)
  3298. {
  3299. /* Check only the output format; PNG is never BGR; don't do this if
  3300. * the output is gray, but fix up the 'format' value in that case.
  3301. */
  3302. if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
  3303. png_set_bgr(png_ptr);
  3304. else
  3305. format &= ~PNG_FORMAT_FLAG_BGR;
  3306. change &= ~PNG_FORMAT_FLAG_BGR;
  3307. }
  3308. # endif
  3309. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  3310. if ((change & PNG_FORMAT_FLAG_AFIRST) != 0)
  3311. {
  3312. /* Only relevant if there is an alpha channel - it's particularly
  3313. * important to handle this correctly because do_local_compose may
  3314. * be set above and then libpng will keep the alpha channel for this
  3315. * code to remove.
  3316. */
  3317. if ((format & PNG_FORMAT_FLAG_ALPHA) != 0)
  3318. {
  3319. /* Disable this if doing a local background,
  3320. * TODO: remove this when local background is no longer required.
  3321. */
  3322. if (do_local_background != 2)
  3323. png_set_swap_alpha(png_ptr);
  3324. }
  3325. else
  3326. format &= ~PNG_FORMAT_FLAG_AFIRST;
  3327. change &= ~PNG_FORMAT_FLAG_AFIRST;
  3328. }
  3329. # endif
  3330. /* If the *output* is 16-bit then we need to check for a byte-swap on this
  3331. * architecture.
  3332. */
  3333. if (linear != 0)
  3334. {
  3335. png_uint_16 le = 0x0001;
  3336. if ((*(png_const_bytep) & le) != 0)
  3337. png_set_swap(png_ptr);
  3338. }
  3339. /* If change is not now 0 some transformation is missing - error out. */
  3340. if (change != 0)
  3341. png_error(png_ptr, "png_read_image: unsupported transformation");
  3342. }
  3343. PNG_SKIP_CHUNKS(png_ptr);
  3344. /* Update the 'info' structure and make sure the result is as required; first
  3345. * make sure to turn on the interlace handling if it will be required
  3346. * (because it can't be turned on *after* the call to png_read_update_info!)
  3347. *
  3348. * TODO: remove the do_local_background fixup below.
  3349. */
  3350. if (do_local_compose == 0 && do_local_background != 2)
  3351. passes = png_set_interlace_handling(png_ptr);
  3352. png_read_update_info(png_ptr, info_ptr);
  3353. {
  3354. png_uint_32 info_format = 0;
  3355. if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
  3356. info_format |= PNG_FORMAT_FLAG_COLOR;
  3357. if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  3358. {
  3359. /* do_local_compose removes this channel below. */
  3360. if (do_local_compose == 0)
  3361. {
  3362. /* do_local_background does the same if required. */
  3363. if (do_local_background != 2 ||
  3364. (format & PNG_FORMAT_FLAG_ALPHA) != 0)
  3365. info_format |= PNG_FORMAT_FLAG_ALPHA;
  3366. }
  3367. }
  3368. else if (do_local_compose != 0) /* internal error */
  3369. png_error(png_ptr, "png_image_read: alpha channel lost");
  3370. if ((format & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) {
  3371. info_format |= PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
  3372. }
  3373. if (info_ptr->bit_depth == 16)
  3374. info_format |= PNG_FORMAT_FLAG_LINEAR;
  3375. #ifdef PNG_FORMAT_BGR_SUPPORTED
  3376. if ((png_ptr->transformations & PNG_BGR) != 0)
  3377. info_format |= PNG_FORMAT_FLAG_BGR;
  3378. #endif
  3379. #ifdef PNG_FORMAT_AFIRST_SUPPORTED
  3380. if (do_local_background == 2)
  3381. {
  3382. if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
  3383. info_format |= PNG_FORMAT_FLAG_AFIRST;
  3384. }
  3385. if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
  3386. ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
  3387. (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
  3388. {
  3389. if (do_local_background == 2)
  3390. png_error(png_ptr, "unexpected alpha swap transformation");
  3391. info_format |= PNG_FORMAT_FLAG_AFIRST;
  3392. }
  3393. # endif
  3394. /* This is actually an internal error. */
  3395. if (info_format != format)
  3396. png_error(png_ptr, "png_read_image: invalid transformations");
  3397. }
  3398. /* Now read the rows. If do_local_compose is set then it is necessary to use
  3399. * a local row buffer. The output will be GA, RGBA or BGRA and must be
  3400. * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
  3401. * display acts as a flag.
  3402. */
  3403. {
  3404. png_voidp first_row = display->buffer;
  3405. ptrdiff_t row_bytes = display->row_stride;
  3406. if (linear != 0)
  3407. row_bytes *= 2;
  3408. /* The following expression is designed to work correctly whether it gives
  3409. * a signed or an unsigned result.
  3410. */
  3411. if (row_bytes < 0)
  3412. {
  3413. char *ptr = png_voidcast(char*, first_row);
  3414. ptr += (image->height-1) * (-row_bytes);
  3415. first_row = png_voidcast(png_voidp, ptr);
  3416. }
  3417. display->first_row = first_row;
  3418. display->row_bytes = row_bytes;
  3419. }
  3420. if (do_local_compose != 0)
  3421. {
  3422. int result;
  3423. png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
  3424. display->local_row = row;
  3425. result = png_safe_execute(image, png_image_read_composite, display);
  3426. display->local_row = NULL;
  3427. png_free(png_ptr, row);
  3428. return result;
  3429. }
  3430. else if (do_local_background == 2)
  3431. {
  3432. int result;
  3433. png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
  3434. display->local_row = row;
  3435. result = png_safe_execute(image, png_image_read_background, display);
  3436. display->local_row = NULL;
  3437. png_free(png_ptr, row);
  3438. return result;
  3439. }
  3440. else
  3441. {
  3442. png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
  3443. while (--passes >= 0)
  3444. {
  3445. png_uint_32 y = image->height;
  3446. png_bytep row = png_voidcast(png_bytep, display->first_row);
  3447. for (; y > 0; --y)
  3448. {
  3449. png_read_row(png_ptr, row, NULL);
  3450. row += row_bytes;
  3451. }
  3452. }
  3453. return 1;
  3454. }
  3455. }
  3456. int PNGAPI
  3457. png_image_finish_read(png_imagep image, png_const_colorp background,
  3458. void *buffer, png_int_32 row_stride, void *colormap)
  3459. {
  3460. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  3461. {
  3462. /* Check for row_stride overflow. This check is not performed on the
  3463. * original PNG format because it may not occur in the output PNG format
  3464. * and libpng deals with the issues of reading the original.
  3465. */
  3466. unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
  3467. /* The following checks just the 'row_stride' calculation to ensure it
  3468. * fits in a signed 32-bit value. Because channels/components can be
  3469. * either 1 or 2 bytes in size the length of a row can still overflow 32
  3470. * bits; this is just to verify that the 'row_stride' argument can be
  3471. * represented.
  3472. */
  3473. if (image->width <= 0x7fffffffU/channels) /* no overflow */
  3474. {
  3475. png_uint_32 check;
  3476. png_uint_32 png_row_stride = image->width * channels;
  3477. if (row_stride == 0)
  3478. row_stride = (png_int_32)/*SAFE*/png_row_stride;
  3479. if (row_stride < 0)
  3480. check = (png_uint_32)(-row_stride);
  3481. else
  3482. check = (png_uint_32)row_stride;
  3483. /* This verifies 'check', the absolute value of the actual stride
  3484. * passed in and detects overflow in the application calculation (i.e.
  3485. * if the app did actually pass in a non-zero 'row_stride'.
  3486. */
  3487. if (image->opaque != NULL && buffer != NULL && check >= png_row_stride)
  3488. {
  3489. /* Now check for overflow of the image buffer calculation; this
  3490. * limits the whole image size to 32 bits for API compatibility with
  3491. * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
  3492. *
  3493. * The PNG_IMAGE_BUFFER_SIZE macro is:
  3494. *
  3495. * (PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)*height*(row_stride))
  3496. *
  3497. * And the component size is always 1 or 2, so make sure that the
  3498. * number of *bytes* that the application is saying are available
  3499. * does actually fit into a 32-bit number.
  3500. *
  3501. * NOTE: this will be changed in 1.7 because PNG_IMAGE_BUFFER_SIZE
  3502. * will be changed to use png_alloc_size_t; bigger images can be
  3503. * accommodated on 64-bit systems.
  3504. */
  3505. if (image->height <=
  3506. 0xffffffffU/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image->format)/check)
  3507. {
  3508. if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
  3509. (image->colormap_entries > 0 && colormap != NULL))
  3510. {
  3511. int result;
  3512. png_image_read_control display;
  3513. memset(&display, 0, (sizeof display));
  3514. display.image = image;
  3515. display.buffer = buffer;
  3516. display.row_stride = row_stride;
  3517. display.colormap = colormap;
  3518. display.background = background;
  3519. display.local_row = NULL;
  3520. /* Choose the correct 'end' routine; for the color-map case
  3521. * all the setup has already been done.
  3522. */
  3523. if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
  3524. result =
  3525. png_safe_execute(image,
  3526. png_image_read_colormap, &display) &&
  3527. png_safe_execute(image,
  3528. png_image_read_colormapped, &display);
  3529. else
  3530. result =
  3531. png_safe_execute(image,
  3532. png_image_read_direct, &display);
  3533. png_image_free(image);
  3534. return result;
  3535. }
  3536. else
  3537. return png_image_error(image,
  3538. "png_image_finish_read[color-map]: no color-map");
  3539. }
  3540. else
  3541. return png_image_error(image,
  3542. "png_image_finish_read: image too large");
  3543. }
  3544. else
  3545. return png_image_error(image,
  3546. "png_image_finish_read: invalid argument");
  3547. }
  3548. else
  3549. return png_image_error(image,
  3550. "png_image_finish_read: row_stride too large");
  3551. }
  3552. else if (image != NULL)
  3553. return png_image_error(image,
  3554. "png_image_finish_read: damaged PNG_IMAGE_VERSION");
  3555. return 0;
  3556. }
  3557. #endif /* SIMPLIFIED_READ */
  3558. #endif /* READ */