pngread.c 139 KB

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