Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. /* pngwrite.c - general routines to write a PNG file
  3.  *
  4.  * Last changed in libpng 1.5.1 [February 3, 2011]
  5.  * Copyright (c) 1998-2011 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.  * This code is released under the libpng license.
  10.  * For conditions of distribution and use, see the disclaimer
  11.  * and license in png.h
  12.  */
  13.  
  14. #include "pngpriv.h"
  15.  
  16. #ifdef PNG_WRITE_SUPPORTED
  17.  
  18. /* Writes all the PNG information.  This is the suggested way to use the
  19.  * library.  If you have a new chunk to add, make a function to write it,
  20.  * and put it in the correct location here.  If you want the chunk written
  21.  * after the image data, put it in png_write_end().  I strongly encourage
  22.  * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
  23.  * the chunk, as that will keep the code from breaking if you want to just
  24.  * write a plain PNG file.  If you have long comments, I suggest writing
  25.  * them in png_write_end(), and compressing them.
  26.  */
  27. void PNGAPI
  28. png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
  29. {
  30.    png_debug(1, "in png_write_info_before_PLTE");
  31.  
  32.    if (png_ptr == NULL || info_ptr == NULL)
  33.       return;
  34.  
  35.    if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
  36.    {
  37.    /* Write PNG signature */
  38.    png_write_sig(png_ptr);
  39.  
  40. #ifdef PNG_MNG_FEATURES_SUPPORTED
  41.    if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
  42.        (png_ptr->mng_features_permitted))
  43.    {
  44.       png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
  45.       png_ptr->mng_features_permitted = 0;
  46.    }
  47. #endif
  48.  
  49.    /* Write IHDR information. */
  50.    png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
  51.        info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
  52.        info_ptr->filter_type,
  53. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  54.        info_ptr->interlace_type);
  55. #else
  56.        0);
  57. #endif
  58.    /* The rest of these check to see if the valid field has the appropriate
  59.     * flag set, and if it does, writes the chunk.
  60.     */
  61. #ifdef PNG_WRITE_gAMA_SUPPORTED
  62.    if (info_ptr->valid & PNG_INFO_gAMA)
  63.       png_write_gAMA_fixed(png_ptr, info_ptr->gamma);
  64. #endif
  65. #ifdef PNG_WRITE_sRGB_SUPPORTED
  66.    if (info_ptr->valid & PNG_INFO_sRGB)
  67.       png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
  68. #endif
  69.  
  70. #ifdef PNG_WRITE_iCCP_SUPPORTED
  71.    if (info_ptr->valid & PNG_INFO_iCCP)
  72.       png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
  73.           (png_charp)info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
  74. #endif
  75. #ifdef PNG_WRITE_sBIT_SUPPORTED
  76.    if (info_ptr->valid & PNG_INFO_sBIT)
  77.       png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
  78. #endif
  79. #ifdef PNG_WRITE_cHRM_SUPPORTED
  80.    if (info_ptr->valid & PNG_INFO_cHRM)
  81.       png_write_cHRM_fixed(png_ptr,
  82.           info_ptr->x_white, info_ptr->y_white,
  83.           info_ptr->x_red, info_ptr->y_red,
  84.           info_ptr->x_green, info_ptr->y_green,
  85.           info_ptr->x_blue, info_ptr->y_blue);
  86. #endif
  87.  
  88. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  89.    if (info_ptr->unknown_chunks_num)
  90.    {
  91.       png_unknown_chunk *up;
  92.  
  93.       png_debug(5, "writing extra chunks");
  94.  
  95.       for (up = info_ptr->unknown_chunks;
  96.            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  97.            up++)
  98.       {
  99.          int keep = png_handle_as_unknown(png_ptr, up->name);
  100.  
  101.          if (keep != PNG_HANDLE_CHUNK_NEVER &&
  102.              up->location && !(up->location & PNG_HAVE_PLTE) &&
  103.              !(up->location & PNG_HAVE_IDAT) &&
  104.              ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  105.              (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  106.          {
  107.             if (up->size == 0)
  108.                png_warning(png_ptr, "Writing zero-length unknown chunk");
  109.  
  110.             png_write_chunk(png_ptr, up->name, up->data, up->size);
  111.          }
  112.       }
  113.    }
  114. #endif
  115.       png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
  116.    }
  117. }
  118.  
  119. void PNGAPI
  120. png_write_info(png_structp png_ptr, png_infop info_ptr)
  121. {
  122. #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
  123.    int i;
  124. #endif
  125.  
  126.    png_debug(1, "in png_write_info");
  127.  
  128.    if (png_ptr == NULL || info_ptr == NULL)
  129.       return;
  130.  
  131.    png_write_info_before_PLTE(png_ptr, info_ptr);
  132.  
  133.    if (info_ptr->valid & PNG_INFO_PLTE)
  134.       png_write_PLTE(png_ptr, info_ptr->palette,
  135.           (png_uint_32)info_ptr->num_palette);
  136.  
  137.    else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  138.       png_error(png_ptr, "Valid palette required for paletted images");
  139.  
  140. #ifdef PNG_WRITE_tRNS_SUPPORTED
  141.    if (info_ptr->valid & PNG_INFO_tRNS)
  142.    {
  143. #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  144.       /* Invert the alpha channel (in tRNS) */
  145.       if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
  146.           info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  147.       {
  148.          int j;
  149.          for (j = 0; j<(int)info_ptr->num_trans; j++)
  150.             info_ptr->trans_alpha[j] =
  151.                (png_byte)(255 - info_ptr->trans_alpha[j]);
  152.       }
  153. #endif
  154.       png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
  155.           info_ptr->num_trans, info_ptr->color_type);
  156.    }
  157. #endif
  158. #ifdef PNG_WRITE_bKGD_SUPPORTED
  159.    if (info_ptr->valid & PNG_INFO_bKGD)
  160.       png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
  161. #endif
  162.  
  163. #ifdef PNG_WRITE_hIST_SUPPORTED
  164.    if (info_ptr->valid & PNG_INFO_hIST)
  165.       png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
  166. #endif
  167.  
  168. #ifdef PNG_WRITE_oFFs_SUPPORTED
  169.    if (info_ptr->valid & PNG_INFO_oFFs)
  170.       png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
  171.           info_ptr->offset_unit_type);
  172. #endif
  173.  
  174. #ifdef PNG_WRITE_pCAL_SUPPORTED
  175.    if (info_ptr->valid & PNG_INFO_pCAL)
  176.       png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
  177.           info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
  178.           info_ptr->pcal_units, info_ptr->pcal_params);
  179. #endif
  180.  
  181. #ifdef PNG_WRITE_sCAL_SUPPORTED
  182.    if (info_ptr->valid & PNG_INFO_sCAL)
  183.       png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
  184.           info_ptr->scal_s_width, info_ptr->scal_s_height);
  185. #endif /* sCAL */
  186.  
  187. #ifdef PNG_WRITE_pHYs_SUPPORTED
  188.    if (info_ptr->valid & PNG_INFO_pHYs)
  189.       png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
  190.           info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
  191. #endif /* pHYs */
  192.  
  193. #ifdef PNG_WRITE_tIME_SUPPORTED
  194.    if (info_ptr->valid & PNG_INFO_tIME)
  195.    {
  196.       png_write_tIME(png_ptr, &(info_ptr->mod_time));
  197.       png_ptr->mode |= PNG_WROTE_tIME;
  198.    }
  199. #endif /* tIME */
  200.  
  201. #ifdef PNG_WRITE_sPLT_SUPPORTED
  202.    if (info_ptr->valid & PNG_INFO_sPLT)
  203.       for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
  204.          png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
  205. #endif /* sPLT */
  206.  
  207. #ifdef PNG_WRITE_TEXT_SUPPORTED
  208.    /* Check to see if we need to write text chunks */
  209.    for (i = 0; i < info_ptr->num_text; i++)
  210.    {
  211.       png_debug2(2, "Writing header text chunk %d, type %d", i,
  212.           info_ptr->text[i].compression);
  213.       /* An internationalized chunk? */
  214.       if (info_ptr->text[i].compression > 0)
  215.       {
  216. #ifdef PNG_WRITE_iTXt_SUPPORTED
  217.          /* Write international chunk */
  218.          png_write_iTXt(png_ptr,
  219.              info_ptr->text[i].compression,
  220.              info_ptr->text[i].key,
  221.              info_ptr->text[i].lang,
  222.              info_ptr->text[i].lang_key,
  223.              info_ptr->text[i].text);
  224. #else
  225.           png_warning(png_ptr, "Unable to write international text");
  226. #endif
  227.           /* Mark this chunk as written */
  228.           info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  229.       }
  230.  
  231.       /* If we want a compressed text chunk */
  232.       else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
  233.       {
  234. #ifdef PNG_WRITE_zTXt_SUPPORTED
  235.          /* Write compressed chunk */
  236.          png_write_zTXt(png_ptr, info_ptr->text[i].key,
  237.              info_ptr->text[i].text, 0,
  238.              info_ptr->text[i].compression);
  239. #else
  240.          png_warning(png_ptr, "Unable to write compressed text");
  241. #endif
  242.          /* Mark this chunk as written */
  243.          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  244.       }
  245.  
  246.       else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  247.       {
  248. #ifdef PNG_WRITE_tEXt_SUPPORTED
  249.          /* Write uncompressed chunk */
  250.          png_write_tEXt(png_ptr, info_ptr->text[i].key,
  251.              info_ptr->text[i].text,
  252.              0);
  253.          /* Mark this chunk as written */
  254.          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  255. #else
  256.          /* Can't get here */
  257.          png_warning(png_ptr, "Unable to write uncompressed text");
  258. #endif
  259.       }
  260.    }
  261. #endif /* tEXt */
  262.  
  263. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  264.    if (info_ptr->unknown_chunks_num)
  265.    {
  266.       png_unknown_chunk *up;
  267.  
  268.       png_debug(5, "writing extra chunks");
  269.  
  270.       for (up = info_ptr->unknown_chunks;
  271.            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  272.            up++)
  273.       {
  274.          int keep = png_handle_as_unknown(png_ptr, up->name);
  275.          if (keep != PNG_HANDLE_CHUNK_NEVER &&
  276.              up->location && (up->location & PNG_HAVE_PLTE) &&
  277.              !(up->location & PNG_HAVE_IDAT) &&
  278.              ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  279.              (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  280.          {
  281.             png_write_chunk(png_ptr, up->name, up->data, up->size);
  282.          }
  283.       }
  284.    }
  285. #endif
  286. }
  287.  
  288. /* Writes the end of the PNG file.  If you don't want to write comments or
  289.  * time information, you can pass NULL for info.  If you already wrote these
  290.  * in png_write_info(), do not write them again here.  If you have long
  291.  * comments, I suggest writing them here, and compressing them.
  292.  */
  293. void PNGAPI
  294. png_write_end(png_structp png_ptr, png_infop info_ptr)
  295. {
  296.    png_debug(1, "in png_write_end");
  297.  
  298.    if (png_ptr == NULL)
  299.       return;
  300.  
  301.    if (!(png_ptr->mode & PNG_HAVE_IDAT))
  302.       png_error(png_ptr, "No IDATs written into file");
  303.  
  304.    /* See if user wants us to write information chunks */
  305.    if (info_ptr != NULL)
  306.    {
  307. #ifdef PNG_WRITE_TEXT_SUPPORTED
  308.       int i; /* local index variable */
  309. #endif
  310. #ifdef PNG_WRITE_tIME_SUPPORTED
  311.       /* Check to see if user has supplied a time chunk */
  312.       if ((info_ptr->valid & PNG_INFO_tIME) &&
  313.           !(png_ptr->mode & PNG_WROTE_tIME))
  314.          png_write_tIME(png_ptr, &(info_ptr->mod_time));
  315.  
  316. #endif
  317. #ifdef PNG_WRITE_TEXT_SUPPORTED
  318.       /* Loop through comment chunks */
  319.       for (i = 0; i < info_ptr->num_text; i++)
  320.       {
  321.          png_debug2(2, "Writing trailer text chunk %d, type %d", i,
  322.             info_ptr->text[i].compression);
  323.          /* An internationalized chunk? */
  324.          if (info_ptr->text[i].compression > 0)
  325.          {
  326. #ifdef PNG_WRITE_iTXt_SUPPORTED
  327.             /* Write international chunk */
  328.             png_write_iTXt(png_ptr,
  329.                 info_ptr->text[i].compression,
  330.                 info_ptr->text[i].key,
  331.                 info_ptr->text[i].lang,
  332.                 info_ptr->text[i].lang_key,
  333.                 info_ptr->text[i].text);
  334. #else
  335.             png_warning(png_ptr, "Unable to write international text");
  336. #endif
  337.             /* Mark this chunk as written */
  338.             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  339.          }
  340.  
  341.          else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
  342.          {
  343. #ifdef PNG_WRITE_zTXt_SUPPORTED
  344.             /* Write compressed chunk */
  345.             png_write_zTXt(png_ptr, info_ptr->text[i].key,
  346.                 info_ptr->text[i].text, 0,
  347.                 info_ptr->text[i].compression);
  348. #else
  349.             png_warning(png_ptr, "Unable to write compressed text");
  350. #endif
  351.             /* Mark this chunk as written */
  352.             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  353.          }
  354.  
  355.          else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  356.          {
  357. #ifdef PNG_WRITE_tEXt_SUPPORTED
  358.             /* Write uncompressed chunk */
  359.             png_write_tEXt(png_ptr, info_ptr->text[i].key,
  360.                 info_ptr->text[i].text, 0);
  361. #else
  362.             png_warning(png_ptr, "Unable to write uncompressed text");
  363. #endif
  364.  
  365.             /* Mark this chunk as written */
  366.             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  367.          }
  368.       }
  369. #endif
  370. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  371.    if (info_ptr->unknown_chunks_num)
  372.    {
  373.       png_unknown_chunk *up;
  374.  
  375.       png_debug(5, "writing extra chunks");
  376.  
  377.       for (up = info_ptr->unknown_chunks;
  378.            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  379.            up++)
  380.       {
  381.          int keep = png_handle_as_unknown(png_ptr, up->name);
  382.          if (keep != PNG_HANDLE_CHUNK_NEVER &&
  383.              up->location && (up->location & PNG_AFTER_IDAT) &&
  384.              ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  385.              (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  386.          {
  387.             png_write_chunk(png_ptr, up->name, up->data, up->size);
  388.          }
  389.       }
  390.    }
  391. #endif
  392.    }
  393.  
  394.    png_ptr->mode |= PNG_AFTER_IDAT;
  395.  
  396.    /* Write end of PNG file */
  397.    png_write_IEND(png_ptr);
  398.    /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
  399.     * and restored again in libpng-1.2.30, may cause some applications that
  400.     * do not set png_ptr->output_flush_fn to crash.  If your application
  401.     * experiences a problem, please try building libpng with
  402.     * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
  403.     * png-mng-implement at lists.sf.net .
  404.     */
  405. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  406. #  ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
  407.    png_flush(png_ptr);
  408. #  endif
  409. #endif
  410. }
  411.  
  412. #ifdef PNG_CONVERT_tIME_SUPPORTED
  413. /* "tm" structure is not supported on WindowsCE */
  414. void PNGAPI
  415. png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm FAR * ttime)
  416. {
  417.    png_debug(1, "in png_convert_from_struct_tm");
  418.  
  419.    ptime->year = (png_uint_16)(1900 + ttime->tm_year);
  420.    ptime->month = (png_byte)(ttime->tm_mon + 1);
  421.    ptime->day = (png_byte)ttime->tm_mday;
  422.    ptime->hour = (png_byte)ttime->tm_hour;
  423.    ptime->minute = (png_byte)ttime->tm_min;
  424.    ptime->second = (png_byte)ttime->tm_sec;
  425. }
  426.  
  427. void PNGAPI
  428. png_convert_from_time_t(png_timep ptime, time_t ttime)
  429. {
  430.    struct tm *tbuf;
  431.  
  432.    png_debug(1, "in png_convert_from_time_t");
  433.  
  434.    tbuf = gmtime(&ttime);
  435.    png_convert_from_struct_tm(ptime, tbuf);
  436. }
  437. #endif
  438.  
  439. /* Initialize png_ptr structure, and allocate any memory needed */
  440. PNG_FUNCTION(png_structp,PNGAPI
  441. png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
  442.     png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
  443. {
  444. #ifdef PNG_USER_MEM_SUPPORTED
  445.    return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
  446.        warn_fn, NULL, NULL, NULL));
  447. }
  448.  
  449. /* Alternate initialize png_ptr structure, and allocate any memory needed */
  450. static void png_reset_filter_heuristics(png_structp png_ptr); /* forward decl */
  451.  
  452. PNG_FUNCTION(png_structp,PNGAPI
  453. png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
  454.     png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  455.     png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
  456. {
  457. #endif /* PNG_USER_MEM_SUPPORTED */
  458.    volatile int png_cleanup_needed = 0;
  459. #ifdef PNG_SETJMP_SUPPORTED
  460.    volatile
  461. #endif
  462.    png_structp png_ptr;
  463. #ifdef PNG_SETJMP_SUPPORTED
  464. #ifdef USE_FAR_KEYWORD
  465.    jmp_buf png_jmpbuf;
  466. #endif
  467. #endif
  468.    int i;
  469.  
  470.    png_debug(1, "in png_create_write_struct");
  471.  
  472. #ifdef PNG_USER_MEM_SUPPORTED
  473.    png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  474.        (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
  475. #else
  476.    png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  477. #endif /* PNG_USER_MEM_SUPPORTED */
  478.    if (png_ptr == NULL)
  479.       return (NULL);
  480.  
  481.    /* Added at libpng-1.2.6 */
  482. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  483.    png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
  484.    png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
  485. #endif
  486.  
  487. #ifdef PNG_SETJMP_SUPPORTED
  488. /* Applications that neglect to set up their own setjmp() and then
  489.    encounter a png_error() will longjmp here.  Since the jmpbuf is
  490.    then meaningless we abort instead of returning. */
  491. #ifdef USE_FAR_KEYWORD
  492.    if (setjmp(png_jmpbuf))
  493. #else
  494.    if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
  495. #endif
  496. #ifdef USE_FAR_KEYWORD
  497.    png_memcpy(png_jmpbuf(png_ptr), png_jmpbuf, png_sizeof(jmp_buf));
  498. #endif
  499.       PNG_ABORT();
  500. #endif
  501.  
  502. #ifdef PNG_USER_MEM_SUPPORTED
  503.    png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
  504. #endif /* PNG_USER_MEM_SUPPORTED */
  505.    png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
  506.  
  507.    if (user_png_ver)
  508.    {
  509.       i = 0;
  510.       do
  511.       {
  512.          if (user_png_ver[i] != png_libpng_ver[i])
  513.             png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  514.       } while (png_libpng_ver[i++]);
  515.    }
  516.  
  517.    if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
  518.    {
  519.      /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
  520.       * we must recompile any applications that use any older library version.
  521.       * For versions after libpng 1.0, we will be compatible, so we need
  522.       * only check the first digit.
  523.       */
  524.      if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
  525.          (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
  526.          (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
  527.      {
  528. #ifdef PNG_CONSOLE_IO_SUPPORTED
  529.         char msg[80];
  530.  
  531.         if (user_png_ver)
  532.         {
  533.             png_snprintf2(msg, 80,
  534.                 "Application built with libpng-%.20s"
  535.                 " but running with %.20s",
  536.                 user_png_ver,
  537.                 png_libpng_ver);
  538.             png_warning(png_ptr, msg);
  539.          }
  540. #else
  541.          png_warning(png_ptr,
  542.              "Incompatible libpng version in application and library");
  543. #endif
  544. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  545.         png_ptr->flags = 0;
  546. #endif
  547.         png_cleanup_needed = 1;
  548.      }
  549.    }
  550.  
  551.    /* Initialize zbuf - compression buffer */
  552.    png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  553.  
  554.    if (!png_cleanup_needed)
  555.    {
  556.       png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
  557.           png_ptr->zbuf_size);
  558.       if (png_ptr->zbuf == NULL)
  559.          png_cleanup_needed = 1;
  560.    }
  561.  
  562.    if (png_cleanup_needed)
  563.    {
  564.        /* Clean up PNG structure and deallocate any memory. */
  565.        png_free(png_ptr, png_ptr->zbuf);
  566.        png_ptr->zbuf = NULL;
  567. #ifdef PNG_USER_MEM_SUPPORTED
  568.        png_destroy_struct_2((png_voidp)png_ptr,
  569.            (png_free_ptr)free_fn, (png_voidp)mem_ptr);
  570. #else
  571.        png_destroy_struct((png_voidp)png_ptr);
  572. #endif
  573.        return (NULL);
  574.    }
  575.  
  576.    png_set_write_fn(png_ptr, NULL, NULL, NULL);
  577.  
  578. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  579.    png_reset_filter_heuristics(png_ptr);
  580. #endif
  581.  
  582.    return (png_ptr);
  583. }
  584.  
  585.  
  586. /* Write a few rows of image data.  If the image is interlaced,
  587.  * either you will have to write the 7 sub images, or, if you
  588.  * have called png_set_interlace_handling(), you will have to
  589.  * "write" the image seven times.
  590.  */
  591. void PNGAPI
  592. png_write_rows(png_structp png_ptr, png_bytepp row,
  593.     png_uint_32 num_rows)
  594. {
  595.    png_uint_32 i; /* row counter */
  596.    png_bytepp rp; /* row pointer */
  597.  
  598.    png_debug(1, "in png_write_rows");
  599.  
  600.    if (png_ptr == NULL)
  601.       return;
  602.  
  603.    /* Loop through the rows */
  604.    for (i = 0, rp = row; i < num_rows; i++, rp++)
  605.    {
  606.       png_write_row(png_ptr, *rp);
  607.    }
  608. }
  609.  
  610. /* Write the image.  You only need to call this function once, even
  611.  * if you are writing an interlaced image.
  612.  */
  613. void PNGAPI
  614. png_write_image(png_structp png_ptr, png_bytepp image)
  615. {
  616.    png_uint_32 i; /* row index */
  617.    int pass, num_pass; /* pass variables */
  618.    png_bytepp rp; /* points to current row */
  619.  
  620.    if (png_ptr == NULL)
  621.       return;
  622.  
  623.    png_debug(1, "in png_write_image");
  624.  
  625. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  626.    /* Initialize interlace handling.  If image is not interlaced,
  627.     * this will set pass to 1
  628.     */
  629.    num_pass = png_set_interlace_handling(png_ptr);
  630. #else
  631.    num_pass = 1;
  632. #endif
  633.    /* Loop through passes */
  634.    for (pass = 0; pass < num_pass; pass++)
  635.    {
  636.       /* Loop through image */
  637.       for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
  638.       {
  639.          png_write_row(png_ptr, *rp);
  640.       }
  641.    }
  642. }
  643.  
  644. /* Called by user to write a row of image data */
  645. void PNGAPI
  646. png_write_row(png_structp png_ptr, png_const_bytep row)
  647. {
  648.    if (png_ptr == NULL)
  649.       return;
  650.  
  651.    png_debug2(1, "in png_write_row (row %u, pass %d)",
  652.       png_ptr->row_number, png_ptr->pass);
  653.  
  654.    /* Initialize transformations and other stuff if first time */
  655.    if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  656.    {
  657.       /* Make sure we wrote the header info */
  658.       if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
  659.          png_error(png_ptr,
  660.              "png_write_info was never called before png_write_row");
  661.  
  662.       /* Check for transforms that have been set but were defined out */
  663. #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
  664.       if (png_ptr->transformations & PNG_INVERT_MONO)
  665.          png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
  666. #endif
  667.  
  668. #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
  669.       if (png_ptr->transformations & PNG_FILLER)
  670.          png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
  671. #endif
  672. #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
  673.     defined(PNG_READ_PACKSWAP_SUPPORTED)
  674.       if (png_ptr->transformations & PNG_PACKSWAP)
  675.          png_warning(png_ptr,
  676.              "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
  677. #endif
  678.  
  679. #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
  680.       if (png_ptr->transformations & PNG_PACK)
  681.          png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
  682. #endif
  683.  
  684. #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
  685.       if (png_ptr->transformations & PNG_SHIFT)
  686.          png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
  687. #endif
  688.  
  689. #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
  690.       if (png_ptr->transformations & PNG_BGR)
  691.          png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
  692. #endif
  693.  
  694. #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
  695.       if (png_ptr->transformations & PNG_SWAP_BYTES)
  696.          png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
  697. #endif
  698.  
  699.       png_write_start_row(png_ptr);
  700.    }
  701.  
  702. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  703.    /* If interlaced and not interested in row, return */
  704.    if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  705.    {
  706.       switch (png_ptr->pass)
  707.       {
  708.          case 0:
  709.             if (png_ptr->row_number & 0x07)
  710.             {
  711.                png_write_finish_row(png_ptr);
  712.                return;
  713.             }
  714.             break;
  715.  
  716.          case 1:
  717.             if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  718.             {
  719.                png_write_finish_row(png_ptr);
  720.                return;
  721.             }
  722.             break;
  723.  
  724.          case 2:
  725.             if ((png_ptr->row_number & 0x07) != 4)
  726.             {
  727.                png_write_finish_row(png_ptr);
  728.                return;
  729.             }
  730.             break;
  731.  
  732.          case 3:
  733.             if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
  734.             {
  735.                png_write_finish_row(png_ptr);
  736.                return;
  737.             }
  738.             break;
  739.  
  740.          case 4:
  741.             if ((png_ptr->row_number & 0x03) != 2)
  742.             {
  743.                png_write_finish_row(png_ptr);
  744.                return;
  745.             }
  746.             break;
  747.  
  748.          case 5:
  749.             if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
  750.             {
  751.                png_write_finish_row(png_ptr);
  752.                return;
  753.             }
  754.             break;
  755.  
  756.          case 6:
  757.             if (!(png_ptr->row_number & 0x01))
  758.             {
  759.                png_write_finish_row(png_ptr);
  760.                return;
  761.             }
  762.             break;
  763.  
  764.          default: /* error: ignore it */
  765.             break;
  766.       }
  767.    }
  768. #endif
  769.  
  770.    /* Set up row info for transformations */
  771.    png_ptr->row_info.color_type = png_ptr->color_type;
  772.    png_ptr->row_info.width = png_ptr->usr_width;
  773.    png_ptr->row_info.channels = png_ptr->usr_channels;
  774.    png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
  775.    png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
  776.       png_ptr->row_info.channels);
  777.  
  778.    png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  779.       png_ptr->row_info.width);
  780.  
  781.    png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
  782.    png_debug1(3, "row_info->width = %u", png_ptr->row_info.width);
  783.    png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
  784.    png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
  785.    png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
  786.    png_debug1(3, "row_info->rowbytes = %lu",
  787.        (unsigned long)png_ptr->row_info.rowbytes);
  788.  
  789.    /* Copy user's row into buffer, leaving room for filter byte. */
  790.    png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
  791.  
  792. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  793.    /* Handle interlacing */
  794.    if (png_ptr->interlaced && png_ptr->pass < 6 &&
  795.        (png_ptr->transformations & PNG_INTERLACE))
  796.    {
  797.       png_do_write_interlace(&(png_ptr->row_info),
  798.           png_ptr->row_buf + 1, png_ptr->pass);
  799.       /* This should always get caught above, but still ... */
  800.       if (!(png_ptr->row_info.width))
  801.       {
  802.          png_write_finish_row(png_ptr);
  803.          return;
  804.       }
  805.    }
  806. #endif
  807.  
  808.    /* Handle other transformations */
  809.    if (png_ptr->transformations)
  810.       png_do_write_transformations(png_ptr);
  811.  
  812. #ifdef PNG_MNG_FEATURES_SUPPORTED
  813.    /* Write filter_method 64 (intrapixel differencing) only if
  814.     * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  815.     * 2. Libpng did not write a PNG signature (this filter_method is only
  816.     *    used in PNG datastreams that are embedded in MNG datastreams) and
  817.     * 3. The application called png_permit_mng_features with a mask that
  818.     *    included PNG_FLAG_MNG_FILTER_64 and
  819.     * 4. The filter_method is 64 and
  820.     * 5. The color_type is RGB or RGBA
  821.     */
  822.    if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  823.        (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  824.    {
  825.       /* Intrapixel differencing */
  826.       png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
  827.    }
  828. #endif
  829.  
  830.    /* Find a filter if necessary, filter the row and write it out. */
  831.    png_write_find_filter(png_ptr, &(png_ptr->row_info));
  832.  
  833.    if (png_ptr->write_row_fn != NULL)
  834.       (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  835. }
  836.  
  837. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  838. /* Set the automatic flush interval or 0 to turn flushing off */
  839. void PNGAPI
  840. png_set_flush(png_structp png_ptr, int nrows)
  841. {
  842.    png_debug(1, "in png_set_flush");
  843.  
  844.    if (png_ptr == NULL)
  845.       return;
  846.  
  847.    png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
  848. }
  849.  
  850. /* Flush the current output buffers now */
  851. void PNGAPI
  852. png_write_flush(png_structp png_ptr)
  853. {
  854.    int wrote_IDAT;
  855.  
  856.    png_debug(1, "in png_write_flush");
  857.  
  858.    if (png_ptr == NULL)
  859.       return;
  860.  
  861.    /* We have already written out all of the data */
  862.    if (png_ptr->row_number >= png_ptr->num_rows)
  863.       return;
  864.  
  865.    do
  866.    {
  867.       int ret;
  868.  
  869.       /* Compress the data */
  870.       ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
  871.       wrote_IDAT = 0;
  872.  
  873.       /* Check for compression errors */
  874.       if (ret != Z_OK)
  875.       {
  876.          if (png_ptr->zstream.msg != NULL)
  877.             png_error(png_ptr, png_ptr->zstream.msg);
  878.  
  879.          else
  880.             png_error(png_ptr, "zlib error");
  881.       }
  882.  
  883.       if (!(png_ptr->zstream.avail_out))
  884.       {
  885.          /* Write the IDAT and reset the zlib output buffer */
  886.          png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  887.          png_ptr->zstream.next_out = png_ptr->zbuf;
  888.          png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  889.          wrote_IDAT = 1;
  890.       }
  891.    } while (wrote_IDAT == 1);
  892.  
  893.    /* If there is any data left to be output, write it into a new IDAT */
  894.    if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
  895.    {
  896.       /* Write the IDAT and reset the zlib output buffer */
  897.       png_write_IDAT(png_ptr, png_ptr->zbuf,
  898.           png_ptr->zbuf_size - png_ptr->zstream.avail_out);
  899.       png_ptr->zstream.next_out = png_ptr->zbuf;
  900.       png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  901.    }
  902.    png_ptr->flush_rows = 0;
  903.    png_flush(png_ptr);
  904. }
  905. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  906.  
  907. /* Free all memory used by the write */
  908. void PNGAPI
  909. png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
  910. {
  911.    png_structp png_ptr = NULL;
  912.    png_infop info_ptr = NULL;
  913. #ifdef PNG_USER_MEM_SUPPORTED
  914.    png_free_ptr free_fn = NULL;
  915.    png_voidp mem_ptr = NULL;
  916. #endif
  917.  
  918.    png_debug(1, "in png_destroy_write_struct");
  919.  
  920.    if (png_ptr_ptr != NULL)
  921.    {
  922.       png_ptr = *png_ptr_ptr;
  923. #ifdef PNG_USER_MEM_SUPPORTED
  924.       free_fn = png_ptr->free_fn;
  925.       mem_ptr = png_ptr->mem_ptr;
  926. #endif
  927.    }
  928.  
  929. #ifdef PNG_USER_MEM_SUPPORTED
  930.    if (png_ptr != NULL)
  931.    {
  932.       free_fn = png_ptr->free_fn;
  933.       mem_ptr = png_ptr->mem_ptr;
  934.    }
  935. #endif
  936.  
  937.    if (info_ptr_ptr != NULL)
  938.       info_ptr = *info_ptr_ptr;
  939.  
  940.    if (info_ptr != NULL)
  941.    {
  942.       if (png_ptr != NULL)
  943.       {
  944.          png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  945.  
  946. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  947.          if (png_ptr->num_chunk_list)
  948.          {
  949.             png_free(png_ptr, png_ptr->chunk_list);
  950.             png_ptr->num_chunk_list = 0;
  951.          }
  952. #endif
  953.       }
  954.  
  955. #ifdef PNG_USER_MEM_SUPPORTED
  956.       png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
  957.           (png_voidp)mem_ptr);
  958. #else
  959.       png_destroy_struct((png_voidp)info_ptr);
  960. #endif
  961.       *info_ptr_ptr = NULL;
  962.    }
  963.  
  964.    if (png_ptr != NULL)
  965.    {
  966.       png_write_destroy(png_ptr);
  967. #ifdef PNG_USER_MEM_SUPPORTED
  968.       png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
  969.           (png_voidp)mem_ptr);
  970. #else
  971.       png_destroy_struct((png_voidp)png_ptr);
  972. #endif
  973.       *png_ptr_ptr = NULL;
  974.    }
  975. }
  976.  
  977.  
  978. /* Free any memory used in png_ptr struct (old method) */
  979. void /* PRIVATE */
  980. png_write_destroy(png_structp png_ptr)
  981. {
  982. #ifdef PNG_SETJMP_SUPPORTED
  983.    jmp_buf tmp_jmp; /* Save jump buffer */
  984. #endif
  985.    png_error_ptr error_fn;
  986.    png_error_ptr warning_fn;
  987.    png_voidp error_ptr;
  988. #ifdef PNG_USER_MEM_SUPPORTED
  989.    png_free_ptr free_fn;
  990. #endif
  991.  
  992.    png_debug(1, "in png_write_destroy");
  993.  
  994.    /* Free any memory zlib uses */
  995.    deflateEnd(&png_ptr->zstream);
  996.  
  997.    /* Free our memory.  png_free checks NULL for us. */
  998.    png_free(png_ptr, png_ptr->zbuf);
  999.    png_free(png_ptr, png_ptr->row_buf);
  1000. #ifdef PNG_WRITE_FILTER_SUPPORTED
  1001.    png_free(png_ptr, png_ptr->prev_row);
  1002.    png_free(png_ptr, png_ptr->sub_row);
  1003.    png_free(png_ptr, png_ptr->up_row);
  1004.    png_free(png_ptr, png_ptr->avg_row);
  1005.    png_free(png_ptr, png_ptr->paeth_row);
  1006. #endif
  1007.  
  1008. #ifdef PNG_TIME_RFC1123_SUPPORTED
  1009.    png_free(png_ptr, png_ptr->time_buffer);
  1010. #endif
  1011.  
  1012. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  1013.    /* Use this to save a little code space, it doesn't free the filter_costs */
  1014.    png_reset_filter_heuristics(png_ptr);
  1015.    png_free(png_ptr, png_ptr->filter_costs);
  1016.    png_free(png_ptr, png_ptr->inv_filter_costs);
  1017. #endif
  1018.  
  1019. #ifdef PNG_SETJMP_SUPPORTED
  1020.    /* Reset structure */
  1021.    png_memcpy(tmp_jmp, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
  1022. #endif
  1023.  
  1024.    error_fn = png_ptr->error_fn;
  1025.    warning_fn = png_ptr->warning_fn;
  1026.    error_ptr = png_ptr->error_ptr;
  1027. #ifdef PNG_USER_MEM_SUPPORTED
  1028.    free_fn = png_ptr->free_fn;
  1029. #endif
  1030.  
  1031.    png_memset(png_ptr, 0, png_sizeof(png_struct));
  1032.  
  1033.    png_ptr->error_fn = error_fn;
  1034.    png_ptr->warning_fn = warning_fn;
  1035.    png_ptr->error_ptr = error_ptr;
  1036. #ifdef PNG_USER_MEM_SUPPORTED
  1037.    png_ptr->free_fn = free_fn;
  1038. #endif
  1039.  
  1040. #ifdef PNG_SETJMP_SUPPORTED
  1041.    png_memcpy(png_ptr->png_jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
  1042. #endif
  1043. }
  1044.  
  1045. /* Allow the application to select one or more row filters to use. */
  1046. void PNGAPI
  1047. png_set_filter(png_structp png_ptr, int method, int filters)
  1048. {
  1049.    png_debug(1, "in png_set_filter");
  1050.  
  1051.    if (png_ptr == NULL)
  1052.       return;
  1053.  
  1054. #ifdef PNG_MNG_FEATURES_SUPPORTED
  1055.    if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  1056.        (method == PNG_INTRAPIXEL_DIFFERENCING))
  1057.       method = PNG_FILTER_TYPE_BASE;
  1058.  
  1059. #endif
  1060.    if (method == PNG_FILTER_TYPE_BASE)
  1061.    {
  1062.       switch (filters & (PNG_ALL_FILTERS | 0x07))
  1063.       {
  1064. #ifdef PNG_WRITE_FILTER_SUPPORTED
  1065.          case 5:
  1066.          case 6:
  1067.          case 7: png_warning(png_ptr, "Unknown row filter for method 0");
  1068. #endif /* PNG_WRITE_FILTER_SUPPORTED */
  1069.          case PNG_FILTER_VALUE_NONE:
  1070.             png_ptr->do_filter = PNG_FILTER_NONE; break;
  1071.  
  1072. #ifdef PNG_WRITE_FILTER_SUPPORTED
  1073.          case PNG_FILTER_VALUE_SUB:
  1074.             png_ptr->do_filter = PNG_FILTER_SUB; break;
  1075.  
  1076.          case PNG_FILTER_VALUE_UP:
  1077.             png_ptr->do_filter = PNG_FILTER_UP; break;
  1078.  
  1079.          case PNG_FILTER_VALUE_AVG:
  1080.             png_ptr->do_filter = PNG_FILTER_AVG; break;
  1081.  
  1082.          case PNG_FILTER_VALUE_PAETH:
  1083.             png_ptr->do_filter = PNG_FILTER_PAETH; break;
  1084.  
  1085.          default:
  1086.             png_ptr->do_filter = (png_byte)filters; break;
  1087. #else
  1088.          default:
  1089.             png_warning(png_ptr, "Unknown row filter for method 0");
  1090. #endif /* PNG_WRITE_FILTER_SUPPORTED */
  1091.       }
  1092.  
  1093.       /* If we have allocated the row_buf, this means we have already started
  1094.        * with the image and we should have allocated all of the filter buffers
  1095.        * that have been selected.  If prev_row isn't already allocated, then
  1096.        * it is too late to start using the filters that need it, since we
  1097.        * will be missing the data in the previous row.  If an application
  1098.        * wants to start and stop using particular filters during compression,
  1099.        * it should start out with all of the filters, and then add and
  1100.        * remove them after the start of compression.
  1101.        */
  1102.       if (png_ptr->row_buf != NULL)
  1103.       {
  1104. #ifdef PNG_WRITE_FILTER_SUPPORTED
  1105.          if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
  1106.          {
  1107.             png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
  1108.                 (png_ptr->rowbytes + 1));
  1109.             png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
  1110.          }
  1111.  
  1112.          if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
  1113.          {
  1114.             if (png_ptr->prev_row == NULL)
  1115.             {
  1116.                png_warning(png_ptr, "Can't add Up filter after starting");
  1117.                png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
  1118.                    ~PNG_FILTER_UP);
  1119.             }
  1120.  
  1121.             else
  1122.             {
  1123.                png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
  1124.                    (png_ptr->rowbytes + 1));
  1125.                png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
  1126.             }
  1127.          }
  1128.  
  1129.          if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
  1130.          {
  1131.             if (png_ptr->prev_row == NULL)
  1132.             {
  1133.                png_warning(png_ptr, "Can't add Average filter after starting");
  1134.                png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
  1135.                    ~PNG_FILTER_AVG);
  1136.             }
  1137.  
  1138.             else
  1139.             {
  1140.                png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
  1141.                    (png_ptr->rowbytes + 1));
  1142.                png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
  1143.             }
  1144.          }
  1145.  
  1146.          if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
  1147.              png_ptr->paeth_row == NULL)
  1148.          {
  1149.             if (png_ptr->prev_row == NULL)
  1150.             {
  1151.                png_warning(png_ptr, "Can't add Paeth filter after starting");
  1152.                png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
  1153.             }
  1154.  
  1155.             else
  1156.             {
  1157.                png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
  1158.                    (png_ptr->rowbytes + 1));
  1159.                png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
  1160.             }
  1161.          }
  1162.  
  1163.          if (png_ptr->do_filter == PNG_NO_FILTERS)
  1164. #endif /* PNG_WRITE_FILTER_SUPPORTED */
  1165.             png_ptr->do_filter = PNG_FILTER_NONE;
  1166.       }
  1167.    }
  1168.    else
  1169.       png_error(png_ptr, "Unknown custom filter method");
  1170. }
  1171.  
  1172. /* This allows us to influence the way in which libpng chooses the "best"
  1173.  * filter for the current scanline.  While the "minimum-sum-of-absolute-
  1174.  * differences metric is relatively fast and effective, there is some
  1175.  * question as to whether it can be improved upon by trying to keep the
  1176.  * filtered data going to zlib more consistent, hopefully resulting in
  1177.  * better compression.
  1178.  */
  1179. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED      /* GRR 970116 */
  1180. /* Conveneince reset API. */
  1181. static void
  1182. png_reset_filter_heuristics(png_structp png_ptr)
  1183. {
  1184.    /* Clear out any old values in the 'weights' - this must be done because if
  1185.     * the app calls set_filter_heuristics multiple times with different
  1186.     * 'num_weights' values we would otherwise potentially have wrong sized
  1187.     * arrays.
  1188.     */
  1189.    png_ptr->num_prev_filters = 0;
  1190.    png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
  1191.    if (png_ptr->prev_filters != NULL)
  1192.    {
  1193.       png_bytep old = png_ptr->prev_filters;
  1194.       png_ptr->prev_filters = NULL;
  1195.       png_free(png_ptr, old);
  1196.    }
  1197.    if (png_ptr->filter_weights != NULL)
  1198.    {
  1199.       png_uint_16p old = png_ptr->filter_weights;
  1200.       png_ptr->filter_weights = NULL;
  1201.       png_free(png_ptr, old);
  1202.    }
  1203.  
  1204.    if (png_ptr->inv_filter_weights != NULL)
  1205.    {
  1206.       png_uint_16p old = png_ptr->inv_filter_weights;
  1207.       png_ptr->inv_filter_weights = NULL;
  1208.       png_free(png_ptr, old);
  1209.    }
  1210.  
  1211.    /* Leave the filter_costs - this array is fixed size. */
  1212. }
  1213.  
  1214. static int
  1215. png_init_filter_heuristics(png_structp png_ptr, int heuristic_method,
  1216.    int num_weights)
  1217. {
  1218.    if (png_ptr == NULL)
  1219.       return 0;
  1220.  
  1221.    /* Clear out the arrays */
  1222.    png_reset_filter_heuristics(png_ptr);
  1223.  
  1224.    /* Check arguments; the 'reset' function makes the correct settings for the
  1225.     * unweighted case, but we must handle the weight case by initializing the
  1226.     * arrays for the caller.
  1227.     */
  1228.    if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1229.    {
  1230.       int i;
  1231.  
  1232.       if (num_weights > 0)
  1233.       {
  1234.          png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
  1235.              (png_uint_32)(png_sizeof(png_byte) * num_weights));
  1236.  
  1237.          /* To make sure that the weighting starts out fairly */
  1238.          for (i = 0; i < num_weights; i++)
  1239.          {
  1240.             png_ptr->prev_filters[i] = 255;
  1241.          }
  1242.  
  1243.          png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
  1244.              (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
  1245.  
  1246.          png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
  1247.              (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
  1248.  
  1249.          for (i = 0; i < num_weights; i++)
  1250.          {
  1251.             png_ptr->inv_filter_weights[i] =
  1252.             png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1253.          }
  1254.  
  1255.          /* Safe to set this now */
  1256.          png_ptr->num_prev_filters = (png_byte)num_weights;
  1257.       }
  1258.  
  1259.       /* If, in the future, there are other filter methods, this would
  1260.        * need to be based on png_ptr->filter.
  1261.        */
  1262.       if (png_ptr->filter_costs == NULL)
  1263.       {
  1264.          png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
  1265.              (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
  1266.  
  1267.          png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
  1268.              (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
  1269.       }
  1270.  
  1271.       for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
  1272.       {
  1273.          png_ptr->inv_filter_costs[i] =
  1274.          png_ptr->filter_costs[i] = PNG_COST_FACTOR;
  1275.       }
  1276.  
  1277.       /* All the arrays are inited, safe to set this: */
  1278.       png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
  1279.  
  1280.       /* Return the 'ok' code. */
  1281.       return 1;
  1282.    }
  1283.    else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
  1284.       heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
  1285.    {
  1286.       return 1;
  1287.    }
  1288.    else
  1289.    {
  1290.       png_warning(png_ptr, "Unknown filter heuristic method");
  1291.       return 0;
  1292.    }
  1293. }
  1294.  
  1295. /* Provide floating and fixed point APIs */
  1296. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1297. void PNGAPI
  1298. png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
  1299.     int num_weights, png_const_doublep filter_weights,
  1300.     png_const_doublep filter_costs)
  1301. {
  1302.    png_debug(1, "in png_set_filter_heuristics");
  1303.  
  1304.    /* The internal API allocates all the arrays and ensures that the elements of
  1305.     * those arrays are set to the default value.
  1306.     */
  1307.    if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
  1308.       return;
  1309.  
  1310.    /* If using the weighted method copy in the weights. */
  1311.    if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1312.    {
  1313.       int i;
  1314.       for (i = 0; i < num_weights; i++)
  1315.       {
  1316.          if (filter_weights[i] <= 0.0)
  1317.          {
  1318.             png_ptr->inv_filter_weights[i] =
  1319.             png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1320.          }
  1321.  
  1322.          else
  1323.          {
  1324.             png_ptr->inv_filter_weights[i] =
  1325.                 (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
  1326.  
  1327.             png_ptr->filter_weights[i] =
  1328.                 (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
  1329.          }
  1330.       }
  1331.  
  1332.       /* Here is where we set the relative costs of the different filters.  We
  1333.        * should take the desired compression level into account when setting
  1334.        * the costs, so that Paeth, for instance, has a high relative cost at low
  1335.        * compression levels, while it has a lower relative cost at higher
  1336.        * compression settings.  The filter types are in order of increasing
  1337.        * relative cost, so it would be possible to do this with an algorithm.
  1338.        */
  1339.       for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
  1340.       {
  1341.          png_ptr->inv_filter_costs[i] =
  1342.              (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
  1343.  
  1344.          png_ptr->filter_costs[i] =
  1345.              (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
  1346.       }
  1347.    }
  1348. }
  1349. #endif /* FLOATING_POINT */
  1350.  
  1351. #ifdef PNG_FIXED_POINT_SUPPORTED
  1352. void PNGAPI
  1353. png_set_filter_heuristics_fixed(png_structp png_ptr, int heuristic_method,
  1354.     int num_weights, png_const_fixed_point_p filter_weights,
  1355.     png_const_fixed_point_p filter_costs)
  1356. {
  1357.    png_debug(1, "in png_set_filter_heuristics_fixed");
  1358.  
  1359.    /* The internal API allocates all the arrays and ensures that the elements of
  1360.     * those arrays are set to the default value.
  1361.     */
  1362.    if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
  1363.       return;
  1364.  
  1365.    /* If using the weighted method copy in the weights. */
  1366.    if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1367.    {
  1368.       int i;
  1369.       for (i = 0; i < num_weights; i++)
  1370.       {
  1371.          if (filter_weights[i] <= 0)
  1372.          {
  1373.             png_ptr->inv_filter_weights[i] =
  1374.             png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1375.          }
  1376.  
  1377.          else
  1378.          {
  1379.             png_ptr->inv_filter_weights[i] = (png_uint_16)
  1380.                ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
  1381.  
  1382.             png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
  1383.                PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
  1384.          }
  1385.       }
  1386.  
  1387.       /* Here is where we set the relative costs of the different filters.  We
  1388.        * should take the desired compression level into account when setting
  1389.        * the costs, so that Paeth, for instance, has a high relative cost at low
  1390.        * compression levels, while it has a lower relative cost at higher
  1391.        * compression settings.  The filter types are in order of increasing
  1392.        * relative cost, so it would be possible to do this with an algorithm.
  1393.        */
  1394.       for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
  1395.          if (filter_costs[i] >= PNG_FP_1)
  1396.       {
  1397.          png_uint_32 tmp;
  1398.  
  1399.          /* Use a 32 bit unsigned temporary here because otherwise the
  1400.           * intermediate value will be a 32 bit *signed* integer (ANSI rules)
  1401.           * and this will get the wrong answer on division.
  1402.           */
  1403.          tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
  1404.          tmp /= filter_costs[i];
  1405.  
  1406.          png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
  1407.  
  1408.          tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
  1409.          tmp /= PNG_FP_1;
  1410.  
  1411.          png_ptr->filter_costs[i] = (png_uint_16)tmp;
  1412.       }
  1413.    }
  1414. }
  1415. #endif /* FIXED_POINT */
  1416. #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
  1417.  
  1418. void PNGAPI
  1419. png_set_compression_level(png_structp png_ptr, int level)
  1420. {
  1421.    png_debug(1, "in png_set_compression_level");
  1422.  
  1423.    if (png_ptr == NULL)
  1424.       return;
  1425.  
  1426.    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
  1427.    png_ptr->zlib_level = level;
  1428. }
  1429.  
  1430. void PNGAPI
  1431. png_set_compression_mem_level(png_structp png_ptr, int mem_level)
  1432. {
  1433.    png_debug(1, "in png_set_compression_mem_level");
  1434.  
  1435.    if (png_ptr == NULL)
  1436.       return;
  1437.  
  1438.    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
  1439.    png_ptr->zlib_mem_level = mem_level;
  1440. }
  1441.  
  1442. void PNGAPI
  1443. png_set_compression_strategy(png_structp png_ptr, int strategy)
  1444. {
  1445.    png_debug(1, "in png_set_compression_strategy");
  1446.  
  1447.    if (png_ptr == NULL)
  1448.       return;
  1449.  
  1450.    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
  1451.    png_ptr->zlib_strategy = strategy;
  1452. }
  1453.  
  1454. void PNGAPI
  1455. png_set_compression_window_bits(png_structp png_ptr, int window_bits)
  1456. {
  1457.    if (png_ptr == NULL)
  1458.       return;
  1459.  
  1460.    if (window_bits > 15)
  1461.       png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
  1462.  
  1463.    else if (window_bits < 8)
  1464.       png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
  1465.  
  1466. #ifndef WBITS_8_OK
  1467.    /* Avoid libpng bug with 256-byte windows */
  1468.    if (window_bits == 8)
  1469.       {
  1470.         png_warning(png_ptr, "Compression window is being reset to 512");
  1471.         window_bits = 9;
  1472.       }
  1473.  
  1474. #endif
  1475.    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
  1476.    png_ptr->zlib_window_bits = window_bits;
  1477. }
  1478.  
  1479. void PNGAPI
  1480. png_set_compression_method(png_structp png_ptr, int method)
  1481. {
  1482.    png_debug(1, "in png_set_compression_method");
  1483.  
  1484.    if (png_ptr == NULL)
  1485.       return;
  1486.  
  1487.    if (method != 8)
  1488.       png_warning(png_ptr, "Only compression method 8 is supported by PNG");
  1489.  
  1490.    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
  1491.    png_ptr->zlib_method = method;
  1492. }
  1493.  
  1494. void PNGAPI
  1495. png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
  1496. {
  1497.    if (png_ptr == NULL)
  1498.       return;
  1499.  
  1500.    png_ptr->write_row_fn = write_row_fn;
  1501. }
  1502.  
  1503. #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  1504. void PNGAPI
  1505. png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
  1506.     write_user_transform_fn)
  1507. {
  1508.    png_debug(1, "in png_set_write_user_transform_fn");
  1509.  
  1510.    if (png_ptr == NULL)
  1511.       return;
  1512.  
  1513.    png_ptr->transformations |= PNG_USER_TRANSFORM;
  1514.    png_ptr->write_user_transform_fn = write_user_transform_fn;
  1515. }
  1516. #endif
  1517.  
  1518.  
  1519. #ifdef PNG_INFO_IMAGE_SUPPORTED
  1520. void PNGAPI
  1521. png_write_png(png_structp png_ptr, png_infop info_ptr,
  1522.     int transforms, voidp params)
  1523. {
  1524.    if (png_ptr == NULL || info_ptr == NULL)
  1525.       return;
  1526.  
  1527.    /* Write the file header information. */
  1528.    png_write_info(png_ptr, info_ptr);
  1529.  
  1530.    /* ------ these transformations don't touch the info structure ------- */
  1531.  
  1532. #ifdef PNG_WRITE_INVERT_SUPPORTED
  1533.    /* Invert monochrome pixels */
  1534.    if (transforms & PNG_TRANSFORM_INVERT_MONO)
  1535.       png_set_invert_mono(png_ptr);
  1536. #endif
  1537.  
  1538. #ifdef PNG_WRITE_SHIFT_SUPPORTED
  1539.    /* Shift the pixels up to a legal bit depth and fill in
  1540.     * as appropriate to correctly scale the image.
  1541.     */
  1542.    if ((transforms & PNG_TRANSFORM_SHIFT)
  1543.        && (info_ptr->valid & PNG_INFO_sBIT))
  1544.       png_set_shift(png_ptr, &info_ptr->sig_bit);
  1545. #endif
  1546.  
  1547. #ifdef PNG_WRITE_PACK_SUPPORTED
  1548.    /* Pack pixels into bytes */
  1549.    if (transforms & PNG_TRANSFORM_PACKING)
  1550.        png_set_packing(png_ptr);
  1551. #endif
  1552.  
  1553. #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
  1554.    /* Swap location of alpha bytes from ARGB to RGBA */
  1555.    if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
  1556.       png_set_swap_alpha(png_ptr);
  1557. #endif
  1558.  
  1559. #ifdef PNG_WRITE_FILLER_SUPPORTED
  1560.    /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
  1561.    if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
  1562.       png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
  1563.  
  1564.    else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
  1565.       png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
  1566. #endif
  1567.  
  1568. #ifdef PNG_WRITE_BGR_SUPPORTED
  1569.    /* Flip BGR pixels to RGB */
  1570.    if (transforms & PNG_TRANSFORM_BGR)
  1571.       png_set_bgr(png_ptr);
  1572. #endif
  1573.  
  1574. #ifdef PNG_WRITE_SWAP_SUPPORTED
  1575.    /* Swap bytes of 16-bit files to most significant byte first */
  1576.    if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
  1577.       png_set_swap(png_ptr);
  1578. #endif
  1579.  
  1580. #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
  1581.    /* Swap bits of 1, 2, 4 bit packed pixel formats */
  1582.    if (transforms & PNG_TRANSFORM_PACKSWAP)
  1583.       png_set_packswap(png_ptr);
  1584. #endif
  1585.  
  1586. #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  1587.    /* Invert the alpha channel from opacity to transparency */
  1588.    if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
  1589.       png_set_invert_alpha(png_ptr);
  1590. #endif
  1591.  
  1592.    /* ----------------------- end of transformations ------------------- */
  1593.  
  1594.    /* Write the bits */
  1595.    if (info_ptr->valid & PNG_INFO_IDAT)
  1596.        png_write_image(png_ptr, info_ptr->row_pointers);
  1597.  
  1598.    /* It is REQUIRED to call this to finish writing the rest of the file */
  1599.    png_write_end(png_ptr, info_ptr);
  1600.  
  1601.    PNG_UNUSED(transforms)   /* Quiet compiler warnings */
  1602.    PNG_UNUSED(params)
  1603. }
  1604. #endif
  1605. #endif /* PNG_WRITE_SUPPORTED */
  1606.