Skip to content

Commit

Permalink
--strip option to drop optional PNG chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Nov 25, 2016
1 parent 227a819 commit 4140cc5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pngquant.1
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ will make almost-opaque pixels fully opaque and will reduce amount of semi-trans
.Ql -ie-fs8.png
/
.Ql -ie-or8.png .
.It Fl Fl strip
Remove optional chunks (metadata) from PNG files.

This comment has been minimized.

Copy link
@HailinChenn

HailinChenn Apr 19, 2017

how to use it in script?Could you give a demo?

This comment has been minimized.

Copy link
@kornelski

kornelski Oct 2, 2017

Author Owner
pngquant --strip file.png

Note that it strips in addition to usual pngquant operation. If you just want to manipulate chunks, see pngcrush.

.It Fl Fl transbug
Workaround for readers that expect fully transparent color to be the last entry in the palette.
.It Fl v , Fl Fl verbose
Expand Down
20 changes: 14 additions & 6 deletions pngquant.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ options:\n\
--speed N speed/quality trade-off. 1=slow, 3=default, 11=fast & rough\n\
--nofs disable Floyd-Steinberg dithering\n\
--posterize N output lower-precision color (e.g. for ARGB4444 output)\n\
--strip remove optional metadata (default on Mac)\n\
--verbose print status messages (synonym: -v)\n\
\n\
Quantizes one or more 32-bit RGBA PNGs to 8-bit (or smaller) RGBA-palette.\n\
Expand Down Expand Up @@ -98,12 +99,13 @@ struct pngquant_options {
float floyd;
bool using_stdin, using_stdout, force, fast_compression, ie_mode,
min_quality_limit, skip_if_larger,
strip,
verbose;
};

static pngquant_error prepare_output_image(liq_result *result, liq_image *input_image, rwpng_color_transform tag, png8_image *output_image);
static void set_palette(liq_result *result, png8_image *output_image);
static pngquant_error read_image(liq_attr *options, const char *filename, int using_stdin, png24_image *input_image_p, liq_image **liq_image_p, bool keep_input_pixels, bool verbose);
static pngquant_error read_image(liq_attr *options, const char *filename, int using_stdin, png24_image *input_image_p, liq_image **liq_image_p, bool keep_input_pixels, bool strip, bool verbose);
static pngquant_error write_image(png8_image *output_image, png24_image *output_image24, const char *outname, struct pngquant_options *options);
static char *add_filename_extension(const char *filename, const char *newext);
static bool file_exists(const char *outname);
Expand Down Expand Up @@ -255,7 +257,7 @@ static void fix_obsolete_options(const unsigned int argc, char *argv[])
}

enum {arg_floyd=1, arg_ordered, arg_ext, arg_no_force, arg_iebug,
arg_transbug, arg_map, arg_posterize, arg_skip_larger};
arg_transbug, arg_map, arg_posterize, arg_skip_larger, arg_strip};

