3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Enrico Forestieri
9 * Full author contact details are available in file CREDITS.
11 * The code implements an utility program using the Qt-Framework
12 * to convert an image from a given format to another one.
13 * The image format of the files is either auto detected by Qt or
14 * may explicitely specified with command line arguments.
17 * lyxconvert [-d] [-f infmt] [-t outfmt] inputfile outputfile
18 * -d turn on debug messages
19 * -f format of input file (from)
20 * -t format of output file (to)
22 * Example to convert a compressed SVG image to PNG:
23 * lyxconvert image.svgz image.png
27 #include <QApplication>
31 #if (QT_VERSION >= 0x050300)
36 const char * basename(const char * name)
39 const char * slashpos = strrchr(name, '\\');
41 const char * slashpos = strrchr(name, '/');
44 if (NULL != slashpos) name = ++slashpos ;
49 void usage(const char * name)
51 std::cerr << "Usage: " << name
52 << " [-f infmt] [-t outfmt] input output" << std::endl;
57 void version(const char * name)
59 std::cerr << name << ": version 1.0" << std::endl;
64 bool isFileExt(const char * name, const char * ext)
66 const char * dotpos = strrchr(name, '.');
67 return NULL != dotpos && !strcmp(++dotpos, ext);
71 int main(int argc, char **argv)
74 const char * iformat = NULL;
75 const char * oformat = NULL;
76 const char * infile = NULL;
77 const char * outfile = NULL;
78 const char * myname = basename(argv[0]);
81 (char*)"-platform", (char*)"minimal",
83 int qtargsc = sizeof(qtargs) / sizeof(qtargs[0]) - 1;
84 bool debug = (1 == 0);
87 if ('-' == argv[arg][0] && !strcmp(argv[arg], "-platform")) {
88 qtargs[2] = argv[++arg]; arg++ ;
89 } else if ('-' == argv[arg][0] && 'f' == argv[arg][1]) {
90 iformat = argv[++arg]; arg++ ;
91 } else if ('-' == argv[arg][0] && 't' == argv[arg][1]) {
92 oformat = argv[++arg]; arg++ ;
93 } else if ('-' == argv[arg][0] && 'd' == argv[arg][1]) {
94 debug = (1 == 1); arg++;
95 } else if ('-' == argv[arg][0] && 'V' == argv[arg][1]) {
97 } else if ('-' == argv[arg][0]) {
99 } else if (NULL == infile) {
100 infile = argv[arg++];
101 } else if (NULL == outfile) {
102 outfile = argv[arg++];
103 if (NULL == oformat) {
104 if (isFileExt(outfile, "pdf")) {
106 } else if (isFileExt(outfile, "eps")) {
114 if (NULL == infile || NULL == outfile) {
118 QApplication app(qtargsc, &qtargs[0]);
119 QFile ifile(QString::fromLocal8Bit(infile));
123 std::cerr << myname << ": platform is " << (NULL == qtargs[2] ? "default" : qtargs[2]) << std::endl;
127 std::cerr << myname << ": Load file '" << infile <<
128 "', infmt is '" << (NULL == iformat ? "auto" : iformat) << "'" << std::endl;
130 if (!ifile.exists()) {
131 std::cerr << myname << ": Image file '" << infile << "' doesn't exist" << std::endl;
133 } else if (!img.load(ifile.fileName(), iformat)) {
134 std::cerr << myname << ": Cannot load image '" << infile << "'" << std::endl;
139 std::cerr << myname << ": Save converted image to file '" << outfile <<
140 "', outfmt is '" << (NULL == oformat ? "auto" : oformat) << "'" << std::endl;
142 if (NULL != oformat && !strcmp(oformat, "eps")) {
143 std::cerr << myname << ": Conversion of images to format '" << oformat << "' is not supported" << std::endl;
145 } else if (NULL != oformat && !strcmp(oformat, "pdf")) {
146 #if (QT_VERSION >= 0x050300)
147 QSize size = img.size();
148 QPdfWriter pdfwriter(QString::fromLocal8Bit(outfile));
149 int dpi = pdfwriter.logicalDpiX();
150 QPageSize pagesize(size * qreal(72.0 / dpi));
151 QMarginsF margins(0, 0, 0, 0);
152 QPageLayout pagelayout(pagesize, QPageLayout::Portrait, margins);
153 pdfwriter.setPageLayout(pagelayout);
154 QPainter painter(&pdfwriter);
155 painter.drawImage(0, 0, img);
158 std::cerr << myname << ": Conversion of images to format '" << oformat << "' is not supported" << std::endl;
161 } else if (!img.save(QString::fromLocal8Bit(outfile), oformat)) {
162 std::cerr << myname << ": Cannot save converted image to '" << outfile << "'" << std::endl;