pngstest.c 100 KB

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