mirror of
https://git.code.sf.net/p/libpng/code.git
synced 2025-07-10 18:04:09 +02:00
[devel] Implemented remaining "_fixed" functions and corrected warnings
that were recently introduced, mostly resulting from safe but uncast assignments to shorter integers. Also added a zlib VStudio release library project because the latest zlib Official Windows build does not include such a thing.
This commit is contained in:
290
pngwrite.c
290
pngwrite.c
@@ -1,7 +1,7 @@
|
||||
|
||||
/* pngwrite.c - general routines to write a PNG file
|
||||
*
|
||||
* Last changed in libpng 1.5.0 [July 30, 2010]
|
||||
* Last changed in libpng 1.5.0 [July 31, 2010]
|
||||
* Copyright (c) 1998-2010 Glenn Randers-Pehrson
|
||||
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
|
||||
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
|
||||
@@ -447,6 +447,8 @@ png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
|
||||
}
|
||||
|
||||
/* Alternate initialize png_ptr structure, and allocate any memory needed */
|
||||
static void png_reset_filter_heuristics(png_structp png_ptr); /* forward decl */
|
||||
|
||||
png_structp PNGAPI
|
||||
png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
|
||||
png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
|
||||
@@ -574,8 +576,7 @@ png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
|
||||
png_set_write_fn(png_ptr, NULL, NULL, NULL);
|
||||
|
||||
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
|
||||
png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
|
||||
1, NULL, NULL);
|
||||
png_reset_filter_heuristics(png_ptr);
|
||||
#endif
|
||||
|
||||
return (png_ptr);
|
||||
@@ -999,9 +1000,8 @@ png_write_destroy(png_structp png_ptr)
|
||||
#endif
|
||||
|
||||
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
|
||||
png_free(png_ptr, png_ptr->prev_filters);
|
||||
png_free(png_ptr, png_ptr->filter_weights);
|
||||
png_free(png_ptr, png_ptr->inv_filter_weights);
|
||||
/* Use this to save a little code space, it doesn't free the filter_costs */
|
||||
png_reset_filter_heuristics(png_ptr);
|
||||
png_free(png_ptr, png_ptr->filter_costs);
|
||||
png_free(png_ptr, png_ptr->inv_filter_costs);
|
||||
#endif
|
||||
@@ -1165,41 +1165,59 @@ png_set_filter(png_structp png_ptr, int method, int filters)
|
||||
* better compression.
|
||||
*/
|
||||
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
|
||||
void PNGAPI
|
||||
png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
|
||||
int num_weights, png_doublep filter_weights,
|
||||
png_doublep filter_costs)
|
||||
/* Conveneince reset API. */
|
||||
static void
|
||||
png_reset_filter_heuristics(png_structp png_ptr)
|
||||
{
|
||||
int i;
|
||||
/* Clear out any old values in the 'weights' - this must be done because if
|
||||
* the app calls set_filter_heuristics multiple times with different
|
||||
* 'num_weights' values we would otherwise potentially have wrong sized
|
||||
* arrays.
|
||||
*/
|
||||
png_ptr->num_prev_filters = 0;
|
||||
png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
|
||||
if (png_ptr->prev_filters != NULL)
|
||||
{
|
||||
png_bytep old = png_ptr->prev_filters;
|
||||
png_ptr->prev_filters = NULL;
|
||||
png_free(png_ptr, old);
|
||||
}
|
||||
if (png_ptr->filter_weights != NULL)
|
||||
{
|
||||
png_uint_16p old = png_ptr->filter_weights;
|
||||
png_ptr->filter_weights = NULL;
|
||||
png_free(png_ptr, old);
|
||||
}
|
||||
|
||||
png_debug(1, "in png_set_filter_heuristics");
|
||||
if (png_ptr->inv_filter_weights != NULL)
|
||||
{
|
||||
png_uint_16p old = png_ptr->inv_filter_weights;
|
||||
png_ptr->inv_filter_weights = NULL;
|
||||
png_free(png_ptr, old);
|
||||
}
|
||||
|
||||
/* Leave the filter_costs - this array is fixed size. */
|
||||
}
|
||||
|
||||
static int
|
||||
png_init_filter_heuristics(png_structp png_ptr, int heuristic_method,
|
||||
int num_weights)
|
||||
{
|
||||
if (png_ptr == NULL)
|
||||
return;
|
||||
return 0;
|
||||
|
||||
if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
|
||||
/* Clear out the arrays */
|
||||
png_reset_filter_heuristics(png_ptr);
|
||||
|
||||
/* Check arguments; the 'reset' function makes the correct settings for the
|
||||
* unweighted case, but we must handle the weight case by initializing the
|
||||
* arrays for the caller.
|
||||
*/
|
||||
if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
||||
{
|
||||
png_warning(png_ptr, "Unknown filter heuristic method");
|
||||
return;
|
||||
}
|
||||
int i;
|
||||
|
||||
if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
|
||||
{
|
||||
heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
|
||||
}
|
||||
|
||||
if (num_weights < 0 || filter_weights == NULL ||
|
||||
heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
|
||||
{
|
||||
num_weights = 0;
|
||||
}
|
||||
|
||||
png_ptr->num_prev_filters = (png_byte)num_weights;
|
||||
png_ptr->heuristic_method = (png_byte)heuristic_method;
|
||||
|
||||
if (num_weights > 0)
|
||||
{
|
||||
if (png_ptr->prev_filters == NULL)
|
||||
if (num_weights > 0)
|
||||
{
|
||||
png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
|
||||
(png_uint_32)(png_sizeof(png_byte) * num_weights));
|
||||
@@ -1209,85 +1227,169 @@ png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
|
||||
{
|
||||
png_ptr->prev_filters[i] = 255;
|
||||
}
|
||||
|
||||
png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
|
||||
(png_uint_32)(png_sizeof(png_uint_16) * num_weights));
|
||||
|
||||
png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
|
||||
(png_uint_32)(png_sizeof(png_uint_16) * num_weights));
|
||||
|
||||
for (i = 0; i < num_weights; i++)
|
||||
{
|
||||
png_ptr->inv_filter_weights[i] =
|
||||
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
||||
}
|
||||
|
||||
/* Safe to set this now */
|
||||
png_ptr->num_prev_filters = (png_byte)num_weights;
|
||||
}
|
||||
|
||||
if (png_ptr->filter_weights == NULL)
|
||||
/* If, in the future, there are other filter methods, this would
|
||||
* need to be based on png_ptr->filter.
|
||||
*/
|
||||
if (png_ptr->filter_costs == NULL)
|
||||
{
|
||||
png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
|
||||
(png_uint_32)(png_sizeof(png_uint_16) * num_weights));
|
||||
png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
|
||||
(png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
|
||||
|
||||
png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
|
||||
(png_uint_32)(png_sizeof(png_uint_16) * num_weights));
|
||||
|
||||
for (i = 0; i < num_weights; i++)
|
||||
{
|
||||
png_ptr->inv_filter_weights[i] =
|
||||
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
||||
}
|
||||
png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
|
||||
(png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
|
||||
}
|
||||
|
||||
for (i = 0; i < num_weights; i++)
|
||||
{
|
||||
if (filter_weights[i] < 0.0)
|
||||
{
|
||||
png_ptr->inv_filter_weights[i] =
|
||||
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
png_ptr->inv_filter_weights[i] =
|
||||
(png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
|
||||
|
||||
png_ptr->filter_weights[i] =
|
||||
(png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If, in the future, there are other filter methods, this would
|
||||
* need to be based on png_ptr->filter.
|
||||
*/
|
||||
if (png_ptr->filter_costs == NULL)
|
||||
{
|
||||
png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
|
||||
(png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
|
||||
|
||||
png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
|
||||
(png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
|
||||
|
||||
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
|
||||
{
|
||||
png_ptr->inv_filter_costs[i] =
|
||||
png_ptr->filter_costs[i] = PNG_COST_FACTOR;
|
||||
png_ptr->inv_filter_costs[i] =
|
||||
png_ptr->filter_costs[i] = PNG_COST_FACTOR;
|
||||
}
|
||||
|
||||
/* All the arrays are inited, safe to set this: */
|
||||
png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
|
||||
|
||||
/* Return the 'ok' code. */
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Here is where we set the relative costs of the different filters. We
|
||||
* should take the desired compression level into account when setting
|
||||
* the costs, so that Paeth, for instance, has a high relative cost at low
|
||||
* compression levels, while it has a lower relative cost at higher
|
||||
* compression settings. The filter types are in order of increasing
|
||||
* relative cost, so it would be possible to do this with an algorithm.
|
||||
*/
|
||||
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
|
||||
else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
|
||||
heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
|
||||
{
|
||||
if (filter_costs == NULL || filter_costs[i] < 0.0)
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
png_warning(png_ptr, "Unknown filter heuristic method");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Provide floating and fixed point APIs */
|
||||
#ifdef PNG_FLOATING_POINT_SUPPORTED
|
||||
void PNGAPI
|
||||
png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
|
||||
int num_weights, png_doublep filter_weights, png_doublep filter_costs)
|
||||
{
|
||||
png_debug(1, "in png_set_filter_heuristics");
|
||||
|
||||
/* The internal API allocates all the arrays and ensures that the elements of
|
||||
* those arrays are set to the default value.
|
||||
*/
|
||||
if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
|
||||
return;
|
||||
|
||||
/* If using the weighted method copy in the weights. */
|
||||
if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < num_weights; i++)
|
||||
{
|
||||
png_ptr->inv_filter_costs[i] =
|
||||
png_ptr->filter_costs[i] = PNG_COST_FACTOR;
|
||||
if (filter_weights[i] <= 0.0)
|
||||
{
|
||||
png_ptr->inv_filter_weights[i] =
|
||||
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
png_ptr->inv_filter_weights[i] =
|
||||
(png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
|
||||
|
||||
png_ptr->filter_weights[i] =
|
||||
(png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
|
||||
}
|
||||
}
|
||||
|
||||
else if (filter_costs[i] >= 1.0)
|
||||
/* Here is where we set the relative costs of the different filters. We
|
||||
* should take the desired compression level into account when setting
|
||||
* the costs, so that Paeth, for instance, has a high relative cost at low
|
||||
* compression levels, while it has a lower relative cost at higher
|
||||
* compression settings. The filter types are in order of increasing
|
||||
* relative cost, so it would be possible to do this with an algorithm.
|
||||
*/
|
||||
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
|
||||
{
|
||||
png_ptr->inv_filter_costs[i] =
|
||||
(png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
|
||||
png_ptr->inv_filter_costs[i] =
|
||||
(png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
|
||||
|
||||
png_ptr->filter_costs[i] =
|
||||
(png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
|
||||
png_ptr->filter_costs[i] =
|
||||
(png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* FLOATING_POINT */
|
||||
|
||||
#ifdef PNG_FIXED_POINT_SUPPORTED
|
||||
void PNGAPI
|
||||
png_set_filter_heuristics_fixed(png_structp png_ptr, int heuristic_method,
|
||||
int num_weights, png_fixed_point_p filter_weights,
|
||||
png_fixed_point_p filter_costs)
|
||||
{
|
||||
png_debug(1, "in png_set_filter_heuristics_fixed");
|
||||
|
||||
/* The internal API allocates all the arrays and ensures that the elements of
|
||||
* those arrays are set to the default value.
|
||||
*/
|
||||
if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
|
||||
return;
|
||||
|
||||
/* If using the weighted method copy in the weights. */
|
||||
if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < num_weights; i++)
|
||||
{
|
||||
if (filter_weights[i] <= 0)
|
||||
{
|
||||
png_ptr->inv_filter_weights[i] =
|
||||
png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
png_ptr->inv_filter_weights[i] = (png_uint_16)
|
||||
((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
|
||||
|
||||
png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
|
||||
PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Here is where we set the relative costs of the different filters. We
|
||||
* should take the desired compression level into account when setting
|
||||
* the costs, so that Paeth, for instance, has a high relative cost at low
|
||||
* compression levels, while it has a lower relative cost at higher
|
||||
* compression settings. The filter types are in order of increasing
|
||||
* relative cost, so it would be possible to do this with an algorithm.
|
||||
*/
|
||||
for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
|
||||
if (filter_costs[i] >= PNG_FP_1)
|
||||
{
|
||||
png_ptr->inv_filter_costs[i] = (png_uint_16)((PNG_COST_FACTOR*
|
||||
PNG_FP_1+(filter_costs[i]/2)) / filter_costs[i]);
|
||||
|
||||
png_ptr->filter_costs[i] = (png_uint_16)
|
||||
((PNG_COST_FACTOR * filter_costs[i] +PNG_FP_HALF)/PNG_FP_1);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* FIXED_POINT */
|
||||
#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
|
||||
|
||||
void PNGAPI
|
||||
|
||||
Reference in New Issue
Block a user