Editor: Import Heightmap from image

Function similar to MuwuMs tool https://forum.megaglest.org/index.php?topic=8353.0
Image is resized to the dimensions of the map. Changes in the ratio only work with some image formats (png,jpg) wxWidget bug?
This commit is contained in:
titison 2021-03-16 15:19:40 +01:00
parent b20002e7dc
commit 880730e5e1
6 changed files with 66 additions and 1 deletions

View File

@ -18,6 +18,7 @@
#include <iostream>
#include"platform_util.h"
#include <wx/stdpaths.h>
#include "wx/image.h"
#ifndef WIN32
#include <errno.h>
#endif
@ -181,6 +182,7 @@ void MainWindow::init(string fname) {
// ---------------------------------------------------------
menuEdit->Append(miEditRandomizeHeights, wxT("Randomize &Heights"));
menuEdit->Append(miEditImportHeights, wxT("Import Heights from Image"));
menuEdit->Append(miEditRandomize, wxT("Randomi&ze Players"));
menuEdit->Append(miEditSwitchSurfaces, wxT("Switch Sur&faces..."));
menuEdit->Append(miEditInfo, wxT("&Info..."));
@ -1067,6 +1069,52 @@ void MainWindow::onMenuEditRandomizeHeights(wxCommandEvent &event) {
}
}
void MainWindow::onMenuEditImportHeights(wxCommandEvent &event) {
if(program == NULL) {
return;
}
try {
fileDialog->SetMessage(wxT("Select heigthmap image to load"));
fileDialog->SetWildcard(wxT("All Images|*.bmp;*.png;*.jpg;*.jpeg;*.gif;.*.tga;*.tiff;*.tif|PNG-Image (*.png)|*.png|JPEG-Image (*.jpg, *.jpeg)|*.jpg;*.jpeg|BMP-Image (*.bmp)|*.bmp|GIF-Image (*.gif)|*.gif|TIFF-Image (*.tif, *.tiff)|*.tiff;*.tif"));
if (fileDialog->ShowModal() == wxID_OK) {
#ifdef WIN32
const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(wxFNCONV(fileDialog->GetPath()));
currentFile = tmp_buf;
auto_ptr<wchar_t> wstr(Ansi2WideString(currentFile.c_str()));
currentFile = utf8_encode(wstr.get());
#elif wxCHECK_VERSION(2, 9, 1)
currentFile = fileDialog->GetPath().ToStdString();
#else
const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(fileDialog->GetPath());
currentFile = tmp_buf;
#endif
wxImage* img=new wxImage(currentFile);
if(img != NULL) {
wxImage grey_img = img->ConvertToGreyscale();
delete img;
int w =grey_img.GetWidth();
int h =grey_img.GetHeight();
int map_w = program->getMap()->getW();
int map_h = program->getMap()->getH();
program->setUndoPoint(ctAll);
if(w != map_w && h != map_h) {
grey_img.Rescale(map_w,map_h,wxIMAGE_QUALITY_HIGH);
}
program->importMapHeights(grey_img.GetData());
setDirty();
}
}
}
catch (const string &e) {
MsgDialog(this, ToUnicode(e), wxT("Exception"), wxOK | wxICON_ERROR).ShowModal();
}
catch (const exception &e) {
MsgDialog(this, ToUnicode(e.what()), wxT("Exception"), wxOK | wxICON_ERROR).ShowModal();
}
}
void MainWindow::onMenuEditRandomize(wxCommandEvent &event) {
if(program == NULL) {
return;
@ -1464,6 +1512,7 @@ BEGIN_EVENT_TABLE(MainWindow, wxFrame)
EVT_MENU(miEditRotatecopyCorner, MainWindow::onMenuEditRotatecopyCorner)
EVT_MENU(miEditRandomizeHeights, MainWindow::onMenuEditRandomizeHeights)
EVT_MENU(miEditImportHeights, MainWindow::onMenuEditImportHeights)
EVT_MENU(miEditRandomize, MainWindow::onMenuEditRandomize)
EVT_MENU(miEditSwitchSurfaces, MainWindow::onMenuEditSwitchSurfaces)
EVT_MENU(miEditInfo, MainWindow::onMenuEditInfo)

View File

@ -137,6 +137,7 @@ private:
miEditMirror,
miEditRandomizeHeights,
miEditImportHeights,
miEditRandomize,
miEditSwitchSurfaces,
miEditInfo,
@ -254,6 +255,7 @@ public:
void onMenuEditRotatecopyCorner(wxCommandEvent &event); // copy top left 1/4 to top right 1/4, rotated
void onMenuEditRandomizeHeights(wxCommandEvent &event);
void onMenuEditImportHeights(wxCommandEvent &event);
void onMenuEditRandomize(wxCommandEvent &event);
void onMenuEditSwitchSurfaces(wxCommandEvent &event);
void onMenuEditInfo(wxCommandEvent &event);

View File

@ -565,6 +565,10 @@ void Program::randomizeMapHeights(bool withReset,int minimumHeight, int maximumH
if(map) map->randomizeHeights(withReset, minimumHeight, maximumHeight, chanceDivider, smoothRecursions);
}
void Program::importMapHeights(unsigned char* data) {
if(map) map->importMapHeights(data);
}
void Program::randomizeFactions() {
if(map) map->randomizeFactions();
}

View File

@ -151,7 +151,8 @@ public:
void shiftUp();
void shiftDown();
void randomizeMapHeights(bool withReset, int minimumHeight, int maximumHeight, int chanceDivider, int smoothRecursions);;
void randomizeMapHeights(bool withReset, int minimumHeight, int maximumHeight, int chanceDivider, int smoothRecursions);
void importMapHeights(unsigned char* data);
void randomizeFactions();
void switchMapSurfaces(int surf1, int surf2);
void loadMap(const string &path);

View File

@ -208,6 +208,7 @@ public:
void resize(int w, int h, float alt, MapSurfaceType surf);
void resetFactions(int maxFactions);
void randomizeHeights(bool withReset,int minimumHeight, int maximumHeight, int chanceDevider, int smoothRecursions);
void importMapHeights(unsigned char* data);
void randomizeFactions();
void smoothSurface(bool limitHeights);
void switchSurfaces(MapSurfaceType surf1, MapSurfaceType surf2);

View File

@ -710,6 +710,14 @@ void MapPreview::randomizeHeights(bool withReset,int minimumHeight, int maximumH
hasChanged = true;
}
void MapPreview::importMapHeights(unsigned char* data) {
for (int i = 0; i < w; ++i) {
for (int j = 0; j < h; ++j) {
cells[i][j].height=(data[(i+j*w)*3]*MAX_MAP_CELL_HEIGHT/255)+MIN_MAP_CELL_HEIGHT;
}
}
}
void MapPreview::randomizeFactions() {
int slPlaceFactorX = random.randRange(0, 1);
int slPlaceFactorY = random.randRange(0, 1) * 2;