"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.5/imageutils/pngrecolor.c" (29 Oct 2021, 3735 Bytes) of package /linux/privat/littleutils-1.2.5.tar.lz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pngrecolor.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.4_vs_1.2.5.

    1 // pngrecolor.c - part of pngrecolor
    2 // Copyright (C) 2010 by Jason Summers (from original pngrewrite.c)
    3 // Modifications (C) 2013-2021 by Brian Lindholm
    4 //   [1] Renamed from "pngrewrite" to "pngrecolor" at avoid confusion with
    5 //       original program.
    6 //   [2] Modified palette comparison routine to ensure a *unique* sorting.
    7 //   [3] Added command-line options "-n" and "-q" for no-interlacing and quiet
    8 //       output.
    9 //   These modifications may be used without restriction.
   10 
   11 #include <config.h>
   12 
   13 #define PNGRW_PALSORT_BY_FREQUENCY
   14 
   15 #if defined(_WIN32) && !defined(__GNUC__) && !defined(PNGRW_WINDOWS)
   16 # define PNGRW_WINDOWS
   17 #endif
   18 
   19 #ifdef PNGRW_WINDOWS
   20 # include <fcntl.h>
   21 # include <io.h>
   22 # include <tchar.h>
   23 #endif
   24 
   25 #ifdef HAVE_STDIO_H
   26 # include <stdio.h>
   27 #endif
   28 #ifdef HAVE_STDLIB_H
   29 # include <stdlib.h>
   30 #endif
   31 #ifdef HAVE_STRING_H
   32 # include <string.h>
   33 #endif
   34 
   35 #ifdef HAVE_UNISTD_H
   36 # include <unistd.h>
   37 # define OPTEND -1
   38 #else
   39 # define OPTEND EOF
   40 #endif
   41 #ifdef HAVE_GETOPT_H
   42 # include <getopt.h>
   43 #endif
   44 
   45 #ifdef __MINGW32__
   46 extern int getopt (int argc, char * const *argv, const char *optstring);
   47 extern char *optarg;
   48 extern int optind;
   49 #endif
   50 
   51 #include "libpngrecolor.h"
   52 
   53 #ifndef PNGRW_WINDOWS
   54 # define TCHAR char
   55 # define _T
   56 # define _ftprintf fprintf
   57 # define _tcscmp   strcmp
   58 # define _fileno   fileno
   59 # define _tmain    main
   60 # define _isatty   isatty
   61 #endif
   62 
   63 static void my_error_fn(struct pngrw_ctx *ctx, const PNGRW_CHAR *msg)
   64 {
   65     _ftprintf(stderr,_T("%s\n"),msg);
   66 }
   67 
   68 static void my_print_fn(struct pngrw_ctx *ctx, const PNGRW_CHAR *msg)
   69 {
   70     _ftprintf(stderr,_T("%s\n"),msg);
   71 }
   72 
   73 // For stdin/stdout, pass "-" as the filename.
   74 static int run(const TCHAR *in_filename, const TCHAR *out_filename, const int no_interlace, const int verbose)
   75 {
   76     struct pngrw_ctx *ctx = NULL;
   77     int retval=0;
   78 
   79     ctx = pngrw_create();
   80     if(!ctx) goto done;
   81     pngrw_set_print_fn(ctx,my_print_fn);
   82     pngrw_set_error_fn(ctx,my_error_fn);
   83 
   84 #ifdef PNGRW_PALSORT_BY_FREQUENCY
   85     pngrw_set_sort_by_frequency(ctx,1);
   86 #else
   87     pngrw_set_sort_by_frequency(ctx,0);
   88 #endif
   89 
   90     // Prepare input file.
   91     if(!_tcscmp(in_filename,_T("-"))) {
   92 #ifdef PNGRW_WINDOWS
   93         _setmode(_fileno(stdin), O_BINARY);
   94 #endif
   95         if(!pngrw_read_stdio(ctx,stdin,verbose)) {
   96             goto done;
   97         }
   98     }
   99     else {
  100         if(!pngrw_read_filename(ctx,in_filename,verbose)) {
  101             goto done;
  102         }
  103     }
  104 
  105     if(!pngrw_optimize_image(ctx,verbose)) {
  106         goto done;
  107     }
  108 
  109     // Prepare output file.
  110     if(!_tcscmp(out_filename,_T("-"))) {
  111 #ifdef PNGRW_WINDOWS
  112         _setmode(_fileno(stdout), O_BINARY);
  113 #endif
  114         if(!pngrw_write_stdio(ctx,stdout,no_interlace)) {
  115             goto done;
  116         }
  117     }
  118     else {
  119         if(!pngrw_write_filename(ctx,out_filename,no_interlace)) {
  120             goto done;
  121         }
  122     }
  123 
  124     retval = 1;
  125 
  126 done:
  127     if(ctx) pngrw_destroy(ctx);
  128     return retval;
  129 }
  130 
  131 static void help (FILE *where)
  132 {
  133     fprintf (where, "pngrecolor " PACKAGE_VERSION "\n"
  134     "usage: pngrecolor [-h(elp)] [-n(o_interlace)] [-q(uiet)] infile outfile\n");
  135 }
  136 
  137 int _tmain(int argc, TCHAR **argv)
  138 {
  139     TCHAR *out_filename;
  140     TCHAR *in_filename;
  141     int c, no_interlace, ret, verbose ;
  142     TCHAR specstamp[38]=_T("");
  143 
  144     /* Parse command-line options */
  145     verbose = 1;
  146     no_interlace = 0;
  147     while ((c = getopt (argc, argv, "hnq")) != OPTEND)
  148         switch (c) {
  149             case 'h':
  150                 help (stdout);
  151                 return (0);
  152             case 'n':
  153                 no_interlace = 1;
  154                 break;
  155             case 'q':
  156                 verbose = 0;
  157                 break;
  158             case '?':
  159                 help (stderr);
  160                 return (1);
  161         }
  162 
  163     /* Ensure that two files were given as input */
  164     if ((argc - optind) != 2) {
  165         _ftprintf (stderr, _T("pngrecolor error: missing input or output file%s\n"), specstamp);
  166         return 1;
  167     }
  168     else {
  169         in_filename = argv[optind];
  170         out_filename = argv[optind + 1];
  171     }
  172 
  173     ret = run(in_filename,out_filename,no_interlace,verbose);
  174     if(!ret) return 1;
  175     return 0;
  176 }