Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

png.c 149KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295
  1. /* png.c - location for general purpose libpng functions
  2. *
  3. * Last changed in libpng 1.6.1 [March 28, 2013]
  4. * Copyright (c) 1998-2013 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. #include "pngpriv.h"
  13. /* Generate a compiler error if there is an old png.h in the search path. */
  14. typedef png_libpng_version_1_6_1 Your_png_h_is_not_version_1_6_1;
  15. /* Tells libpng that we have already handled the first "num_bytes" bytes
  16. * of the PNG file signature. If the PNG data is embedded into another
  17. * stream we can set num_bytes = 8 so that libpng will not attempt to read
  18. * or write any of the magic bytes before it starts on the IHDR.
  19. */
  20. #ifdef PNG_READ_SUPPORTED
  21. void PNGAPI
  22. png_set_sig_bytes(png_structrp png_ptr, int num_bytes)
  23. {
  24. png_debug(1, "in png_set_sig_bytes");
  25. if (png_ptr == NULL)
  26. return;
  27. if (num_bytes > 8)
  28. png_error(png_ptr, "Too many bytes for PNG signature");
  29. png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
  30. }
  31. /* Checks whether the supplied bytes match the PNG signature. We allow
  32. * checking less than the full 8-byte signature so that those apps that
  33. * already read the first few bytes of a file to determine the file type
  34. * can simply check the remaining bytes for extra assurance. Returns
  35. * an integer less than, equal to, or greater than zero if sig is found,
  36. * respectively, to be less than, to match, or be greater than the correct
  37. * PNG signature (this is the same behavior as strcmp, memcmp, etc).
  38. */
  39. int PNGAPI
  40. png_sig_cmp(png_const_bytep sig, png_size_t start, png_size_t num_to_check)
  41. {
  42. png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  43. if (num_to_check > 8)
  44. num_to_check = 8;
  45. else if (num_to_check < 1)
  46. return (-1);
  47. if (start > 7)
  48. return (-1);
  49. if (start + num_to_check > 8)
  50. num_to_check = 8 - start;
  51. return ((int)(memcmp(&sig[start], &png_signature[start], num_to_check)));
  52. }
  53. #endif /* PNG_READ_SUPPORTED */
  54. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  55. /* Function to allocate memory for zlib */
  56. PNG_FUNCTION(voidpf /* PRIVATE */,
  57. png_zalloc,(voidpf png_ptr, uInt items, uInt size),PNG_ALLOCATED)
  58. {
  59. png_alloc_size_t num_bytes = size;
  60. if (png_ptr == NULL)
  61. return NULL;
  62. if (items >= (~(png_alloc_size_t)0)/size)
  63. {
  64. png_warning (png_voidcast(png_structrp, png_ptr),
  65. "Potential overflow in png_zalloc()");
  66. return NULL;
  67. }
  68. num_bytes *= items;
  69. return png_malloc_warn(png_voidcast(png_structrp, png_ptr), num_bytes);
  70. }
  71. /* Function to free memory for zlib */
  72. void /* PRIVATE */
  73. png_zfree(voidpf png_ptr, voidpf ptr)
  74. {
  75. png_free(png_voidcast(png_const_structrp,png_ptr), ptr);
  76. }
  77. /* Reset the CRC variable to 32 bits of 1's. Care must be taken
  78. * in case CRC is > 32 bits to leave the top bits 0.
  79. */
  80. void /* PRIVATE */
  81. png_reset_crc(png_structrp png_ptr)
  82. {
  83. /* The cast is safe because the crc is a 32 bit value. */
  84. png_ptr->crc = (png_uint_32)crc32(0, Z_NULL, 0);
  85. }
  86. /* Calculate the CRC over a section of data. We can only pass as
  87. * much data to this routine as the largest single buffer size. We
  88. * also check that this data will actually be used before going to the
  89. * trouble of calculating it.
  90. */
  91. void /* PRIVATE */
  92. png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, png_size_t length)
  93. {
  94. int need_crc = 1;
  95. if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name))
  96. {
  97. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  98. (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  99. need_crc = 0;
  100. }
  101. else /* critical */
  102. {
  103. if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  104. need_crc = 0;
  105. }
  106. /* 'uLong' is defined in zlib.h as unsigned long; this means that on some
  107. * systems it is a 64 bit value. crc32, however, returns 32 bits so the
  108. * following cast is safe. 'uInt' may be no more than 16 bits, so it is
  109. * necessary to perform a loop here.
  110. */
  111. if (need_crc && length > 0)
  112. {
  113. uLong crc = png_ptr->crc; /* Should never issue a warning */
  114. do
  115. {
  116. uInt safe_length = (uInt)length;
  117. if (safe_length == 0)
  118. safe_length = (uInt)-1; /* evil, but safe */
  119. crc = crc32(crc, ptr, safe_length);
  120. /* The following should never issue compiler warnings; if they do the
  121. * target system has characteristics that will probably violate other
  122. * assumptions within the libpng code.
  123. */
  124. ptr += safe_length;
  125. length -= safe_length;
  126. }
  127. while (length > 0);
  128. /* And the following is always safe because the crc is only 32 bits. */
  129. png_ptr->crc = (png_uint_32)crc;
  130. }
  131. }
  132. /* Check a user supplied version number, called from both read and write
  133. * functions that create a png_struct.
  134. */
  135. int
  136. png_user_version_check(png_structrp png_ptr, png_const_charp user_png_ver)
  137. {
  138. if (user_png_ver)
  139. {
  140. int i = 0;
  141. do
  142. {
  143. if (user_png_ver[i] != png_libpng_ver[i])
  144. png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  145. } while (png_libpng_ver[i++]);
  146. }
  147. else
  148. png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  149. if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
  150. {
  151. /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
  152. * we must recompile any applications that use any older library version.
  153. * For versions after libpng 1.0, we will be compatible, so we need
  154. * only check the first and third digits (note that when we reach version
  155. * 1.10 we will need to check the fourth symbol, namely user_png_ver[3]).
  156. */
  157. if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
  158. (user_png_ver[0] == '1' && (user_png_ver[2] != png_libpng_ver[2] ||
  159. user_png_ver[3] != png_libpng_ver[3])) ||
  160. (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
  161. {
  162. #ifdef PNG_WARNINGS_SUPPORTED
  163. size_t pos = 0;
  164. char m[128];
  165. pos = png_safecat(m, (sizeof m), pos,
  166. "Application built with libpng-");
  167. pos = png_safecat(m, (sizeof m), pos, user_png_ver);
  168. pos = png_safecat(m, (sizeof m), pos, " but running with ");
  169. png_safecat(m, (sizeof m), pos, png_libpng_ver);
  170. png_warning(png_ptr, m);
  171. #endif
  172. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  173. png_ptr->flags = 0;
  174. #endif
  175. return 0;
  176. }
  177. }
  178. /* Success return. */
  179. return 1;
  180. }
  181. /* Generic function to create a png_struct for either read or write - this
  182. * contains the common initialization.
  183. */
  184. PNG_FUNCTION(png_structp /* PRIVATE */,
  185. png_create_png_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
  186. png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp,
  187. png_malloc_ptr, png_free_ptr),PNG_ALLOCATED)
  188. {
  189. png_struct create_struct;
  190. # ifdef PNG_SETJMP_SUPPORTED
  191. jmp_buf create_jmp_buf;
  192. # endif
  193. /* This temporary stack-allocated structure is used to provide a place to
  194. * build enough context to allow the user provided memory allocator (if any)
  195. * to be called.
  196. */
  197. memset(&create_struct, 0, (sizeof create_struct));
  198. /* Added at libpng-1.2.6 */
  199. # ifdef PNG_USER_LIMITS_SUPPORTED
  200. create_struct.user_width_max = PNG_USER_WIDTH_MAX;
  201. create_struct.user_height_max = PNG_USER_HEIGHT_MAX;
  202. # ifdef PNG_USER_CHUNK_CACHE_MAX
  203. /* Added at libpng-1.2.43 and 1.4.0 */
  204. create_struct.user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
  205. # endif
  206. # ifdef PNG_USER_CHUNK_MALLOC_MAX
  207. /* Added at libpng-1.2.43 and 1.4.1, required only for read but exists
  208. * in png_struct regardless.
  209. */
  210. create_struct.user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
  211. # endif
  212. # endif
  213. /* The following two API calls simply set fields in png_struct, so it is safe
  214. * to do them now even though error handling is not yet set up.
  215. */
  216. # ifdef PNG_USER_MEM_SUPPORTED
  217. png_set_mem_fn(&create_struct, mem_ptr, malloc_fn, free_fn);
  218. # endif
  219. /* (*error_fn) can return control to the caller after the error_ptr is set,
  220. * this will result in a memory leak unless the error_fn does something
  221. * extremely sophisticated. The design lacks merit but is implicit in the
  222. * API.
  223. */
  224. png_set_error_fn(&create_struct, error_ptr, error_fn, warn_fn);
  225. # ifdef PNG_SETJMP_SUPPORTED
  226. if (!setjmp(create_jmp_buf))
  227. {
  228. /* Temporarily fake out the longjmp information until we have
  229. * successfully completed this function. This only works if we have
  230. * setjmp() support compiled in, but it is safe - this stuff should
  231. * never happen.
  232. */
  233. create_struct.jmp_buf_ptr = &create_jmp_buf;
  234. create_struct.jmp_buf_size = 0; /*stack allocation*/
  235. create_struct.longjmp_fn = longjmp;
  236. # else
  237. {
  238. # endif
  239. /* Call the general version checker (shared with read and write code):
  240. */
  241. if (png_user_version_check(&create_struct, user_png_ver))
  242. {
  243. png_structrp png_ptr = png_voidcast(png_structrp,
  244. png_malloc_warn(&create_struct, (sizeof *png_ptr)));
  245. if (png_ptr != NULL)
  246. {
  247. /* png_ptr->zstream holds a back-pointer to the png_struct, so
  248. * this can only be done now:
  249. */
  250. create_struct.zstream.zalloc = png_zalloc;
  251. create_struct.zstream.zfree = png_zfree;
  252. create_struct.zstream.opaque = png_ptr;
  253. # ifdef PNG_SETJMP_SUPPORTED
  254. /* Eliminate the local error handling: */
  255. create_struct.jmp_buf_ptr = NULL;
  256. create_struct.jmp_buf_size = 0;
  257. create_struct.longjmp_fn = 0;
  258. # endif
  259. *png_ptr = create_struct;
  260. /* This is the successful return point */
  261. return png_ptr;
  262. }
  263. }
  264. }
  265. /* A longjmp because of a bug in the application storage allocator or a
  266. * simple failure to allocate the png_struct.
  267. */
  268. return NULL;
  269. }
  270. /* Allocate the memory for an info_struct for the application. */
  271. PNG_FUNCTION(png_infop,PNGAPI
  272. png_create_info_struct,(png_const_structrp png_ptr),PNG_ALLOCATED)
  273. {
  274. png_inforp info_ptr;
  275. png_debug(1, "in png_create_info_struct");
  276. if (png_ptr == NULL)
  277. return NULL;
  278. /* Use the internal API that does not (or at least should not) error out, so
  279. * that this call always returns ok. The application typically sets up the
  280. * error handling *after* creating the info_struct because this is the way it
  281. * has always been done in 'example.c'.
  282. */
  283. info_ptr = png_voidcast(png_inforp, png_malloc_base(png_ptr,
  284. (sizeof *info_ptr)));
  285. if (info_ptr != NULL)
  286. memset(info_ptr, 0, (sizeof *info_ptr));
  287. return info_ptr;
  288. }
  289. /* This function frees the memory associated with a single info struct.
  290. * Normally, one would use either png_destroy_read_struct() or
  291. * png_destroy_write_struct() to free an info struct, but this may be
  292. * useful for some applications. From libpng 1.6.0 this function is also used
  293. * internally to implement the png_info release part of the 'struct' destroy
  294. * APIs. This ensures that all possible approaches free the same data (all of
  295. * it).
  296. */
  297. void PNGAPI
  298. png_destroy_info_struct(png_const_structrp png_ptr, png_infopp info_ptr_ptr)
  299. {
  300. png_inforp info_ptr = NULL;
  301. png_debug(1, "in png_destroy_info_struct");
  302. if (png_ptr == NULL)
  303. return;
  304. if (info_ptr_ptr != NULL)
  305. info_ptr = *info_ptr_ptr;
  306. if (info_ptr != NULL)
  307. {
  308. /* Do this first in case of an error below; if the app implements its own
  309. * memory management this can lead to png_free calling png_error, which
  310. * will abort this routine and return control to the app error handler.
  311. * An infinite loop may result if it then tries to free the same info
  312. * ptr.
  313. */
  314. *info_ptr_ptr = NULL;
  315. png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  316. memset(info_ptr, 0, (sizeof *info_ptr));
  317. png_free(png_ptr, info_ptr);
  318. }
  319. }
  320. /* Initialize the info structure. This is now an internal function (0.89)
  321. * and applications using it are urged to use png_create_info_struct()
  322. * instead. Use deprecated in 1.6.0, internal use removed (used internally it
  323. * is just a memset).
  324. *
  325. * NOTE: it is almost inconceivable that this API is used because it bypasses
  326. * the user-memory mechanism and the user error handling/warning mechanisms in
  327. * those cases where it does anything other than a memset.
  328. */
  329. PNG_FUNCTION(void,PNGAPI
  330. png_info_init_3,(png_infopp ptr_ptr, png_size_t png_info_struct_size),
  331. PNG_DEPRECATED)
  332. {
  333. png_inforp info_ptr = *ptr_ptr;
  334. png_debug(1, "in png_info_init_3");
  335. if (info_ptr == NULL)
  336. return;
  337. if ((sizeof (png_info)) > png_info_struct_size)
  338. {
  339. *ptr_ptr = NULL;
  340. /* The following line is why this API should not be used: */
  341. free(info_ptr);
  342. info_ptr = png_voidcast(png_inforp, png_malloc_base(NULL,
  343. (sizeof *info_ptr)));
  344. *ptr_ptr = info_ptr;
  345. }
  346. /* Set everything to 0 */
  347. memset(info_ptr, 0, (sizeof *info_ptr));
  348. }
  349. /* The following API is not called internally */
  350. void PNGAPI
  351. png_data_freer(png_const_structrp png_ptr, png_inforp info_ptr,
  352. int freer, png_uint_32 mask)
  353. {
  354. png_debug(1, "in png_data_freer");
  355. if (png_ptr == NULL || info_ptr == NULL)
  356. return;
  357. if (freer == PNG_DESTROY_WILL_FREE_DATA)
  358. info_ptr->free_me |= mask;
  359. else if (freer == PNG_USER_WILL_FREE_DATA)
  360. info_ptr->free_me &= ~mask;
  361. else
  362. png_error(png_ptr, "Unknown freer parameter in png_data_freer");
  363. }
  364. void PNGAPI
  365. png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
  366. int num)
  367. {
  368. png_debug(1, "in png_free_data");
  369. if (png_ptr == NULL || info_ptr == NULL)
  370. return;
  371. #ifdef PNG_TEXT_SUPPORTED
  372. /* Free text item num or (if num == -1) all text items */
  373. if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
  374. {
  375. if (num != -1)
  376. {
  377. if (info_ptr->text && info_ptr->text[num].key)
  378. {
  379. png_free(png_ptr, info_ptr->text[num].key);
  380. info_ptr->text[num].key = NULL;
  381. }
  382. }
  383. else
  384. {
  385. int i;
  386. for (i = 0; i < info_ptr->num_text; i++)
  387. png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
  388. png_free(png_ptr, info_ptr->text);
  389. info_ptr->text = NULL;
  390. info_ptr->num_text=0;
  391. }
  392. }
  393. #endif
  394. #ifdef PNG_tRNS_SUPPORTED
  395. /* Free any tRNS entry */
  396. if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
  397. {
  398. png_free(png_ptr, info_ptr->trans_alpha);
  399. info_ptr->trans_alpha = NULL;
  400. info_ptr->valid &= ~PNG_INFO_tRNS;
  401. }
  402. #endif
  403. #ifdef PNG_sCAL_SUPPORTED
  404. /* Free any sCAL entry */
  405. if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
  406. {
  407. png_free(png_ptr, info_ptr->scal_s_width);
  408. png_free(png_ptr, info_ptr->scal_s_height);
  409. info_ptr->scal_s_width = NULL;
  410. info_ptr->scal_s_height = NULL;
  411. info_ptr->valid &= ~PNG_INFO_sCAL;
  412. }
  413. #endif
  414. #ifdef PNG_pCAL_SUPPORTED
  415. /* Free any pCAL entry */
  416. if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
  417. {
  418. png_free(png_ptr, info_ptr->pcal_purpose);
  419. png_free(png_ptr, info_ptr->pcal_units);
  420. info_ptr->pcal_purpose = NULL;
  421. info_ptr->pcal_units = NULL;
  422. if (info_ptr->pcal_params != NULL)
  423. {
  424. unsigned int i;
  425. for (i = 0; i < info_ptr->pcal_nparams; i++)
  426. {
  427. png_free(png_ptr, info_ptr->pcal_params[i]);
  428. info_ptr->pcal_params[i] = NULL;
  429. }
  430. png_free(png_ptr, info_ptr->pcal_params);
  431. info_ptr->pcal_params = NULL;
  432. }
  433. info_ptr->valid &= ~PNG_INFO_pCAL;
  434. }
  435. #endif
  436. #ifdef PNG_iCCP_SUPPORTED
  437. /* Free any profile entry */
  438. if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
  439. {
  440. png_free(png_ptr, info_ptr->iccp_name);
  441. png_free(png_ptr, info_ptr->iccp_profile);
  442. info_ptr->iccp_name = NULL;
  443. info_ptr->iccp_profile = NULL;
  444. info_ptr->valid &= ~PNG_INFO_iCCP;
  445. }
  446. #endif
  447. #ifdef PNG_sPLT_SUPPORTED
  448. /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
  449. if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
  450. {
  451. if (num != -1)
  452. {
  453. if (info_ptr->splt_palettes)
  454. {
  455. png_free(png_ptr, info_ptr->splt_palettes[num].name);
  456. png_free(png_ptr, info_ptr->splt_palettes[num].entries);
  457. info_ptr->splt_palettes[num].name = NULL;
  458. info_ptr->splt_palettes[num].entries = NULL;
  459. }
  460. }
  461. else
  462. {
  463. if (info_ptr->splt_palettes_num)
  464. {
  465. int i;
  466. for (i = 0; i < info_ptr->splt_palettes_num; i++)
  467. png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, (int)i);
  468. png_free(png_ptr, info_ptr->splt_palettes);
  469. info_ptr->splt_palettes = NULL;
  470. info_ptr->splt_palettes_num = 0;
  471. }
  472. info_ptr->valid &= ~PNG_INFO_sPLT;
  473. }
  474. }
  475. #endif
  476. #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
  477. if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
  478. {
  479. if (num != -1)
  480. {
  481. if (info_ptr->unknown_chunks)
  482. {
  483. png_free(png_ptr, info_ptr->unknown_chunks[num].data);
  484. info_ptr->unknown_chunks[num].data = NULL;
  485. }
  486. }
  487. else
  488. {
  489. int i;
  490. if (info_ptr->unknown_chunks_num)
  491. {
  492. for (i = 0; i < info_ptr->unknown_chunks_num; i++)
  493. png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, (int)i);
  494. png_free(png_ptr, info_ptr->unknown_chunks);
  495. info_ptr->unknown_chunks = NULL;
  496. info_ptr->unknown_chunks_num = 0;
  497. }
  498. }
  499. }
  500. #endif
  501. #ifdef PNG_hIST_SUPPORTED
  502. /* Free any hIST entry */
  503. if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
  504. {
  505. png_free(png_ptr, info_ptr->hist);
  506. info_ptr->hist = NULL;
  507. info_ptr->valid &= ~PNG_INFO_hIST;
  508. }
  509. #endif
  510. /* Free any PLTE entry that was internally allocated */
  511. if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
  512. {
  513. png_free(png_ptr, info_ptr->palette);
  514. info_ptr->palette = NULL;
  515. info_ptr->valid &= ~PNG_INFO_PLTE;
  516. info_ptr->num_palette = 0;
  517. }
  518. #ifdef PNG_INFO_IMAGE_SUPPORTED
  519. /* Free any image bits attached to the info structure */
  520. if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
  521. {
  522. if (info_ptr->row_pointers)
  523. {
  524. png_uint_32 row;
  525. for (row = 0; row < info_ptr->height; row++)
  526. {
  527. png_free(png_ptr, info_ptr->row_pointers[row]);
  528. info_ptr->row_pointers[row] = NULL;
  529. }
  530. png_free(png_ptr, info_ptr->row_pointers);
  531. info_ptr->row_pointers = NULL;
  532. }
  533. info_ptr->valid &= ~PNG_INFO_IDAT;
  534. }
  535. #endif
  536. if (num != -1)
  537. mask &= ~PNG_FREE_MUL;
  538. info_ptr->free_me &= ~mask;
  539. }
  540. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  541. /* This function returns a pointer to the io_ptr associated with the user
  542. * functions. The application should free any memory associated with this
  543. * pointer before png_write_destroy() or png_read_destroy() are called.
  544. */
  545. png_voidp PNGAPI
  546. png_get_io_ptr(png_const_structrp png_ptr)
  547. {
  548. if (png_ptr == NULL)
  549. return (NULL);
  550. return (png_ptr->io_ptr);
  551. }
  552. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  553. # ifdef PNG_STDIO_SUPPORTED
  554. /* Initialize the default input/output functions for the PNG file. If you
  555. * use your own read or write routines, you can call either png_set_read_fn()
  556. * or png_set_write_fn() instead of png_init_io(). If you have defined
  557. * PNG_NO_STDIO or otherwise disabled PNG_STDIO_SUPPORTED, you must use a
  558. * function of your own because "FILE *" isn't necessarily available.
  559. */
  560. void PNGAPI
  561. png_init_io(png_structrp png_ptr, png_FILE_p fp)
  562. {
  563. png_debug(1, "in png_init_io");
  564. if (png_ptr == NULL)
  565. return;
  566. png_ptr->io_ptr = (png_voidp)fp;
  567. }
  568. # endif
  569. #ifdef PNG_SAVE_INT_32_SUPPORTED
  570. /* The png_save_int_32 function assumes integers are stored in two's
  571. * complement format. If this isn't the case, then this routine needs to
  572. * be modified to write data in two's complement format. Note that,
  573. * the following works correctly even if png_int_32 has more than 32 bits
  574. * (compare the more complex code required on read for sign extension.)
  575. */
  576. void PNGAPI
  577. png_save_int_32(png_bytep buf, png_int_32 i)
  578. {
  579. buf[0] = (png_byte)((i >> 24) & 0xff);
  580. buf[1] = (png_byte)((i >> 16) & 0xff);
  581. buf[2] = (png_byte)((i >> 8) & 0xff);
  582. buf[3] = (png_byte)(i & 0xff);
  583. }
  584. #endif
  585. # ifdef PNG_TIME_RFC1123_SUPPORTED
  586. /* Convert the supplied time into an RFC 1123 string suitable for use in
  587. * a "Creation Time" or other text-based time string.
  588. */
  589. int PNGAPI
  590. png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime)
  591. {
  592. static PNG_CONST char short_months[12][4] =
  593. {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  594. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  595. if (out == NULL)
  596. return 0;
  597. if (ptime->year > 9999 /* RFC1123 limitation */ ||
  598. ptime->month == 0 || ptime->month > 12 ||
  599. ptime->day == 0 || ptime->day > 31 ||
  600. ptime->hour > 23 || ptime->minute > 59 ||
  601. ptime->second > 60)
  602. return 0;
  603. {
  604. size_t pos = 0;
  605. char number_buf[5]; /* enough for a four-digit year */
  606. # define APPEND_STRING(string) pos = png_safecat(out, 29, pos, (string))
  607. # define APPEND_NUMBER(format, value)\
  608. APPEND_STRING(PNG_FORMAT_NUMBER(number_buf, format, (value)))
  609. # define APPEND(ch) if (pos < 28) out[pos++] = (ch)
  610. APPEND_NUMBER(PNG_NUMBER_FORMAT_u, (unsigned)ptime->day);
  611. APPEND(' ');
  612. APPEND_STRING(short_months[(ptime->month - 1)]);
  613. APPEND(' ');
  614. APPEND_NUMBER(PNG_NUMBER_FORMAT_u, ptime->year);
  615. APPEND(' ');
  616. APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->hour);
  617. APPEND(':');
  618. APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->minute);
  619. APPEND(':');
  620. APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second);
  621. APPEND_STRING(" +0000"); /* This reliably terminates the buffer */
  622. # undef APPEND
  623. # undef APPEND_NUMBER
  624. # undef APPEND_STRING
  625. }
  626. return 1;
  627. }
  628. # if PNG_LIBPNG_VER < 10700
  629. /* To do: remove the following from libpng-1.7 */
  630. /* Original API that uses a private buffer in png_struct.
  631. * Deprecated because it causes png_struct to carry a spurious temporary
  632. * buffer (png_struct::time_buffer), better to have the caller pass this in.
  633. */
  634. png_const_charp PNGAPI
  635. png_convert_to_rfc1123(png_structrp png_ptr, png_const_timep ptime)
  636. {
  637. if (png_ptr != NULL)
  638. {
  639. /* The only failure above if png_ptr != NULL is from an invalid ptime */
  640. if (!png_convert_to_rfc1123_buffer(png_ptr->time_buffer, ptime))
  641. png_warning(png_ptr, "Ignoring invalid time value");
  642. else
  643. return png_ptr->time_buffer;
  644. }
  645. return NULL;
  646. }
  647. # endif
  648. # endif /* PNG_TIME_RFC1123_SUPPORTED */
  649. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  650. png_const_charp PNGAPI
  651. png_get_copyright(png_const_structrp png_ptr)
  652. {
  653. PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
  654. #ifdef PNG_STRING_COPYRIGHT
  655. return PNG_STRING_COPYRIGHT
  656. #else
  657. # ifdef __STDC__
  658. return PNG_STRING_NEWLINE \
  659. "libpng version 1.6.1 - March 28, 2013" PNG_STRING_NEWLINE \
  660. "Copyright (c) 1998-2013 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
  661. "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
  662. "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
  663. PNG_STRING_NEWLINE;
  664. # else
  665. return "libpng version 1.6.1 - March 28, 2013\
  666. Copyright (c) 1998-2013 Glenn Randers-Pehrson\
  667. Copyright (c) 1996-1997 Andreas Dilger\
  668. Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.";
  669. # endif
  670. #endif
  671. }
  672. /* The following return the library version as a short string in the
  673. * format 1.0.0 through 99.99.99zz. To get the version of *.h files
  674. * used with your application, print out PNG_LIBPNG_VER_STRING, which
  675. * is defined in png.h.
  676. * Note: now there is no difference between png_get_libpng_ver() and
  677. * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
  678. * it is guaranteed that png.c uses the correct version of png.h.
  679. */
  680. png_const_charp PNGAPI
  681. png_get_libpng_ver(png_const_structrp png_ptr)
  682. {
  683. /* Version of *.c files used when building libpng */
  684. return png_get_header_ver(png_ptr);
  685. }
  686. png_const_charp PNGAPI
  687. png_get_header_ver(png_const_structrp png_ptr)
  688. {
  689. /* Version of *.h files used when building libpng */
  690. PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
  691. return PNG_LIBPNG_VER_STRING;
  692. }
  693. png_const_charp PNGAPI
  694. png_get_header_version(png_const_structrp png_ptr)
  695. {
  696. /* Returns longer string containing both version and date */
  697. PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
  698. #ifdef __STDC__
  699. return PNG_HEADER_VERSION_STRING
  700. # ifndef PNG_READ_SUPPORTED
  701. " (NO READ SUPPORT)"
  702. # endif
  703. PNG_STRING_NEWLINE;
  704. #else
  705. return PNG_HEADER_VERSION_STRING;
  706. #endif
  707. }
  708. #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  709. int PNGAPI
  710. png_handle_as_unknown(png_const_structrp png_ptr, png_const_bytep chunk_name)
  711. {
  712. /* Check chunk_name and return "keep" value if it's on the list, else 0 */
  713. png_const_bytep p, p_end;
  714. if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list == 0)
  715. return PNG_HANDLE_CHUNK_AS_DEFAULT;
  716. p_end = png_ptr->chunk_list;
  717. p = p_end + png_ptr->num_chunk_list*5; /* beyond end */
  718. /* The code is the fifth byte after each four byte string. Historically this
  719. * code was always searched from the end of the list, this is no longer
  720. * necessary because the 'set' routine handles duplicate entries correcty.
  721. */
  722. do /* num_chunk_list > 0, so at least one */
  723. {
  724. p -= 5;
  725. if (!memcmp(chunk_name, p, 4))
  726. return p[4];
  727. }
  728. while (p > p_end);
  729. /* This means that known chunks should be processed and unknown chunks should
  730. * be handled according to the value of png_ptr->unknown_default; this can be
  731. * confusing because, as a result, there are two levels of defaulting for
  732. * unknown chunks.
  733. */
  734. return PNG_HANDLE_CHUNK_AS_DEFAULT;
  735. }
  736. #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  737. int /* PRIVATE */
  738. png_chunk_unknown_handling(png_const_structrp png_ptr, png_uint_32 chunk_name)
  739. {
  740. png_byte chunk_string[5];
  741. PNG_CSTRING_FROM_CHUNK(chunk_string, chunk_name);
  742. return png_handle_as_unknown(png_ptr, chunk_string);
  743. }
  744. #endif /* READ_UNKNOWN_CHUNKS */
  745. #endif /* SET_UNKNOWN_CHUNKS */
  746. #ifdef PNG_READ_SUPPORTED
  747. /* This function, added to libpng-1.0.6g, is untested. */
  748. int PNGAPI
  749. png_reset_zstream(png_structrp png_ptr)
  750. {
  751. if (png_ptr == NULL)
  752. return Z_STREAM_ERROR;
  753. /* WARNING: this resets the window bits to the maximum! */
  754. return (inflateReset(&png_ptr->zstream));
  755. }
  756. #endif /* PNG_READ_SUPPORTED */
  757. /* This function was added to libpng-1.0.7 */
  758. png_uint_32 PNGAPI
  759. png_access_version_number(void)
  760. {
  761. /* Version of *.c files used when building libpng */
  762. return((png_uint_32)PNG_LIBPNG_VER);
  763. }
  764. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  765. /* Ensure that png_ptr->zstream.msg holds some appropriate error message string.
  766. * If it doesn't 'ret' is used to set it to something appropriate, even in cases
  767. * like Z_OK or Z_STREAM_END where the error code is apparently a success code.
  768. */
  769. void /* PRIVATE */
  770. png_zstream_error(png_structrp png_ptr, int ret)
  771. {
  772. /* Translate 'ret' into an appropriate error string, priority is given to the
  773. * one in zstream if set. This always returns a string, even in cases like
  774. * Z_OK or Z_STREAM_END where the error code is a success code.
  775. */
  776. if (png_ptr->zstream.msg == NULL) switch (ret)
  777. {
  778. default:
  779. case Z_OK:
  780. png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected zlib return code");
  781. break;
  782. case Z_STREAM_END:
  783. /* Normal exit */
  784. png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected end of LZ stream");
  785. break;
  786. case Z_NEED_DICT:
  787. /* This means the deflate stream did not have a dictionary; this
  788. * indicates a bogus PNG.
  789. */
  790. png_ptr->zstream.msg = PNGZ_MSG_CAST("missing LZ dictionary");
  791. break;
  792. case Z_ERRNO:
  793. /* gz APIs only: should not happen */
  794. png_ptr->zstream.msg = PNGZ_MSG_CAST("zlib IO error");
  795. break;
  796. case Z_STREAM_ERROR:
  797. /* internal libpng error */
  798. png_ptr->zstream.msg = PNGZ_MSG_CAST("bad parameters to zlib");
  799. break;
  800. case Z_DATA_ERROR:
  801. png_ptr->zstream.msg = PNGZ_MSG_CAST("damaged LZ stream");
  802. break;
  803. case Z_MEM_ERROR:
  804. png_ptr->zstream.msg = PNGZ_MSG_CAST("insufficient memory");
  805. break;
  806. case Z_BUF_ERROR:
  807. /* End of input or output; not a problem if the caller is doing
  808. * incremental read or write.
  809. */
  810. png_ptr->zstream.msg = PNGZ_MSG_CAST("truncated");
  811. break;
  812. case Z_VERSION_ERROR:
  813. png_ptr->zstream.msg = PNGZ_MSG_CAST("unsupported zlib version");
  814. break;
  815. case PNG_UNEXPECTED_ZLIB_RETURN:
  816. /* Compile errors here mean that zlib now uses the value co-opted in
  817. * pngpriv.h for PNG_UNEXPECTED_ZLIB_RETURN; update the switch above
  818. * and change pngpriv.h. Note that this message is "... return",
  819. * whereas the default/Z_OK one is "... return code".
  820. */
  821. png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected zlib return");
  822. break;
  823. }
  824. }
  825. /* png_convert_size: a PNGAPI but no longer in png.h, so deleted
  826. * at libpng 1.5.5!
  827. */
  828. /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
  829. #ifdef PNG_GAMMA_SUPPORTED /* always set if COLORSPACE */
  830. static int
  831. png_colorspace_check_gamma(png_const_structrp png_ptr,
  832. png_colorspacerp colorspace, png_fixed_point gAMA, int from)
  833. /* This is called to check a new gamma value against an existing one. The
  834. * routine returns false if the new gamma value should not be written.
  835. *
  836. * 'from' says where the new gamma value comes from:
  837. *
  838. * 0: the new gamma value is the libpng estimate for an ICC profile
  839. * 1: the new gamma value comes from a gAMA chunk
  840. * 2: the new gamma value comes from an sRGB chunk
  841. */
  842. {
  843. png_fixed_point gtest;
  844. if ((colorspace->flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 &&
  845. (!png_muldiv(&gtest, colorspace->gamma, PNG_FP_1, gAMA) ||
  846. png_gamma_significant(gtest)))
  847. {
  848. /* Either this is an sRGB image, in which case the calculated gamma
  849. * approximation should match, or this is an image with a profile and the
  850. * value libpng calculates for the gamma of the profile does not match the
  851. * value recorded in the file. The former, sRGB, case is an error, the
  852. * latter is just a warning.
  853. */
  854. if ((colorspace->flags & PNG_COLORSPACE_FROM_sRGB) != 0 || from == 2)
  855. {
  856. png_chunk_report(png_ptr, "gamma value does not match sRGB",
  857. PNG_CHUNK_ERROR);
  858. /* Do not overwrite an sRGB value */
  859. return from == 2;
  860. }
  861. else /* sRGB tag not involved */
  862. {
  863. png_chunk_report(png_ptr, "gamma value does not match libpng estimate",
  864. PNG_CHUNK_WARNING);
  865. return from == 1;
  866. }
  867. }
  868. return 1;
  869. }
  870. void /* PRIVATE */
  871. png_colorspace_set_gamma(png_const_structrp png_ptr,
  872. png_colorspacerp colorspace, png_fixed_point gAMA)
  873. {
  874. /* Changed in libpng-1.5.4 to limit the values to ensure overflow can't
  875. * occur. Since the fixed point representation is assymetrical it is
  876. * possible for 1/gamma to overflow the limit of 21474 and this means the
  877. * gamma value must be at least 5/100000 and hence at most 20000.0. For
  878. * safety the limits here are a little narrower. The values are 0.00016 to
  879. * 6250.0, which are truly ridiculous gamma values (and will produce
  880. * displays that are all black or all white.)
  881. *
  882. * In 1.6.0 this test replaces the ones in pngrutil.c, in the gAMA chunk
  883. * handling code, which only required the value to be >0.
  884. */
  885. png_const_charp errmsg;
  886. if (gAMA < 16 || gAMA > 625000000)
  887. errmsg = "gamma value out of range";
  888. # ifdef PNG_READ_gAMA_SUPPORTED
  889. /* Allow the application to set the gamma value more than once */
  890. else if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
  891. (colorspace->flags & PNG_COLORSPACE_FROM_gAMA) != 0)
  892. errmsg = "duplicate";
  893. # endif
  894. /* Do nothing if the colorspace is already invalid */
  895. else if (colorspace->flags & PNG_COLORSPACE_INVALID)
  896. return;
  897. else
  898. {
  899. if (png_colorspace_check_gamma(png_ptr, colorspace, gAMA, 1/*from gAMA*/))
  900. {
  901. /* Store this gamma value. */
  902. colorspace->gamma = gAMA;
  903. colorspace->flags |=
  904. (PNG_COLORSPACE_HAVE_GAMMA | PNG_COLORSPACE_FROM_gAMA);
  905. }
  906. /* At present if the check_gamma test fails the gamma of the colorspace is
  907. * not updated however the colorspace is not invalidated. This
  908. * corresponds to the case where the existing gamma comes from an sRGB
  909. * chunk or profile. An error message has already been output.
  910. */
  911. return;
  912. }
  913. /* Error exit - errmsg has been set. */
  914. colorspace->flags |= PNG_COLORSPACE_INVALID;
  915. png_chunk_report(png_ptr, errmsg, PNG_CHUNK_WRITE_ERROR);
  916. }
  917. void /* PRIVATE */
  918. png_colorspace_sync_info(png_const_structrp png_ptr, png_inforp info_ptr)
  919. {
  920. if (info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID)
  921. {
  922. /* Everything is invalid */
  923. info_ptr->valid &= ~(PNG_INFO_gAMA|PNG_INFO_cHRM|PNG_INFO_sRGB|
  924. PNG_INFO_iCCP);
  925. # ifdef PNG_COLORSPACE_SUPPORTED
  926. /* Clean up the iCCP profile now if it won't be used. */
  927. png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, -1/*not used*/);
  928. # else
  929. PNG_UNUSED(png_ptr)
  930. # endif
  931. }
  932. else
  933. {
  934. # ifdef PNG_COLORSPACE_SUPPORTED
  935. /* Leave the INFO_iCCP flag set if the pngset.c code has already set
  936. * it; this allows a PNG to contain a profile which matches sRGB and
  937. * yet still have that profile retrievable by the application.
  938. */
  939. if (info_ptr->colorspace.flags & PNG_COLORSPACE_MATCHES_sRGB)
  940. info_ptr->valid |= PNG_INFO_sRGB;
  941. else
  942. info_ptr->valid &= ~PNG_INFO_sRGB;
  943. if (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS)
  944. info_ptr->valid |= PNG_INFO_cHRM;
  945. else
  946. info_ptr->valid &= ~PNG_INFO_cHRM;
  947. # endif
  948. if (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA)
  949. info_ptr->valid |= PNG_INFO_gAMA;
  950. else
  951. info_ptr->valid &= ~PNG_INFO_gAMA;
  952. }
  953. }
  954. #ifdef PNG_READ_SUPPORTED
  955. void /* PRIVATE */
  956. png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr)
  957. {
  958. if (info_ptr == NULL) /* reduce code size; check here not in the caller */
  959. return;
  960. info_ptr->colorspace = png_ptr->colorspace;
  961. png_colorspace_sync_info(png_ptr, info_ptr);
  962. }
  963. #endif
  964. #endif
  965. #ifdef PNG_COLORSPACE_SUPPORTED
  966. /* Added at libpng-1.5.5 to support read and write of true CIEXYZ values for
  967. * cHRM, as opposed to using chromaticities. These internal APIs return
  968. * non-zero on a parameter error. The X, Y and Z values are required to be
  969. * positive and less than 1.0.
  970. */
  971. static int
  972. png_xy_from_XYZ(png_xy *xy, const png_XYZ *XYZ)
  973. {
  974. png_int_32 d, dwhite, whiteX, whiteY;
  975. d = XYZ->red_X + XYZ->red_Y + XYZ->red_Z;
  976. if (!png_muldiv(&xy->redx, XYZ->red_X, PNG_FP_1, d)) return 1;
  977. if (!png_muldiv(&xy->redy, XYZ->red_Y, PNG_FP_1, d)) return 1;
  978. dwhite = d;
  979. whiteX = XYZ->red_X;
  980. whiteY = XYZ->red_Y;
  981. d = XYZ->green_X + XYZ->green_Y + XYZ->green_Z;
  982. if (!png_muldiv(&xy->greenx, XYZ->green_X, PNG_FP_1, d)) return 1;
  983. if (!png_muldiv(&xy->greeny, XYZ->green_Y, PNG_FP_1, d)) return 1;
  984. dwhite += d;
  985. whiteX += XYZ->green_X;
  986. whiteY += XYZ->green_Y;
  987. d = XYZ->blue_X + XYZ->blue_Y + XYZ->blue_Z;
  988. if (!png_muldiv(&xy->bluex, XYZ->blue_X, PNG_FP_1, d)) return 1;
  989. if (!png_muldiv(&xy->bluey, XYZ->blue_Y, PNG_FP_1, d)) return 1;
  990. dwhite += d;
  991. whiteX += XYZ->blue_X;
  992. whiteY += XYZ->blue_Y;
  993. /* The reference white is simply the sum of the end-point (X,Y,Z) vectors,
  994. * thus:
  995. */
  996. if (!png_muldiv(&xy->whitex, whiteX, PNG_FP_1, dwhite)) return 1;
  997. if (!png_muldiv(&xy->whitey, whiteY, PNG_FP_1, dwhite)) return 1;
  998. return 0;
  999. }
  1000. static int
  1001. png_XYZ_from_xy(png_XYZ *XYZ, const png_xy *xy)
  1002. {
  1003. png_fixed_point red_inverse, green_inverse, blue_scale;
  1004. png_fixed_point left, right, denominator;
  1005. /* Check xy and, implicitly, z. Note that wide gamut color spaces typically
  1006. * have end points with 0 tristimulus values (these are impossible end
  1007. * points, but they are used to cover the possible colors.)
  1008. */
  1009. if (xy->redx < 0 || xy->redx > PNG_FP_1) return 1;
  1010. if (xy->redy < 0 || xy->redy > PNG_FP_1-xy->redx) return 1;
  1011. if (xy->greenx < 0 || xy->greenx > PNG_FP_1) return 1;
  1012. if (xy->greeny < 0 || xy->greeny > PNG_FP_1-xy->greenx) return 1;
  1013. if (xy->bluex < 0 || xy->bluex > PNG_FP_1) return 1;
  1014. if (xy->bluey < 0 || xy->bluey > PNG_FP_1-xy->bluex) return 1;
  1015. if (xy->whitex < 0 || xy->whitex > PNG_FP_1) return 1;
  1016. if (xy->whitey < 0 || xy->whitey > PNG_FP_1-xy->whitex) return 1;
  1017. /* The reverse calculation is more difficult because the original tristimulus
  1018. * value had 9 independent values (red,green,blue)x(X,Y,Z) however only 8
  1019. * derived values were recorded in the cHRM chunk;
  1020. * (red,green,blue,white)x(x,y). This loses one degree of freedom and
  1021. * therefore an arbitrary ninth value has to be introduced to undo the
  1022. * original transformations.
  1023. *
  1024. * Think of the original end-points as points in (X,Y,Z) space. The
  1025. * chromaticity values (c) have the property:
  1026. *
  1027. * C
  1028. * c = ---------
  1029. * X + Y + Z
  1030. *
  1031. * For each c (x,y,z) from the corresponding original C (X,Y,Z). Thus the
  1032. * three chromaticity values (x,y,z) for each end-point obey the
  1033. * relationship:
  1034. *
  1035. * x + y + z = 1
  1036. *
  1037. * This describes the plane in (X,Y,Z) space that intersects each axis at the
  1038. * value 1.0; call this the chromaticity plane. Thus the chromaticity
  1039. * calculation has scaled each end-point so that it is on the x+y+z=1 plane
  1040. * and chromaticity is the intersection of the vector from the origin to the
  1041. * (X,Y,Z) value with the chromaticity plane.
  1042. *
  1043. * To fully invert the chromaticity calculation we would need the three
  1044. * end-point scale factors, (red-scale, green-scale, blue-scale), but these
  1045. * were not recorded. Instead we calculated the reference white (X,Y,Z) and
  1046. * recorded the chromaticity of this. The reference white (X,Y,Z) would have
  1047. * given all three of the scale factors since:
  1048. *
  1049. * color-C = color-c * color-scale
  1050. * white-C = red-C + green-C + blue-C
  1051. * = red-c*red-scale + green-c*green-scale + blue-c*blue-scale
  1052. *
  1053. * But cHRM records only white-x and white-y, so we have lost the white scale
  1054. * factor:
  1055. *
  1056. * white-C = white-c*white-scale
  1057. *
  1058. * To handle this the inverse transformation makes an arbitrary assumption
  1059. * about white-scale:
  1060. *
  1061. * Assume: white-Y = 1.0
  1062. * Hence: white-scale = 1/white-y
  1063. * Or: red-Y + green-Y + blue-Y = 1.0
  1064. *
  1065. * Notice the last statement of the assumption gives an equation in three of
  1066. * the nine values we want to calculate. 8 more equations come from the
  1067. * above routine as summarised at the top above (the chromaticity
  1068. * calculation):
  1069. *
  1070. * Given: color-x = color-X / (color-X + color-Y + color-Z)
  1071. * Hence: (color-x - 1)*color-X + color.x*color-Y + color.x*color-Z = 0
  1072. *
  1073. * This is 9 simultaneous equations in the 9 variables "color-C" and can be
  1074. * solved by Cramer's rule. Cramer's rule requires calculating 10 9x9 matrix
  1075. * determinants, however this is not as bad as it seems because only 28 of
  1076. * the total of 90 terms in the various matrices are non-zero. Nevertheless
  1077. * Cramer's rule is notoriously numerically unstable because the determinant
  1078. * calculation involves the difference of large, but similar, numbers. It is
  1079. * difficult to be sure that the calculation is stable for real world values
  1080. * and it is certain that it becomes unstable where the end points are close
  1081. * together.
  1082. *
  1083. * So this code uses the perhaps slightly less optimal but more
  1084. * understandable and totally obvious approach of calculating color-scale.
  1085. *
  1086. * This algorithm depends on the precision in white-scale and that is
  1087. * (1/white-y), so we can immediately see that as white-y approaches 0 the
  1088. * accuracy inherent in the cHRM chunk drops off substantially.
  1089. *
  1090. * libpng arithmetic: a simple invertion of the above equations
  1091. * ------------------------------------------------------------
  1092. *
  1093. * white_scale = 1/white-y
  1094. * white-X = white-x * white-scale
  1095. * white-Y = 1.0
  1096. * white-Z = (1 - white-x - white-y) * white_scale
  1097. *
  1098. * white-C = red-C + green-C + blue-C
  1099. * = red-c*red-scale + green-c*green-scale + blue-c*blue-scale
  1100. *
  1101. * This gives us three equations in (red-scale,green-scale,blue-scale) where
  1102. * all the coefficients are now known:
  1103. *
  1104. * red-x*red-scale + green-x*green-scale + blue-x*blue-scale
  1105. * = white-x/white-y
  1106. * red-y*red-scale + green-y*green-scale + blue-y*blue-scale = 1
  1107. * red-z*red-scale + green-z*green-scale + blue-z*blue-scale
  1108. * = (1 - white-x - white-y)/white-y
  1109. *
  1110. * In the last equation color-z is (1 - color-x - color-y) so we can add all
  1111. * three equations together to get an alternative third:
  1112. *
  1113. * red-scale + green-scale + blue-scale = 1/white-y = white-scale
  1114. *
  1115. * So now we have a Cramer's rule solution where the determinants are just
  1116. * 3x3 - far more tractible. Unfortunately 3x3 determinants still involve
  1117. * multiplication of three coefficients so we can't guarantee to avoid
  1118. * overflow in the libpng fixed point representation. Using Cramer's rule in
  1119. * floating point is probably a good choice here, but it's not an option for
  1120. * fixed point. Instead proceed to simplify the first two equations by
  1121. * eliminating what is likely to be the largest value, blue-scale:
  1122. *
  1123. * blue-scale = white-scale - red-scale - green-scale
  1124. *
  1125. * Hence:
  1126. *
  1127. * (red-x - blue-x)*red-scale + (green-x - blue-x)*green-scale =
  1128. * (white-x - blue-x)*white-scale
  1129. *
  1130. * (red-y - blue-y)*red-scale + (green-y - blue-y)*green-scale =
  1131. * 1 - blue-y*white-scale
  1132. *
  1133. * And now we can trivially solve for (red-scale,green-scale):
  1134. *
  1135. * green-scale =
  1136. * (white-x - blue-x)*white-scale - (red-x - blue-x)*red-scale
  1137. * -----------------------------------------------------------
  1138. * green-x - blue-x
  1139. *
  1140. * red-scale =
  1141. * 1 - blue-y*white-scale - (green-y - blue-y) * green-scale
  1142. * ---------------------------------------------------------
  1143. * red-y - blue-y
  1144. *
  1145. * Hence:
  1146. *
  1147. * red-scale =
  1148. * ( (green-x - blue-x) * (white-y - blue-y) -
  1149. * (green-y - blue-y) * (white-x - blue-x) ) / white-y
  1150. * -------------------------------------------------------------------------
  1151. * (green-x - blue-x)*(red-y - blue-y)-(green-y - blue-y)*(red-x - blue-x)
  1152. *
  1153. * green-scale =
  1154. * ( (red-y - blue-y) * (white-x - blue-x) -
  1155. * (red-x - blue-x) * (white-y - blue-y) ) / white-y
  1156. * -------------------------------------------------------------------------
  1157. * (green-x - blue-x)*(red-y - blue-y)-(green-y - blue-y)*(red-x - blue-x)
  1158. *
  1159. * Accuracy:
  1160. * The input values have 5 decimal digits of accuracy. The values are all in
  1161. * the range 0 < value < 1, so simple products are in the same range but may
  1162. * need up to 10 decimal digits to preserve the original precision and avoid
  1163. * underflow. Because we are using a 32-bit signed representation we cannot
  1164. * match this; the best is a little over 9 decimal digits, less than 10.
  1165. *
  1166. * The approach used here is to preserve the maximum precision within the
  1167. * signed representation. Because the red-scale calculation above uses the
  1168. * difference between two products of values that must be in the range -1..+1
  1169. * it is sufficient to divide the product by 7; ceil(100,000/32767*2). The
  1170. * factor is irrelevant in the calculation because it is applied to both
  1171. * numerator and denominator.
  1172. *
  1173. * Note that the values of the differences of the products of the
  1174. * chromaticities in the above equations tend to be small, for example for
  1175. * the sRGB chromaticities they are:
  1176. *
  1177. * red numerator: -0.04751
  1178. * green numerator: -0.08788
  1179. * denominator: -0.2241 (without white-y multiplication)
  1180. *
  1181. * The resultant Y coefficients from the chromaticities of some widely used
  1182. * color space definitions are (to 15 decimal places):
  1183. *
  1184. * sRGB
  1185. * 0.212639005871510 0.715168678767756 0.072192315360734
  1186. * Kodak ProPhoto
  1187. * 0.288071128229293 0.711843217810102 0.000085653960605
  1188. * Adobe RGB
  1189. * 0.297344975250536 0.627363566255466 0.075291458493998
  1190. * Adobe Wide Gamut RGB
  1191. * 0.258728243040113 0.724682314948566 0.016589442011321
  1192. */
  1193. /* By the argument, above overflow should be impossible here. The return
  1194. * value of 2 indicates an internal error to the caller.
  1195. */
  1196. if (!png_muldiv(&left, xy->greenx-xy->bluex, xy->redy - xy->bluey, 7))
  1197. return 2;
  1198. if (!png_muldiv(&right, xy->greeny-xy->bluey, xy->redx - xy->bluex, 7))
  1199. return 2;
  1200. denominator = left - right;
  1201. /* Now find the red numerator. */
  1202. if (!png_muldiv(&left, xy->greenx-xy->bluex, xy->whitey-xy->bluey, 7))
  1203. return 2;
  1204. if (!png_muldiv(&right, xy->greeny-xy->bluey, xy->whitex-xy->bluex, 7))
  1205. return 2;
  1206. /* Overflow is possible here and it indicates an extreme set of PNG cHRM
  1207. * chunk values. This calculation actually returns the reciprocal of the
  1208. * scale value because this allows us to delay the multiplication of white-y
  1209. * into the denominator, which tends to produce a small number.
  1210. */
  1211. if (!png_muldiv(&red_inverse, xy->whitey, denominator, left-right) ||
  1212. red_inverse <= xy->whitey /* r+g+b scales = white scale */)
  1213. return 1;
  1214. /* Similarly for green_inverse: */
  1215. if (!png_muldiv(&left, xy->redy-xy->bluey, xy->whitex-xy->bluex, 7))
  1216. return 2;
  1217. if (!png_muldiv(&right, xy->redx-xy->bluex, xy->whitey-xy->bluey, 7))
  1218. return 2;
  1219. if (!png_muldiv(&green_inverse, xy->whitey, denominator, left-right) ||
  1220. green_inverse <= xy->whitey)
  1221. return 1;
  1222. /* And the blue scale, the checks above guarantee this can't overflow but it
  1223. * can still produce 0 for extreme cHRM values.
  1224. */
  1225. blue_scale = png_reciprocal(xy->whitey) - png_reciprocal(red_inverse) -
  1226. png_reciprocal(green_inverse);
  1227. if (blue_scale <= 0) return 1;
  1228. /* And fill in the png_XYZ: */
  1229. if (!png_muldiv(&XYZ->red_X, xy->redx, PNG_FP_1, red_inverse)) return 1;
  1230. if (!png_muldiv(&XYZ->red_Y, xy->redy, PNG_FP_1, red_inverse)) return 1;
  1231. if (!png_muldiv(&XYZ->red_Z, PNG_FP_1 - xy->redx - xy->redy, PNG_FP_1,
  1232. red_inverse))
  1233. return 1;
  1234. if (!png_muldiv(&XYZ->green_X, xy->greenx, PNG_FP_1, green_inverse))
  1235. return 1;
  1236. if (!png_muldiv(&XYZ->green_Y, xy->greeny, PNG_FP_1, green_inverse))
  1237. return 1;
  1238. if (!png_muldiv(&XYZ->green_Z, PNG_FP_1 - xy->greenx - xy->greeny, PNG_FP_1,
  1239. green_inverse))
  1240. return 1;
  1241. if (!png_muldiv(&XYZ->blue_X, xy->bluex, blue_scale, PNG_FP_1)) return 1;
  1242. if (!png_muldiv(&XYZ->blue_Y, xy->bluey, blue_scale, PNG_FP_1)) return 1;
  1243. if (!png_muldiv(&XYZ->blue_Z, PNG_FP_1 - xy->bluex - xy->bluey, blue_scale,
  1244. PNG_FP_1))
  1245. return 1;
  1246. return 0; /*success*/
  1247. }
  1248. static int
  1249. png_XYZ_normalize(png_XYZ *XYZ)
  1250. {
  1251. png_int_32 Y;
  1252. if (XYZ->red_Y < 0 || XYZ->green_Y < 0 || XYZ->blue_Y < 0 ||
  1253. XYZ->red_X < 0 || XYZ->green_X < 0 || XYZ->blue_X < 0 ||
  1254. XYZ->red_Z < 0 || XYZ->green_Z < 0 || XYZ->blue_Z < 0)
  1255. return 1;
  1256. /* Normalize by scaling so the sum of the end-point Y values is PNG_FP_1.
  1257. * IMPLEMENTATION NOTE: ANSI requires signed overflow not to occur, therefore
  1258. * relying on addition of two positive values producing a negative one is not
  1259. * safe.
  1260. */
  1261. Y = XYZ->red_Y;
  1262. if (0x7fffffff - Y < XYZ->green_X) return 1;
  1263. Y += XYZ->green_Y;
  1264. if (0x7fffffff - Y < XYZ->blue_X) return 1;
  1265. Y += XYZ->blue_Y;
  1266. if (Y != PNG_FP_1)
  1267. {
  1268. if (!png_muldiv(&XYZ->red_X, XYZ->red_X, PNG_FP_1, Y)) return 1;
  1269. if (!png_muldiv(&XYZ->red_Y, XYZ->red_Y, PNG_FP_1, Y)) return 1;
  1270. if (!png_muldiv(&XYZ->red_Z, XYZ->red_Z, PNG_FP_1, Y)) return 1;
  1271. if (!png_muldiv(&XYZ->green_X, XYZ->green_X, PNG_FP_1, Y)) return 1;
  1272. if (!png_muldiv(&XYZ->green_Y, XYZ->green_Y, PNG_FP_1, Y)) return 1;
  1273. if (!png_muldiv(&XYZ->green_Z, XYZ->green_Z, PNG_FP_1, Y)) return 1;
  1274. if (!png_muldiv(&XYZ->blue_X, XYZ->blue_X, PNG_FP_1, Y)) return 1;
  1275. if (!png_muldiv(&XYZ->blue_Y, XYZ->blue_Y, PNG_FP_1, Y)) return 1;
  1276. if (!png_muldiv(&XYZ->blue_Z, XYZ->blue_Z, PNG_FP_1, Y)) return 1;
  1277. }
  1278. return 0;
  1279. }
  1280. static int
  1281. png_colorspace_endpoints_match(const png_xy *xy1, const png_xy *xy2, int delta)
  1282. {
  1283. /* Allow an error of +/-0.01 (absolute value) on each chromaticity */
  1284. return !(PNG_OUT_OF_RANGE(xy1->whitex, xy2->whitex,delta) ||
  1285. PNG_OUT_OF_RANGE(xy1->whitey, xy2->whitey,delta) ||
  1286. PNG_OUT_OF_RANGE(xy1->redx, xy2->redx, delta) ||
  1287. PNG_OUT_OF_RANGE(xy1->redy, xy2->redy, delta) ||
  1288. PNG_OUT_OF_RANGE(xy1->greenx, xy2->greenx,delta) ||
  1289. PNG_OUT_OF_RANGE(xy1->greeny, xy2->greeny,delta) ||
  1290. PNG_OUT_OF_RANGE(xy1->bluex, xy2->bluex, delta) ||
  1291. PNG_OUT_OF_RANGE(xy1->bluey, xy2->bluey, delta));
  1292. }
  1293. /* Added in libpng-1.6.0, a different check for the validity of a set of cHRM
  1294. * chunk chromaticities. Earlier checks used to simply look for the overflow
  1295. * condition (where the determinant of the matrix to solve for XYZ ends up zero
  1296. * because the chromaticity values are not all distinct.) Despite this it is
  1297. * theoretically possible to produce chromaticities that are apparently valid
  1298. * but that rapidly degrade to invalid, potentially crashing, sets because of
  1299. * arithmetic inaccuracies when calculations are performed on them. The new
  1300. * check is to round-trip xy -> XYZ -> xy and then check that the result is
  1301. * within a small percentage of the original.
  1302. */
  1303. static int
  1304. png_colorspace_check_xy(png_XYZ *XYZ, const png_xy *xy)
  1305. {
  1306. int result;
  1307. png_xy xy_test;
  1308. /* As a side-effect this routine also returns the XYZ endpoints. */
  1309. result = png_XYZ_from_xy(XYZ, xy);
  1310. if (result) return result;
  1311. result = png_xy_from_XYZ(&xy_test, XYZ);
  1312. if (result) return result;
  1313. if (png_colorspace_endpoints_match(xy, &xy_test,
  1314. 5/*actually, the math is pretty accurate*/))
  1315. return 0;
  1316. /* Too much slip */
  1317. return 1;
  1318. }
  1319. /* This is the check going the other way. The XYZ is modified to normalize it
  1320. * (another side-effect) and the xy chromaticities are returned.
  1321. */
  1322. static int
  1323. png_colorspace_check_XYZ(png_xy *xy, png_XYZ *XYZ)
  1324. {
  1325. int result;
  1326. png_XYZ XYZtemp;
  1327. result = png_XYZ_normalize(XYZ);
  1328. if (result) return result;
  1329. result = png_xy_from_XYZ(xy, XYZ);
  1330. if (result) return result;
  1331. XYZtemp = *XYZ;
  1332. return png_colorspace_check_xy(&XYZtemp, xy);
  1333. }
  1334. /* Used to check for an endpoint match against sRGB */
  1335. static const png_xy sRGB_xy = /* From ITU-R BT.709-3 */
  1336. {
  1337. /* color x y */
  1338. /* red */ 64000, 33000,
  1339. /* green */ 30000, 60000,
  1340. /* blue */ 15000, 6000,
  1341. /* white */ 31270, 32900
  1342. };
  1343. static int
  1344. png_colorspace_set_xy_and_XYZ(png_const_structrp png_ptr,
  1345. png_colorspacerp colorspace, const png_xy *xy, const png_XYZ *XYZ,
  1346. int preferred)
  1347. {
  1348. if (colorspace->flags & PNG_COLORSPACE_INVALID)
  1349. return 0;
  1350. /* The consistency check is performed on the chromaticities; this factors out
  1351. * variations because of the normalization (or not) of the end point Y
  1352. * values.
  1353. */
  1354. if (preferred < 2 && (colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS))
  1355. {
  1356. /* The end points must be reasonably close to any we already have. The
  1357. * following allows an error of up to +/-.001
  1358. */
  1359. if (!png_colorspace_endpoints_match(xy, &colorspace->end_points_xy, 100))
  1360. {
  1361. colorspace->flags |= PNG_COLORSPACE_INVALID;
  1362. png_benign_error(png_ptr, "inconsistent chromaticities");
  1363. return 0; /* failed */
  1364. }
  1365. /* Only overwrite with preferred values */
  1366. if (!preferred)
  1367. return 1; /* ok, but no change */
  1368. }
  1369. colorspace->end_points_xy = *xy;
  1370. colorspace->end_points_XYZ = *XYZ;
  1371. colorspace->flags |= PNG_COLORSPACE_HAVE_ENDPOINTS;
  1372. /* The end points are normally quoted to two decimal digits, so allow +/-0.01
  1373. * on this test.
  1374. */
  1375. if (png_colorspace_endpoints_match(xy, &sRGB_xy, 1000))
  1376. colorspace->flags |= PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB;
  1377. else
  1378. colorspace->flags &= PNG_COLORSPACE_CANCEL(
  1379. PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB);
  1380. return 2; /* ok and changed */
  1381. }
  1382. int /* PRIVATE */
  1383. png_colorspace_set_chromaticities(png_const_structrp png_ptr,
  1384. png_colorspacerp colorspace, const png_xy *xy, int preferred)
  1385. {
  1386. /* We must check the end points to ensure they are reasonable - in the past
  1387. * color management systems have crashed as a result of getting bogus
  1388. * colorant values, while this isn't the fault of libpng it is the
  1389. * responsibility of libpng because PNG carries the bomb and libpng is in a
  1390. * position to protect against it.
  1391. */
  1392. png_XYZ XYZ;
  1393. switch (png_colorspace_check_xy(&XYZ, xy))
  1394. {
  1395. case 0: /* success */
  1396. return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, xy, &XYZ,
  1397. preferred);
  1398. case 1:
  1399. /* We can't invert the chromaticities so we can't produce value XYZ
  1400. * values. Likely as not a color management system will fail too.
  1401. */
  1402. colorspace->flags |= PNG_COLORSPACE_INVALID;
  1403. png_benign_error(png_ptr, "invalid chromaticities");
  1404. break;
  1405. default:
  1406. /* libpng is broken; this should be a warning but if it happens we
  1407. * want error reports so for the moment it is an error.
  1408. */
  1409. colorspace->flags |= PNG_COLORSPACE_INVALID;
  1410. png_error(png_ptr, "internal error checking chromaticities");
  1411. break;
  1412. }
  1413. return 0; /* failed */
  1414. }
  1415. int /* PRIVATE */
  1416. png_colorspace_set_endpoints(png_const_structrp png_ptr,
  1417. png_colorspacerp colorspace, const png_XYZ *XYZ_in, int preferred)
  1418. {
  1419. png_XYZ XYZ = *XYZ_in;
  1420. png_xy xy;
  1421. switch (png_colorspace_check_XYZ(&xy, &XYZ))
  1422. {
  1423. case 0:
  1424. return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, &xy, &XYZ,
  1425. preferred);
  1426. case 1:
  1427. /* End points are invalid. */
  1428. colorspace->flags |= PNG_COLORSPACE_INVALID;
  1429. png_benign_error(png_ptr, "invalid end points");
  1430. break;
  1431. default:
  1432. colorspace->flags |= PNG_COLORSPACE_INVALID;
  1433. png_error(png_ptr, "internal error checking chromaticities");
  1434. break;
  1435. }
  1436. return 0; /* failed */
  1437. }
  1438. #if defined(PNG_sRGB_SUPPORTED) || defined(PNG_iCCP_SUPPORTED)
  1439. /* Error message generation */
  1440. static char
  1441. png_icc_tag_char(png_uint_32 byte)
  1442. {
  1443. byte &= 0xff;
  1444. if (byte >= 32 && byte <= 126)
  1445. return (char)byte;
  1446. else
  1447. return '?';
  1448. }
  1449. static void
  1450. png_icc_tag_name(char *name, png_uint_32 tag)
  1451. {
  1452. name[0] = '\'';
  1453. name[1] = png_icc_tag_char(tag >> 24);
  1454. name[2] = png_icc_tag_char(tag >> 16);
  1455. name[3] = png_icc_tag_char(tag >> 8);
  1456. name[4] = png_icc_tag_char(tag );
  1457. name[5] = '\'';
  1458. }
  1459. static int
  1460. is_ICC_signature_char(png_alloc_size_t it)
  1461. {
  1462. return it == 32 || (it >= 48 && it <= 57) || (it >= 65 && it <= 90) ||
  1463. (it >= 97 && it <= 122);
  1464. }
  1465. static int is_ICC_signature(png_alloc_size_t it)
  1466. {
  1467. return is_ICC_signature_char(it >> 24) /* checks all the top bits */ &&
  1468. is_ICC_signature_char((it >> 16) & 0xff) &&
  1469. is_ICC_signature_char((it >> 8) & 0xff) &&
  1470. is_ICC_signature_char(it & 0xff);
  1471. }
  1472. static int
  1473. png_icc_profile_error(png_const_structrp png_ptr, png_colorspacerp colorspace,
  1474. png_const_charp name, png_alloc_size_t value, png_const_charp reason)
  1475. {
  1476. size_t pos;
  1477. char message[196]; /* see below for calculation */
  1478. if (colorspace != NULL)
  1479. colorspace->flags |= PNG_COLORSPACE_INVALID;
  1480. pos = png_safecat(message, (sizeof message), 0, "profile '"); /* 9 chars */
  1481. pos = png_safecat(message, pos+79, pos, name); /* Truncate to 79 chars */
  1482. pos = png_safecat(message, (sizeof message), pos, "': "); /* +2 = 90 */
  1483. if (is_ICC_signature(value))
  1484. {
  1485. /* So 'value' is at most 4 bytes and the following cast is safe */
  1486. png_icc_tag_name(message+pos, (png_uint_32)value);
  1487. pos += 6; /* total +8; less than the else clause */
  1488. message[pos++] = ':';
  1489. message[pos++] = ' ';
  1490. }
  1491. # ifdef PNG_WARNINGS_SUPPORTED
  1492. else
  1493. {
  1494. char number[PNG_NUMBER_BUFFER_SIZE]; /* +24 = 114*/
  1495. pos = png_safecat(message, (sizeof message), pos,
  1496. png_format_number(number, number+(sizeof number),
  1497. PNG_NUMBER_FORMAT_x, value));
  1498. pos = png_safecat(message, (sizeof message), pos, "h: "); /*+2 = 116*/
  1499. }
  1500. # endif
  1501. /* The 'reason' is an arbitrary message, allow +79 maximum 195 */
  1502. png_safecat(message, (sizeof message), pos, reason);
  1503. /* This is recoverable, but make it unconditionally an app_error on write to
  1504. * avoid writing invalid ICC profiles into PNG files. (I.e. we handle them
  1505. * on read, with a warning, but on write unless the app turns off
  1506. * application errors the PNG won't be written.)
  1507. */
  1508. png_chunk_report(png_ptr, message,
  1509. (colorspace != NULL) ? PNG_CHUNK_ERROR : PNG_CHUNK_WRITE_ERROR);
  1510. return 0;
  1511. }
  1512. #endif /* sRGB || iCCP */
  1513. #ifdef PNG_sRGB_SUPPORTED
  1514. int /* PRIVATE */
  1515. png_colorspace_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace,
  1516. int intent)
  1517. {
  1518. /* sRGB sets known gamma, end points and (from the chunk) intent. */
  1519. /* IMPORTANT: these are not necessarily the values found in an ICC profile
  1520. * because ICC profiles store values adapted to a D50 environment; it is
  1521. * expected that the ICC profile mediaWhitePointTag will be D50, see the
  1522. * checks and code elsewhere to understand this better.
  1523. *
  1524. * These XYZ values, which are accurate to 5dp, produce rgb to gray
  1525. * coefficients of (6968,23435,2366), which are reduced (because they add up
  1526. * to 32769 not 32768) to (6968,23434,2366). These are the values that
  1527. * libpng has traditionally used (and are the best values given the 15bit
  1528. * algorithm used by the rgb to gray code.)
  1529. */
  1530. static const png_XYZ sRGB_XYZ = /* D65 XYZ (*not* the D50 adapted values!) */
  1531. {
  1532. /* color X Y Z */
  1533. /* red */ 41239, 21264, 1933,
  1534. /* green */ 35758, 71517, 11919,
  1535. /* blue */ 18048, 7219, 95053
  1536. };
  1537. /* Do nothing if the colorspace is already invalidated. */
  1538. if (colorspace->flags & PNG_COLORSPACE_INVALID)
  1539. return 0;
  1540. /* Check the intent, then check for existing settings. It is valid for the
  1541. * PNG file to have cHRM or gAMA chunks along with sRGB, but the values must
  1542. * be consistent with the correct values. If, however, this function is
  1543. * called below because an iCCP chunk matches sRGB then it is quite
  1544. * conceivable that an older app recorded incorrect gAMA and cHRM because of
  1545. * an incorrect calculation based on the values in the profile - this does
  1546. * *not* invalidate the profile (though it still produces an error, which can
  1547. * be ignored.)
  1548. */
  1549. if (intent < 0 || intent >= PNG_sRGB_INTENT_LAST)
  1550. return png_icc_profile_error(png_ptr, colorspace, "sRGB",
  1551. (unsigned)intent, "invalid sRGB rendering intent");
  1552. if ((colorspace->flags & PNG_COLORSPACE_HAVE_INTENT) != 0 &&
  1553. colorspace->rendering_intent != intent)
  1554. return png_icc_profile_error(png_ptr, colorspace, "sRGB",
  1555. (unsigned)intent, "inconsistent rendering intents");
  1556. if ((colorspace->flags & PNG_COLORSPACE_FROM_sRGB) != 0)
  1557. {
  1558. png_benign_error(png_ptr, "duplicate sRGB information ignored");
  1559. return 0;
  1560. }
  1561. /* If the standard sRGB cHRM chunk does not match the one from the PNG file
  1562. * warn but overwrite the value with the correct one.
  1563. */
  1564. if ((colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0 &&
  1565. !png_colorspace_endpoints_match(&sRGB_xy, &colorspace->end_points_xy,
  1566. 100))
  1567. png_chunk_report(png_ptr, "cHRM chunk does not match sRGB",
  1568. PNG_CHUNK_ERROR);
  1569. /* This check is just done for the error reporting - the routine always
  1570. * returns true when the 'from' argument corresponds to sRGB (2).
  1571. */
  1572. (void)png_colorspace_check_gamma(png_ptr, colorspace, PNG_GAMMA_sRGB_INVERSE,
  1573. 2/*from sRGB*/);
  1574. /* intent: bugs in GCC force 'int' to be used as the parameter type. */
  1575. colorspace->rendering_intent = (png_uint_16)intent;
  1576. colorspace->flags |= PNG_COLORSPACE_HAVE_INTENT;
  1577. /* endpoints */
  1578. colorspace->end_points_xy = sRGB_xy;
  1579. colorspace->end_points_XYZ = sRGB_XYZ;
  1580. colorspace->flags |=
  1581. (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB);
  1582. /* gamma */
  1583. colorspace->gamma = PNG_GAMMA_sRGB_INVERSE;
  1584. colorspace->flags |= PNG_COLORSPACE_HAVE_GAMMA;
  1585. /* Finally record that we have an sRGB profile */
  1586. colorspace->flags |=
  1587. (PNG_COLORSPACE_MATCHES_sRGB|PNG_COLORSPACE_FROM_sRGB);
  1588. return 1; /* set */
  1589. }
  1590. #endif /* sRGB */
  1591. #ifdef PNG_iCCP_SUPPORTED
  1592. /* Encoded value of D50 as an ICC XYZNumber. From the ICC 2010 spec the value
  1593. * is XYZ(0.9642,1.0,0.8249), which scales to:
  1594. *
  1595. * (63189.8112, 65536, 54060.6464)
  1596. */
  1597. static const png_byte D50_nCIEXYZ[12] =
  1598. { 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d };
  1599. int /* PRIVATE */
  1600. png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace,
  1601. png_const_charp name, png_uint_32 profile_length)
  1602. {
  1603. if (profile_length < 132)
  1604. return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
  1605. "too short");
  1606. if (profile_length & 3)
  1607. return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
  1608. "invalid length");
  1609. return 1;
  1610. }
  1611. int /* PRIVATE */
  1612. png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace,
  1613. png_const_charp name, png_uint_32 profile_length,
  1614. png_const_bytep profile/* first 132 bytes only */, int color_type)
  1615. {
  1616. png_uint_32 temp;
  1617. /* Length check; this cannot be ignored in this code because profile_length
  1618. * is used later to check the tag table, so even if the profile seems over
  1619. * long profile_length from the caller must be correct. The caller can fix
  1620. * this up on read or write by just passing in the profile header length.
  1621. */
  1622. temp = png_get_uint_32(profile);
  1623. if (temp != profile_length)
  1624. return png_icc_profile_error(png_ptr, colorspace, name, temp,
  1625. "length does not match profile");
  1626. temp = png_get_uint_32(profile+128); /* tag count: 12 bytes/tag */
  1627. if (temp > 357913930 || /* (2^32-4-132)/12: maxium possible tag count */
  1628. profile_length < 132+12*temp) /* truncated tag table */
  1629. return png_icc_profile_error(png_ptr, colorspace, name, temp,
  1630. "tag count too large");
  1631. /* The 'intent' must be valid or we can't store it, ICC limits the intent to
  1632. * 16 bits.
  1633. */
  1634. temp = png_get_uint_32(profile+64);
  1635. if (temp >= 0xffff) /* The ICC limit */
  1636. return png_icc_profile_error(png_ptr, colorspace, name, temp,
  1637. "invalid rendering intent");
  1638. /* This is just a warning because the profile may be valid in future
  1639. * versions.
  1640. */
  1641. if (temp >= PNG_sRGB_INTENT_LAST)
  1642. (void)png_icc_profile_error(png_ptr, NULL, name, temp,
  1643. "intent outside defined range");
  1644. /* At this point the tag table can't be checked because it hasn't necessarily
  1645. * been loaded; however, various header fields can be checked. These checks
  1646. * are for values permitted by the PNG spec in an ICC profile; the PNG spec
  1647. * restricts the profiles that can be passed in an iCCP chunk (they must be
  1648. * appropriate to processing PNG data!)
  1649. */
  1650. /* Data checks (could be skipped). These checks must be independent of the
  1651. * version number; however, the version number doesn't accomodate changes in
  1652. * the header fields (just the known tags and the interpretation of the
  1653. * data.)
  1654. */
  1655. temp = png_get_uint_32(profile+36); /* signature 'ascp' */
  1656. if (temp != 0x61637370)
  1657. return png_icc_profile_error(png_ptr, colorspace, name, temp,
  1658. "invalid signature");
  1659. /* Currently the PCS illuminant/adopted white point (the computational
  1660. * white point) are required to be D50,
  1661. * however the profile contains a record of the illuminant so perhaps ICC
  1662. * expects to be able to change this in the future (despite the rationale in
  1663. * the introduction for using a fixed PCS adopted white.) Consequently the
  1664. * following is just a warning.
  1665. */
  1666. if (memcmp(profile+68, D50_nCIEXYZ, 12) != 0)
  1667. (void)png_icc_profile_error(png_ptr, NULL, name, 0/*no tag value*/,
  1668. "PCS illuminant is not D50");
  1669. /* The PNG spec requires this:
  1670. * "If the iCCP chunk is present, the image samples conform to the colour
  1671. * space represented by the embedded ICC profile as defined by the
  1672. * International Color Consortium [ICC]. The colour space of the ICC profile
  1673. * shall be an RGB colour space for colour images (PNG colour types 2, 3, and
  1674. * 6), or a greyscale colour space for greyscale images (PNG colour types 0
  1675. * and 4)."
  1676. *
  1677. * This checking code ensures the embedded profile (on either read or write)
  1678. * conforms to the specification requirements. Notice that an ICC 'gray'
  1679. * color-space profile contains the information to transform the monochrome
  1680. * data to XYZ or L*a*b (according to which PCS the profile uses) and this
  1681. * should be used in preference to the standard libpng K channel replication
  1682. * into R, G and B channels.
  1683. *
  1684. * Previously it was suggested that an RGB profile on grayscale data could be
  1685. * handled. However it it is clear that using an RGB profile in this context
  1686. * must be an error - there is no specification of what it means. Thus it is
  1687. * almost certainly more correct to ignore the profile.
  1688. */
  1689. temp = png_get_uint_32(profile+16); /* data colour space field */
  1690. switch (temp)
  1691. {
  1692. case 0x52474220: /* 'RGB ' */
  1693. if (!(color_type & PNG_COLOR_MASK_COLOR))
  1694. return png_icc_profile_error(png_ptr, colorspace, name, temp,
  1695. "RGB color space not permitted on grayscale PNG");
  1696. break;
  1697. case 0x47524159: /* 'GRAY' */
  1698. if (color_type & PNG_COLOR_MASK_COLOR)
  1699. return png_icc_profile_error(png_ptr, colorspace, name, temp,
  1700. "Gray color space not permitted on RGB PNG");
  1701. break;
  1702. default:
  1703. return png_icc_profile_error(png_ptr, colorspace, name, temp,
  1704. "invalid ICC profile color space");
  1705. }
  1706. /* It is up to the application to check that the profile class matches the
  1707. * application requirements; the spec provides no guidance, but it's pretty
  1708. * weird if the profile is not scanner ('scnr'), monitor ('mntr'), printer
  1709. * ('prtr') or 'spac' (for generic color spaces). Issue a warning in these
  1710. * cases. Issue an error for device link or abstract profiles - these don't
  1711. * contain the records necessary to transform the color-space to anything
  1712. * other than the target device (and not even that for an abstract profile).
  1713. * Profiles of these classes may not be embedded in images.
  1714. */
  1715. temp = png_get_uint_32(profile+12); /* profile/device class */
  1716. switch (temp)
  1717. {
  1718. case 0x73636E72: /* 'scnr' */
  1719. case 0x6D6E7472: /* 'mntr' */
  1720. case 0x70727472: /* 'prtr' */
  1721. case 0x73706163: /* 'spac' */
  1722. /* All supported */
  1723. break;
  1724. case 0x61627374: /* 'abst' */
  1725. /* May not be embedded in an image */
  1726. return png_icc_profile_error(png_ptr, colorspace, name, temp,
  1727. "invalid embedded Abstract ICC profile");
  1728. case 0x6C696E6B: /* 'link' */
  1729. /* DeviceLink profiles cannnot be interpreted in a non-device specific
  1730. * fashion, if an app uses the AToB0Tag in the profile the results are
  1731. * undefined unless the result is sent to the intended device,
  1732. * therefore a DeviceLink profile should not be found embedded in a
  1733. * PNG.
  1734. */
  1735. return png_icc_profile_error(png_ptr, colorspace, name, temp,
  1736. "unexpected DeviceLink ICC profile class");
  1737. case 0x6E6D636C: /* 'nmcl' */
  1738. /* A NamedColor profile is also device specific, however it doesn't
  1739. * contain an AToB0 tag that is open to misintrepretation. Almost
  1740. * certainly it will fail the tests below.
  1741. */
  1742. (void)png_icc_profile_error(png_ptr, NULL, name, temp,
  1743. "unexpected NamedColor ICC profile class");
  1744. break;
  1745. default:
  1746. /* To allow for future enhancements to the profile accept unrecognized
  1747. * profile classes with a warning, these then hit the test below on the
  1748. * tag content to ensure they are backward compatible with one of the
  1749. * understood profiles.
  1750. */
  1751. (void)png_icc_profile_error(png_ptr, NULL, name, temp,
  1752. "unrecognized ICC profile class");
  1753. break;
  1754. }
  1755. /* For any profile other than a device link one the PCS must be encoded
  1756. * either in XYZ or Lab.
  1757. */
  1758. temp = png_get_uint_32(profile+20);
  1759. switch (temp)
  1760. {
  1761. case 0x58595A20: /* 'XYZ ' */
  1762. case 0x4C616220: /* 'Lab ' */
  1763. break;
  1764. default:
  1765. return png_icc_profile_error(png_ptr, colorspace, name, temp,
  1766. "unexpected ICC PCS encoding");
  1767. }
  1768. return 1;
  1769. }
  1770. int /* PRIVATE */
  1771. png_icc_check_tag_table(png_const_structrp png_ptr, png_colorspacerp colorspace,
  1772. png_const_charp name, png_uint_32 profile_length,
  1773. png_const_bytep profile /* header plus whole tag table */)
  1774. {
  1775. png_uint_32 tag_count = png_get_uint_32(profile+128);
  1776. png_uint_32 itag;
  1777. png_const_bytep tag = profile+132; /* The first tag */
  1778. /* First scan all the tags in the table and add bits to the icc_info value
  1779. * (temporarily in 'tags').
  1780. */
  1781. for (itag=0; itag < tag_count; ++itag, tag += 12)
  1782. {
  1783. png_uint_32 tag_id = png_get_uint_32(tag+0);
  1784. png_uint_32 tag_start = png_get_uint_32(tag+4); /* must be aligned */
  1785. png_uint_32 tag_length = png_get_uint_32(tag+8);/* not padded */
  1786. /* The ICC specification does not exclude zero length tags, therefore the
  1787. * start might actually be anywhere if there is no data, but this would be
  1788. * a clear abuse of the intent of the standard so the start is checked for
  1789. * being in range. All defined tag types have an 8 byte header - a 4 byte
  1790. * type signature then 0.
  1791. */
  1792. if ((tag_start & 3) != 0)
  1793. {
  1794. /* CNHP730S.icc shipped with Microsoft Windows 64 violates this, it is
  1795. * only a warning here because libpng does not care about the
  1796. * alignment.
  1797. */
  1798. (void)png_icc_profile_error(png_ptr, NULL, name, tag_id,
  1799. "ICC profile tag start not a multiple of 4");
  1800. }
  1801. /* This is a hard error; potentially it can cause read outside the
  1802. * profile.
  1803. */
  1804. if (tag_start > profile_length || tag_length > profile_length - tag_start)
  1805. return png_icc_profile_error(png_ptr, colorspace, name, tag_id,
  1806. "ICC profile tag outside profile");
  1807. }
  1808. return 1; /* success, maybe with warnings */
  1809. }
  1810. #ifdef PNG_sRGB_SUPPORTED
  1811. /* Information about the known ICC sRGB profiles */
  1812. static const struct
  1813. {
  1814. png_uint_32 adler, crc, length;
  1815. png_uint_32 md5[4];
  1816. png_byte have_md5;
  1817. png_byte is_broken;
  1818. png_uint_16 intent;
  1819. # define PNG_MD5(a,b,c,d) { a, b, c, d }, (a!=0)||(b!=0)||(c!=0)||(d!=0)
  1820. # define PNG_ICC_CHECKSUM(adler, crc, md5, intent, broke, date, length, fname)\
  1821. { adler, crc, length, md5, broke, intent },
  1822. } png_sRGB_checks[] =
  1823. {
  1824. /* This data comes from contrib/tools/checksum-icc run on downloads of
  1825. * all four ICC sRGB profiles from www.color.org.
  1826. */
  1827. /* adler32, crc32, MD5[4], intent, date, length, file-name */
  1828. PNG_ICC_CHECKSUM(0x0a3fd9f6, 0x3b8772b9,
  1829. PNG_MD5(0x29f83dde, 0xaff255ae, 0x7842fae4, 0xca83390d), 0, 0,
  1830. "2009/03/27 21:36:31", 3048, "sRGB_IEC61966-2-1_black_scaled.icc")
  1831. /* ICC sRGB v2 perceptual no black-compensation: */
  1832. PNG_ICC_CHECKSUM(0x4909e5e1, 0x427ebb21,
  1833. PNG_MD5(0xc95bd637, 0xe95d8a3b, 0x0df38f99, 0xc1320389), 1, 0,
  1834. "2009/03/27 21:37:45", 3052, "sRGB_IEC61966-2-1_no_black_scaling.icc")
  1835. PNG_ICC_CHECKSUM(0xfd2144a1, 0x306fd8ae,
  1836. PNG_MD5(0xfc663378, 0x37e2886b, 0xfd72e983, 0x8228f1b8), 0, 0,
  1837. "2009/08/10 17:28:01", 60988, "sRGB_v4_ICC_preference_displayclass.icc")
  1838. /* ICC sRGB v4 perceptual */
  1839. PNG_ICC_CHECKSUM(0x209c35d2, 0xbbef7812,
  1840. PNG_MD5(0x34562abf, 0x994ccd06, 0x6d2c5721, 0xd0d68c5d), 0, 0,
  1841. "2007/07/25 00:05:37", 60960, "sRGB_v4_ICC_preference.icc")
  1842. /* The following profiles have no known MD5 checksum. If there is a match
  1843. * on the (empty) MD5 the other fields are used to attempt a match and
  1844. * a warning is produced. The first two of these profiles have a 'cprt' tag
  1845. * which suggests that they were also made by Hewlett Packard.
  1846. */
  1847. PNG_ICC_CHECKSUM(0xa054d762, 0x5d5129ce,
  1848. PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 0,
  1849. "2004/07/21 18:57:42", 3024, "sRGB_IEC61966-2-1_noBPC.icc")
  1850. /* This is a 'mntr' (display) profile with a mediaWhitePointTag that does not
  1851. * match the D50 PCS illuminant in the header (it is in fact the D65 values,
  1852. * so the white point is recorded as the un-adapted value.) The profiles
  1853. * below only differ in one byte - the intent - and are basically the same as
  1854. * the previous profile except for the mediaWhitePointTag error and a missing
  1855. * chromaticAdaptationTag.
  1856. */
  1857. PNG_ICC_CHECKSUM(0xf784f3fb, 0x182ea552,
  1858. PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 0, 1/*broken*/,
  1859. "1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 perceptual")
  1860. PNG_ICC_CHECKSUM(0x0398f3fc, 0xf29e526d,
  1861. PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 1/*broken*/,
  1862. "1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 media-relative")
  1863. };
  1864. static int
  1865. png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr,
  1866. png_const_bytep profile, uLong adler)
  1867. {
  1868. /* The quick check is to verify just the MD5 signature and trust the
  1869. * rest of the data. Because the profile has already been verified for
  1870. * correctness this is safe. png_colorspace_set_sRGB will check the 'intent'
  1871. * field too, so if the profile has been edited with an intent not defined
  1872. * by sRGB (but maybe defined by a later ICC specification) the read of
  1873. * the profile will fail at that point.
  1874. */
  1875. png_uint_32 length = 0;
  1876. png_uint_32 intent = 0x10000; /* invalid */
  1877. #if PNG_sRGB_PROFILE_CHECKS > 1
  1878. uLong crc = 0; /* the value for 0 length data */
  1879. #endif
  1880. unsigned int i;
  1881. for (i=0; i < (sizeof png_sRGB_checks) / (sizeof png_sRGB_checks[0]); ++i)
  1882. {
  1883. if (png_get_uint_32(profile+84) == png_sRGB_checks[i].md5[0] &&
  1884. png_get_uint_32(profile+88) == png_sRGB_checks[i].md5[1] &&
  1885. png_get_uint_32(profile+92) == png_sRGB_checks[i].md5[2] &&
  1886. png_get_uint_32(profile+96) == png_sRGB_checks[i].md5[3])
  1887. {
  1888. /* This may be one of the old HP profiles without an MD5, in that
  1889. * case we can only use the length and Adler32 (note that these
  1890. * are not used by default if there is an MD5!)
  1891. */
  1892. # if PNG_sRGB_PROFILE_CHECKS == 0
  1893. if (png_sRGB_checks[i].have_md5)
  1894. return 1+png_sRGB_checks[i].is_broken;
  1895. # endif
  1896. /* Profile is unsigned or more checks have been configured in. */
  1897. if (length == 0)
  1898. {
  1899. length = png_get_uint_32(profile);
  1900. intent = png_get_uint_32(profile+64);
  1901. }
  1902. /* Length *and* intent must match */
  1903. if (length == png_sRGB_checks[i].length &&
  1904. intent == png_sRGB_checks[i].intent)
  1905. {
  1906. /* Now calculate the adler32 if not done already. */
  1907. if (adler == 0)
  1908. {
  1909. adler = adler32(0, NULL, 0);
  1910. adler = adler32(adler, profile, length);
  1911. }
  1912. if (adler == png_sRGB_checks[i].adler)
  1913. {
  1914. /* These basic checks suggest that the data has not been
  1915. * modified, but if the check level is more than 1 perform
  1916. * our own crc32 checksum on the data.
  1917. */
  1918. # if PNG_sRGB_PROFILE_CHECKS > 1
  1919. if (crc == 0)
  1920. {
  1921. crc = crc32(0, NULL, 0);
  1922. crc = crc32(crc, profile, length);
  1923. }
  1924. /* So this check must pass for the 'return' below to happen.
  1925. */
  1926. if (crc == png_sRGB_checks[i].crc)
  1927. # endif
  1928. {
  1929. if (png_sRGB_checks[i].is_broken)
  1930. {
  1931. /* These profiles are known to have bad data that may cause
  1932. * problems if they are used, therefore attempt to
  1933. * discourage their use, skip the 'have_md5' warning below,
  1934. * which is made irrelevant by this error.
  1935. */
  1936. png_chunk_report(png_ptr, "known incorrect sRGB profile",
  1937. PNG_CHUNK_ERROR);
  1938. }
  1939. /* Warn that this being done; this isn't even an error since
  1940. * the profile is perfectly valid, but it would be nice if
  1941. * people used the up-to-date ones.
  1942. */
  1943. else if (!png_sRGB_checks[i].have_md5)
  1944. {
  1945. png_chunk_report(png_ptr,
  1946. "out-of-date sRGB profile with no signature",
  1947. PNG_CHUNK_WARNING);
  1948. }
  1949. return 1+png_sRGB_checks[i].is_broken;
  1950. }
  1951. }
  1952. }
  1953. # if PNG_sRGB_PROFILE_CHECKS > 0
  1954. /* The signature matched, but the profile had been changed in some
  1955. * way. This is an apparent violation of the ICC terms of use and,
  1956. * anyway, probably indicates a data error or uninformed hacking.
  1957. */
  1958. if (png_sRGB_checks[i].have_md5)
  1959. png_benign_error(png_ptr,
  1960. "copyright violation: edited ICC profile ignored");
  1961. # endif
  1962. }
  1963. }
  1964. return 0; /* no match */
  1965. }
  1966. #endif
  1967. #ifdef PNG_sRGB_SUPPORTED
  1968. void /* PRIVATE */
  1969. png_icc_set_sRGB(png_const_structrp png_ptr,
  1970. png_colorspacerp colorspace, png_const_bytep profile, uLong adler)
  1971. {
  1972. /* Is this profile one of the known ICC sRGB profiles? If it is, just set
  1973. * the sRGB information.
  1974. */
  1975. if (png_compare_ICC_profile_with_sRGB(png_ptr, profile, adler))
  1976. (void)png_colorspace_set_sRGB(png_ptr, colorspace,
  1977. (int)/*already checked*/png_get_uint_32(profile+64));
  1978. }
  1979. #endif /* PNG_READ_sRGB_SUPPORTED */
  1980. int /* PRIVATE */
  1981. png_colorspace_set_ICC(png_const_structrp png_ptr, png_colorspacerp colorspace,
  1982. png_const_charp name, png_uint_32 profile_length, png_const_bytep profile,
  1983. int color_type)
  1984. {
  1985. if (colorspace->flags & PNG_COLORSPACE_INVALID)
  1986. return 0;
  1987. if (png_icc_check_length(png_ptr, colorspace, name, profile_length) &&
  1988. png_icc_check_header(png_ptr, colorspace, name, profile_length, profile,
  1989. color_type) &&
  1990. png_icc_check_tag_table(png_ptr, colorspace, name, profile_length,
  1991. profile))
  1992. {
  1993. png_icc_set_sRGB(png_ptr, colorspace, profile, 0);
  1994. return 1;
  1995. }
  1996. /* Failure case */
  1997. return 0;
  1998. }
  1999. #endif /* iCCP */
  2000. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  2001. void /* PRIVATE */
  2002. png_colorspace_set_rgb_coefficients(png_structrp png_ptr)
  2003. {
  2004. /* Set the rgb_to_gray coefficients from the colorspace. */
  2005. if (!png_ptr->rgb_to_gray_coefficients_set &&
  2006. (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
  2007. {
  2008. /* png_set_background has not been called, get the coefficients from the Y
  2009. * values of the colorspace colorants.
  2010. */
  2011. png_fixed_point r = png_ptr->colorspace.end_points_XYZ.red_Y;
  2012. png_fixed_point g = png_ptr->colorspace.end_points_XYZ.green_Y;
  2013. png_fixed_point b = png_ptr->colorspace.end_points_XYZ.blue_Y;
  2014. png_fixed_point total = r+g+b;
  2015. if (total > 0 &&
  2016. r >= 0 && png_muldiv(&r, r, 32768, total) && r >= 0 && r <= 32768 &&
  2017. g >= 0 && png_muldiv(&g, g, 32768, total) && g >= 0 && g <= 32768 &&
  2018. b >= 0 && png_muldiv(&b, b, 32768, total) && b >= 0 && b <= 32768 &&
  2019. r+g+b <= 32769)
  2020. {
  2021. /* We allow 0 coefficients here. r+g+b may be 32769 if two or
  2022. * all of the coefficients were rounded up. Handle this by
  2023. * reducing the *largest* coefficient by 1; this matches the
  2024. * approach used for the default coefficients in pngrtran.c
  2025. */
  2026. int add = 0;
  2027. if (r+g+b > 32768)
  2028. add = -1;
  2029. else if (r+g+b < 32768)
  2030. add = 1;
  2031. if (add != 0)
  2032. {
  2033. if (g >= r && g >= b)
  2034. g += add;
  2035. else if (r >= g && r >= b)
  2036. r += add;
  2037. else
  2038. b += add;
  2039. }
  2040. /* Check for an internal error. */
  2041. if (r+g+b != 32768)
  2042. png_error(png_ptr,
  2043. "internal error handling cHRM coefficients");
  2044. else
  2045. {
  2046. png_ptr->rgb_to_gray_red_coeff = (png_uint_16)r;
  2047. png_ptr->rgb_to_gray_green_coeff = (png_uint_16)g;
  2048. }
  2049. }
  2050. /* This is a png_error at present even though it could be ignored -
  2051. * it should never happen, but it is important that if it does, the
  2052. * bug is fixed.
  2053. */
  2054. else
  2055. png_error(png_ptr, "internal error handling cHRM->XYZ");
  2056. }
  2057. }
  2058. #endif
  2059. #endif /* COLORSPACE */
  2060. void /* PRIVATE */
  2061. png_check_IHDR(png_const_structrp png_ptr,
  2062. png_uint_32 width, png_uint_32 height, int bit_depth,
  2063. int color_type, int interlace_type, int compression_type,
  2064. int filter_type)
  2065. {
  2066. int error = 0;
  2067. /* Check for width and height valid values */
  2068. if (width == 0)
  2069. {
  2070. png_warning(png_ptr, "Image width is zero in IHDR");
  2071. error = 1;
  2072. }
  2073. if (height == 0)
  2074. {
  2075. png_warning(png_ptr, "Image height is zero in IHDR");
  2076. error = 1;
  2077. }
  2078. # ifdef PNG_SET_USER_LIMITS_SUPPORTED
  2079. if (width > png_ptr->user_width_max)
  2080. # else
  2081. if (width > PNG_USER_WIDTH_MAX)
  2082. # endif
  2083. {
  2084. png_warning(png_ptr, "Image width exceeds user limit in IHDR");
  2085. error = 1;
  2086. }
  2087. # ifdef PNG_SET_USER_LIMITS_SUPPORTED
  2088. if (height > png_ptr->user_height_max)
  2089. # else
  2090. if (height > PNG_USER_HEIGHT_MAX)
  2091. # endif
  2092. {
  2093. png_warning(png_ptr, "Image height exceeds user limit in IHDR");
  2094. error = 1;
  2095. }
  2096. if (width > PNG_UINT_31_MAX)
  2097. {
  2098. png_warning(png_ptr, "Invalid image width in IHDR");
  2099. error = 1;
  2100. }
  2101. if (height > PNG_UINT_31_MAX)
  2102. {
  2103. png_warning(png_ptr, "Invalid image height in IHDR");
  2104. error = 1;
  2105. }
  2106. if (width > (PNG_UINT_32_MAX
  2107. >> 3) /* 8-byte RGBA pixels */
  2108. - 48 /* bigrowbuf hack */
  2109. - 1 /* filter byte */
  2110. - 7*8 /* rounding of width to multiple of 8 pixels */
  2111. - 8) /* extra max_pixel_depth pad */
  2112. png_warning(png_ptr, "Width is too large for libpng to process pixels");
  2113. /* Check other values */
  2114. if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
  2115. bit_depth != 8 && bit_depth != 16)
  2116. {
  2117. png_warning(png_ptr, "Invalid bit depth in IHDR");
  2118. error = 1;
  2119. }
  2120. if (color_type < 0 || color_type == 1 ||
  2121. color_type == 5 || color_type > 6)
  2122. {
  2123. png_warning(png_ptr, "Invalid color type in IHDR");
  2124. error = 1;
  2125. }
  2126. if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
  2127. ((color_type == PNG_COLOR_TYPE_RGB ||
  2128. color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
  2129. color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
  2130. {
  2131. png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
  2132. error = 1;
  2133. }
  2134. if (interlace_type >= PNG_INTERLACE_LAST)
  2135. {
  2136. png_warning(png_ptr, "Unknown interlace method in IHDR");
  2137. error = 1;
  2138. }
  2139. if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  2140. {
  2141. png_warning(png_ptr, "Unknown compression method in IHDR");
  2142. error = 1;
  2143. }
  2144. # ifdef PNG_MNG_FEATURES_SUPPORTED
  2145. /* Accept filter_method 64 (intrapixel differencing) only if
  2146. * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  2147. * 2. Libpng did not read a PNG signature (this filter_method is only
  2148. * used in PNG datastreams that are embedded in MNG datastreams) and
  2149. * 3. The application called png_permit_mng_features with a mask that
  2150. * included PNG_FLAG_MNG_FILTER_64 and
  2151. * 4. The filter_method is 64 and
  2152. * 5. The color_type is RGB or RGBA
  2153. */
  2154. if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&
  2155. png_ptr->mng_features_permitted)
  2156. png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
  2157. if (filter_type != PNG_FILTER_TYPE_BASE)
  2158. {
  2159. if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  2160. (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
  2161. ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
  2162. (color_type == PNG_COLOR_TYPE_RGB ||
  2163. color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
  2164. {
  2165. png_warning(png_ptr, "Unknown filter method in IHDR");
  2166. error = 1;
  2167. }
  2168. if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)
  2169. {
  2170. png_warning(png_ptr, "Invalid filter method in IHDR");
  2171. error = 1;
  2172. }
  2173. }
  2174. # else
  2175. if (filter_type != PNG_FILTER_TYPE_BASE)
  2176. {
  2177. png_warning(png_ptr, "Unknown filter method in IHDR");
  2178. error = 1;
  2179. }
  2180. # endif
  2181. if (error == 1)
  2182. png_error(png_ptr, "Invalid IHDR data");
  2183. }
  2184. #if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED)
  2185. /* ASCII to fp functions */
  2186. /* Check an ASCII formated floating point value, see the more detailed
  2187. * comments in pngpriv.h
  2188. */
  2189. /* The following is used internally to preserve the sticky flags */
  2190. #define png_fp_add(state, flags) ((state) |= (flags))
  2191. #define png_fp_set(state, value) ((state) = (value) | ((state) & PNG_FP_STICKY))
  2192. int /* PRIVATE */
  2193. png_check_fp_number(png_const_charp string, png_size_t size, int *statep,
  2194. png_size_tp whereami)
  2195. {
  2196. int state = *statep;
  2197. png_size_t i = *whereami;
  2198. while (i < size)
  2199. {
  2200. int type;
  2201. /* First find the type of the next character */
  2202. switch (string[i])
  2203. {
  2204. case 43: type = PNG_FP_SAW_SIGN; break;
  2205. case 45: type = PNG_FP_SAW_SIGN + PNG_FP_NEGATIVE; break;
  2206. case 46: type = PNG_FP_SAW_DOT; break;
  2207. case 48: type = PNG_FP_SAW_DIGIT; break;
  2208. case 49: case 50: case 51: case 52:
  2209. case 53: case 54: case 55: case 56:
  2210. case 57: type = PNG_FP_SAW_DIGIT + PNG_FP_NONZERO; break;
  2211. case 69:
  2212. case 101: type = PNG_FP_SAW_E; break;
  2213. default: goto PNG_FP_End;
  2214. }
  2215. /* Now deal with this type according to the current
  2216. * state, the type is arranged to not overlap the
  2217. * bits of the PNG_FP_STATE.
  2218. */
  2219. switch ((state & PNG_FP_STATE) + (type & PNG_FP_SAW_ANY))
  2220. {
  2221. case PNG_FP_INTEGER + PNG_FP_SAW_SIGN:
  2222. if (state & PNG_FP_SAW_ANY)
  2223. goto PNG_FP_End; /* not a part of the number */
  2224. png_fp_add(state, type);
  2225. break;
  2226. case PNG_FP_INTEGER + PNG_FP_SAW_DOT:
  2227. /* Ok as trailer, ok as lead of fraction. */
  2228. if (state & PNG_FP_SAW_DOT) /* two dots */
  2229. goto PNG_FP_End;
  2230. else if (state & PNG_FP_SAW_DIGIT) /* trailing dot? */
  2231. png_fp_add(state, type);
  2232. else
  2233. png_fp_set(state, PNG_FP_FRACTION | type);
  2234. break;
  2235. case PNG_FP_INTEGER + PNG_FP_SAW_DIGIT:
  2236. if (state & PNG_FP_SAW_DOT) /* delayed fraction */
  2237. png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT);
  2238. png_fp_add(state, type | PNG_FP_WAS_VALID);
  2239. break;
  2240. case PNG_FP_INTEGER + PNG_FP_SAW_E:
  2241. if ((state & PNG_FP_SAW_DIGIT) == 0)
  2242. goto PNG_FP_End;
  2243. png_fp_set(state, PNG_FP_EXPONENT);
  2244. break;
  2245. /* case PNG_FP_FRACTION + PNG_FP_SAW_SIGN:
  2246. goto PNG_FP_End; ** no sign in fraction */
  2247. /* case PNG_FP_FRACTION + PNG_FP_SAW_DOT:
  2248. goto PNG_FP_End; ** Because SAW_DOT is always set */
  2249. case PNG_FP_FRACTION + PNG_FP_SAW_DIGIT:
  2250. png_fp_add(state, type | PNG_FP_WAS_VALID);
  2251. break;
  2252. case PNG_FP_FRACTION + PNG_FP_SAW_E:
  2253. /* This is correct because the trailing '.' on an
  2254. * integer is handled above - so we can only get here
  2255. * with the sequence ".E" (with no preceding digits).
  2256. */
  2257. if ((state & PNG_FP_SAW_DIGIT) == 0)
  2258. goto PNG_FP_End;
  2259. png_fp_set(state, PNG_FP_EXPONENT);
  2260. break;
  2261. case PNG_FP_EXPONENT + PNG_FP_SAW_SIGN:
  2262. if (state & PNG_FP_SAW_ANY)
  2263. goto PNG_FP_End; /* not a part of the number */
  2264. png_fp_add(state, PNG_FP_SAW_SIGN);
  2265. break;
  2266. /* case PNG_FP_EXPONENT + PNG_FP_SAW_DOT:
  2267. goto PNG_FP_End; */
  2268. case PNG_FP_EXPONENT + PNG_FP_SAW_DIGIT:
  2269. png_fp_add(state, PNG_FP_SAW_DIGIT | PNG_FP_WAS_VALID);
  2270. break;
  2271. /* case PNG_FP_EXPONEXT + PNG_FP_SAW_E:
  2272. goto PNG_FP_End; */
  2273. default: goto PNG_FP_End; /* I.e. break 2 */
  2274. }
  2275. /* The character seems ok, continue. */
  2276. ++i;
  2277. }
  2278. PNG_FP_End:
  2279. /* Here at the end, update the state and return the correct
  2280. * return code.
  2281. */
  2282. *statep = state;
  2283. *whereami = i;
  2284. return (state & PNG_FP_SAW_DIGIT) != 0;
  2285. }
  2286. /* The same but for a complete string. */
  2287. int
  2288. png_check_fp_string(png_const_charp string, png_size_t size)
  2289. {
  2290. int state=0;
  2291. png_size_t char_index=0;
  2292. if (png_check_fp_number(string, size, &state, &char_index) &&
  2293. (char_index == size || string[char_index] == 0))
  2294. return state /* must be non-zero - see above */;
  2295. return 0; /* i.e. fail */
  2296. }
  2297. #endif /* pCAL or sCAL */
  2298. #ifdef PNG_sCAL_SUPPORTED
  2299. # ifdef PNG_FLOATING_POINT_SUPPORTED
  2300. /* Utility used below - a simple accurate power of ten from an integral
  2301. * exponent.
  2302. */
  2303. static double
  2304. png_pow10(int power)
  2305. {
  2306. int recip = 0;
  2307. double d = 1;
  2308. /* Handle negative exponent with a reciprocal at the end because
  2309. * 10 is exact whereas .1 is inexact in base 2
  2310. */
  2311. if (power < 0)
  2312. {
  2313. if (power < DBL_MIN_10_EXP) return 0;
  2314. recip = 1, power = -power;
  2315. }
  2316. if (power > 0)
  2317. {
  2318. /* Decompose power bitwise. */
  2319. double mult = 10;
  2320. do
  2321. {
  2322. if (power & 1) d *= mult;
  2323. mult *= mult;
  2324. power >>= 1;
  2325. }
  2326. while (power > 0);
  2327. if (recip) d = 1/d;
  2328. }
  2329. /* else power is 0 and d is 1 */
  2330. return d;
  2331. }
  2332. /* Function to format a floating point value in ASCII with a given
  2333. * precision.
  2334. */
  2335. void /* PRIVATE */
  2336. png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
  2337. double fp, unsigned int precision)
  2338. {
  2339. /* We use standard functions from math.h, but not printf because
  2340. * that would require stdio. The caller must supply a buffer of
  2341. * sufficient size or we will png_error. The tests on size and
  2342. * the space in ascii[] consumed are indicated below.
  2343. */
  2344. if (precision < 1)
  2345. precision = DBL_DIG;
  2346. /* Enforce the limit of the implementation precision too. */
  2347. if (precision > DBL_DIG+1)
  2348. precision = DBL_DIG+1;
  2349. /* Basic sanity checks */
  2350. if (size >= precision+5) /* See the requirements below. */
  2351. {
  2352. if (fp < 0)
  2353. {
  2354. fp = -fp;
  2355. *ascii++ = 45; /* '-' PLUS 1 TOTAL 1 */
  2356. --size;
  2357. }
  2358. if (fp >= DBL_MIN && fp <= DBL_MAX)
  2359. {
  2360. int exp_b10; /* A base 10 exponent */
  2361. double base; /* 10^exp_b10 */
  2362. /* First extract a base 10 exponent of the number,
  2363. * the calculation below rounds down when converting
  2364. * from base 2 to base 10 (multiply by log10(2) -
  2365. * 0.3010, but 77/256 is 0.3008, so exp_b10 needs to
  2366. * be increased. Note that the arithmetic shift
  2367. * performs a floor() unlike C arithmetic - using a
  2368. * C multiply would break the following for negative
  2369. * exponents.
  2370. */
  2371. (void)frexp(fp, &exp_b10); /* exponent to base 2 */
  2372. exp_b10 = (exp_b10 * 77) >> 8; /* <= exponent to base 10 */
  2373. /* Avoid underflow here. */
  2374. base = png_pow10(exp_b10); /* May underflow */
  2375. while (base < DBL_MIN || base < fp)
  2376. {
  2377. /* And this may overflow. */
  2378. double test = png_pow10(exp_b10+1);
  2379. if (test <= DBL_MAX)
  2380. ++exp_b10, base = test;
  2381. else
  2382. break;
  2383. }
  2384. /* Normalize fp and correct exp_b10, after this fp is in the
  2385. * range [.1,1) and exp_b10 is both the exponent and the digit
  2386. * *before* which the decimal point should be inserted
  2387. * (starting with 0 for the first digit). Note that this
  2388. * works even if 10^exp_b10 is out of range because of the
  2389. * test on DBL_MAX above.
  2390. */
  2391. fp /= base;
  2392. while (fp >= 1) fp /= 10, ++exp_b10;
  2393. /* Because of the code above fp may, at this point, be
  2394. * less than .1, this is ok because the code below can
  2395. * handle the leading zeros this generates, so no attempt
  2396. * is made to correct that here.
  2397. */
  2398. {
  2399. int czero, clead, cdigits;
  2400. char exponent[10];
  2401. /* Allow up to two leading zeros - this will not lengthen
  2402. * the number compared to using E-n.
  2403. */
  2404. if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */
  2405. {
  2406. czero = -exp_b10; /* PLUS 2 digits: TOTAL 3 */
  2407. exp_b10 = 0; /* Dot added below before first output. */
  2408. }
  2409. else
  2410. czero = 0; /* No zeros to add */
  2411. /* Generate the digit list, stripping trailing zeros and
  2412. * inserting a '.' before a digit if the exponent is 0.
  2413. */
  2414. clead = czero; /* Count of leading zeros */
  2415. cdigits = 0; /* Count of digits in list. */
  2416. do
  2417. {
  2418. double d;
  2419. fp *= 10;
  2420. /* Use modf here, not floor and subtract, so that
  2421. * the separation is done in one step. At the end
  2422. * of the loop don't break the number into parts so
  2423. * that the final digit is rounded.
  2424. */
  2425. if (cdigits+czero-clead+1 < (int)precision)
  2426. fp = modf(fp, &d);
  2427. else
  2428. {
  2429. d = floor(fp + .5);
  2430. if (d > 9)
  2431. {
  2432. /* Rounding up to 10, handle that here. */
  2433. if (czero > 0)
  2434. {
  2435. --czero, d = 1;
  2436. if (cdigits == 0) --clead;
  2437. }
  2438. else
  2439. {
  2440. while (cdigits > 0 && d > 9)
  2441. {
  2442. int ch = *--ascii;
  2443. if (exp_b10 != (-1))
  2444. ++exp_b10;
  2445. else if (ch == 46)
  2446. {
  2447. ch = *--ascii, ++size;
  2448. /* Advance exp_b10 to '1', so that the
  2449. * decimal point happens after the
  2450. * previous digit.
  2451. */
  2452. exp_b10 = 1;
  2453. }
  2454. --cdigits;
  2455. d = ch - 47; /* I.e. 1+(ch-48) */
  2456. }
  2457. /* Did we reach the beginning? If so adjust the
  2458. * exponent but take into account the leading
  2459. * decimal point.
  2460. */
  2461. if (d > 9) /* cdigits == 0 */
  2462. {
  2463. if (exp_b10 == (-1))
  2464. {
  2465. /* Leading decimal point (plus zeros?), if
  2466. * we lose the decimal point here it must
  2467. * be reentered below.
  2468. */
  2469. int ch = *--ascii;
  2470. if (ch == 46)
  2471. ++size, exp_b10 = 1;
  2472. /* Else lost a leading zero, so 'exp_b10' is
  2473. * still ok at (-1)
  2474. */
  2475. }
  2476. else
  2477. ++exp_b10;
  2478. /* In all cases we output a '1' */
  2479. d = 1;
  2480. }
  2481. }
  2482. }
  2483. fp = 0; /* Guarantees termination below. */
  2484. }
  2485. if (d == 0)
  2486. {
  2487. ++czero;
  2488. if (cdigits == 0) ++clead;
  2489. }
  2490. else
  2491. {
  2492. /* Included embedded zeros in the digit count. */
  2493. cdigits += czero - clead;
  2494. clead = 0;
  2495. while (czero > 0)
  2496. {
  2497. /* exp_b10 == (-1) means we just output the decimal
  2498. * place - after the DP don't adjust 'exp_b10' any
  2499. * more!
  2500. */
  2501. if (exp_b10 != (-1))
  2502. {
  2503. if (exp_b10 == 0) *ascii++ = 46, --size;
  2504. /* PLUS 1: TOTAL 4 */
  2505. --exp_b10;
  2506. }
  2507. *ascii++ = 48, --czero;
  2508. }
  2509. if (exp_b10 != (-1))
  2510. {
  2511. if (exp_b10 == 0) *ascii++ = 46, --size; /* counted
  2512. above */
  2513. --exp_b10;
  2514. }
  2515. *ascii++ = (char)(48 + (int)d), ++cdigits;
  2516. }
  2517. }
  2518. while (cdigits+czero-clead < (int)precision && fp > DBL_MIN);
  2519. /* The total output count (max) is now 4+precision */
  2520. /* Check for an exponent, if we don't need one we are
  2521. * done and just need to terminate the string. At
  2522. * this point exp_b10==(-1) is effectively if flag - it got
  2523. * to '-1' because of the decrement after outputing
  2524. * the decimal point above (the exponent required is
  2525. * *not* -1!)
  2526. */
  2527. if (exp_b10 >= (-1) && exp_b10 <= 2)
  2528. {
  2529. /* The following only happens if we didn't output the
  2530. * leading zeros above for negative exponent, so this
  2531. * doest add to the digit requirement. Note that the
  2532. * two zeros here can only be output if the two leading
  2533. * zeros were *not* output, so this doesn't increase
  2534. * the output count.
  2535. */
  2536. while (--exp_b10 >= 0) *ascii++ = 48;
  2537. *ascii = 0;
  2538. /* Total buffer requirement (including the '\0') is
  2539. * 5+precision - see check at the start.
  2540. */
  2541. return;
  2542. }
  2543. /* Here if an exponent is required, adjust size for
  2544. * the digits we output but did not count. The total
  2545. * digit output here so far is at most 1+precision - no
  2546. * decimal point and no leading or trailing zeros have
  2547. * been output.
  2548. */
  2549. size -= cdigits;
  2550. *ascii++ = 69, --size; /* 'E': PLUS 1 TOTAL 2+precision */
  2551. /* The following use of an unsigned temporary avoids ambiguities in
  2552. * the signed arithmetic on exp_b10 and permits GCC at least to do
  2553. * better optimization.
  2554. */
  2555. {
  2556. unsigned int uexp_b10;
  2557. if (exp_b10 < 0)
  2558. {
  2559. *ascii++ = 45, --size; /* '-': PLUS 1 TOTAL 3+precision */
  2560. uexp_b10 = -exp_b10;
  2561. }
  2562. else
  2563. uexp_b10 = exp_b10;
  2564. cdigits = 0;
  2565. while (uexp_b10 > 0)
  2566. {
  2567. exponent[cdigits++] = (char)(48 + uexp_b10 % 10);
  2568. uexp_b10 /= 10;
  2569. }
  2570. }
  2571. /* Need another size check here for the exponent digits, so
  2572. * this need not be considered above.
  2573. */
  2574. if ((int)size > cdigits)
  2575. {
  2576. while (cdigits > 0) *ascii++ = exponent[--cdigits];
  2577. *ascii = 0;
  2578. return;
  2579. }
  2580. }
  2581. }
  2582. else if (!(fp >= DBL_MIN))
  2583. {
  2584. *ascii++ = 48; /* '0' */
  2585. *ascii = 0;
  2586. return;
  2587. }
  2588. else
  2589. {
  2590. *ascii++ = 105; /* 'i' */
  2591. *ascii++ = 110; /* 'n' */
  2592. *ascii++ = 102; /* 'f' */
  2593. *ascii = 0;
  2594. return;
  2595. }
  2596. }
  2597. /* Here on buffer too small. */
  2598. png_error(png_ptr, "ASCII conversion buffer too small");
  2599. }
  2600. # endif /* FLOATING_POINT */
  2601. # ifdef PNG_FIXED_POINT_SUPPORTED
  2602. /* Function to format a fixed point value in ASCII.
  2603. */
  2604. void /* PRIVATE */
  2605. png_ascii_from_fixed(png_const_structrp png_ptr, png_charp ascii,
  2606. png_size_t size, png_fixed_point fp)
  2607. {
  2608. /* Require space for 10 decimal digits, a decimal point, a minus sign and a
  2609. * trailing \0, 13 characters:
  2610. */
  2611. if (size > 12)
  2612. {
  2613. png_uint_32 num;
  2614. /* Avoid overflow here on the minimum integer. */
  2615. if (fp < 0)
  2616. *ascii++ = 45, --size, num = -fp;
  2617. else
  2618. num = fp;
  2619. if (num <= 0x80000000) /* else overflowed */
  2620. {
  2621. unsigned int ndigits = 0, first = 16 /* flag value */;
  2622. char digits[10];
  2623. while (num)
  2624. {
  2625. /* Split the low digit off num: */
  2626. unsigned int tmp = num/10;
  2627. num -= tmp*10;
  2628. digits[ndigits++] = (char)(48 + num);
  2629. /* Record the first non-zero digit, note that this is a number
  2630. * starting at 1, it's not actually the array index.
  2631. */
  2632. if (first == 16 && num > 0)
  2633. first = ndigits;
  2634. num = tmp;
  2635. }
  2636. if (ndigits > 0)
  2637. {
  2638. while (ndigits > 5) *ascii++ = digits[--ndigits];
  2639. /* The remaining digits are fractional digits, ndigits is '5' or
  2640. * smaller at this point. It is certainly not zero. Check for a
  2641. * non-zero fractional digit:
  2642. */
  2643. if (first <= 5)
  2644. {
  2645. unsigned int i;
  2646. *ascii++ = 46; /* decimal point */
  2647. /* ndigits may be <5 for small numbers, output leading zeros
  2648. * then ndigits digits to first:
  2649. */
  2650. i = 5;
  2651. while (ndigits < i) *ascii++ = 48, --i;
  2652. while (ndigits >= first) *ascii++ = digits[--ndigits];
  2653. /* Don't output the trailing zeros! */
  2654. }
  2655. }
  2656. else
  2657. *ascii++ = 48;
  2658. /* And null terminate the string: */
  2659. *ascii = 0;
  2660. return;
  2661. }
  2662. }
  2663. /* Here on buffer too small. */
  2664. png_error(png_ptr, "ASCII conversion buffer too small");
  2665. }
  2666. # endif /* FIXED_POINT */
  2667. #endif /* READ_SCAL */
  2668. #if defined(PNG_FLOATING_POINT_SUPPORTED) && \
  2669. !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) && \
  2670. (defined(PNG_gAMA_SUPPORTED) || defined(PNG_cHRM_SUPPORTED) || \
  2671. defined(PNG_sCAL_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) || \
  2672. defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)) || \
  2673. (defined(PNG_sCAL_SUPPORTED) && \
  2674. defined(PNG_FLOATING_ARITHMETIC_SUPPORTED))
  2675. png_fixed_point
  2676. png_fixed(png_const_structrp png_ptr, double fp, png_const_charp)
  2677. {
  2678. double r = floor(100000 * fp + .5);
  2679. if (r > 2147483647. || r < -2147483648.)
  2680. png_fixed_error(png_ptr, text);
  2681. return (png_fixed_point)r;
  2682. }
  2683. #endif
  2684. #if defined(PNG_READ_GAMMA_SUPPORTED) || \
  2685. defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED)
  2686. /* muldiv functions */
  2687. /* This API takes signed arguments and rounds the result to the nearest
  2688. * integer (or, for a fixed point number - the standard argument - to
  2689. * the nearest .00001). Overflow and divide by zero are signalled in
  2690. * the result, a boolean - true on success, false on overflow.
  2691. */
  2692. int
  2693. png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times,
  2694. png_int_32 divisor)
  2695. {
  2696. /* Return a * times / divisor, rounded. */
  2697. if (divisor != 0)
  2698. {
  2699. if (a == 0 || times == 0)
  2700. {
  2701. *res = 0;
  2702. return 1;
  2703. }
  2704. else
  2705. {
  2706. #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
  2707. double r = a;
  2708. r *= times;
  2709. r /= divisor;
  2710. r = floor(r+.5);
  2711. /* A png_fixed_point is a 32-bit integer. */
  2712. if (r <= 2147483647. && r >= -2147483648.)
  2713. {
  2714. *res = (png_fixed_point)r;
  2715. return 1;
  2716. }
  2717. #else
  2718. int negative = 0;
  2719. png_uint_32 A, T, D;
  2720. png_uint_32 s16, s32, s00;
  2721. if (a < 0)
  2722. negative = 1, A = -a;
  2723. else
  2724. A = a;
  2725. if (times < 0)
  2726. negative = !negative, T = -times;
  2727. else
  2728. T = times;
  2729. if (divisor < 0)
  2730. negative = !negative, D = -divisor;
  2731. else
  2732. D = divisor;
  2733. /* Following can't overflow because the arguments only
  2734. * have 31 bits each, however the result may be 32 bits.
  2735. */
  2736. s16 = (A >> 16) * (T & 0xffff) +
  2737. (A & 0xffff) * (T >> 16);
  2738. /* Can't overflow because the a*times bit is only 30
  2739. * bits at most.
  2740. */
  2741. s32 = (A >> 16) * (T >> 16) + (s16 >> 16);
  2742. s00 = (A & 0xffff) * (T & 0xffff);
  2743. s16 = (s16 & 0xffff) << 16;
  2744. s00 += s16;
  2745. if (s00 < s16)
  2746. ++s32; /* carry */
  2747. if (s32 < D) /* else overflow */
  2748. {
  2749. /* s32.s00 is now the 64-bit product, do a standard
  2750. * division, we know that s32 < D, so the maximum
  2751. * required shift is 31.
  2752. */
  2753. int bitshift = 32;
  2754. png_fixed_point result = 0; /* NOTE: signed */
  2755. while (--bitshift >= 0)
  2756. {
  2757. png_uint_32 d32, d00;
  2758. if (bitshift > 0)
  2759. d32 = D >> (32-bitshift), d00 = D << bitshift;
  2760. else
  2761. d32 = 0, d00 = D;
  2762. if (s32 > d32)
  2763. {
  2764. if (s00 < d00) --s32; /* carry */
  2765. s32 -= d32, s00 -= d00, result += 1<<bitshift;
  2766. }
  2767. else
  2768. if (s32 == d32 && s00 >= d00)
  2769. s32 = 0, s00 -= d00, result += 1<<bitshift;
  2770. }
  2771. /* Handle the rounding. */
  2772. if (s00 >= (D >> 1))
  2773. ++result;
  2774. if (negative)
  2775. result = -result;
  2776. /* Check for overflow. */
  2777. if ((negative && result <= 0) || (!negative && result >= 0))
  2778. {
  2779. *res = result;
  2780. return 1;
  2781. }
  2782. }
  2783. #endif
  2784. }
  2785. }
  2786. return 0;
  2787. }
  2788. #endif /* READ_GAMMA || INCH_CONVERSIONS */
  2789. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED)
  2790. /* The following is for when the caller doesn't much care about the
  2791. * result.
  2792. */
  2793. png_fixed_point
  2794. png_muldiv_warn(png_const_structrp png_ptr, png_fixed_point a, png_int_32 times,
  2795. png_int_32 divisor)
  2796. {
  2797. png_fixed_point result;
  2798. if (png_muldiv(&result, a, times, divisor))
  2799. return result;
  2800. png_warning(png_ptr, "fixed point overflow ignored");
  2801. return 0;
  2802. }
  2803. #endif
  2804. #ifdef PNG_GAMMA_SUPPORTED /* more fixed point functions for gamma */
  2805. /* Calculate a reciprocal, return 0 on div-by-zero or overflow. */
  2806. png_fixed_point
  2807. png_reciprocal(png_fixed_point a)
  2808. {
  2809. #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
  2810. double r = floor(1E10/a+.5);
  2811. if (r <= 2147483647. && r >= -2147483648.)
  2812. return (png_fixed_point)r;
  2813. #else
  2814. png_fixed_point res;
  2815. if (png_muldiv(&res, 100000, 100000, a))
  2816. return res;
  2817. #endif
  2818. return 0; /* error/overflow */
  2819. }
  2820. /* This is the shared test on whether a gamma value is 'significant' - whether
  2821. * it is worth doing gamma correction.
  2822. */
  2823. int /* PRIVATE */
  2824. png_gamma_significant(png_fixed_point gamma_val)
  2825. {
  2826. return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED ||
  2827. gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED;
  2828. }
  2829. #endif
  2830. #ifdef PNG_READ_GAMMA_SUPPORTED
  2831. /* A local convenience routine. */
  2832. static png_fixed_point
  2833. png_product2(png_fixed_point a, png_fixed_point b)
  2834. {
  2835. /* The required result is 1/a * 1/b; the following preserves accuracy. */
  2836. #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
  2837. double r = a * 1E-5;
  2838. r *= b;
  2839. r = floor(r+.5);
  2840. if (r <= 2147483647. && r >= -2147483648.)
  2841. return (png_fixed_point)r;
  2842. #else
  2843. png_fixed_point res;
  2844. if (png_muldiv(&res, a, b, 100000))
  2845. return res;
  2846. #endif
  2847. return 0; /* overflow */
  2848. }
  2849. /* The inverse of the above. */
  2850. png_fixed_point
  2851. png_reciprocal2(png_fixed_point a, png_fixed_point b)
  2852. {
  2853. /* The required result is 1/a * 1/b; the following preserves accuracy. */
  2854. #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
  2855. double r = 1E15/a;
  2856. r /= b;
  2857. r = floor(r+.5);
  2858. if (r <= 2147483647. && r >= -2147483648.)
  2859. return (png_fixed_point)r;
  2860. #else
  2861. /* This may overflow because the range of png_fixed_point isn't symmetric,
  2862. * but this API is only used for the product of file and screen gamma so it
  2863. * doesn't matter that the smallest number it can produce is 1/21474, not
  2864. * 1/100000
  2865. */
  2866. png_fixed_point res = png_product2(a, b);
  2867. if (res != 0)
  2868. return png_reciprocal(res);
  2869. #endif
  2870. return 0; /* overflow */
  2871. }
  2872. #endif /* READ_GAMMA */
  2873. #ifdef PNG_READ_GAMMA_SUPPORTED /* gamma table code */
  2874. #ifndef PNG_FLOATING_ARITHMETIC_SUPPORTED
  2875. /* Fixed point gamma.
  2876. *
  2877. * The code to calculate the tables used below can be found in the shell script
  2878. * contrib/tools/intgamma.sh
  2879. *
  2880. * To calculate gamma this code implements fast log() and exp() calls using only
  2881. * fixed point arithmetic. This code has sufficient precision for either 8-bit
  2882. * or 16-bit sample values.
  2883. *
  2884. * The tables used here were calculated using simple 'bc' programs, but C double
  2885. * precision floating point arithmetic would work fine.
  2886. *
  2887. * 8-bit log table
  2888. * This is a table of -log(value/255)/log(2) for 'value' in the range 128 to
  2889. * 255, so it's the base 2 logarithm of a normalized 8-bit floating point
  2890. * mantissa. The numbers are 32-bit fractions.
  2891. */
  2892. static const png_uint_32
  2893. png_8bit_l2[128] =
  2894. {
  2895. 4270715492U, 4222494797U, 4174646467U, 4127164793U, 4080044201U, 4033279239U,
  2896. 3986864580U, 3940795015U, 3895065449U, 3849670902U, 3804606499U, 3759867474U,
  2897. 3715449162U, 3671346997U, 3627556511U, 3584073329U, 3540893168U, 3498011834U,
  2898. 3455425220U, 3413129301U, 3371120137U, 3329393864U, 3287946700U, 3246774933U,
  2899. 3205874930U, 3165243125U, 3124876025U, 3084770202U, 3044922296U, 3005329011U,
  2900. 2965987113U, 2926893432U, 2888044853U, 2849438323U, 2811070844U, 2772939474U,
  2901. 2735041326U, 2697373562U, 2659933400U, 2622718104U, 2585724991U, 2548951424U,
  2902. 2512394810U, 2476052606U, 2439922311U, 2404001468U, 2368287663U, 2332778523U,
  2903. 2297471715U, 2262364947U, 2227455964U, 2192742551U, 2158222529U, 2123893754U,
  2904. 2089754119U, 2055801552U, 2022034013U, 1988449497U, 1955046031U, 1921821672U,
  2905. 1888774511U, 1855902668U, 1823204291U, 1790677560U, 1758320682U, 1726131893U,
  2906. 1694109454U, 1662251657U, 1630556815U, 1599023271U, 1567649391U, 1536433567U,
  2907. 1505374214U, 1474469770U, 1443718700U, 1413119487U, 1382670639U, 1352370686U,
  2908. 1322218179U, 1292211689U, 1262349810U, 1232631153U, 1203054352U, 1173618059U,
  2909. 1144320946U, 1115161701U, 1086139034U, 1057251672U, 1028498358U, 999877854U,
  2910. 971388940U, 943030410U, 914801076U, 886699767U, 858725327U, 830876614U,
  2911. 803152505U, 775551890U, 748073672U, 720716771U, 693480120U, 666362667U,
  2912. 639363374U, 612481215U, 585715177U, 559064263U, 532527486U, 506103872U,
  2913. 479792461U, 453592303U, 427502463U, 401522014U, 375650043U, 349885648U,
  2914. 324227938U, 298676034U, 273229066U, 247886176U, 222646516U, 197509248U,
  2915. 172473545U, 147538590U, 122703574U, 97967701U, 73330182U, 48790236U,
  2916. 24347096U, 0U
  2917. #if 0
  2918. /* The following are the values for 16-bit tables - these work fine for the
  2919. * 8-bit conversions but produce very slightly larger errors in the 16-bit
  2920. * log (about 1.2 as opposed to 0.7 absolute error in the final value). To
  2921. * use these all the shifts below must be adjusted appropriately.
  2922. */
  2923. 65166, 64430, 63700, 62976, 62257, 61543, 60835, 60132, 59434, 58741, 58054,
  2924. 57371, 56693, 56020, 55352, 54689, 54030, 53375, 52726, 52080, 51439, 50803,
  2925. 50170, 49542, 48918, 48298, 47682, 47070, 46462, 45858, 45257, 44661, 44068,
  2926. 43479, 42894, 42312, 41733, 41159, 40587, 40020, 39455, 38894, 38336, 37782,
  2927. 37230, 36682, 36137, 35595, 35057, 34521, 33988, 33459, 32932, 32408, 31887,
  2928. 31369, 30854, 30341, 29832, 29325, 28820, 28319, 27820, 27324, 26830, 26339,
  2929. 25850, 25364, 24880, 24399, 23920, 23444, 22970, 22499, 22029, 21562, 21098,
  2930. 20636, 20175, 19718, 19262, 18808, 18357, 17908, 17461, 17016, 16573, 16132,
  2931. 15694, 15257, 14822, 14390, 13959, 13530, 13103, 12678, 12255, 11834, 11415,
  2932. 10997, 10582, 10168, 9756, 9346, 8937, 8531, 8126, 7723, 7321, 6921, 6523,
  2933. 6127, 5732, 5339, 4947, 4557, 4169, 3782, 3397, 3014, 2632, 2251, 1872, 1495,
  2934. 1119, 744, 372
  2935. #endif
  2936. };
  2937. static png_int_32
  2938. png_log8bit(unsigned int x)
  2939. {
  2940. unsigned int lg2 = 0;
  2941. /* Each time 'x' is multiplied by 2, 1 must be subtracted off the final log,
  2942. * because the log is actually negate that means adding 1. The final
  2943. * returned value thus has the range 0 (for 255 input) to 7.994 (for 1
  2944. * input), return -1 for the overflow (log 0) case, - so the result is
  2945. * always at most 19 bits.
  2946. */
  2947. if ((x &= 0xff) == 0)
  2948. return -1;
  2949. if ((x & 0xf0) == 0)
  2950. lg2 = 4, x <<= 4;
  2951. if ((x & 0xc0) == 0)
  2952. lg2 += 2, x <<= 2;
  2953. if ((x & 0x80) == 0)
  2954. lg2 += 1, x <<= 1;
  2955. /* result is at most 19 bits, so this cast is safe: */
  2956. return (png_int_32)((lg2 << 16) + ((png_8bit_l2[x-128]+32768)>>16));
  2957. }
  2958. /* The above gives exact (to 16 binary places) log2 values for 8-bit images,
  2959. * for 16-bit images we use the most significant 8 bits of the 16-bit value to
  2960. * get an approximation then multiply the approximation by a correction factor
  2961. * determined by the remaining up to 8 bits. This requires an additional step
  2962. * in the 16-bit case.
  2963. *
  2964. * We want log2(value/65535), we have log2(v'/255), where:
  2965. *
  2966. * value = v' * 256 + v''
  2967. * = v' * f
  2968. *
  2969. * So f is value/v', which is equal to (256+v''/v') since v' is in the range 128
  2970. * to 255 and v'' is in the range 0 to 255 f will be in the range 256 to less
  2971. * than 258. The final factor also needs to correct for the fact that our 8-bit
  2972. * value is scaled by 255, whereas the 16-bit values must be scaled by 65535.
  2973. *
  2974. * This gives a final formula using a calculated value 'x' which is value/v' and
  2975. * scaling by 65536 to match the above table:
  2976. *
  2977. * log2(x/257) * 65536
  2978. *
  2979. * Since these numbers are so close to '1' we can use simple linear
  2980. * interpolation between the two end values 256/257 (result -368.61) and 258/257
  2981. * (result 367.179). The values used below are scaled by a further 64 to give
  2982. * 16-bit precision in the interpolation:
  2983. *
  2984. * Start (256): -23591
  2985. * Zero (257): 0
  2986. * End (258): 23499
  2987. */
  2988. static png_int_32
  2989. png_log16bit(png_uint_32 x)
  2990. {
  2991. unsigned int lg2 = 0;
  2992. /* As above, but now the input has 16 bits. */
  2993. if ((x &= 0xffff) == 0)
  2994. return -1;
  2995. if ((x & 0xff00) == 0)
  2996. lg2 = 8, x <<= 8;
  2997. if ((x & 0xf000) == 0)
  2998. lg2 += 4, x <<= 4;
  2999. if ((x & 0xc000) == 0)
  3000. lg2 += 2, x <<= 2;
  3001. if ((x & 0x8000) == 0)
  3002. lg2 += 1, x <<= 1;
  3003. /* Calculate the base logarithm from the top 8 bits as a 28-bit fractional
  3004. * value.
  3005. */
  3006. lg2 <<= 28;
  3007. lg2 += (png_8bit_l2[(x>>8)-128]+8) >> 4;
  3008. /* Now we need to interpolate the factor, this requires a division by the top
  3009. * 8 bits. Do this with maximum precision.
  3010. */
  3011. x = ((x << 16) + (x >> 9)) / (x >> 8);
  3012. /* Since we divided by the top 8 bits of 'x' there will be a '1' at 1<<24,
  3013. * the value at 1<<16 (ignoring this) will be 0 or 1; this gives us exactly
  3014. * 16 bits to interpolate to get the low bits of the result. Round the
  3015. * answer. Note that the end point values are scaled by 64 to retain overall
  3016. * precision and that 'lg2' is current scaled by an extra 12 bits, so adjust
  3017. * the overall scaling by 6-12. Round at every step.
  3018. */
  3019. x -= 1U << 24;
  3020. if (x <= 65536U) /* <= '257' */
  3021. lg2 += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12);
  3022. else
  3023. lg2 -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12);
  3024. /* Safe, because the result can't have more than 20 bits: */
  3025. return (png_int_32)((lg2 + 2048) >> 12);
  3026. }
  3027. /* The 'exp()' case must invert the above, taking a 20-bit fixed point
  3028. * logarithmic value and returning a 16 or 8-bit number as appropriate. In
  3029. * each case only the low 16 bits are relevant - the fraction - since the
  3030. * integer bits (the top 4) simply determine a shift.
  3031. *
  3032. * The worst case is the 16-bit distinction between 65535 and 65534, this
  3033. * requires perhaps spurious accuracty in the decoding of the logarithm to
  3034. * distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance
  3035. * of getting this accuracy in practice.
  3036. *
  3037. * To deal with this the following exp() function works out the exponent of the
  3038. * frational part of the logarithm by using an accurate 32-bit value from the
  3039. * top four fractional bits then multiplying in the remaining bits.
  3040. */
  3041. static const png_uint_32
  3042. png_32bit_exp[16] =
  3043. {
  3044. /* NOTE: the first entry is deliberately set to the maximum 32-bit value. */
  3045. 4294967295U, 4112874773U, 3938502376U, 3771522796U, 3611622603U, 3458501653U,
  3046. 3311872529U, 3171459999U, 3037000500U, 2908241642U, 2784941738U, 2666869345U,
  3047. 2553802834U, 2445529972U, 2341847524U, 2242560872U
  3048. };
  3049. /* Adjustment table; provided to explain the numbers in the code below. */
  3050. #if 0
  3051. for (i=11;i>=0;--i){ print i, " ", (1 - e(-(2^i)/65536*l(2))) * 2^(32-i), "\n"}
  3052. 11 44937.64284865548751208448
  3053. 10 45180.98734845585101160448
  3054. 9 45303.31936980687359311872
  3055. 8 45364.65110595323018870784
  3056. 7 45395.35850361789624614912
  3057. 6 45410.72259715102037508096
  3058. 5 45418.40724413220722311168
  3059. 4 45422.25021786898173001728
  3060. 3 45424.17186732298419044352
  3061. 2 45425.13273269940811464704
  3062. 1 45425.61317555035558641664
  3063. 0 45425.85339951654943850496
  3064. #endif
  3065. static png_uint_32
  3066. png_exp(png_fixed_point x)
  3067. {
  3068. if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */
  3069. {
  3070. /* Obtain a 4-bit approximation */
  3071. png_uint_32 e = png_32bit_exp[(x >> 12) & 0xf];
  3072. /* Incorporate the low 12 bits - these decrease the returned value by
  3073. * multiplying by a number less than 1 if the bit is set. The multiplier
  3074. * is determined by the above table and the shift. Notice that the values
  3075. * converge on 45426 and this is used to allow linear interpolation of the
  3076. * low bits.
  3077. */
  3078. if (x & 0x800)
  3079. e -= (((e >> 16) * 44938U) + 16U) >> 5;
  3080. if (x & 0x400)
  3081. e -= (((e >> 16) * 45181U) + 32U) >> 6;
  3082. if (x & 0x200)
  3083. e -= (((e >> 16) * 45303U) + 64U) >> 7;
  3084. if (x & 0x100)
  3085. e -= (((e >> 16) * 45365U) + 128U) >> 8;
  3086. if (x & 0x080)
  3087. e -= (((e >> 16) * 45395U) + 256U) >> 9;
  3088. if (x & 0x040)
  3089. e -= (((e >> 16) * 45410U) + 512U) >> 10;
  3090. /* And handle the low 6 bits in a single block. */
  3091. e -= (((e >> 16) * 355U * (x & 0x3fU)) + 256U) >> 9;
  3092. /* Handle the upper bits of x. */
  3093. e >>= x >> 16;
  3094. return e;
  3095. }
  3096. /* Check for overflow */
  3097. if (x <= 0)
  3098. return png_32bit_exp[0];
  3099. /* Else underflow */
  3100. return 0;
  3101. }
  3102. static png_byte
  3103. png_exp8bit(png_fixed_point lg2)
  3104. {
  3105. /* Get a 32-bit value: */
  3106. png_uint_32 x = png_exp(lg2);
  3107. /* Convert the 32-bit value to 0..255 by multiplying by 256-1, note that the
  3108. * second, rounding, step can't overflow because of the first, subtraction,
  3109. * step.
  3110. */
  3111. x -= x >> 8;
  3112. return (png_byte)((x + 0x7fffffU) >> 24);
  3113. }
  3114. static png_uint_16
  3115. png_exp16bit(png_fixed_point lg2)
  3116. {
  3117. /* Get a 32-bit value: */
  3118. png_uint_32 x = png_exp(lg2);
  3119. /* Convert the 32-bit value to 0..65535 by multiplying by 65536-1: */
  3120. x -= x >> 16;
  3121. return (png_uint_16)((x + 32767U) >> 16);
  3122. }
  3123. #endif /* FLOATING_ARITHMETIC */
  3124. png_byte
  3125. png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val)
  3126. {
  3127. if (value > 0 && value < 255)
  3128. {
  3129. # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
  3130. double r = floor(255*pow(value/255.,gamma_val*.00001)+.5);
  3131. return (png_byte)r;
  3132. # else
  3133. png_int_32 lg2 = png_log8bit(value);
  3134. png_fixed_point res;
  3135. if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1))
  3136. return png_exp8bit(res);
  3137. /* Overflow. */
  3138. value = 0;
  3139. # endif
  3140. }
  3141. return (png_byte)value;
  3142. }
  3143. png_uint_16
  3144. png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val)
  3145. {
  3146. if (value > 0 && value < 65535)
  3147. {
  3148. # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
  3149. double r = floor(65535*pow(value/65535.,gamma_val*.00001)+.5);
  3150. return (png_uint_16)r;
  3151. # else
  3152. png_int_32 lg2 = png_log16bit(value);
  3153. png_fixed_point res;
  3154. if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1))
  3155. return png_exp16bit(res);
  3156. /* Overflow. */
  3157. value = 0;
  3158. # endif
  3159. }
  3160. return (png_uint_16)value;
  3161. }
  3162. /* This does the right thing based on the bit_depth field of the
  3163. * png_struct, interpreting values as 8-bit or 16-bit. While the result
  3164. * is nominally a 16-bit value if bit depth is 8 then the result is
  3165. * 8-bit (as are the arguments.)
  3166. */
  3167. png_uint_16 /* PRIVATE */
  3168. png_gamma_correct(png_structrp png_ptr, unsigned int value,
  3169. png_fixed_point gamma_val)
  3170. {
  3171. if (png_ptr->bit_depth == 8)
  3172. return png_gamma_8bit_correct(value, gamma_val);
  3173. else
  3174. return png_gamma_16bit_correct(value, gamma_val);
  3175. }
  3176. /* Internal function to build a single 16-bit table - the table consists of
  3177. * 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount
  3178. * to shift the input values right (or 16-number_of_signifiant_bits).
  3179. *
  3180. * The caller is responsible for ensuring that the table gets cleaned up on
  3181. * png_error (i.e. if one of the mallocs below fails) - i.e. the *table argument
  3182. * should be somewhere that will be cleaned.
  3183. */
  3184. static void
  3185. png_build_16bit_table(png_structrp png_ptr, png_uint_16pp *ptable,
  3186. PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val)
  3187. {
  3188. /* Various values derived from 'shift': */
  3189. PNG_CONST unsigned int num = 1U << (8U - shift);
  3190. PNG_CONST unsigned int max = (1U << (16U - shift))-1U;
  3191. PNG_CONST unsigned int max_by_2 = 1U << (15U-shift);
  3192. unsigned int i;
  3193. png_uint_16pp table = *ptable =
  3194. (png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p)));
  3195. for (i = 0; i < num; i++)
  3196. {
  3197. png_uint_16p sub_table = table[i] =
  3198. (png_uint_16p)png_malloc(png_ptr, 256 * (sizeof (png_uint_16)));
  3199. /* The 'threshold' test is repeated here because it can arise for one of
  3200. * the 16-bit tables even if the others don't hit it.
  3201. */
  3202. if (png_gamma_significant(gamma_val))
  3203. {
  3204. /* The old code would overflow at the end and this would cause the
  3205. * 'pow' function to return a result >1, resulting in an
  3206. * arithmetic error. This code follows the spec exactly; ig is
  3207. * the recovered input sample, it always has 8-16 bits.
  3208. *
  3209. * We want input * 65535/max, rounded, the arithmetic fits in 32
  3210. * bits (unsigned) so long as max <= 32767.
  3211. */
  3212. unsigned int j;
  3213. for (j = 0; j < 256; j++)
  3214. {
  3215. png_uint_32 ig = (j << (8-shift)) + i;
  3216. # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
  3217. /* Inline the 'max' scaling operation: */
  3218. double d = floor(65535*pow(ig/(double)max, gamma_val*.00001)+.5);
  3219. sub_table[j] = (png_uint_16)d;
  3220. # else
  3221. if (shift)
  3222. ig = (ig * 65535U + max_by_2)/max;
  3223. sub_table[j] = png_gamma_16bit_correct(ig, gamma_val);
  3224. # endif
  3225. }
  3226. }
  3227. else
  3228. {
  3229. /* We must still build a table, but do it the fast way. */
  3230. unsigned int j;
  3231. for (j = 0; j < 256; j++)
  3232. {
  3233. png_uint_32 ig = (j << (8-shift)) + i;
  3234. if (shift)
  3235. ig = (ig * 65535U + max_by_2)/max;
  3236. sub_table[j] = (png_uint_16)ig;
  3237. }
  3238. }
  3239. }
  3240. }
  3241. /* NOTE: this function expects the *inverse* of the overall gamma transformation
  3242. * required.
  3243. */
  3244. static void
  3245. png_build_16to8_table(png_structrp png_ptr, png_uint_16pp *ptable,
  3246. PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val)
  3247. {
  3248. PNG_CONST unsigned int num = 1U << (8U - shift);
  3249. PNG_CONST unsigned int max = (1U << (16U - shift))-1U;
  3250. unsigned int i;
  3251. png_uint_32 last;
  3252. png_uint_16pp table = *ptable =
  3253. (png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p)));
  3254. /* 'num' is the number of tables and also the number of low bits of low
  3255. * bits of the input 16-bit value used to select a table. Each table is
  3256. * itself index by the high 8 bits of the value.
  3257. */
  3258. for (i = 0; i < num; i++)
  3259. table[i] = (png_uint_16p)png_malloc(png_ptr,
  3260. 256 * (sizeof (png_uint_16)));
  3261. /* 'gamma_val' is set to the reciprocal of the value calculated above, so
  3262. * pow(out,g) is an *input* value. 'last' is the last input value set.
  3263. *
  3264. * In the loop 'i' is used to find output values. Since the output is
  3265. * 8-bit there are only 256 possible values. The tables are set up to
  3266. * select the closest possible output value for each input by finding
  3267. * the input value at the boundary between each pair of output values
  3268. * and filling the table up to that boundary with the lower output
  3269. * value.
  3270. *
  3271. * The boundary values are 0.5,1.5..253.5,254.5. Since these are 9-bit
  3272. * values the code below uses a 16-bit value in i; the values start at
  3273. * 128.5 (for 0.5) and step by 257, for a total of 254 values (the last
  3274. * entries are filled with 255). Start i at 128 and fill all 'last'
  3275. * table entries <= 'max'
  3276. */
  3277. last = 0;
  3278. for (i = 0; i < 255; ++i) /* 8-bit output value */
  3279. {
  3280. /* Find the corresponding maximum input value */
  3281. png_uint_16 out = (png_uint_16)(i * 257U); /* 16-bit output value */
  3282. /* Find the boundary value in 16 bits: */
  3283. png_uint_32 bound = png_gamma_16bit_correct(out+128U, gamma_val);
  3284. /* Adjust (round) to (16-shift) bits: */
  3285. bound = (bound * max + 32768U)/65535U + 1U;
  3286. while (last < bound)
  3287. {
  3288. table[last & (0xffU >> shift)][last >> (8U - shift)] = out;
  3289. last++;
  3290. }
  3291. }
  3292. /* And fill in the final entries. */
  3293. while (last < (num << 8))
  3294. {
  3295. table[last & (0xff >> shift)][last >> (8U - shift)] = 65535U;
  3296. last++;
  3297. }
  3298. }
  3299. /* Build a single 8-bit table: same as the 16-bit case but much simpler (and
  3300. * typically much faster). Note that libpng currently does no sBIT processing
  3301. * (apparently contrary to the spec) so a 256 entry table is always generated.
  3302. */
  3303. static void
  3304. png_build_8bit_table(png_structrp png_ptr, png_bytepp ptable,
  3305. PNG_CONST png_fixed_point gamma_val)
  3306. {
  3307. unsigned int i;
  3308. png_bytep table = *ptable = (png_bytep)png_malloc(png_ptr, 256);
  3309. if (png_gamma_significant(gamma_val)) for (i=0; i<256; i++)
  3310. table[i] = png_gamma_8bit_correct(i, gamma_val);
  3311. else for (i=0; i<256; ++i)
  3312. table[i] = (png_byte)i;
  3313. }
  3314. /* Used from png_read_destroy and below to release the memory used by the gamma
  3315. * tables.
  3316. */
  3317. void /* PRIVATE */
  3318. png_destroy_gamma_table(png_structrp png_ptr)
  3319. {
  3320. png_free(png_ptr, png_ptr->gamma_table);
  3321. png_ptr->gamma_table = NULL;
  3322. if (png_ptr->gamma_16_table != NULL)
  3323. {
  3324. int i;
  3325. int istop = (1 << (8 - png_ptr->gamma_shift));
  3326. for (i = 0; i < istop; i++)
  3327. {
  3328. png_free(png_ptr, png_ptr->gamma_16_table[i]);
  3329. }
  3330. png_free(png_ptr, png_ptr->gamma_16_table);
  3331. png_ptr->gamma_16_table = NULL;
  3332. }
  3333. #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
  3334. defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
  3335. defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  3336. png_free(png_ptr, png_ptr->gamma_from_1);
  3337. png_ptr->gamma_from_1 = NULL;
  3338. png_free(png_ptr, png_ptr->gamma_to_1);
  3339. png_ptr->gamma_to_1 = NULL;
  3340. if (png_ptr->gamma_16_from_1 != NULL)
  3341. {
  3342. int i;
  3343. int istop = (1 << (8 - png_ptr->gamma_shift));
  3344. for (i = 0; i < istop; i++)
  3345. {
  3346. png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
  3347. }
  3348. png_free(png_ptr, png_ptr->gamma_16_from_1);
  3349. png_ptr->gamma_16_from_1 = NULL;
  3350. }
  3351. if (png_ptr->gamma_16_to_1 != NULL)
  3352. {
  3353. int i;
  3354. int istop = (1 << (8 - png_ptr->gamma_shift));
  3355. for (i = 0; i < istop; i++)
  3356. {
  3357. png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
  3358. }
  3359. png_free(png_ptr, png_ptr->gamma_16_to_1);
  3360. png_ptr->gamma_16_to_1 = NULL;
  3361. }
  3362. #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
  3363. }
  3364. /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit
  3365. * tables, we don't make a full table if we are reducing to 8-bit in
  3366. * the future. Note also how the gamma_16 tables are segmented so that
  3367. * we don't need to allocate > 64K chunks for a full 16-bit table.
  3368. */
  3369. void /* PRIVATE */
  3370. png_build_gamma_table(png_structrp png_ptr, int bit_depth)
  3371. {
  3372. png_debug(1, "in png_build_gamma_table");
  3373. /* Remove any existing table; this copes with multiple calls to
  3374. * png_read_update_info. The warning is because building the gamma tables
  3375. * multiple times is a performance hit - it's harmless but the ability to call
  3376. * png_read_update_info() multiple times is new in 1.5.6 so it seems sensible
  3377. * to warn if the app introduces such a hit.
  3378. */
  3379. if (png_ptr->gamma_table != NULL || png_ptr->gamma_16_table != NULL)
  3380. {
  3381. png_warning(png_ptr, "gamma table being rebuilt");
  3382. png_destroy_gamma_table(png_ptr);
  3383. }
  3384. if (bit_depth <= 8)
  3385. {
  3386. png_build_8bit_table(png_ptr, &png_ptr->gamma_table,
  3387. png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma,
  3388. png_ptr->screen_gamma) : PNG_FP_1);
  3389. #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
  3390. defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
  3391. defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  3392. if (png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY))
  3393. {
  3394. png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1,
  3395. png_reciprocal(png_ptr->colorspace.gamma));
  3396. png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1,
  3397. png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
  3398. png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
  3399. }
  3400. #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
  3401. }
  3402. else
  3403. {
  3404. png_byte shift, sig_bit;
  3405. if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  3406. {
  3407. sig_bit = png_ptr->sig_bit.red;
  3408. if (png_ptr->sig_bit.green > sig_bit)
  3409. sig_bit = png_ptr->sig_bit.green;
  3410. if (png_ptr->sig_bit.blue > sig_bit)
  3411. sig_bit = png_ptr->sig_bit.blue;
  3412. }
  3413. else
  3414. sig_bit = png_ptr->sig_bit.gray;
  3415. /* 16-bit gamma code uses this equation:
  3416. *
  3417. * ov = table[(iv & 0xff) >> gamma_shift][iv >> 8]
  3418. *
  3419. * Where 'iv' is the input color value and 'ov' is the output value -
  3420. * pow(iv, gamma).
  3421. *
  3422. * Thus the gamma table consists of up to 256 256 entry tables. The table
  3423. * is selected by the (8-gamma_shift) most significant of the low 8 bits of
  3424. * the color value then indexed by the upper 8 bits:
  3425. *
  3426. * table[low bits][high 8 bits]
  3427. *
  3428. * So the table 'n' corresponds to all those 'iv' of:
  3429. *
  3430. * <all high 8-bit values><n << gamma_shift>..<(n+1 << gamma_shift)-1>
  3431. *
  3432. */
  3433. if (sig_bit > 0 && sig_bit < 16U)
  3434. shift = (png_byte)(16U - sig_bit); /* shift == insignificant bits */
  3435. else
  3436. shift = 0; /* keep all 16 bits */
  3437. if (png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8))
  3438. {
  3439. /* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively
  3440. * the significant bits in the *input* when the output will
  3441. * eventually be 8 bits. By default it is 11.
  3442. */
  3443. if (shift < (16U - PNG_MAX_GAMMA_8))
  3444. shift = (16U - PNG_MAX_GAMMA_8);
  3445. }
  3446. if (shift > 8U)
  3447. shift = 8U; /* Guarantees at least one table! */
  3448. png_ptr->gamma_shift = shift;
  3449. #ifdef PNG_16BIT_SUPPORTED
  3450. /* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now
  3451. * PNG_COMPOSE). This effectively smashed the background calculation for
  3452. * 16-bit output because the 8-bit table assumes the result will be reduced
  3453. * to 8 bits.
  3454. */
  3455. if (png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8))
  3456. #endif
  3457. png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift,
  3458. png_ptr->screen_gamma > 0 ? png_product2(png_ptr->colorspace.gamma,
  3459. png_ptr->screen_gamma) : PNG_FP_1);
  3460. #ifdef PNG_16BIT_SUPPORTED
  3461. else
  3462. png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift,
  3463. png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma,
  3464. png_ptr->screen_gamma) : PNG_FP_1);
  3465. #endif
  3466. #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
  3467. defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
  3468. defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  3469. if (png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY))
  3470. {
  3471. png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift,
  3472. png_reciprocal(png_ptr->colorspace.gamma));
  3473. /* Notice that the '16 from 1' table should be full precision, however
  3474. * the lookup on this table still uses gamma_shift, so it can't be.
  3475. * TODO: fix this.
  3476. */
  3477. png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift,
  3478. png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
  3479. png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
  3480. }
  3481. #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
  3482. }
  3483. }
  3484. #endif /* READ_GAMMA */
  3485. /* HARDWARE OPTION SUPPORT */
  3486. #ifdef PNG_SET_OPTION_SUPPORTED
  3487. int PNGAPI
  3488. png_set_option(png_structrp png_ptr, int option, int onoff)
  3489. {
  3490. if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT &&
  3491. (option & 1) == 0)
  3492. {
  3493. int mask = 3 << option;
  3494. int setting = (2 + (onoff != 0)) << option;
  3495. int current = png_ptr->options;
  3496. png_ptr->options = (png_byte)((current & ~mask) | setting);
  3497. return (current & mask) >> option;
  3498. }
  3499. return PNG_OPTION_INVALID;
  3500. }
  3501. #endif
  3502. /* sRGB support */
  3503. #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
  3504. defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
  3505. /* sRGB conversion tables; these are machine generated with the code in
  3506. * contrib/tools/makesRGB.c. The actual sRGB transfer curve defined in the
  3507. * specification (see the article at http://en.wikipedia.org/wiki/SRGB)
  3508. * is used, not the gamma=1/2.2 approximation use elsewhere in libpng.
  3509. * The sRGB to linear table is exact (to the nearest 16 bit linear fraction).
  3510. * The inverse (linear to sRGB) table has accuracies as follows:
  3511. *
  3512. * For all possible (255*65535+1) input values:
  3513. *
  3514. * error: -0.515566 - 0.625971, 79441 (0.475369%) of readings inexact
  3515. *
  3516. * For the input values corresponding to the 65536 16-bit values:
  3517. *
  3518. * error: -0.513727 - 0.607759, 308 (0.469978%) of readings inexact
  3519. *
  3520. * In all cases the inexact readings are off by one.
  3521. */
  3522. #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
  3523. /* The convert-to-sRGB table is only currently required for read. */
  3524. const png_uint_16 png_sRGB_table[256] =
  3525. {
  3526. 0,20,40,60,80,99,119,139,
  3527. 159,179,199,219,241,264,288,313,
  3528. 340,367,396,427,458,491,526,562,
  3529. 599,637,677,718,761,805,851,898,
  3530. 947,997,1048,1101,1156,1212,1270,1330,
  3531. 1391,1453,1517,1583,1651,1720,1790,1863,
  3532. 1937,2013,2090,2170,2250,2333,2418,2504,
  3533. 2592,2681,2773,2866,2961,3058,3157,3258,
  3534. 3360,3464,3570,3678,3788,3900,4014,4129,
  3535. 4247,4366,4488,4611,4736,4864,4993,5124,
  3536. 5257,5392,5530,5669,5810,5953,6099,6246,
  3537. 6395,6547,6700,6856,7014,7174,7335,7500,
  3538. 7666,7834,8004,8177,8352,8528,8708,8889,
  3539. 9072,9258,9445,9635,9828,10022,10219,10417,
  3540. 10619,10822,11028,11235,11446,11658,11873,12090,
  3541. 12309,12530,12754,12980,13209,13440,13673,13909,
  3542. 14146,14387,14629,14874,15122,15371,15623,15878,
  3543. 16135,16394,16656,16920,17187,17456,17727,18001,
  3544. 18277,18556,18837,19121,19407,19696,19987,20281,
  3545. 20577,20876,21177,21481,21787,22096,22407,22721,
  3546. 23038,23357,23678,24002,24329,24658,24990,25325,
  3547. 25662,26001,26344,26688,27036,27386,27739,28094,
  3548. 28452,28813,29176,29542,29911,30282,30656,31033,
  3549. 31412,31794,32179,32567,32957,33350,33745,34143,
  3550. 34544,34948,35355,35764,36176,36591,37008,37429,
  3551. 37852,38278,38706,39138,39572,40009,40449,40891,
  3552. 41337,41785,42236,42690,43147,43606,44069,44534,
  3553. 45002,45473,45947,46423,46903,47385,47871,48359,
  3554. 48850,49344,49841,50341,50844,51349,51858,52369,
  3555. 52884,53401,53921,54445,54971,55500,56032,56567,
  3556. 57105,57646,58190,58737,59287,59840,60396,60955,
  3557. 61517,62082,62650,63221,63795,64372,64952,65535
  3558. };
  3559. #endif /* simplified read only */
  3560. /* The base/delta tables are required for both read and write (but currently
  3561. * only the simplified versions.)
  3562. */
  3563. const png_uint_16 png_sRGB_base[512] =
  3564. {
  3565. 128,1782,3383,4644,5675,6564,7357,8074,
  3566. 8732,9346,9921,10463,10977,11466,11935,12384,
  3567. 12816,13233,13634,14024,14402,14769,15125,15473,
  3568. 15812,16142,16466,16781,17090,17393,17690,17981,
  3569. 18266,18546,18822,19093,19359,19621,19879,20133,
  3570. 20383,20630,20873,21113,21349,21583,21813,22041,
  3571. 22265,22487,22707,22923,23138,23350,23559,23767,
  3572. 23972,24175,24376,24575,24772,24967,25160,25352,
  3573. 25542,25730,25916,26101,26284,26465,26645,26823,
  3574. 27000,27176,27350,27523,27695,27865,28034,28201,
  3575. 28368,28533,28697,28860,29021,29182,29341,29500,
  3576. 29657,29813,29969,30123,30276,30429,30580,30730,
  3577. 30880,31028,31176,31323,31469,31614,31758,31902,
  3578. 32045,32186,32327,32468,32607,32746,32884,33021,
  3579. 33158,33294,33429,33564,33697,33831,33963,34095,
  3580. 34226,34357,34486,34616,34744,34873,35000,35127,
  3581. 35253,35379,35504,35629,35753,35876,35999,36122,
  3582. 36244,36365,36486,36606,36726,36845,36964,37083,
  3583. 37201,37318,37435,37551,37668,37783,37898,38013,
  3584. 38127,38241,38354,38467,38580,38692,38803,38915,
  3585. 39026,39136,39246,39356,39465,39574,39682,39790,
  3586. 39898,40005,40112,40219,40325,40431,40537,40642,
  3587. 40747,40851,40955,41059,41163,41266,41369,41471,
  3588. 41573,41675,41777,41878,41979,42079,42179,42279,
  3589. 42379,42478,42577,42676,42775,42873,42971,43068,
  3590. 43165,43262,43359,43456,43552,43648,43743,43839,
  3591. 43934,44028,44123,44217,44311,44405,44499,44592,
  3592. 44685,44778,44870,44962,45054,45146,45238,45329,
  3593. 45420,45511,45601,45692,45782,45872,45961,46051,
  3594. 46140,46229,46318,46406,46494,46583,46670,46758,
  3595. 46846,46933,47020,47107,47193,47280,47366,47452,
  3596. 47538,47623,47709,47794,47879,47964,48048,48133,
  3597. 48217,48301,48385,48468,48552,48635,48718,48801,
  3598. 48884,48966,49048,49131,49213,49294,49376,49458,
  3599. 49539,49620,49701,49782,49862,49943,50023,50103,
  3600. 50183,50263,50342,50422,50501,50580,50659,50738,
  3601. 50816,50895,50973,51051,51129,51207,51285,51362,
  3602. 51439,51517,51594,51671,51747,51824,51900,51977,
  3603. 52053,52129,52205,52280,52356,52432,52507,52582,
  3604. 52657,52732,52807,52881,52956,53030,53104,53178,
  3605. 53252,53326,53400,53473,53546,53620,53693,53766,
  3606. 53839,53911,53984,54056,54129,54201,54273,54345,
  3607. 54417,54489,54560,54632,54703,54774,54845,54916,
  3608. 54987,55058,55129,55199,55269,55340,55410,55480,
  3609. 55550,55620,55689,55759,55828,55898,55967,56036,
  3610. 56105,56174,56243,56311,56380,56448,56517,56585,
  3611. 56653,56721,56789,56857,56924,56992,57059,57127,
  3612. 57194,57261,57328,57395,57462,57529,57595,57662,
  3613. 57728,57795,57861,57927,57993,58059,58125,58191,
  3614. 58256,58322,58387,58453,58518,58583,58648,58713,
  3615. 58778,58843,58908,58972,59037,59101,59165,59230,
  3616. 59294,59358,59422,59486,59549,59613,59677,59740,
  3617. 59804,59867,59930,59993,60056,60119,60182,60245,
  3618. 60308,60370,60433,60495,60558,60620,60682,60744,
  3619. 60806,60868,60930,60992,61054,61115,61177,61238,
  3620. 61300,61361,61422,61483,61544,61605,61666,61727,
  3621. 61788,61848,61909,61969,62030,62090,62150,62211,
  3622. 62271,62331,62391,62450,62510,62570,62630,62689,
  3623. 62749,62808,62867,62927,62986,63045,63104,63163,
  3624. 63222,63281,63340,63398,63457,63515,63574,63632,
  3625. 63691,63749,63807,63865,63923,63981,64039,64097,
  3626. 64155,64212,64270,64328,64385,64443,64500,64557,
  3627. 64614,64672,64729,64786,64843,64900,64956,65013,
  3628. 65070,65126,65183,65239,65296,65352,65409,65465
  3629. };
  3630. const png_byte png_sRGB_delta[512] =
  3631. {
  3632. 207,201,158,129,113,100,90,82,77,72,68,64,61,59,56,54,
  3633. 52,50,49,47,46,45,43,42,41,40,39,39,38,37,36,36,
  3634. 35,34,34,33,33,32,32,31,31,30,30,30,29,29,28,28,
  3635. 28,27,27,27,27,26,26,26,25,25,25,25,24,24,24,24,
  3636. 23,23,23,23,23,22,22,22,22,22,22,21,21,21,21,21,
  3637. 21,20,20,20,20,20,20,20,20,19,19,19,19,19,19,19,
  3638. 19,18,18,18,18,18,18,18,18,18,18,17,17,17,17,17,
  3639. 17,17,17,17,17,17,16,16,16,16,16,16,16,16,16,16,
  3640. 16,16,16,16,15,15,15,15,15,15,15,15,15,15,15,15,
  3641. 15,15,15,15,14,14,14,14,14,14,14,14,14,14,14,14,
  3642. 14,14,14,14,14,14,14,13,13,13,13,13,13,13,13,13,
  3643. 13,13,13,13,13,13,13,13,13,13,13,13,13,13,12,12,
  3644. 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
  3645. 12,12,12,12,12,12,12,12,12,12,12,12,11,11,11,11,
  3646. 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
  3647. 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
  3648. 11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
  3649. 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
  3650. 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
  3651. 10,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  3652. 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  3653. 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  3654. 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  3655. 9,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  3656. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  3657. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  3658. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  3659. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  3660. 8,8,8,8,8,8,8,8,8,7,7,7,7,7,7,7,
  3661. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  3662. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  3663. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  3664. };
  3665. #endif /* SIMPLIFIED READ/WRITE sRGB support */
  3666. /* SIMPLIFIED READ/WRITE SUPPORT */
  3667. #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
  3668. defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
  3669. static int
  3670. png_image_free_function(png_voidp argument)
  3671. {
  3672. png_imagep image = png_voidcast(png_imagep, argument);
  3673. png_controlp cp = image->opaque;
  3674. png_control c;
  3675. /* Double check that we have a png_ptr - it should be impossible to get here
  3676. * without one.
  3677. */
  3678. if (cp->png_ptr == NULL)
  3679. return 0;
  3680. /* First free any data held in the control structure. */
  3681. # ifdef PNG_STDIO_SUPPORTED
  3682. if (cp->owned_file)
  3683. {
  3684. FILE *fp = png_voidcast(FILE*, cp->png_ptr->io_ptr);
  3685. cp->owned_file = 0;
  3686. /* Ignore errors here. */
  3687. if (fp != NULL)
  3688. {
  3689. cp->png_ptr->io_ptr = NULL;
  3690. (void)fclose(fp);
  3691. }
  3692. }
  3693. # endif
  3694. /* Copy the control structure so that the original, allocated, version can be
  3695. * safely freed. Notice that a png_error here stops the remainder of the
  3696. * cleanup, but this is probably fine because that would indicate bad memory
  3697. * problems anyway.
  3698. */
  3699. c = *cp;
  3700. image->opaque = &c;
  3701. png_free(c.png_ptr, cp);
  3702. /* Then the structures, calling the correct API. */
  3703. if (c.for_write)
  3704. {
  3705. # ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
  3706. png_destroy_write_struct(&c.png_ptr, &c.info_ptr);
  3707. # else
  3708. png_error(c.png_ptr, "simplified write not supported");
  3709. # endif
  3710. }
  3711. else
  3712. {
  3713. # ifdef PNG_SIMPLIFIED_READ_SUPPORTED
  3714. png_destroy_read_struct(&c.png_ptr, &c.info_ptr, NULL);
  3715. # else
  3716. png_error(c.png_ptr, "simplified read not supported");
  3717. # endif
  3718. }
  3719. /* Success. */
  3720. return 1;
  3721. }
  3722. void PNGAPI
  3723. png_image_free(png_imagep image)
  3724. {
  3725. /* Safely call the real function, but only if doing so is safe at this point
  3726. * (if not inside an error handling context). Otherwise assume
  3727. * png_safe_execute will call this API after the return.
  3728. */
  3729. if (image != NULL && image->opaque != NULL &&
  3730. image->opaque->error_buf == NULL)
  3731. {
  3732. /* Ignore errors here: */
  3733. (void)png_safe_execute(image, png_image_free_function, image);
  3734. image->opaque = NULL;
  3735. }
  3736. }
  3737. int /* PRIVATE */
  3738. png_image_error(png_imagep image, png_const_charp error_message)
  3739. {
  3740. /* Utility to log an error. */
  3741. png_safecat(image->message, (sizeof image->message), 0, error_message);
  3742. image->warning_or_error |= PNG_IMAGE_ERROR;
  3743. png_image_free(image);
  3744. return 0;
  3745. }
  3746. #endif /* SIMPLIFIED READ/WRITE */
  3747. #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */