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.

pngread.c 134KB

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