1 // pngrewrite.c - part of pngrewrite
2 // Copyright (C) 2010 by Jason Summers
3
4 //#define PNGRW_SUPPORT_1_ARG_MODE
5 #define PNGRW_PALSORT_BY_FREQUENCY
6
7 #if defined(_WIN32) && !defined(__GNUC__) && !defined(PNGRW_WINDOWS)
8 #define PNGRW_WINDOWS
9 #endif
10
11 #ifdef PNGRW_WINDOWS
12 #include <tchar.h>
13 #endif
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18
19 #ifdef PNGRW_WINDOWS
20 #include <io.h>
21 #include <fcntl.h>
22 #else
23 #include <unistd.h>
24 #endif
25
26 #include "libpngrewrite.h"
27
28 #ifndef PNGRW_WINDOWS
29 #define TCHAR char
30 #define _T
31 #define _ftprintf fprintf
32 #define _tcscmp strcmp
33 #define _fileno fileno
34 #define _tmain main
35 #define _isatty isatty
36 #endif
37
my_error_fn(struct pngrw_ctx * ctx,const PNGRW_CHAR * msg)38 static void my_error_fn(struct pngrw_ctx *ctx, const PNGRW_CHAR *msg)
39 {
40 _ftprintf(stderr,_T("%s\n"),msg);
41 }
42
my_print_fn(struct pngrw_ctx * ctx,const PNGRW_CHAR * msg)43 static void my_print_fn(struct pngrw_ctx *ctx, const PNGRW_CHAR *msg)
44 {
45 _ftprintf(stderr,_T("%s\n"),msg);
46 }
47
48 // For stdin/stdout, pass "-" as the filename.
run(const TCHAR * in_filename,const TCHAR * out_filename)49 static int run(const TCHAR *in_filename, const TCHAR *out_filename)
50 {
51 struct pngrw_ctx *ctx = NULL;
52 int retval=0;
53
54 ctx = pngrw_create();
55 if(!ctx) goto done;
56 pngrw_set_print_fn(ctx,my_print_fn);
57 pngrw_set_error_fn(ctx,my_error_fn);
58
59 #ifdef PNGRW_PALSORT_BY_FREQUENCY
60 pngrw_set_sort_by_frequency(ctx,1);
61 #else
62 pngrw_set_sort_by_frequency(ctx,0);
63 #endif
64
65 // Prepare input file.
66 if(!_tcscmp(in_filename,_T("-"))) {
67 #ifdef PNGRW_WINDOWS
68 _setmode(_fileno(stdin), O_BINARY);
69 #endif
70 if(!pngrw_read_stdio(ctx,stdin)) {
71 goto done;
72 }
73 }
74 else {
75 if(!pngrw_read_filename(ctx,in_filename)) {
76 goto done;
77 }
78 }
79
80 if(!pngrw_optimize_image(ctx)) {
81 goto done;
82 }
83
84 // Prepare output file.
85 if(!_tcscmp(out_filename,_T("-"))) {
86 #ifdef PNGRW_WINDOWS
87 _setmode(_fileno(stdout), O_BINARY);
88 #endif
89 if(!pngrw_write_stdio(ctx,stdout)) {
90 goto done;
91 }
92 }
93 else {
94 if(!pngrw_write_filename(ctx,out_filename)) {
95 goto done;
96 }
97 }
98
99 retval = 1;
100
101 done:
102 if(ctx) pngrw_destroy(ctx);
103 return retval;
104 }
105
_tmain(int argc,TCHAR ** argv)106 int _tmain(int argc, TCHAR **argv)
107 {
108 TCHAR *out_filename;
109 TCHAR *in_filename;
110 int ret;
111 TCHAR specstamp[38]=_T("");
112
113 if(argc==3) {
114 in_filename = argv[1];
115 out_filename = argv[2];
116 }
117 #ifdef PNGRW_SUPPORT_1_ARG_MODE
118 else if( argc==2 && !_isatty(_fileno(stdin)) ) {
119 in_filename = _T("-");
120 out_filename = argv[1];
121 }
122 #endif
123 else {
124 _ftprintf(stderr, _T("pngrewrite v%s: PNG image palette optimizer%s\n"),
125 pngrw_get_version_string(),
126 specstamp);
127 #ifdef PNGRW_SUPPORT_1_ARG_MODE
128 _ftprintf(stderr, _T("Usage: %s infile.png outfile.png OR |%s outfile.png\n"), argv[0], argv[0]);
129 #else
130 _ftprintf(stderr, _T("Usage: %s infile.png outfile.png\n"), argv[0]);
131 #endif
132 return 1;
133 }
134
135 ret = run(in_filename,out_filename);
136 if(!ret) return 1;
137 return 0;
138 }
139