PNG_POV.C 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. /*****************************************************************************
  2. * png_pov.c
  3. *
  4. * This module contains the code to read and write the PNG output file
  5. *
  6. * from Persistence of Vision(tm) Ray Tracer
  7. * Copyright 1996,1999 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. * NOTICE: This source code file is provided so that users may experiment
  10. * with enhancements to POV-Ray and to port the software to platforms other
  11. * than those supported by the POV-Ray Team. There are strict rules under
  12. * which you are permitted to use this file. The rules are in the file
  13. * named POVLEGAL.DOC which should be distributed with this file.
  14. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  16. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. * Original patch copyright 1995 Andreas Dilger
  23. * Updated for POV 3.0 by Tim Wegner, August 1995.
  24. * Updated to allow resuming by Andreas Dilger, Sept 1995.
  25. * Updated to support Alpha channel input/output by Andreas Dilger, Sept 1995
  26. * Updated to set the flush distance based on the file buffer size, Dec 1995
  27. * Updated to use the libpng 0.87 messaging functions, Dec 1995
  28. * Updated to use the libpng 0.89 structure interface, Jun 1996
  29. *
  30. * Modifications by Hans-Detlev Fink, January 1999, used with permission
  31. *
  32. *****************************************************************************/
  33. /*****************************************************************************
  34. * This code requires the use of libpng, Group 42's PNG reference library.
  35. * libpng is Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
  36. *
  37. * This code also requires the use of Zlib,
  38. * Zlib is Copyright (C) 1995 Jean-loup Gailly and Mark Adler
  39. *
  40. * The latest version of these libraries are available at ftp.uu.net as
  41. *
  42. * /graphics/png/libpngXX.tar.gz.
  43. * /archiver/zlib/zlib-XX.tar.gz.
  44. *
  45. * where XX is the latest version of the library.
  46. *
  47. *****************************************************************************/
  48. #include "frame.h"
  49. #include "povproto.h"
  50. #include "povray.h"
  51. #include "optout.h"
  52. #include "png.h"
  53. #include "png_pov.h"
  54. /*****************************************************************************
  55. * Local preprocessor defines
  56. ******************************************************************************/
  57. /* Number of scanlines between output flushes, and hence the maximum number of
  58. * lines lost for an interrupted render. Note that making it much smaller
  59. * than about 10 for a 640x480 image will noticably degrade compression.
  60. * If a very small buffer is specified, we don't want to flush more than once
  61. * every 10 lines or so (assuming a 2:1 compression ratio).
  62. */
  63. /*
  64. #define FLUSH_DIST 10
  65. #define FLUSH_DIST (*width >= 640 ? 10 : 6400 / *width)
  66. */
  67. #define FLUSH_DIST ((opts.Options & BUFFERED_OUTPUT && \
  68. handle->buffer_size > (*width * png_stride * 5)) ? \
  69. (handle->buffer_size / (*width * png_stride)): \
  70. (*width >= 640 ? 10 : 6400 / *width))
  71. #define NTEXT 15 /* Maximum number of tEXt comment blocks */
  72. #define MAXTEXT 1024 /* Maximum length of a tEXt message */
  73. /*****************************************************************************
  74. * Local typedefs
  75. ******************************************************************************/
  76. /*****************************************************************************
  77. * Local variables
  78. ******************************************************************************/
  79. static png_struct *png_ptr = NULL;
  80. static png_info *info_ptr = NULL;
  81. static png_struct *o_png_ptr = NULL;
  82. static png_byte *row_ptr = NULL;
  83. static int png_stride;
  84. static char tmp_fname[FILE_NAME_LENGTH];
  85. static FILE *tmp_fp = NULL;
  86. /*****************************************************************************
  87. * Static functions
  88. ******************************************************************************/
  89. static int Open_Png_File (FILE_HANDLE *handle, char *name, int *width,
  90. int *height, int buffer_size, int mode);
  91. static void Write_Png_Line (FILE_HANDLE *handle, COLOUR *line_data,
  92. int line_number);
  93. static int Read_Png_Line (FILE_HANDLE *handle, COLOUR *line_data,
  94. int *line_number);
  95. static void Close_Png_File (FILE_HANDLE *handle);
  96. /* These are replacement error and warning functions for the libpng code */
  97. static void png_pov_err (png_structp, png_const_charp);
  98. static void png_pov_warn (png_structp, png_const_charp);
  99. /* This is an internal function for libpng */
  100. extern
  101. #ifdef __cplusplus
  102. "C" {
  103. #endif
  104. void png_write_finish_row (png_structp);
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. /*****************************************************************************
  109. *
  110. * FUNCTION : Get_Png_File_Handle
  111. *
  112. * ARGUMENTS : none
  113. *
  114. * MODIFIED ARGS : none
  115. *
  116. * RETURN VALUE : File Handle
  117. *
  118. * AUTHOR : Andreas Dilger
  119. *
  120. * DESCRIPTION
  121. *
  122. * Allocate memory for and setup file handle for PNG file.
  123. *
  124. * CHANGES
  125. *
  126. * Updated for POV-Ray 3.X - [TIW]
  127. *
  128. ******************************************************************************/
  129. FILE_HANDLE *Get_Png_File_Handle()
  130. {
  131. FILE_HANDLE *handle;
  132. handle = (FILE_HANDLE *)POV_MALLOC(sizeof(FILE_HANDLE), "PNG file handle");
  133. handle->Open_File_p = Open_Png_File;
  134. handle->Write_Line_p = Write_Png_Line;
  135. handle->Read_Line_p = Read_Png_Line;
  136. handle->Read_Image_p = Read_Png_Image;
  137. handle->Close_File_p = Close_Png_File;
  138. handle->buffer_size = 0;
  139. handle->file = NULL;
  140. handle->buffer = NULL;
  141. return (handle);
  142. }
  143. /*****************************************************************************
  144. *
  145. * FUNCTION : png_pov_warn
  146. *
  147. * ARGUMENTS : png_struct *png_ptr; char *msg;
  148. *
  149. * MODIFIED ARGS :
  150. *
  151. * RETURN VALUE :
  152. *
  153. * AUTHOR : Andreas Dilger
  154. *
  155. * DESCRIPTION
  156. *
  157. * Prints an warning message using the POV I/O functions. This uses the
  158. * png io_ptr to determine whether error messages should be printed or
  159. * not.
  160. *
  161. * CHANGES
  162. *
  163. ******************************************************************************/
  164. static void png_pov_warn(png_structp png_ptr, png_const_charp msg)
  165. {
  166. if (png_get_error_ptr(png_ptr))
  167. Warning(0.0,"libpng: %s\n",msg);
  168. }
  169. /*****************************************************************************
  170. *
  171. * FUNCTION : png_pov_err
  172. *
  173. * ARGUMENTS : png_struct *png_ptr; char *msg;
  174. *
  175. * MODIFIED ARGS :
  176. *
  177. * RETURN VALUE :
  178. *
  179. * AUTHOR : Andreas Dilger
  180. *
  181. * DESCRIPTION
  182. *
  183. * If the png io_ptr is TRUE, this prints an error message using the POV
  184. * I/O function. It will return to the location of the last setjmp call
  185. * for this stream in any case.
  186. *
  187. * CHANGES
  188. *
  189. ******************************************************************************/
  190. static void png_pov_err(png_structp png_ptr, png_const_charp msg)
  191. {
  192. if (png_get_error_ptr(png_ptr))
  193. Error_Line("libpng: %s\n",msg);
  194. longjmp(png_ptr->jmpbuf,1);
  195. }
  196. /*****************************************************************************
  197. *
  198. * FUNCTION : Open_Png_File
  199. *
  200. * ARGUMENTS : FILE_HANDLE *handle; char *name; int *width; int *height;
  201. * int buffer_size; int mode;
  202. *
  203. * MODIFIED ARGS : handle, width, height
  204. *
  205. * RETURN VALUE : 1 or 0 for success or failure
  206. *
  207. * AUTHOR : Andreas Dilger
  208. *
  209. * DESCRIPTION
  210. *
  211. * Open a PNG file and allocate the needed PNG structure buffers
  212. *
  213. * CHANGES
  214. *
  215. * Updated for POV-Ray 3.X - [TIW]
  216. * Updated to handle resuming interrupted traces, Sept 1995 - [AED]
  217. * Updated to output grayscale heightfield if requested - [AED]
  218. * Updated to allow grayscale and alpha together, Oct 1995 - [AED]
  219. * Updated to write gamma differently based on file type, Nov 1995 - [AED]
  220. * Changed temp file name from TEMP_FILE_BASE to scene name, Feb 1996 - [AED]
  221. * Changed temp file from scene name to path + scene name, Jun 1996 - [AED]
  222. *
  223. ******************************************************************************/
  224. static int Open_Png_File(FILE_HANDLE *handle, char *name, int *width, int *height, int buffer_size, int mode)
  225. {
  226. handle->mode = mode;
  227. handle->filename = name;
  228. switch (mode)
  229. {
  230. case READ_MODE:
  231. /* We can't resume from stdout. */
  232. if (opts.Options & TO_STDOUT)
  233. {
  234. Status_Info("\n");
  235. return(0);
  236. }
  237. /* Initialize PNG output routines using temporary file name. We
  238. * need to use the path, or the rename will fail if the temp file
  239. * is not on the same drive as the output file.
  240. */
  241. sprintf(tmp_fname, "%s%s.tpn", opts.Output_Path, opts.Scene_Name);
  242. /* Move the old output file to a temporary file, so it can be
  243. * read in and simultaneously written out to the new output file.
  244. * Note that this can potentially be destructive, but it is
  245. * impossible to change the output stream in mid-write. We have
  246. * to check if a temp file already exists, in case the transfer
  247. * has been previously aborted.
  248. */
  249. if ((tmp_fp = fopen(tmp_fname,READ_BINFILE_STRING)) == NULL)
  250. {
  251. /* The temp file doesn't exist. Try the original file. */
  252. if ((tmp_fp = fopen(name,READ_BINFILE_STRING)) == NULL)
  253. {
  254. Status_Info("\n");
  255. return(0); /* Neither file exists - start from scratch. */
  256. }
  257. else /* The original file exists, but the temp file doesn't. */
  258. {
  259. fclose(tmp_fp);
  260. if (RENAME_FILE(name,tmp_fname) == RENAME_FILE_ERR)
  261. {
  262. Error("\nError making temporary PNG file for continuing trace.\n");
  263. }
  264. /* Open the original file (now with a new name) for reading */
  265. if ((tmp_fp = fopen(tmp_fname,READ_BINFILE_STRING)) == NULL)
  266. {
  267. RENAME_FILE(tmp_fname,name); /* Try to rename back - not crucial */
  268. Error("\nError opening temporary PNG file for continuing trace.\n");
  269. }
  270. }
  271. }
  272. /* The temp file already exists. If we can open the original file
  273. * as well, then there is something wrong, and we can't automatically
  274. * defide which file to delete.
  275. */
  276. else if((handle->file = fopen(name,READ_BINFILE_STRING)) != NULL)
  277. {
  278. fclose(tmp_fp);
  279. fclose(handle->file);
  280. Error_Line("\nBoth original and temporary PNG files exist after an interrupted trace.\n");
  281. Error("Please delete either %s or %s (preferrably the smaller).\n",name,tmp_fname);
  282. }
  283. /* Try to open the new output file for writing. If we can't, try
  284. * to move the old one back so that users don't fret if it's missing.
  285. * PNG will be able to continue without loss of data either way.
  286. */
  287. if ((handle->file = fopen(name, WRITE_BINFILE_STRING)) == NULL)
  288. {
  289. Status_Info("\n");
  290. fclose(tmp_fp);
  291. RENAME_FILE(tmp_fname,name);
  292. return(-1);
  293. }
  294. if (buffer_size > 0)
  295. {
  296. handle->buffer = (char *)POV_MALLOC(buffer_size, "PNG file buffer");
  297. setvbuf(handle->file, handle->buffer, _IOFBF, buffer_size);
  298. }
  299. handle->buffer_size = buffer_size;
  300. /* The original input file */
  301. if ((o_png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  302. (png_voidp)FALSE, png_pov_err, png_pov_warn)) == NULL ||
  303. (info_ptr = png_create_info_struct(o_png_ptr)) == NULL)
  304. {
  305. Error("Error allocating PNG data structures");
  306. }
  307. if (setjmp(o_png_ptr->jmpbuf))
  308. {
  309. /* If we get here, we had a problem reading the file */
  310. Status_Info("\n");
  311. if (handle->buffer != NULL)
  312. {
  313. POV_FREE(handle->buffer);
  314. handle->buffer = NULL;
  315. }
  316. png_destroy_read_struct(&o_png_ptr, &info_ptr, (png_infopp)NULL);
  317. fclose(handle->file);
  318. handle->file = NULL;
  319. fclose(tmp_fp);
  320. tmp_fp = NULL;
  321. return(0);
  322. }
  323. /* Set up the compression structure */
  324. png_init_io(o_png_ptr, tmp_fp);
  325. /* Read in header info from the file */
  326. png_read_info(o_png_ptr, info_ptr);
  327. if (info_ptr->color_type & ~(PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA))
  328. {
  329. return(0);
  330. }
  331. Status_Info("\nResuming interrupted trace from %s",handle->filename);
  332. /* The new output file. Thank god for re-entrant libpng/libz code! */
  333. if ((png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
  334. (png_voidp)TRUE, png_pov_err, png_pov_warn)) == NULL)
  335. {
  336. Error("Error allocating PNG data structures");
  337. }
  338. if (setjmp(png_ptr->jmpbuf))
  339. {
  340. /* If we get here, we had a problem writing the file */
  341. Status_Info("\n");
  342. if (handle->buffer != NULL)
  343. {
  344. POV_FREE(handle->buffer);
  345. handle->buffer = NULL;
  346. }
  347. png_destroy_read_struct(&o_png_ptr, &info_ptr, (png_infopp)NULL);
  348. png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
  349. fclose(handle->file);
  350. handle->file = NULL;
  351. fclose(tmp_fp);
  352. tmp_fp = NULL;
  353. if (DELETE_FILE(name) != DELETE_FILE_ERR)
  354. {
  355. RENAME_FILE(tmp_fname,name); /* Try to get the original file back */
  356. }
  357. return(-1);
  358. }
  359. /* Set up the compression structure */
  360. png_init_io(png_ptr, handle->file);
  361. /* Fill in the relevant image information from the resumed file */
  362. *width = handle->width = info_ptr->width;
  363. *height = handle->height = info_ptr->height;
  364. /* Find out if file is a valid format, and if it had Alpha in it */
  365. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  366. {
  367. opts.Options |= OUTPUT_ALPHA;
  368. }
  369. if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) == PNG_COLOR_TYPE_GRAY)
  370. {
  371. opts.Options |= HF_GRAY_16;
  372. opts.PaletteOption = GREY; /* Force grayscale preview */
  373. }
  374. #if defined(PNG_READ_sBIT_SUPPORTED)
  375. if (info_ptr->valid & PNG_INFO_sBIT)
  376. {
  377. if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  378. {
  379. opts.OutputQuality = info_ptr->sig_bit.red;
  380. }
  381. else
  382. {
  383. opts.OutputQuality = info_ptr->sig_bit.gray;
  384. }
  385. }
  386. #else /* !PNG_READ_sBIT_SUPPORTED */
  387. if (info_ptr->bit_depth == 8 && opts.OutputQuality > 8 ||
  388. info_ptr->bit_depth == 16 && opts.OutputQuality <= 8)
  389. {
  390. Error("\nSpecified color depth +fn%d not the same as depth %d in %s\n",
  391. opts.OutputQuality, info_ptr->bit_depth, name);
  392. }
  393. #endif /* !PNG_READ_sBIT_SUPPORTED */
  394. #if defined(PNG_READ_oFFs_SUPPORTED)
  395. opts.First_Column = info_ptr->x_offset;
  396. opts.First_Line = info_ptr->y_offset;
  397. #endif /* PNG_READ_oFFs_SUPPORTED */
  398. png_write_info(png_ptr, info_ptr);
  399. png_stride = info_ptr->color_type & PNG_COLOR_MASK_COLOR ? 3 : 1;
  400. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  401. png_stride++;
  402. png_stride *= (opts.OutputQuality + 7) / 8;
  403. row_ptr = (png_byte *)POV_MALLOC(*width*png_stride,"PNG read row buffer");
  404. break;
  405. case WRITE_MODE:
  406. if (opts.Options & TO_STDOUT)
  407. {
  408. buffer_size = 0;
  409. handle->file = stdout;
  410. }
  411. else if ((handle->file = fopen(name, WRITE_BINFILE_STRING)) == NULL)
  412. {
  413. return(0);
  414. }
  415. if (buffer_size != 0)
  416. {
  417. handle->buffer = (char *)POV_MALLOC((size_t)buffer_size, "PNG file buffer");
  418. setvbuf(handle->file, handle->buffer, _IOFBF, buffer_size);
  419. }
  420. handle->buffer_size = buffer_size;
  421. if ((png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
  422. (png_voidp)TRUE, png_pov_err, png_pov_warn)) == NULL ||
  423. (info_ptr = png_create_info_struct(png_ptr)) == NULL)
  424. {
  425. Error("Error allocating PNG data structures");
  426. }
  427. if (setjmp(png_ptr->jmpbuf))
  428. {
  429. /* If we get here, we had a problem writing the file */
  430. if (handle->buffer != NULL)
  431. {
  432. POV_FREE(handle->buffer);
  433. handle->buffer = NULL;
  434. }
  435. png_destroy_write_struct(&png_ptr, &info_ptr);
  436. fclose(handle->file);
  437. handle->file = NULL;
  438. return(0);
  439. }
  440. /* Set up the compression structure */
  441. png_init_io(png_ptr, handle->file);
  442. /* Fill in the relevant image information */
  443. info_ptr->width = handle->width = *width;
  444. info_ptr->height = handle->height = *height;
  445. info_ptr->bit_depth = 8 * ((opts.OutputQuality + 7) / 8);
  446. if (opts.Options & HF_GRAY_16)
  447. {
  448. info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
  449. }
  450. else
  451. {
  452. info_ptr->color_type = PNG_COLOR_TYPE_RGB;
  453. }
  454. if (opts.Options & OUTPUT_ALPHA)
  455. {
  456. info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
  457. }
  458. #if defined(PNG_WRITE_sBIT_SUPPORTED)
  459. if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  460. {
  461. info_ptr->sig_bit.red =
  462. info_ptr->sig_bit.green =
  463. info_ptr->sig_bit.blue = opts.OutputQuality;
  464. }
  465. else
  466. {
  467. info_ptr->sig_bit.gray = opts.OutputQuality;
  468. }
  469. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  470. {
  471. info_ptr->sig_bit.alpha = opts.OutputQuality;
  472. }
  473. info_ptr->valid |= PNG_INFO_sBIT;
  474. #endif /* PNG_WRITE_sBIT_SUPPORTED */
  475. #if defined(PNG_WRITE_gAMA_SUPPORTED)
  476. if (handle->file_type & (IMAGE_FTYPE | GRAY_FTYPE))
  477. {
  478. info_ptr->gamma = 1.0/opts.DisplayGamma;
  479. info_ptr->valid |= PNG_INFO_gAMA;
  480. }
  481. else if (handle->file_type & (HIST_FTYPE | HF_FTYPE))
  482. {
  483. info_ptr->gamma = 1.0;
  484. info_ptr->valid |= PNG_INFO_gAMA;
  485. }
  486. #endif /* PNG_WRITE_gAMA_SUPPORTED */
  487. #if defined(PNG_WRITE_oFFs_SUPPORTED)
  488. if (opts.First_Column != 0 || opts.First_Line != 0)
  489. {
  490. info_ptr->x_offset = opts.First_Column;
  491. info_ptr->y_offset = opts.First_Line;
  492. info_ptr->offset_unit_type = PNG_OFFSET_PIXEL;
  493. info_ptr->valid |= PNG_INFO_oFFs;
  494. }
  495. #endif /* PNG_WRITE_oFFs_SUPPORTED */
  496. if (handle->file_type & HIST_FTYPE)
  497. {
  498. /* If we are writing a histogram file, we could potentially output
  499. * a pCAL chunk with the max histogram value, to allow recovery of
  500. * the original timing data. However, pCAL is not yet official at
  501. * the time of this writing.
  502. */
  503. }
  504. png_write_info(png_ptr, info_ptr);
  505. png_stride = info_ptr->color_type & PNG_COLOR_MASK_COLOR ? 3 : 1;
  506. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  507. png_stride++;
  508. png_stride *= (opts.OutputQuality + 7) / 8;
  509. row_ptr = (png_byte *)POV_MALLOC(*width*png_stride, "PNG write row buffer");
  510. #if defined(PNG_WRITE_FLUSH_SUPPORTED)
  511. /* Set libpng to flush the output buffers every few lines, so that
  512. * in case of a rude crash we don't lose very much data.
  513. */
  514. png_set_flush(png_ptr, FLUSH_DIST);
  515. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  516. break;
  517. case APPEND_MODE:
  518. #if defined(PNG_WRITE_FLUSH_SUPPORTED)
  519. if (setjmp(png_ptr->jmpbuf))
  520. {
  521. /* If we get here, we had a problem writing the file */
  522. if (handle->buffer != NULL)
  523. {
  524. POV_FREE(handle->buffer);
  525. handle->buffer = NULL;
  526. }
  527. png_destroy_write_struct(&png_ptr, &info_ptr);
  528. fclose(handle->file);
  529. handle->file = NULL;
  530. return(0);
  531. }
  532. /* Write out the data in the PNG/zlib buffers, and set automatic
  533. * flushing for every few scanlines, in case of a rude crash.
  534. */
  535. png_write_flush(png_ptr);
  536. png_set_flush(png_ptr, FLUSH_DIST);
  537. #else /* !PNG_WRITE_FLUSH_SUPPORTED */
  538. fflush(handle->file);
  539. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  540. if (!(opts.Options & TO_STDOUT) && (handle->file =
  541. freopen(name, APPEND_BINFILE_STRING, handle->file)) == NULL)
  542. {
  543. if (handle->buffer != NULL)
  544. {
  545. POV_FREE(handle->buffer);
  546. handle->buffer = NULL;
  547. }
  548. png_destroy_write_struct(&png_ptr, &info_ptr);
  549. return(0);
  550. }
  551. /* Delete the temporary data file. Note that the new output file
  552. * is all ready to go - nothing needs to be done here.
  553. */
  554. if (tmp_fp != NULL)
  555. {
  556. fclose(tmp_fp);
  557. tmp_fp = NULL;
  558. if (DELETE_FILE(tmp_fname) == DELETE_FILE_ERR)
  559. {
  560. Warning(0.0,"Can't delete temporary PNG file %s. Please delete it.\n",tmp_fname);
  561. }
  562. }
  563. }
  564. return(1);
  565. }
  566. /*****************************************************************************
  567. *
  568. * FUNCTION : Write_Png_Line
  569. *
  570. * ARGUMENTS : handle, line_data, line_number
  571. *
  572. * MODIFIED ARGS : none
  573. *
  574. * RETURN VALUE : none
  575. *
  576. * AUTHOR : Andreas Dilger
  577. *
  578. * DESCRIPTION
  579. *
  580. * Write a line of data to the PNG file
  581. *
  582. * CHANGES
  583. *
  584. * Updated for POV-Ray 3.X - [TIW]
  585. * Updated to do flush output to reduce data loss - [AED]
  586. * Updated to output Alpha channel if requested - [AED]
  587. * Updated to output grayscale heightfield if requested - [AED]
  588. * Updated to allow grayscale in 5-8 bpp if desired, Oct 1995 - [AED]
  589. * Updated to allow grayscale and alpha together, Oct 1995 - [AED]
  590. * Changed how bit-depths 9-15 get promoted to 16 bits based on new
  591. * recommendations from the PNG Group, Nov 1995 - [AED]
  592. *
  593. ******************************************************************************/
  594. static void Write_Png_Line(FILE_HANDLE *handle, COLOUR *line_data, int line_number)
  595. {
  596. register int col, j;
  597. int himask;
  598. int color;
  599. /*
  600. * We must copy all the values because PNG expects RGBRGB bytes, but
  601. * POV-Ray stores RGB components in separate arrays as floats. In
  602. * order to use the full scale values at the lower bit depth, PNG
  603. * recommends filling the low-order bits with a copy of the high-order
  604. * bits. However, studies have shown that filling the low order bits
  605. * with constant bits significantly improves compression, which I'm
  606. * doing here. Note that since the true bit depth is stored in the
  607. * sBIT chunk, the extra packed bits are not important.
  608. */
  609. switch (opts.OutputQuality)
  610. {
  611. case 5:
  612. case 6:
  613. case 7:
  614. /* Handle shifting for arbitrary output bit depth */
  615. himask = 0xFF ^ ((1 << (8 - opts.OutputQuality)) - 1);
  616. if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) == PNG_COLOR_TYPE_GRAY)
  617. {
  618. for (col = j = 0; col < handle->width; col++, j += png_stride)
  619. {
  620. color = (png_byte)floor((line_data[col][RED]*0.30 +
  621. line_data[col][GREEN]*0.59 +
  622. line_data[col][BLUE]*0.11) * 255.0);
  623. /* Use left-bit replication (LBR) for bit depths < 8 */
  624. row_ptr[j] = color & himask;
  625. row_ptr[j] |= color >> opts.OutputQuality;
  626. /* Handle Alpha here if needed - must use exact bit replication
  627. * instead of truncation or 100... termination
  628. */
  629. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  630. {
  631. color = 255 - (int)floor(line_data[col][TRANSM] * 255.0);
  632. row_ptr[j + 1] = color & himask;
  633. row_ptr[j + 1] |= color >> opts.OutputQuality;
  634. }
  635. }
  636. }
  637. else
  638. {
  639. for (col = j = 0; col < handle->width; col++, j += png_stride)
  640. {
  641. color = (int)floor(line_data[col][RED] * 255.0);
  642. row_ptr[j] = color & himask;
  643. row_ptr[j] |= color >> opts.OutputQuality;
  644. color = (int)floor(line_data[col][GREEN] * 255.0);
  645. row_ptr[j + 1] = color & himask;
  646. row_ptr[j + 1] |= color >> opts.OutputQuality;
  647. color = (int)floor(line_data[col][BLUE] * 255.0);
  648. row_ptr[j + 2] = color & himask;
  649. row_ptr[j + 2] |= color >> opts.OutputQuality;
  650. /* Handle Alpha here if needed */
  651. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  652. {
  653. color = 255 - (int)floor(line_data[col][TRANSM] * 255.0);
  654. row_ptr[j + 3] = color & himask;
  655. row_ptr[j + 3] |= color >> opts.OutputQuality;
  656. }
  657. }
  658. }
  659. break;
  660. case 8:
  661. if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) == PNG_COLOR_TYPE_GRAY)
  662. {
  663. for (col = j = 0; col < handle->width; col++, j += png_stride)
  664. {
  665. row_ptr[j] = (png_byte)floor((line_data[col][RED]*0.30 +
  666. line_data[col][GREEN]*0.59 +
  667. line_data[col][BLUE]*0.11) * 255.0);
  668. /* Handle Alpha here if needed */
  669. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  670. {
  671. row_ptr[j+1] = (png_byte)(255-floor(line_data[col][TRANSM]*255.0));
  672. }
  673. }
  674. }
  675. else
  676. {
  677. for (col = j = 0; col < handle->width; col++, j += png_stride)
  678. {
  679. row_ptr[j] = (png_byte)floor(line_data[col][RED] * 255.0);
  680. row_ptr[j + 1] = (png_byte)floor(line_data[col][GREEN] * 255.0);
  681. row_ptr[j + 2] = (png_byte)floor(line_data[col][BLUE] * 255.0);
  682. /* Handle Alpha here if needed */
  683. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  684. {
  685. row_ptr[j+3] = (png_byte)(255-floor(line_data[col][TRANSM]*255.0));
  686. }
  687. }
  688. }
  689. break;
  690. case 16:
  691. if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) == PNG_COLOR_TYPE_GRAY)
  692. {
  693. for (col = j = 0; col < handle->width; col++, j += png_stride)
  694. {
  695. color = (int)floor((line_data[col][RED]*0.30 +
  696. line_data[col][GREEN]*0.59 +
  697. line_data[col][BLUE]*0.11) * 65535.0);
  698. row_ptr[j] = color >> 8;
  699. row_ptr[j + 1] = color & 0xFF;
  700. /* Handle Alpha here if needed */
  701. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  702. {
  703. color = 65535 - (int)floor(line_data[col][TRANSM] * 65535.0);
  704. row_ptr[j + 2] = color >> 8;
  705. row_ptr[j + 3] = color & 0xFF;
  706. }
  707. }
  708. }
  709. else
  710. {
  711. for (col = j = 0; col < handle->width; col++, j += png_stride)
  712. {
  713. color = (int)floor(line_data[col][RED] * 65535.0);
  714. row_ptr[j] = color >> 8;
  715. row_ptr[j + 1] = color & 0xFF;
  716. color = (int)floor(line_data[col][GREEN] * 65535.0);
  717. row_ptr[j + 2] = color >> 8;
  718. row_ptr[j + 3] = color & 0xFF;
  719. color = (int)floor(line_data[col][BLUE] * 65535.0);
  720. row_ptr[j + 4] = color >> 8;
  721. row_ptr[j + 5] = color & 0xFF;
  722. /* Handle Alpha here if needed */
  723. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  724. {
  725. color = 65535 - (int)floor(line_data[col][TRANSM] * 65535.0);
  726. row_ptr[j + 6] = color >> 8;
  727. row_ptr[j + 7] = color & 0xFF;
  728. }
  729. }
  730. }
  731. break;
  732. default: /* OutputQuality 9 - 15 */
  733. /* Handle shifting for arbitrary output bit depth */
  734. himask = 0xFF ^ ((1 << (16 - opts.OutputQuality)) - 1);
  735. if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) == PNG_COLOR_TYPE_GRAY)
  736. {
  737. for (col = j = 0; col < handle->width; col++, j += png_stride)
  738. {
  739. color = (int)floor((line_data[col][RED]*0.30 +
  740. line_data[col][GREEN]*0.59 +
  741. line_data[col][BLUE]*0.11) * 65535.0);
  742. row_ptr[j] = color >> 8;
  743. row_ptr[j + 1] = color & himask;
  744. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  745. {
  746. color = 65535 - (int)floor(line_data[col][TRANSM] * 65535.0);
  747. row_ptr[j + 2] = color >> 8;
  748. row_ptr[j + 3] = color & himask;
  749. row_ptr[j + 3] |= color >> opts.OutputQuality;
  750. }
  751. }
  752. }
  753. else
  754. {
  755. for (col = j = 0; col < handle->width; col++, j += png_stride)
  756. {
  757. color = (int)floor(line_data[col][RED] * 65535.0);
  758. row_ptr[j] = color >> 8;
  759. row_ptr[j + 1] = color & himask;
  760. color = (int)floor(line_data[col][GREEN] * 65535.0);
  761. row_ptr[j + 2] = color >> 8;
  762. row_ptr[j + 3] = color & himask;
  763. color = (int)floor(line_data[col][BLUE] * 65535.0);
  764. row_ptr[j + 4] = color >> 8;
  765. row_ptr[j + 5] = color & himask;
  766. /* Handle Alpha here if needed */
  767. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  768. {
  769. color = 65535 - (int)floor(line_data[col][TRANSM] * 65535.0);
  770. row_ptr[j + 6] = color >> 8;
  771. row_ptr[j + 7] = color & himask;
  772. row_ptr[j + 7] |= color >> opts.OutputQuality;
  773. }
  774. }
  775. }
  776. }
  777. if (setjmp(png_ptr->jmpbuf))
  778. {
  779. /* If we get here, we had a problem writing the file */
  780. fclose(handle->file);
  781. handle->file = NULL;
  782. Error("Error writing PNG output data to %s.\n", handle->file);
  783. }
  784. /* Write out a scanline */
  785. png_write_row(png_ptr, row_ptr);
  786. /* Close and reopen file (if not stdout) for integrity in case we crash */
  787. if (handle->buffer_size == 0 && !(opts.Options & TO_STDOUT))
  788. {
  789. #ifndef PNG_WRITE_FLUSH
  790. fflush(handle->file);
  791. #endif
  792. handle->file = freopen(handle->filename, APPEND_BINFILE_STRING, handle->file);
  793. }
  794. }
  795. /*****************************************************************************
  796. *
  797. * FUNCTION : Read_Png_Line
  798. *
  799. * ARGUMENTS : FILE_HANDLE *handle; COLOUR *line_data; int *line_number;
  800. *
  801. * MODIFIED ARGS : none
  802. *
  803. * RETURN VALUE : 1 if no error exit
  804. *
  805. * AUTHOR : Andreas Dilger
  806. *
  807. * DESCRIPTION
  808. *
  809. * Read a line of PNG data
  810. *
  811. * CHANGES
  812. *
  813. * Updated for POV-Ray 3.X - [TIW]
  814. * Updated to handle interrupted file resuming Sept 1995 - [AED]
  815. * Updated to support grayscale and alpha together, Oct 1995 - [AED]
  816. *
  817. ******************************************************************************/
  818. static int Read_Png_Line(FILE_HANDLE *handle, COLOUR *line_data, int *line_number)
  819. {
  820. register int col, j, step;
  821. if (setjmp(o_png_ptr->jmpbuf))
  822. {
  823. /* If we get here, we had a problem reading the file, which probably
  824. * means that we have read all the available data, rather than a real
  825. * error, but there is no sure way to know.
  826. */
  827. Status_Info("\n");
  828. return(0);
  829. }
  830. if (setjmp(png_ptr->jmpbuf))
  831. {
  832. /* If we get here, we had a problem writing the new file */
  833. Status_Info("\n");
  834. fclose(handle->file);
  835. handle->file = NULL;
  836. fclose(tmp_fp);
  837. tmp_fp = NULL;
  838. if (DELETE_FILE(handle->filename) != DELETE_FILE_ERR)
  839. {
  840. RENAME_FILE(tmp_fname,handle->filename); /* Move original file back */
  841. }
  842. return(-1);
  843. }
  844. /* Read in another row if available */
  845. png_read_row(o_png_ptr, row_ptr, NULL);
  846. /* We won't get here if there was a read error */
  847. png_write_row(png_ptr, row_ptr);
  848. if (*line_number % 32 == 31)
  849. Status_Info(".");
  850. /*
  851. * We must copy all the values because PNG supplies RGBRGB, but POV-Ray
  852. * stores RGB components in separate arrays. Note that since we have
  853. * already written the data out to the temporary file, we only need to
  854. * use the top 8 bits for the line_data info as it is only used for
  855. * potential screen output.
  856. */
  857. /* How many bytes in a sample */
  858. step = (info_ptr->bit_depth <= 8) ? 1 : 2;
  859. if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) == PNG_COLOR_TYPE_GRAY)
  860. {
  861. for (col = j = 0; col < handle->width; col++, j += png_stride)
  862. {
  863. line_data[col][RED] = (DBL)row_ptr[j] / 255.0;
  864. line_data[col][GREEN] = (DBL)row_ptr[j] / 255.0;
  865. line_data[col][BLUE] = (DBL)row_ptr[j] / 255.0;
  866. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  867. {
  868. line_data[col][TRANSM] = (DBL)(255 - row_ptr[j + step]) / 255.0;
  869. }
  870. }
  871. }
  872. else
  873. {
  874. for (col = j = 0; col < handle->width; col++, j += png_stride)
  875. {
  876. line_data[col][RED] = (DBL)row_ptr[j] / 255.0;
  877. line_data[col][GREEN] = (DBL)row_ptr[j + step] / 255.0;
  878. line_data[col][BLUE] = (DBL)row_ptr[j + 2*step] / 255.0;
  879. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  880. {
  881. line_data[col][TRANSM] = (DBL)(255 - row_ptr[j + 3*step]) / 255.0;
  882. }
  883. }
  884. }
  885. /* Note that line_number is the line number of the completed row, not
  886. * the next row!
  887. */
  888. #if defined(PNG_READ_oFFS_SUPPORTED)
  889. *line_number = info_ptr->y_offset + png_ptr->row_number - 1;
  890. #else
  891. *line_number = png_ptr->row_number - 1;
  892. #endif
  893. return(1);
  894. }
  895. /*****************************************************************************
  896. *
  897. * FUNCTION : Close_Png_File
  898. *
  899. * ARGUMENTS : FILE_HANDLE *handle
  900. *
  901. * MODIFIED ARGS : handle
  902. *
  903. * RETURN VALUE : none
  904. *
  905. * AUTHOR : Andreas Dilger
  906. *
  907. * DESCRIPTION
  908. *
  909. * Write any chunks coming after image (eg comments), and free all the
  910. * memory associated with the PNG IO streams. Will output some rendering
  911. * stats and info into tEXt chunks if POV_COMMENTS is #defined, and will
  912. * also record the rendering time if CTIME is #defined.
  913. *
  914. * CHANGES
  915. *
  916. * Updated for POV-Ray 3.X - [TIW]
  917. *
  918. ******************************************************************************/
  919. static void Close_Png_File(FILE_HANDLE *handle)
  920. {
  921. #ifdef POV_COMMENTS
  922. int n, index = - 1;
  923. png_text *text_ptr = NULL;
  924. char allocated[NTEXT]; /* Boolean array if text is MALLOCed */
  925. char bigtext[MAXTEXT]; /* Large temporary string to print into */
  926. # ifdef CAMERA
  927. CAMERA *Camera = Frame.Camera;
  928. # endif
  929. #ifdef CTIME
  930. char months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  931. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  932. #endif
  933. #endif
  934. /* Why are we here? */
  935. if (handle->file == NULL)
  936. {
  937. return;
  938. }
  939. if (handle->mode == WRITE_MODE || handle->mode == APPEND_MODE)
  940. {
  941. if (png_ptr != NULL)
  942. {
  943. if (setjmp(png_ptr->jmpbuf))
  944. {
  945. /* If we get here, we had a problem writing the file */
  946. png_destroy_write_struct(&png_ptr, &info_ptr);
  947. if (row_ptr != NULL)
  948. {
  949. POV_FREE(row_ptr);
  950. row_ptr = NULL;
  951. }
  952. if (handle->file != NULL)
  953. {
  954. fclose (handle->file);
  955. handle->file = NULL;
  956. }
  957. if (handle->buffer != NULL)
  958. {
  959. POV_FREE(handle->buffer);
  960. handle->buffer = NULL;
  961. }
  962. Error("Error writing PNG file.");
  963. }
  964. if(png_ptr->row_number < png_ptr->num_rows)
  965. {
  966. /* finished prematurely - trick into thinking done*/
  967. png_ptr->num_rows = png_ptr->row_number;
  968. png_write_finish_row(png_ptr);
  969. }
  970. #ifdef POV_COMMENTS /* temporarily skip comment writing code */
  971. if (info_ptr != NULL)
  972. {
  973. #if defined(PNG_WRITE_tIME_SUPPORTED)
  974. png_convert_from_time_t(&info_ptr->mod_time, tstart);
  975. info_ptr->valid = PNG_INFO_tIME;
  976. #endif /* PNG_WRITE_tIME_SUPPORTED */
  977. #if defined(PNG_WRITE_tEXt_SUPPORTED)
  978. text_ptr = (png_text *)POV_MALLOC(NTEXT*sizeof(png_text), "PNG comment structure");
  979. /* Init allocation flags. */
  980. for (n = 0; n < NTEXT; n++)
  981. {
  982. allocated[n] = FALSE;
  983. text_ptr[n].compression = - 1;
  984. }
  985. #ifdef TRACER
  986. text_ptr[++index].key = "Author";
  987. text_ptr[index].text = TRACER;
  988. text_ptr[index].text_length = strlen(text_ptr[index].text);
  989. #endif
  990. #ifdef COPYRIGHT
  991. text_ptr[++index].key = "Copyright";
  992. /* 0xA9 is the ISO-8859-1 (used in PNG tEXt) copyright character */
  993. sprintf(bigtext, "Copyright %c %d %s", 0xA9, info_ptr->mod_time.year,
  994. COPYRIGHT);
  995. text_ptr[index].text_length = strlen(bigtext);
  996. text_ptr[index].text = (char *)POV_MALLOC(text_ptr[index].text_length + 1, "PNG comment");
  997. strcpy(text_ptr[index].text, bigtext);
  998. allocated[index] = TRUE;
  999. if (text_ptr[index].text_length > 200) /* Compress if long copyright */
  1000. text_ptr[index].compression = 0;
  1001. #endif
  1002. #ifdef CTIME
  1003. /* Print the image "creation" time in RFC 1123 format */
  1004. text_ptr[++index].key = "Creation Time";
  1005. sprintf(bigtext, "%02d %3s %4d %02d:%02d:%02d GMT",
  1006. info_ptr->mod_time.day, months[info_ptr->mod_time.month],
  1007. info_ptr->mod_time.year, info_ptr->mod_time.hour,
  1008. info_ptr->mod_time.minute, info_ptr->mod_time.second);
  1009. text_ptr[index].text_length = strlen(bigtext);
  1010. text_ptr[index].text = (char *)POV_MALLOC(text_ptr[index].text_length + 1, "PNG comment");
  1011. strcpy(text_ptr[index].text, bigtext);
  1012. allocated[index] = TRUE;
  1013. #endif
  1014. text_ptr[++index].key = "Source";
  1015. sprintf(bigtext, "Persistence of Vision(tm) Ray Tracer v%s%s",
  1016. POV_RAY_VERSION, COMPILER_VER);
  1017. text_ptr[index].text_length = strlen(bigtext);
  1018. text_ptr[index].text = (char *)POV_MALLOC(text_ptr[index].text_length + 1, "PNG comment");
  1019. strcpy(text_ptr[index].text, bigtext);
  1020. allocated[index] = TRUE;
  1021. if (!(opts.Options & FROM_STDIN))
  1022. {
  1023. text_ptr[++index].key = "Input File";
  1024. text_ptr[index].text = opts.Input_File_Name;
  1025. text_ptr[index].text_length = strlen(text_ptr[index].text);
  1026. }
  1027. #ifdef CAMERA
  1028. text_ptr[++index].key = "POV Camera";
  1029. sprintf(bigtext, "Location: %7g %7g %7g\n"
  1030. " Direction: %7g %7g %7g\n"
  1031. " Up: %7g %7g %7g\n"
  1032. " Right: %7g %7g %7g\n"
  1033. " Sky: %7g %7g %7g",
  1034. Camera->Location[X], Camera->Location[Y], Camera->Location[Z],
  1035. Camera->Direction[X], Camera->Direction[Y], Camera->Direction[Z],
  1036. Camera->Up[X], Camera->Up[Y], Camera->Up[Z],
  1037. Camera->Right[X], Camera->Right[Y], Camera->Right[Z],
  1038. Camera->Sky[X], Camera->Sky[Y], Camera->Sky[Z]);
  1039. text_ptr[index].text_length = strlen(bigtext);
  1040. text_ptr[index].text = (char *)POV_MALLOC(text_ptr[index].text_length + 1, "PNG comment");
  1041. strcpy(text_ptr[index].text, bigtext);
  1042. allocated[index] = TRUE;
  1043. #endif
  1044. if (opts.FrameSeq.Clock_Value != 0)
  1045. {
  1046. text_ptr[++index].key = "POV Clock";
  1047. sprintf(bigtext, "%g", opts.FrameSeq.Clock_Value);
  1048. text_ptr[index].text_length = strlen(bigtext);
  1049. text_ptr[index].text = (char *)POV_MALLOC(text_ptr[index].text_length + 1, "PNG comment");
  1050. strcpy(text_ptr[index].text, bigtext);
  1051. allocated[index] = TRUE;
  1052. }
  1053. if (opts.Quality != 9)
  1054. {
  1055. text_ptr[++index].key = "Rendering Quality";
  1056. sprintf(bigtext, "%d", opts.Quality);
  1057. text_ptr[index].text_length = strlen(bigtext);
  1058. text_ptr[index].text = (char *)POV_MALLOC(text_ptr[index].text_length + 1, "PNG comment");
  1059. strcpy(text_ptr[index].text, bigtext);
  1060. allocated[index] = TRUE;
  1061. }
  1062. text_ptr[++index].key = "Rendering Time";
  1063. sprintf(bigtext, "%g s", trender);
  1064. text_ptr[index].text_length = strlen(bigtext);
  1065. text_ptr[index].text = (char *)POV_MALLOC(text_ptr[index].text_length + 1, "PNG comment");
  1066. strcpy(text_ptr[index].text, bigtext);
  1067. allocated[index] = TRUE;
  1068. info_ptr->num_text = index + 1;
  1069. info_ptr->max_text = NTEXT;
  1070. info_ptr->text = text_ptr;
  1071. #endif /* PNG_WRITE_tEXt_SUPPORTED */
  1072. }
  1073. #endif /* POV_COMMENTS */
  1074. png_write_end(png_ptr, info_ptr);
  1075. png_destroy_write_struct(&png_ptr, &info_ptr);
  1076. #ifdef POV_COMMENTS
  1077. if (text_ptr != NULL)
  1078. {
  1079. for (n = 0; n <= index; n++)
  1080. {
  1081. if (allocated[n])
  1082. {
  1083. POV_FREE(text_ptr[n].text);
  1084. }
  1085. }
  1086. POV_FREE(text_ptr);
  1087. text_ptr = NULL;
  1088. }
  1089. #endif /* POV_COMMENTS */
  1090. }
  1091. if (row_ptr != NULL)
  1092. {
  1093. POV_FREE(row_ptr);
  1094. row_ptr = NULL;
  1095. }
  1096. if (handle->file != NULL && !(opts.Options & TO_STDOUT))
  1097. {
  1098. fclose (handle->file);
  1099. handle->file = NULL;
  1100. }
  1101. if (handle->buffer != NULL)
  1102. {
  1103. POV_FREE(handle->buffer);
  1104. handle->buffer = NULL;
  1105. }
  1106. }
  1107. else /* READ_MODE */
  1108. {
  1109. if (o_png_ptr != NULL)
  1110. {
  1111. png_destroy_read_struct(&o_png_ptr, (png_infopp)NULL, (png_infopp)NULL);
  1112. }
  1113. }
  1114. }
  1115. /*****************************************************************************
  1116. *
  1117. * FUNCTION : Read_Png_Image
  1118. *
  1119. * ARGUMENTS : IMAGE *Image; char *name;
  1120. *
  1121. * MODIFIED ARGS : Image
  1122. *
  1123. * RETURN VALUE : none
  1124. *
  1125. * AUTHOR : Andreas Dilger
  1126. *
  1127. * DESCRIPTION
  1128. *
  1129. * Reads a PNG image into an RGB image buffer
  1130. *
  1131. * CHANGES
  1132. *
  1133. * Updated for POV-Ray 3.X - [TIW]
  1134. * Updated to allow grayscale and alpha together, Oct 1995 - [AED]
  1135. * Fixed palette size for grayscale images with bit-depth <= 8, Nov 1995 [AED]
  1136. * Changed how grayscale images > 8bpp are stored based on use, Nov 1995 [AED]
  1137. *
  1138. ******************************************************************************/
  1139. void Read_Png_Image(IMAGE *Image, char *name)
  1140. {
  1141. unsigned int width, height;
  1142. int row, col, j;
  1143. int stride;
  1144. FILE *filep;
  1145. IMAGE_LINE *line_data;
  1146. png_struct *r_png_ptr;
  1147. png_info *r_info_ptr;
  1148. png_byte **row_ptrs;
  1149. /* Start by trying to open the file */
  1150. if ((filep = Locate_File(name, READ_BINFILE_STRING, ".png", ".PNG",NULL,TRUE)) == NULL)
  1151. {
  1152. Error("Error opening PNG file.\n");
  1153. return; /* -hdf99- */
  1154. }
  1155. if ((r_png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  1156. (png_voidp)TRUE, png_pov_err, png_pov_warn)) == NULL ||
  1157. (r_info_ptr = png_create_info_struct(r_png_ptr)) == NULL)
  1158. {
  1159. Error("Error allocating PNG data structures");
  1160. return; /* -hdf99- */
  1161. }
  1162. if (setjmp(r_png_ptr->jmpbuf))
  1163. {
  1164. /* If we get here, we had a problem reading the file */
  1165. png_destroy_read_struct(&r_png_ptr, &r_info_ptr, (png_infopp)NULL);
  1166. Error("Error reading PNG image.");
  1167. return; /* -hdf99- */
  1168. }
  1169. /* set up the input control */
  1170. png_init_io(r_png_ptr, filep);
  1171. /* read the file information */
  1172. png_read_info(r_png_ptr, r_info_ptr);
  1173. width = r_info_ptr->width;
  1174. height = r_info_ptr->height;
  1175. Image->iwidth = width;
  1176. Image->iheight = height;
  1177. Image->width = (DBL)width;
  1178. Image->height = (DBL)height;
  1179. /* Allocate buffers for the image */
  1180. stride = 1;
  1181. if (r_info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1182. {
  1183. IMAGE_COLOUR *cmap;
  1184. png_color *png_cmap;
  1185. int cmap_len = r_info_ptr->num_palette;
  1186. int index;
  1187. Image->Colour_Map_Size = cmap_len;
  1188. cmap = (IMAGE_COLOUR *)POV_MALLOC(cmap_len*sizeof(IMAGE_COLOUR), "PNG image color map");
  1189. Image->Colour_Map = cmap;
  1190. png_cmap = r_info_ptr->palette;
  1191. for (index = 0; index < cmap_len; index++)
  1192. {
  1193. cmap[index].Red = png_cmap[index].red;
  1194. cmap[index].Green = png_cmap[index].green;
  1195. cmap[index].Blue = png_cmap[index].blue;
  1196. cmap[index].Filter = 0;
  1197. cmap[index].Transmit = 0;
  1198. }
  1199. if (r_info_ptr->valid & PNG_INFO_tRNS)
  1200. {
  1201. for (index = 0; index < r_info_ptr->num_trans; index++)
  1202. cmap[index].Transmit = 255 - r_info_ptr->trans[index];
  1203. }
  1204. Image->data.map_lines = (unsigned char **)
  1205. POV_MALLOC(height * sizeof(unsigned char *), "PNG image");
  1206. /* tell pnglib to expand data to 1 pixel/byte */
  1207. png_set_packing(r_png_ptr);
  1208. }
  1209. else if (r_info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  1210. r_info_ptr->bit_depth <= 8)
  1211. {
  1212. IMAGE_COLOUR *cmap;
  1213. int cmap_len;
  1214. int index;
  1215. Image->Colour_Map_Size = cmap_len = 1 << r_info_ptr->bit_depth;
  1216. cmap = (IMAGE_COLOUR *)POV_MALLOC(cmap_len*sizeof(IMAGE_COLOUR), "PNG image color map");
  1217. Image->Colour_Map = cmap;
  1218. for (index = 0; index < cmap_len; index++)
  1219. {
  1220. cmap[index].Red =
  1221. cmap[index].Green =
  1222. cmap[index].Blue = index;
  1223. cmap[index].Filter = 0;
  1224. cmap[index].Transmit = 0;
  1225. }
  1226. if (r_info_ptr->valid & PNG_INFO_tRNS)
  1227. {
  1228. for (index = 0; index < r_info_ptr->num_trans; index++)
  1229. cmap[index].Transmit = 255 - r_info_ptr->trans[index];
  1230. }
  1231. Image->data.map_lines = (unsigned char **)
  1232. POV_MALLOC(height * sizeof(unsigned char *), "PNG image");
  1233. /* tell pnglib to expand data to 1 pixel/byte */
  1234. png_set_packing(r_png_ptr);
  1235. }
  1236. else if (r_info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
  1237. r_info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  1238. {
  1239. Image->Colour_Map = NULL;
  1240. Image->data.rgb_lines = (IMAGE_LINE *)
  1241. POV_MALLOC(height * sizeof(IMAGE_LINE), "PNG image");
  1242. if (r_info_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1243. {
  1244. stride = 2;
  1245. }
  1246. else if (r_info_ptr->bit_depth <= 8) /* PNG_COLOR_TYPE_GRAY_ALPHA */
  1247. {
  1248. /* tell pnglib to expand data to 1 pixel/byte */
  1249. png_set_packing(r_png_ptr);
  1250. stride = 2;
  1251. }
  1252. else /* PNG_COLOR_TYPE_GRAY_ALPHA */
  1253. {
  1254. stride = 4;
  1255. }
  1256. }
  1257. else if (r_info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
  1258. r_info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  1259. {
  1260. Image->Colour_Map = NULL;
  1261. /* tell pnglib to strip 16 bit depth files down to 8 bits */
  1262. if (r_info_ptr->bit_depth > 8)
  1263. {
  1264. if (opts.Options & VERBOSE)
  1265. Warning(0.0,"\nConverting PNG image map to 8 bits/sample from higher bit depth.");
  1266. png_set_strip_16(r_png_ptr);
  1267. }
  1268. Image->data.rgb_lines = (IMAGE_LINE *)
  1269. POV_MALLOC(height * sizeof(IMAGE_LINE), "PNG image");
  1270. if (r_info_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1271. stride = 3;
  1272. else /* PNG_COLOR_TYPE_RGB_ALPHA */
  1273. stride = 4;
  1274. }
  1275. else /* Unknown PNG type */
  1276. {
  1277. Error("Unsupported color type %d in PNG image.\n", r_info_ptr->color_type);
  1278. }
  1279. /* Tell pnglib to handle the gamma conversion for you. Note that
  1280. * GammaFactor * DisplayFactor = assumed_gamma, so we are converting
  1281. * images into the "internal gamma" space of POV (rather than to a
  1282. * gamma of 1.0) to avoid doing gamma correction on image maps twice for
  1283. * those scene files which don't have a gamma of 1.0. For POV 3.0,
  1284. * we will only do input gamma conversion on those files which will be
  1285. * used as image maps, and the other types will load the raw pixel values.
  1286. */
  1287. #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_READ_gAMA_SUPPORTED)
  1288. if (r_info_ptr->valid & PNG_INFO_gAMA && (Image->Image_Type & IMAGE_FTYPE))
  1289. {
  1290. png_set_gamma(r_png_ptr, opts.GammaFactor*opts.DisplayGamma,
  1291. r_info_ptr->gamma);
  1292. }
  1293. #endif /* PNG_READ_GAMMA_SUPPORTED and PNG_READ_gAMA_SUPPORTED */
  1294. png_set_interlace_handling(r_png_ptr);
  1295. png_read_update_info(r_png_ptr, r_info_ptr);
  1296. /* Allocate row buffers for the input */
  1297. row_ptrs = (png_byte **)POV_MALLOC(height*sizeof(png_byte *), "PNG image");
  1298. for (row = 0; row < height; row++)
  1299. {
  1300. row_ptrs[row] = (png_byte *)POV_MALLOC(r_info_ptr->rowbytes, "PNG image line");
  1301. }
  1302. /* Read in the entire image */
  1303. png_read_image(r_png_ptr, row_ptrs);
  1304. /* We must copy all the values because PNG supplies RGBRGB, but POV-Ray
  1305. * stores RGB components in separate arrays
  1306. */
  1307. for (row = 0; row < height; row++)
  1308. {
  1309. if (Image->Colour_Map == NULL)
  1310. {
  1311. line_data = &Image->data.rgb_lines[row];
  1312. line_data->red = (unsigned char *)POV_MALLOC(width, "PNG image line");
  1313. line_data->green = (unsigned char *)POV_MALLOC(width, "PNG image line");
  1314. line_data->blue = (unsigned char *)POV_MALLOC(width, "PNG image line");
  1315. if (r_info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  1316. {
  1317. line_data->transm = (unsigned char *)POV_MALLOC(width,"PNG image line");
  1318. }
  1319. else
  1320. {
  1321. line_data->transm = NULL;
  1322. }
  1323. /* 8-bit grayscale image with a full alpha channel. Since the paletted
  1324. * images don't support different transparencies for each pixel, we
  1325. * have to make this a full-color image.
  1326. */
  1327. if (r_info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  1328. r_info_ptr->bit_depth <= 8)
  1329. {
  1330. for (col = j = 0; col < width; col ++, j += stride)
  1331. {
  1332. line_data->red[col] =
  1333. line_data->green[col] =
  1334. line_data->blue[col] = row_ptrs[row][j];
  1335. line_data->transm[col] = 255 - row_ptrs[row][j + 1];
  1336. }
  1337. }
  1338. /* For 16 bit PNG heightfields, we need to know if the map will be used
  1339. * for an image (in which case we want to store the values as grays, or
  1340. * if we want to use it as a heightfield (in which case we need to store
  1341. * it in the MSB-read, LSB-green format that POV uses to store 16-bit
  1342. * heightfields.
  1343. */
  1344. else if ((r_info_ptr->color_type & PNG_COLOR_MASK_COLOR) ==
  1345. PNG_COLOR_TYPE_GRAY)
  1346. {
  1347. if (Image->Image_Type & HF_FTYPE)
  1348. {
  1349. for (col = j = 0; col < width; col ++, j += stride)
  1350. {
  1351. int red = row_ptrs[row][j];
  1352. int green = row_ptrs[row][j + 1];
  1353. line_data->red[col] = red;
  1354. line_data->green[col] = green;
  1355. line_data->blue[col] = 0;
  1356. if (r_png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  1357. {
  1358. line_data->transm[col] = 255 - row_ptrs[row][j + 2];
  1359. }
  1360. }
  1361. }
  1362. else
  1363. {
  1364. for (col = j = 0; col < width; col ++, j += stride)
  1365. {
  1366. int red = row_ptrs[row][j];
  1367. line_data->red[col] = red;
  1368. line_data->green[col] = red;
  1369. line_data->blue[col] = red;
  1370. if (r_png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  1371. {
  1372. line_data->transm[col] = 255 - row_ptrs[row][j + 2];
  1373. }
  1374. }
  1375. }
  1376. }
  1377. else /* r_info_ptr->color_type & PNG_COLOR_MASK_COLOR */
  1378. {
  1379. for (col = j = 0; col < width; col ++, j += stride)
  1380. {
  1381. line_data->red[col] = row_ptrs[row][j];
  1382. line_data->green[col] = row_ptrs[row][j + 1];
  1383. line_data->blue[col] = row_ptrs[row][j + 2];
  1384. if (r_png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  1385. {
  1386. line_data->transm[col] = 255 - row_ptrs[row][j + 3];
  1387. }
  1388. }
  1389. }
  1390. POV_FREE(row_ptrs[row]);
  1391. }
  1392. else
  1393. {
  1394. Image->data.map_lines[row] = row_ptrs[row];
  1395. }
  1396. }
  1397. /* Clean up the rest of the PNG memory and such */
  1398. POV_FREE(row_ptrs);
  1399. /* read the rest of the file, getting any additional chunks in png_info */
  1400. png_read_end(r_png_ptr, r_info_ptr);
  1401. /* clean up after the read, and free any memory allocated */
  1402. png_destroy_read_struct(&r_png_ptr, &r_info_ptr, (png_infopp)NULL);
  1403. fclose(filep);
  1404. }