File: ImageFileFilter.cpp

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (191 lines) | stat: -rw-r--r-- 6,186 bytes parent folder | download | duplicates (2)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//##########################################################################
//#                                                                        #
//#                              CLOUDCOMPARE                              #
//#                                                                        #
//#  This program is free software; you can redistribute it and/or modify  #
//#  it under the terms of the GNU General Public License as published by  #
//#  the Free Software Foundation; version 2 or later of the License.      #
//#                                                                        #
//#  This program is distributed in the hope that it will be useful,       #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          #
//#  GNU General Public License for more details.                          #
//#                                                                        #
//#          COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI)             #
//#                                                                        #
//##########################################################################

#include "ImageFileFilter.h"
#include "FileIO.h"

//qCC_db
#include <ccImage.h>

//Qt
#include <QImageReader>
#include <QImageWriter>
#include <QFileInfo>
#include <QImage>
#include <QFileDialog>

//System
#include <assert.h>

ImageFileFilter::ImageFileFilter()
	: FileIOFilter()
{
	//output filters
	{
		//we grab the list of supported image file formats (for writing)
		QList<QByteArray> formats = QImageWriter::supportedImageFormats();
		//we convert this list into a proper "filters" string
		for (int i = 0; i < formats.size(); ++i)
		{
			m_outputFilters.append(QString("%1 image (*.%2)").arg(QString(formats[i].data()).toUpper(),formats[i].data()));
		}
	}

	//input filters
	{
		//we grab the list of supported image file formats (for reading)
		QList<QByteArray> formats = QImageReader::supportedImageFormats();
		QStringList imageExts;
		for (int i = 0; i < formats.size(); ++i)
		{
			imageExts.append(QString("*.%1").arg(formats[i].data()));
		}
		//we convert this list into a proper "filters" string
		if (!imageExts.empty())
		{
			m_inputFilter = QString("Image (%1)").arg(imageExts.join(" "));
		}
	}
}

QString ImageFileFilter::GetSaveFilename(const QString& dialogTitle, const QString& baseName, const QString& imageSavePath, QWidget* parentWidget/*=0*/)
{
	//add images output file filters
	QString filters;

	//we grab the list of supported image file formats (writing)
	QList<QByteArray> formats = QImageWriter::supportedImageFormats();
	if (formats.empty())
	{
		ccLog::Error("No image format supported by your system?!\n(check that the 'imageformats' directory is alongside the application executable)");
		return QString();
	}

	//we convert this list into a proper "filters" string
	QString pngFilter;
	for (int i = 0; i < formats.size(); ++i)
	{
		QString ext = QString(formats[i].data()).toUpper();
		QString filter = QString("%1 image (*.%2)").arg(ext,formats[i].data());
		filters.append(filter + QString("\n"));

		//find PNG by default
		if (pngFilter.isEmpty() && ext == "PNG")
		{
			pngFilter = filter;
		}
	}

	QString outputFilename = QFileDialog::getSaveFileName(	parentWidget,
															dialogTitle,
															imageSavePath + QString("/%1.%2").arg(baseName, pngFilter.isEmpty() ? QString(formats[0].data()) : QString("png")),
															filters,
															pngFilter.isEmpty() ? static_cast<QString*>(0) : &pngFilter);

	return outputFilename;
}

QString ImageFileFilter::GetLoadFilename(const QString& dialogTitle, const QString& imageLoadPath, QWidget* parentWidget/*=0*/)
{
	//we grab the list of supported image file formats (for reading)
	QList<QByteArray> formats = QImageReader::supportedImageFormats();
	QStringList imageExts;
	for (int i = 0; i < formats.size(); ++i)
	{
		imageExts.append(QString("*.%1").arg(formats[i].data()));
	}
	//we convert this list into a proper "filters" string
	QString imageFilter = QString("Image (%1)").arg(imageExts.join(" "));

	return QFileDialog::getOpenFileName(	parentWidget,
											dialogTitle,
											imageLoadPath,
											imageFilter);
}

QStringList ImageFileFilter::getFileFilters(bool onImport) const
{
	return onImport ? QStringList(m_inputFilter) : m_outputFilters;
}

bool ImageFileFilter::canLoadExtension(const QString& upperCaseExt) const
{
	//we grab the list of supported image file formats (for reading)
	QList<QByteArray> formats = QImageReader::supportedImageFormats();
	//we convert this list into a proper "filters" string
	for (int i=0; i<formats.size(); ++i)
		if (QString(formats[i].data()).toUpper() == upperCaseExt)
			return true;

	return false;
}

bool ImageFileFilter::canSave(CC_CLASS_ENUM type, bool& multiple, bool& exclusive) const
{
	if (type == CC_TYPES::IMAGE)
	{
		multiple = false;
		exclusive = true;
		return true;
	}
	return false;
}

CC_FILE_ERROR ImageFileFilter::saveToFile(ccHObject* entity, const QString& filename, const SaveParameters& parameters)
{
	if (!entity)
		return CC_FERR_BAD_ARGUMENT;

	ccImage* image = ccHObjectCaster::ToImage(entity);
	if (!image)
		return CC_FERR_BAD_ENTITY_TYPE;

	if (image->data().isNull() || image->getW() == 0 || image->getH() == 0)
	{
		ccLog::Warning(QString("[IMAGE] Image '%1' is empty!").arg(image->getName()));
		return CC_FERR_NO_SAVE;
	}
	
	QImageWriter writer(filename);
	
	writer.setText("Author", FileIO::writerInfo());
	
	if (!writer.write(image->data()))
	{
		ccLog::Warning(QString("[IMAGE] Failed to save image in '%1").arg(filename));
		return CC_FERR_CONSOLE_ERROR;
	}
	
	return CC_FERR_NO_ERROR;
}

CC_FILE_ERROR ImageFileFilter::loadFile(const QString& filename, ccHObject& container, LoadParameters& parameters)
{
	QImage qImage;
	if (!qImage.load(filename))
	{
		ccLog::Warning(QString("[IMAGE] Failed to load image '%1").arg(filename));
		return CC_FERR_CONSOLE_ERROR;
	}

	//create corresponding ccImage
	ccImage* image = new ccImage(qImage,QFileInfo(filename).baseName());

	container.addChild(image);

	return CC_FERR_NO_ERROR;
}