pngstest.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828
  1. /*-
  2. * pngstest.c
  3. *
  4. * Last changed in libpng 1.6.31 [July 27, 2017]
  5. * Copyright (c) 2013-2017 John Cunningham Bowler
  6. *
  7. * This code is released under the libpng license.
  8. * For conditions of distribution and use, see the disclaimer
  9. * and license in png.h
  10. *
  11. * Test for the PNG 'simplified' APIs.
  12. */
  13. #define _ISOC90_SOURCE 1
  14. #define MALLOC_CHECK_ 2/*glibc facility: turn on debugging*/
  15. #include <stddef.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <ctype.h>
  21. #include <math.h>
  22. #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
  23. # include <config.h>
  24. #endif
  25. /* Define the following to use this test against your installed libpng, rather
  26. * than the one being built here:
  27. */
  28. #ifdef PNG_FREESTANDING_TESTS
  29. # include <png.h>
  30. #else
  31. # include "../../png.h"
  32. #endif
  33. /* 1.6.1 added support for the configure test harness, which uses 77 to indicate
  34. * a skipped test, in earlier versions we need to succeed on a skipped test, so:
  35. */
  36. #if PNG_LIBPNG_VER >= 10601 && defined(HAVE_CONFIG_H)
  37. # define SKIP 77
  38. #else
  39. # define SKIP 0
  40. #endif
  41. #ifdef PNG_SIMPLIFIED_READ_SUPPORTED /* Else nothing can be done */
  42. #include "../tools/sRGB.h"
  43. /* KNOWN ISSUES
  44. *
  45. * These defines switch on alternate algorithms for format conversions to match
  46. * the current libpng implementation; they are set to allow pngstest to pass
  47. * even though libpng is producing answers that are not as correct as they
  48. * should be.
  49. */
  50. #define ALLOW_UNUSED_GPC 0
  51. /* If true include unused static GPC functions and declare an external array
  52. * of them to hide the fact that they are unused. This is for development
  53. * use while testing the correct function to use to take into account libpng
  54. * misbehavior, such as using a simple power law to correct sRGB to linear.
  55. */
  56. /* The following is to support direct compilation of this file as C++ */
  57. #ifdef __cplusplus
  58. # define voidcast(type, value) static_cast<type>(value)
  59. # define aligncastconst(type, value) \
  60. static_cast<type>(static_cast<const void*>(value))
  61. #else
  62. # define voidcast(type, value) (value)
  63. # define aligncastconst(type, value) ((const void*)(value))
  64. #endif /* __cplusplus */
  65. /* During parallel runs of pngstest each temporary file needs a unique name,
  66. * this is used to permit uniqueness using a command line argument which can be
  67. * up to 22 characters long.
  68. */
  69. static char tmpf[23] = "TMP";
  70. /* Generate random bytes. This uses a boring repeatable algorithm and it
  71. * is implemented here so that it gives the same set of numbers on every
  72. * architecture. It's a linear congruential generator (Knuth or Sedgewick
  73. * "Algorithms") but it comes from the 'feedback taps' table in Horowitz and
  74. * Hill, "The Art of Electronics".
  75. */
  76. static void
  77. make_random_bytes(png_uint_32* seed, void* pv, size_t size)
  78. {
  79. png_uint_32 u0 = seed[0], u1 = seed[1];
  80. png_bytep bytes = voidcast(png_bytep, pv);
  81. /* There are thirty three bits, the next bit in the sequence is bit-33 XOR
  82. * bit-20. The top 1 bit is in u1, the bottom 32 are in u0.
  83. */
  84. size_t i;
  85. for (i=0; i<size; ++i)
  86. {
  87. /* First generate 8 new bits then shift them in at the end. */
  88. png_uint_32 u = ((u0 >> (20-8)) ^ ((u1 << 7) | (u0 >> (32-7)))) & 0xff;
  89. u1 <<= 8;
  90. u1 |= u0 >> 24;
  91. u0 <<= 8;
  92. u0 |= u;
  93. *bytes++ = (png_byte)u;
  94. }
  95. seed[0] = u0;
  96. seed[1] = u1;
  97. }
  98. static png_uint_32 color_seed[2];
  99. static void
  100. reseed(void)
  101. {
  102. color_seed[0] = 0x12345678U;
  103. color_seed[1] = 0x9abcdefU;
  104. }
  105. static void
  106. random_color(png_colorp color)
  107. {
  108. make_random_bytes(color_seed, color, sizeof *color);
  109. }
  110. /* Math support - neither Cygwin nor Visual Studio have C99 support and we need
  111. * a predictable rounding function, so make one here:
  112. */
  113. static double
  114. closestinteger(double x)
  115. {
  116. return floor(x + .5);
  117. }
  118. /* Cast support: remove GCC whines. */
  119. static png_byte
  120. u8d(double d)
  121. {
  122. d = closestinteger(d);
  123. return (png_byte)d;
  124. }
  125. static png_uint_16
  126. u16d(double d)
  127. {
  128. d = closestinteger(d);
  129. return (png_uint_16)d;
  130. }
  131. /* sRGB support: use exact calculations rounded to the nearest int, see the
  132. * fesetround() call in main(). sRGB_to_d optimizes the 8 to 16-bit conversion.
  133. */
  134. static double sRGB_to_d[256];
  135. static double g22_to_d[256];
  136. static void
  137. init_sRGB_to_d(void)
  138. {
  139. int i;
  140. sRGB_to_d[0] = 0;
  141. for (i=1; i<255; ++i)
  142. sRGB_to_d[i] = linear_from_sRGB(i/255.);
  143. sRGB_to_d[255] = 1;
  144. g22_to_d[0] = 0;
  145. for (i=1; i<255; ++i)
  146. g22_to_d[i] = pow(i/255., 1/.45455);
  147. g22_to_d[255] = 1;
  148. }
  149. static png_byte
  150. sRGB(double linear /*range 0.0 .. 1.0*/)
  151. {
  152. return u8d(255 * sRGB_from_linear(linear));
  153. }
  154. static png_byte
  155. isRGB(int fixed_linear)
  156. {
  157. return sRGB(fixed_linear / 65535.);
  158. }
  159. #if 0 /* not used */
  160. static png_byte
  161. unpremultiply(int component, int alpha)
  162. {
  163. if (alpha <= component)
  164. return 255; /* Arbitrary, but consistent with the libpng code */
  165. else if (alpha >= 65535)
  166. return isRGB(component);
  167. else
  168. return sRGB((double)component / alpha);
  169. }
  170. #endif
  171. static png_uint_16
  172. ilinear(int fixed_srgb)
  173. {
  174. return u16d(65535 * sRGB_to_d[fixed_srgb]);
  175. }
  176. static png_uint_16
  177. ilineara(int fixed_srgb, int alpha)
  178. {
  179. return u16d((257 * alpha) * sRGB_to_d[fixed_srgb]);
  180. }
  181. static png_uint_16
  182. ilinear_g22(int fixed_srgb)
  183. {
  184. return u16d(65535 * g22_to_d[fixed_srgb]);
  185. }
  186. #if ALLOW_UNUSED_GPC
  187. static png_uint_16
  188. ilineara_g22(int fixed_srgb, int alpha)
  189. {
  190. return u16d((257 * alpha) * g22_to_d[fixed_srgb]);
  191. }
  192. #endif
  193. static double
  194. YfromRGBint(int ir, int ig, int ib)
  195. {
  196. double r = ir;
  197. double g = ig;
  198. double b = ib;
  199. return YfromRGB(r, g, b);
  200. }
  201. #if 0 /* unused */
  202. /* The error that results from using a 2.2 power law in place of the correct
  203. * sRGB transform, given an 8-bit value which might be either sRGB or power-law.
  204. */
  205. static int
  206. power_law_error8(int value)
  207. {
  208. if (value > 0 && value < 255)
  209. {
  210. double vd = value / 255.;
  211. double e = fabs(
  212. pow(sRGB_to_d[value], 1/2.2) - sRGB_from_linear(pow(vd, 2.2)));
  213. /* Always allow an extra 1 here for rounding errors */
  214. e = 1+floor(255 * e);
  215. return (int)e;
  216. }
  217. return 0;
  218. }
  219. static int error_in_sRGB_roundtrip = 56; /* by experiment */
  220. static int
  221. power_law_error16(int value)
  222. {
  223. if (value > 0 && value < 65535)
  224. {
  225. /* Round trip the value through an 8-bit representation but using
  226. * non-matching to/from conversions.
  227. */
  228. double vd = value / 65535.;
  229. double e = fabs(
  230. pow(sRGB_from_linear(vd), 2.2) - linear_from_sRGB(pow(vd, 1/2.2)));
  231. /* Always allow an extra 1 here for rounding errors */
  232. e = error_in_sRGB_roundtrip+floor(65535 * e);
  233. return (int)e;
  234. }
  235. return 0;
  236. }
  237. static int
  238. compare_8bit(int v1, int v2, int error_limit, int multiple_algorithms)
  239. {
  240. int e = abs(v1-v2);
  241. int ev1, ev2;
  242. if (e <= error_limit)
  243. return 1;
  244. if (!multiple_algorithms)
  245. return 0;
  246. ev1 = power_law_error8(v1);
  247. if (e <= ev1)
  248. return 1;
  249. ev2 = power_law_error8(v2);
  250. if (e <= ev2)
  251. return 1;
  252. return 0;
  253. }
  254. static int
  255. compare_16bit(int v1, int v2, int error_limit, int multiple_algorithms)
  256. {
  257. int e = abs(v1-v2);
  258. int ev1, ev2;
  259. if (e <= error_limit)
  260. return 1;
  261. /* "multiple_algorithms" in this case means that a color-map has been
  262. * involved somewhere, so we can deduce that the values were forced to 8-bit
  263. * (like the via_linear case for 8-bit.)
  264. */
  265. if (!multiple_algorithms)
  266. return 0;
  267. ev1 = power_law_error16(v1);
  268. if (e <= ev1)
  269. return 1;
  270. ev2 = power_law_error16(v2);
  271. if (e <= ev2)
  272. return 1;
  273. return 0;
  274. }
  275. #endif /* unused */
  276. #define USE_FILE 1 /* else memory */
  277. #define USE_STDIO 2 /* else use file name */
  278. #define STRICT 4 /* fail on warnings too */
  279. #define VERBOSE 8
  280. #define KEEP_TMPFILES 16 /* else delete temporary files */
  281. #define KEEP_GOING 32
  282. #define ACCUMULATE 64
  283. #define FAST_WRITE 128
  284. #define sRGB_16BIT 256
  285. #define NO_RESEED 512 /* do not reseed on each new file */
  286. #define GBG_ERROR 1024 /* do not ignore the gamma+background_rgb_to_gray
  287. * libpng warning. */
  288. static void
  289. print_opts(png_uint_32 opts)
  290. {
  291. if (opts & USE_FILE)
  292. printf(" --file");
  293. if (opts & USE_STDIO)
  294. printf(" --stdio");
  295. if (!(opts & STRICT))
  296. printf(" --nostrict");
  297. if (opts & VERBOSE)
  298. printf(" --verbose");
  299. if (opts & KEEP_TMPFILES)
  300. printf(" --preserve");
  301. if (opts & KEEP_GOING)
  302. printf(" --keep-going");
  303. if (opts & ACCUMULATE)
  304. printf(" --accumulate");
  305. if (!(opts & FAST_WRITE)) /* --fast is currently the default */
  306. printf(" --slow");
  307. if (opts & sRGB_16BIT)
  308. printf(" --sRGB-16bit");
  309. if (opts & NO_RESEED)
  310. printf(" --noreseed");
  311. #if PNG_LIBPNG_VER < 10700 /* else on by default */
  312. if (opts & GBG_ERROR)
  313. printf(" --fault-gbg-warning");
  314. #endif
  315. }
  316. #define FORMAT_NO_CHANGE 0x80000000 /* additional flag */
  317. /* A name table for all the formats - defines the format of the '+' arguments to
  318. * pngstest.
  319. */
  320. #define FORMAT_COUNT 64
  321. #define FORMAT_MASK 0x3f
  322. static const char * const format_names[FORMAT_COUNT] =
  323. {
  324. "sRGB-gray",
  325. "sRGB-gray+alpha",
  326. "sRGB-rgb",
  327. "sRGB-rgb+alpha",
  328. "linear-gray",
  329. "linear-gray+alpha",
  330. "linear-rgb",
  331. "linear-rgb+alpha",
  332. "color-mapped-sRGB-gray",
  333. "color-mapped-sRGB-gray+alpha",
  334. "color-mapped-sRGB-rgb",
  335. "color-mapped-sRGB-rgb+alpha",
  336. "color-mapped-linear-gray",
  337. "color-mapped-linear-gray+alpha",
  338. "color-mapped-linear-rgb",
  339. "color-mapped-linear-rgb+alpha",
  340. "sRGB-gray",
  341. "sRGB-gray+alpha",
  342. "sRGB-bgr",
  343. "sRGB-bgr+alpha",
  344. "linear-gray",
  345. "linear-gray+alpha",
  346. "linear-bgr",
  347. "linear-bgr+alpha",
  348. "color-mapped-sRGB-gray",
  349. "color-mapped-sRGB-gray+alpha",
  350. "color-mapped-sRGB-bgr",
  351. "color-mapped-sRGB-bgr+alpha",
  352. "color-mapped-linear-gray",
  353. "color-mapped-linear-gray+alpha",
  354. "color-mapped-linear-bgr",
  355. "color-mapped-linear-bgr+alpha",
  356. "sRGB-gray",
  357. "alpha+sRGB-gray",
  358. "sRGB-rgb",
  359. "alpha+sRGB-rgb",
  360. "linear-gray",
  361. "alpha+linear-gray",
  362. "linear-rgb",
  363. "alpha+linear-rgb",
  364. "color-mapped-sRGB-gray",
  365. "color-mapped-alpha+sRGB-gray",
  366. "color-mapped-sRGB-rgb",
  367. "color-mapped-alpha+sRGB-rgb",
  368. "color-mapped-linear-gray",
  369. "color-mapped-alpha+linear-gray",
  370. "color-mapped-linear-rgb",
  371. "color-mapped-alpha+linear-rgb",
  372. "sRGB-gray",
  373. "alpha+sRGB-gray",
  374. "sRGB-bgr",
  375. "alpha+sRGB-bgr",
  376. "linear-gray",
  377. "alpha+linear-gray",
  378. "linear-bgr",
  379. "alpha+linear-bgr",
  380. "color-mapped-sRGB-gray",
  381. "color-mapped-alpha+sRGB-gray",
  382. "color-mapped-sRGB-bgr",
  383. "color-mapped-alpha+sRGB-bgr",
  384. "color-mapped-linear-gray",
  385. "color-mapped-alpha+linear-gray",
  386. "color-mapped-linear-bgr",
  387. "color-mapped-alpha+linear-bgr",
  388. };
  389. /* Decode an argument to a format number. */
  390. static png_uint_32
  391. formatof(const char *arg)
  392. {
  393. char *ep;
  394. unsigned long format = strtoul(arg, &ep, 0);
  395. if (ep > arg && *ep == 0 && format < FORMAT_COUNT)
  396. return (png_uint_32)format;
  397. else for (format=0; format < FORMAT_COUNT; ++format)
  398. {
  399. if (strcmp(format_names[format], arg) == 0)
  400. return (png_uint_32)format;
  401. }
  402. fprintf(stderr, "pngstest: format name '%s' invalid\n", arg);
  403. return FORMAT_COUNT;
  404. }
  405. /* Bitset/test functions for formats */
  406. #define FORMAT_SET_COUNT (FORMAT_COUNT / 32)
  407. typedef struct
  408. {
  409. png_uint_32 bits[FORMAT_SET_COUNT];
  410. }
  411. format_list;
  412. static void format_init(format_list *pf)
  413. {
  414. int i;
  415. for (i=0; i<FORMAT_SET_COUNT; ++i)
  416. pf->bits[i] = 0; /* All off */
  417. }
  418. #if 0 /* currently unused */
  419. static void format_clear(format_list *pf)
  420. {
  421. int i;
  422. for (i=0; i<FORMAT_SET_COUNT; ++i)
  423. pf->bits[i] = 0;
  424. }
  425. #endif
  426. static int format_is_initial(format_list *pf)
  427. {
  428. int i;
  429. for (i=0; i<FORMAT_SET_COUNT; ++i)
  430. if (pf->bits[i] != 0)
  431. return 0;
  432. return 1;
  433. }
  434. static int format_set(format_list *pf, png_uint_32 format)
  435. {
  436. if (format < FORMAT_COUNT)
  437. return pf->bits[format >> 5] |= ((png_uint_32)1) << (format & 31);
  438. return 0;
  439. }
  440. #if 0 /* currently unused */
  441. static int format_unset(format_list *pf, png_uint_32 format)
  442. {
  443. if (format < FORMAT_COUNT)
  444. return pf->bits[format >> 5] &= ~((png_uint_32)1) << (format & 31);
  445. return 0;
  446. }
  447. #endif
  448. static int format_isset(format_list *pf, png_uint_32 format)
  449. {
  450. return format < FORMAT_COUNT &&
  451. (pf->bits[format >> 5] & (((png_uint_32)1) << (format & 31))) != 0;
  452. }
  453. static void format_default(format_list *pf, int redundant)
  454. {
  455. if (redundant)
  456. {
  457. int i;
  458. /* set everything, including flags that are pointless */
  459. for (i=0; i<FORMAT_SET_COUNT; ++i)
  460. pf->bits[i] = ~(png_uint_32)0;
  461. }
  462. else
  463. {
  464. png_uint_32 f;
  465. for (f=0; f<FORMAT_COUNT; ++f)
  466. {
  467. /* Eliminate redundant and unsupported settings. */
  468. # ifdef PNG_FORMAT_BGR_SUPPORTED
  469. /* BGR is meaningless if no color: */
  470. if ((f & PNG_FORMAT_FLAG_COLOR) == 0 &&
  471. (f & PNG_FORMAT_FLAG_BGR) != 0)
  472. # else
  473. if ((f & 0x10U/*HACK: fixed value*/) != 0)
  474. # endif
  475. continue;
  476. /* AFIRST is meaningless if no alpha: */
  477. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  478. if ((f & PNG_FORMAT_FLAG_ALPHA) == 0 &&
  479. (f & PNG_FORMAT_FLAG_AFIRST) != 0)
  480. # else
  481. if ((f & 0x20U/*HACK: fixed value*/) != 0)
  482. # endif
  483. continue;
  484. format_set(pf, f);
  485. }
  486. }
  487. }
  488. /* THE Image STRUCTURE */
  489. /* The super-class of a png_image, contains the decoded image plus the input
  490. * data necessary to re-read the file with a different format.
  491. */
  492. typedef struct
  493. {
  494. png_image image;
  495. png_uint_32 opts;
  496. const char *file_name;
  497. int stride_extra;
  498. FILE *input_file;
  499. png_voidp input_memory;
  500. size_t input_memory_size;
  501. png_bytep buffer;
  502. ptrdiff_t stride;
  503. size_t bufsize;
  504. size_t allocsize;
  505. char tmpfile_name[32];
  506. png_uint_16 colormap[256*4];
  507. }
  508. Image;
  509. /* Initializer: also sets the permitted error limit for 16-bit operations. */
  510. static void
  511. newimage(Image *image)
  512. {
  513. memset(image, 0, sizeof *image);
  514. }
  515. /* Reset the image to be read again - only needs to rewind the FILE* at present.
  516. */
  517. static void
  518. resetimage(Image *image)
  519. {
  520. if (image->input_file != NULL)
  521. rewind(image->input_file);
  522. }
  523. /* Free the image buffer; the buffer is re-used on a re-read, this is just for
  524. * cleanup.
  525. */
  526. static void
  527. freebuffer(Image *image)
  528. {
  529. if (image->buffer) free(image->buffer);
  530. image->buffer = NULL;
  531. image->bufsize = 0;
  532. image->allocsize = 0;
  533. }
  534. /* Delete function; cleans out all the allocated data and the temporary file in
  535. * the image.
  536. */
  537. static void
  538. freeimage(Image *image)
  539. {
  540. freebuffer(image);
  541. png_image_free(&image->image);
  542. if (image->input_file != NULL)
  543. {
  544. fclose(image->input_file);
  545. image->input_file = NULL;
  546. }
  547. if (image->input_memory != NULL)
  548. {
  549. free(image->input_memory);
  550. image->input_memory = NULL;
  551. image->input_memory_size = 0;
  552. }
  553. if (image->tmpfile_name[0] != 0 && (image->opts & KEEP_TMPFILES) == 0)
  554. {
  555. (void)remove(image->tmpfile_name);
  556. image->tmpfile_name[0] = 0;
  557. }
  558. }
  559. /* This is actually a re-initializer; allows an image structure to be re-used by
  560. * freeing everything that relates to an old image.
  561. */
  562. static void initimage(Image *image, png_uint_32 opts, const char *file_name,
  563. int stride_extra)
  564. {
  565. freeimage(image);
  566. memset(&image->image, 0, sizeof image->image);
  567. image->opts = opts;
  568. image->file_name = file_name;
  569. image->stride_extra = stride_extra;
  570. }
  571. /* Make sure the image buffer is big enough; allows re-use of the buffer if the
  572. * image is re-read.
  573. */
  574. #define BUFFER_INIT8 73
  575. static void
  576. allocbuffer(Image *image)
  577. {
  578. size_t size = PNG_IMAGE_BUFFER_SIZE(image->image, image->stride);
  579. if (size+32 > image->bufsize)
  580. {
  581. freebuffer(image);
  582. image->buffer = voidcast(png_bytep, malloc(size+32));
  583. if (image->buffer == NULL)
  584. {
  585. fflush(stdout);
  586. fprintf(stderr,
  587. "simpletest: out of memory allocating %lu(+32) byte buffer\n",
  588. (unsigned long)size);
  589. exit(1);
  590. }
  591. image->bufsize = size+32;
  592. }
  593. memset(image->buffer, 95, image->bufsize);
  594. memset(image->buffer+16, BUFFER_INIT8, size);
  595. image->allocsize = size;
  596. }
  597. /* Make sure 16 bytes match the given byte. */
  598. static int
  599. check16(png_const_bytep bp, int b)
  600. {
  601. int i = 16;
  602. do
  603. if (*bp != b) return 1;
  604. while (--i);
  605. return 0;
  606. }
  607. /* Check for overwrite in the image buffer. */
  608. static void
  609. checkbuffer(Image *image, const char *arg)
  610. {
  611. if (check16(image->buffer, 95))
  612. {
  613. fflush(stdout);
  614. fprintf(stderr, "%s: overwrite at start of image buffer\n", arg);
  615. exit(1);
  616. }
  617. if (check16(image->buffer+16+image->allocsize, 95))
  618. {
  619. fflush(stdout);
  620. fprintf(stderr, "%s: overwrite at end of image buffer\n", arg);
  621. exit(1);
  622. }
  623. }
  624. /* ERROR HANDLING */
  625. /* Log a terminal error, also frees the libpng part of the image if necessary.
  626. */
  627. static int
  628. logerror(Image *image, const char *a1, const char *a2, const char *a3)
  629. {
  630. fflush(stdout);
  631. if (image->image.warning_or_error)
  632. fprintf(stderr, "%s%s%s: %s\n", a1, a2, a3, image->image.message);
  633. else
  634. fprintf(stderr, "%s%s%s\n", a1, a2, a3);
  635. if (image->image.opaque != NULL)
  636. {
  637. fprintf(stderr, "%s: image opaque pointer non-NULL on error\n",
  638. image->file_name);
  639. png_image_free(&image->image);
  640. }
  641. return 0;
  642. }
  643. /* Log an error and close a file (just a utility to do both things in one
  644. * function call.)
  645. */
  646. static int
  647. logclose(Image *image, FILE *f, const char *name, const char *operation)
  648. {
  649. int e = errno;
  650. fclose(f);
  651. return logerror(image, name, operation, strerror(e));
  652. }
  653. /* Make sure the png_image has been freed - validates that libpng is doing what
  654. * the spec says and freeing the image.
  655. */
  656. static int
  657. checkopaque(Image *image)
  658. {
  659. if (image->image.opaque != NULL)
  660. {
  661. png_image_free(&image->image);
  662. return logerror(image, image->file_name, ": opaque not NULL", "");
  663. }
  664. /* Separate out the gamma+background_rgb_to_gray warning because it may
  665. * produce opaque component errors:
  666. */
  667. else if (image->image.warning_or_error != 0 &&
  668. (strcmp(image->image.message,
  669. "libpng does not support gamma+background+rgb_to_gray") == 0 ?
  670. (image->opts & GBG_ERROR) != 0 : (image->opts & STRICT) != 0))
  671. return logerror(image, image->file_name, (image->opts & GBG_ERROR) != 0 ?
  672. " --fault-gbg-warning" : " --strict", "");
  673. else
  674. return 1;
  675. }
  676. /* IMAGE COMPARISON/CHECKING */
  677. /* Compare the pixels of two images, which should be the same but aren't. The
  678. * images must have been checked for a size match.
  679. */
  680. typedef struct
  681. {
  682. /* The components, for grayscale images the gray value is in 'g' and if alpha
  683. * is not present 'a' is set to 255 or 65535 according to format.
  684. */
  685. int r, g, b, a;
  686. } Pixel;
  687. typedef struct
  688. {
  689. /* The background as the original sRGB 8-bit value converted to the final
  690. * integer format and as a double precision linear value in the range 0..1
  691. * for with partially transparent pixels.
  692. */
  693. int ir, ig, ib;
  694. double dr, dg, db; /* linear r,g,b scaled to 0..1 */
  695. } Background;
  696. /* Basic image formats; control the data but not the layout thereof. */
  697. #define BASE_FORMATS\
  698. (PNG_FORMAT_FLAG_ALPHA|PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_LINEAR)
  699. /* Read a Pixel from a buffer. The code below stores the correct routine for
  700. * the format in a function pointer, these are the routines:
  701. */
  702. static void
  703. gp_g8(Pixel *p, png_const_voidp pb)
  704. {
  705. png_const_bytep pp = voidcast(png_const_bytep, pb);
  706. p->r = p->g = p->b = pp[0];
  707. p->a = 255;
  708. }
  709. static void
  710. gp_ga8(Pixel *p, png_const_voidp pb)
  711. {
  712. png_const_bytep pp = voidcast(png_const_bytep, pb);
  713. p->r = p->g = p->b = pp[0];
  714. p->a = pp[1];
  715. }
  716. #ifdef PNG_FORMAT_AFIRST_SUPPORTED
  717. static void
  718. gp_ag8(Pixel *p, png_const_voidp pb)
  719. {
  720. png_const_bytep pp = voidcast(png_const_bytep, pb);
  721. p->r = p->g = p->b = pp[1];
  722. p->a = pp[0];
  723. }
  724. #endif
  725. static void
  726. gp_rgb8(Pixel *p, png_const_voidp pb)
  727. {
  728. png_const_bytep pp = voidcast(png_const_bytep, pb);
  729. p->r = pp[0];
  730. p->g = pp[1];
  731. p->b = pp[2];
  732. p->a = 255;
  733. }
  734. #ifdef PNG_FORMAT_BGR_SUPPORTED
  735. static void
  736. gp_bgr8(Pixel *p, png_const_voidp pb)
  737. {
  738. png_const_bytep pp = voidcast(png_const_bytep, pb);
  739. p->r = pp[2];
  740. p->g = pp[1];
  741. p->b = pp[0];
  742. p->a = 255;
  743. }
  744. #endif
  745. static void
  746. gp_rgba8(Pixel *p, png_const_voidp pb)
  747. {
  748. png_const_bytep pp = voidcast(png_const_bytep, pb);
  749. p->r = pp[0];
  750. p->g = pp[1];
  751. p->b = pp[2];
  752. p->a = pp[3];
  753. }
  754. #ifdef PNG_FORMAT_BGR_SUPPORTED
  755. static void
  756. gp_bgra8(Pixel *p, png_const_voidp pb)
  757. {
  758. png_const_bytep pp = voidcast(png_const_bytep, pb);
  759. p->r = pp[2];
  760. p->g = pp[1];
  761. p->b = pp[0];
  762. p->a = pp[3];
  763. }
  764. #endif
  765. #ifdef PNG_FORMAT_AFIRST_SUPPORTED
  766. static void
  767. gp_argb8(Pixel *p, png_const_voidp pb)
  768. {
  769. png_const_bytep pp = voidcast(png_const_bytep, pb);
  770. p->r = pp[1];
  771. p->g = pp[2];
  772. p->b = pp[3];
  773. p->a = pp[0];
  774. }
  775. #endif
  776. #if defined(PNG_FORMAT_AFIRST_SUPPORTED) && defined(PNG_FORMAT_BGR_SUPPORTED)
  777. static void
  778. gp_abgr8(Pixel *p, png_const_voidp pb)
  779. {
  780. png_const_bytep pp = voidcast(png_const_bytep, pb);
  781. p->r = pp[3];
  782. p->g = pp[2];
  783. p->b = pp[1];
  784. p->a = pp[0];
  785. }
  786. #endif
  787. static void
  788. gp_g16(Pixel *p, png_const_voidp pb)
  789. {
  790. png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
  791. p->r = p->g = p->b = pp[0];
  792. p->a = 65535;
  793. }
  794. static void
  795. gp_ga16(Pixel *p, png_const_voidp pb)
  796. {
  797. png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
  798. p->r = p->g = p->b = pp[0];
  799. p->a = pp[1];
  800. }
  801. #ifdef PNG_FORMAT_AFIRST_SUPPORTED
  802. static void
  803. gp_ag16(Pixel *p, png_const_voidp pb)
  804. {
  805. png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
  806. p->r = p->g = p->b = pp[1];
  807. p->a = pp[0];
  808. }
  809. #endif
  810. static void
  811. gp_rgb16(Pixel *p, png_const_voidp pb)
  812. {
  813. png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
  814. p->r = pp[0];
  815. p->g = pp[1];
  816. p->b = pp[2];
  817. p->a = 65535;
  818. }
  819. #ifdef PNG_FORMAT_BGR_SUPPORTED
  820. static void
  821. gp_bgr16(Pixel *p, png_const_voidp pb)
  822. {
  823. png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
  824. p->r = pp[2];
  825. p->g = pp[1];
  826. p->b = pp[0];
  827. p->a = 65535;
  828. }
  829. #endif
  830. static void
  831. gp_rgba16(Pixel *p, png_const_voidp pb)
  832. {
  833. png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
  834. p->r = pp[0];
  835. p->g = pp[1];
  836. p->b = pp[2];
  837. p->a = pp[3];
  838. }
  839. #ifdef PNG_FORMAT_BGR_SUPPORTED
  840. static void
  841. gp_bgra16(Pixel *p, png_const_voidp pb)
  842. {
  843. png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
  844. p->r = pp[2];
  845. p->g = pp[1];
  846. p->b = pp[0];
  847. p->a = pp[3];
  848. }
  849. #endif
  850. #ifdef PNG_FORMAT_AFIRST_SUPPORTED
  851. static void
  852. gp_argb16(Pixel *p, png_const_voidp pb)
  853. {
  854. png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
  855. p->r = pp[1];
  856. p->g = pp[2];
  857. p->b = pp[3];
  858. p->a = pp[0];
  859. }
  860. #endif
  861. #if defined(PNG_FORMAT_AFIRST_SUPPORTED) && defined(PNG_FORMAT_BGR_SUPPORTED)
  862. static void
  863. gp_abgr16(Pixel *p, png_const_voidp pb)
  864. {
  865. png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
  866. p->r = pp[3];
  867. p->g = pp[2];
  868. p->b = pp[1];
  869. p->a = pp[0];
  870. }
  871. #endif
  872. /* Given a format, return the correct one of the above functions. */
  873. static void (*
  874. get_pixel(png_uint_32 format))(Pixel *p, png_const_voidp pb)
  875. {
  876. /* The color-map flag is irrelevant here - the caller of the function
  877. * returned must either pass the buffer or, for a color-mapped image, the
  878. * correct entry in the color-map.
  879. */
  880. if (format & PNG_FORMAT_FLAG_LINEAR)
  881. {
  882. if (format & PNG_FORMAT_FLAG_COLOR)
  883. {
  884. # ifdef PNG_FORMAT_BGR_SUPPORTED
  885. if (format & PNG_FORMAT_FLAG_BGR)
  886. {
  887. if (format & PNG_FORMAT_FLAG_ALPHA)
  888. {
  889. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  890. if (format & PNG_FORMAT_FLAG_AFIRST)
  891. return gp_abgr16;
  892. else
  893. # endif
  894. return gp_bgra16;
  895. }
  896. else
  897. return gp_bgr16;
  898. }
  899. else
  900. # endif
  901. {
  902. if (format & PNG_FORMAT_FLAG_ALPHA)
  903. {
  904. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  905. if (format & PNG_FORMAT_FLAG_AFIRST)
  906. return gp_argb16;
  907. else
  908. # endif
  909. return gp_rgba16;
  910. }
  911. else
  912. return gp_rgb16;
  913. }
  914. }
  915. else
  916. {
  917. if (format & PNG_FORMAT_FLAG_ALPHA)
  918. {
  919. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  920. if (format & PNG_FORMAT_FLAG_AFIRST)
  921. return gp_ag16;
  922. else
  923. # endif
  924. return gp_ga16;
  925. }
  926. else
  927. return gp_g16;
  928. }
  929. }
  930. else
  931. {
  932. if (format & PNG_FORMAT_FLAG_COLOR)
  933. {
  934. # ifdef PNG_FORMAT_BGR_SUPPORTED
  935. if (format & PNG_FORMAT_FLAG_BGR)
  936. {
  937. if (format & PNG_FORMAT_FLAG_ALPHA)
  938. {
  939. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  940. if (format & PNG_FORMAT_FLAG_AFIRST)
  941. return gp_abgr8;
  942. else
  943. # endif
  944. return gp_bgra8;
  945. }
  946. else
  947. return gp_bgr8;
  948. }
  949. else
  950. # endif
  951. {
  952. if (format & PNG_FORMAT_FLAG_ALPHA)
  953. {
  954. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  955. if (format & PNG_FORMAT_FLAG_AFIRST)
  956. return gp_argb8;
  957. else
  958. # endif
  959. return gp_rgba8;
  960. }
  961. else
  962. return gp_rgb8;
  963. }
  964. }
  965. else
  966. {
  967. if (format & PNG_FORMAT_FLAG_ALPHA)
  968. {
  969. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  970. if (format & PNG_FORMAT_FLAG_AFIRST)
  971. return gp_ag8;
  972. else
  973. # endif
  974. return gp_ga8;
  975. }
  976. else
  977. return gp_g8;
  978. }
  979. }
  980. }
  981. /* Conversion between pixel formats. The code above effectively eliminates the
  982. * component ordering changes leaving three basic changes:
  983. *
  984. * 1) Remove an alpha channel by pre-multiplication or compositing on a
  985. * background color. (Adding an alpha channel is a no-op.)
  986. *
  987. * 2) Remove color by mapping to grayscale. (Grayscale to color is a no-op.)
  988. *
  989. * 3) Convert between 8-bit and 16-bit components. (Both directtions are
  990. * relevant.)
  991. *
  992. * This gives the following base format conversion matrix:
  993. *
  994. * OUT: ----- 8-bit ----- ----- 16-bit -----
  995. * IN G GA RGB RGBA G GA RGB RGBA
  996. * 8 G . . . . lin lin lin lin
  997. * 8 GA bckg . bckc . pre' pre pre' pre
  998. * 8 RGB g8 g8 . . glin glin lin lin
  999. * 8 RGBA g8b g8 bckc . gpr' gpre pre' pre
  1000. * 16 G sRGB sRGB sRGB sRGB . . . .
  1001. * 16 GA b16g unpg b16c unpc A . A .
  1002. * 16 RGB sG sG sRGB sRGB g16 g16 . .
  1003. * 16 RGBA gb16 sGp cb16 sCp g16 g16' A .
  1004. *
  1005. * 8-bit to 8-bit:
  1006. * bckg: composite on gray background
  1007. * bckc: composite on color background
  1008. * g8: convert sRGB components to sRGB grayscale
  1009. * g8b: convert sRGB components to grayscale and composite on gray background
  1010. *
  1011. * 8-bit to 16-bit:
  1012. * lin: make sRGB components linear, alpha := 65535
  1013. * pre: make sRGB components linear and premultiply by alpha (scale alpha)
  1014. * pre': as 'pre' but alpha := 65535
  1015. * glin: make sRGB components linear, convert to grayscale, alpha := 65535
  1016. * gpre: make sRGB components grayscale and linear and premultiply by alpha
  1017. * gpr': as 'gpre' but alpha := 65535
  1018. *
  1019. * 16-bit to 8-bit:
  1020. * sRGB: convert linear components to sRGB, alpha := 255
  1021. * unpg: unpremultiply gray component and convert to sRGB (scale alpha)
  1022. * unpc: unpremultiply color components and convert to sRGB (scale alpha)
  1023. * b16g: composite linear onto gray background and convert the result to sRGB
  1024. * b16c: composite linear onto color background and convert the result to sRGB
  1025. * sG: convert linear RGB to sRGB grayscale
  1026. * sGp: unpremultiply RGB then convert to sRGB grayscale
  1027. * sCp: unpremultiply RGB then convert to sRGB
  1028. * gb16: composite linear onto background and convert to sRGB grayscale
  1029. * (order doesn't matter, the composite and grayscale operations permute)
  1030. * cb16: composite linear onto background and convert to sRGB
  1031. *
  1032. * 16-bit to 16-bit:
  1033. * A: set alpha to 65535
  1034. * g16: convert linear RGB to linear grayscale (alpha := 65535)
  1035. * g16': as 'g16' but alpha is unchanged
  1036. */
  1037. /* Simple copy: */
  1038. static void
  1039. gpc_noop(Pixel *out, const Pixel *in, const Background *back)
  1040. {
  1041. (void)back;
  1042. out->r = in->r;
  1043. out->g = in->g;
  1044. out->b = in->b;
  1045. out->a = in->a;
  1046. }
  1047. #if ALLOW_UNUSED_GPC
  1048. static void
  1049. gpc_nop8(Pixel *out, const Pixel *in, const Background *back)
  1050. {
  1051. (void)back;
  1052. if (in->a == 0)
  1053. out->r = out->g = out->b = 255;
  1054. else
  1055. {
  1056. out->r = in->r;
  1057. out->g = in->g;
  1058. out->b = in->b;
  1059. }
  1060. out->a = in->a;
  1061. }
  1062. #endif
  1063. #if ALLOW_UNUSED_GPC
  1064. static void
  1065. gpc_nop6(Pixel *out, const Pixel *in, const Background *back)
  1066. {
  1067. (void)back;
  1068. if (in->a == 0)
  1069. out->r = out->g = out->b = 65535;
  1070. else
  1071. {
  1072. out->r = in->r;
  1073. out->g = in->g;
  1074. out->b = in->b;
  1075. }
  1076. out->a = in->a;
  1077. }
  1078. #endif
  1079. /* 8-bit to 8-bit conversions */
  1080. /* bckg: composite on gray background */
  1081. static void
  1082. gpc_bckg(Pixel *out, const Pixel *in, const Background *back)
  1083. {
  1084. if (in->a <= 0)
  1085. out->r = out->g = out->b = back->ig;
  1086. else if (in->a >= 255)
  1087. out->r = out->g = out->b = in->g;
  1088. else
  1089. {
  1090. double a = in->a / 255.;
  1091. out->r = out->g = out->b = sRGB(sRGB_to_d[in->g] * a + back->dg * (1-a));
  1092. }
  1093. out->a = 255;
  1094. }
  1095. /* bckc: composite on color background */
  1096. static void
  1097. gpc_bckc(Pixel *out, const Pixel *in, const Background *back)
  1098. {
  1099. if (in->a <= 0)
  1100. {
  1101. out->r = back->ir;
  1102. out->g = back->ig;
  1103. out->b = back->ib;
  1104. }
  1105. else if (in->a >= 255)
  1106. {
  1107. out->r = in->r;
  1108. out->g = in->g;
  1109. out->b = in->b;
  1110. }
  1111. else
  1112. {
  1113. double a = in->a / 255.;
  1114. out->r = sRGB(sRGB_to_d[in->r] * a + back->dr * (1-a));
  1115. out->g = sRGB(sRGB_to_d[in->g] * a + back->dg * (1-a));
  1116. out->b = sRGB(sRGB_to_d[in->b] * a + back->db * (1-a));
  1117. }
  1118. out->a = 255;
  1119. }
  1120. /* g8: convert sRGB components to sRGB grayscale */
  1121. static void
  1122. gpc_g8(Pixel *out, const Pixel *in, const Background *back)
  1123. {
  1124. (void)back;
  1125. if (in->r == in->g && in->g == in->b)
  1126. out->r = out->g = out->b = in->g;
  1127. else
  1128. out->r = out->g = out->b =
  1129. sRGB(YfromRGB(sRGB_to_d[in->r], sRGB_to_d[in->g], sRGB_to_d[in->b]));
  1130. out->a = in->a;
  1131. }
  1132. /* g8b: convert sRGB components to grayscale and composite on gray background */
  1133. static void
  1134. gpc_g8b(Pixel *out, const Pixel *in, const Background *back)
  1135. {
  1136. if (in->a <= 0)
  1137. out->r = out->g = out->b = back->ig;
  1138. else if (in->a >= 255)
  1139. {
  1140. if (in->r == in->g && in->g == in->b)
  1141. out->r = out->g = out->b = in->g;
  1142. else
  1143. out->r = out->g = out->b = sRGB(YfromRGB(
  1144. sRGB_to_d[in->r], sRGB_to_d[in->g], sRGB_to_d[in->b]));
  1145. }
  1146. else
  1147. {
  1148. double a = in->a/255.;
  1149. out->r = out->g = out->b = sRGB(a * YfromRGB(sRGB_to_d[in->r],
  1150. sRGB_to_d[in->g], sRGB_to_d[in->b]) + back->dg * (1-a));
  1151. }
  1152. out->a = 255;
  1153. }
  1154. /* 8-bit to 16-bit conversions */
  1155. /* lin: make sRGB components linear, alpha := 65535 */
  1156. static void
  1157. gpc_lin(Pixel *out, const Pixel *in, const Background *back)
  1158. {
  1159. (void)back;
  1160. out->r = ilinear(in->r);
  1161. if (in->g == in->r)
  1162. {
  1163. out->g = out->r;
  1164. if (in->b == in->r)
  1165. out->b = out->r;
  1166. else
  1167. out->b = ilinear(in->b);
  1168. }
  1169. else
  1170. {
  1171. out->g = ilinear(in->g);
  1172. if (in->b == in->r)
  1173. out->b = out->r;
  1174. else if (in->b == in->g)
  1175. out->b = out->g;
  1176. else
  1177. out->b = ilinear(in->b);
  1178. }
  1179. out->a = 65535;
  1180. }
  1181. /* pre: make sRGB components linear and premultiply by alpha (scale alpha) */
  1182. static void
  1183. gpc_pre(Pixel *out, const Pixel *in, const Background *back)
  1184. {
  1185. (void)back;
  1186. out->r = ilineara(in->r, in->a);
  1187. if (in->g == in->r)
  1188. {
  1189. out->g = out->r;
  1190. if (in->b == in->r)
  1191. out->b = out->r;
  1192. else
  1193. out->b = ilineara(in->b, in->a);
  1194. }
  1195. else
  1196. {
  1197. out->g = ilineara(in->g, in->a);
  1198. if (in->b == in->r)
  1199. out->b = out->r;
  1200. else if (in->b == in->g)
  1201. out->b = out->g;
  1202. else
  1203. out->b = ilineara(in->b, in->a);
  1204. }
  1205. out->a = in->a * 257;
  1206. }
  1207. /* pre': as 'pre' but alpha := 65535 */
  1208. static void
  1209. gpc_preq(Pixel *out, const Pixel *in, const Background *back)
  1210. {
  1211. (void)back;
  1212. out->r = ilineara(in->r, in->a);
  1213. if (in->g == in->r)
  1214. {
  1215. out->g = out->r;
  1216. if (in->b == in->r)
  1217. out->b = out->r;
  1218. else
  1219. out->b = ilineara(in->b, in->a);
  1220. }
  1221. else
  1222. {
  1223. out->g = ilineara(in->g, in->a);
  1224. if (in->b == in->r)
  1225. out->b = out->r;
  1226. else if (in->b == in->g)
  1227. out->b = out->g;
  1228. else
  1229. out->b = ilineara(in->b, in->a);
  1230. }
  1231. out->a = 65535;
  1232. }
  1233. /* glin: make sRGB components linear, convert to grayscale, alpha := 65535 */
  1234. static void
  1235. gpc_glin(Pixel *out, const Pixel *in, const Background *back)
  1236. {
  1237. (void)back;
  1238. if (in->r == in->g && in->g == in->b)
  1239. out->r = out->g = out->b = ilinear(in->g);
  1240. else
  1241. out->r = out->g = out->b = u16d(65535 *
  1242. YfromRGB(sRGB_to_d[in->r], sRGB_to_d[in->g], sRGB_to_d[in->b]));
  1243. out->a = 65535;
  1244. }
  1245. /* gpre: make sRGB components grayscale and linear and premultiply by alpha */
  1246. static void
  1247. gpc_gpre(Pixel *out, const Pixel *in, const Background *back)
  1248. {
  1249. (void)back;
  1250. if (in->r == in->g && in->g == in->b)
  1251. out->r = out->g = out->b = ilineara(in->g, in->a);
  1252. else
  1253. out->r = out->g = out->b = u16d(in->a * 257 *
  1254. YfromRGB(sRGB_to_d[in->r], sRGB_to_d[in->g], sRGB_to_d[in->b]));
  1255. out->a = 257 * in->a;
  1256. }
  1257. /* gpr': as 'gpre' but alpha := 65535 */
  1258. static void
  1259. gpc_gprq(Pixel *out, const Pixel *in, const Background *back)
  1260. {
  1261. (void)back;
  1262. if (in->r == in->g && in->g == in->b)
  1263. out->r = out->g = out->b = ilineara(in->g, in->a);
  1264. else
  1265. out->r = out->g = out->b = u16d(in->a * 257 *
  1266. YfromRGB(sRGB_to_d[in->r], sRGB_to_d[in->g], sRGB_to_d[in->b]));
  1267. out->a = 65535;
  1268. }
  1269. /* 8-bit to 16-bit conversions for gAMA 45455 encoded values */
  1270. /* Lin: make gAMA 45455 components linear, alpha := 65535 */
  1271. static void
  1272. gpc_Lin(Pixel *out, const Pixel *in, const Background *back)
  1273. {
  1274. (void)back;
  1275. out->r = ilinear_g22(in->r);
  1276. if (in->g == in->r)
  1277. {
  1278. out->g = out->r;
  1279. if (in->b == in->r)
  1280. out->b = out->r;
  1281. else
  1282. out->b = ilinear_g22(in->b);
  1283. }
  1284. else
  1285. {
  1286. out->g = ilinear_g22(in->g);
  1287. if (in->b == in->r)
  1288. out->b = out->r;
  1289. else if (in->b == in->g)
  1290. out->b = out->g;
  1291. else
  1292. out->b = ilinear_g22(in->b);
  1293. }
  1294. out->a = 65535;
  1295. }
  1296. #if ALLOW_UNUSED_GPC
  1297. /* Pre: make gAMA 45455 components linear and premultiply by alpha (scale alpha)
  1298. */
  1299. static void
  1300. gpc_Pre(Pixel *out, const Pixel *in, const Background *back)
  1301. {
  1302. (void)back;
  1303. out->r = ilineara_g22(in->r, in->a);
  1304. if (in->g == in->r)
  1305. {
  1306. out->g = out->r;
  1307. if (in->b == in->r)
  1308. out->b = out->r;
  1309. else
  1310. out->b = ilineara_g22(in->b, in->a);
  1311. }
  1312. else
  1313. {
  1314. out->g = ilineara_g22(in->g, in->a);
  1315. if (in->b == in->r)
  1316. out->b = out->r;
  1317. else if (in->b == in->g)
  1318. out->b = out->g;
  1319. else
  1320. out->b = ilineara_g22(in->b, in->a);
  1321. }
  1322. out->a = in->a * 257;
  1323. }
  1324. #endif
  1325. #if ALLOW_UNUSED_GPC
  1326. /* Pre': as 'Pre' but alpha := 65535 */
  1327. static void
  1328. gpc_Preq(Pixel *out, const Pixel *in, const Background *back)
  1329. {
  1330. (void)back;
  1331. out->r = ilineara_g22(in->r, in->a);
  1332. if (in->g == in->r)
  1333. {
  1334. out->g = out->r;
  1335. if (in->b == in->r)
  1336. out->b = out->r;
  1337. else
  1338. out->b = ilineara_g22(in->b, in->a);
  1339. }
  1340. else
  1341. {
  1342. out->g = ilineara_g22(in->g, in->a);
  1343. if (in->b == in->r)
  1344. out->b = out->r;
  1345. else if (in->b == in->g)
  1346. out->b = out->g;
  1347. else
  1348. out->b = ilineara_g22(in->b, in->a);
  1349. }
  1350. out->a = 65535;
  1351. }
  1352. #endif
  1353. #if ALLOW_UNUSED_GPC
  1354. /* Glin: make gAMA 45455 components linear, convert to grayscale, alpha := 65535
  1355. */
  1356. static void
  1357. gpc_Glin(Pixel *out, const Pixel *in, const Background *back)
  1358. {
  1359. (void)back;
  1360. if (in->r == in->g && in->g == in->b)
  1361. out->r = out->g = out->b = ilinear_g22(in->g);
  1362. else
  1363. out->r = out->g = out->b = u16d(65535 *
  1364. YfromRGB(g22_to_d[in->r], g22_to_d[in->g], g22_to_d[in->b]));
  1365. out->a = 65535;
  1366. }
  1367. #endif
  1368. #if ALLOW_UNUSED_GPC
  1369. /* Gpre: make gAMA 45455 components grayscale and linear and premultiply by
  1370. * alpha.
  1371. */
  1372. static void
  1373. gpc_Gpre(Pixel *out, const Pixel *in, const Background *back)
  1374. {
  1375. (void)back;
  1376. if (in->r == in->g && in->g == in->b)
  1377. out->r = out->g = out->b = ilineara_g22(in->g, in->a);
  1378. else
  1379. out->r = out->g = out->b = u16d(in->a * 257 *
  1380. YfromRGB(g22_to_d[in->r], g22_to_d[in->g], g22_to_d[in->b]));
  1381. out->a = 257 * in->a;
  1382. }
  1383. #endif
  1384. #if ALLOW_UNUSED_GPC
  1385. /* Gpr': as 'Gpre' but alpha := 65535 */
  1386. static void
  1387. gpc_Gprq(Pixel *out, const Pixel *in, const Background *back)
  1388. {
  1389. (void)back;
  1390. if (in->r == in->g && in->g == in->b)
  1391. out->r = out->g = out->b = ilineara_g22(in->g, in->a);
  1392. else
  1393. out->r = out->g = out->b = u16d(in->a * 257 *
  1394. YfromRGB(g22_to_d[in->r], g22_to_d[in->g], g22_to_d[in->b]));
  1395. out->a = 65535;
  1396. }
  1397. #endif
  1398. /* 16-bit to 8-bit conversions */
  1399. /* sRGB: convert linear components to sRGB, alpha := 255 */
  1400. static void
  1401. gpc_sRGB(Pixel *out, const Pixel *in, const Background *back)
  1402. {
  1403. (void)back;
  1404. out->r = isRGB(in->r);
  1405. if (in->g == in->r)
  1406. {
  1407. out->g = out->r;
  1408. if (in->b == in->r)
  1409. out->b = out->r;
  1410. else
  1411. out->b = isRGB(in->b);
  1412. }
  1413. else
  1414. {
  1415. out->g = isRGB(in->g);
  1416. if (in->b == in->r)
  1417. out->b = out->r;
  1418. else if (in->b == in->g)
  1419. out->b = out->g;
  1420. else
  1421. out->b = isRGB(in->b);
  1422. }
  1423. out->a = 255;
  1424. }
  1425. /* unpg: unpremultiply gray component and convert to sRGB (scale alpha) */
  1426. static void
  1427. gpc_unpg(Pixel *out, const Pixel *in, const Background *back)
  1428. {
  1429. (void)back;
  1430. if (in->a <= 128)
  1431. {
  1432. out->r = out->g = out->b = 255;
  1433. out->a = 0;
  1434. }
  1435. else
  1436. {
  1437. out->r = out->g = out->b = sRGB((double)in->g / in->a);
  1438. out->a = u8d(in->a / 257.);
  1439. }
  1440. }
  1441. /* unpc: unpremultiply color components and convert to sRGB (scale alpha) */
  1442. static void
  1443. gpc_unpc(Pixel *out, const Pixel *in, const Background *back)
  1444. {
  1445. (void)back;
  1446. if (in->a <= 128)
  1447. {
  1448. out->r = out->g = out->b = 255;
  1449. out->a = 0;
  1450. }
  1451. else
  1452. {
  1453. out->r = sRGB((double)in->r / in->a);
  1454. out->g = sRGB((double)in->g / in->a);
  1455. out->b = sRGB((double)in->b / in->a);
  1456. out->a = u8d(in->a / 257.);
  1457. }
  1458. }
  1459. /* b16g: composite linear onto gray background and convert the result to sRGB */
  1460. static void
  1461. gpc_b16g(Pixel *out, const Pixel *in, const Background *back)
  1462. {
  1463. if (in->a <= 0)
  1464. out->r = out->g = out->b = back->ig;
  1465. else
  1466. {
  1467. double a = in->a/65535.;
  1468. double a1 = 1-a;
  1469. a /= 65535;
  1470. out->r = out->g = out->b = sRGB(in->g * a + back->dg * a1);
  1471. }
  1472. out->a = 255;
  1473. }
  1474. /* b16c: composite linear onto color background and convert the result to sRGB*/
  1475. static void
  1476. gpc_b16c(Pixel *out, const Pixel *in, const Background *back)
  1477. {
  1478. if (in->a <= 0)
  1479. {
  1480. out->r = back->ir;
  1481. out->g = back->ig;
  1482. out->b = back->ib;
  1483. }
  1484. else
  1485. {
  1486. double a = in->a/65535.;
  1487. double a1 = 1-a;
  1488. a /= 65535;
  1489. out->r = sRGB(in->r * a + back->dr * a1);
  1490. out->g = sRGB(in->g * a + back->dg * a1);
  1491. out->b = sRGB(in->b * a + back->db * a1);
  1492. }
  1493. out->a = 255;
  1494. }
  1495. /* sG: convert linear RGB to sRGB grayscale */
  1496. static void
  1497. gpc_sG(Pixel *out, const Pixel *in, const Background *back)
  1498. {
  1499. (void)back;
  1500. out->r = out->g = out->b = sRGB(YfromRGBint(in->r, in->g, in->b)/65535);
  1501. out->a = 255;
  1502. }
  1503. /* sGp: unpremultiply RGB then convert to sRGB grayscale */
  1504. static void
  1505. gpc_sGp(Pixel *out, const Pixel *in, const Background *back)
  1506. {
  1507. (void)back;
  1508. if (in->a <= 128)
  1509. {
  1510. out->r = out->g = out->b = 255;
  1511. out->a = 0;
  1512. }
  1513. else
  1514. {
  1515. out->r = out->g = out->b = sRGB(YfromRGBint(in->r, in->g, in->b)/in->a);
  1516. out->a = u8d(in->a / 257.);
  1517. }
  1518. }
  1519. /* sCp: unpremultiply RGB then convert to sRGB */
  1520. static void
  1521. gpc_sCp(Pixel *out, const Pixel *in, const Background *back)
  1522. {
  1523. (void)back;
  1524. if (in->a <= 128)
  1525. {
  1526. out->r = out->g = out->b = 255;
  1527. out->a = 0;
  1528. }
  1529. else
  1530. {
  1531. out->r = sRGB((double)in->r / in->a);
  1532. out->g = sRGB((double)in->g / in->a);
  1533. out->b = sRGB((double)in->b / in->a);
  1534. out->a = u8d(in->a / 257.);
  1535. }
  1536. }
  1537. /* gb16: composite linear onto background and convert to sRGB grayscale */
  1538. /* (order doesn't matter, the composite and grayscale operations permute) */
  1539. static void
  1540. gpc_gb16(Pixel *out, const Pixel *in, const Background *back)
  1541. {
  1542. if (in->a <= 0)
  1543. out->r = out->g = out->b = back->ig;
  1544. else if (in->a >= 65535)
  1545. out->r = out->g = out->b = isRGB(in->g);
  1546. else
  1547. {
  1548. double a = in->a / 65535.;
  1549. double a1 = 1-a;
  1550. a /= 65535;
  1551. out->r = out->g = out->b = sRGB(in->g * a + back->dg * a1);
  1552. }
  1553. out->a = 255;
  1554. }
  1555. /* cb16: composite linear onto background and convert to sRGB */
  1556. static void
  1557. gpc_cb16(Pixel *out, const Pixel *in, const Background *back)
  1558. {
  1559. if (in->a <= 0)
  1560. {
  1561. out->r = back->ir;
  1562. out->g = back->ig;
  1563. out->b = back->ib;
  1564. }
  1565. else if (in->a >= 65535)
  1566. {
  1567. out->r = isRGB(in->r);
  1568. out->g = isRGB(in->g);
  1569. out->b = isRGB(in->b);
  1570. }
  1571. else
  1572. {
  1573. double a = in->a / 65535.;
  1574. double a1 = 1-a;
  1575. a /= 65535;
  1576. out->r = sRGB(in->r * a + back->dr * a1);
  1577. out->g = sRGB(in->g * a + back->dg * a1);
  1578. out->b = sRGB(in->b * a + back->db * a1);
  1579. }
  1580. out->a = 255;
  1581. }
  1582. /* 16-bit to 16-bit conversions */
  1583. /* A: set alpha to 65535 */
  1584. static void
  1585. gpc_A(Pixel *out, const Pixel *in, const Background *back)
  1586. {
  1587. (void)back;
  1588. out->r = in->r;
  1589. out->g = in->g;
  1590. out->b = in->b;
  1591. out->a = 65535;
  1592. }
  1593. /* g16: convert linear RGB to linear grayscale (alpha := 65535) */
  1594. static void
  1595. gpc_g16(Pixel *out, const Pixel *in, const Background *back)
  1596. {
  1597. (void)back;
  1598. out->r = out->g = out->b = u16d(YfromRGBint(in->r, in->g, in->b));
  1599. out->a = 65535;
  1600. }
  1601. /* g16': as 'g16' but alpha is unchanged */
  1602. static void
  1603. gpc_g16q(Pixel *out, const Pixel *in, const Background *back)
  1604. {
  1605. (void)back;
  1606. out->r = out->g = out->b = u16d(YfromRGBint(in->r, in->g, in->b));
  1607. out->a = in->a;
  1608. }
  1609. #if ALLOW_UNUSED_GPC
  1610. /* Unused functions (to hide them from GCC unused function warnings) */
  1611. void (* const gpc_unused[])
  1612. (Pixel *out, const Pixel *in, const Background *back) =
  1613. {
  1614. gpc_Pre, gpc_Preq, gpc_Glin, gpc_Gpre, gpc_Gprq, gpc_nop8, gpc_nop6
  1615. };
  1616. #endif
  1617. /* OUT: ----- 8-bit ----- ----- 16-bit -----
  1618. * IN G GA RGB RGBA G GA RGB RGBA
  1619. * 8 G . . . . lin lin lin lin
  1620. * 8 GA bckg . bckc . pre' pre pre' pre
  1621. * 8 RGB g8 g8 . . glin glin lin lin
  1622. * 8 RGBA g8b g8 bckc . gpr' gpre pre' pre
  1623. * 16 G sRGB sRGB sRGB sRGB . . . .
  1624. * 16 GA b16g unpg b16c unpc A . A .
  1625. * 16 RGB sG sG sRGB sRGB g16 g16 . .
  1626. * 16 RGBA gb16 sGp cb16 sCp g16 g16' A .
  1627. *
  1628. * The matrix is held in an array indexed thus:
  1629. *
  1630. * gpc_fn[out_format & BASE_FORMATS][in_format & BASE_FORMATS];
  1631. */
  1632. /* This will produce a compile time error if the FORMAT_FLAG values don't
  1633. * match the above matrix!
  1634. */
  1635. #if PNG_FORMAT_FLAG_ALPHA == 1 && PNG_FORMAT_FLAG_COLOR == 2 &&\
  1636. PNG_FORMAT_FLAG_LINEAR == 4
  1637. static void (* const gpc_fn[8/*in*/][8/*out*/])
  1638. (Pixel *out, const Pixel *in, const Background *back) =
  1639. {
  1640. /*out: G-8 GA-8 RGB-8 RGBA-8 G-16 GA-16 RGB-16 RGBA-16 */
  1641. {gpc_noop,gpc_noop,gpc_noop,gpc_noop, gpc_Lin, gpc_Lin, gpc_Lin, gpc_Lin },
  1642. {gpc_bckg,gpc_noop,gpc_bckc,gpc_noop, gpc_preq,gpc_pre, gpc_preq,gpc_pre },
  1643. {gpc_g8, gpc_g8, gpc_noop,gpc_noop, gpc_glin,gpc_glin,gpc_lin, gpc_lin },
  1644. {gpc_g8b, gpc_g8, gpc_bckc,gpc_noop, gpc_gprq,gpc_gpre,gpc_preq,gpc_pre },
  1645. {gpc_sRGB,gpc_sRGB,gpc_sRGB,gpc_sRGB, gpc_noop,gpc_noop,gpc_noop,gpc_noop},
  1646. {gpc_b16g,gpc_unpg,gpc_b16c,gpc_unpc, gpc_A, gpc_noop,gpc_A, gpc_noop},
  1647. {gpc_sG, gpc_sG, gpc_sRGB,gpc_sRGB, gpc_g16, gpc_g16, gpc_noop,gpc_noop},
  1648. {gpc_gb16,gpc_sGp, gpc_cb16,gpc_sCp, gpc_g16, gpc_g16q,gpc_A, gpc_noop}
  1649. };
  1650. /* The array is repeated for the cases where both the input and output are color
  1651. * mapped because then different algorithms are used.
  1652. */
  1653. static void (* const gpc_fn_colormapped[8/*in*/][8/*out*/])
  1654. (Pixel *out, const Pixel *in, const Background *back) =
  1655. {
  1656. /*out: G-8 GA-8 RGB-8 RGBA-8 G-16 GA-16 RGB-16 RGBA-16 */
  1657. {gpc_noop,gpc_noop,gpc_noop,gpc_noop, gpc_lin, gpc_lin, gpc_lin, gpc_lin },
  1658. {gpc_bckg,gpc_noop,gpc_bckc,gpc_noop, gpc_preq,gpc_pre, gpc_preq,gpc_pre },
  1659. {gpc_g8, gpc_g8, gpc_noop,gpc_noop, gpc_glin,gpc_glin,gpc_lin, gpc_lin },
  1660. {gpc_g8b, gpc_g8, gpc_bckc,gpc_noop, gpc_gprq,gpc_gpre,gpc_preq,gpc_pre },
  1661. {gpc_sRGB,gpc_sRGB,gpc_sRGB,gpc_sRGB, gpc_noop,gpc_noop,gpc_noop,gpc_noop},
  1662. {gpc_b16g,gpc_unpg,gpc_b16c,gpc_unpc, gpc_A, gpc_noop,gpc_A, gpc_noop},
  1663. {gpc_sG, gpc_sG, gpc_sRGB,gpc_sRGB, gpc_g16, gpc_g16, gpc_noop,gpc_noop},
  1664. {gpc_gb16,gpc_sGp, gpc_cb16,gpc_sCp, gpc_g16, gpc_g16q,gpc_A, gpc_noop}
  1665. };
  1666. /* The error arrays record the error in the same matrix; 64 entries, however
  1667. * the different algorithms used in libpng for colormap and direct conversions
  1668. * mean that four separate matrices are used (for each combination of
  1669. * colormapped and direct.)
  1670. *
  1671. * In some cases the conversion between sRGB formats goes via a linear
  1672. * intermediate; an sRGB to linear conversion (as above) is followed by a simple
  1673. * linear to sRGB step with no other conversions. This is done by a separate
  1674. * error array from an arbitrary 'in' format to one of the four basic outputs
  1675. * (since final output is always sRGB not colormapped).
  1676. *
  1677. * These arrays may be modified if the --accumulate flag is set during the run;
  1678. * then instead of logging errors they are simply added in.
  1679. *
  1680. * The three entries are currently for transparent, partially transparent and
  1681. * opaque input pixel values. Notice that alpha should be exact in each case.
  1682. *
  1683. * Errors in alpha should only occur when converting from a direct format
  1684. * to a colormapped format, when alpha is effectively smashed (so large
  1685. * errors can occur.) There should be no error in the '0' and 'opaque'
  1686. * values. The fourth entry in the array is used for the alpha error (and it
  1687. * should always be zero for the 'via linear' case since this is never color
  1688. * mapped.)
  1689. *
  1690. * Mapping to a colormap smashes the colors, it is necessary to have separate
  1691. * values for these cases because they are much larger; it is very much
  1692. * impossible to obtain a reasonable result, these are held in
  1693. * gpc_error_to_colormap.
  1694. */
  1695. #if PNG_FORMAT_FLAG_COLORMAP == 8 /* extra check also required */
  1696. # include "pngstest-errors.h" /* machine generated */
  1697. #endif /* COLORMAP flag check */
  1698. #endif /* flag checks */
  1699. typedef struct
  1700. {
  1701. /* Basic pixel information: */
  1702. Image* in_image; /* Input image */
  1703. const Image* out_image; /* Output image */
  1704. /* 'background' is the value passed to the gpc_ routines, it may be NULL if
  1705. * it should not be used (*this* program has an error if it crashes as a
  1706. * result!)
  1707. */
  1708. Background background_color;
  1709. const Background* background;
  1710. /* Precalculated values: */
  1711. int in_opaque; /* Value of input alpha that is opaque */
  1712. int is_palette; /* Sample values come from the palette */
  1713. int accumulate; /* Accumulate component errors (don't log) */
  1714. int output_8bit; /* Output is 8-bit (else 16-bit) */
  1715. void (*in_gp)(Pixel*, png_const_voidp);
  1716. void (*out_gp)(Pixel*, png_const_voidp);
  1717. void (*transform)(Pixel *out, const Pixel *in, const Background *back);
  1718. /* A function to perform the required transform */
  1719. void (*from_linear)(Pixel *out, const Pixel *in, const Background *back);
  1720. /* For 'via_linear' transforms the final, from linear, step, else NULL */
  1721. png_uint_16 error[4];
  1722. /* Three error values for transparent, partially transparent and opaque
  1723. * input pixels (in turn).
  1724. */
  1725. png_uint_16 *error_ptr;
  1726. /* Where these are stored in the static array (for 'accumulate') */
  1727. }
  1728. Transform;
  1729. /* Return a 'transform' as above for the given format conversion. */
  1730. static void
  1731. transform_from_formats(Transform *result, Image *in_image,
  1732. const Image *out_image, png_const_colorp background, int via_linear)
  1733. {
  1734. png_uint_32 in_format, out_format;
  1735. png_uint_32 in_base, out_base;
  1736. memset(result, 0, sizeof *result);
  1737. /* Store the original images for error messages */
  1738. result->in_image = in_image;
  1739. result->out_image = out_image;
  1740. in_format = in_image->image.format;
  1741. out_format = out_image->image.format;
  1742. if (in_format & PNG_FORMAT_FLAG_LINEAR)
  1743. result->in_opaque = 65535;
  1744. else
  1745. result->in_opaque = 255;
  1746. result->output_8bit = (out_format & PNG_FORMAT_FLAG_LINEAR) == 0;
  1747. result->is_palette = 0; /* set by caller if required */
  1748. result->accumulate = (in_image->opts & ACCUMULATE) != 0;
  1749. /* The loaders (which need the ordering information) */
  1750. result->in_gp = get_pixel(in_format);
  1751. result->out_gp = get_pixel(out_format);
  1752. /* Remove the ordering information: */
  1753. in_format &= BASE_FORMATS | PNG_FORMAT_FLAG_COLORMAP;
  1754. in_base = in_format & BASE_FORMATS;
  1755. out_format &= BASE_FORMATS | PNG_FORMAT_FLAG_COLORMAP;
  1756. out_base = out_format & BASE_FORMATS;
  1757. if (via_linear)
  1758. {
  1759. /* Check for an error in this program: */
  1760. if (out_format & (PNG_FORMAT_FLAG_LINEAR|PNG_FORMAT_FLAG_COLORMAP))
  1761. {
  1762. fprintf(stderr, "internal transform via linear error 0x%x->0x%x\n",
  1763. in_format, out_format);
  1764. exit(1);
  1765. }
  1766. result->transform = gpc_fn[in_base][out_base | PNG_FORMAT_FLAG_LINEAR];
  1767. result->from_linear = gpc_fn[out_base | PNG_FORMAT_FLAG_LINEAR][out_base];
  1768. result->error_ptr = gpc_error_via_linear[in_format][out_format];
  1769. }
  1770. else if (~in_format & out_format & PNG_FORMAT_FLAG_COLORMAP)
  1771. {
  1772. /* The input is not colormapped but the output is, the errors will
  1773. * typically be large (only the grayscale-no-alpha case permits preserving
  1774. * even 8-bit values.)
  1775. */
  1776. result->transform = gpc_fn[in_base][out_base];
  1777. result->from_linear = NULL;
  1778. result->error_ptr = gpc_error_to_colormap[in_base][out_base];
  1779. }
  1780. else
  1781. {
  1782. /* The caller handles the colormap->pixel value conversion, so the
  1783. * transform function just gets a pixel value, however because libpng
  1784. * currently contains a different implementation for mapping a colormap if
  1785. * both input and output are colormapped we need different conversion
  1786. * functions to deal with errors in the libpng implementation.
  1787. */
  1788. if (in_format & out_format & PNG_FORMAT_FLAG_COLORMAP)
  1789. result->transform = gpc_fn_colormapped[in_base][out_base];
  1790. else
  1791. result->transform = gpc_fn[in_base][out_base];
  1792. result->from_linear = NULL;
  1793. result->error_ptr = gpc_error[in_format][out_format];
  1794. }
  1795. /* Follow the libpng simplified API rules to work out what to pass to the gpc
  1796. * routines as a background value, if one is not required pass NULL so that
  1797. * this program crashes in the even of a programming error.
  1798. */
  1799. result->background = NULL; /* default: not required */
  1800. /* Rule 1: background only need be supplied if alpha is to be removed */
  1801. if (in_format & ~out_format & PNG_FORMAT_FLAG_ALPHA)
  1802. {
  1803. /* The input value is 'NULL' to use the background and (otherwise) an sRGB
  1804. * background color (to use a solid color). The code above uses a fixed
  1805. * byte value, BUFFER_INIT8, for buffer even for 16-bit output. For
  1806. * linear (16-bit) output the sRGB background color is ignored; the
  1807. * composition is always on the background (so BUFFER_INIT8 * 257), except
  1808. * that for the colormap (i.e. linear colormapped output) black is used.
  1809. */
  1810. result->background = &result->background_color;
  1811. if (out_format & PNG_FORMAT_FLAG_LINEAR || via_linear)
  1812. {
  1813. if (out_format & PNG_FORMAT_FLAG_COLORMAP)
  1814. {
  1815. result->background_color.ir =
  1816. result->background_color.ig =
  1817. result->background_color.ib = 0;
  1818. result->background_color.dr =
  1819. result->background_color.dg =
  1820. result->background_color.db = 0;
  1821. }
  1822. else
  1823. {
  1824. result->background_color.ir =
  1825. result->background_color.ig =
  1826. result->background_color.ib = BUFFER_INIT8 * 257;
  1827. result->background_color.dr =
  1828. result->background_color.dg =
  1829. result->background_color.db = 0;
  1830. }
  1831. }
  1832. else /* sRGB output */
  1833. {
  1834. if (background != NULL)
  1835. {
  1836. if (out_format & PNG_FORMAT_FLAG_COLOR)
  1837. {
  1838. result->background_color.ir = background->red;
  1839. result->background_color.ig = background->green;
  1840. result->background_color.ib = background->blue;
  1841. /* TODO: sometimes libpng uses the power law conversion here, how
  1842. * to handle this?
  1843. */
  1844. result->background_color.dr = sRGB_to_d[background->red];
  1845. result->background_color.dg = sRGB_to_d[background->green];
  1846. result->background_color.db = sRGB_to_d[background->blue];
  1847. }
  1848. else /* grayscale: libpng only looks at 'g' */
  1849. {
  1850. result->background_color.ir =
  1851. result->background_color.ig =
  1852. result->background_color.ib = background->green;
  1853. /* TODO: sometimes libpng uses the power law conversion here, how
  1854. * to handle this?
  1855. */
  1856. result->background_color.dr =
  1857. result->background_color.dg =
  1858. result->background_color.db = sRGB_to_d[background->green];
  1859. }
  1860. }
  1861. else if ((out_format & PNG_FORMAT_FLAG_COLORMAP) == 0)
  1862. {
  1863. result->background_color.ir =
  1864. result->background_color.ig =
  1865. result->background_color.ib = BUFFER_INIT8;
  1866. /* TODO: sometimes libpng uses the power law conversion here, how
  1867. * to handle this?
  1868. */
  1869. result->background_color.dr =
  1870. result->background_color.dg =
  1871. result->background_color.db = sRGB_to_d[BUFFER_INIT8];
  1872. }
  1873. /* Else the output is colormapped and a background color must be
  1874. * provided; if pngstest crashes then that is a bug in this program
  1875. * (though libpng should png_error as well.)
  1876. */
  1877. else
  1878. result->background = NULL;
  1879. }
  1880. }
  1881. if (result->background == NULL)
  1882. {
  1883. result->background_color.ir =
  1884. result->background_color.ig =
  1885. result->background_color.ib = -1; /* not used */
  1886. result->background_color.dr =
  1887. result->background_color.dg =
  1888. result->background_color.db = 1E30; /* not used */
  1889. }
  1890. /* Copy the error values into the Transform: */
  1891. result->error[0] = result->error_ptr[0];
  1892. result->error[1] = result->error_ptr[1];
  1893. result->error[2] = result->error_ptr[2];
  1894. result->error[3] = result->error_ptr[3];
  1895. }
  1896. /* Compare two pixels.
  1897. *
  1898. * OLD error values:
  1899. static int error_to_linear = 811; * by experiment *
  1900. static int error_to_linear_grayscale = 424; * by experiment *
  1901. static int error_to_sRGB = 6; * by experiment *
  1902. static int error_to_sRGB_grayscale = 17; * libpng error by calculation +
  1903. 2 by experiment *
  1904. static int error_in_compose = 2; * by experiment *
  1905. static int error_in_premultiply = 1;
  1906. *
  1907. * The following is *just* the result of a round trip from 8-bit sRGB to linear
  1908. * then back to 8-bit sRGB when it is done by libpng. There are two problems:
  1909. *
  1910. * 1) libpng currently uses a 2.2 power law with no linear segment, this results
  1911. * in instability in the low values and even with 16-bit precision sRGB(1) ends
  1912. * up mapping to sRGB(0) as a result of rounding in the 16-bit representation.
  1913. * This gives an error of 1 in the handling of value 1 only.
  1914. *
  1915. * 2) libpng currently uses an intermediate 8-bit linear value in gamma
  1916. * correction of 8-bit values. This results in many more errors, the worse of
  1917. * which is mapping sRGB(14) to sRGB(0).
  1918. *
  1919. * The general 'error_via_linear' is more complex because of pre-multiplication,
  1920. * this compounds the 8-bit errors according to the alpha value of the pixel.
  1921. * As a result 256 values are pre-calculated for error_via_linear.
  1922. */
  1923. #if 0
  1924. static int error_in_libpng_gamma;
  1925. static int error_via_linear[256]; /* Indexed by 8-bit alpha */
  1926. static void
  1927. init_error_via_linear(void)
  1928. {
  1929. int alpha;
  1930. error_via_linear[0] = 255; /* transparent pixel */
  1931. for (alpha=1; alpha<=255; ++alpha)
  1932. {
  1933. /* 16-bit values less than 128.5 get rounded to 8-bit 0 and so the worst
  1934. * case error arises with 16-bit 128.5, work out what sRGB
  1935. * (non-associated) value generates 128.5; any value less than this is
  1936. * going to map to 0, so the worst error is floor(value).
  1937. *
  1938. * Note that errors are considerably higher (more than a factor of 2)
  1939. * because libpng uses a simple power law for sRGB data at present.
  1940. *
  1941. * Add .1 for arithmetic errors inside libpng.
  1942. */
  1943. double v = floor(255*pow(.5/*(128.5 * 255 / 65535)*/ / alpha, 1/2.2)+.1);
  1944. error_via_linear[alpha] = (int)v;
  1945. }
  1946. /* This is actually 14.99, but, despite the closeness to 15, 14 seems to work
  1947. * ok in this case.
  1948. */
  1949. error_in_libpng_gamma = 14;
  1950. }
  1951. #endif
  1952. static void
  1953. print_pixel(char string[64], const Pixel *pixel, png_uint_32 format)
  1954. {
  1955. switch (format & (PNG_FORMAT_FLAG_ALPHA|PNG_FORMAT_FLAG_COLOR))
  1956. {
  1957. case 0:
  1958. sprintf(string, "%s(%d)", format_names[format], pixel->g);
  1959. break;
  1960. case PNG_FORMAT_FLAG_ALPHA:
  1961. sprintf(string, "%s(%d,%d)", format_names[format], pixel->g,
  1962. pixel->a);
  1963. break;
  1964. case PNG_FORMAT_FLAG_COLOR:
  1965. sprintf(string, "%s(%d,%d,%d)", format_names[format],
  1966. pixel->r, pixel->g, pixel->b);
  1967. break;
  1968. case PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_ALPHA:
  1969. sprintf(string, "%s(%d,%d,%d,%d)", format_names[format],
  1970. pixel->r, pixel->g, pixel->b, pixel->a);
  1971. break;
  1972. default:
  1973. sprintf(string, "invalid-format");
  1974. break;
  1975. }
  1976. }
  1977. static int
  1978. logpixel(const Transform *transform, png_uint_32 x, png_uint_32 y,
  1979. const Pixel *in, const Pixel *calc, const Pixel *out, const char *reason)
  1980. {
  1981. png_uint_32 in_format = transform->in_image->image.format;
  1982. png_uint_32 out_format = transform->out_image->image.format;
  1983. png_uint_32 back_format = out_format & ~PNG_FORMAT_FLAG_ALPHA;
  1984. const char *via_linear = "";
  1985. char pixel_in[64], pixel_calc[64], pixel_out[64], pixel_loc[64];
  1986. char background_info[100];
  1987. print_pixel(pixel_in, in, in_format);
  1988. print_pixel(pixel_calc, calc, out_format);
  1989. print_pixel(pixel_out, out, out_format);
  1990. if (transform->is_palette)
  1991. sprintf(pixel_loc, "palette: %lu", (unsigned long)y);
  1992. else
  1993. sprintf(pixel_loc, "%lu,%lu", (unsigned long)x, (unsigned long)y);
  1994. if (transform->from_linear != NULL)
  1995. {
  1996. via_linear = " (via linear)";
  1997. /* And as a result the *read* format which did any background processing
  1998. * was itself linear, so the background color information is also
  1999. * linear.
  2000. */
  2001. back_format |= PNG_FORMAT_FLAG_LINEAR;
  2002. }
  2003. if (transform->background != NULL)
  2004. {
  2005. Pixel back;
  2006. char pixel_back[64];
  2007. back.r = transform->background->ir;
  2008. back.g = transform->background->ig;
  2009. back.b = transform->background->ib;
  2010. back.a = -1; /* not used */
  2011. print_pixel(pixel_back, &back, back_format);
  2012. sprintf(background_info, " on background %s", pixel_back);
  2013. }
  2014. else
  2015. background_info[0] = 0;
  2016. if (transform->in_image->file_name != transform->out_image->file_name)
  2017. {
  2018. char error_buffer[512];
  2019. sprintf(error_buffer,
  2020. "(%s) %s error%s:\n %s%s ->\n %s\n not: %s.\n"
  2021. "Use --preserve and examine: ", pixel_loc, reason, via_linear,
  2022. pixel_in, background_info, pixel_out, pixel_calc);
  2023. return logerror(transform->in_image, transform->in_image->file_name,
  2024. error_buffer, transform->out_image->file_name);
  2025. }
  2026. else
  2027. {
  2028. char error_buffer[512];
  2029. sprintf(error_buffer,
  2030. "(%s) %s error%s:\n %s%s ->\n %s\n not: %s.\n"
  2031. " The error happened when reading the original file with this format.",
  2032. pixel_loc, reason, via_linear, pixel_in, background_info, pixel_out,
  2033. pixel_calc);
  2034. return logerror(transform->in_image, transform->in_image->file_name,
  2035. error_buffer, "");
  2036. }
  2037. }
  2038. static int
  2039. cmppixel(Transform *transform, png_const_voidp in, png_const_voidp out,
  2040. png_uint_32 x, png_uint_32 y/*or palette index*/)
  2041. {
  2042. int maxerr;
  2043. png_const_charp errmsg;
  2044. Pixel pixel_in, pixel_calc, pixel_out;
  2045. transform->in_gp(&pixel_in, in);
  2046. if (transform->from_linear == NULL)
  2047. transform->transform(&pixel_calc, &pixel_in, transform->background);
  2048. else
  2049. {
  2050. transform->transform(&pixel_out, &pixel_in, transform->background);
  2051. transform->from_linear(&pixel_calc, &pixel_out, NULL);
  2052. }
  2053. transform->out_gp(&pixel_out, out);
  2054. /* Eliminate the case where the input and output values match exactly. */
  2055. if (pixel_calc.a == pixel_out.a && pixel_calc.r == pixel_out.r &&
  2056. pixel_calc.g == pixel_out.g && pixel_calc.b == pixel_out.b)
  2057. return 1;
  2058. /* Eliminate the case where the output pixel is transparent and the output
  2059. * is 8-bit - any component values are valid. Don't check the input alpha
  2060. * here to also skip the 16-bit small alpha cases.
  2061. */
  2062. if (transform->output_8bit && pixel_calc.a == 0 && pixel_out.a == 0)
  2063. return 1;
  2064. /* Check for alpha errors first; an alpha error can damage the components too
  2065. * so avoid spurious checks on components if one is found.
  2066. */
  2067. errmsg = NULL;
  2068. {
  2069. int err_a = abs(pixel_calc.a-pixel_out.a);
  2070. if (err_a > transform->error[3])
  2071. {
  2072. /* If accumulating check the components too */
  2073. if (transform->accumulate)
  2074. transform->error[3] = (png_uint_16)err_a;
  2075. else
  2076. errmsg = "alpha";
  2077. }
  2078. }
  2079. /* Now if *either* of the output alphas are 0 but alpha is within tolerance
  2080. * eliminate the 8-bit component comparison.
  2081. */
  2082. if (errmsg == NULL && transform->output_8bit &&
  2083. (pixel_calc.a == 0 || pixel_out.a == 0))
  2084. return 1;
  2085. if (errmsg == NULL) /* else just signal an alpha error */
  2086. {
  2087. int err_r = abs(pixel_calc.r - pixel_out.r);
  2088. int err_g = abs(pixel_calc.g - pixel_out.g);
  2089. int err_b = abs(pixel_calc.b - pixel_out.b);
  2090. int limit;
  2091. if ((err_r | err_g | err_b) == 0)
  2092. return 1; /* exact match */
  2093. /* Mismatch on a component, check the input alpha */
  2094. if (pixel_in.a >= transform->in_opaque)
  2095. {
  2096. errmsg = "opaque component";
  2097. limit = 2; /* opaque */
  2098. }
  2099. else if (pixel_in.a > 0)
  2100. {
  2101. errmsg = "alpha component";
  2102. limit = 1; /* partially transparent */
  2103. }
  2104. else
  2105. {
  2106. errmsg = "transparent component (background)";
  2107. limit = 0; /* transparent */
  2108. }
  2109. maxerr = err_r;
  2110. if (maxerr < err_g) maxerr = err_g;
  2111. if (maxerr < err_b) maxerr = err_b;
  2112. if (maxerr <= transform->error[limit])
  2113. return 1; /* within the error limits */
  2114. /* Handle a component mis-match; log it, just return an error code, or
  2115. * accumulate it.
  2116. */
  2117. if (transform->accumulate)
  2118. {
  2119. transform->error[limit] = (png_uint_16)maxerr;
  2120. return 1; /* to cause the caller to keep going */
  2121. }
  2122. }
  2123. /* Failure to match and not accumulating, so the error must be logged. */
  2124. return logpixel(transform, x, y, &pixel_in, &pixel_calc, &pixel_out, errmsg);
  2125. }
  2126. static png_byte
  2127. component_loc(png_byte loc[4], png_uint_32 format)
  2128. {
  2129. /* Given a format return the number of channels and the location of
  2130. * each channel.
  2131. *
  2132. * The mask 'loc' contains the component offset of the channels in the
  2133. * following order. Note that if 'format' is grayscale the entries 1-3 must
  2134. * all contain the location of the gray channel.
  2135. *
  2136. * 0: alpha
  2137. * 1: red or gray
  2138. * 2: green or gray
  2139. * 3: blue or gray
  2140. */
  2141. png_byte channels;
  2142. if (format & PNG_FORMAT_FLAG_COLOR)
  2143. {
  2144. channels = 3;
  2145. loc[2] = 1;
  2146. # ifdef PNG_FORMAT_BGR_SUPPORTED
  2147. if (format & PNG_FORMAT_FLAG_BGR)
  2148. {
  2149. loc[1] = 2;
  2150. loc[3] = 0;
  2151. }
  2152. else
  2153. # endif
  2154. {
  2155. loc[1] = 0;
  2156. loc[3] = 2;
  2157. }
  2158. }
  2159. else
  2160. {
  2161. channels = 1;
  2162. loc[1] = loc[2] = loc[3] = 0;
  2163. }
  2164. if (format & PNG_FORMAT_FLAG_ALPHA)
  2165. {
  2166. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  2167. if (format & PNG_FORMAT_FLAG_AFIRST)
  2168. {
  2169. loc[0] = 0;
  2170. ++loc[1];
  2171. ++loc[2];
  2172. ++loc[3];
  2173. }
  2174. else
  2175. # endif
  2176. loc[0] = channels;
  2177. ++channels;
  2178. }
  2179. else
  2180. loc[0] = 4; /* not present */
  2181. return channels;
  2182. }
  2183. /* Compare two images, the original 'a', which was written out then read back in
  2184. * to * give image 'b'. The formats may have been changed.
  2185. */
  2186. static int
  2187. compare_two_images(Image *a, Image *b, int via_linear,
  2188. png_const_colorp background)
  2189. {
  2190. ptrdiff_t stridea = a->stride;
  2191. ptrdiff_t strideb = b->stride;
  2192. png_const_bytep rowa = a->buffer+16;
  2193. png_const_bytep rowb = b->buffer+16;
  2194. png_uint_32 width = a->image.width;
  2195. png_uint_32 height = a->image.height;
  2196. png_uint_32 formata = a->image.format;
  2197. png_uint_32 formatb = b->image.format;
  2198. unsigned int a_sample = PNG_IMAGE_SAMPLE_SIZE(formata);
  2199. unsigned int b_sample = PNG_IMAGE_SAMPLE_SIZE(formatb);
  2200. int alpha_added, alpha_removed;
  2201. int bchannels;
  2202. png_uint_32 y;
  2203. Transform tr;
  2204. int btoa[4]={0,0,0,0};
  2205. /* This should never happen: */
  2206. if (width != b->image.width || height != b->image.height)
  2207. return logerror(a, a->file_name, ": width x height changed: ",
  2208. b->file_name);
  2209. /* Set up the background and the transform */
  2210. transform_from_formats(&tr, a, b, background, via_linear);
  2211. /* Find the first row and inter-row space. */
  2212. if (!(formata & PNG_FORMAT_FLAG_COLORMAP) &&
  2213. (formata & PNG_FORMAT_FLAG_LINEAR))
  2214. stridea *= 2;
  2215. if (!(formatb & PNG_FORMAT_FLAG_COLORMAP) &&
  2216. (formatb & PNG_FORMAT_FLAG_LINEAR))
  2217. strideb *= 2;
  2218. if (stridea < 0) rowa += (height-1) * (-stridea);
  2219. if (strideb < 0) rowb += (height-1) * (-strideb);
  2220. /* First shortcut the two colormap case by comparing the image data; if it
  2221. * matches then we expect the colormaps to match, although this is not
  2222. * absolutely necessary for an image match. If the colormaps fail to match
  2223. * then there is a problem in libpng.
  2224. */
  2225. if (formata & formatb & PNG_FORMAT_FLAG_COLORMAP)
  2226. {
  2227. /* Only check colormap entries that actually exist; */
  2228. png_const_bytep ppa, ppb;
  2229. int match;
  2230. png_byte in_use[256], amax = 0, bmax = 0;
  2231. memset(in_use, 0, sizeof in_use);
  2232. ppa = rowa;
  2233. ppb = rowb;
  2234. /* Do this the slow way to accumulate the 'in_use' flags, don't break out
  2235. * of the loop until the end; this validates the color-mapped data to
  2236. * ensure all pixels are valid color-map indexes.
  2237. */
  2238. for (y=0, match=1; y<height && match; ++y, ppa += stridea, ppb += strideb)
  2239. {
  2240. png_uint_32 x;
  2241. for (x=0; x<width; ++x)
  2242. {
  2243. png_byte bval = ppb[x];
  2244. png_byte aval = ppa[x];
  2245. if (bval > bmax)
  2246. bmax = bval;
  2247. if (bval != aval)
  2248. match = 0;
  2249. in_use[aval] = 1;
  2250. if (aval > amax)
  2251. amax = aval;
  2252. }
  2253. }
  2254. /* If the buffers match then the colormaps must too. */
  2255. if (match)
  2256. {
  2257. /* Do the color-maps match, entry by entry? Only check the 'in_use'
  2258. * entries. An error here should be logged as a color-map error.
  2259. */
  2260. png_const_bytep a_cmap = (png_const_bytep)a->colormap;
  2261. png_const_bytep b_cmap = (png_const_bytep)b->colormap;
  2262. int result = 1; /* match by default */
  2263. /* This is used in logpixel to get the error message correct. */
  2264. tr.is_palette = 1;
  2265. for (y=0; y<256; ++y, a_cmap += a_sample, b_cmap += b_sample)
  2266. if (in_use[y])
  2267. {
  2268. /* The colormap entries should be valid, but because libpng doesn't
  2269. * do any checking at present the original image may contain invalid
  2270. * pixel values. These cause an error here (at present) unless
  2271. * accumulating errors in which case the program just ignores them.
  2272. */
  2273. if (y >= a->image.colormap_entries)
  2274. {
  2275. if ((a->opts & ACCUMULATE) == 0)
  2276. {
  2277. char pindex[9];
  2278. sprintf(pindex, "%lu[%lu]", (unsigned long)y,
  2279. (unsigned long)a->image.colormap_entries);
  2280. logerror(a, a->file_name, ": bad pixel index: ", pindex);
  2281. }
  2282. result = 0;
  2283. }
  2284. else if (y >= b->image.colormap_entries)
  2285. {
  2286. if ((b->opts & ACCUMULATE) == 0)
  2287. {
  2288. char pindex[9];
  2289. sprintf(pindex, "%lu[%lu]", (unsigned long)y,
  2290. (unsigned long)b->image.colormap_entries);
  2291. logerror(b, b->file_name, ": bad pixel index: ", pindex);
  2292. }
  2293. result = 0;
  2294. }
  2295. /* All the mismatches are logged here; there can only be 256! */
  2296. else if (!cmppixel(&tr, a_cmap, b_cmap, 0, y))
  2297. result = 0;
  2298. }
  2299. /* If requested, copy the error values back from the Transform. */
  2300. if (a->opts & ACCUMULATE)
  2301. {
  2302. tr.error_ptr[0] = tr.error[0];
  2303. tr.error_ptr[1] = tr.error[1];
  2304. tr.error_ptr[2] = tr.error[2];
  2305. tr.error_ptr[3] = tr.error[3];
  2306. result = 1; /* force a continue */
  2307. }
  2308. return result;
  2309. }
  2310. /* else the image buffers don't match pixel-wise so compare sample values
  2311. * instead, but first validate that the pixel indexes are in range (but
  2312. * only if not accumulating, when the error is ignored.)
  2313. */
  2314. else if ((a->opts & ACCUMULATE) == 0)
  2315. {
  2316. # ifdef __GNUC__
  2317. # define BYTE_CHARS 20 /* 2^32: GCC sprintf warning */
  2318. # else
  2319. # define BYTE_CHARS 3 /* 2^8: real maximum value */
  2320. # endif
  2321. /* Check the original image first,
  2322. * TODO: deal with input images with bad pixel values?
  2323. */
  2324. if (amax >= a->image.colormap_entries)
  2325. {
  2326. char pindex[3+2*BYTE_CHARS];
  2327. sprintf(pindex, "%d[%u]", amax,
  2328. (png_byte)/*SAFE*/a->image.colormap_entries);
  2329. return logerror(a, a->file_name, ": bad pixel index: ", pindex);
  2330. }
  2331. else if (bmax >= b->image.colormap_entries)
  2332. {
  2333. char pindex[3+2*BYTE_CHARS];
  2334. sprintf(pindex, "%d[%u]", bmax,
  2335. (png_byte)/*SAFE*/b->image.colormap_entries);
  2336. return logerror(b, b->file_name, ": bad pixel index: ", pindex);
  2337. }
  2338. }
  2339. }
  2340. /* We can directly compare pixel values without the need to use the read
  2341. * or transform support (i.e. a memory compare) if:
  2342. *
  2343. * 1) The bit depth has not changed.
  2344. * 2) RGB to grayscale has not been done (the reverse is ok; we just compare
  2345. * the three RGB values to the original grayscale.)
  2346. * 3) An alpha channel has not been removed from an 8-bit format, or the
  2347. * 8-bit alpha value of the pixel was 255 (opaque).
  2348. *
  2349. * If an alpha channel has been *added* then it must have the relevant opaque
  2350. * value (255 or 65535).
  2351. *
  2352. * The fist two the tests (in the order given above) (using the boolean
  2353. * equivalence !a && !b == !(a || b))
  2354. */
  2355. if (!(((formata ^ formatb) & PNG_FORMAT_FLAG_LINEAR) |
  2356. (formata & (formatb ^ PNG_FORMAT_FLAG_COLOR) & PNG_FORMAT_FLAG_COLOR)))
  2357. {
  2358. /* Was an alpha channel changed? */
  2359. png_uint_32 alpha_changed = (formata ^ formatb) & PNG_FORMAT_FLAG_ALPHA;
  2360. /* Was an alpha channel removed? (The third test.) If so the direct
  2361. * comparison is only possible if the input alpha is opaque.
  2362. */
  2363. alpha_removed = (formata & alpha_changed) != 0;
  2364. /* Was an alpha channel added? */
  2365. alpha_added = (formatb & alpha_changed) != 0;
  2366. /* The channels may have been moved between input and output, this finds
  2367. * out how, recording the result in the btoa array, which says where in
  2368. * 'a' to find each channel of 'b'. If alpha was added then btoa[alpha]
  2369. * ends up as 4 (and is not used.)
  2370. */
  2371. {
  2372. int i;
  2373. png_byte aloc[4];
  2374. png_byte bloc[4];
  2375. /* The following are used only if the formats match, except that
  2376. * 'bchannels' is a flag for matching formats. btoa[x] says, for each
  2377. * channel in b, where to find the corresponding value in a, for the
  2378. * bchannels. achannels may be different for a gray to rgb transform
  2379. * (a will be 1 or 2, b will be 3 or 4 channels.)
  2380. */
  2381. (void)component_loc(aloc, formata);
  2382. bchannels = component_loc(bloc, formatb);
  2383. /* Hence the btoa array. */
  2384. for (i=0; i<4; ++i) if (bloc[i] < 4)
  2385. btoa[bloc[i]] = aloc[i]; /* may be '4' for alpha */
  2386. if (alpha_added)
  2387. alpha_added = bloc[0]; /* location of alpha channel in image b */
  2388. else
  2389. alpha_added = 4; /* Won't match an image b channel */
  2390. if (alpha_removed)
  2391. alpha_removed = aloc[0]; /* location of alpha channel in image a */
  2392. else
  2393. alpha_removed = 4;
  2394. }
  2395. }
  2396. else
  2397. {
  2398. /* Direct compare is not possible, cancel out all the corresponding local
  2399. * variables.
  2400. */
  2401. bchannels = 0;
  2402. alpha_removed = alpha_added = 4;
  2403. btoa[3] = btoa[2] = btoa[1] = btoa[0] = 4; /* 4 == not present */
  2404. }
  2405. for (y=0; y<height; ++y, rowa += stridea, rowb += strideb)
  2406. {
  2407. png_const_bytep ppa, ppb;
  2408. png_uint_32 x;
  2409. for (x=0, ppa=rowa, ppb=rowb; x<width; ++x)
  2410. {
  2411. png_const_bytep psa, psb;
  2412. if (formata & PNG_FORMAT_FLAG_COLORMAP)
  2413. psa = (png_const_bytep)a->colormap + a_sample * *ppa++;
  2414. else
  2415. psa = ppa, ppa += a_sample;
  2416. if (formatb & PNG_FORMAT_FLAG_COLORMAP)
  2417. psb = (png_const_bytep)b->colormap + b_sample * *ppb++;
  2418. else
  2419. psb = ppb, ppb += b_sample;
  2420. /* Do the fast test if possible. */
  2421. if (bchannels)
  2422. {
  2423. /* Check each 'b' channel against either the corresponding 'a'
  2424. * channel or the opaque alpha value, as appropriate. If
  2425. * alpha_removed value is set (not 4) then also do this only if the
  2426. * 'a' alpha channel (alpha_removed) is opaque; only relevant for
  2427. * the 8-bit case.
  2428. */
  2429. if (formatb & PNG_FORMAT_FLAG_LINEAR) /* 16-bit checks */
  2430. {
  2431. png_const_uint_16p pua = aligncastconst(png_const_uint_16p, psa);
  2432. png_const_uint_16p pub = aligncastconst(png_const_uint_16p, psb);
  2433. switch (bchannels)
  2434. {
  2435. case 4:
  2436. if (pua[btoa[3]] != pub[3]) break;
  2437. /* FALLTHROUGH */
  2438. case 3:
  2439. if (pua[btoa[2]] != pub[2]) break;
  2440. /* FALLTHROUGH */
  2441. case 2:
  2442. if (pua[btoa[1]] != pub[1]) break;
  2443. /* FALLTHROUGH */
  2444. case 1:
  2445. if (pua[btoa[0]] != pub[0]) break;
  2446. if (alpha_added != 4 && pub[alpha_added] != 65535) break;
  2447. continue; /* x loop */
  2448. default:
  2449. break; /* impossible */
  2450. }
  2451. }
  2452. else if (alpha_removed == 4 || psa[alpha_removed] == 255)
  2453. {
  2454. switch (bchannels)
  2455. {
  2456. case 4:
  2457. if (psa[btoa[3]] != psb[3]) break;
  2458. /* FALLTHROUGH */
  2459. case 3:
  2460. if (psa[btoa[2]] != psb[2]) break;
  2461. /* FALLTHROUGH */
  2462. case 2:
  2463. if (psa[btoa[1]] != psb[1]) break;
  2464. /* FALLTHROUGH */
  2465. case 1:
  2466. if (psa[btoa[0]] != psb[0]) break;
  2467. if (alpha_added != 4 && psb[alpha_added] != 255) break;
  2468. continue; /* x loop */
  2469. default:
  2470. break; /* impossible */
  2471. }
  2472. }
  2473. }
  2474. /* If we get to here the fast match failed; do the slow match for this
  2475. * pixel.
  2476. */
  2477. if (!cmppixel(&tr, psa, psb, x, y) && (a->opts & KEEP_GOING) == 0)
  2478. return 0; /* error case */
  2479. }
  2480. }
  2481. /* If requested, copy the error values back from the Transform. */
  2482. if (a->opts & ACCUMULATE)
  2483. {
  2484. tr.error_ptr[0] = tr.error[0];
  2485. tr.error_ptr[1] = tr.error[1];
  2486. tr.error_ptr[2] = tr.error[2];
  2487. tr.error_ptr[3] = tr.error[3];
  2488. }
  2489. return 1;
  2490. }
  2491. /* Read the file; how the read gets done depends on which of input_file and
  2492. * input_memory have been set.
  2493. */
  2494. static int
  2495. read_file(Image *image, png_uint_32 format, png_const_colorp background)
  2496. {
  2497. memset(&image->image, 0, sizeof image->image);
  2498. image->image.version = PNG_IMAGE_VERSION;
  2499. if (image->input_memory != NULL)
  2500. {
  2501. if (!png_image_begin_read_from_memory(&image->image, image->input_memory,
  2502. image->input_memory_size))
  2503. return logerror(image, "memory init: ", image->file_name, "");
  2504. }
  2505. # ifdef PNG_STDIO_SUPPORTED
  2506. else if (image->input_file != NULL)
  2507. {
  2508. if (!png_image_begin_read_from_stdio(&image->image, image->input_file))
  2509. return logerror(image, "stdio init: ", image->file_name, "");
  2510. }
  2511. else
  2512. {
  2513. if (!png_image_begin_read_from_file(&image->image, image->file_name))
  2514. return logerror(image, "file init: ", image->file_name, "");
  2515. }
  2516. # else
  2517. else
  2518. {
  2519. return logerror(image, "unsupported file/stdio init: ",
  2520. image->file_name, "");
  2521. }
  2522. # endif
  2523. /* This must be set after the begin_read call: */
  2524. if (image->opts & sRGB_16BIT)
  2525. image->image.flags |= PNG_IMAGE_FLAG_16BIT_sRGB;
  2526. /* Have an initialized image with all the data we need plus, maybe, an
  2527. * allocated file (myfile) or buffer (mybuffer) that need to be freed.
  2528. */
  2529. {
  2530. int result;
  2531. png_uint_32 image_format;
  2532. /* Print both original and output formats. */
  2533. image_format = image->image.format;
  2534. if (image->opts & VERBOSE)
  2535. {
  2536. printf("%s %lu x %lu %s -> %s", image->file_name,
  2537. (unsigned long)image->image.width,
  2538. (unsigned long)image->image.height,
  2539. format_names[image_format & FORMAT_MASK],
  2540. (format & FORMAT_NO_CHANGE) != 0 || image->image.format == format
  2541. ? "no change" : format_names[format & FORMAT_MASK]);
  2542. if (background != NULL)
  2543. printf(" background(%d,%d,%d)\n", background->red,
  2544. background->green, background->blue);
  2545. else
  2546. printf("\n");
  2547. fflush(stdout);
  2548. }
  2549. /* 'NO_CHANGE' combined with the color-map flag forces the base format
  2550. * flags to be set on read to ensure that the original representation is
  2551. * not lost in the pass through a colormap format.
  2552. */
  2553. if ((format & FORMAT_NO_CHANGE) != 0)
  2554. {
  2555. if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0 &&
  2556. (image_format & PNG_FORMAT_FLAG_COLORMAP) != 0)
  2557. format = (image_format & ~BASE_FORMATS) | (format & BASE_FORMATS);
  2558. else
  2559. format = image_format;
  2560. }
  2561. image->image.format = format;
  2562. image->stride = PNG_IMAGE_ROW_STRIDE(image->image) + image->stride_extra;
  2563. allocbuffer(image);
  2564. result = png_image_finish_read(&image->image, background,
  2565. image->buffer+16, (png_int_32)image->stride, image->colormap);
  2566. checkbuffer(image, image->file_name);
  2567. if (result)
  2568. return checkopaque(image);
  2569. else
  2570. return logerror(image, image->file_name, ": image read failed", "");
  2571. }
  2572. }
  2573. /* Reads from a filename, which must be in image->file_name, but uses
  2574. * image->opts to choose the method. The file is always read in its native
  2575. * format (the one the simplified API suggests).
  2576. */
  2577. static int
  2578. read_one_file(Image *image)
  2579. {
  2580. if (!(image->opts & USE_FILE) || (image->opts & USE_STDIO))
  2581. {
  2582. /* memory or stdio. */
  2583. FILE *f = fopen(image->file_name, "rb");
  2584. if (f != NULL)
  2585. {
  2586. if (image->opts & USE_FILE)
  2587. image->input_file = f;
  2588. else /* memory */
  2589. {
  2590. if (fseek(f, 0, SEEK_END) == 0)
  2591. {
  2592. long int cb = ftell(f);
  2593. if (cb > 0)
  2594. {
  2595. #ifndef __COVERITY__
  2596. if ((unsigned long int)cb <= (size_t)~(size_t)0)
  2597. #endif
  2598. {
  2599. png_bytep b = voidcast(png_bytep, malloc((size_t)cb));
  2600. if (b != NULL)
  2601. {
  2602. rewind(f);
  2603. if (fread(b, (size_t)cb, 1, f) == 1)
  2604. {
  2605. fclose(f);
  2606. image->input_memory_size = cb;
  2607. image->input_memory = b;
  2608. }
  2609. else
  2610. {
  2611. free(b);
  2612. return logclose(image, f, image->file_name,
  2613. ": read failed: ");
  2614. }
  2615. }
  2616. else
  2617. return logclose(image, f, image->file_name,
  2618. ": out of memory: ");
  2619. }
  2620. else
  2621. return logclose(image, f, image->file_name,
  2622. ": file too big for this architecture: ");
  2623. /* cb is the length of the file as a (long) and
  2624. * this is greater than the maximum amount of
  2625. * memory that can be requested from malloc.
  2626. */
  2627. }
  2628. else if (cb == 0)
  2629. return logclose(image, f, image->file_name,
  2630. ": zero length: ");
  2631. else
  2632. return logclose(image, f, image->file_name,
  2633. ": tell failed: ");
  2634. }
  2635. else
  2636. return logclose(image, f, image->file_name, ": seek failed: ");
  2637. }
  2638. }
  2639. else
  2640. return logerror(image, image->file_name, ": open failed: ",
  2641. strerror(errno));
  2642. }
  2643. return read_file(image, FORMAT_NO_CHANGE, NULL);
  2644. }
  2645. #ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
  2646. static int
  2647. write_one_file(Image *output, Image *image, int convert_to_8bit)
  2648. {
  2649. if (image->opts & FAST_WRITE)
  2650. image->image.flags |= PNG_IMAGE_FLAG_FAST;
  2651. if (image->opts & USE_STDIO)
  2652. {
  2653. #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
  2654. #ifndef __COVERITY__
  2655. FILE *f = tmpfile();
  2656. #else
  2657. /* Experimental. Coverity says tmpfile() is insecure because it
  2658. * generates predictable names.
  2659. *
  2660. * It is possible to satisfy Coverity by using mkstemp(); however,
  2661. * any platform supporting mkstemp() undoubtedly has a secure tmpfile()
  2662. * implementation as well, and doesn't need the fix. Note that
  2663. * the fix won't work on platforms that don't support mkstemp().
  2664. *
  2665. * https://www.securecoding.cert.org/confluence/display/c/
  2666. * FIO21-C.+Do+not+create+temporary+files+in+shared+directories
  2667. * says that most historic implementations of tmpfile() provide
  2668. * only a limited number of possible temporary file names
  2669. * (usually 26) before file names are recycled. That article also
  2670. * provides a secure solution that unfortunately depends upon mkstemp().
  2671. */
  2672. char tmpfile[] = "pngstest-XXXXXX";
  2673. int filedes;
  2674. FILE *f;
  2675. umask(0177);
  2676. filedes = mkstemp(tmpfile);
  2677. if (filedes < 0)
  2678. f = NULL;
  2679. else
  2680. {
  2681. f = fdopen(filedes,"w+");
  2682. /* Hide the filename immediately and ensure that the file does
  2683. * not exist after the program ends
  2684. */
  2685. (void) unlink(tmpfile);
  2686. }
  2687. #endif
  2688. if (f != NULL)
  2689. {
  2690. if (png_image_write_to_stdio(&image->image, f, convert_to_8bit,
  2691. image->buffer+16, (png_int_32)image->stride, image->colormap))
  2692. {
  2693. if (fflush(f) == 0)
  2694. {
  2695. rewind(f);
  2696. initimage(output, image->opts, "tmpfile", image->stride_extra);
  2697. output->input_file = f;
  2698. if (!checkopaque(image))
  2699. return 0;
  2700. }
  2701. else
  2702. return logclose(image, f, "tmpfile", ": flush: ");
  2703. }
  2704. else
  2705. {
  2706. fclose(f);
  2707. return logerror(image, "tmpfile", ": write failed", "");
  2708. }
  2709. }
  2710. else
  2711. return logerror(image, "tmpfile", ": open: ", strerror(errno));
  2712. #else /* SIMPLIFIED_WRITE_STDIO */
  2713. return logerror(image, "tmpfile", ": open: unsupported", "");
  2714. #endif /* SIMPLIFIED_WRITE_STDIO */
  2715. }
  2716. else if (image->opts & USE_FILE)
  2717. {
  2718. #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
  2719. static int counter = 0;
  2720. char name[32];
  2721. sprintf(name, "%s%d.png", tmpf, ++counter);
  2722. if (png_image_write_to_file(&image->image, name, convert_to_8bit,
  2723. image->buffer+16, (png_int_32)image->stride, image->colormap))
  2724. {
  2725. initimage(output, image->opts, output->tmpfile_name,
  2726. image->stride_extra);
  2727. /* Afterwards, or freeimage will delete it! */
  2728. strcpy(output->tmpfile_name, name);
  2729. if (!checkopaque(image))
  2730. return 0;
  2731. }
  2732. else
  2733. return logerror(image, name, ": write failed", "");
  2734. #else /* SIMPLIFIED_WRITE_STDIO */
  2735. return logerror(image, "stdio", ": open: unsupported", "");
  2736. #endif /* SIMPLIFIED_WRITE_STDIO */
  2737. }
  2738. else /* use memory */
  2739. {
  2740. png_alloc_size_t size;
  2741. if (png_image_write_get_memory_size(image->image, size, convert_to_8bit,
  2742. image->buffer+16, (png_int_32)image->stride, image->colormap))
  2743. {
  2744. /* This is non-fatal but ignoring it was causing serious problems in
  2745. * the macro to be ignored:
  2746. */
  2747. if (size > PNG_IMAGE_PNG_SIZE_MAX(image->image))
  2748. return logerror(image, "memory", ": PNG_IMAGE_SIZE_MAX wrong", "");
  2749. initimage(output, image->opts, "memory", image->stride_extra);
  2750. output->input_memory = malloc(size);
  2751. if (output->input_memory != NULL)
  2752. {
  2753. output->input_memory_size = size;
  2754. if (png_image_write_to_memory(&image->image, output->input_memory,
  2755. &output->input_memory_size, convert_to_8bit, image->buffer+16,
  2756. (png_int_32)image->stride, image->colormap))
  2757. {
  2758. /* This is also non-fatal but it safes safer to error out anyway:
  2759. */
  2760. if (size != output->input_memory_size)
  2761. return logerror(image, "memory", ": memory size wrong", "");
  2762. }
  2763. else
  2764. return logerror(image, "memory", ": write failed", "");
  2765. }
  2766. else
  2767. return logerror(image, "memory", ": out of memory", "");
  2768. }
  2769. else
  2770. return logerror(image, "memory", ": get size:", "");
  2771. }
  2772. /* 'output' has an initialized temporary image, read this back in and compare
  2773. * this against the original: there should be no change since the original
  2774. * format was written unmodified unless 'convert_to_8bit' was specified.
  2775. * However, if the original image was color-mapped, a simple read will zap
  2776. * the linear, color and maybe alpha flags, this will cause spurious failures
  2777. * under some circumstances.
  2778. */
  2779. if (read_file(output, image->image.format | FORMAT_NO_CHANGE, NULL))
  2780. {
  2781. png_uint_32 original_format = image->image.format;
  2782. if (convert_to_8bit)
  2783. original_format &= ~PNG_FORMAT_FLAG_LINEAR;
  2784. if ((output->image.format & BASE_FORMATS) !=
  2785. (original_format & BASE_FORMATS))
  2786. return logerror(image, image->file_name, ": format changed on read: ",
  2787. output->file_name);
  2788. return compare_two_images(image, output, 0/*via linear*/, NULL);
  2789. }
  2790. else
  2791. return logerror(output, output->tmpfile_name,
  2792. ": read of new file failed", "");
  2793. }
  2794. #endif
  2795. static int
  2796. testimage(Image *image, png_uint_32 opts, format_list *pf)
  2797. {
  2798. int result;
  2799. Image copy;
  2800. /* Copy the original data, stealing it from 'image' */
  2801. checkopaque(image);
  2802. copy = *image;
  2803. copy.opts = opts;
  2804. copy.buffer = NULL;
  2805. copy.bufsize = 0;
  2806. copy.allocsize = 0;
  2807. image->input_file = NULL;
  2808. image->input_memory = NULL;
  2809. image->input_memory_size = 0;
  2810. image->tmpfile_name[0] = 0;
  2811. {
  2812. png_uint_32 counter;
  2813. Image output;
  2814. newimage(&output);
  2815. result = 1;
  2816. /* Use the low bit of 'counter' to indicate whether or not to do alpha
  2817. * removal with a background color or by composting onto the image; this
  2818. * step gets skipped if it isn't relevant
  2819. */
  2820. for (counter=0; counter<2*FORMAT_COUNT; ++counter)
  2821. if (format_isset(pf, counter >> 1))
  2822. {
  2823. png_uint_32 format = counter >> 1;
  2824. png_color background_color;
  2825. png_colorp background = NULL;
  2826. /* If there is a format change that removes the alpha channel then
  2827. * the background is relevant. If the output is 8-bit color-mapped
  2828. * then a background color *must* be provided, otherwise there are
  2829. * two tests to do - one with a color, the other with NULL. The
  2830. * NULL test happens second.
  2831. */
  2832. if ((counter & 1) == 0)
  2833. {
  2834. if ((format & PNG_FORMAT_FLAG_ALPHA) == 0 &&
  2835. (image->image.format & PNG_FORMAT_FLAG_ALPHA) != 0)
  2836. {
  2837. /* Alpha/transparency will be removed, the background is
  2838. * relevant: make it a color the first time
  2839. */
  2840. random_color(&background_color);
  2841. background = &background_color;
  2842. /* BUT if the output is to a color-mapped 8-bit format then
  2843. * the background must always be a color, so increment 'counter'
  2844. * to skip the NULL test.
  2845. */
  2846. if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0 &&
  2847. (format & PNG_FORMAT_FLAG_LINEAR) == 0)
  2848. ++counter;
  2849. }
  2850. /* Otherwise an alpha channel is not being eliminated, just leave
  2851. * background NULL and skip the (counter & 1) NULL test.
  2852. */
  2853. else
  2854. ++counter;
  2855. }
  2856. /* else just use NULL for background */
  2857. resetimage(&copy);
  2858. copy.opts = opts; /* in case read_file needs to change it */
  2859. result = read_file(&copy, format, background);
  2860. if (!result)
  2861. break;
  2862. /* Make sure the file just read matches the original file. */
  2863. result = compare_two_images(image, &copy, 0/*via linear*/, background);
  2864. if (!result)
  2865. break;
  2866. # ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
  2867. /* Write the *copy* just made to a new file to make sure the write
  2868. * side works ok. Check the conversion to sRGB if the copy is
  2869. * linear.
  2870. */
  2871. output.opts = opts;
  2872. result = write_one_file(&output, &copy, 0/*convert to 8bit*/);
  2873. if (!result)
  2874. break;
  2875. /* Validate against the original too; the background is needed here
  2876. * as well so that compare_two_images knows what color was used.
  2877. */
  2878. result = compare_two_images(image, &output, 0, background);
  2879. if (!result)
  2880. break;
  2881. if ((format & PNG_FORMAT_FLAG_LINEAR) != 0 &&
  2882. (format & PNG_FORMAT_FLAG_COLORMAP) == 0)
  2883. {
  2884. /* 'output' is linear, convert to the corresponding sRGB format.
  2885. */
  2886. output.opts = opts;
  2887. result = write_one_file(&output, &copy, 1/*convert to 8bit*/);
  2888. if (!result)
  2889. break;
  2890. /* This may involve a conversion via linear; in the ideal world
  2891. * this would round-trip correctly, but libpng 1.5.7 is not the
  2892. * ideal world so allow a drift (error_via_linear).
  2893. *
  2894. * 'image' has an alpha channel but 'output' does not then there
  2895. * will a strip-alpha-channel operation (because 'output' is
  2896. * linear), handle this by composing on black when doing the
  2897. * comparison.
  2898. */
  2899. result = compare_two_images(image, &output, 1/*via_linear*/,
  2900. background);
  2901. if (!result)
  2902. break;
  2903. }
  2904. # endif /* PNG_SIMPLIFIED_WRITE_SUPPORTED */
  2905. }
  2906. freeimage(&output);
  2907. }
  2908. freeimage(&copy);
  2909. return result;
  2910. }
  2911. static int
  2912. test_one_file(const char *file_name, format_list *formats, png_uint_32 opts,
  2913. int stride_extra, int log_pass)
  2914. {
  2915. int result;
  2916. Image image;
  2917. if (!(opts & NO_RESEED))
  2918. reseed(); /* ensure that the random numbers don't depend on file order */
  2919. newimage(&image);
  2920. initimage(&image, opts, file_name, stride_extra);
  2921. result = read_one_file(&image);
  2922. if (result)
  2923. result = testimage(&image, opts, formats);
  2924. freeimage(&image);
  2925. /* Ensure that stderr is flushed into any log file */
  2926. fflush(stderr);
  2927. if (log_pass)
  2928. {
  2929. if (result)
  2930. printf("PASS:");
  2931. else
  2932. printf("FAIL:");
  2933. # ifndef PNG_SIMPLIFIED_WRITE_SUPPORTED
  2934. printf(" (no write)");
  2935. # endif
  2936. print_opts(opts);
  2937. printf(" %s\n", file_name);
  2938. /* stdout may not be line-buffered if it is piped to a file, so: */
  2939. fflush(stdout);
  2940. }
  2941. else if (!result)
  2942. exit(1);
  2943. return result;
  2944. }
  2945. int
  2946. main(int argc, char **argv)
  2947. {
  2948. png_uint_32 opts = FAST_WRITE | STRICT;
  2949. format_list formats;
  2950. const char *touch = NULL;
  2951. int log_pass = 0;
  2952. int redundant = 0;
  2953. int stride_extra = 0;
  2954. int retval = 0;
  2955. int c;
  2956. #if PNG_LIBPNG_VER >= 10700
  2957. /* This error should not exist in 1.7 or later: */
  2958. opts |= GBG_ERROR;
  2959. #endif
  2960. init_sRGB_to_d();
  2961. #if 0
  2962. init_error_via_linear();
  2963. #endif
  2964. format_init(&formats);
  2965. reseed(); /* initialize random number seeds */
  2966. for (c=1; c<argc; ++c)
  2967. {
  2968. const char *arg = argv[c];
  2969. if (strcmp(arg, "--log") == 0)
  2970. log_pass = 1;
  2971. else if (strcmp(arg, "--fresh") == 0)
  2972. {
  2973. memset(gpc_error, 0, sizeof gpc_error);
  2974. memset(gpc_error_via_linear, 0, sizeof gpc_error_via_linear);
  2975. }
  2976. else if (strcmp(arg, "--file") == 0)
  2977. # ifdef PNG_STDIO_SUPPORTED
  2978. opts |= USE_FILE;
  2979. # else
  2980. return SKIP; /* skipped: no support */
  2981. # endif
  2982. else if (strcmp(arg, "--memory") == 0)
  2983. opts &= ~USE_FILE;
  2984. else if (strcmp(arg, "--stdio") == 0)
  2985. # ifdef PNG_STDIO_SUPPORTED
  2986. opts |= USE_STDIO;
  2987. # else
  2988. return SKIP; /* skipped: no support */
  2989. # endif
  2990. else if (strcmp(arg, "--name") == 0)
  2991. opts &= ~USE_STDIO;
  2992. else if (strcmp(arg, "--verbose") == 0)
  2993. opts |= VERBOSE;
  2994. else if (strcmp(arg, "--quiet") == 0)
  2995. opts &= ~VERBOSE;
  2996. else if (strcmp(arg, "--preserve") == 0)
  2997. opts |= KEEP_TMPFILES;
  2998. else if (strcmp(arg, "--nopreserve") == 0)
  2999. opts &= ~KEEP_TMPFILES;
  3000. else if (strcmp(arg, "--keep-going") == 0)
  3001. opts |= KEEP_GOING;
  3002. else if (strcmp(arg, "--fast") == 0)
  3003. opts |= FAST_WRITE;
  3004. else if (strcmp(arg, "--slow") == 0)
  3005. opts &= ~FAST_WRITE;
  3006. else if (strcmp(arg, "--accumulate") == 0)
  3007. opts |= ACCUMULATE;
  3008. else if (strcmp(arg, "--redundant") == 0)
  3009. redundant = 1;
  3010. else if (strcmp(arg, "--stop") == 0)
  3011. opts &= ~KEEP_GOING;
  3012. else if (strcmp(arg, "--strict") == 0)
  3013. opts |= STRICT;
  3014. else if (strcmp(arg, "--nostrict") == 0)
  3015. opts &= ~STRICT;
  3016. else if (strcmp(arg, "--sRGB-16bit") == 0)
  3017. opts |= sRGB_16BIT;
  3018. else if (strcmp(arg, "--linear-16bit") == 0)
  3019. opts &= ~sRGB_16BIT;
  3020. else if (strcmp(arg, "--noreseed") == 0)
  3021. opts |= NO_RESEED;
  3022. else if (strcmp(arg, "--fault-gbg-warning") == 0)
  3023. opts |= GBG_ERROR;
  3024. else if (strcmp(arg, "--tmpfile") == 0)
  3025. {
  3026. if (c+1 < argc)
  3027. {
  3028. if (strlen(argv[++c]) >= sizeof tmpf)
  3029. {
  3030. fflush(stdout);
  3031. fprintf(stderr, "%s: %s is too long for a temp file prefix\n",
  3032. argv[0], argv[c]);
  3033. exit(99);
  3034. }
  3035. /* Safe: checked above */
  3036. strncpy(tmpf, argv[c], sizeof (tmpf)-1);
  3037. }
  3038. else
  3039. {
  3040. fflush(stdout);
  3041. fprintf(stderr, "%s: %s requires a temporary file prefix\n",
  3042. argv[0], arg);
  3043. exit(99);
  3044. }
  3045. }
  3046. else if (strcmp(arg, "--touch") == 0)
  3047. {
  3048. if (c+1 < argc)
  3049. touch = argv[++c];
  3050. else
  3051. {
  3052. fflush(stdout);
  3053. fprintf(stderr, "%s: %s requires a file name argument\n",
  3054. argv[0], arg);
  3055. exit(99);
  3056. }
  3057. }
  3058. else if (arg[0] == '+')
  3059. {
  3060. png_uint_32 format = formatof(arg+1);
  3061. if (format > FORMAT_COUNT)
  3062. exit(99);
  3063. format_set(&formats, format);
  3064. }
  3065. else if (arg[0] == '-' && arg[1] != 0 && (arg[1] != '0' || arg[2] != 0))
  3066. {
  3067. fflush(stdout);
  3068. fprintf(stderr, "%s: unknown option: %s\n", argv[0], arg);
  3069. exit(99);
  3070. }
  3071. else
  3072. {
  3073. if (format_is_initial(&formats))
  3074. format_default(&formats, redundant);
  3075. if (arg[0] == '-')
  3076. {
  3077. int term = (arg[1] == '0' ? 0 : '\n');
  3078. unsigned int ich = 0;
  3079. /* Loop reading files, use a static buffer to simplify this and just
  3080. * stop if the name gets to long.
  3081. */
  3082. static char buffer[4096];
  3083. do
  3084. {
  3085. int ch = getchar();
  3086. /* Don't allow '\0' in file names, and terminate with '\n' or,
  3087. * for -0, just '\0' (use -print0 to find to make this work!)
  3088. */
  3089. if (ch == EOF || ch == term || ch == 0)
  3090. {
  3091. buffer[ich] = 0;
  3092. if (ich > 0 && !test_one_file(buffer, &formats, opts,
  3093. stride_extra, log_pass))
  3094. retval = 1;
  3095. if (ch == EOF)
  3096. break;
  3097. ich = 0;
  3098. --ich; /* so that the increment below sets it to 0 again */
  3099. }
  3100. else
  3101. buffer[ich] = (char)ch;
  3102. } while (++ich < sizeof buffer);
  3103. if (ich)
  3104. {
  3105. buffer[32] = 0;
  3106. buffer[4095] = 0;
  3107. fprintf(stderr, "%s...%s: file name too long\n", buffer,
  3108. buffer+(4096-32));
  3109. exit(99);
  3110. }
  3111. }
  3112. else if (!test_one_file(arg, &formats, opts, stride_extra, log_pass))
  3113. retval = 1;
  3114. }
  3115. }
  3116. if (opts & ACCUMULATE)
  3117. {
  3118. unsigned int in;
  3119. printf("/* contrib/libtests/pngstest-errors.h\n");
  3120. printf(" *\n");
  3121. printf(" * BUILT USING:" PNG_HEADER_VERSION_STRING);
  3122. printf(" *\n");
  3123. printf(" * This code is released under the libpng license.\n");
  3124. printf(" * For conditions of distribution and use, see the disclaimer\n");
  3125. printf(" * and license in png.h\n");
  3126. printf(" *\n");
  3127. printf(" * THIS IS A MACHINE GENERATED FILE: do not edit it directly!\n");
  3128. printf(" * Instead run:\n");
  3129. printf(" *\n");
  3130. printf(" * pngstest --accumulate\n");
  3131. printf(" *\n");
  3132. printf(" * on as many PNG files as possible; at least PNGSuite and\n");
  3133. printf(" * contrib/libtests/testpngs.\n");
  3134. printf(" */\n");
  3135. printf("static png_uint_16 gpc_error[16/*in*/][16/*out*/][4/*a*/] =\n");
  3136. printf("{\n");
  3137. for (in=0; in<16; ++in)
  3138. {
  3139. unsigned int out;
  3140. printf(" { /* input: %s */\n ", format_names[in]);
  3141. for (out=0; out<16; ++out)
  3142. {
  3143. unsigned int alpha;
  3144. printf(" {");
  3145. for (alpha=0; alpha<4; ++alpha)
  3146. {
  3147. printf(" %d", gpc_error[in][out][alpha]);
  3148. if (alpha < 3) putchar(',');
  3149. }
  3150. printf(" }");
  3151. if (out < 15)
  3152. {
  3153. putchar(',');
  3154. if (out % 4 == 3) printf("\n ");
  3155. }
  3156. }
  3157. printf("\n }");
  3158. if (in < 15)
  3159. putchar(',');
  3160. else
  3161. putchar('\n');
  3162. }
  3163. printf("};\n");
  3164. printf("static png_uint_16 gpc_error_via_linear[16][4/*out*/][4] =\n");
  3165. printf("{\n");
  3166. for (in=0; in<16; ++in)
  3167. {
  3168. unsigned int out;
  3169. printf(" { /* input: %s */\n ", format_names[in]);
  3170. for (out=0; out<4; ++out)
  3171. {
  3172. unsigned int alpha;
  3173. printf(" {");
  3174. for (alpha=0; alpha<4; ++alpha)
  3175. {
  3176. printf(" %d", gpc_error_via_linear[in][out][alpha]);
  3177. if (alpha < 3) putchar(',');
  3178. }
  3179. printf(" }");
  3180. if (out < 3)
  3181. putchar(',');
  3182. }
  3183. printf("\n }");
  3184. if (in < 15)
  3185. putchar(',');
  3186. else
  3187. putchar('\n');
  3188. }
  3189. printf("};\n");
  3190. printf("static png_uint_16 gpc_error_to_colormap[8/*i*/][8/*o*/][4] =\n");
  3191. printf("{\n");
  3192. for (in=0; in<8; ++in)
  3193. {
  3194. unsigned int out;
  3195. printf(" { /* input: %s */\n ", format_names[in]);
  3196. for (out=0; out<8; ++out)
  3197. {
  3198. unsigned int alpha;
  3199. printf(" {");
  3200. for (alpha=0; alpha<4; ++alpha)
  3201. {
  3202. printf(" %d", gpc_error_to_colormap[in][out][alpha]);
  3203. if (alpha < 3) putchar(',');
  3204. }
  3205. printf(" }");
  3206. if (out < 7)
  3207. {
  3208. putchar(',');
  3209. if (out % 4 == 3) printf("\n ");
  3210. }
  3211. }
  3212. printf("\n }");
  3213. if (in < 7)
  3214. putchar(',');
  3215. else
  3216. putchar('\n');
  3217. }
  3218. printf("};\n");
  3219. printf("/* END MACHINE GENERATED */\n");
  3220. }
  3221. if (retval == 0 && touch != NULL)
  3222. {
  3223. FILE *fsuccess = fopen(touch, "wt");
  3224. if (fsuccess != NULL)
  3225. {
  3226. int error = 0;
  3227. fprintf(fsuccess, "PNG simple API tests succeeded\n");
  3228. fflush(fsuccess);
  3229. error = ferror(fsuccess);
  3230. if (fclose(fsuccess) || error)
  3231. {
  3232. fflush(stdout);
  3233. fprintf(stderr, "%s: write failed\n", touch);
  3234. exit(99);
  3235. }
  3236. }
  3237. else
  3238. {
  3239. fflush(stdout);
  3240. fprintf(stderr, "%s: open failed\n", touch);
  3241. exit(99);
  3242. }
  3243. }
  3244. return retval;
  3245. }
  3246. #else /* !PNG_SIMPLIFIED_READ_SUPPORTED */
  3247. int main(void)
  3248. {
  3249. fprintf(stderr, "pngstest: no read support in libpng, test skipped\n");
  3250. /* So the test is skipped: */
  3251. return SKIP;
  3252. }
  3253. #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */