/* * This file is part of the Advance project. * * Copyright (C) 2002, 2003, 2005 Andrea Mazzoleni * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "portable.h" #include "pngex.h" #include "file.h" #include "compress.h" #include "siglock.h" #include "lib/endianrw.h" #include #include using namespace std; shrink_t opt_level; bool opt_quiet; bool opt_force; bool opt_crc; bool reduce_image(unsigned char** out_ptr, unsigned* out_scanline, unsigned char* pal_ptr, unsigned* pal_count, unsigned char* palrns_ptr, unsigned *palrns_count, unsigned width, unsigned height, unsigned char* img_ptr, unsigned img_scanline, const unsigned char* rns_ptr, unsigned rns_size) { unsigned char col_ptr[256*3]; unsigned col_count; unsigned i, j, k; unsigned char* new_ptr; unsigned new_scanline; col_count = 0; if (rns_ptr != 0 && rns_size == 6) { /* assume 8 bits per pixel */ col_count = 1; col_ptr[0] = rns_ptr[1]; col_ptr[1] = rns_ptr[3]; col_ptr[2] = rns_ptr[5]; *palrns_count = 1; palrns_ptr[0] = 0x0; } else { *palrns_count = 0; } new_scanline = width; new_ptr = data_alloc(height * new_scanline); for(i=0;i 1 && !opt_crc) cout << "File: " << argv[i] << endl; png_print(argv[i]); } } #if HAVE_GETOPT_LONG struct option long_options[] = { {"recompress", 0, 0, 'z'}, {"list", 0, 0, 'l'}, {"list-crc", 0, 0, 'L'}, {"shrink-store", 0, 0, '0'}, {"shrink-fast", 0, 0, '1'}, {"shrink-normal", 0, 0, '2'}, {"shrink-extra", 0, 0, '3'}, {"shrink-insane", 0, 0, '4'}, {"quiet", 0, 0, 'q'}, {"help", 0, 0, 'h'}, {"version", 0, 0, 'V'}, {0, 0, 0, 0} }; #endif #define OPTIONS "zlL01234fqhV" void version() { cout << PACKAGE " v" VERSION " by Andrea Mazzoleni" << endl; } void usage() { version(); cout << "Usage: advpng [options] [FILES...]" << endl; cout << endl; cout << "Modes:" << endl; cout << " " SWITCH_GETOPT_LONG("-l, --list ", "-l") " List the content of the files" << endl; cout << " " SWITCH_GETOPT_LONG("-z, --recompress ", "-z") " Recompress the specified files" << endl; cout << "Options:" << endl; cout << " " SWITCH_GETOPT_LONG("-0, --shrink-store ", "-0") " Don't compress" << endl; cout << " " SWITCH_GETOPT_LONG("-1, --shrink-fast ", "-1") " Compress fast" << endl; cout << " " SWITCH_GETOPT_LONG("-2, --shrink-normal ", "-2") " Compress normal" << endl; cout << " " SWITCH_GETOPT_LONG("-3, --shrink-extra ", "-3") " Compress extra" << endl; cout << " " SWITCH_GETOPT_LONG("-4, --shrink-insane ", "-4") " Compress extreme" << endl; cout << " " SWITCH_GETOPT_LONG("-f, --force ", "-f") " Force the new file also if it's bigger" << endl; cout << " " SWITCH_GETOPT_LONG("-q, --quiet ", "-q") " Don't print on the console" << endl; cout << " " SWITCH_GETOPT_LONG("-h, --help ", "-h") " Help of the program" << endl; cout << " " SWITCH_GETOPT_LONG("-V, --version ", "-V") " Version of the program" << endl; } void process(int argc, char* argv[]) { enum cmd_t { cmd_unset, cmd_recompress, cmd_list } cmd = cmd_unset; opt_quiet = false; opt_level = shrink_normal; opt_force = false; opt_crc = false; if (argc <= 1) { usage(); return; } int c = 0; opterr = 0; // don't print errors while ((c = #if HAVE_GETOPT_LONG getopt_long(argc, argv, OPTIONS, long_options, 0)) #else getopt(argc, argv, OPTIONS)) #endif != EOF) { switch (c) { case 'z' : if (cmd != cmd_unset) throw error() << "Too many commands"; cmd = cmd_recompress; break; case 'l' : if (cmd != cmd_unset) throw error() << "Too many commands"; cmd = cmd_list; break; case 'L' : if (cmd != cmd_unset) throw error() << "Too many commands"; cmd = cmd_list; opt_crc = true; break; case '0' : opt_level = shrink_none; opt_force = true; break; case '1' : opt_level = shrink_fast; break; case '2' : opt_level = shrink_normal; break; case '3' : opt_level = shrink_extra; break; case '4' : opt_level = shrink_extreme; break; case 'f' : opt_force = true; break; case 'q' : opt_quiet = true; break; case 'h' : usage(); return; case 'V' : version(); return; default: { // not optimal code for g++ 2.95.3 string opt; opt = (char)optopt; throw error() << "Unknown option `" << opt << "'"; } } } switch (cmd) { case cmd_recompress : rezip_all(argc - optind, argv + optind); break; case cmd_list : list_all(argc - optind, argv + optind); break; case cmd_unset : throw error() << "No command specified"; } } int main(int argc, char* argv[]) { try { process(argc, argv); } catch (error& e) { cerr << e << endl; exit(EXIT_FAILURE); } catch (std::bad_alloc) { cerr << "Low memory" << endl; exit(EXIT_FAILURE); } catch (...) { cerr << "Unknown error" << endl; exit(EXIT_FAILURE); } return EXIT_SUCCESS; }