pngimage.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  1. /* pngimage.c
  2. *
  3. * Copyright (c) 2014 John Cunningham Bowler
  4. *
  5. * Last changed in libpng 1.6.10 [March 6, 2014]
  6. *
  7. * This code is released under the libpng license.
  8. * For conditions of distribution and use, see the disclaimer
  9. * and license in png.h
  10. *
  11. * Test the png_read_png and png_write_png interfaces. Given a PNG file load it
  12. * using png_read_png and then write with png_write_png. Test all possible
  13. * transforms.
  14. */
  15. #include <stdarg.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <stdio.h>
  20. #include <assert.h>
  21. #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
  22. # include <config.h>
  23. #endif
  24. /* Define the following to use this test against your installed libpng, rather
  25. * than the one being built here:
  26. */
  27. #ifdef PNG_FREESTANDING_TESTS
  28. # include <png.h>
  29. #else
  30. # include "../../png.h"
  31. #endif
  32. #ifndef PNG_SETJMP_SUPPORTED
  33. # include <setjmp.h> /* because png.h did *not* include this */
  34. #endif
  35. #if defined(PNG_INFO_IMAGE_SUPPORTED) && defined(PNG_SEQUENTIAL_READ_SUPPORTED)
  36. /* If a transform is valid on both read and write this implies that if the
  37. * transform is applied to read it must also be applied on write to produce
  38. * meaningful data. This is because these transforms when performed on read
  39. * produce data with a memory format that does not correspond to a PNG format.
  40. *
  41. * Most of these transforms are invertible; after applying the transform on
  42. * write the result is the original PNG data that would have would have been
  43. * read if no transform were applied.
  44. *
  45. * The exception is _SHIFT, which destroys the low order bits marked as not
  46. * significant in a PNG with the sBIT chunk.
  47. *
  48. * The following table lists, for each transform, the conditions under which it
  49. * is expected to do anything. Conditions are defined as follows:
  50. *
  51. * 1) Color mask bits required - simply a mask to AND with color_type; one of
  52. * these must be present for the transform to fire, except that 0 means
  53. * 'always'.
  54. * 2) Color mask bits which must be absent - another mask - none of these must
  55. * be present.
  56. * 3) Bit depths - a mask of component bit depths for the transform to fire.
  57. * 4) 'read' - the transform works in png_read_png.
  58. * 5) 'write' - the transform works in png_write_png.
  59. * 6) PNG_INFO_chunk; a mask of the chunks that must be present for the
  60. * transform to fire. All must be present - the requirement is that
  61. * png_get_valid() & mask == mask, so if mask is 0 there is no requirement.
  62. *
  63. * The condition refers to the original image state - if multiple transforms are
  64. * used together it is possible to cause a transform that wouldn't fire on the
  65. * original image to fire.
  66. */
  67. static struct transform_info
  68. {
  69. const char *name;
  70. int transform;
  71. png_uint_32 valid_chunks;
  72. # define CHUNK_NONE 0
  73. # define CHUNK_sBIT PNG_INFO_sBIT
  74. # define CHUNK_tRNS PNG_INFO_tRNS
  75. png_byte color_mask_required;
  76. png_byte color_mask_absent;
  77. # define COLOR_MASK_X 0
  78. # define COLOR_MASK_P PNG_COLOR_MASK_PALETTE
  79. # define COLOR_MASK_C PNG_COLOR_MASK_COLOR
  80. # define COLOR_MASK_A PNG_COLOR_MASK_ALPHA
  81. # define COLOR_MASK_ALL (PALETTE+COLOR+ALPHA) /* absent = gray, no alpha */
  82. png_byte bit_depths;
  83. # define BD_ALL (1 + 2 + 4 + 8 + 16)
  84. # define BD_PAL (1 + 2 + 4 + 8)
  85. # define BD_LOW (1 + 2 + 4)
  86. # define BD_16 16
  87. # define BD_TRUE (8+16) /* i.e. true-color depths */
  88. png_byte when;
  89. # define TRANSFORM_R 1
  90. # define TRANSFORM_W 2
  91. # define TRANSFORM_RW 3
  92. png_byte tested; /* the transform was tested somewhere */
  93. } transform_info[] =
  94. {
  95. /* List ALL the PNG_TRANSFORM_ macros here. Check for support using the READ
  96. * macros; even if the transform is supported on write it cannot be tested
  97. * without the read support.
  98. */
  99. # define T(name,chunk,cm_required,cm_absent,bd,when)\
  100. { #name, PNG_TRANSFORM_ ## name, CHUNK_ ## chunk,\
  101. COLOR_MASK_ ## cm_required, COLOR_MASK_ ## cm_absent, BD_ ## bd,\
  102. TRANSFORM_ ## when, 0/*!tested*/ }
  103. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  104. T(STRIP_16, NONE, X, X, 16, R),
  105. /* drops the bottom 8 bits when bit depth is 16 */
  106. #endif
  107. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  108. T(STRIP_ALPHA, NONE, A, X, ALL, R),
  109. /* removes the alpha channel if present */
  110. #endif
  111. #ifdef PNG_WRITE_PACK_SUPPORTED
  112. # define TRANSFORM_RW_PACK TRANSFORM_RW
  113. #else
  114. # define TRANSFORM_RW_PACK TRANSFORM_R
  115. #endif
  116. #ifdef PNG_READ_PACK_SUPPORTED
  117. T(PACKING, NONE, X, X, LOW, RW_PACK),
  118. /* unpacks low-bit-depth components into 1 byte per component on read,
  119. * reverses this on write.
  120. */
  121. #endif
  122. #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
  123. # define TRANSFORM_RW_PACKSWAP TRANSFORM_RW
  124. #else
  125. # define TRANSFORM_RW_PACKSWAP TRANSFORM_R
  126. #endif
  127. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  128. T(PACKSWAP, NONE, X, X, LOW, RW_PACKSWAP),
  129. /* reverses the order of low-bit-depth components packed into a byte */
  130. #endif
  131. #ifdef PNG_READ_EXPAND_SUPPORTED
  132. T(EXPAND, NONE, P, X, ALL, R),
  133. /* expands PLTE PNG files to RGB (no tRNS) or RGBA (tRNS) *
  134. * Note that the 'EXPAND' transform does lots of different things: */
  135. T(EXPAND, NONE, X, C, ALL, R),
  136. /* expands grayscale PNG files to RGB, or RGBA */
  137. T(EXPAND, tRNS, X, A, ALL, R),
  138. /* expands the tRNS chunk in files without alpha */
  139. #endif
  140. #ifdef PNG_WRITE_INVERT_SUPPORTED
  141. # define TRANSFORM_RW_INVERT TRANSFORM_RW
  142. #else
  143. # define TRANSFORM_RW_INVERT TRANSFORM_R
  144. #endif
  145. #ifdef PNG_READ_INVERT_SUPPORTED
  146. T(INVERT_MONO, NONE, X, C, ALL, RW_INVERT),
  147. /* converts gray-scale components to 1..0 from 0..1 */
  148. #endif
  149. #ifdef PNG_WRITE_SHIFT_SUPPORTED
  150. # define TRANSFORM_RW_SHIFT TRANSFORM_RW
  151. #else
  152. # define TRANSFORM_RW_SHIFT TRANSFORM_R
  153. #endif
  154. #ifdef PNG_READ_SHIFT_SUPPORTED
  155. T(SHIFT, sBIT, X, X, ALL, RW_SHIFT),
  156. /* reduces component values to the original range based on the sBIT chunk,
  157. * this is only partially reversible - the low bits are lost and cannot be
  158. * recovered on write. In fact write code replicates the bits to generate
  159. * new low-order bits.
  160. */
  161. #endif
  162. #ifdef PNG_WRITE_BGR_SUPPORTED
  163. # define TRANSFORM_RW_BGR TRANSFORM_RW
  164. #else
  165. # define TRANSFORM_RW_BGR TRANSFORM_R
  166. #endif
  167. #ifdef PNG_READ_BGR_SUPPORTED
  168. T(BGR, NONE, C, P, TRUE, RW_BGR),
  169. /* reverses the rgb component values of true-color pixels */
  170. #endif
  171. #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
  172. # define TRANSFORM_RW_SWAP_ALPHA TRANSFORM_RW
  173. #else
  174. # define TRANSFORM_RW_SWAP_ALPHA TRANSFORM_R
  175. #endif
  176. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  177. T(SWAP_ALPHA, NONE, A, X, TRUE, RW_SWAP_ALPHA),
  178. /* swaps the alpha channel of RGBA or GA pixels to the front - ARGB or
  179. * AG, on write reverses the process.
  180. */
  181. #endif
  182. #ifdef PNG_WRITE_SWAP_SUPPORTED
  183. # define TRANSFORM_RW_SWAP TRANSFORM_RW
  184. #else
  185. # define TRANSFORM_RW_SWAP TRANSFORM_R
  186. #endif
  187. #ifdef PNG_READ_SWAP_SUPPORTED
  188. T(SWAP_ENDIAN, NONE, X, P, 16, RW_SWAP),
  189. /* byte-swaps 16-bit component values */
  190. #endif
  191. #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  192. # define TRANSFORM_RW_INVERT_ALPHA TRANSFORM_RW
  193. #else
  194. # define TRANSFORM_RW_INVERT_ALPHA TRANSFORM_R
  195. #endif
  196. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  197. T(INVERT_ALPHA, NONE, A, X, TRUE, RW_INVERT_ALPHA),
  198. /* converts an alpha channel from 0..1 to 1..0 */
  199. #endif
  200. #ifdef PNG_WRITE_FILLER_SUPPORTED
  201. T(STRIP_FILLER_BEFORE, NONE, A, P, TRUE, W), /* 'A' for a filler! */
  202. /* on write skips a leading filler channel; testing requires data with a
  203. * filler channel so this is produced from RGBA or GA images by removing
  204. * the 'alpha' flag from the color type in place.
  205. */
  206. T(STRIP_FILLER_AFTER, NONE, A, P, TRUE, W),
  207. /* on write strips a trailing filler channel */
  208. #endif
  209. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  210. T(GRAY_TO_RGB, NONE, X, C, ALL, R),
  211. /* expands grayscale images to RGB, also causes the palette part of
  212. * 'EXPAND' to happen. Low bit depth grayscale images are expanded to
  213. * 8-bits per component and no attempt is made to convert the image to a
  214. * palette image. While this transform is partially reversible
  215. * png_write_png does not currently support this.
  216. */
  217. T(GRAY_TO_RGB, NONE, P, X, ALL, R),
  218. /* The 'palette' side effect mentioned above; a bit bogus but this is the
  219. * way the libpng code works.
  220. */
  221. #endif
  222. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  223. T(EXPAND_16, NONE, X, X, PAL, R),
  224. /* expands images to 16-bits per component, as a side effect expands
  225. * palette images to RGB and expands the tRNS chunk if present, so it can
  226. * modify 16-bit per component images as well:
  227. */
  228. T(EXPAND_16, tRNS, X, A, 16, R),
  229. /* side effect of EXPAND_16 - expands the tRNS chunk in an RGB or G 16-bit
  230. * image.
  231. */
  232. #endif
  233. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  234. T(SCALE_16, NONE, X, X, 16, R)
  235. /* scales 16-bit components to 8-bits. */
  236. #endif
  237. #undef T
  238. };
  239. #define ARRAY_SIZE(a) ((sizeof a)/(sizeof a[0]))
  240. #define TTABLE_SIZE ARRAY_SIZE(transform_info)
  241. /* Some combinations of options that should be reversible are not; these cases
  242. * are bugs.
  243. */
  244. static int known_bad_combos[][2] =
  245. {
  246. /* problem, antidote */
  247. { PNG_TRANSFORM_SHIFT | PNG_TRANSFORM_INVERT_ALPHA, 0/*antidote*/ }
  248. };
  249. static int
  250. is_combo(int transforms)
  251. {
  252. return transforms & (transforms-1); /* non-zero if more than one set bit */
  253. }
  254. static int
  255. first_transform(int transforms)
  256. {
  257. return transforms & -transforms; /* lowest set bit */
  258. }
  259. static int
  260. is_bad_combo(int transforms)
  261. {
  262. unsigned int i;
  263. for (i=0; i<ARRAY_SIZE(known_bad_combos); ++i)
  264. {
  265. int combo = known_bad_combos[i][0];
  266. if ((combo & transforms) == combo &&
  267. (transforms & known_bad_combos[i][1]) == 0)
  268. return 1;
  269. }
  270. return 0; /* combo is ok */
  271. }
  272. static const char *
  273. transform_name(int t)
  274. /* The name, if 't' has multiple bits set the name of the lowest set bit is
  275. * returned.
  276. */
  277. {
  278. unsigned int i;
  279. t &= -t; /* first set bit */
  280. for (i=0; i<TTABLE_SIZE; ++i)
  281. {
  282. if ((transform_info[i].transform & t) != 0)
  283. return transform_info[i].name;
  284. }
  285. return "invalid transform";
  286. }
  287. /* Variables calculated by validate_T below and used to record all the supported
  288. * transforms. Need (unsigned int) here because of the places where these
  289. * values are used (unsigned compares in the 'exhaustive' iterator.)
  290. */
  291. static unsigned int read_transforms, write_transforms, rw_transforms;
  292. static void
  293. validate_T(void)
  294. /* Validate the above table - this just builds the above values */
  295. {
  296. unsigned int i;
  297. for (i=0; i<TTABLE_SIZE; ++i)
  298. {
  299. if (transform_info[i].when & TRANSFORM_R)
  300. read_transforms |= transform_info[i].transform;
  301. if (transform_info[i].when & TRANSFORM_W)
  302. write_transforms |= transform_info[i].transform;
  303. }
  304. /* Reversible transforms are those which are supported on both read and
  305. * write.
  306. */
  307. rw_transforms = read_transforms & write_transforms;
  308. }
  309. /* FILE DATA HANDLING
  310. * The original file is cached in memory. During write the output file is
  311. * written to memory.
  312. *
  313. * In both cases the file data is held in a linked list of buffers - not all
  314. * of these are in use at any time.
  315. */
  316. struct buffer_list
  317. {
  318. struct buffer_list *next; /* next buffer in list */
  319. png_byte buffer[1024]; /* the actual buffer */
  320. };
  321. struct buffer
  322. {
  323. struct buffer_list *last; /* last buffer in use */
  324. size_t end_count; /* bytes in the last buffer */
  325. struct buffer_list *current; /* current buffer being read */
  326. size_t read_count; /* count of bytes read from current */
  327. struct buffer_list first; /* the very first buffer */
  328. };
  329. static void
  330. buffer_init(struct buffer *buffer)
  331. /* Call this only once for a given buffer */
  332. {
  333. buffer->first.next = NULL;
  334. buffer->last = NULL;
  335. buffer->current = NULL;
  336. }
  337. #ifdef PNG_WRITE_SUPPORTED
  338. static void
  339. buffer_start_write(struct buffer *buffer)
  340. {
  341. buffer->last = &buffer->first;
  342. buffer->end_count = 0;
  343. buffer->current = NULL;
  344. }
  345. #endif
  346. static void
  347. buffer_start_read(struct buffer *buffer)
  348. {
  349. buffer->current = &buffer->first;
  350. buffer->read_count = 0;
  351. }
  352. #ifdef ENOMEM /* required by POSIX 1003.1 */
  353. # define MEMORY ENOMEM
  354. #else
  355. # define MEMORY ERANGE /* required by ANSI-C */
  356. #endif
  357. static struct buffer *
  358. get_buffer(png_structp pp)
  359. /* Used from libpng callbacks to get the current buffer */
  360. {
  361. return (struct buffer*)png_get_io_ptr(pp);
  362. }
  363. #define NEW(type) ((type *)malloc(sizeof (type)))
  364. static struct buffer_list *
  365. buffer_extend(struct buffer_list *current)
  366. {
  367. struct buffer_list *add;
  368. assert(current->next == NULL);
  369. add = NEW(struct buffer_list);
  370. if (add == NULL)
  371. return NULL;
  372. add->next = NULL;
  373. current->next = add;
  374. return add;
  375. }
  376. /* Load a buffer from a file; does the equivalent of buffer_start_write. On a
  377. * read error returns an errno value, else returns 0.
  378. */
  379. static int
  380. buffer_from_file(struct buffer *buffer, FILE *fp)
  381. {
  382. struct buffer_list *last = &buffer->first;
  383. size_t count = 0;
  384. for (;;)
  385. {
  386. size_t r = fread(last->buffer+count, 1/*size*/,
  387. (sizeof last->buffer)-count, fp);
  388. if (r > 0)
  389. {
  390. count += r;
  391. if (count >= sizeof last->buffer)
  392. {
  393. assert(count == sizeof last->buffer);
  394. count = 0;
  395. if (last->next == NULL)
  396. {
  397. last = buffer_extend(last);
  398. if (last == NULL)
  399. return MEMORY;
  400. }
  401. else
  402. last = last->next;
  403. }
  404. }
  405. else /* fread failed - probably end of file */
  406. {
  407. if (feof(fp))
  408. {
  409. buffer->last = last;
  410. buffer->end_count = count;
  411. return 0; /* no error */
  412. }
  413. /* Some kind of funky error; errno should be non-zero */
  414. return errno == 0 ? ERANGE : errno;
  415. }
  416. }
  417. }
  418. /* This structure is used to control the test of a single file. */
  419. typedef enum
  420. {
  421. VERBOSE, /* switches on all messages */
  422. INFORMATION,
  423. WARNINGS, /* switches on warnings */
  424. LIBPNG_WARNING,
  425. APP_WARNING,
  426. ERRORS, /* just errors */
  427. APP_FAIL, /* continuable error - no need to longjmp */
  428. LIBPNG_ERROR, /* this and higher cause a longjmp */
  429. LIBPNG_BUG, /* erroneous behavior in libpng */
  430. APP_ERROR, /* such as out-of-memory in a callback */
  431. QUIET, /* no normal messages */
  432. USER_ERROR, /* such as file-not-found */
  433. INTERNAL_ERROR
  434. } error_level;
  435. #define LEVEL_MASK 0xf /* where the level is in 'options' */
  436. #define EXHAUSTIVE 0x010 /* Test all combinations of active options */
  437. #define STRICT 0x020 /* Fail on warnings as well as errors */
  438. #define LOG 0x040 /* Log pass/fail to stdout */
  439. #define CONTINUE 0x080 /* Continue on APP_FAIL errors */
  440. #define SKIP_BUGS 0x100 /* Skip over known bugs */
  441. #define LOG_SKIPPED 0x200 /* Log skipped bugs */
  442. #define FIND_BAD_COMBOS 0x400 /* Attempt to deduce bad combos */
  443. /* Result masks apply to the result bits in the 'results' field below; these
  444. * bits are simple 1U<<error_level. A pass requires either nothing worse than
  445. * warnings (--relaxes) or nothing worse than information (--strict)
  446. */
  447. #define RESULT_STRICT(r) (((r) & ~((1U<<WARNINGS)-1)) == 0)
  448. #define RESULT_RELAXED(r) (((r) & ~((1U<<ERRORS)-1)) == 0)
  449. struct display
  450. {
  451. jmp_buf error_return; /* Where to go to on error */
  452. const char *filename; /* The name of the original file */
  453. const char *operation; /* Operation being performed */
  454. int transforms; /* Transform used in operation */
  455. png_uint_32 options; /* See display_log below */
  456. png_uint_32 results; /* A mask of errors seen */
  457. png_structp original_pp; /* used on the original read */
  458. png_infop original_ip; /* set by the original read */
  459. png_size_t original_rowbytes; /* of the original rows: */
  460. png_bytepp original_rows; /* from the original read */
  461. /* Original chunks valid */
  462. png_uint_32 chunks;
  463. /* Original IHDR information */
  464. png_uint_32 width;
  465. png_uint_32 height;
  466. int bit_depth;
  467. int color_type;
  468. int interlace_method;
  469. int compression_method;
  470. int filter_method;
  471. /* Derived information for the original image. */
  472. int active_transforms; /* transforms that do something on read */
  473. int ignored_transforms; /* transforms that should do nothing */
  474. /* Used on a read, both the original read and when validating a written
  475. * image.
  476. */
  477. png_structp read_pp;
  478. png_infop read_ip;
  479. # ifdef PNG_WRITE_SUPPORTED
  480. /* Used to write a new image (the original info_ptr is used) */
  481. png_structp write_pp;
  482. struct buffer written_file; /* where the file gets written */
  483. # endif
  484. struct buffer original_file; /* Data read from the original file */
  485. };
  486. static void
  487. display_init(struct display *dp)
  488. /* Call this only once right at the start to initialize the control
  489. * structure, the (struct buffer) lists are maintained across calls - the
  490. * memory is not freed.
  491. */
  492. {
  493. memset(dp, 0, sizeof *dp);
  494. dp->options = WARNINGS; /* default to !verbose, !quiet */
  495. dp->filename = NULL;
  496. dp->operation = NULL;
  497. dp->original_pp = NULL;
  498. dp->original_ip = NULL;
  499. dp->original_rows = NULL;
  500. dp->read_pp = NULL;
  501. dp->read_ip = NULL;
  502. buffer_init(&dp->original_file);
  503. # ifdef PNG_WRITE_SUPPORTED
  504. dp->write_pp = NULL;
  505. buffer_init(&dp->written_file);
  506. # endif
  507. }
  508. static void
  509. display_clean_read(struct display *dp)
  510. {
  511. if (dp->read_pp != NULL)
  512. png_destroy_read_struct(&dp->read_pp, &dp->read_ip, NULL);
  513. }
  514. #ifdef PNG_WRITE_SUPPORTED
  515. static void
  516. display_clean_write(struct display *dp)
  517. {
  518. if (dp->write_pp != NULL)
  519. png_destroy_write_struct(&dp->write_pp, NULL);
  520. }
  521. #endif
  522. static void
  523. display_clean(struct display *dp)
  524. {
  525. # ifdef PNG_WRITE_SUPPORTED
  526. display_clean_write(dp);
  527. # endif
  528. display_clean_read(dp);
  529. dp->original_rowbytes = 0;
  530. dp->original_rows = NULL;
  531. dp->chunks = 0;
  532. png_destroy_read_struct(&dp->original_pp, &dp->original_ip, NULL);
  533. /* leave the filename for error detection */
  534. dp->results = 0; /* reset for next time */
  535. }
  536. static struct display *
  537. get_dp(png_structp pp)
  538. /* The display pointer is always stored in the png_struct error pointer */
  539. {
  540. struct display *dp = (struct display*)png_get_error_ptr(pp);
  541. if (dp == NULL)
  542. {
  543. fprintf(stderr, "pngimage: internal error (no display)\n");
  544. exit(99); /* prevents a crash */
  545. }
  546. return dp;
  547. }
  548. /* error handling */
  549. #ifdef __GNUC__
  550. # define VGATTR __attribute__((__format__ (__printf__,3,4)))
  551. /* Required to quiet GNUC warnings when the compiler sees a stdarg function
  552. * that calls one of the stdio v APIs.
  553. */
  554. #else
  555. # define VGATTR
  556. #endif
  557. static void VGATTR
  558. display_log(struct display *dp, error_level level, const char *fmt, ...)
  559. /* 'level' is as above, fmt is a stdio style format string. This routine
  560. * does not return if level is above LIBPNG_WARNING
  561. */
  562. {
  563. dp->results |= 1U << level;
  564. if (level > (error_level)(dp->options & LEVEL_MASK))
  565. {
  566. const char *lp;
  567. va_list ap;
  568. switch (level)
  569. {
  570. case INFORMATION: lp = "information"; break;
  571. case LIBPNG_WARNING: lp = "warning(libpng)"; break;
  572. case APP_WARNING: lp = "warning(pngimage)"; break;
  573. case APP_FAIL: lp = "error(continuable)"; break;
  574. case LIBPNG_ERROR: lp = "error(libpng)"; break;
  575. case LIBPNG_BUG: lp = "bug(libpng)"; break;
  576. case APP_ERROR: lp = "error(pngimage)"; break;
  577. case USER_ERROR: lp = "error(user)"; break;
  578. case INTERNAL_ERROR: /* anything unexpected is an internal error: */
  579. case VERBOSE: case WARNINGS: case ERRORS: case QUIET:
  580. default: lp = "bug(pngimage)"; break;
  581. }
  582. fprintf(stderr, "%s: %s: %s",
  583. dp->filename != NULL ? dp->filename : "<stdin>", lp, dp->operation);
  584. if (dp->transforms != 0)
  585. {
  586. int tr = dp->transforms;
  587. if (is_combo(tr))
  588. fprintf(stderr, "(0x%x)", tr);
  589. else
  590. fprintf(stderr, "(%s)", transform_name(tr));
  591. }
  592. fprintf(stderr, ": ");
  593. va_start(ap, fmt);
  594. vfprintf(stderr, fmt, ap);
  595. va_end(ap);
  596. fputc('\n', stderr);
  597. }
  598. /* else do not output any message */
  599. /* Errors cause this routine to exit to the fail code */
  600. if (level > APP_FAIL || (level > ERRORS && !(dp->options & CONTINUE)))
  601. longjmp(dp->error_return, level);
  602. }
  603. /* error handler callbacks for libpng */
  604. static void PNGCBAPI
  605. display_warning(png_structp pp, png_const_charp warning)
  606. {
  607. display_log(get_dp(pp), LIBPNG_WARNING, "%s", warning);
  608. }
  609. static void PNGCBAPI
  610. display_error(png_structp pp, png_const_charp error)
  611. {
  612. struct display *dp = get_dp(pp);
  613. display_log(dp, LIBPNG_ERROR, "%s", error);
  614. }
  615. static void
  616. display_cache_file(struct display *dp, const char *filename)
  617. /* Does the initial cache of the file. */
  618. {
  619. FILE *fp;
  620. int ret;
  621. dp->filename = filename;
  622. if (filename != NULL)
  623. {
  624. fp = fopen(filename, "rb");
  625. if (fp == NULL)
  626. display_log(dp, USER_ERROR, "open failed: %s", strerror(errno));
  627. }
  628. else
  629. fp = stdin;
  630. ret = buffer_from_file(&dp->original_file, fp);
  631. fclose(fp);
  632. if (ret != 0)
  633. display_log(dp, APP_ERROR, "read failed: %s", strerror(ret));
  634. }
  635. static void
  636. buffer_read(struct display *dp, struct buffer *bp, png_bytep data,
  637. png_size_t size)
  638. {
  639. struct buffer_list *last = bp->current;
  640. size_t read_count = bp->read_count;
  641. while (size > 0)
  642. {
  643. size_t avail;
  644. if (last == NULL ||
  645. (last == bp->last && read_count >= bp->end_count))
  646. {
  647. display_log(dp, USER_ERROR, "file truncated (%lu bytes)",
  648. (unsigned long)size);
  649. /*NOTREACHED*/
  650. break;
  651. }
  652. else if (read_count >= sizeof last->buffer)
  653. {
  654. /* Move to the next buffer: */
  655. last = last->next;
  656. read_count = 0;
  657. bp->current = last; /* Avoid update outside the loop */
  658. /* And do a sanity check (the EOF case is caught above) */
  659. if (last == NULL)
  660. {
  661. display_log(dp, INTERNAL_ERROR, "damaged buffer list");
  662. /*NOTREACHED*/
  663. break;
  664. }
  665. }
  666. avail = (sizeof last->buffer) - read_count;
  667. if (avail > size)
  668. avail = size;
  669. memcpy(data, last->buffer + read_count, avail);
  670. read_count += avail;
  671. size -= avail;
  672. data += avail;
  673. }
  674. bp->read_count = read_count;
  675. }
  676. static void PNGCBAPI
  677. read_function(png_structp pp, png_bytep data, png_size_t size)
  678. {
  679. buffer_read(get_dp(pp), get_buffer(pp), data, size);
  680. }
  681. static void
  682. read_png(struct display *dp, struct buffer *bp, const char *operation,
  683. int transforms)
  684. {
  685. png_structp pp;
  686. png_infop ip;
  687. /* This cleans out any previous read and sets operation and transforms to
  688. * empty.
  689. */
  690. display_clean_read(dp);
  691. if (operation != NULL) /* else this is a verify and do not overwrite info */
  692. {
  693. dp->operation = operation;
  694. dp->transforms = transforms;
  695. }
  696. dp->read_pp = pp = png_create_read_struct(PNG_LIBPNG_VER_STRING, dp,
  697. display_error, display_warning);
  698. if (pp == NULL)
  699. display_log(dp, LIBPNG_ERROR, "failed to create read struct");
  700. /* The png_read_png API requires us to make the info struct, but it does the
  701. * call to png_read_info.
  702. */
  703. dp->read_ip = ip = png_create_info_struct(pp);
  704. if (ip == NULL)
  705. display_log(dp, LIBPNG_ERROR, "failed to create info struct");
  706. # ifdef PNG_SET_USER_LIMITS_SUPPORTED
  707. /* Remove the user limits, if any */
  708. png_set_user_limits(pp, 0x7fffffff, 0x7fffffff);
  709. # endif
  710. /* Set the IO handling */
  711. buffer_start_read(bp);
  712. png_set_read_fn(pp, bp, read_function);
  713. png_read_png(pp, ip, transforms, NULL/*params*/);
  714. #if 0 /* crazy debugging */
  715. {
  716. png_bytep pr = png_get_rows(pp, ip)[0];
  717. size_t rb = png_get_rowbytes(pp, ip);
  718. size_t cb;
  719. char c = ' ';
  720. fprintf(stderr, "%.4x %2d (%3lu bytes):", transforms, png_get_bit_depth(pp,ip), (unsigned long)rb);
  721. for (cb=0; cb<rb; ++cb)
  722. fputc(c, stderr), fprintf(stderr, "%.2x", pr[cb]), c='.';
  723. fputc('\n', stderr);
  724. }
  725. #endif
  726. }
  727. static void
  728. update_display(struct display *dp)
  729. /* called once after the first read to update all the info, original_pp and
  730. * original_ip must have been filled in.
  731. */
  732. {
  733. png_structp pp;
  734. png_infop ip;
  735. /* Now perform the initial read with a 0 tranform. */
  736. read_png(dp, &dp->original_file, "original read", 0/*no transform*/);
  737. /* Move the result to the 'original' fields */
  738. dp->original_pp = pp = dp->read_pp, dp->read_pp = NULL;
  739. dp->original_ip = ip = dp->read_ip, dp->read_ip = NULL;
  740. dp->original_rowbytes = png_get_rowbytes(pp, ip);
  741. if (dp->original_rowbytes == 0)
  742. display_log(dp, LIBPNG_BUG, "png_get_rowbytes returned 0");
  743. dp->chunks = png_get_valid(pp, ip, 0xffffffff);
  744. if ((dp->chunks & PNG_INFO_IDAT) == 0) /* set by png_read_png */
  745. display_log(dp, LIBPNG_BUG, "png_read_png did not set IDAT flag");
  746. dp->original_rows = png_get_rows(pp, ip);
  747. if (dp->original_rows == NULL)
  748. display_log(dp, LIBPNG_BUG, "png_read_png did not create row buffers");
  749. if (!png_get_IHDR(pp, ip,
  750. &dp->width, &dp->height, &dp->bit_depth, &dp->color_type,
  751. &dp->interlace_method, &dp->compression_method, &dp->filter_method))
  752. display_log(dp, LIBPNG_BUG, "png_get_IHDR failed");
  753. /* 'active' transforms are discovered based on the original image format;
  754. * running one active transform can activate others. At present the code
  755. * does not attempt to determine the closure.
  756. */
  757. {
  758. png_uint_32 chunks = dp->chunks;
  759. int active = 0, inactive = 0;
  760. int ct = dp->color_type;
  761. int bd = dp->bit_depth;
  762. unsigned int i;
  763. for (i=0; i<TTABLE_SIZE; ++i)
  764. {
  765. int transform = transform_info[i].transform;
  766. if ((transform_info[i].valid_chunks == 0 ||
  767. (transform_info[i].valid_chunks & chunks) != 0) &&
  768. (transform_info[i].color_mask_required & ct) ==
  769. transform_info[i].color_mask_required &&
  770. (transform_info[i].color_mask_absent & ct) == 0 &&
  771. (transform_info[i].bit_depths & bd) != 0 &&
  772. (transform_info[i].when & TRANSFORM_R) != 0)
  773. active |= transform;
  774. else if ((transform_info[i].when & TRANSFORM_R) != 0)
  775. inactive |= transform;
  776. }
  777. /* Some transforms appear multiple times in the table; the 'active' status
  778. * is the logical OR of these and the inactive status must be adjusted to
  779. * take this into account.
  780. */
  781. inactive &= ~active;
  782. dp->active_transforms = active;
  783. dp->ignored_transforms = inactive; /* excluding write-only transforms */
  784. if (active == 0)
  785. display_log(dp, INTERNAL_ERROR, "bad transform table");
  786. }
  787. }
  788. static int
  789. compare_read(struct display *dp, int applied_transforms)
  790. {
  791. /* Compare the png_info from read_ip with original_info */
  792. size_t rowbytes;
  793. png_uint_32 width, height;
  794. int bit_depth, color_type;
  795. int interlace_method, compression_method, filter_method;
  796. const char *e = NULL;
  797. png_get_IHDR(dp->read_pp, dp->read_ip, &width, &height, &bit_depth,
  798. &color_type, &interlace_method, &compression_method, &filter_method);
  799. # define C(item) if (item != dp->item) \
  800. display_log(dp, APP_WARNING, "IHDR " #item "(%lu) changed to %lu",\
  801. (unsigned long)dp->item, (unsigned long)item), e = #item
  802. /* The IHDR should be identical: */
  803. C(width);
  804. C(height);
  805. C(bit_depth);
  806. C(color_type);
  807. C(interlace_method);
  808. C(compression_method);
  809. C(filter_method);
  810. /* 'e' remains set to the name of the last thing changed: */
  811. if (e)
  812. display_log(dp, APP_ERROR, "IHDR changed (%s)", e);
  813. /* All the chunks from the original PNG should be preserved in the output PNG
  814. * because the PNG format has not been changed.
  815. */
  816. {
  817. unsigned long chunks =
  818. png_get_valid(dp->read_pp, dp->read_ip, 0xffffffff);
  819. if (chunks != dp->chunks)
  820. display_log(dp, APP_FAIL, "PNG chunks changed from 0x%lx to 0x%lx",
  821. (unsigned long)dp->chunks, chunks);
  822. }
  823. /* rowbytes should be the same */
  824. rowbytes = png_get_rowbytes(dp->read_pp, dp->read_ip);
  825. /* NOTE: on 64-bit systems this may trash the top bits of rowbytes,
  826. * which could lead to weird error messages.
  827. */
  828. if (rowbytes != dp->original_rowbytes)
  829. display_log(dp, APP_ERROR, "PNG rowbytes changed from %lu to %lu",
  830. (unsigned long)dp->original_rowbytes, (unsigned long)rowbytes);
  831. /* The rows should be the same too, unless the applied transforms includes
  832. * the shift transform, in which case low bits may have been lost.
  833. */
  834. {
  835. png_bytepp rows = png_get_rows(dp->read_pp, dp->read_ip);
  836. unsigned int mask; /* mask (if not zero) for the final byte */
  837. if (bit_depth < 8)
  838. {
  839. /* Need the stray bits at the end, this depends only on the low bits
  840. * of the image width; overflow does not matter. If the width is an
  841. * exact multiple of 8 bits this gives a mask of 0, not 0xff.
  842. */
  843. mask = 0xff & (0xff00 >> ((bit_depth * width) & 7));
  844. }
  845. else
  846. mask = 0;
  847. if (rows == NULL)
  848. display_log(dp, LIBPNG_BUG, "png_get_rows returned NULL");
  849. if ((applied_transforms & PNG_TRANSFORM_SHIFT) == 0 ||
  850. (dp->active_transforms & PNG_TRANSFORM_SHIFT) == 0 ||
  851. color_type == PNG_COLOR_TYPE_PALETTE)
  852. {
  853. unsigned long y;
  854. for (y=0; y<height; ++y)
  855. {
  856. png_bytep row = rows[y];
  857. png_bytep orig = dp->original_rows[y];
  858. if (memcmp(row, orig, rowbytes-(mask != 0)) != 0 || (mask != 0 &&
  859. ((row[rowbytes-1] & mask) != (orig[rowbytes-1] & mask))))
  860. {
  861. size_t x;
  862. /* Find the first error */
  863. for (x=0; x<rowbytes-1; ++x) if (row[x] != orig[x])
  864. break;
  865. display_log(dp, APP_FAIL,
  866. "byte(%lu,%lu) changed 0x%.2x -> 0x%.2x",
  867. (unsigned long)x, (unsigned long)y, orig[x], row[x]);
  868. return 0; /* don't keep reporting failed rows on 'continue' */
  869. }
  870. }
  871. }
  872. else
  873. {
  874. unsigned long y;
  875. int bpp; /* bits-per-pixel then bytes-per-pixel */
  876. /* components are up to 8 bytes in size */
  877. png_byte sig_bits[8];
  878. png_color_8p sBIT;
  879. if (png_get_sBIT(dp->read_pp, dp->read_ip, &sBIT) != PNG_INFO_sBIT)
  880. display_log(dp, INTERNAL_ERROR,
  881. "active shift transform but no sBIT in file");
  882. switch (color_type)
  883. {
  884. case PNG_COLOR_TYPE_GRAY:
  885. sig_bits[0] = sBIT->gray;
  886. bpp = bit_depth;
  887. break;
  888. case PNG_COLOR_TYPE_GA:
  889. sig_bits[0] = sBIT->gray;
  890. sig_bits[1] = sBIT->alpha;
  891. bpp = 2 * bit_depth;
  892. break;
  893. case PNG_COLOR_TYPE_RGB:
  894. sig_bits[0] = sBIT->red;
  895. sig_bits[1] = sBIT->green;
  896. sig_bits[2] = sBIT->blue;
  897. bpp = 3 * bit_depth;
  898. break;
  899. case PNG_COLOR_TYPE_RGBA:
  900. sig_bits[0] = sBIT->red;
  901. sig_bits[1] = sBIT->green;
  902. sig_bits[2] = sBIT->blue;
  903. sig_bits[3] = sBIT->alpha;
  904. bpp = 4 * bit_depth;
  905. break;
  906. default:
  907. display_log(dp, LIBPNG_ERROR, "invalid colour type %d",
  908. color_type);
  909. /*NOTREACHED*/
  910. bpp = 0;
  911. break;
  912. }
  913. {
  914. int b;
  915. for (b=0; 8*b<bpp; ++b)
  916. {
  917. /* libpng should catch this; if not there is a security issue
  918. * because an app (like this one) may overflow an array. In fact
  919. * libpng doesn't catch this at present.
  920. */
  921. if (sig_bits[b] == 0 || sig_bits[b] > bit_depth/*!palette*/)
  922. display_log(dp, LIBPNG_BUG,
  923. "invalid sBIT[%u] value %d returned for PNG bit depth %d",
  924. b, sig_bits[b], bit_depth);
  925. }
  926. }
  927. if (bpp < 8 && bpp != bit_depth)
  928. {
  929. /* sanity check; this is a grayscale PNG; something is wrong in the
  930. * code above.
  931. */
  932. display_log(dp, INTERNAL_ERROR, "invalid bpp %u for bit_depth %u",
  933. bpp, bit_depth);
  934. }
  935. switch (bit_depth)
  936. {
  937. int b;
  938. case 16: /* Two bytes per component, bit-endian */
  939. for (b = (bpp >> 4); b > 0; )
  940. {
  941. unsigned int sig = (unsigned int)(0xffff0000 >> sig_bits[b]);
  942. sig_bits[2*b+1] = (png_byte)sig;
  943. sig_bits[2*b+0] = (png_byte)(sig >> 8); /* big-endian */
  944. }
  945. break;
  946. case 8: /* One byte per component */
  947. for (b=0; b*8 < bpp; ++b)
  948. sig_bits[b] = (png_byte)(0xff00 >> sig_bits[b]);
  949. break;
  950. case 1: /* allowed, but dumb */
  951. /* Value is 1 */
  952. sig_bits[0] = 0xff;
  953. break;
  954. case 2: /* Replicate 4 times */
  955. /* Value is 1 or 2 */
  956. b = 0x3 & ((0x3<<2) >> sig_bits[0]);
  957. b |= b << 2;
  958. b |= b << 4;
  959. sig_bits[0] = (png_byte)b;
  960. break;
  961. case 4: /* Relicate twice */
  962. /* Value is 1, 2, 3 or 4 */
  963. b = 0xf & ((0xf << 4) >> sig_bits[0]);
  964. b |= b << 4;
  965. sig_bits[0] = (png_byte)b;
  966. break;
  967. default:
  968. display_log(dp, LIBPNG_BUG, "invalid bit depth %d", bit_depth);
  969. break;
  970. }
  971. /* Convert bpp to bytes; this gives '1' for low-bit depth grayscale,
  972. * where there are multiple pixels per byte.
  973. */
  974. bpp = (bpp+7) >> 3;
  975. /* The mask can be combined with sig_bits[0] */
  976. if (mask != 0)
  977. {
  978. mask &= sig_bits[0];
  979. if (bpp != 1 || mask == 0)
  980. display_log(dp, INTERNAL_ERROR, "mask calculation error %u, %u",
  981. bpp, mask);
  982. }
  983. for (y=0; y<height; ++y)
  984. {
  985. png_bytep row = rows[y];
  986. png_bytep orig = dp->original_rows[y];
  987. unsigned long x;
  988. for (x=0; x<(width-(mask!=0)); ++x)
  989. {
  990. int b;
  991. for (b=0; b<bpp; ++b)
  992. {
  993. if ((*row++ & sig_bits[b]) != (*orig++ & sig_bits[b]))
  994. {
  995. display_log(dp, APP_FAIL,
  996. "significant bits at (%lu[%u],%lu) changed %.2x->%.2x",
  997. x, b, y, orig[-1], row[-1]);
  998. return 0;
  999. }
  1000. }
  1001. }
  1002. if (mask != 0 && (*row & mask) != (*orig & mask))
  1003. {
  1004. display_log(dp, APP_FAIL,
  1005. "significant bits at (%lu[end],%lu) changed", x, y);
  1006. return 0;
  1007. }
  1008. } /* for y */
  1009. }
  1010. }
  1011. return 1; /* compare succeeded */
  1012. }
  1013. #ifdef PNG_WRITE_SUPPORTED
  1014. static void
  1015. buffer_write(struct display *dp, struct buffer *buffer, png_bytep data,
  1016. png_size_t size)
  1017. /* Generic write function used both from the write callback provided to
  1018. * libpng and from the generic read code.
  1019. */
  1020. {
  1021. /* Write the data into the buffer, adding buffers as required */
  1022. struct buffer_list *last = buffer->last;
  1023. size_t end_count = buffer->end_count;
  1024. while (size > 0)
  1025. {
  1026. size_t avail;
  1027. if (end_count >= sizeof last->buffer)
  1028. {
  1029. if (last->next == NULL)
  1030. {
  1031. last = buffer_extend(last);
  1032. if (last == NULL)
  1033. display_log(dp, APP_ERROR, "out of memory saving file");
  1034. }
  1035. else
  1036. last = last->next;
  1037. buffer->last = last; /* avoid the need to rewrite every time */
  1038. end_count = 0;
  1039. }
  1040. avail = (sizeof last->buffer) - end_count;
  1041. if (avail > size)
  1042. avail = size;
  1043. memcpy(last->buffer + end_count, data, avail);
  1044. end_count += avail;
  1045. size -= avail;
  1046. data += avail;
  1047. }
  1048. buffer->end_count = end_count;
  1049. }
  1050. static void PNGCBAPI
  1051. write_function(png_structp pp, png_bytep data, png_size_t size)
  1052. {
  1053. buffer_write(get_dp(pp), get_buffer(pp), data, size);
  1054. }
  1055. static void
  1056. write_png(struct display *dp, png_infop ip, int transforms)
  1057. {
  1058. display_clean_write(dp); /* safety */
  1059. buffer_start_write(&dp->written_file);
  1060. dp->operation = "write";
  1061. dp->transforms = transforms;
  1062. dp->write_pp = png_create_write_struct(PNG_LIBPNG_VER_STRING, dp,
  1063. display_error, display_warning);
  1064. if (dp->write_pp == NULL)
  1065. display_log(dp, APP_ERROR, "failed to create write png_struct");
  1066. png_set_write_fn(dp->write_pp, &dp->written_file, write_function,
  1067. NULL/*flush*/);
  1068. # ifdef PNG_SET_USER_LIMITS_SUPPORTED
  1069. /* Remove the user limits, if any */
  1070. png_set_user_limits(dp->write_pp, 0x7fffffff, 0x7fffffff);
  1071. # endif
  1072. /* Certain transforms require the png_info to be zapped to allow the
  1073. * transform to work correctly.
  1074. */
  1075. if (transforms & (PNG_TRANSFORM_PACKING|
  1076. PNG_TRANSFORM_STRIP_FILLER|
  1077. PNG_TRANSFORM_STRIP_FILLER_BEFORE))
  1078. {
  1079. int ct = dp->color_type;
  1080. if (transforms & (PNG_TRANSFORM_STRIP_FILLER|
  1081. PNG_TRANSFORM_STRIP_FILLER_BEFORE))
  1082. ct &= ~PNG_COLOR_MASK_ALPHA;
  1083. png_set_IHDR(dp->write_pp, ip, dp->width, dp->height, dp->bit_depth, ct,
  1084. dp->interlace_method, dp->compression_method, dp->filter_method);
  1085. }
  1086. png_write_png(dp->write_pp, ip, transforms, NULL/*params*/);
  1087. /* Clean it on the way out - if control returns to the caller then the
  1088. * written_file contains the required data.
  1089. */
  1090. display_clean_write(dp);
  1091. }
  1092. #endif /* WRITE_SUPPORTED */
  1093. static int
  1094. skip_transform(struct display *dp, int tr)
  1095. /* Helper to test for a bad combo and log it if it is skipped */
  1096. {
  1097. if ((dp->options & SKIP_BUGS) != 0 && is_bad_combo(tr))
  1098. {
  1099. /* Log this to stdout if logging is on, otherwise just do an information
  1100. * display_log.
  1101. */
  1102. if ((dp->options & LOG_SKIPPED) != 0)
  1103. {
  1104. printf("SKIP: %s transforms ", dp->filename);
  1105. while (tr != 0)
  1106. {
  1107. int next = first_transform(tr);
  1108. tr &= ~next;
  1109. printf("%s", transform_name(next));
  1110. if (tr != 0)
  1111. putchar('+');
  1112. }
  1113. putchar('\n');
  1114. }
  1115. else
  1116. display_log(dp, INFORMATION, "%s: skipped known bad combo 0x%x",
  1117. dp->filename, tr);
  1118. return 1; /* skip */
  1119. }
  1120. return 0; /* don't skip */
  1121. }
  1122. static void
  1123. test_one_file(struct display *dp, const char *filename)
  1124. {
  1125. /* First cache the file and update the display original file
  1126. * information for the new file.
  1127. */
  1128. dp->operation = "cache file";
  1129. dp->transforms = 0;
  1130. display_cache_file(dp, filename);
  1131. update_display(dp);
  1132. /* First test: if there are options that should be ignored for this file
  1133. * verify that they really are ignored.
  1134. */
  1135. if (dp->ignored_transforms != 0)
  1136. {
  1137. read_png(dp, &dp->original_file, "ignored transforms",
  1138. dp->ignored_transforms);
  1139. /* The result should be identical to the original_rows */
  1140. if (!compare_read(dp, 0/*transforms applied*/))
  1141. return; /* no point testing more */
  1142. }
  1143. #ifdef PNG_WRITE_SUPPORTED
  1144. /* Second test: write the original PNG data out to a new file (to test the
  1145. * write side) then read the result back in and make sure that it hasn't
  1146. * changed.
  1147. */
  1148. dp->operation = "write";
  1149. write_png(dp, dp->original_ip, 0/*transforms*/);
  1150. read_png(dp, &dp->written_file, NULL, 0/*transforms*/);
  1151. if (!compare_read(dp, 0/*transforms applied*/))
  1152. return;
  1153. #endif
  1154. /* Third test: the active options. Test each in turn, or, with the
  1155. * EXHAUSTIVE option, test all possible combinations.
  1156. */
  1157. {
  1158. /* Use unsigned int here because the code below to increment through all
  1159. * the possibilities exhaustively has to use a compare and that must be
  1160. * unsigned, because some transforms are negative on a 16-bit system.
  1161. */
  1162. unsigned int active = dp->active_transforms;
  1163. const int exhaustive = (dp->options & EXHAUSTIVE) != 0;
  1164. unsigned int current = first_transform(active);
  1165. unsigned int bad_transforms = 0;
  1166. unsigned int bad_combo = ~0U; /* bitwise AND of failing transforms */
  1167. unsigned int bad_combo_list = 0; /* bitwise OR of failures */
  1168. for (;;)
  1169. {
  1170. read_png(dp, &dp->original_file, "active transforms", current);
  1171. /* If this involved any irreversible transformations then if we write
  1172. * it out with just the reversible transformations and read it in again
  1173. * with the same transforms we should get the same thing. At present
  1174. * this isn't done - it just seems like a waste of time and it would
  1175. * require two sets of read png_struct/png_info.
  1176. *
  1177. * If there were no irreversible transformations then if we write it
  1178. * out and read it back in again (without the reversible transforms)
  1179. * we should get back to the place where we started.
  1180. */
  1181. #ifdef PNG_WRITE_SUPPORTED
  1182. if ((current & write_transforms) == current)
  1183. {
  1184. /* All transforms reversible: write the PNG with the transformations
  1185. * reversed, then read it back in with no transformations. The
  1186. * result should be the same as the original apart from the loss of
  1187. * low order bits because of the SHIFT/sBIT transform.
  1188. */
  1189. dp->operation = "reversible transforms";
  1190. write_png(dp, dp->read_ip, current);
  1191. /* And if this is read back in, because all the transformations were
  1192. * reversible, the result should be the same.
  1193. */
  1194. read_png(dp, &dp->written_file, NULL, 0);
  1195. if (!compare_read(dp, current/*for the SHIFT/sBIT transform*/))
  1196. {
  1197. /* This set of transforms failed. If a single bit is set - if
  1198. * there is just one transform - don't include this in further
  1199. * 'exhaustive' tests. Notice that each transform is tested on
  1200. * its own before testing combos in the exhaustive case.
  1201. */
  1202. if (is_combo(current))
  1203. {
  1204. bad_combo &= current;
  1205. bad_combo_list |= current;
  1206. }
  1207. else
  1208. bad_transforms |= current;
  1209. }
  1210. }
  1211. #endif
  1212. /* Now move to the next transform */
  1213. if (exhaustive) /* all combinations */
  1214. {
  1215. unsigned int next = current;
  1216. do
  1217. {
  1218. if (next == read_transforms) /* Everything tested */
  1219. goto combo;
  1220. ++next;
  1221. } /* skip known bad combos if the relevant option is set; skip
  1222. * combos involving known bad single transforms in all cases.
  1223. */
  1224. while ( (next & read_transforms) <= current
  1225. || (next & active) == 0 /* skip cases that do nothing */
  1226. || (next & bad_transforms) != 0
  1227. || skip_transform(dp, next));
  1228. assert((next & read_transforms) == next);
  1229. current = next;
  1230. }
  1231. else /* one at a time */
  1232. {
  1233. active &= ~current;
  1234. if (active == 0)
  1235. goto combo;
  1236. current = first_transform(active);
  1237. }
  1238. }
  1239. combo:
  1240. if (dp->options & FIND_BAD_COMBOS)
  1241. {
  1242. /* bad_combos identifies the combos that occur in all failing cases;
  1243. * bad_combo_list identifies transforms that do not prevent the
  1244. * failure.
  1245. */
  1246. if (bad_combo != ~0U)
  1247. printf("%s[0x%x]: PROBLEM: 0x%x[0x%x] ANTIDOTE: 0x%x\n",
  1248. dp->filename, active, bad_combo, bad_combo_list,
  1249. rw_transforms & ~bad_combo_list);
  1250. else
  1251. printf("%s: no %sbad combos found\n", dp->filename,
  1252. (dp->options & SKIP_BUGS) ? "additional " : "");
  1253. }
  1254. }
  1255. }
  1256. static int
  1257. do_test(struct display *dp, const char *file)
  1258. /* Exists solely to isolate the setjmp clobbers */
  1259. {
  1260. int ret = setjmp(dp->error_return);
  1261. if (ret == 0)
  1262. {
  1263. test_one_file(dp, file);
  1264. return 0;
  1265. }
  1266. else if (ret < ERRORS) /* shouldn't longjmp on warnings */
  1267. display_log(dp, INTERNAL_ERROR, "unexpected return code %d", ret);
  1268. return ret;
  1269. }
  1270. int
  1271. main(const int argc, const char * const * const argv)
  1272. {
  1273. /* For each file on the command line test it with a range of transforms */
  1274. int option_end, ilog = 0;
  1275. struct display d;
  1276. validate_T();
  1277. display_init(&d);
  1278. for (option_end=1; option_end<argc; ++option_end)
  1279. {
  1280. const char *name = argv[option_end];
  1281. if (strcmp(name, "--verbose") == 0)
  1282. d.options = (d.options & ~LEVEL_MASK) | VERBOSE;
  1283. else if (strcmp(name, "--warnings") == 0)
  1284. d.options = (d.options & ~LEVEL_MASK) | WARNINGS;
  1285. else if (strcmp(name, "--errors") == 0)
  1286. d.options = (d.options & ~LEVEL_MASK) | ERRORS;
  1287. else if (strcmp(name, "--quiet") == 0)
  1288. d.options = (d.options & ~LEVEL_MASK) | QUIET;
  1289. else if (strcmp(name, "--exhaustive") == 0)
  1290. d.options |= EXHAUSTIVE;
  1291. else if (strcmp(name, "--fast") == 0)
  1292. d.options &= ~EXHAUSTIVE;
  1293. else if (strcmp(name, "--strict") == 0)
  1294. d.options |= STRICT;
  1295. else if (strcmp(name, "--relaxed") == 0)
  1296. d.options &= ~STRICT;
  1297. else if (strcmp(name, "--log") == 0)
  1298. {
  1299. ilog = option_end; /* prevent display */
  1300. d.options |= LOG;
  1301. }
  1302. else if (strcmp(name, "--nolog") == 0)
  1303. d.options &= ~LOG;
  1304. else if (strcmp(name, "--continue") == 0)
  1305. d.options |= CONTINUE;
  1306. else if (strcmp(name, "--stop") == 0)
  1307. d.options &= ~CONTINUE;
  1308. else if (strcmp(name, "--skip-bugs") == 0)
  1309. d.options |= SKIP_BUGS;
  1310. else if (strcmp(name, "--test-all") == 0)
  1311. d.options &= ~SKIP_BUGS;
  1312. else if (strcmp(name, "--log-skipped") == 0)
  1313. d.options |= LOG_SKIPPED;
  1314. else if (strcmp(name, "--nolog-skipped") == 0)
  1315. d.options &= ~LOG_SKIPPED;
  1316. else if (strcmp(name, "--find-bad-combos") == 0)
  1317. d.options |= FIND_BAD_COMBOS;
  1318. else if (strcmp(name, "--nofind-bad-combos") == 0)
  1319. d.options &= ~FIND_BAD_COMBOS;
  1320. else if (name[0] == '-' && name[1] == '-')
  1321. {
  1322. fprintf(stderr, "pngimage: %s: unknown option\n", name);
  1323. return 99;
  1324. }
  1325. else
  1326. break; /* Not an option */
  1327. }
  1328. {
  1329. int i;
  1330. int errors = 0;
  1331. for (i=option_end; i<argc; ++i)
  1332. {
  1333. {
  1334. int ret = do_test(&d, argv[i]);
  1335. if (ret > QUIET) /* abort on user or internal error */
  1336. return 99;
  1337. }
  1338. /* Here on any return, including failures, except user/internal issues
  1339. */
  1340. {
  1341. const int pass = (d.options & STRICT) ?
  1342. RESULT_STRICT(d.results) : RESULT_RELAXED(d.results);
  1343. if (!pass)
  1344. ++errors;
  1345. if (d.options & LOG)
  1346. {
  1347. int j;
  1348. printf("%s: pngimage ", pass ? "PASS" : "FAIL");
  1349. for (j=1; j<option_end; ++j) if (j != ilog)
  1350. printf("%s ", argv[j]);
  1351. printf("%s\n", d.filename);
  1352. }
  1353. }
  1354. display_clean(&d);
  1355. }
  1356. return errors != 0;
  1357. }
  1358. }
  1359. #else /* !PNG_INFO_IMAGE_SUPPORTED || !PNG_READ_SUPPORTED */
  1360. int
  1361. main(void)
  1362. {
  1363. fprintf(stderr, "pngimage: no support for png_read/write_image\n");
  1364. return 77;
  1365. }
  1366. #endif