50 #include "FileHeightFieldImage.hh"
51 #include "ImageDialog.hh"
53 #if QT_VERSION >= 0x050000
62 QList<QByteArray> imageFormats = QImageReader::supportedImageFormats();
65 if ( imageFormats.contains(
"jpeg")) {
66 formats +=
"*.jpeg *.jpg ";
69 if ( imageFormats.contains(
"tiff")) {
73 if ( imageFormats.contains(
"tif")) {
77 if ( imageFormats.contains(
"png")) {
81 return QString( tr(
"HeightField PNG files ( ")+ formats +
")" );
93 bool FileHeightFieldPNGPlugin::showImageDialog(
const QImage& _image,
int* _minX,
int* _maxX,
int* _minY,
int* _maxY,
double* _height)
96 imageDialog.heightValue->setValue(*_height);
98 int result = imageDialog.exec();
100 if ( result == QDialog::Accepted) {
101 *_minX = imageDialog.minX->value();
102 *_maxX = imageDialog.maxX->value();
103 *_minY = imageDialog.minY->value();
104 *_maxY = imageDialog.maxY->value();
105 *_height = imageDialog.heightValue->value();
113 int FileHeightFieldPNGPlugin::loadObject(QString _filename)
116 QFile file(_filename);
117 if ( !file.exists() ) {
118 emit log(
LOGERR,tr(
"Unable to load file: ") + _filename);
122 QImage image(_filename);
126 int maxX = image.width();
129 int maxY = image.height();
132 double height = image.height() / 100;
134 if ( OpenFlipper::Options::gui() ) {
137 QMetaObject::invokeMethod(
this,
"showImageDialog",
139 (QThread::currentThread() != QApplication::instance()->thread()) ? Qt::BlockingQueuedConnection: Qt::DirectConnection,
140 Q_RETURN_ARG(
bool, ret),
141 Q_ARG(
const QImage&, image),
146 Q_ARG(
double*,&height));
158 TriMesh* mesh =
object->mesh();
164 loadImageAsTriangleMesh(image,mesh,minX ,maxX , minY ,maxY, height);
170 emit openedFile(
id );
178 void FileHeightFieldPNGPlugin::loadImageAsTriangleMesh(QImage& _image,TriMesh* _mesh,
int _minX ,
int _maxX ,
int _minY ,
int _maxY,
double _height) {
180 const int mWidth = _maxY - _minY;
181 const int mHeigth = _maxX - _minX;
184 _mesh->reserve( mWidth*mHeigth, mWidth*mHeigth * 4, mWidth*mHeigth * 2 );
186 for (
int i = _minX ; i < _maxX ; ++i ) {
187 for (
int j = _minY ; j < _maxY ; ++j ) {
188 const QColor currentColor = _image.pixel(i,j);
189 double value = std::max(currentColor.redF(),currentColor.blueF());
190 value = std::max(currentColor.greenF(),value);
191 TriMesh::VertexHandle vh = _mesh->add_vertex(TriMesh::Point(i,j,-value * _height));
192 _mesh->set_color(vh,
TriMesh::Color(currentColor.redF(),currentColor.greenF(),currentColor.blueF(),1.0f));
197 for (
int i = 0 ; i < mHeigth - 1 ; ++i ) {
198 const int upperStart = mWidth * i;
199 const int lowerStart = mWidth * (i + 1);
201 for (
int j = 0 ; j < mWidth -1 ; ++j ) {
202 std::vector<TriMesh::VertexHandle> handles;
203 handles.push_back( _mesh->vertex_handle(upperStart + j) );
204 handles.push_back( _mesh->vertex_handle(lowerStart + j + 1) );
205 handles.push_back( _mesh->vertex_handle(lowerStart + j) );
208 _mesh->add_face(handles);
212 handles.push_back( _mesh->vertex_handle(upperStart + j) );
213 handles.push_back( _mesh->vertex_handle(upperStart + j + 1) );
214 handles.push_back( _mesh->vertex_handle(lowerStart + j + 1) );
216 _mesh->add_face(handles);
221 _mesh->update_normals();
225 bool FileHeightFieldPNGPlugin::saveObject(
int _id, QString _filename) {
229 #if QT_VERSION < 0x050000
Add colors to mesh item (vertices/faces/edges)
DataType supportedType()
Return your supported object type( e.g. DATA_TRIANGLE_MESH )
Type for a MeshObject containing a triangle mesh.
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
bool getObject(int _identifier, BSplineCurveObject *&_object)
void initializePlugin()
Initialize Plugin.
#define DATA_TRIANGLE_MESH