pngwutil.c 82 KB

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