Back to index

plt-scheme  4.2.1
pngwrite.c
Go to the documentation of this file.
00001 
00002 /* pngwrite.c - general routines to write a PNG file
00003  *
00004  * libpng 1.2.5 - October 3, 2002
00005  * For conditions of distribution and use, see copyright notice in png.h
00006  * Copyright (c) 1998-2002 Glenn Randers-Pehrson
00007  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
00008  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
00009  */
00010 
00011 /* get internal access to png.h */
00012 #define PNG_INTERNAL
00013 #include "png.h"
00014 #ifdef PNG_WRITE_SUPPORTED
00015 
00016 /* Writes all the PNG information.  This is the suggested way to use the
00017  * library.  If you have a new chunk to add, make a function to write it,
00018  * and put it in the correct location here.  If you want the chunk written
00019  * after the image data, put it in png_write_end().  I strongly encourage
00020  * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
00021  * the chunk, as that will keep the code from breaking if you want to just
00022  * write a plain PNG file.  If you have long comments, I suggest writing
00023  * them in png_write_end(), and compressing them.
00024  */
00025 void PNGAPI
00026 png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
00027 {
00028    png_debug(1, "in png_write_info_before_PLTE\n");
00029    if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
00030    {
00031    png_write_sig(png_ptr); /* write PNG signature */
00032 #if defined(PNG_MNG_FEATURES_SUPPORTED)
00033    if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
00034    {
00035       png_warning(png_ptr,"MNG features are not allowed in a PNG datastream\n");
00036       png_ptr->mng_features_permitted=0;
00037    }
00038 #endif
00039    /* write IHDR information. */
00040    png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
00041       info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
00042       info_ptr->filter_type,
00043 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
00044       info_ptr->interlace_type);
00045 #else
00046       0);
00047 #endif
00048    /* the rest of these check to see if the valid field has the appropriate
00049       flag set, and if it does, writes the chunk. */
00050 #if defined(PNG_WRITE_gAMA_SUPPORTED)
00051    if (info_ptr->valid & PNG_INFO_gAMA)
00052    {
00053 #  ifdef PNG_FLOATING_POINT_SUPPORTED
00054       png_write_gAMA(png_ptr, info_ptr->gamma);
00055 #else
00056 #ifdef PNG_FIXED_POINT_SUPPORTED
00057       png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
00058 #  endif
00059 #endif
00060    }
00061 #endif
00062 #if defined(PNG_WRITE_sRGB_SUPPORTED)
00063    if (info_ptr->valid & PNG_INFO_sRGB)
00064       png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
00065 #endif
00066 #if defined(PNG_WRITE_iCCP_SUPPORTED)
00067    if (info_ptr->valid & PNG_INFO_iCCP)
00068       png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
00069                      info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
00070 #endif
00071 #if defined(PNG_WRITE_sBIT_SUPPORTED)
00072    if (info_ptr->valid & PNG_INFO_sBIT)
00073       png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
00074 #endif
00075 #if defined(PNG_WRITE_cHRM_SUPPORTED)
00076    if (info_ptr->valid & PNG_INFO_cHRM)
00077    {
00078 #ifdef PNG_FLOATING_POINT_SUPPORTED
00079       png_write_cHRM(png_ptr,
00080          info_ptr->x_white, info_ptr->y_white,
00081          info_ptr->x_red, info_ptr->y_red,
00082          info_ptr->x_green, info_ptr->y_green,
00083          info_ptr->x_blue, info_ptr->y_blue);
00084 #else
00085 #  ifdef PNG_FIXED_POINT_SUPPORTED
00086       png_write_cHRM_fixed(png_ptr,
00087          info_ptr->int_x_white, info_ptr->int_y_white,
00088          info_ptr->int_x_red, info_ptr->int_y_red,
00089          info_ptr->int_x_green, info_ptr->int_y_green,
00090          info_ptr->int_x_blue, info_ptr->int_y_blue);
00091 #  endif
00092 #endif
00093    }
00094 #endif
00095 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
00096    if (info_ptr->unknown_chunks_num)
00097    {
00098        png_unknown_chunk *up;
00099 
00100        png_debug(5, "writing extra chunks\n");
00101 
00102        for (up = info_ptr->unknown_chunks;
00103             up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
00104             up++)
00105        {
00106          int keep=png_handle_as_unknown(png_ptr, up->name);
00107          if (keep != HANDLE_CHUNK_NEVER &&
00108             up->location && (!(up->location & PNG_HAVE_PLTE)) &&
00109             ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
00110             (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
00111          {
00112             png_write_chunk(png_ptr, up->name, up->data, up->size);
00113          }
00114        }
00115    }
00116 #endif
00117       png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
00118    }
00119 }
00120 
00121 void PNGAPI
00122 png_write_info(png_structp png_ptr, png_infop info_ptr)
00123 {
00124 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
00125    int i;
00126 #endif
00127 
00128    png_debug(1, "in png_write_info\n");
00129 
00130    png_write_info_before_PLTE(png_ptr, info_ptr);
00131 
00132    if (info_ptr->valid & PNG_INFO_PLTE)
00133       png_write_PLTE(png_ptr, info_ptr->palette,
00134          (png_uint_32)info_ptr->num_palette);
00135    else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
00136       png_error(png_ptr, "Valid palette required for paletted images\n");
00137 
00138 #if defined(PNG_WRITE_tRNS_SUPPORTED)
00139    if (info_ptr->valid & PNG_INFO_tRNS)
00140       {
00141 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
00142          /* invert the alpha channel (in tRNS) */
00143          if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
00144             info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
00145          {
00146             int j;
00147             for (j=0; j<(int)info_ptr->num_trans; j++)
00148                info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
00149          }
00150 #endif
00151       png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
00152          info_ptr->num_trans, info_ptr->color_type);
00153       }
00154 #endif
00155 #if defined(PNG_WRITE_bKGD_SUPPORTED)
00156    if (info_ptr->valid & PNG_INFO_bKGD)
00157       png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
00158 #endif
00159 #if defined(PNG_WRITE_hIST_SUPPORTED)
00160    if (info_ptr->valid & PNG_INFO_hIST)
00161       png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
00162 #endif
00163 #if defined(PNG_WRITE_oFFs_SUPPORTED)
00164    if (info_ptr->valid & PNG_INFO_oFFs)
00165       png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
00166          info_ptr->offset_unit_type);
00167 #endif
00168 #if defined(PNG_WRITE_pCAL_SUPPORTED)
00169    if (info_ptr->valid & PNG_INFO_pCAL)
00170       png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
00171          info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
00172          info_ptr->pcal_units, info_ptr->pcal_params);
00173 #endif
00174 #if defined(PNG_WRITE_sCAL_SUPPORTED)
00175    if (info_ptr->valid & PNG_INFO_sCAL)
00176 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
00177       png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
00178           info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
00179 #else
00180 #ifdef PNG_FIXED_POINT_SUPPORTED
00181       png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
00182           info_ptr->scal_s_width, info_ptr->scal_s_height);
00183 #else
00184       png_warning(png_ptr,
00185           "png_write_sCAL not supported; sCAL chunk not written.\n");
00186 #endif
00187 #endif
00188 #endif
00189 #if defined(PNG_WRITE_pHYs_SUPPORTED)
00190    if (info_ptr->valid & PNG_INFO_pHYs)
00191       png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
00192          info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
00193 #endif
00194 #if defined(PNG_WRITE_tIME_SUPPORTED)
00195    if (info_ptr->valid & PNG_INFO_tIME)
00196    {
00197       png_write_tIME(png_ptr, &(info_ptr->mod_time));
00198       png_ptr->mode |= PNG_WROTE_tIME;
00199    }
00200 #endif
00201 #if defined(PNG_WRITE_sPLT_SUPPORTED)
00202    if (info_ptr->valid & PNG_INFO_sPLT)
00203      for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
00204        png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
00205 #endif
00206 #if defined(PNG_WRITE_TEXT_SUPPORTED)
00207    /* Check to see if we need to write text chunks */
00208    for (i = 0; i < info_ptr->num_text; i++)
00209    {
00210       png_debug2(2, "Writing header text chunk %d, type %d\n", i,
00211          info_ptr->text[i].compression);
00212       /* an internationalized chunk? */
00213       if (info_ptr->text[i].compression > 0)
00214       {
00215 #if defined(PNG_WRITE_iTXt_SUPPORTED)
00216           /* write international chunk */
00217           png_write_iTXt(png_ptr,
00218                          info_ptr->text[i].compression,
00219                          info_ptr->text[i].key,
00220                          info_ptr->text[i].lang,
00221                          info_ptr->text[i].lang_key,
00222                          info_ptr->text[i].text);
00223 #else
00224           png_warning(png_ptr, "Unable to write international text\n");
00225 #endif
00226           /* Mark this chunk as written */
00227           info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
00228       }
00229       /* If we want a compressed text chunk */
00230       else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
00231       {
00232 #if defined(PNG_WRITE_zTXt_SUPPORTED)
00233          /* write compressed chunk */
00234          png_write_zTXt(png_ptr, info_ptr->text[i].key,
00235             info_ptr->text[i].text, 0,
00236             info_ptr->text[i].compression);
00237 #else
00238          png_warning(png_ptr, "Unable to write compressed text\n");
00239 #endif
00240          /* Mark this chunk as written */
00241          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
00242       }
00243       else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
00244       {
00245 #if defined(PNG_WRITE_tEXt_SUPPORTED)
00246          /* write uncompressed chunk */
00247          png_write_tEXt(png_ptr, info_ptr->text[i].key,
00248                          info_ptr->text[i].text,
00249                          0);
00250 #else
00251          png_warning(png_ptr, "Unable to write uncompressed text\n");
00252 #endif
00253          /* Mark this chunk as written */
00254          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
00255       }
00256    }
00257 #endif
00258 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
00259    if (info_ptr->unknown_chunks_num)
00260    {
00261        png_unknown_chunk *up;
00262 
00263        png_debug(5, "writing extra chunks\n");
00264 
00265        for (up = info_ptr->unknown_chunks;
00266             up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
00267             up++)
00268        {
00269          int keep=png_handle_as_unknown(png_ptr, up->name);
00270          if (keep != HANDLE_CHUNK_NEVER &&
00271             up->location && (up->location & PNG_HAVE_PLTE) &&
00272             !(up->location & PNG_HAVE_IDAT) &&
00273             ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
00274             (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
00275          {
00276             png_write_chunk(png_ptr, up->name, up->data, up->size);
00277          }
00278        }
00279    }
00280 #endif
00281 }
00282 
00283 /* Writes the end of the PNG file.  If you don't want to write comments or
00284  * time information, you can pass NULL for info.  If you already wrote these
00285  * in png_write_info(), do not write them again here.  If you have long
00286  * comments, I suggest writing them here, and compressing them.
00287  */
00288 void PNGAPI
00289 png_write_end(png_structp png_ptr, png_infop info_ptr)
00290 {
00291    png_debug(1, "in png_write_end\n");
00292    if (!(png_ptr->mode & PNG_HAVE_IDAT))
00293       png_error(png_ptr, "No IDATs written into file");
00294 
00295    /* see if user wants us to write information chunks */
00296    if (info_ptr != NULL)
00297    {
00298 #if defined(PNG_WRITE_TEXT_SUPPORTED)
00299       int i; /* local index variable */
00300 #endif
00301 #if defined(PNG_WRITE_tIME_SUPPORTED)
00302       /* check to see if user has supplied a time chunk */
00303       if ((info_ptr->valid & PNG_INFO_tIME) &&
00304          !(png_ptr->mode & PNG_WROTE_tIME))
00305          png_write_tIME(png_ptr, &(info_ptr->mod_time));
00306 #endif
00307 #if defined(PNG_WRITE_TEXT_SUPPORTED)
00308       /* loop through comment chunks */
00309       for (i = 0; i < info_ptr->num_text; i++)
00310       {
00311          png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
00312             info_ptr->text[i].compression);
00313          /* an internationalized chunk? */
00314          if (info_ptr->text[i].compression > 0)
00315          {
00316 #if defined(PNG_WRITE_iTXt_SUPPORTED)
00317              /* write international chunk */
00318              png_write_iTXt(png_ptr,
00319                          info_ptr->text[i].compression,
00320                          info_ptr->text[i].key,
00321                          info_ptr->text[i].lang,
00322                          info_ptr->text[i].lang_key,
00323                          info_ptr->text[i].text);
00324 #else
00325              png_warning(png_ptr, "Unable to write international text\n");
00326 #endif
00327              /* Mark this chunk as written */
00328              info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
00329          }
00330          else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
00331          {
00332 #if defined(PNG_WRITE_zTXt_SUPPORTED)
00333             /* write compressed chunk */
00334             png_write_zTXt(png_ptr, info_ptr->text[i].key,
00335                info_ptr->text[i].text, 0,
00336                info_ptr->text[i].compression);
00337 #else
00338             png_warning(png_ptr, "Unable to write compressed text\n");
00339 #endif
00340             /* Mark this chunk as written */
00341             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
00342          }
00343          else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
00344          {
00345 #if defined(PNG_WRITE_tEXt_SUPPORTED)
00346             /* write uncompressed chunk */
00347             png_write_tEXt(png_ptr, info_ptr->text[i].key,
00348                info_ptr->text[i].text, 0);
00349 #else
00350             png_warning(png_ptr, "Unable to write uncompressed text\n");
00351 #endif
00352 
00353             /* Mark this chunk as written */
00354             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
00355          }
00356       }
00357 #endif
00358 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
00359    if (info_ptr->unknown_chunks_num)
00360    {
00361        png_unknown_chunk *up;
00362 
00363        png_debug(5, "writing extra chunks\n");
00364 
00365        for (up = info_ptr->unknown_chunks;
00366             up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
00367             up++)
00368        {
00369          int keep=png_handle_as_unknown(png_ptr, up->name);
00370          if (keep != HANDLE_CHUNK_NEVER &&
00371             up->location && (up->location & PNG_AFTER_IDAT) &&
00372             ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
00373             (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
00374          {
00375             png_write_chunk(png_ptr, up->name, up->data, up->size);
00376          }
00377        }
00378    }
00379 #endif
00380    }
00381 
00382    png_ptr->mode |= PNG_AFTER_IDAT;
00383 
00384    /* write end of PNG file */
00385    png_write_IEND(png_ptr);
00386 #if 0
00387 /* This flush, added in libpng-1.0.8,  causes some applications to crash
00388    because they do not set png_ptr->output_flush_fn */
00389    png_flush(png_ptr);
00390 #endif
00391 }
00392 
00393 #if defined(PNG_WRITE_tIME_SUPPORTED)
00394 #if !defined(_WIN32_WCE)
00395 /* "time.h" functions are not supported on WindowsCE */
00396 void PNGAPI
00397 png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
00398 {
00399    png_debug(1, "in png_convert_from_struct_tm\n");
00400    ptime->year = (png_uint_16)(1900 + ttime->tm_year);
00401    ptime->month = (png_byte)(ttime->tm_mon + 1);
00402    ptime->day = (png_byte)ttime->tm_mday;
00403    ptime->hour = (png_byte)ttime->tm_hour;
00404    ptime->minute = (png_byte)ttime->tm_min;
00405    ptime->second = (png_byte)ttime->tm_sec;
00406 }
00407 
00408 void PNGAPI
00409 png_convert_from_time_t(png_timep ptime, time_t ttime)
00410 {
00411    struct tm *tbuf;
00412 
00413    png_debug(1, "in png_convert_from_time_t\n");
00414    tbuf = gmtime(&ttime);
00415    png_convert_from_struct_tm(ptime, tbuf);
00416 }
00417 #endif
00418 #endif
00419 
00420 /* Initialize png_ptr structure, and allocate any memory needed */
00421 png_structp PNGAPI
00422 png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
00423    png_error_ptr error_fn, png_error_ptr warn_fn)
00424 {
00425 #ifdef PNG_USER_MEM_SUPPORTED
00426    return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
00427       warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
00428 }
00429 
00430 /* Alternate initialize png_ptr structure, and allocate any memory needed */
00431 png_structp PNGAPI
00432 png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
00433    png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
00434    png_malloc_ptr malloc_fn, png_free_ptr free_fn)
00435 {
00436 #endif /* PNG_USER_MEM_SUPPORTED */
00437    png_structp png_ptr;
00438 #ifdef PNG_SETJMP_SUPPORTED
00439 #ifdef USE_FAR_KEYWORD
00440    jmp_buf jmpbuf;
00441 #endif
00442 #endif
00443    int i;
00444    png_debug(1, "in png_create_write_struct\n");
00445 #ifdef PNG_USER_MEM_SUPPORTED
00446    png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
00447       (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
00448 #else
00449    png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
00450 #endif /* PNG_USER_MEM_SUPPORTED */
00451    if (png_ptr == NULL)
00452       return (NULL);
00453 
00454 #if !defined(PNG_1_0_X)
00455 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
00456    png_init_mmx_flags(png_ptr);   /* 1.2.0 addition */
00457 #endif
00458 #endif /* PNG_1_0_X */
00459 
00460 #ifdef PNG_SETJMP_SUPPORTED
00461 #ifdef USE_FAR_KEYWORD
00462    if (setjmp(jmpbuf))
00463 #else
00464    if (setjmp(png_ptr->jmpbuf))
00465 #endif
00466    {
00467       png_free(png_ptr, png_ptr->zbuf);
00468       png_ptr->zbuf=NULL;
00469       png_destroy_struct(png_ptr);
00470       return (NULL);
00471    }
00472 #ifdef USE_FAR_KEYWORD
00473    png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
00474 #endif
00475 #endif
00476 
00477 #ifdef PNG_USER_MEM_SUPPORTED
00478    png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
00479 #endif /* PNG_USER_MEM_SUPPORTED */
00480    png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
00481 
00482    i=0;
00483    do
00484    {
00485      if(user_png_ver[i] != png_libpng_ver[i])
00486         png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
00487    } while (png_libpng_ver[i++]);
00488 
00489    if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
00490    {
00491      /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
00492       * we must recompile any applications that use any older library version.
00493       * For versions after libpng 1.0, we will be compatible, so we need
00494       * only check the first digit.
00495       */
00496      if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
00497          (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
00498          (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
00499      {
00500 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
00501         char msg[80];
00502         if (user_png_ver)
00503         {
00504           sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
00505              user_png_ver);
00506           png_warning(png_ptr, msg);
00507         }
00508         sprintf(msg, "Application  is  running with png.c from libpng-%.20s",
00509            png_libpng_ver);
00510         png_warning(png_ptr, msg);
00511 #endif
00512 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00513         png_ptr->flags=0;
00514 #endif
00515         png_error(png_ptr,
00516            "Incompatible libpng version in application and library");
00517      }
00518    }
00519 
00520    /* initialize zbuf - compression buffer */
00521    png_ptr->zbuf_size = PNG_ZBUF_SIZE;
00522    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
00523       (png_uint_32)png_ptr->zbuf_size);
00524 
00525    png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
00526       png_flush_ptr_NULL);
00527 
00528 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
00529    png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
00530       1, png_doublep_NULL, png_doublep_NULL);
00531 #endif
00532 
00533 #ifdef PNG_SETJMP_SUPPORTED
00534 /* Applications that neglect to set up their own setjmp() and then encounter
00535    a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
00536    abort instead of returning. */
00537 #ifdef USE_FAR_KEYWORD
00538    if (setjmp(jmpbuf))
00539       PNG_ABORT();
00540    png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
00541 #else
00542    if (setjmp(png_ptr->jmpbuf))
00543       PNG_ABORT();
00544 #endif
00545 #endif
00546    return (png_ptr);
00547 }
00548 
00549 /* Initialize png_ptr structure, and allocate any memory needed */
00550 #undef png_write_init
00551 void PNGAPI
00552 png_write_init(png_structp png_ptr)
00553 {
00554    /* We only come here via pre-1.0.7-compiled applications */
00555    png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
00556 }
00557 
00558 void PNGAPI
00559 png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
00560    png_size_t png_struct_size, png_size_t png_info_size)
00561 {
00562    /* We only come here via pre-1.0.12-compiled applications */
00563 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
00564    if(sizeof(png_struct) > png_struct_size || sizeof(png_info) > png_info_size)
00565    {
00566       char msg[80];
00567       png_ptr->warning_fn=NULL;
00568       if (user_png_ver)
00569       {
00570         sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
00571            user_png_ver);
00572         png_warning(png_ptr, msg);
00573       }
00574       sprintf(msg, "Application  is  running with png.c from libpng-%.20s",
00575          png_libpng_ver);
00576       png_warning(png_ptr, msg);
00577    }
00578 #endif
00579    if(sizeof(png_struct) > png_struct_size)
00580      {
00581        png_ptr->error_fn=NULL;
00582 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00583        png_ptr->flags=0;
00584 #endif
00585        png_error(png_ptr,
00586        "The png struct allocated by the application for writing is too small.");
00587      }
00588    if(sizeof(png_info) > png_info_size)
00589      {
00590        png_ptr->error_fn=NULL;
00591 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00592        png_ptr->flags=0;
00593 #endif
00594        png_error(png_ptr,
00595        "The info struct allocated by the application for writing is too small.");
00596      }
00597    png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
00598 }
00599 
00600 
00601 void PNGAPI
00602 png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
00603    png_size_t png_struct_size)
00604 {
00605    png_structp png_ptr=*ptr_ptr;
00606 #ifdef PNG_SETJMP_SUPPORTED
00607    jmp_buf tmp_jmp; /* to save current jump buffer */
00608 #endif
00609    int i = 0;
00610    do
00611    {
00612      if (user_png_ver[i] != png_libpng_ver[i])
00613      {
00614 #ifdef PNG_LEGACY_SUPPORTED
00615        png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
00616 #else
00617        png_ptr->warning_fn=NULL;
00618        png_warning(png_ptr,
00619      "Application uses deprecated png_write_init() and should be recompiled.");
00620        break;
00621 #endif
00622      }
00623    } while (png_libpng_ver[i++]);
00624 
00625    png_debug(1, "in png_write_init_3\n");
00626 
00627 #ifdef PNG_SETJMP_SUPPORTED
00628    /* save jump buffer and error functions */
00629    png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
00630 #endif
00631 
00632    if (sizeof(png_struct) > png_struct_size)
00633      {
00634        png_destroy_struct(png_ptr);
00635        png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
00636        *ptr_ptr = png_ptr;
00637      }
00638 
00639    /* reset all variables to 0 */
00640    png_memset(png_ptr, 0, sizeof (png_struct));
00641 
00642 #if !defined(PNG_1_0_X)
00643 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
00644    png_init_mmx_flags(png_ptr);   /* 1.2.0 addition */
00645 #endif
00646 #endif /* PNG_1_0_X */
00647 
00648 #ifdef PNG_SETJMP_SUPPORTED
00649    /* restore jump buffer */
00650    png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
00651 #endif
00652 
00653    png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
00654       png_flush_ptr_NULL);
00655 
00656    /* initialize zbuf - compression buffer */
00657    png_ptr->zbuf_size = PNG_ZBUF_SIZE;
00658    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
00659       (png_uint_32)png_ptr->zbuf_size);
00660 
00661 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
00662    png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
00663       1, png_doublep_NULL, png_doublep_NULL);
00664 #endif
00665 }
00666 
00667 /* Write a few rows of image data.  If the image is interlaced,
00668  * either you will have to write the 7 sub images, or, if you
00669  * have called png_set_interlace_handling(), you will have to
00670  * "write" the image seven times.
00671  */
00672 void PNGAPI
00673 png_write_rows(png_structp png_ptr, png_bytepp row,
00674    png_uint_32 num_rows)
00675 {
00676    png_uint_32 i; /* row counter */
00677    png_bytepp rp; /* row pointer */
00678 
00679    png_debug(1, "in png_write_rows\n");
00680    /* loop through the rows */
00681    for (i = 0, rp = row; i < num_rows; i++, rp++)
00682    {
00683       png_write_row(png_ptr, *rp);
00684    }
00685 }
00686 
00687 /* Write the image.  You only need to call this function once, even
00688  * if you are writing an interlaced image.
00689  */
00690 void PNGAPI
00691 png_write_image(png_structp png_ptr, png_bytepp image)
00692 {
00693    png_uint_32 i; /* row index */
00694    int pass, num_pass; /* pass variables */
00695    png_bytepp rp; /* points to current row */
00696 
00697    png_debug(1, "in png_write_image\n");
00698 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
00699    /* intialize interlace handling.  If image is not interlaced,
00700       this will set pass to 1 */
00701    num_pass = png_set_interlace_handling(png_ptr);
00702 #else
00703    num_pass = 1;
00704 #endif
00705    /* loop through passes */
00706    for (pass = 0; pass < num_pass; pass++)
00707    {
00708       /* loop through image */
00709       for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
00710       {
00711          png_write_row(png_ptr, *rp);
00712       }
00713    }
00714 }
00715 
00716 /* called by user to write a row of image data */
00717 void PNGAPI
00718 png_write_row(png_structp png_ptr, png_bytep row)
00719 {
00720    png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
00721       png_ptr->row_number, png_ptr->pass);
00722    /* initialize transformations and other stuff if first time */
00723    if (png_ptr->row_number == 0 && png_ptr->pass == 0)
00724    {
00725    /* make sure we wrote the header info */
00726    if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
00727       png_error(png_ptr,
00728          "png_write_info was never called before png_write_row.");
00729 
00730    /* check for transforms that have been set but were defined out */
00731 #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
00732    if (png_ptr->transformations & PNG_INVERT_MONO)
00733       png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
00734 #endif
00735 #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
00736    if (png_ptr->transformations & PNG_FILLER)
00737       png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
00738 #endif
00739 #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
00740    if (png_ptr->transformations & PNG_PACKSWAP)
00741       png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
00742 #endif
00743 #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
00744    if (png_ptr->transformations & PNG_PACK)
00745       png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
00746 #endif
00747 #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
00748    if (png_ptr->transformations & PNG_SHIFT)
00749       png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
00750 #endif
00751 #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
00752    if (png_ptr->transformations & PNG_BGR)
00753       png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
00754 #endif
00755 #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
00756    if (png_ptr->transformations & PNG_SWAP_BYTES)
00757       png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
00758 #endif
00759 
00760       png_write_start_row(png_ptr);
00761    }
00762 
00763 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
00764    /* if interlaced and not interested in row, return */
00765    if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
00766    {
00767       switch (png_ptr->pass)
00768       {
00769          case 0:
00770             if (png_ptr->row_number & 0x07)
00771             {
00772                png_write_finish_row(png_ptr);
00773                return;
00774             }
00775             break;
00776          case 1:
00777             if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
00778             {
00779                png_write_finish_row(png_ptr);
00780                return;
00781             }
00782             break;
00783          case 2:
00784             if ((png_ptr->row_number & 0x07) != 4)
00785             {
00786                png_write_finish_row(png_ptr);
00787                return;
00788             }
00789             break;
00790          case 3:
00791             if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
00792             {
00793                png_write_finish_row(png_ptr);
00794                return;
00795             }
00796             break;
00797          case 4:
00798             if ((png_ptr->row_number & 0x03) != 2)
00799             {
00800                png_write_finish_row(png_ptr);
00801                return;
00802             }
00803             break;
00804          case 5:
00805             if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
00806             {
00807                png_write_finish_row(png_ptr);
00808                return;
00809             }
00810             break;
00811          case 6:
00812             if (!(png_ptr->row_number & 0x01))
00813             {
00814                png_write_finish_row(png_ptr);
00815                return;
00816             }
00817             break;
00818       }
00819    }
00820 #endif
00821 
00822    /* set up row info for transformations */
00823    png_ptr->row_info.color_type = png_ptr->color_type;
00824    png_ptr->row_info.width = png_ptr->usr_width;
00825    png_ptr->row_info.channels = png_ptr->usr_channels;
00826    png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
00827    png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
00828       png_ptr->row_info.channels);
00829 
00830    png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
00831       (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
00832 
00833    png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
00834    png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
00835    png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
00836    png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
00837    png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
00838    png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
00839 
00840    /* Copy user's row into buffer, leaving room for filter byte. */
00841    png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
00842       png_ptr->row_info.rowbytes);
00843 
00844 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
00845    /* handle interlacing */
00846    if (png_ptr->interlaced && png_ptr->pass < 6 &&
00847       (png_ptr->transformations & PNG_INTERLACE))
00848    {
00849       png_do_write_interlace(&(png_ptr->row_info),
00850          png_ptr->row_buf + 1, png_ptr->pass);
00851       /* this should always get caught above, but still ... */
00852       if (!(png_ptr->row_info.width))
00853       {
00854          png_write_finish_row(png_ptr);
00855          return;
00856       }
00857    }
00858 #endif
00859 
00860    /* handle other transformations */
00861    if (png_ptr->transformations)
00862       png_do_write_transformations(png_ptr);
00863 
00864 #if defined(PNG_MNG_FEATURES_SUPPORTED)
00865    /* Write filter_method 64 (intrapixel differencing) only if
00866     * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
00867     * 2. Libpng did not write a PNG signature (this filter_method is only
00868     *    used in PNG datastreams that are embedded in MNG datastreams) and
00869     * 3. The application called png_permit_mng_features with a mask that
00870     *    included PNG_FLAG_MNG_FILTER_64 and
00871     * 4. The filter_method is 64 and
00872     * 5. The color_type is RGB or RGBA
00873     */
00874    if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
00875       (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
00876    {
00877       /* Intrapixel differencing */
00878       png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
00879    }
00880 #endif
00881 
00882    /* Find a filter if necessary, filter the row and write it out. */
00883    png_write_find_filter(png_ptr, &(png_ptr->row_info));
00884 
00885    if (png_ptr->write_row_fn != NULL)
00886       (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
00887 }
00888 
00889 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
00890 /* Set the automatic flush interval or 0 to turn flushing off */
00891 void PNGAPI
00892 png_set_flush(png_structp png_ptr, int nrows)
00893 {
00894    png_debug(1, "in png_set_flush\n");
00895    png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
00896 }
00897 
00898 /* flush the current output buffers now */
00899 void PNGAPI
00900 png_write_flush(png_structp png_ptr)
00901 {
00902    int wrote_IDAT;
00903 
00904    png_debug(1, "in png_write_flush\n");
00905    /* We have already written out all of the data */
00906    if (png_ptr->row_number >= png_ptr->num_rows)
00907      return;
00908 
00909    do
00910    {
00911       int ret;
00912 
00913       /* compress the data */
00914       ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
00915       wrote_IDAT = 0;
00916 
00917       /* check for compression errors */
00918       if (ret != Z_OK)
00919       {
00920          if (png_ptr->zstream.msg != NULL)
00921             png_error(png_ptr, png_ptr->zstream.msg);
00922          else
00923             png_error(png_ptr, "zlib error");
00924       }
00925 
00926       if (!(png_ptr->zstream.avail_out))
00927       {
00928          /* write the IDAT and reset the zlib output buffer */
00929          png_write_IDAT(png_ptr, png_ptr->zbuf,
00930                         png_ptr->zbuf_size);
00931          png_ptr->zstream.next_out = png_ptr->zbuf;
00932          png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
00933          wrote_IDAT = 1;
00934       }
00935    } while(wrote_IDAT == 1);
00936 
00937    /* If there is any data left to be output, write it into a new IDAT */
00938    if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
00939    {
00940       /* write the IDAT and reset the zlib output buffer */
00941       png_write_IDAT(png_ptr, png_ptr->zbuf,
00942                      png_ptr->zbuf_size - png_ptr->zstream.avail_out);
00943       png_ptr->zstream.next_out = png_ptr->zbuf;
00944       png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
00945    }
00946    png_ptr->flush_rows = 0;
00947    png_flush(png_ptr);
00948 }
00949 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
00950 
00951 /* free all memory used by the write */
00952 void PNGAPI
00953 png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
00954 {
00955    png_structp png_ptr = NULL;
00956    png_infop info_ptr = NULL;
00957 #ifdef PNG_USER_MEM_SUPPORTED
00958    png_free_ptr free_fn = NULL;
00959    png_voidp mem_ptr = NULL;
00960 #endif
00961 
00962    png_debug(1, "in png_destroy_write_struct\n");
00963    if (png_ptr_ptr != NULL)
00964    {
00965       png_ptr = *png_ptr_ptr;
00966 #ifdef PNG_USER_MEM_SUPPORTED
00967       free_fn = png_ptr->free_fn;
00968       mem_ptr = png_ptr->mem_ptr;
00969 #endif
00970    }
00971 
00972    if (info_ptr_ptr != NULL)
00973       info_ptr = *info_ptr_ptr;
00974 
00975    if (info_ptr != NULL)
00976    {
00977       png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
00978 
00979 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
00980       if (png_ptr->num_chunk_list)
00981       {
00982          png_free(png_ptr, png_ptr->chunk_list);
00983          png_ptr->chunk_list=NULL;
00984          png_ptr->num_chunk_list=0;
00985       }
00986 #endif
00987 
00988 #ifdef PNG_USER_MEM_SUPPORTED
00989       png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
00990          (png_voidp)mem_ptr);
00991 #else
00992       png_destroy_struct((png_voidp)info_ptr);
00993 #endif
00994       *info_ptr_ptr = NULL;
00995    }
00996 
00997    if (png_ptr != NULL)
00998    {
00999       png_write_destroy(png_ptr);
01000 #ifdef PNG_USER_MEM_SUPPORTED
01001       png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
01002          (png_voidp)mem_ptr);
01003 #else
01004       png_destroy_struct((png_voidp)png_ptr);
01005 #endif
01006       *png_ptr_ptr = NULL;
01007    }
01008 }
01009 
01010 
01011 /* Free any memory used in png_ptr struct (old method) */
01012 void /* PRIVATE */
01013 png_write_destroy(png_structp png_ptr)
01014 {
01015 #ifdef PNG_SETJMP_SUPPORTED
01016    jmp_buf tmp_jmp; /* save jump buffer */
01017 #endif
01018    png_error_ptr error_fn;
01019    png_error_ptr warning_fn;
01020    png_voidp error_ptr;
01021 #ifdef PNG_USER_MEM_SUPPORTED
01022    png_free_ptr free_fn;
01023 #endif
01024 
01025    png_debug(1, "in png_write_destroy\n");
01026    /* free any memory zlib uses */
01027    deflateEnd(&png_ptr->zstream);
01028 
01029    /* free our memory.  png_free checks NULL for us. */
01030    png_free(png_ptr, png_ptr->zbuf);
01031    png_free(png_ptr, png_ptr->row_buf);
01032    png_free(png_ptr, png_ptr->prev_row);
01033    png_free(png_ptr, png_ptr->sub_row);
01034    png_free(png_ptr, png_ptr->up_row);
01035    png_free(png_ptr, png_ptr->avg_row);
01036    png_free(png_ptr, png_ptr->paeth_row);
01037 
01038 #if defined(PNG_TIME_RFC1123_SUPPORTED)
01039    png_free(png_ptr, png_ptr->time_buffer);
01040 #endif
01041 
01042 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
01043    png_free(png_ptr, png_ptr->prev_filters);
01044    png_free(png_ptr, png_ptr->filter_weights);
01045    png_free(png_ptr, png_ptr->inv_filter_weights);
01046    png_free(png_ptr, png_ptr->filter_costs);
01047    png_free(png_ptr, png_ptr->inv_filter_costs);
01048 #endif
01049 
01050 #ifdef PNG_SETJMP_SUPPORTED
01051    /* reset structure */
01052    png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
01053 #endif
01054 
01055    error_fn = png_ptr->error_fn;
01056    warning_fn = png_ptr->warning_fn;
01057    error_ptr = png_ptr->error_ptr;
01058 #ifdef PNG_USER_MEM_SUPPORTED
01059    free_fn = png_ptr->free_fn;
01060 #endif
01061 
01062    png_memset(png_ptr, 0, sizeof (png_struct));
01063 
01064    png_ptr->error_fn = error_fn;
01065    png_ptr->warning_fn = warning_fn;
01066    png_ptr->error_ptr = error_ptr;
01067 #ifdef PNG_USER_MEM_SUPPORTED
01068    png_ptr->free_fn = free_fn;
01069 #endif
01070 
01071 #ifdef PNG_SETJMP_SUPPORTED
01072    png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
01073 #endif
01074 }
01075 
01076 /* Allow the application to select one or more row filters to use. */
01077 void PNGAPI
01078 png_set_filter(png_structp png_ptr, int method, int filters)
01079 {
01080    png_debug(1, "in png_set_filter\n");
01081 #if defined(PNG_MNG_FEATURES_SUPPORTED)
01082    if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
01083       (method == PNG_INTRAPIXEL_DIFFERENCING))
01084          method = PNG_FILTER_TYPE_BASE;
01085 #endif
01086    if (method == PNG_FILTER_TYPE_BASE)
01087    {
01088       switch (filters & (PNG_ALL_FILTERS | 0x07))
01089       {
01090          case 5:
01091          case 6:
01092          case 7: png_warning(png_ptr, "Unknown row filter for method 0");
01093          case PNG_FILTER_VALUE_NONE:  png_ptr->do_filter=PNG_FILTER_NONE; break;
01094          case PNG_FILTER_VALUE_SUB:   png_ptr->do_filter=PNG_FILTER_SUB;  break;
01095          case PNG_FILTER_VALUE_UP:    png_ptr->do_filter=PNG_FILTER_UP;   break;
01096          case PNG_FILTER_VALUE_AVG:   png_ptr->do_filter=PNG_FILTER_AVG;  break;
01097          case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
01098          default: png_ptr->do_filter = (png_byte)filters; break;
01099       }
01100 
01101       /* If we have allocated the row_buf, this means we have already started
01102        * with the image and we should have allocated all of the filter buffers
01103        * that have been selected.  If prev_row isn't already allocated, then
01104        * it is too late to start using the filters that need it, since we
01105        * will be missing the data in the previous row.  If an application
01106        * wants to start and stop using particular filters during compression,
01107        * it should start out with all of the filters, and then add and
01108        * remove them after the start of compression.
01109        */
01110       if (png_ptr->row_buf != NULL)
01111       {
01112          if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
01113          {
01114             png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
01115               (png_ptr->rowbytes + 1));
01116             png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
01117          }
01118 
01119          if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
01120          {
01121             if (png_ptr->prev_row == NULL)
01122             {
01123                png_warning(png_ptr, "Can't add Up filter after starting");
01124                png_ptr->do_filter &= ~PNG_FILTER_UP;
01125             }
01126             else
01127             {
01128                png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
01129                   (png_ptr->rowbytes + 1));
01130                png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
01131             }
01132          }
01133 
01134          if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
01135          {
01136             if (png_ptr->prev_row == NULL)
01137             {
01138                png_warning(png_ptr, "Can't add Average filter after starting");
01139                png_ptr->do_filter &= ~PNG_FILTER_AVG;
01140             }
01141             else
01142             {
01143                png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
01144                   (png_ptr->rowbytes + 1));
01145                png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
01146             }
01147          }
01148 
01149          if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
01150              png_ptr->paeth_row == NULL)
01151          {
01152             if (png_ptr->prev_row == NULL)
01153             {
01154                png_warning(png_ptr, "Can't add Paeth filter after starting");
01155                png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
01156             }
01157             else
01158             {
01159                png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
01160                   (png_ptr->rowbytes + 1));
01161                png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
01162             }
01163          }
01164 
01165          if (png_ptr->do_filter == PNG_NO_FILTERS)
01166             png_ptr->do_filter = PNG_FILTER_NONE;
01167       }
01168    }
01169    else
01170       png_error(png_ptr, "Unknown custom filter method");
01171 }
01172 
01173 /* This allows us to influence the way in which libpng chooses the "best"
01174  * filter for the current scanline.  While the "minimum-sum-of-absolute-
01175  * differences metric is relatively fast and effective, there is some
01176  * question as to whether it can be improved upon by trying to keep the
01177  * filtered data going to zlib more consistent, hopefully resulting in
01178  * better compression.
01179  */
01180 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)      /* GRR 970116 */
01181 void PNGAPI
01182 png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
01183    int num_weights, png_doublep filter_weights,
01184    png_doublep filter_costs)
01185 {
01186    int i;
01187 
01188    png_debug(1, "in png_set_filter_heuristics\n");
01189    if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
01190    {
01191       png_warning(png_ptr, "Unknown filter heuristic method");
01192       return;
01193    }
01194 
01195    if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
01196    {
01197       heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
01198    }
01199 
01200    if (num_weights < 0 || filter_weights == NULL ||
01201       heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
01202    {
01203       num_weights = 0;
01204    }
01205 
01206    png_ptr->num_prev_filters = (png_byte)num_weights;
01207    png_ptr->heuristic_method = (png_byte)heuristic_method;
01208 
01209    if (num_weights > 0)
01210    {
01211       if (png_ptr->prev_filters == NULL)
01212       {
01213          png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
01214             (png_uint_32)(sizeof(png_byte) * num_weights));
01215 
01216          /* To make sure that the weighting starts out fairly */
01217          for (i = 0; i < num_weights; i++)
01218          {
01219             png_ptr->prev_filters[i] = 255;
01220          }
01221       }
01222 
01223       if (png_ptr->filter_weights == NULL)
01224       {
01225          png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
01226             (png_uint_32)(sizeof(png_uint_16) * num_weights));
01227 
01228          png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
01229             (png_uint_32)(sizeof(png_uint_16) * num_weights));
01230          for (i = 0; i < num_weights; i++)
01231          {
01232             png_ptr->inv_filter_weights[i] =
01233             png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
01234          }
01235       }
01236 
01237       for (i = 0; i < num_weights; i++)
01238       {
01239          if (filter_weights[i] < 0.0)
01240          {
01241             png_ptr->inv_filter_weights[i] =
01242             png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
01243          }
01244          else
01245          {
01246             png_ptr->inv_filter_weights[i] =
01247                (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
01248             png_ptr->filter_weights[i] =
01249                (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
01250          }
01251       }
01252    }
01253 
01254    /* If, in the future, there are other filter methods, this would
01255     * need to be based on png_ptr->filter.
01256     */
01257    if (png_ptr->filter_costs == NULL)
01258    {
01259       png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
01260          (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
01261 
01262       png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
01263          (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
01264 
01265       for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
01266       {
01267          png_ptr->inv_filter_costs[i] =
01268          png_ptr->filter_costs[i] = PNG_COST_FACTOR;
01269       }
01270    }
01271 
01272    /* Here is where we set the relative costs of the different filters.  We
01273     * should take the desired compression level into account when setting
01274     * the costs, so that Paeth, for instance, has a high relative cost at low
01275     * compression levels, while it has a lower relative cost at higher
01276     * compression settings.  The filter types are in order of increasing
01277     * relative cost, so it would be possible to do this with an algorithm.
01278     */
01279    for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
01280    {
01281       if (filter_costs == NULL || filter_costs[i] < 0.0)
01282       {
01283          png_ptr->inv_filter_costs[i] =
01284          png_ptr->filter_costs[i] = PNG_COST_FACTOR;
01285       }
01286       else if (filter_costs[i] >= 1.0)
01287       {
01288          png_ptr->inv_filter_costs[i] =
01289             (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
01290          png_ptr->filter_costs[i] =
01291             (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
01292       }
01293    }
01294 }
01295 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
01296 
01297 void PNGAPI
01298 png_set_compression_level(png_structp png_ptr, int level)
01299 {
01300    png_debug(1, "in png_set_compression_level\n");
01301    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
01302    png_ptr->zlib_level = level;
01303 }
01304 
01305 void PNGAPI
01306 png_set_compression_mem_level(png_structp png_ptr, int mem_level)
01307 {
01308    png_debug(1, "in png_set_compression_mem_level\n");
01309    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
01310    png_ptr->zlib_mem_level = mem_level;
01311 }
01312 
01313 void PNGAPI
01314 png_set_compression_strategy(png_structp png_ptr, int strategy)
01315 {
01316    png_debug(1, "in png_set_compression_strategy\n");
01317    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
01318    png_ptr->zlib_strategy = strategy;
01319 }
01320 
01321 void PNGAPI
01322 png_set_compression_window_bits(png_structp png_ptr, int window_bits)
01323 {
01324    if (window_bits > 15)
01325       png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
01326    else if (window_bits < 8)
01327       png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
01328 #ifndef WBITS_8_OK
01329    /* avoid libpng bug with 256-byte windows */
01330    if (window_bits == 8)
01331      {
01332        png_warning(png_ptr, "Compression window is being reset to 512");
01333        window_bits=9;
01334      }
01335 #endif
01336    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
01337    png_ptr->zlib_window_bits = window_bits;
01338 }
01339 
01340 void PNGAPI
01341 png_set_compression_method(png_structp png_ptr, int method)
01342 {
01343    png_debug(1, "in png_set_compression_method\n");
01344    if (method != 8)
01345       png_warning(png_ptr, "Only compression method 8 is supported by PNG");
01346    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
01347    png_ptr->zlib_method = method;
01348 }
01349 
01350 void PNGAPI
01351 png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
01352 {
01353    png_ptr->write_row_fn = write_row_fn;
01354 }
01355 
01356 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
01357 void PNGAPI
01358 png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
01359    write_user_transform_fn)
01360 {
01361    png_debug(1, "in png_set_write_user_transform_fn\n");
01362    png_ptr->transformations |= PNG_USER_TRANSFORM;
01363    png_ptr->write_user_transform_fn = write_user_transform_fn;
01364 }
01365 #endif
01366 
01367 
01368 #if defined(PNG_INFO_IMAGE_SUPPORTED)
01369 void PNGAPI
01370 png_write_png(png_structp png_ptr, png_infop info_ptr,
01371               int transforms, voidp params)
01372 {
01373 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
01374    /* invert the alpha channel from opacity to transparency */
01375    if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
01376        png_set_invert_alpha(png_ptr);
01377 #endif
01378 
01379    /* Write the file header information. */
01380    png_write_info(png_ptr, info_ptr);
01381 
01382    /* ------ these transformations don't touch the info structure ------- */
01383 
01384 #if defined(PNG_WRITE_INVERT_SUPPORTED)
01385    /* invert monochrome pixels */
01386    if (transforms & PNG_TRANSFORM_INVERT_MONO)
01387        png_set_invert_mono(png_ptr);
01388 #endif
01389 
01390 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
01391    /* Shift the pixels up to a legal bit depth and fill in
01392     * as appropriate to correctly scale the image.
01393     */
01394    if ((transforms & PNG_TRANSFORM_SHIFT)
01395                && (info_ptr->valid & PNG_INFO_sBIT))
01396        png_set_shift(png_ptr, &info_ptr->sig_bit);
01397 #endif
01398 
01399 #if defined(PNG_WRITE_PACK_SUPPORTED)
01400    /* pack pixels into bytes */
01401    if (transforms & PNG_TRANSFORM_PACKING)
01402        png_set_packing(png_ptr);
01403 #endif
01404 
01405 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
01406    /* swap location of alpha bytes from ARGB to RGBA */
01407    if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
01408        png_set_swap_alpha(png_ptr);
01409 #endif
01410 
01411 #if defined(PNG_WRITE_FILLER_SUPPORTED)
01412    /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
01413     * RGB (4 channels -> 3 channels). The second parameter is not used.
01414     */
01415    if (transforms & PNG_TRANSFORM_STRIP_FILLER)
01416        png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
01417 #endif
01418 
01419 #if defined(PNG_WRITE_BGR_SUPPORTED)
01420    /* flip BGR pixels to RGB */
01421    if (transforms & PNG_TRANSFORM_BGR)
01422        png_set_bgr(png_ptr);
01423 #endif
01424 
01425 #if defined(PNG_WRITE_SWAP_SUPPORTED)
01426    /* swap bytes of 16-bit files to most significant byte first */
01427    if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
01428        png_set_swap(png_ptr);
01429 #endif
01430 
01431 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
01432    /* swap bits of 1, 2, 4 bit packed pixel formats */
01433    if (transforms & PNG_TRANSFORM_PACKSWAP)
01434        png_set_packswap(png_ptr);
01435 #endif
01436 
01437    /* ----------------------- end of transformations ------------------- */
01438 
01439    /* write the bits */
01440    if (info_ptr->valid & PNG_INFO_IDAT)
01441        png_write_image(png_ptr, info_ptr->row_pointers);
01442 
01443    /* It is REQUIRED to call this to finish writing the rest of the file */
01444    png_write_end(png_ptr, info_ptr);
01445 
01446    if(transforms == 0 || params == NULL)
01447       /* quiet compiler warnings */ return;
01448 }
01449 #endif
01450 #endif /* PNG_WRITE_SUPPORTED */