37#include "chomp/system/config.h"
38#include "chomp/system/textfile.h"
39#include "chomp/system/timeused.h"
40#include "chomp/system/arg.h"
53PNGSMALL, ver. 0.01, 04/10/08. Copyright (C) 1997-2014 by Pawel Pilarczyk.\n\
54This is free software. No warranty. Consult 'license.txt' for details.";
60This program reads a given PNG image and writes another PNG image\n\
61which is the original one scaled down so that the provided width\n\
62and height are not exceeded. The down-scaling factors are limited\n\
63to the integer numbers for optimal efficiency. The colors of output\n\
64pixels are computed by means of an arithmetic average of the input ones.\n\
65Command line arguments:\n\
66input.png - the name of the input PNG image file to be down-scaled,\n\
67output.png - the name of the output PNG file (rewrites the input if none),\n\
68-w width - the maximal allowed width of the output PNG image,\n\
69-h height - the maximal allowed height of the output PNG image,\n\
70-s scaling - the downscaling factor (overrides max width/height settings),\n\
71--quiet - makes the program run quietly, without any output messages,\n\
72--help - shows this brief help information on the program's usage.\n\
73For more information consult the accompanying documentation (if available)\n\
74or ask the program's author at http://www.PawelPilarczyk.com/.";
87int pngSmall (
const char *inName,
const char *outName,
88 int maxWidth,
int maxHeight,
int scalingFactor)
90 using chomp::homology::sout;
94 int inWidth = inPng. width ();
95 int inHeight = inPng. height ();
98 if (scalingFactor <= 0)
103 widthFactor = (inWidth + maxWidth - 1) / maxWidth;
105 int heightFactor = 0;
108 heightFactor = (inHeight + maxHeight - 1) /
111 scalingFactor = (widthFactor < heightFactor) ?
112 heightFactor : widthFactor;
113 if (scalingFactor <= 0)
118 if (scalingFactor == 1)
121 if (!outName || !strcmp (inName, outName))
123 sout << inWidth <<
" x " << inHeight <<
124 ": No downscaling necessary. "
125 "Leaving the file intact.\n";
128 sout << inWidth <<
" x " << inHeight <<
129 ": No downscaling necessary. "
130 "Copying the file byte-for-byte.\n";
131 FILE *inFile = fopen (inName,
"rb");
132 FILE *outFile = fopen (outName,
"wb");
133 int ch = getc (inFile);
145 int outWidth = inWidth / scalingFactor;
148 int outHeight = inHeight / scalingFactor;
151 PngImage outPng (outWidth, outHeight);
154 sout <<
"Downscaling " << inName;
156 sout <<
" -> " << outName;
157 sout <<
":\n" << inWidth <<
" x " << inHeight <<
" -> " <<
158 outWidth <<
" x " << outHeight <<
" (" << scalingFactor <<
159 " times smaller).\n";
162 for (
int outX = 0; outX < outWidth; ++ outX)
164 for (
int outY = 0; outY < outHeight; ++ outY)
172 int inX = outX * scalingFactor;
173 int inY = outY * scalingFactor;
174 for (
int x = 0; x < scalingFactor; ++ x)
176 for (
int y = 0; y < scalingFactor; ++ y)
178 int color = inPng. getPixel
180 red += (color >> 16) & 0xFF;
181 green += (color >> 8) & 0xFF;
182 blue += color & 0xFF;
187 int n = scalingFactor * scalingFactor;
188 red = (red + (n >> 1)) / n;
189 green = (green + (n >> 1)) / n;
190 blue = (blue + (n >> 1)) / n;
193 int color = (red << 16) | (green << 8) | blue;
194 outPng. putPixel (outX, outY, color);
199 outPng. save (outName ? outName : inName);
210int main (
int argc,
char *argv [])
212 using namespace chomp::homology;
219 int scalingFactor = 0;
225 arg (a,
"w", maxWidth);
226 arg (a,
"h", maxHeight);
227 arg (a,
"s", scalingFactor);
230 argstreamprepare (a);
231 int argresult = a. analyze (argc, argv);
240 sout <<
title <<
'\n';
245 sout <<
"Call with '--help' for help.\n";
260 program_time =
"Aborted after:";
264 pngSmall (inName, outName, maxWidth, maxHeight,
268 program_time =
"Total time used:";
273 catch (
const char *msg)
275 sout <<
"ERROR: " << msg <<
'\n';
278 catch (
const std::exception &e)
280 sout <<
"ERROR: " << e. what () <<
'\n';
285 sout <<
"ABORT: An unknown error occurred.\n";
An interface to the PNG library.
int main(int argc, char *argv[])
The main procedure of the program.
const char * helpinfo
The help information about the program which is displayed if the program is launched without command-...
int pngSmall(const char *inName, const char *outName, int maxWidth, int maxHeight, int scalingFactor)
The main function of the program "pngsmall".
const char * title
The title of the program which is displayed every time the program is launched.