void MainWindow::writeGraphviz() { QFileDialog dialog(this); dialog.setFileMode(QFileDialog::AnyFile); dialog.setNameFilter(tr("Graphviz file (*.gv)")); if(dialog.exec()) { QFile f(dialog.selectedFiles()[0]); f.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream out(&f); GameBoard b(*gbw->gameBoard()); QString fdir = QFileInfo(dialog.selectedFiles()[0]).dir().path(); GraphGen::setPrepareImageCallback([&](const GameBoard& b, const std::string& fname) { QFile imfile(fdir + "/" + QString::fromStdString(fname) + ".png"); if(!imfile.exists()) { QImage image(100, 100, QImage::Format_RGB888); QPainter imp(&image); GameBoardWidget::paintBoard(&b, imp); image.save(imfile.fileName()); std::cout << "Wrote image " << imfile.fileName().toStdString() << std::endl; } }); out << QString::fromStdString(GraphGen::graph(b, 3*2, 4)); } }
void ImLoad::saveImage(const char *fn, Image *im) { ofstream imfile(fn, ios::binary); if (!imfile.is_open()) { cerr << "Sorry, could not open: " << fn << endl; exit(0); } imfile << "P5" << endl; imfile << im->getWidth() << " " << im->getHeight() << " 255" << endl; for (int y = 0; y < im->getHeight(); y++) for (int x = 0; x < im->getWidth(); x++) imfile.put((unsigned char)(im->getPix(x, y) * 255)); }
Image *ImLoad::readImage(const char *fn){ ifstream imfile(fn, ios::binary); if (!imfile.is_open()) { cerr << "Sorry, could not open: " << fn << endl; exit(0); } // Reading file header char P; char num; imfile >> P >> num; ignoreComments(imfile); // Read image dimensions and extremum value int width, height, extr; imfile >> width; ignoreComments(imfile); imfile >> height; ignoreComments(imfile); imfile >> extr; // Check whether the file is OK if (P != 'P' || num != '5' || width <= 0 || height <= 0 || extr > 255) { cerr << "Input image has to be PGM format" << endl; exit(0); } // Get the image intensities and normalise to 0 - 1. imfile.get(); Image *im = new Image(width, height); for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) im->setPix(x, y, ((double) imfile.get()) / extr); return im; }