static const struct option long_options[] = {
{"verbose", no_argument, NULL, 'v'},
Expand All @@ -273,6 +275,7 @@ static const struct option long_options[] = {
{"speed", required_argument, NULL, 's'},
{"quality", required_argument, NULL, 'Q'},
{"posterize", required_argument, NULL, arg_posterize},
{"strip", no_argument, NULL, arg_strip},
{"map", required_argument, NULL, arg_map},
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
Expand All @@ -286,6 +289,7 @@ int main(int argc, char *argv[])
{
struct pngquant_options options = {
.floyd = 1.f, // floyd-steinberg dithering
.strip = false,
};
options.liq = liq_attr_create();

Expand Down Expand Up @@ -376,10 +380,14 @@ int main(int argc, char *argv[])
}
break;

case arg_strip:
options.strip = true;
break;

case arg_map:
{
png24_image tmp = {};
if (SUCCESS != read_image(options.liq, optarg, false, &tmp, &options.fixed_palette_image, true, false)) {
if (SUCCESS != read_image(options.liq, optarg, false, &tmp, &options.fixed_palette_image, true, true, false)) {
fprintf(stderr, " error: unable to load %s", optarg);
return INVALID_ARGUMENT;
}
Expand Down Expand Up @@ -559,7 +567,7 @@ pngquant_error pngquant_file(const char *filename, const char *outname, struct p
png24_image input_image_rwpng = {};
bool keep_input_pixels = options->skip_if_larger || (options->using_stdout && options->min_quality_limit); // original may need to be output to stdout
if (SUCCESS == retval) {
retval = read_image(options->liq, filename, options->using_stdin, &input_image_rwpng, &input_image, keep_input_pixels, options->verbose);
retval = read_image(options->liq, filename, options->using_stdin, &input_image_rwpng, &input_image, keep_input_pixels, options->strip, options->verbose);
}

int quality_percent = 90; // quality on 0-100 scale, updated upon successful remap
Expand Down Expand Up @@ -797,7 +805,7 @@ static pngquant_error write_image(png8_image *output_image, png24_image *output_
return retval;
}

static pngquant_error read_image(liq_attr *options, const char *filename, int using_stdin, png24_image *input_image_p, liq_image **liq_image_p, bool keep_input_pixels, bool verbose)
static pngquant_error read_image(liq_attr *options, const char *filename, int using_stdin, png24_image *input_image_p, liq_image **liq_image_p, bool keep_input_pixels, bool strip, bool verbose)
{
FILE *infile;

Expand All @@ -812,7 +820,7 @@ static pngquant_error read_image(liq_attr *options, const char *filename, int us
pngquant_error retval;
#pragma omp critical (libpng)
{
retval = rwpng_read_image24(infile, input_image_p, verbose);
retval = rwpng_read_image24(infile, input_image_p, strip, verbose);
}

if (!using_stdin) {
Expand Down
16 changes: 10 additions & 6 deletions rwpng.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ static void rwpng_warning_stderr_handler(png_structp png_ptr, png_const_charp ms
static void rwpng_warning_silent_handler(png_structp png_ptr, png_const_charp msg) {
}

static pngquant_error rwpng_read_image24_libpng(FILE *infile, png24_image *mainprog_ptr, int verbose)
static pngquant_error rwpng_read_image24_libpng(FILE *infile, png24_image *mainprog_ptr, int strip, int verbose)
{
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
Expand Down Expand Up @@ -231,10 +231,14 @@ static pngquant_error rwpng_read_image24_libpng(FILE *infile, png24_image *mainp
#endif

#if PNG_LIBPNG_VER >= 10500 && defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
/* copy standard chunks too */
png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_IF_SAFE, (png_const_bytep)"pHYs\0iTXt\0tEXt\0zTXt", 4);
if (!strip) {
/* copy standard chunks too */
png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_IF_SAFE, (png_const_bytep)"pHYs\0iTXt\0tEXt\0zTXt", 4);
}
#endif
png_set_read_user_chunk_fn(png_ptr, &mainprog_ptr->chunks, read_chunk_callback);
if (!strip) {
png_set_read_user_chunk_fn(png_ptr, &mainprog_ptr->chunks, read_chunk_callback);
}

struct rwpng_read_data read_data = {infile, 0};
png_set_read_fn(png_ptr, &read_data, user_read_data);
Expand Down Expand Up @@ -453,12 +457,12 @@ void rwpng_free_image8(png8_image *image)
image->chunks = NULL;
}

pngquant_error rwpng_read_image24(FILE *infile, png24_image *input_image_p, int verbose)
pngquant_error rwpng_read_image24(FILE *infile, png24_image *input_image_p, int strip, int verbose)
{
#if USE_COCOA
return rwpng_read_image24_cocoa(infile, input_image_p);
#else
return rwpng_read_image24_libpng(infile, input_image_p, verbose);
return rwpng_read_image24_libpng(infile, input_image_p, strip, verbose);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion rwpng.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ typedef union {

void rwpng_version_info(FILE *fp);

pngquant_error rwpng_read_image24(FILE *infile, png24_image *mainprog_ptr, int verbose);
pngquant_error rwpng_read_image24(FILE *infile, png24_image *mainprog_ptr, int strip, int verbose);
pngquant_error rwpng_write_image8(FILE *outfile, const png8_image *mainprog_ptr);
pngquant_error rwpng_write_image24(FILE *outfile, const png24_image *mainprog_ptr);
void rwpng_free_image24(png24_image *);
Expand Down

1 comment on commit 4140cc5

@iftkharhussain
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi sir, can you tell me how to use it with PHP ?

Please sign in to comment.