pngwutil.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029
  1. /* pngwutil.c - utilities to write a PNG file
  2. *
  3. * Last changed in libpng 1.6.15 [November 20, 2014]
  4. * Copyright (c) 1998-2014 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. */
  12. #include "pngpriv.h"
  13. #ifdef PNG_WRITE_SUPPORTED
  14. #ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
  15. /* Place a 32-bit number into a buffer in PNG byte order. We work
  16. * with unsigned numbers for convenience, although one supported
  17. * ancillary chunk uses signed (two's complement) numbers.
  18. */
  19. void PNGAPI
  20. png_save_uint_32(png_bytep buf, png_uint_32 i)
  21. {
  22. buf[0] = (png_byte)((i >> 24) & 0xff);
  23. buf[1] = (png_byte)((i >> 16) & 0xff);
  24. buf[2] = (png_byte)((i >> 8) & 0xff);
  25. buf[3] = (png_byte)(i & 0xff);
  26. }
  27. /* Place a 16-bit number into a buffer in PNG byte order.
  28. * The parameter is declared unsigned int, not png_uint_16,
  29. * just to avoid potential problems on pre-ANSI C compilers.
  30. */
  31. void PNGAPI
  32. png_save_uint_16(png_bytep buf, unsigned int i)
  33. {
  34. buf[0] = (png_byte)((i >> 8) & 0xff);
  35. buf[1] = (png_byte)(i & 0xff);
  36. }
  37. #endif
  38. /* Simple function to write the signature. If we have already written
  39. * the magic bytes of the signature, or more likely, the PNG stream is
  40. * being embedded into another stream and doesn't need its own signature,
  41. * we should call png_set_sig_bytes() to tell libpng how many of the
  42. * bytes have already been written.
  43. */
  44. void PNGAPI
  45. png_write_sig(png_structrp png_ptr)
  46. {
  47. png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  48. #ifdef PNG_IO_STATE_SUPPORTED
  49. /* Inform the I/O callback that the signature is being written */
  50. png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
  51. #endif
  52. /* Write the rest of the 8 byte signature */
  53. png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
  54. (png_size_t)(8 - png_ptr->sig_bytes));
  55. if (png_ptr->sig_bytes < 3)
  56. png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
  57. }
  58. /* Write the start of a PNG chunk. The type is the chunk type.
  59. * The total_length is the sum of the lengths of all the data you will be
  60. * passing in png_write_chunk_data().
  61. */
  62. static void
  63. png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name,
  64. png_uint_32 length)
  65. {
  66. png_byte buf[8];
  67. #if defined(PNG_DEBUG) && (PNG_DEBUG > 0)
  68. PNG_CSTRING_FROM_CHUNK(buf, chunk_name);
  69. png_debug2(0, "Writing %s chunk, length = %lu", buf, (unsigned long)length);
  70. #endif
  71. if (png_ptr == NULL)
  72. return;
  73. #ifdef PNG_IO_STATE_SUPPORTED
  74. /* Inform the I/O callback that the chunk header is being written.
  75. * PNG_IO_CHUNK_HDR requires a single I/O call.
  76. */
  77. png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
  78. #endif
  79. /* Write the length and the chunk name */
  80. png_save_uint_32(buf, length);
  81. png_save_uint_32(buf + 4, chunk_name);
  82. png_write_data(png_ptr, buf, 8);
  83. /* Put the chunk name into png_ptr->chunk_name */
  84. png_ptr->chunk_name = chunk_name;
  85. /* Reset the crc and run it over the chunk name */
  86. png_reset_crc(png_ptr);
  87. png_calculate_crc(png_ptr, buf + 4, 4);
  88. #ifdef PNG_IO_STATE_SUPPORTED
  89. /* Inform the I/O callback that chunk data will (possibly) be written.
  90. * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
  91. */
  92. png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
  93. #endif
  94. }
  95. void PNGAPI
  96. png_write_chunk_start(png_structrp png_ptr, png_const_bytep chunk_string,
  97. png_uint_32 length)
  98. {
  99. png_write_chunk_header(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), length);
  100. }
  101. /* Write the data of a PNG chunk started with png_write_chunk_header().
  102. * Note that multiple calls to this function are allowed, and that the
  103. * sum of the lengths from these calls *must* add up to the total_length
  104. * given to png_write_chunk_header().
  105. */
  106. void PNGAPI
  107. png_write_chunk_data(png_structrp png_ptr, png_const_bytep data,
  108. png_size_t length)
  109. {
  110. /* Write the data, and run the CRC over it */
  111. if (png_ptr == NULL)
  112. return;
  113. if (data != NULL && length > 0)
  114. {
  115. png_write_data(png_ptr, data, length);
  116. /* Update the CRC after writing the data,
  117. * in case the user I/O routine alters it.
  118. */
  119. png_calculate_crc(png_ptr, data, length);
  120. }
  121. }
  122. /* Finish a chunk started with png_write_chunk_header(). */
  123. void PNGAPI
  124. png_write_chunk_end(png_structrp png_ptr)
  125. {
  126. png_byte buf[4];
  127. if (png_ptr == NULL) return;
  128. #ifdef PNG_IO_STATE_SUPPORTED
  129. /* Inform the I/O callback that the chunk CRC is being written.
  130. * PNG_IO_CHUNK_CRC requires a single I/O function call.
  131. */
  132. png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
  133. #endif
  134. /* Write the crc in a single operation */
  135. png_save_uint_32(buf, png_ptr->crc);
  136. png_write_data(png_ptr, buf, (png_size_t)4);
  137. }
  138. /* Write a PNG chunk all at once. The type is an array of ASCII characters
  139. * representing the chunk name. The array must be at least 4 bytes in
  140. * length, and does not need to be null terminated. To be safe, pass the
  141. * pre-defined chunk names here, and if you need a new one, define it
  142. * where the others are defined. The length is the length of the data.
  143. * All the data must be present. If that is not possible, use the
  144. * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
  145. * functions instead.
  146. */
  147. static void
  148. png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name,
  149. png_const_bytep data, png_size_t length)
  150. {
  151. if (png_ptr == NULL)
  152. return;
  153. /* On 64 bit architectures 'length' may not fit in a png_uint_32. */
  154. if (length > PNG_UINT_31_MAX)
  155. png_error(png_ptr, "length exceeds PNG maximum");
  156. png_write_chunk_header(png_ptr, chunk_name, (png_uint_32)length);
  157. png_write_chunk_data(png_ptr, data, length);
  158. png_write_chunk_end(png_ptr);
  159. }
  160. /* This is the API that calls the internal function above. */
  161. void PNGAPI
  162. png_write_chunk(png_structrp png_ptr, png_const_bytep chunk_string,
  163. png_const_bytep data, png_size_t length)
  164. {
  165. png_write_complete_chunk(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), data,
  166. length);
  167. }
  168. /* This is used below to find the size of an image to pass to png_deflate_claim,
  169. * so it only needs to be accurate if the size is less than 16384 bytes (the
  170. * point at which a lower LZ window size can be used.)
  171. */
  172. static png_alloc_size_t
  173. png_image_size(png_structrp png_ptr)
  174. {
  175. /* Only return sizes up to the maximum of a png_uint_32; do this by limiting
  176. * the width and height used to 15 bits.
  177. */
  178. png_uint_32 h = png_ptr->height;
  179. if (png_ptr->rowbytes < 32768 && h < 32768)
  180. {
  181. if (png_ptr->interlaced != 0)
  182. {
  183. /* Interlacing makes the image larger because of the replication of
  184. * both the filter byte and the padding to a byte boundary.
  185. */
  186. png_uint_32 w = png_ptr->width;
  187. unsigned int pd = png_ptr->pixel_depth;
  188. png_alloc_size_t cb_base;
  189. int pass;
  190. for (cb_base=0, pass=0; pass<=6; ++pass)
  191. {
  192. png_uint_32 pw = PNG_PASS_COLS(w, pass);
  193. if (pw > 0)
  194. cb_base += (PNG_ROWBYTES(pd, pw)+1) * PNG_PASS_ROWS(h, pass);
  195. }
  196. return cb_base;
  197. }
  198. else
  199. return (png_ptr->rowbytes+1) * h;
  200. }
  201. else
  202. return 0xffffffffU;
  203. }
  204. #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
  205. /* This is the code to hack the first two bytes of the deflate stream (the
  206. * deflate header) to correct the windowBits value to match the actual data
  207. * size. Note that the second argument is the *uncompressed* size but the
  208. * first argument is the *compressed* data (and it must be deflate
  209. * compressed.)
  210. */
  211. static void
  212. optimize_cmf(png_bytep data, png_alloc_size_t data_size)
  213. {
  214. /* Optimize the CMF field in the zlib stream. The resultant zlib stream is
  215. * still compliant to the stream specification.
  216. */
  217. if (data_size <= 16384) /* else windowBits must be 15 */
  218. {
  219. unsigned int z_cmf = data[0]; /* zlib compression method and flags */
  220. if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
  221. {
  222. unsigned int z_cinfo;
  223. unsigned int half_z_window_size;
  224. z_cinfo = z_cmf >> 4;
  225. half_z_window_size = 1U << (z_cinfo + 7);
  226. if (data_size <= half_z_window_size) /* else no change */
  227. {
  228. unsigned int tmp;
  229. do
  230. {
  231. half_z_window_size >>= 1;
  232. --z_cinfo;
  233. }
  234. while (z_cinfo > 0 && data_size <= half_z_window_size);
  235. z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
  236. data[0] = (png_byte)z_cmf;
  237. tmp = data[1] & 0xe0;
  238. tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
  239. data[1] = (png_byte)tmp;
  240. }
  241. }
  242. }
  243. }
  244. #endif /* WRITE_OPTIMIZE_CMF */
  245. /* Initialize the compressor for the appropriate type of compression. */
  246. static int
  247. png_deflate_claim(png_structrp png_ptr, png_uint_32 owner,
  248. png_alloc_size_t data_size)
  249. {
  250. if (png_ptr->zowner != 0)
  251. {
  252. #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
  253. char msg[64];
  254. PNG_STRING_FROM_CHUNK(msg, owner);
  255. msg[4] = ':';
  256. msg[5] = ' ';
  257. PNG_STRING_FROM_CHUNK(msg+6, png_ptr->zowner);
  258. /* So the message that results is "<chunk> using zstream"; this is an
  259. * internal error, but is very useful for debugging. i18n requirements
  260. * are minimal.
  261. */
  262. (void)png_safecat(msg, (sizeof msg), 10, " using zstream");
  263. #endif
  264. #if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
  265. png_warning(png_ptr, msg);
  266. /* Attempt sane error recovery */
  267. if (png_ptr->zowner == png_IDAT) /* don't steal from IDAT */
  268. {
  269. png_ptr->zstream.msg = PNGZ_MSG_CAST("in use by IDAT");
  270. return Z_STREAM_ERROR;
  271. }
  272. png_ptr->zowner = 0;
  273. #else
  274. png_error(png_ptr, msg);
  275. #endif
  276. }
  277. {
  278. int level = png_ptr->zlib_level;
  279. int method = png_ptr->zlib_method;
  280. int windowBits = png_ptr->zlib_window_bits;
  281. int memLevel = png_ptr->zlib_mem_level;
  282. int strategy; /* set below */
  283. int ret; /* zlib return code */
  284. if (owner == png_IDAT)
  285. {
  286. if ((png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY) != 0)
  287. strategy = png_ptr->zlib_strategy;
  288. else if (png_ptr->do_filter != PNG_FILTER_NONE)
  289. strategy = PNG_Z_DEFAULT_STRATEGY;
  290. else
  291. strategy = PNG_Z_DEFAULT_NOFILTER_STRATEGY;
  292. }
  293. else
  294. {
  295. #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
  296. level = png_ptr->zlib_text_level;
  297. method = png_ptr->zlib_text_method;
  298. windowBits = png_ptr->zlib_text_window_bits;
  299. memLevel = png_ptr->zlib_text_mem_level;
  300. strategy = png_ptr->zlib_text_strategy;
  301. #else
  302. /* If customization is not supported the values all come from the
  303. * IDAT values except for the strategy, which is fixed to the
  304. * default. (This is the pre-1.6.0 behavior too, although it was
  305. * implemented in a very different way.)
  306. */
  307. strategy = Z_DEFAULT_STRATEGY;
  308. #endif
  309. }
  310. /* Adjust 'windowBits' down if larger than 'data_size'; to stop this
  311. * happening just pass 32768 as the data_size parameter. Notice that zlib
  312. * requires an extra 262 bytes in the window in addition to the data to be
  313. * able to see the whole of the data, so if data_size+262 takes us to the
  314. * next windowBits size we need to fix up the value later. (Because even
  315. * though deflate needs the extra window, inflate does not!)
  316. */
  317. if (data_size <= 16384)
  318. {
  319. /* IMPLEMENTATION NOTE: this 'half_window_size' stuff is only here to
  320. * work round a Microsoft Visual C misbehavior which, contrary to C-90,
  321. * widens the result of the following shift to 64-bits if (and,
  322. * apparently, only if) it is used in a test.
  323. */
  324. unsigned int half_window_size = 1U << (windowBits-1);
  325. while (data_size + 262 <= half_window_size)
  326. {
  327. half_window_size >>= 1;
  328. --windowBits;
  329. }
  330. }
  331. /* Check against the previous initialized values, if any. */
  332. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0 &&
  333. (png_ptr->zlib_set_level != level ||
  334. png_ptr->zlib_set_method != method ||
  335. png_ptr->zlib_set_window_bits != windowBits ||
  336. png_ptr->zlib_set_mem_level != memLevel ||
  337. png_ptr->zlib_set_strategy != strategy))
  338. {
  339. if (deflateEnd(&png_ptr->zstream) != Z_OK)
  340. png_warning(png_ptr, "deflateEnd failed (ignored)");
  341. png_ptr->flags &= ~PNG_FLAG_ZSTREAM_INITIALIZED;
  342. }
  343. /* For safety clear out the input and output pointers (currently zlib
  344. * doesn't use them on Init, but it might in the future).
  345. */
  346. png_ptr->zstream.next_in = NULL;
  347. png_ptr->zstream.avail_in = 0;
  348. png_ptr->zstream.next_out = NULL;
  349. png_ptr->zstream.avail_out = 0;
  350. /* Now initialize if required, setting the new parameters, otherwise just
  351. * to a simple reset to the previous parameters.
  352. */
  353. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
  354. ret = deflateReset(&png_ptr->zstream);
  355. else
  356. {
  357. ret = deflateInit2(&png_ptr->zstream, level, method, windowBits,
  358. memLevel, strategy);
  359. if (ret == Z_OK)
  360. png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
  361. }
  362. /* The return code is from either deflateReset or deflateInit2; they have
  363. * pretty much the same set of error codes.
  364. */
  365. if (ret == Z_OK)
  366. png_ptr->zowner = owner;
  367. else
  368. png_zstream_error(png_ptr, ret);
  369. return ret;
  370. }
  371. }
  372. /* Clean up (or trim) a linked list of compression buffers. */
  373. void /* PRIVATE */
  374. png_free_buffer_list(png_structrp png_ptr, png_compression_bufferp *listp)
  375. {
  376. png_compression_bufferp list = *listp;
  377. if (list != NULL)
  378. {
  379. *listp = NULL;
  380. do
  381. {
  382. png_compression_bufferp next = list->next;
  383. png_free(png_ptr, list);
  384. list = next;
  385. }
  386. while (list != NULL);
  387. }
  388. }
  389. #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
  390. /* This pair of functions encapsulates the operation of (a) compressing a
  391. * text string, and (b) issuing it later as a series of chunk data writes.
  392. * The compression_state structure is shared context for these functions
  393. * set up by the caller to allow access to the relevant local variables.
  394. *
  395. * compression_buffer (new in 1.6.0) is just a linked list of zbuffer_size
  396. * temporary buffers. From 1.6.0 it is retained in png_struct so that it will
  397. * be correctly freed in the event of a write error (previous implementations
  398. * just leaked memory.)
  399. */
  400. typedef struct
  401. {
  402. png_const_bytep input; /* The uncompressed input data */
  403. png_alloc_size_t input_len; /* Its length */
  404. png_uint_32 output_len; /* Final compressed length */
  405. png_byte output[1024]; /* First block of output */
  406. } compression_state;
  407. static void
  408. png_text_compress_init(compression_state *comp, png_const_bytep input,
  409. png_alloc_size_t input_len)
  410. {
  411. comp->input = input;
  412. comp->input_len = input_len;
  413. comp->output_len = 0;
  414. }
  415. /* Compress the data in the compression state input */
  416. static int
  417. png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name,
  418. compression_state *comp, png_uint_32 prefix_len)
  419. {
  420. int ret;
  421. /* To find the length of the output it is necessary to first compress the
  422. * input. The result is buffered rather than using the two-pass algorithm
  423. * that is used on the inflate side; deflate is assumed to be slower and a
  424. * PNG writer is assumed to have more memory available than a PNG reader.
  425. *
  426. * IMPLEMENTATION NOTE: the zlib API deflateBound() can be used to find an
  427. * upper limit on the output size, but it is always bigger than the input
  428. * size so it is likely to be more efficient to use this linked-list
  429. * approach.
  430. */
  431. ret = png_deflate_claim(png_ptr, chunk_name, comp->input_len);
  432. if (ret != Z_OK)
  433. return ret;
  434. /* Set up the compression buffers, we need a loop here to avoid overflowing a
  435. * uInt. Use ZLIB_IO_MAX to limit the input. The output is always limited
  436. * by the output buffer size, so there is no need to check that. Since this
  437. * is ANSI-C we know that an 'int', hence a uInt, is always at least 16 bits
  438. * in size.
  439. */
  440. {
  441. png_compression_bufferp *end = &png_ptr->zbuffer_list;
  442. png_alloc_size_t input_len = comp->input_len; /* may be zero! */
  443. png_uint_32 output_len;
  444. /* zlib updates these for us: */
  445. png_ptr->zstream.next_in = PNGZ_INPUT_CAST(comp->input);
  446. png_ptr->zstream.avail_in = 0; /* Set below */
  447. png_ptr->zstream.next_out = comp->output;
  448. png_ptr->zstream.avail_out = (sizeof comp->output);
  449. output_len = png_ptr->zstream.avail_out;
  450. do
  451. {
  452. uInt avail_in = ZLIB_IO_MAX;
  453. if (avail_in > input_len)
  454. avail_in = (uInt)input_len;
  455. input_len -= avail_in;
  456. png_ptr->zstream.avail_in = avail_in;
  457. if (png_ptr->zstream.avail_out == 0)
  458. {
  459. png_compression_buffer *next;
  460. /* Chunk data is limited to 2^31 bytes in length, so the prefix
  461. * length must be counted here.
  462. */
  463. if (output_len + prefix_len > PNG_UINT_31_MAX)
  464. {
  465. ret = Z_MEM_ERROR;
  466. break;
  467. }
  468. /* Need a new (malloc'ed) buffer, but there may be one present
  469. * already.
  470. */
  471. next = *end;
  472. if (next == NULL)
  473. {
  474. next = png_voidcast(png_compression_bufferp, png_malloc_base
  475. (png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr)));
  476. if (next == NULL)
  477. {
  478. ret = Z_MEM_ERROR;
  479. break;
  480. }
  481. /* Link in this buffer (so that it will be freed later) */
  482. next->next = NULL;
  483. *end = next;
  484. }
  485. png_ptr->zstream.next_out = next->output;
  486. png_ptr->zstream.avail_out = png_ptr->zbuffer_size;
  487. output_len += png_ptr->zstream.avail_out;
  488. /* Move 'end' to the next buffer pointer. */
  489. end = &next->next;
  490. }
  491. /* Compress the data */
  492. ret = deflate(&png_ptr->zstream,
  493. input_len > 0 ? Z_NO_FLUSH : Z_FINISH);
  494. /* Claw back input data that was not consumed (because avail_in is
  495. * reset above every time round the loop).
  496. */
  497. input_len += png_ptr->zstream.avail_in;
  498. png_ptr->zstream.avail_in = 0; /* safety */
  499. }
  500. while (ret == Z_OK);
  501. /* There may be some space left in the last output buffer. This needs to
  502. * be subtracted from output_len.
  503. */
  504. output_len -= png_ptr->zstream.avail_out;
  505. png_ptr->zstream.avail_out = 0; /* safety */
  506. comp->output_len = output_len;
  507. /* Now double check the output length, put in a custom message if it is
  508. * too long. Otherwise ensure the z_stream::msg pointer is set to
  509. * something.
  510. */
  511. if (output_len + prefix_len >= PNG_UINT_31_MAX)
  512. {
  513. png_ptr->zstream.msg = PNGZ_MSG_CAST("compressed data too long");
  514. ret = Z_MEM_ERROR;
  515. }
  516. else
  517. png_zstream_error(png_ptr, ret);
  518. /* Reset zlib for another zTXt/iTXt or image data */
  519. png_ptr->zowner = 0;
  520. /* The only success case is Z_STREAM_END, input_len must be 0; if not this
  521. * is an internal error.
  522. */
  523. if (ret == Z_STREAM_END && input_len == 0)
  524. {
  525. #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
  526. /* Fix up the deflate header, if required */
  527. optimize_cmf(comp->output, comp->input_len);
  528. #endif
  529. /* But Z_OK is returned, not Z_STREAM_END; this allows the claim
  530. * function above to return Z_STREAM_END on an error (though it never
  531. * does in the current versions of zlib.)
  532. */
  533. return Z_OK;
  534. }
  535. else
  536. return ret;
  537. }
  538. }
  539. /* Ship the compressed text out via chunk writes */
  540. static void
  541. png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp)
  542. {
  543. png_uint_32 output_len = comp->output_len;
  544. png_const_bytep output = comp->output;
  545. png_uint_32 avail = (sizeof comp->output);
  546. png_compression_buffer *next = png_ptr->zbuffer_list;
  547. for (;;)
  548. {
  549. if (avail > output_len)
  550. avail = output_len;
  551. png_write_chunk_data(png_ptr, output, avail);
  552. output_len -= avail;
  553. if (output_len == 0 || next == NULL)
  554. break;
  555. avail = png_ptr->zbuffer_size;
  556. output = next->output;
  557. next = next->next;
  558. }
  559. /* This is an internal error; 'next' must have been NULL! */
  560. if (output_len > 0)
  561. png_error(png_ptr, "error writing ancillary chunked compressed data");
  562. }
  563. #endif /* WRITE_COMPRESSED_TEXT */
  564. #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
  565. defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
  566. /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
  567. * and if invalid, correct the keyword rather than discarding the entire
  568. * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
  569. * length, forbids leading or trailing whitespace, multiple internal spaces,
  570. * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
  571. *
  572. * The 'new_key' buffer must be 80 characters in size (for the keyword plus a
  573. * trailing '\0'). If this routine returns 0 then there was no keyword, or a
  574. * valid one could not be generated, and the caller must png_error.
  575. */
  576. static png_uint_32
  577. png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
  578. {
  579. png_const_charp orig_key = key;
  580. png_uint_32 key_len = 0;
  581. int bad_character = 0;
  582. int space = 1;
  583. png_debug(1, "in png_check_keyword");
  584. if (key == NULL)
  585. {
  586. *new_key = 0;
  587. return 0;
  588. }
  589. while (*key && key_len < 79)
  590. {
  591. png_byte ch = (png_byte)(0xff & *key++);
  592. if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
  593. *new_key++ = ch, ++key_len, space = 0;
  594. else if (space == 0)
  595. {
  596. /* A space or an invalid character when one wasn't seen immediately
  597. * before; output just a space.
  598. */
  599. *new_key++ = 32, ++key_len, space = 1;
  600. /* If the character was not a space then it is invalid. */
  601. if (ch != 32)
  602. bad_character = ch;
  603. }
  604. else if (bad_character == 0)
  605. bad_character = ch; /* just skip it, record the first error */
  606. }
  607. if (key_len > 0 && space != 0) /* trailing space */
  608. {
  609. --key_len, --new_key;
  610. if (bad_character == 0)
  611. bad_character = 32;
  612. }
  613. /* Terminate the keyword */
  614. *new_key = 0;
  615. if (key_len == 0)
  616. return 0;
  617. #ifdef PNG_WARNINGS_SUPPORTED
  618. /* Try to only output one warning per keyword: */
  619. if (*key != 0) /* keyword too long */
  620. png_warning(png_ptr, "keyword truncated");
  621. else if (bad_character != 0)
  622. {
  623. PNG_WARNING_PARAMETERS(p)
  624. png_warning_parameter(p, 1, orig_key);
  625. png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
  626. png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
  627. }
  628. #endif /* WARNINGS */
  629. return key_len;
  630. }
  631. #endif /* WRITE_TEXT || WRITE_pCAL || WRITE_iCCP || WRITE_sPLT */
  632. /* Write the IHDR chunk, and update the png_struct with the necessary
  633. * information. Note that the rest of this code depends upon this
  634. * information being correct.
  635. */
  636. void /* PRIVATE */
  637. png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height,
  638. int bit_depth, int color_type, int compression_type, int filter_type,
  639. int interlace_type)
  640. {
  641. png_byte buf[13]; /* Buffer to store the IHDR info */
  642. png_debug(1, "in png_write_IHDR");
  643. /* Check that we have valid input data from the application info */
  644. switch (color_type)
  645. {
  646. case PNG_COLOR_TYPE_GRAY:
  647. switch (bit_depth)
  648. {
  649. case 1:
  650. case 2:
  651. case 4:
  652. case 8:
  653. #ifdef PNG_WRITE_16BIT_SUPPORTED
  654. case 16:
  655. #endif
  656. png_ptr->channels = 1; break;
  657. default:
  658. png_error(png_ptr,
  659. "Invalid bit depth for grayscale image");
  660. }
  661. break;
  662. case PNG_COLOR_TYPE_RGB:
  663. #ifdef PNG_WRITE_16BIT_SUPPORTED
  664. if (bit_depth != 8 && bit_depth != 16)
  665. #else
  666. if (bit_depth != 8)
  667. #endif
  668. png_error(png_ptr, "Invalid bit depth for RGB image");
  669. png_ptr->channels = 3;
  670. break;
  671. case PNG_COLOR_TYPE_PALETTE:
  672. switch (bit_depth)
  673. {
  674. case 1:
  675. case 2:
  676. case 4:
  677. case 8:
  678. png_ptr->channels = 1;
  679. break;
  680. default:
  681. png_error(png_ptr, "Invalid bit depth for paletted image");
  682. }
  683. break;
  684. case PNG_COLOR_TYPE_GRAY_ALPHA:
  685. if (bit_depth != 8 && bit_depth != 16)
  686. png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
  687. png_ptr->channels = 2;
  688. break;
  689. case PNG_COLOR_TYPE_RGB_ALPHA:
  690. #ifdef PNG_WRITE_16BIT_SUPPORTED
  691. if (bit_depth != 8 && bit_depth != 16)
  692. #else
  693. if (bit_depth != 8)
  694. #endif
  695. png_error(png_ptr, "Invalid bit depth for RGBA image");
  696. png_ptr->channels = 4;
  697. break;
  698. default:
  699. png_error(png_ptr, "Invalid image color type specified");
  700. }
  701. if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  702. {
  703. png_warning(png_ptr, "Invalid compression type specified");
  704. compression_type = PNG_COMPRESSION_TYPE_BASE;
  705. }
  706. /* Write filter_method 64 (intrapixel differencing) only if
  707. * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  708. * 2. Libpng did not write a PNG signature (this filter_method is only
  709. * used in PNG datastreams that are embedded in MNG datastreams) and
  710. * 3. The application called png_permit_mng_features with a mask that
  711. * included PNG_FLAG_MNG_FILTER_64 and
  712. * 4. The filter_method is 64 and
  713. * 5. The color_type is RGB or RGBA
  714. */
  715. if (
  716. #ifdef PNG_MNG_FEATURES_SUPPORTED
  717. !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
  718. ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
  719. (color_type == PNG_COLOR_TYPE_RGB ||
  720. color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
  721. (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
  722. #endif
  723. filter_type != PNG_FILTER_TYPE_BASE)
  724. {
  725. png_warning(png_ptr, "Invalid filter type specified");
  726. filter_type = PNG_FILTER_TYPE_BASE;
  727. }
  728. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  729. if (interlace_type != PNG_INTERLACE_NONE &&
  730. interlace_type != PNG_INTERLACE_ADAM7)
  731. {
  732. png_warning(png_ptr, "Invalid interlace type specified");
  733. interlace_type = PNG_INTERLACE_ADAM7;
  734. }
  735. #else
  736. interlace_type=PNG_INTERLACE_NONE;
  737. #endif
  738. /* Save the relevent information */
  739. png_ptr->bit_depth = (png_byte)bit_depth;
  740. png_ptr->color_type = (png_byte)color_type;
  741. png_ptr->interlaced = (png_byte)interlace_type;
  742. #ifdef PNG_MNG_FEATURES_SUPPORTED
  743. png_ptr->filter_type = (png_byte)filter_type;
  744. #endif
  745. png_ptr->compression_type = (png_byte)compression_type;
  746. png_ptr->width = width;
  747. png_ptr->height = height;
  748. png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
  749. png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
  750. /* Set the usr info, so any transformations can modify it */
  751. png_ptr->usr_width = png_ptr->width;
  752. png_ptr->usr_bit_depth = png_ptr->bit_depth;
  753. png_ptr->usr_channels = png_ptr->channels;
  754. /* Pack the header information into the buffer */
  755. png_save_uint_32(buf, width);
  756. png_save_uint_32(buf + 4, height);
  757. buf[8] = (png_byte)bit_depth;
  758. buf[9] = (png_byte)color_type;
  759. buf[10] = (png_byte)compression_type;
  760. buf[11] = (png_byte)filter_type;
  761. buf[12] = (png_byte)interlace_type;
  762. /* Write the chunk */
  763. png_write_complete_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
  764. if ((png_ptr->do_filter) == PNG_NO_FILTERS)
  765. {
  766. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
  767. png_ptr->bit_depth < 8)
  768. png_ptr->do_filter = PNG_FILTER_NONE;
  769. else
  770. png_ptr->do_filter = PNG_ALL_FILTERS;
  771. }
  772. png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
  773. }
  774. /* Write the palette. We are careful not to trust png_color to be in the
  775. * correct order for PNG, so people can redefine it to any convenient
  776. * structure.
  777. */
  778. void /* PRIVATE */
  779. png_write_PLTE(png_structrp png_ptr, png_const_colorp palette,
  780. png_uint_32 num_pal)
  781. {
  782. png_uint_32 i;
  783. png_const_colorp pal_ptr;
  784. png_byte buf[3];
  785. png_debug(1, "in png_write_PLTE");
  786. if ((
  787. #ifdef PNG_MNG_FEATURES_SUPPORTED
  788. (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 &&
  789. #endif
  790. num_pal == 0) || num_pal > 256)
  791. {
  792. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  793. {
  794. png_error(png_ptr, "Invalid number of colors in palette");
  795. }
  796. else
  797. {
  798. png_warning(png_ptr, "Invalid number of colors in palette");
  799. return;
  800. }
  801. }
  802. if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
  803. {
  804. png_warning(png_ptr,
  805. "Ignoring request to write a PLTE chunk in grayscale PNG");
  806. return;
  807. }
  808. png_ptr->num_palette = (png_uint_16)num_pal;
  809. png_debug1(3, "num_palette = %d", png_ptr->num_palette);
  810. png_write_chunk_header(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
  811. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  812. for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
  813. {
  814. buf[0] = pal_ptr->red;
  815. buf[1] = pal_ptr->green;
  816. buf[2] = pal_ptr->blue;
  817. png_write_chunk_data(png_ptr, buf, (png_size_t)3);
  818. }
  819. #else
  820. /* This is a little slower but some buggy compilers need to do this
  821. * instead
  822. */
  823. pal_ptr=palette;
  824. for (i = 0; i < num_pal; i++)
  825. {
  826. buf[0] = pal_ptr[i].red;
  827. buf[1] = pal_ptr[i].green;
  828. buf[2] = pal_ptr[i].blue;
  829. png_write_chunk_data(png_ptr, buf, (png_size_t)3);
  830. }
  831. #endif
  832. png_write_chunk_end(png_ptr);
  833. png_ptr->mode |= PNG_HAVE_PLTE;
  834. }
  835. /* This is similar to png_text_compress, above, except that it does not require
  836. * all of the data at once and, instead of buffering the compressed result,
  837. * writes it as IDAT chunks. Unlike png_text_compress it *can* png_error out
  838. * because it calls the write interface. As a result it does its own error
  839. * reporting and does not return an error code. In the event of error it will
  840. * just call png_error. The input data length may exceed 32-bits. The 'flush'
  841. * parameter is exactly the same as that to deflate, with the following
  842. * meanings:
  843. *
  844. * Z_NO_FLUSH: normal incremental output of compressed data
  845. * Z_SYNC_FLUSH: do a SYNC_FLUSH, used by png_write_flush
  846. * Z_FINISH: this is the end of the input, do a Z_FINISH and clean up
  847. *
  848. * The routine manages the acquire and release of the png_ptr->zstream by
  849. * checking and (at the end) clearing png_ptr->zowner; it does some sanity
  850. * checks on the 'mode' flags while doing this.
  851. */
  852. void /* PRIVATE */
  853. png_compress_IDAT(png_structrp png_ptr, png_const_bytep input,
  854. png_alloc_size_t input_len, int flush)
  855. {
  856. if (png_ptr->zowner != png_IDAT)
  857. {
  858. /* First time. Ensure we have a temporary buffer for compression and
  859. * trim the buffer list if it has more than one entry to free memory.
  860. * If 'WRITE_COMPRESSED_TEXT' is not set the list will never have been
  861. * created at this point, but the check here is quick and safe.
  862. */
  863. if (png_ptr->zbuffer_list == NULL)
  864. {
  865. png_ptr->zbuffer_list = png_voidcast(png_compression_bufferp,
  866. png_malloc(png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr)));
  867. png_ptr->zbuffer_list->next = NULL;
  868. }
  869. else
  870. png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list->next);
  871. /* It is a terminal error if we can't claim the zstream. */
  872. if (png_deflate_claim(png_ptr, png_IDAT, png_image_size(png_ptr)) != Z_OK)
  873. png_error(png_ptr, png_ptr->zstream.msg);
  874. /* The output state is maintained in png_ptr->zstream, so it must be
  875. * initialized here after the claim.
  876. */
  877. png_ptr->zstream.next_out = png_ptr->zbuffer_list->output;
  878. png_ptr->zstream.avail_out = png_ptr->zbuffer_size;
  879. }
  880. /* Now loop reading and writing until all the input is consumed or an error
  881. * terminates the operation. The _out values are maintained across calls to
  882. * this function, but the input must be reset each time.
  883. */
  884. png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
  885. png_ptr->zstream.avail_in = 0; /* set below */
  886. for (;;)
  887. {
  888. int ret;
  889. /* INPUT: from the row data */
  890. uInt avail = ZLIB_IO_MAX;
  891. if (avail > input_len)
  892. avail = (uInt)input_len; /* safe because of the check */
  893. png_ptr->zstream.avail_in = avail;
  894. input_len -= avail;
  895. ret = deflate(&png_ptr->zstream, input_len > 0 ? Z_NO_FLUSH : flush);
  896. /* Include as-yet unconsumed input */
  897. input_len += png_ptr->zstream.avail_in;
  898. png_ptr->zstream.avail_in = 0;
  899. /* OUTPUT: write complete IDAT chunks when avail_out drops to zero. Note
  900. * that these two zstream fields are preserved across the calls, therefore
  901. * there is no need to set these up on entry to the loop.
  902. */
  903. if (png_ptr->zstream.avail_out == 0)
  904. {
  905. png_bytep data = png_ptr->zbuffer_list->output;
  906. uInt size = png_ptr->zbuffer_size;
  907. /* Write an IDAT containing the data then reset the buffer. The
  908. * first IDAT may need deflate header optimization.
  909. */
  910. #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
  911. if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 &&
  912. png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
  913. optimize_cmf(data, png_image_size(png_ptr));
  914. #endif
  915. png_write_complete_chunk(png_ptr, png_IDAT, data, size);
  916. png_ptr->mode |= PNG_HAVE_IDAT;
  917. png_ptr->zstream.next_out = data;
  918. png_ptr->zstream.avail_out = size;
  919. /* For SYNC_FLUSH or FINISH it is essential to keep calling zlib with
  920. * the same flush parameter until it has finished output, for NO_FLUSH
  921. * it doesn't matter.
  922. */
  923. if (ret == Z_OK && flush != Z_NO_FLUSH)
  924. continue;
  925. }
  926. /* The order of these checks doesn't matter much; it just affects which
  927. * possible error might be detected if multiple things go wrong at once.
  928. */
  929. if (ret == Z_OK) /* most likely return code! */
  930. {
  931. /* If all the input has been consumed then just return. If Z_FINISH
  932. * was used as the flush parameter something has gone wrong if we get
  933. * here.
  934. */
  935. if (input_len == 0)
  936. {
  937. if (flush == Z_FINISH)
  938. png_error(png_ptr, "Z_OK on Z_FINISH with output space");
  939. return;
  940. }
  941. }
  942. else if (ret == Z_STREAM_END && flush == Z_FINISH)
  943. {
  944. /* This is the end of the IDAT data; any pending output must be
  945. * flushed. For small PNG files we may still be at the beginning.
  946. */
  947. png_bytep data = png_ptr->zbuffer_list->output;
  948. uInt size = png_ptr->zbuffer_size - png_ptr->zstream.avail_out;
  949. #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
  950. if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 &&
  951. png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
  952. optimize_cmf(data, png_image_size(png_ptr));
  953. #endif
  954. png_write_complete_chunk(png_ptr, png_IDAT, data, size);
  955. png_ptr->zstream.avail_out = 0;
  956. png_ptr->zstream.next_out = NULL;
  957. png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT;
  958. png_ptr->zowner = 0; /* Release the stream */
  959. return;
  960. }
  961. else
  962. {
  963. /* This is an error condition. */
  964. png_zstream_error(png_ptr, ret);
  965. png_error(png_ptr, png_ptr->zstream.msg);
  966. }
  967. }
  968. }
  969. /* Write an IEND chunk */
  970. void /* PRIVATE */
  971. png_write_IEND(png_structrp png_ptr)
  972. {
  973. png_debug(1, "in png_write_IEND");
  974. png_write_complete_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
  975. png_ptr->mode |= PNG_HAVE_IEND;
  976. }
  977. #ifdef PNG_WRITE_gAMA_SUPPORTED
  978. /* Write a gAMA chunk */
  979. void /* PRIVATE */
  980. png_write_gAMA_fixed(png_structrp png_ptr, png_fixed_point file_gamma)
  981. {
  982. png_byte buf[4];
  983. png_debug(1, "in png_write_gAMA");
  984. /* file_gamma is saved in 1/100,000ths */
  985. png_save_uint_32(buf, (png_uint_32)file_gamma);
  986. png_write_complete_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
  987. }
  988. #endif
  989. #ifdef PNG_WRITE_sRGB_SUPPORTED
  990. /* Write a sRGB chunk */
  991. void /* PRIVATE */
  992. png_write_sRGB(png_structrp png_ptr, int srgb_intent)
  993. {
  994. png_byte buf[1];
  995. png_debug(1, "in png_write_sRGB");
  996. if (srgb_intent >= PNG_sRGB_INTENT_LAST)
  997. png_warning(png_ptr,
  998. "Invalid sRGB rendering intent specified");
  999. buf[0]=(png_byte)srgb_intent;
  1000. png_write_complete_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
  1001. }
  1002. #endif
  1003. #ifdef PNG_WRITE_iCCP_SUPPORTED
  1004. /* Write an iCCP chunk */
  1005. void /* PRIVATE */
  1006. png_write_iCCP(png_structrp png_ptr, png_const_charp name,
  1007. png_const_bytep profile)
  1008. {
  1009. png_uint_32 name_len;
  1010. png_uint_32 profile_len;
  1011. png_byte new_name[81]; /* 1 byte for the compression byte */
  1012. compression_state comp;
  1013. png_uint_32 temp;
  1014. png_debug(1, "in png_write_iCCP");
  1015. /* These are all internal problems: the profile should have been checked
  1016. * before when it was stored.
  1017. */
  1018. if (profile == NULL)
  1019. png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */
  1020. profile_len = png_get_uint_32(profile);
  1021. if (profile_len < 132)
  1022. png_error(png_ptr, "ICC profile too short");
  1023. temp = (png_uint_32) (*(profile+8));
  1024. if (temp > 3 && (profile_len & 0x03))
  1025. png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)");
  1026. {
  1027. png_uint_32 embedded_profile_len = png_get_uint_32(profile);
  1028. if (profile_len != embedded_profile_len)
  1029. png_error(png_ptr, "Profile length does not match profile");
  1030. }
  1031. name_len = png_check_keyword(png_ptr, name, new_name);
  1032. if (name_len == 0)
  1033. png_error(png_ptr, "iCCP: invalid keyword");
  1034. new_name[++name_len] = PNG_COMPRESSION_TYPE_BASE;
  1035. /* Make sure we include the NULL after the name and the compression type */
  1036. ++name_len;
  1037. png_text_compress_init(&comp, profile, profile_len);
  1038. /* Allow for keyword terminator and compression byte */
  1039. if (png_text_compress(png_ptr, png_iCCP, &comp, name_len) != Z_OK)
  1040. png_error(png_ptr, png_ptr->zstream.msg);
  1041. png_write_chunk_header(png_ptr, png_iCCP, name_len + comp.output_len);
  1042. png_write_chunk_data(png_ptr, new_name, name_len);
  1043. png_write_compressed_data_out(png_ptr, &comp);
  1044. png_write_chunk_end(png_ptr);
  1045. }
  1046. #endif
  1047. #ifdef PNG_WRITE_sPLT_SUPPORTED
  1048. /* Write a sPLT chunk */
  1049. void /* PRIVATE */
  1050. png_write_sPLT(png_structrp png_ptr, png_const_sPLT_tp spalette)
  1051. {
  1052. png_uint_32 name_len;
  1053. png_byte new_name[80];
  1054. png_byte entrybuf[10];
  1055. png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
  1056. png_size_t palette_size = entry_size * spalette->nentries;
  1057. png_sPLT_entryp ep;
  1058. #ifndef PNG_POINTER_INDEXING_SUPPORTED
  1059. int i;
  1060. #endif
  1061. png_debug(1, "in png_write_sPLT");
  1062. name_len = png_check_keyword(png_ptr, spalette->name, new_name);
  1063. if (name_len == 0)
  1064. png_error(png_ptr, "sPLT: invalid keyword");
  1065. /* Make sure we include the NULL after the name */
  1066. png_write_chunk_header(png_ptr, png_sPLT,
  1067. (png_uint_32)(name_len + 2 + palette_size));
  1068. png_write_chunk_data(png_ptr, (png_bytep)new_name,
  1069. (png_size_t)(name_len + 1));
  1070. png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1);
  1071. /* Loop through each palette entry, writing appropriately */
  1072. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  1073. for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
  1074. {
  1075. if (spalette->depth == 8)
  1076. {
  1077. entrybuf[0] = (png_byte)ep->red;
  1078. entrybuf[1] = (png_byte)ep->green;
  1079. entrybuf[2] = (png_byte)ep->blue;
  1080. entrybuf[3] = (png_byte)ep->alpha;
  1081. png_save_uint_16(entrybuf + 4, ep->frequency);
  1082. }
  1083. else
  1084. {
  1085. png_save_uint_16(entrybuf + 0, ep->red);
  1086. png_save_uint_16(entrybuf + 2, ep->green);
  1087. png_save_uint_16(entrybuf + 4, ep->blue);
  1088. png_save_uint_16(entrybuf + 6, ep->alpha);
  1089. png_save_uint_16(entrybuf + 8, ep->frequency);
  1090. }
  1091. png_write_chunk_data(png_ptr, entrybuf, entry_size);
  1092. }
  1093. #else
  1094. ep=spalette->entries;
  1095. for (i = 0; i>spalette->nentries; i++)
  1096. {
  1097. if (spalette->depth == 8)
  1098. {
  1099. entrybuf[0] = (png_byte)ep[i].red;
  1100. entrybuf[1] = (png_byte)ep[i].green;
  1101. entrybuf[2] = (png_byte)ep[i].blue;
  1102. entrybuf[3] = (png_byte)ep[i].alpha;
  1103. png_save_uint_16(entrybuf + 4, ep[i].frequency);
  1104. }
  1105. else
  1106. {
  1107. png_save_uint_16(entrybuf + 0, ep[i].red);
  1108. png_save_uint_16(entrybuf + 2, ep[i].green);
  1109. png_save_uint_16(entrybuf + 4, ep[i].blue);
  1110. png_save_uint_16(entrybuf + 6, ep[i].alpha);
  1111. png_save_uint_16(entrybuf + 8, ep[i].frequency);
  1112. }
  1113. png_write_chunk_data(png_ptr, entrybuf, entry_size);
  1114. }
  1115. #endif
  1116. png_write_chunk_end(png_ptr);
  1117. }
  1118. #endif
  1119. #ifdef PNG_WRITE_sBIT_SUPPORTED
  1120. /* Write the sBIT chunk */
  1121. void /* PRIVATE */
  1122. png_write_sBIT(png_structrp png_ptr, png_const_color_8p sbit, int color_type)
  1123. {
  1124. png_byte buf[4];
  1125. png_size_t size;
  1126. png_debug(1, "in png_write_sBIT");
  1127. /* Make sure we don't depend upon the order of PNG_COLOR_8 */
  1128. if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
  1129. {
  1130. png_byte maxbits;
  1131. maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
  1132. png_ptr->usr_bit_depth);
  1133. if (sbit->red == 0 || sbit->red > maxbits ||
  1134. sbit->green == 0 || sbit->green > maxbits ||
  1135. sbit->blue == 0 || sbit->blue > maxbits)
  1136. {
  1137. png_warning(png_ptr, "Invalid sBIT depth specified");
  1138. return;
  1139. }
  1140. buf[0] = sbit->red;
  1141. buf[1] = sbit->green;
  1142. buf[2] = sbit->blue;
  1143. size = 3;
  1144. }
  1145. else
  1146. {
  1147. if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
  1148. {
  1149. png_warning(png_ptr, "Invalid sBIT depth specified");
  1150. return;
  1151. }
  1152. buf[0] = sbit->gray;
  1153. size = 1;
  1154. }
  1155. if ((color_type & PNG_COLOR_MASK_ALPHA) != 0)
  1156. {
  1157. if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
  1158. {
  1159. png_warning(png_ptr, "Invalid sBIT depth specified");
  1160. return;
  1161. }
  1162. buf[size++] = sbit->alpha;
  1163. }
  1164. png_write_complete_chunk(png_ptr, png_sBIT, buf, size);
  1165. }
  1166. #endif
  1167. #ifdef PNG_WRITE_cHRM_SUPPORTED
  1168. /* Write the cHRM chunk */
  1169. void /* PRIVATE */
  1170. png_write_cHRM_fixed(png_structrp png_ptr, const png_xy *xy)
  1171. {
  1172. png_byte buf[32];
  1173. png_debug(1, "in png_write_cHRM");
  1174. /* Each value is saved in 1/100,000ths */
  1175. png_save_int_32(buf, xy->whitex);
  1176. png_save_int_32(buf + 4, xy->whitey);
  1177. png_save_int_32(buf + 8, xy->redx);
  1178. png_save_int_32(buf + 12, xy->redy);
  1179. png_save_int_32(buf + 16, xy->greenx);
  1180. png_save_int_32(buf + 20, xy->greeny);
  1181. png_save_int_32(buf + 24, xy->bluex);
  1182. png_save_int_32(buf + 28, xy->bluey);
  1183. png_write_complete_chunk(png_ptr, png_cHRM, buf, 32);
  1184. }
  1185. #endif
  1186. #ifdef PNG_WRITE_tRNS_SUPPORTED
  1187. /* Write the tRNS chunk */
  1188. void /* PRIVATE */
  1189. png_write_tRNS(png_structrp png_ptr, png_const_bytep trans_alpha,
  1190. png_const_color_16p tran, int num_trans, int color_type)
  1191. {
  1192. png_byte buf[6];
  1193. png_debug(1, "in png_write_tRNS");
  1194. if (color_type == PNG_COLOR_TYPE_PALETTE)
  1195. {
  1196. if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
  1197. {
  1198. png_app_warning(png_ptr,
  1199. "Invalid number of transparent colors specified");
  1200. return;
  1201. }
  1202. /* Write the chunk out as it is */
  1203. png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha,
  1204. (png_size_t)num_trans);
  1205. }
  1206. else if (color_type == PNG_COLOR_TYPE_GRAY)
  1207. {
  1208. /* One 16 bit value */
  1209. if (tran->gray >= (1 << png_ptr->bit_depth))
  1210. {
  1211. png_app_warning(png_ptr,
  1212. "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
  1213. return;
  1214. }
  1215. png_save_uint_16(buf, tran->gray);
  1216. png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
  1217. }
  1218. else if (color_type == PNG_COLOR_TYPE_RGB)
  1219. {
  1220. /* Three 16 bit values */
  1221. png_save_uint_16(buf, tran->red);
  1222. png_save_uint_16(buf + 2, tran->green);
  1223. png_save_uint_16(buf + 4, tran->blue);
  1224. #ifdef PNG_WRITE_16BIT_SUPPORTED
  1225. if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0)
  1226. #else
  1227. if ((buf[0] | buf[2] | buf[4]) != 0)
  1228. #endif
  1229. {
  1230. png_app_warning(png_ptr,
  1231. "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
  1232. return;
  1233. }
  1234. png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
  1235. }
  1236. else
  1237. {
  1238. png_app_warning(png_ptr, "Can't write tRNS with an alpha channel");
  1239. }
  1240. }
  1241. #endif
  1242. #ifdef PNG_WRITE_bKGD_SUPPORTED
  1243. /* Write the background chunk */
  1244. void /* PRIVATE */
  1245. png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type)
  1246. {
  1247. png_byte buf[6];
  1248. png_debug(1, "in png_write_bKGD");
  1249. if (color_type == PNG_COLOR_TYPE_PALETTE)
  1250. {
  1251. if (
  1252. #ifdef PNG_MNG_FEATURES_SUPPORTED
  1253. (png_ptr->num_palette != 0 ||
  1254. (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0) &&
  1255. #endif
  1256. back->index >= png_ptr->num_palette)
  1257. {
  1258. png_warning(png_ptr, "Invalid background palette index");
  1259. return;
  1260. }
  1261. buf[0] = back->index;
  1262. png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
  1263. }
  1264. else if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
  1265. {
  1266. png_save_uint_16(buf, back->red);
  1267. png_save_uint_16(buf + 2, back->green);
  1268. png_save_uint_16(buf + 4, back->blue);
  1269. #ifdef PNG_WRITE_16BIT_SUPPORTED
  1270. if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0)
  1271. #else
  1272. if ((buf[0] | buf[2] | buf[4]) != 0)
  1273. #endif
  1274. {
  1275. png_warning(png_ptr,
  1276. "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
  1277. return;
  1278. }
  1279. png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
  1280. }
  1281. else
  1282. {
  1283. if (back->gray >= (1 << png_ptr->bit_depth))
  1284. {
  1285. png_warning(png_ptr,
  1286. "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
  1287. return;
  1288. }
  1289. png_save_uint_16(buf, back->gray);
  1290. png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
  1291. }
  1292. }
  1293. #endif
  1294. #ifdef PNG_WRITE_hIST_SUPPORTED
  1295. /* Write the histogram */
  1296. void /* PRIVATE */
  1297. png_write_hIST(png_structrp png_ptr, png_const_uint_16p hist, int num_hist)
  1298. {
  1299. int i;
  1300. png_byte buf[3];
  1301. png_debug(1, "in png_write_hIST");
  1302. if (num_hist > (int)png_ptr->num_palette)
  1303. {
  1304. png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
  1305. png_ptr->num_palette);
  1306. png_warning(png_ptr, "Invalid number of histogram entries specified");
  1307. return;
  1308. }
  1309. png_write_chunk_header(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
  1310. for (i = 0; i < num_hist; i++)
  1311. {
  1312. png_save_uint_16(buf, hist[i]);
  1313. png_write_chunk_data(png_ptr, buf, (png_size_t)2);
  1314. }
  1315. png_write_chunk_end(png_ptr);
  1316. }
  1317. #endif
  1318. #ifdef PNG_WRITE_tEXt_SUPPORTED
  1319. /* Write a tEXt chunk */
  1320. void /* PRIVATE */
  1321. png_write_tEXt(png_structrp png_ptr, png_const_charp key, png_const_charp text,
  1322. png_size_t text_len)
  1323. {
  1324. png_uint_32 key_len;
  1325. png_byte new_key[80];
  1326. png_debug(1, "in png_write_tEXt");
  1327. key_len = png_check_keyword(png_ptr, key, new_key);
  1328. if (key_len == 0)
  1329. png_error(png_ptr, "tEXt: invalid keyword");
  1330. if (text == NULL || *text == '\0')
  1331. text_len = 0;
  1332. else
  1333. text_len = strlen(text);
  1334. if (text_len > PNG_UINT_31_MAX - (key_len+1))
  1335. png_error(png_ptr, "tEXt: text too long");
  1336. /* Make sure we include the 0 after the key */
  1337. png_write_chunk_header(png_ptr, png_tEXt,
  1338. (png_uint_32)/*checked above*/(key_len + text_len + 1));
  1339. /*
  1340. * We leave it to the application to meet PNG-1.0 requirements on the
  1341. * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
  1342. * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
  1343. * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
  1344. */
  1345. png_write_chunk_data(png_ptr, new_key, key_len + 1);
  1346. if (text_len != 0)
  1347. png_write_chunk_data(png_ptr, (png_const_bytep)text, text_len);
  1348. png_write_chunk_end(png_ptr);
  1349. }
  1350. #endif
  1351. #ifdef PNG_WRITE_zTXt_SUPPORTED
  1352. /* Write a compressed text chunk */
  1353. void /* PRIVATE */
  1354. png_write_zTXt(png_structrp png_ptr, png_const_charp key, png_const_charp text,
  1355. int compression)
  1356. {
  1357. png_uint_32 key_len;
  1358. png_byte new_key[81];
  1359. compression_state comp;
  1360. png_debug(1, "in png_write_zTXt");
  1361. if (compression == PNG_TEXT_COMPRESSION_NONE)
  1362. {
  1363. png_write_tEXt(png_ptr, key, text, 0);
  1364. return;
  1365. }
  1366. if (compression != PNG_TEXT_COMPRESSION_zTXt)
  1367. png_error(png_ptr, "zTXt: invalid compression type");
  1368. key_len = png_check_keyword(png_ptr, key, new_key);
  1369. if (key_len == 0)
  1370. png_error(png_ptr, "zTXt: invalid keyword");
  1371. /* Add the compression method and 1 for the keyword separator. */
  1372. new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE;
  1373. ++key_len;
  1374. /* Compute the compressed data; do it now for the length */
  1375. png_text_compress_init(&comp, (png_const_bytep)text,
  1376. text == NULL ? 0 : strlen(text));
  1377. if (png_text_compress(png_ptr, png_zTXt, &comp, key_len) != Z_OK)
  1378. png_error(png_ptr, png_ptr->zstream.msg);
  1379. /* Write start of chunk */
  1380. png_write_chunk_header(png_ptr, png_zTXt, key_len + comp.output_len);
  1381. /* Write key */
  1382. png_write_chunk_data(png_ptr, new_key, key_len);
  1383. /* Write the compressed data */
  1384. png_write_compressed_data_out(png_ptr, &comp);
  1385. /* Close the chunk */
  1386. png_write_chunk_end(png_ptr);
  1387. }
  1388. #endif
  1389. #ifdef PNG_WRITE_iTXt_SUPPORTED
  1390. /* Write an iTXt chunk */
  1391. void /* PRIVATE */
  1392. png_write_iTXt(png_structrp png_ptr, int compression, png_const_charp key,
  1393. png_const_charp lang, png_const_charp lang_key, png_const_charp text)
  1394. {
  1395. png_uint_32 key_len, prefix_len;
  1396. png_size_t lang_len, lang_key_len;
  1397. png_byte new_key[82];
  1398. compression_state comp;
  1399. png_debug(1, "in png_write_iTXt");
  1400. key_len = png_check_keyword(png_ptr, key, new_key);
  1401. if (key_len == 0)
  1402. png_error(png_ptr, "iTXt: invalid keyword");
  1403. /* Set the compression flag */
  1404. switch (compression)
  1405. {
  1406. case PNG_ITXT_COMPRESSION_NONE:
  1407. case PNG_TEXT_COMPRESSION_NONE:
  1408. compression = new_key[++key_len] = 0; /* no compression */
  1409. break;
  1410. case PNG_TEXT_COMPRESSION_zTXt:
  1411. case PNG_ITXT_COMPRESSION_zTXt:
  1412. compression = new_key[++key_len] = 1; /* compressed */
  1413. break;
  1414. default:
  1415. png_error(png_ptr, "iTXt: invalid compression");
  1416. }
  1417. new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE;
  1418. ++key_len; /* for the keywod separator */
  1419. /* We leave it to the application to meet PNG-1.0 requirements on the
  1420. * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
  1421. * any non-Latin-1 characters except for NEWLINE. ISO PNG, however,
  1422. * specifies that the text is UTF-8 and this really doesn't require any
  1423. * checking.
  1424. *
  1425. * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
  1426. *
  1427. * TODO: validate the language tag correctly (see the spec.)
  1428. */
  1429. if (lang == NULL) lang = ""; /* empty language is valid */
  1430. lang_len = strlen(lang)+1;
  1431. if (lang_key == NULL) lang_key = ""; /* may be empty */
  1432. lang_key_len = strlen(lang_key)+1;
  1433. if (text == NULL) text = ""; /* may be empty */
  1434. prefix_len = key_len;
  1435. if (lang_len > PNG_UINT_31_MAX-prefix_len)
  1436. prefix_len = PNG_UINT_31_MAX;
  1437. else
  1438. prefix_len = (png_uint_32)(prefix_len + lang_len);
  1439. if (lang_key_len > PNG_UINT_31_MAX-prefix_len)
  1440. prefix_len = PNG_UINT_31_MAX;
  1441. else
  1442. prefix_len = (png_uint_32)(prefix_len + lang_key_len);
  1443. png_text_compress_init(&comp, (png_const_bytep)text, strlen(text));
  1444. if (compression != 0)
  1445. {
  1446. if (png_text_compress(png_ptr, png_iTXt, &comp, prefix_len) != Z_OK)
  1447. png_error(png_ptr, png_ptr->zstream.msg);
  1448. }
  1449. else
  1450. {
  1451. if (comp.input_len > PNG_UINT_31_MAX-prefix_len)
  1452. png_error(png_ptr, "iTXt: uncompressed text too long");
  1453. /* So the string will fit in a chunk: */
  1454. comp.output_len = (png_uint_32)/*SAFE*/comp.input_len;
  1455. }
  1456. png_write_chunk_header(png_ptr, png_iTXt, comp.output_len + prefix_len);
  1457. png_write_chunk_data(png_ptr, new_key, key_len);
  1458. png_write_chunk_data(png_ptr, (png_const_bytep)lang, lang_len);
  1459. png_write_chunk_data(png_ptr, (png_const_bytep)lang_key, lang_key_len);
  1460. if (compression != 0)
  1461. png_write_compressed_data_out(png_ptr, &comp);
  1462. else
  1463. png_write_chunk_data(png_ptr, (png_const_bytep)text, comp.input_len);
  1464. png_write_chunk_end(png_ptr);
  1465. }
  1466. #endif
  1467. #ifdef PNG_WRITE_oFFs_SUPPORTED
  1468. /* Write the oFFs chunk */
  1469. void /* PRIVATE */
  1470. png_write_oFFs(png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
  1471. int unit_type)
  1472. {
  1473. png_byte buf[9];
  1474. png_debug(1, "in png_write_oFFs");
  1475. if (unit_type >= PNG_OFFSET_LAST)
  1476. png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
  1477. png_save_int_32(buf, x_offset);
  1478. png_save_int_32(buf + 4, y_offset);
  1479. buf[8] = (png_byte)unit_type;
  1480. png_write_complete_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
  1481. }
  1482. #endif
  1483. #ifdef PNG_WRITE_pCAL_SUPPORTED
  1484. /* Write the pCAL chunk (described in the PNG extensions document) */
  1485. void /* PRIVATE */
  1486. png_write_pCAL(png_structrp png_ptr, png_charp purpose, png_int_32 X0,
  1487. png_int_32 X1, int type, int nparams, png_const_charp units,
  1488. png_charpp params)
  1489. {
  1490. png_uint_32 purpose_len;
  1491. png_size_t units_len, total_len;
  1492. png_size_tp params_len;
  1493. png_byte buf[10];
  1494. png_byte new_purpose[80];
  1495. int i;
  1496. png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
  1497. if (type >= PNG_EQUATION_LAST)
  1498. png_error(png_ptr, "Unrecognized equation type for pCAL chunk");
  1499. purpose_len = png_check_keyword(png_ptr, purpose, new_purpose);
  1500. if (purpose_len == 0)
  1501. png_error(png_ptr, "pCAL: invalid keyword");
  1502. ++purpose_len; /* terminator */
  1503. png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
  1504. units_len = strlen(units) + (nparams == 0 ? 0 : 1);
  1505. png_debug1(3, "pCAL units length = %d", (int)units_len);
  1506. total_len = purpose_len + units_len + 10;
  1507. params_len = (png_size_tp)png_malloc(png_ptr,
  1508. (png_alloc_size_t)(nparams * (sizeof (png_size_t))));
  1509. /* Find the length of each parameter, making sure we don't count the
  1510. * null terminator for the last parameter.
  1511. */
  1512. for (i = 0; i < nparams; i++)
  1513. {
  1514. params_len[i] = strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
  1515. png_debug2(3, "pCAL parameter %d length = %lu", i,
  1516. (unsigned long)params_len[i]);
  1517. total_len += params_len[i];
  1518. }
  1519. png_debug1(3, "pCAL total length = %d", (int)total_len);
  1520. png_write_chunk_header(png_ptr, png_pCAL, (png_uint_32)total_len);
  1521. png_write_chunk_data(png_ptr, new_purpose, purpose_len);
  1522. png_save_int_32(buf, X0);
  1523. png_save_int_32(buf + 4, X1);
  1524. buf[8] = (png_byte)type;
  1525. buf[9] = (png_byte)nparams;
  1526. png_write_chunk_data(png_ptr, buf, (png_size_t)10);
  1527. png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len);
  1528. for (i = 0; i < nparams; i++)
  1529. {
  1530. png_write_chunk_data(png_ptr, (png_const_bytep)params[i], params_len[i]);
  1531. }
  1532. png_free(png_ptr, params_len);
  1533. png_write_chunk_end(png_ptr);
  1534. }
  1535. #endif
  1536. #ifdef PNG_WRITE_sCAL_SUPPORTED
  1537. /* Write the sCAL chunk */
  1538. void /* PRIVATE */
  1539. png_write_sCAL_s(png_structrp png_ptr, int unit, png_const_charp width,
  1540. png_const_charp height)
  1541. {
  1542. png_byte buf[64];
  1543. png_size_t wlen, hlen, total_len;
  1544. png_debug(1, "in png_write_sCAL_s");
  1545. wlen = strlen(width);
  1546. hlen = strlen(height);
  1547. total_len = wlen + hlen + 2;
  1548. if (total_len > 64)
  1549. {
  1550. png_warning(png_ptr, "Can't write sCAL (buffer too small)");
  1551. return;
  1552. }
  1553. buf[0] = (png_byte)unit;
  1554. memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
  1555. memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
  1556. png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
  1557. png_write_complete_chunk(png_ptr, png_sCAL, buf, total_len);
  1558. }
  1559. #endif
  1560. #ifdef PNG_WRITE_pHYs_SUPPORTED
  1561. /* Write the pHYs chunk */
  1562. void /* PRIVATE */
  1563. png_write_pHYs(png_structrp png_ptr, png_uint_32 x_pixels_per_unit,
  1564. png_uint_32 y_pixels_per_unit,
  1565. int unit_type)
  1566. {
  1567. png_byte buf[9];
  1568. png_debug(1, "in png_write_pHYs");
  1569. if (unit_type >= PNG_RESOLUTION_LAST)
  1570. png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
  1571. png_save_uint_32(buf, x_pixels_per_unit);
  1572. png_save_uint_32(buf + 4, y_pixels_per_unit);
  1573. buf[8] = (png_byte)unit_type;
  1574. png_write_complete_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
  1575. }
  1576. #endif
  1577. #ifdef PNG_WRITE_tIME_SUPPORTED
  1578. /* Write the tIME chunk. Use either png_convert_from_struct_tm()
  1579. * or png_convert_from_time_t(), or fill in the structure yourself.
  1580. */
  1581. void /* PRIVATE */
  1582. png_write_tIME(png_structrp png_ptr, png_const_timep mod_time)
  1583. {
  1584. png_byte buf[7];
  1585. png_debug(1, "in png_write_tIME");
  1586. if (mod_time->month > 12 || mod_time->month < 1 ||
  1587. mod_time->day > 31 || mod_time->day < 1 ||
  1588. mod_time->hour > 23 || mod_time->second > 60)
  1589. {
  1590. png_warning(png_ptr, "Invalid time specified for tIME chunk");
  1591. return;
  1592. }
  1593. png_save_uint_16(buf, mod_time->year);
  1594. buf[2] = mod_time->month;
  1595. buf[3] = mod_time->day;
  1596. buf[4] = mod_time->hour;
  1597. buf[5] = mod_time->minute;
  1598. buf[6] = mod_time->second;
  1599. png_write_complete_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
  1600. }
  1601. #endif
  1602. /* Initializes the row writing capability of libpng */
  1603. void /* PRIVATE */
  1604. png_write_start_row(png_structrp png_ptr)
  1605. {
  1606. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  1607. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  1608. /* Start of interlace block */
  1609. static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  1610. /* Offset to next interlace block */
  1611. static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  1612. /* Start of interlace block in the y direction */
  1613. static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  1614. /* Offset to next interlace block in the y direction */
  1615. static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  1616. #endif
  1617. png_alloc_size_t buf_size;
  1618. int usr_pixel_depth;
  1619. png_debug(1, "in png_write_start_row");
  1620. usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth;
  1621. buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1;
  1622. /* 1.5.6: added to allow checking in the row write code. */
  1623. png_ptr->transformed_pixel_depth = png_ptr->pixel_depth;
  1624. png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth;
  1625. /* Set up row buffer */
  1626. png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
  1627. png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
  1628. #ifdef PNG_WRITE_FILTER_SUPPORTED
  1629. /* Set up filtering buffer, if using this filter */
  1630. if (png_ptr->do_filter & PNG_FILTER_SUB)
  1631. {
  1632. png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
  1633. png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
  1634. }
  1635. /* We only need to keep the previous row if we are using one of these. */
  1636. if ((png_ptr->do_filter &
  1637. (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) != 0)
  1638. {
  1639. /* Set up previous row buffer */
  1640. png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, buf_size);
  1641. if ((png_ptr->do_filter & PNG_FILTER_UP) != 0)
  1642. {
  1643. png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
  1644. png_ptr->rowbytes + 1);
  1645. png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
  1646. }
  1647. if ((png_ptr->do_filter & PNG_FILTER_AVG) != 0)
  1648. {
  1649. png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
  1650. png_ptr->rowbytes + 1);
  1651. png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
  1652. }
  1653. if ((png_ptr->do_filter & PNG_FILTER_PAETH) != 0)
  1654. {
  1655. png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
  1656. png_ptr->rowbytes + 1);
  1657. png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
  1658. }
  1659. }
  1660. #endif /* WRITE_FILTER */
  1661. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  1662. /* If interlaced, we need to set up width and height of pass */
  1663. if (png_ptr->interlaced != 0)
  1664. {
  1665. if ((png_ptr->transformations & PNG_INTERLACE) == 0)
  1666. {
  1667. png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  1668. png_pass_ystart[0]) / png_pass_yinc[0];
  1669. png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
  1670. png_pass_start[0]) / png_pass_inc[0];
  1671. }
  1672. else
  1673. {
  1674. png_ptr->num_rows = png_ptr->height;
  1675. png_ptr->usr_width = png_ptr->width;
  1676. }
  1677. }
  1678. else
  1679. #endif
  1680. {
  1681. png_ptr->num_rows = png_ptr->height;
  1682. png_ptr->usr_width = png_ptr->width;
  1683. }
  1684. }
  1685. /* Internal use only. Called when finished processing a row of data. */
  1686. void /* PRIVATE */
  1687. png_write_finish_row(png_structrp png_ptr)
  1688. {
  1689. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  1690. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  1691. /* Start of interlace block */
  1692. static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  1693. /* Offset to next interlace block */
  1694. static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  1695. /* Start of interlace block in the y direction */
  1696. static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  1697. /* Offset to next interlace block in the y direction */
  1698. static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  1699. #endif
  1700. png_debug(1, "in png_write_finish_row");
  1701. /* Next row */
  1702. png_ptr->row_number++;
  1703. /* See if we are done */
  1704. if (png_ptr->row_number < png_ptr->num_rows)
  1705. return;
  1706. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  1707. /* If interlaced, go to next pass */
  1708. if (png_ptr->interlaced != 0)
  1709. {
  1710. png_ptr->row_number = 0;
  1711. if ((png_ptr->transformations & PNG_INTERLACE) != 0)
  1712. {
  1713. png_ptr->pass++;
  1714. }
  1715. else
  1716. {
  1717. /* Loop until we find a non-zero width or height pass */
  1718. do
  1719. {
  1720. png_ptr->pass++;
  1721. if (png_ptr->pass >= 7)
  1722. break;
  1723. png_ptr->usr_width = (png_ptr->width +
  1724. png_pass_inc[png_ptr->pass] - 1 -
  1725. png_pass_start[png_ptr->pass]) /
  1726. png_pass_inc[png_ptr->pass];
  1727. png_ptr->num_rows = (png_ptr->height +
  1728. png_pass_yinc[png_ptr->pass] - 1 -
  1729. png_pass_ystart[png_ptr->pass]) /
  1730. png_pass_yinc[png_ptr->pass];
  1731. if ((png_ptr->transformations & PNG_INTERLACE) != 0)
  1732. break;
  1733. } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
  1734. }
  1735. /* Reset the row above the image for the next pass */
  1736. if (png_ptr->pass < 7)
  1737. {
  1738. if (png_ptr->prev_row != NULL)
  1739. memset(png_ptr->prev_row, 0,
  1740. (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
  1741. png_ptr->usr_bit_depth, png_ptr->width)) + 1);
  1742. return;
  1743. }
  1744. }
  1745. #endif
  1746. /* If we get here, we've just written the last row, so we need
  1747. to flush the compressor */
  1748. png_compress_IDAT(png_ptr, NULL, 0, Z_FINISH);
  1749. }
  1750. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  1751. /* Pick out the correct pixels for the interlace pass.
  1752. * The basic idea here is to go through the row with a source
  1753. * pointer and a destination pointer (sp and dp), and copy the
  1754. * correct pixels for the pass. As the row gets compacted,
  1755. * sp will always be >= dp, so we should never overwrite anything.
  1756. * See the default: case for the easiest code to understand.
  1757. */
  1758. void /* PRIVATE */
  1759. png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
  1760. {
  1761. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  1762. /* Start of interlace block */
  1763. static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  1764. /* Offset to next interlace block */
  1765. static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  1766. png_debug(1, "in png_do_write_interlace");
  1767. /* We don't have to do anything on the last pass (6) */
  1768. if (pass < 6)
  1769. {
  1770. /* Each pixel depth is handled separately */
  1771. switch (row_info->pixel_depth)
  1772. {
  1773. case 1:
  1774. {
  1775. png_bytep sp;
  1776. png_bytep dp;
  1777. int shift;
  1778. int d;
  1779. int value;
  1780. png_uint_32 i;
  1781. png_uint_32 row_width = row_info->width;
  1782. dp = row;
  1783. d = 0;
  1784. shift = 7;
  1785. for (i = png_pass_start[pass]; i < row_width;
  1786. i += png_pass_inc[pass])
  1787. {
  1788. sp = row + (png_size_t)(i >> 3);
  1789. value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
  1790. d |= (value << shift);
  1791. if (shift == 0)
  1792. {
  1793. shift = 7;
  1794. *dp++ = (png_byte)d;
  1795. d = 0;
  1796. }
  1797. else
  1798. shift--;
  1799. }
  1800. if (shift != 7)
  1801. *dp = (png_byte)d;
  1802. break;
  1803. }
  1804. case 2:
  1805. {
  1806. png_bytep sp;
  1807. png_bytep dp;
  1808. int shift;
  1809. int d;
  1810. int value;
  1811. png_uint_32 i;
  1812. png_uint_32 row_width = row_info->width;
  1813. dp = row;
  1814. shift = 6;
  1815. d = 0;
  1816. for (i = png_pass_start[pass]; i < row_width;
  1817. i += png_pass_inc[pass])
  1818. {
  1819. sp = row + (png_size_t)(i >> 2);
  1820. value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
  1821. d |= (value << shift);
  1822. if (shift == 0)
  1823. {
  1824. shift = 6;
  1825. *dp++ = (png_byte)d;
  1826. d = 0;
  1827. }
  1828. else
  1829. shift -= 2;
  1830. }
  1831. if (shift != 6)
  1832. *dp = (png_byte)d;
  1833. break;
  1834. }
  1835. case 4:
  1836. {
  1837. png_bytep sp;
  1838. png_bytep dp;
  1839. int shift;
  1840. int d;
  1841. int value;
  1842. png_uint_32 i;
  1843. png_uint_32 row_width = row_info->width;
  1844. dp = row;
  1845. shift = 4;
  1846. d = 0;
  1847. for (i = png_pass_start[pass]; i < row_width;
  1848. i += png_pass_inc[pass])
  1849. {
  1850. sp = row + (png_size_t)(i >> 1);
  1851. value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
  1852. d |= (value << shift);
  1853. if (shift == 0)
  1854. {
  1855. shift = 4;
  1856. *dp++ = (png_byte)d;
  1857. d = 0;
  1858. }
  1859. else
  1860. shift -= 4;
  1861. }
  1862. if (shift != 4)
  1863. *dp = (png_byte)d;
  1864. break;
  1865. }
  1866. default:
  1867. {
  1868. png_bytep sp;
  1869. png_bytep dp;
  1870. png_uint_32 i;
  1871. png_uint_32 row_width = row_info->width;
  1872. png_size_t pixel_bytes;
  1873. /* Start at the beginning */
  1874. dp = row;
  1875. /* Find out how many bytes each pixel takes up */
  1876. pixel_bytes = (row_info->pixel_depth >> 3);
  1877. /* Loop through the row, only looking at the pixels that matter */
  1878. for (i = png_pass_start[pass]; i < row_width;
  1879. i += png_pass_inc[pass])
  1880. {
  1881. /* Find out where the original pixel is */
  1882. sp = row + (png_size_t)i * pixel_bytes;
  1883. /* Move the pixel */
  1884. if (dp != sp)
  1885. memcpy(dp, sp, pixel_bytes);
  1886. /* Next pixel */
  1887. dp += pixel_bytes;
  1888. }
  1889. break;
  1890. }
  1891. }
  1892. /* Set new row width */
  1893. row_info->width = (row_info->width +
  1894. png_pass_inc[pass] - 1 -
  1895. png_pass_start[pass]) /
  1896. png_pass_inc[pass];
  1897. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
  1898. row_info->width);
  1899. }
  1900. }
  1901. #endif
  1902. /* This filters the row, chooses which filter to use, if it has not already
  1903. * been specified by the application, and then writes the row out with the
  1904. * chosen filter.
  1905. */
  1906. static void
  1907. png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
  1908. png_size_t row_bytes);
  1909. #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
  1910. #define PNG_HISHIFT 10
  1911. #define PNG_LOMASK ((png_uint_32)0xffffL)
  1912. #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
  1913. void /* PRIVATE */
  1914. png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
  1915. {
  1916. png_bytep best_row;
  1917. #ifdef PNG_WRITE_FILTER_SUPPORTED
  1918. png_bytep prev_row, row_buf;
  1919. png_uint_32 mins, bpp;
  1920. png_byte filter_to_do = png_ptr->do_filter;
  1921. png_size_t row_bytes = row_info->rowbytes;
  1922. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  1923. int num_p_filters = png_ptr->num_prev_filters;
  1924. #endif
  1925. png_debug(1, "in png_write_find_filter");
  1926. #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  1927. if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
  1928. {
  1929. /* These will never be selected so we need not test them. */
  1930. filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
  1931. }
  1932. #endif
  1933. /* Find out how many bytes offset each pixel is */
  1934. bpp = (row_info->pixel_depth + 7) >> 3;
  1935. prev_row = png_ptr->prev_row;
  1936. #endif
  1937. best_row = png_ptr->row_buf;
  1938. #ifdef PNG_WRITE_FILTER_SUPPORTED
  1939. row_buf = best_row;
  1940. mins = PNG_MAXSUM;
  1941. /* The prediction method we use is to find which method provides the
  1942. * smallest value when summing the absolute values of the distances
  1943. * from zero, using anything >= 128 as negative numbers. This is known
  1944. * as the "minimum sum of absolute differences" heuristic. Other
  1945. * heuristics are the "weighted minimum sum of absolute differences"
  1946. * (experimental and can in theory improve compression), and the "zlib
  1947. * predictive" method (not implemented yet), which does test compressions
  1948. * of lines using different filter methods, and then chooses the
  1949. * (series of) filter(s) that give minimum compressed data size (VERY
  1950. * computationally expensive).
  1951. *
  1952. * GRR 980525: consider also
  1953. *
  1954. * (1) minimum sum of absolute differences from running average (i.e.,
  1955. * keep running sum of non-absolute differences & count of bytes)
  1956. * [track dispersion, too? restart average if dispersion too large?]
  1957. *
  1958. * (1b) minimum sum of absolute differences from sliding average, probably
  1959. * with window size <= deflate window (usually 32K)
  1960. *
  1961. * (2) minimum sum of squared differences from zero or running average
  1962. * (i.e., ~ root-mean-square approach)
  1963. */
  1964. /* We don't need to test the 'no filter' case if this is the only filter
  1965. * that has been chosen, as it doesn't actually do anything to the data.
  1966. */
  1967. if ((filter_to_do & PNG_FILTER_NONE) != 0 && filter_to_do != PNG_FILTER_NONE)
  1968. {
  1969. png_bytep rp;
  1970. png_uint_32 sum = 0;
  1971. png_size_t i;
  1972. int v;
  1973. for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
  1974. {
  1975. v = *rp;
  1976. sum += (v < 128) ? v : 256 - v;
  1977. }
  1978. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  1979. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1980. {
  1981. png_uint_32 sumhi, sumlo;
  1982. int j;
  1983. sumlo = sum & PNG_LOMASK;
  1984. sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
  1985. /* Reduce the sum if we match any of the previous rows */
  1986. for (j = 0; j < num_p_filters; j++)
  1987. {
  1988. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
  1989. {
  1990. sumlo = (sumlo * png_ptr->filter_weights[j]) >>
  1991. PNG_WEIGHT_SHIFT;
  1992. sumhi = (sumhi * png_ptr->filter_weights[j]) >>
  1993. PNG_WEIGHT_SHIFT;
  1994. }
  1995. }
  1996. /* Factor in the cost of this filter (this is here for completeness,
  1997. * but it makes no sense to have a "cost" for the NONE filter, as
  1998. * it has the minimum possible computational cost - none).
  1999. */
  2000. sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
  2001. PNG_COST_SHIFT;
  2002. sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
  2003. PNG_COST_SHIFT;
  2004. if (sumhi > PNG_HIMASK)
  2005. sum = PNG_MAXSUM;
  2006. else
  2007. sum = (sumhi << PNG_HISHIFT) + sumlo;
  2008. }
  2009. #endif
  2010. mins = sum;
  2011. }
  2012. /* Sub filter */
  2013. if (filter_to_do == PNG_FILTER_SUB)
  2014. /* It's the only filter so no testing is needed */
  2015. {
  2016. png_bytep rp, lp, dp;
  2017. png_size_t i;
  2018. for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
  2019. i++, rp++, dp++)
  2020. {
  2021. *dp = *rp;
  2022. }
  2023. for (lp = row_buf + 1; i < row_bytes;
  2024. i++, rp++, lp++, dp++)
  2025. {
  2026. *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
  2027. }
  2028. best_row = png_ptr->sub_row;
  2029. }
  2030. else if ((filter_to_do & PNG_FILTER_SUB) != 0)
  2031. {
  2032. png_bytep rp, dp, lp;
  2033. png_uint_32 sum = 0, lmins = mins;
  2034. png_size_t i;
  2035. int v;
  2036. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  2037. /* We temporarily increase the "minimum sum" by the factor we
  2038. * would reduce the sum of this filter, so that we can do the
  2039. * early exit comparison without scaling the sum each time.
  2040. */
  2041. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2042. {
  2043. int j;
  2044. png_uint_32 lmhi, lmlo;
  2045. lmlo = lmins & PNG_LOMASK;
  2046. lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
  2047. for (j = 0; j < num_p_filters; j++)
  2048. {
  2049. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
  2050. {
  2051. lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
  2052. PNG_WEIGHT_SHIFT;
  2053. lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
  2054. PNG_WEIGHT_SHIFT;
  2055. }
  2056. }
  2057. lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
  2058. PNG_COST_SHIFT;
  2059. lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
  2060. PNG_COST_SHIFT;
  2061. if (lmhi > PNG_HIMASK)
  2062. lmins = PNG_MAXSUM;
  2063. else
  2064. lmins = (lmhi << PNG_HISHIFT) + lmlo;
  2065. }
  2066. #endif
  2067. for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
  2068. i++, rp++, dp++)
  2069. {
  2070. v = *dp = *rp;
  2071. sum += (v < 128) ? v : 256 - v;
  2072. }
  2073. for (lp = row_buf + 1; i < row_bytes;
  2074. i++, rp++, lp++, dp++)
  2075. {
  2076. v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
  2077. sum += (v < 128) ? v : 256 - v;
  2078. if (sum > lmins) /* We are already worse, don't continue. */
  2079. break;
  2080. }
  2081. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  2082. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2083. {
  2084. int j;
  2085. png_uint_32 sumhi, sumlo;
  2086. sumlo = sum & PNG_LOMASK;
  2087. sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
  2088. for (j = 0; j < num_p_filters; j++)
  2089. {
  2090. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
  2091. {
  2092. sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
  2093. PNG_WEIGHT_SHIFT;
  2094. sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
  2095. PNG_WEIGHT_SHIFT;
  2096. }
  2097. }
  2098. sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
  2099. PNG_COST_SHIFT;
  2100. sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
  2101. PNG_COST_SHIFT;
  2102. if (sumhi > PNG_HIMASK)
  2103. sum = PNG_MAXSUM;
  2104. else
  2105. sum = (sumhi << PNG_HISHIFT) + sumlo;
  2106. }
  2107. #endif
  2108. if (sum < mins)
  2109. {
  2110. mins = sum;
  2111. best_row = png_ptr->sub_row;
  2112. }
  2113. }
  2114. /* Up filter */
  2115. if (filter_to_do == PNG_FILTER_UP)
  2116. {
  2117. png_bytep rp, dp, pp;
  2118. png_size_t i;
  2119. for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
  2120. pp = prev_row + 1; i < row_bytes;
  2121. i++, rp++, pp++, dp++)
  2122. {
  2123. *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
  2124. }
  2125. best_row = png_ptr->up_row;
  2126. }
  2127. else if ((filter_to_do & PNG_FILTER_UP) != 0)
  2128. {
  2129. png_bytep rp, dp, pp;
  2130. png_uint_32 sum = 0, lmins = mins;
  2131. png_size_t i;
  2132. int v;
  2133. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  2134. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2135. {
  2136. int j;
  2137. png_uint_32 lmhi, lmlo;
  2138. lmlo = lmins & PNG_LOMASK;
  2139. lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
  2140. for (j = 0; j < num_p_filters; j++)
  2141. {
  2142. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
  2143. {
  2144. lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
  2145. PNG_WEIGHT_SHIFT;
  2146. lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
  2147. PNG_WEIGHT_SHIFT;
  2148. }
  2149. }
  2150. lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
  2151. PNG_COST_SHIFT;
  2152. lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
  2153. PNG_COST_SHIFT;
  2154. if (lmhi > PNG_HIMASK)
  2155. lmins = PNG_MAXSUM;
  2156. else
  2157. lmins = (lmhi << PNG_HISHIFT) + lmlo;
  2158. }
  2159. #endif
  2160. for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
  2161. pp = prev_row + 1; i < row_bytes; i++)
  2162. {
  2163. v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
  2164. sum += (v < 128) ? v : 256 - v;
  2165. if (sum > lmins) /* We are already worse, don't continue. */
  2166. break;
  2167. }
  2168. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  2169. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2170. {
  2171. int j;
  2172. png_uint_32 sumhi, sumlo;
  2173. sumlo = sum & PNG_LOMASK;
  2174. sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
  2175. for (j = 0; j < num_p_filters; j++)
  2176. {
  2177. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
  2178. {
  2179. sumlo = (sumlo * png_ptr->filter_weights[j]) >>
  2180. PNG_WEIGHT_SHIFT;
  2181. sumhi = (sumhi * png_ptr->filter_weights[j]) >>
  2182. PNG_WEIGHT_SHIFT;
  2183. }
  2184. }
  2185. sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
  2186. PNG_COST_SHIFT;
  2187. sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
  2188. PNG_COST_SHIFT;
  2189. if (sumhi > PNG_HIMASK)
  2190. sum = PNG_MAXSUM;
  2191. else
  2192. sum = (sumhi << PNG_HISHIFT) + sumlo;
  2193. }
  2194. #endif
  2195. if (sum < mins)
  2196. {
  2197. mins = sum;
  2198. best_row = png_ptr->up_row;
  2199. }
  2200. }
  2201. /* Avg filter */
  2202. if (filter_to_do == PNG_FILTER_AVG)
  2203. {
  2204. png_bytep rp, dp, pp, lp;
  2205. png_uint_32 i;
  2206. for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
  2207. pp = prev_row + 1; i < bpp; i++)
  2208. {
  2209. *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
  2210. }
  2211. for (lp = row_buf + 1; i < row_bytes; i++)
  2212. {
  2213. *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
  2214. & 0xff);
  2215. }
  2216. best_row = png_ptr->avg_row;
  2217. }
  2218. else if ((filter_to_do & PNG_FILTER_AVG) != 0)
  2219. {
  2220. png_bytep rp, dp, pp, lp;
  2221. png_uint_32 sum = 0, lmins = mins;
  2222. png_size_t i;
  2223. int v;
  2224. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  2225. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2226. {
  2227. int j;
  2228. png_uint_32 lmhi, lmlo;
  2229. lmlo = lmins & PNG_LOMASK;
  2230. lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
  2231. for (j = 0; j < num_p_filters; j++)
  2232. {
  2233. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
  2234. {
  2235. lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
  2236. PNG_WEIGHT_SHIFT;
  2237. lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
  2238. PNG_WEIGHT_SHIFT;
  2239. }
  2240. }
  2241. lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
  2242. PNG_COST_SHIFT;
  2243. lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
  2244. PNG_COST_SHIFT;
  2245. if (lmhi > PNG_HIMASK)
  2246. lmins = PNG_MAXSUM;
  2247. else
  2248. lmins = (lmhi << PNG_HISHIFT) + lmlo;
  2249. }
  2250. #endif
  2251. for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
  2252. pp = prev_row + 1; i < bpp; i++)
  2253. {
  2254. v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
  2255. sum += (v < 128) ? v : 256 - v;
  2256. }
  2257. for (lp = row_buf + 1; i < row_bytes; i++)
  2258. {
  2259. v = *dp++ =
  2260. (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
  2261. sum += (v < 128) ? v : 256 - v;
  2262. if (sum > lmins) /* We are already worse, don't continue. */
  2263. break;
  2264. }
  2265. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  2266. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2267. {
  2268. int j;
  2269. png_uint_32 sumhi, sumlo;
  2270. sumlo = sum & PNG_LOMASK;
  2271. sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
  2272. for (j = 0; j < num_p_filters; j++)
  2273. {
  2274. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
  2275. {
  2276. sumlo = (sumlo * png_ptr->filter_weights[j]) >>
  2277. PNG_WEIGHT_SHIFT;
  2278. sumhi = (sumhi * png_ptr->filter_weights[j]) >>
  2279. PNG_WEIGHT_SHIFT;
  2280. }
  2281. }
  2282. sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
  2283. PNG_COST_SHIFT;
  2284. sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
  2285. PNG_COST_SHIFT;
  2286. if (sumhi > PNG_HIMASK)
  2287. sum = PNG_MAXSUM;
  2288. else
  2289. sum = (sumhi << PNG_HISHIFT) + sumlo;
  2290. }
  2291. #endif
  2292. if (sum < mins)
  2293. {
  2294. mins = sum;
  2295. best_row = png_ptr->avg_row;
  2296. }
  2297. }
  2298. /* Paeth filter */
  2299. if ((filter_to_do == PNG_FILTER_PAETH) != 0)
  2300. {
  2301. png_bytep rp, dp, pp, cp, lp;
  2302. png_size_t i;
  2303. for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
  2304. pp = prev_row + 1; i < bpp; i++)
  2305. {
  2306. *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
  2307. }
  2308. for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
  2309. {
  2310. int a, b, c, pa, pb, pc, p;
  2311. b = *pp++;
  2312. c = *cp++;
  2313. a = *lp++;
  2314. p = b - c;
  2315. pc = a - c;
  2316. #ifdef PNG_USE_ABS
  2317. pa = abs(p);
  2318. pb = abs(pc);
  2319. pc = abs(p + pc);
  2320. #else
  2321. pa = p < 0 ? -p : p;
  2322. pb = pc < 0 ? -pc : pc;
  2323. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  2324. #endif
  2325. p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
  2326. *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
  2327. }
  2328. best_row = png_ptr->paeth_row;
  2329. }
  2330. else if ((filter_to_do & PNG_FILTER_PAETH) != 0)
  2331. {
  2332. png_bytep rp, dp, pp, cp, lp;
  2333. png_uint_32 sum = 0, lmins = mins;
  2334. png_size_t i;
  2335. int v;
  2336. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  2337. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2338. {
  2339. int j;
  2340. png_uint_32 lmhi, lmlo;
  2341. lmlo = lmins & PNG_LOMASK;
  2342. lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
  2343. for (j = 0; j < num_p_filters; j++)
  2344. {
  2345. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
  2346. {
  2347. lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
  2348. PNG_WEIGHT_SHIFT;
  2349. lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
  2350. PNG_WEIGHT_SHIFT;
  2351. }
  2352. }
  2353. lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
  2354. PNG_COST_SHIFT;
  2355. lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
  2356. PNG_COST_SHIFT;
  2357. if (lmhi > PNG_HIMASK)
  2358. lmins = PNG_MAXSUM;
  2359. else
  2360. lmins = (lmhi << PNG_HISHIFT) + lmlo;
  2361. }
  2362. #endif
  2363. for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
  2364. pp = prev_row + 1; i < bpp; i++)
  2365. {
  2366. v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
  2367. sum += (v < 128) ? v : 256 - v;
  2368. }
  2369. for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
  2370. {
  2371. int a, b, c, pa, pb, pc, p;
  2372. b = *pp++;
  2373. c = *cp++;
  2374. a = *lp++;
  2375. #ifndef PNG_SLOW_PAETH
  2376. p = b - c;
  2377. pc = a - c;
  2378. #ifdef PNG_USE_ABS
  2379. pa = abs(p);
  2380. pb = abs(pc);
  2381. pc = abs(p + pc);
  2382. #else
  2383. pa = p < 0 ? -p : p;
  2384. pb = pc < 0 ? -pc : pc;
  2385. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  2386. #endif
  2387. p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
  2388. #else /* SLOW_PAETH */
  2389. p = a + b - c;
  2390. pa = abs(p - a);
  2391. pb = abs(p - b);
  2392. pc = abs(p - c);
  2393. if (pa <= pb && pa <= pc)
  2394. p = a;
  2395. else if (pb <= pc)
  2396. p = b;
  2397. else
  2398. p = c;
  2399. #endif /* SLOW_PAETH */
  2400. v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
  2401. sum += (v < 128) ? v : 256 - v;
  2402. if (sum > lmins) /* We are already worse, don't continue. */
  2403. break;
  2404. }
  2405. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  2406. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2407. {
  2408. int j;
  2409. png_uint_32 sumhi, sumlo;
  2410. sumlo = sum & PNG_LOMASK;
  2411. sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
  2412. for (j = 0; j < num_p_filters; j++)
  2413. {
  2414. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
  2415. {
  2416. sumlo = (sumlo * png_ptr->filter_weights[j]) >>
  2417. PNG_WEIGHT_SHIFT;
  2418. sumhi = (sumhi * png_ptr->filter_weights[j]) >>
  2419. PNG_WEIGHT_SHIFT;
  2420. }
  2421. }
  2422. sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
  2423. PNG_COST_SHIFT;
  2424. sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
  2425. PNG_COST_SHIFT;
  2426. if (sumhi > PNG_HIMASK)
  2427. sum = PNG_MAXSUM;
  2428. else
  2429. sum = (sumhi << PNG_HISHIFT) + sumlo;
  2430. }
  2431. #endif
  2432. if (sum < mins)
  2433. {
  2434. best_row = png_ptr->paeth_row;
  2435. }
  2436. }
  2437. #endif /* WRITE_FILTER */
  2438. /* Do the actual writing of the filtered row data from the chosen filter. */
  2439. png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1);
  2440. #ifdef PNG_WRITE_FILTER_SUPPORTED
  2441. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  2442. /* Save the type of filter we picked this time for future calculations */
  2443. if (png_ptr->num_prev_filters > 0)
  2444. {
  2445. int j;
  2446. for (j = 1; j < num_p_filters; j++)
  2447. {
  2448. png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
  2449. }
  2450. png_ptr->prev_filters[j] = best_row[0];
  2451. }
  2452. #endif
  2453. #endif /* WRITE_FILTER */
  2454. }
  2455. /* Do the actual writing of a previously filtered row. */
  2456. static void
  2457. png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
  2458. png_size_t full_row_length/*includes filter byte*/)
  2459. {
  2460. png_debug(1, "in png_write_filtered_row");
  2461. png_debug1(2, "filter = %d", filtered_row[0]);
  2462. png_compress_IDAT(png_ptr, filtered_row, full_row_length, Z_NO_FLUSH);
  2463. /* Swap the current and previous rows */
  2464. if (png_ptr->prev_row != NULL)
  2465. {
  2466. png_bytep tptr;
  2467. tptr = png_ptr->prev_row;
  2468. png_ptr->prev_row = png_ptr->row_buf;
  2469. png_ptr->row_buf = tptr;
  2470. }
  2471. /* Finish row - updates counters and flushes zlib if last row */
  2472. png_write_finish_row(png_ptr);
  2473. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  2474. png_ptr->flush_rows++;
  2475. if (png_ptr->flush_dist > 0 &&
  2476. png_ptr->flush_rows >= png_ptr->flush_dist)
  2477. {
  2478. png_write_flush(png_ptr);
  2479. }
  2480. #endif /* WRITE_FLUSH */
  2481. }
  2482. #endif /* WRITE */