-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpngconv.cpp
159 lines (134 loc) · 4.42 KB
/
pngconv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// pngconv.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "pngconv.h"
#include "CommandLinePngConv.h"
#include "PngImage.h"
#include "RGBAImage.h"
#include "ZxImage.h"
#include "exceptions.h"
#include <fstream>
#include <ostream>
int main(int argc, char* argv[])
{
int nRetCode = 0;
CCommandLinePngConv cmdLine;
int error = cmdLine.ParseCommandLine(argc, argv);
if (!cmdLine.m_bSilent || cmdLine.m_bVersion)
{
// output some header/credit information
std::cout << PNGCONV_SHORT_TITLE << " : " << PNGCONV_LONG_TITLE << " v" << PNGCONV_VERSION_STRING << PNGCONV_RELEASE << std::endl;
std::cout << PNGCONV_COPYRIGHT << std::endl << std::endl;
}
if (cmdLine.m_bVersion)
{
std::cout << "Built on " << PNGCONV_BUILD_DATE << " @ " << PNGCONV_BUILD_TIME << std::endl << std::endl;
std::cout << "Includes the following libraries:" << std::endl;
std::cout << " libpng v" << PNG_LIBPNG_VER_STRING << std::endl;
std::cout << " zlib v" << ZLIB_VERSION << std::endl;
}
else if (cmdLine.m_bDisplayHelp || 1 == argc || cmdLine.m_sPngFileName.empty())
{
std::cout << cmdLine.Help() << std::endl;
}
else if (error)
{
std::cerr << "Error with parameter " << error << ": " << argv[error] << std::endl;
nRetCode = 1;
}
else
{
// here we'll grab all of our values and create the object that'll do the conversion.
try
{
CPngImage pngimage(cmdLine.m_sPngFileName);
// if we get here we should have the file open, so read in the image
pngimage.ReadImage();
// get the projected image size and create a temporary RGBA image...
POINT pt = cmdLine.m_pSize;
if (pt.x < 1 || pt.y < 1)
{
pt = pngimage.GetImageSize();
}
CRGBAImage rgbaimg(pt);
// now pass in the CPngImage so we can extract the data we want...
rgbaimg.ExtractImageData(pngimage, cmdLine.m_pOrigin);
// we've done that, now we can free up the pngimage
pngimage.Close();
// now we'll create a ZX Image in the appropriate format
CZxImage zximg(pt);
// set up our conversion options
zximg.InvertMask(cmdLine.m_bInverseMask);
zximg.InvertByte(cmdLine.m_bInverseByte);
zximg.SetMaskFormat(cmdLine.m_nMaskFormat);
if (cmdLine.m_bUseRGBMask)
zximg.SetRGBMask(cmdLine.m_rgbaMaskColour);
zximg.ProcessRGBAImage(rgbaimg);
// set the output format
zximg.SetTextOutput(cmdLine.m_bTxtOut);
zximg.SetReverseOutput(cmdLine.m_bReverse);
zximg.SetUpsideDown(cmdLine.m_bUpsideDown);
zximg.SetZigZag(cmdLine.m_bZigZag);
zximg.SetLeadText(cmdLine.m_sLeadText);
zximg.SetHexOutput(cmdLine.m_bHexOutput, cmdLine.m_sLeadTextByte);
// now we'll set up our output stream.
// we'll send to either stdout or a file.
// set to stdout here by default, and use a file if need be.
std::ofstream fileout;
std::ostream out(std::cout.rdbuf());
if (!cmdLine.m_bStdOut)
{
// validate the filename
if (cmdLine.m_sOutFileName.empty() || (cmdLine.m_sOutFileName == cmdLine.m_sPngFileName))
{
cmdLine.m_sOutFileName = cmdLine.m_sPngFileName + ".out";
}
// open the file
int open_flags = std::ios::out;
if (!cmdLine.m_bTxtOut)
open_flags |= std::ios::binary;
if (cmdLine.m_bOutputAppend)
open_flags |= std::ios::app;
fileout.open(cmdLine.m_sOutFileName.c_str(), open_flags);
if (!fileout.is_open())
{
throw CFileException(cmdLine.m_sOutFileName);
}
else
{
out.rdbuf(fileout.rdbuf());
}
}
// here we'll pass in the ostream so that
// we can actually output the data. w00t.
out << zximg;
if (fileout.is_open())
fileout.close();
}
catch (CFileException &e)
{
std::cerr << "Error accessing file \"" << e.what() << "\"" << std::endl;
}
catch (CPngImage::CPngFileException &e)
{
std::cerr << "Error with input file: " << e.what() << std::endl;
}
catch (CPngImage::CPngLibException &e)
{
std::cerr << "Error in pnglib: " << e.what() << std::endl;
}
catch (CRGBAImage::CExBadSize &e)
{
std::cerr << "Error in output image dimensions: " << e.what() << std::endl;
}
catch (CZxImage::CExUnknownMask &e)
{
std::cerr << "Error - unknown mask: " << e.what() << std::endl;
}
catch (std::bad_alloc &e)
{
std::cerr << "Error allocating image memory: " << e.what() << std::endl;
}
}
return nRetCode;
}