pngwutil.c 83 KB

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