pngread.c 135 KB

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