void PngFile::Save(const Image2D &image, const std::string &filename, const ColorMap &colorMap, long double normalizeFactor, long double zeroLevel) throw(IOException) { PngFile pngFile(filename, image.Width(), image.Height()); pngFile.BeginWrite(); pngFile.SetFromImage(image, colorMap, normalizeFactor, zeroLevel); pngFile.Close(); }
void POVRayExtension::render() { // Render the scene using POV-Ray QString fileName = m_POVRayDialog->fileName().mid(0, m_POVRayDialog->fileName().lastIndexOf(".")); // Check a filename was supplied if (fileName.isEmpty()) { QMessageBox::warning(m_POVRayDialog, tr("No filename supplied."), tr("No valid filename was supplied.")); return; } if (!m_POVRayDialog->renderDirect() && !m_POVRayDialog->keepSource()) { QMessageBox::warning(m_POVRayDialog, tr("Does not compute."), tr("You requested no direct rendering using POV-Ray and not to keep the POV-Ray file. This will result in no output being saved. Are you sure that is what you want?")); return; } // Check that the povray executable exists - FIXME implement path search... /* QFileInfo info(m_POVRayDialog->command()); if (!info.exists()) { QMessageBox::warning(m_POVRayDialog, "POV-Ray executable not found.", "The POV-Ray executable, normally named 'povray', cannot be found."); return; } */ // Check whether the .pov file can be written QFile povFile(fileName + ".pov"); if (povFile.open(QIODevice::WriteOnly | QIODevice::Text)) { double aspectRatio = static_cast<double>(m_POVRayDialog->imageWidth()) / m_POVRayDialog->imageHeight(); qDebug() << "Aspect ratio:" << aspectRatio; povFile.close(); POVPainterDevice pd(fileName + ".pov", aspectRatio, m_glwidget); } else { QMessageBox::warning(m_POVRayDialog, tr("Cannot Write to File."), tr("Cannot write to file %1. Do you have permissions to write to that location?").arg(fileName+".pov")); return; } if (m_POVRayDialog->renderDirect()) { m_process = new QProcess(this); QFile pngFile(fileName + ".png"); if (pngFile.open(QIODevice::WriteOnly | QIODevice::Text)) { pngFile.close(); QFileInfo info(fileName + ".png"); m_process->setWorkingDirectory(info.absolutePath()); m_process->start(m_POVRayDialog->command(), m_POVRayDialog->commandLine()); qDebug() << "Command:" << m_POVRayDialog->command() + ' ' + m_POVRayDialog->commandLine().join(" "); qDebug() << "Rendering started..."; if (!m_process->waitForStarted()) { QMessageBox::warning(m_POVRayDialog, tr("POV-Ray failed to start."), tr("POV-Ray failed to start. May be the path to the executable is not set correctly.")); } connect(m_process, SIGNAL(finished(int)), this, SLOT(finished(int))); }
void BitmapLoader::initialize() { wxString zipname; wxFileName fn; zipname = "codelite-icons.zip"; if(EditorConfigST::Get()->GetOptions()->GetOptions() & OptionsConfig::Opt_IconSet_FreshFarm) { zipname = wxT("codelite-icons-fresh-farm.zip"); } else if(EditorConfigST::Get()->GetOptions()->GetOptions() & OptionsConfig::Opt_IconSet_Classic_Dark) { zipname = wxT("codelite-icons-dark.zip"); } else if(EditorConfigST::Get()->GetOptions()->GetOptions() & OptionsConfig::Opt_IconSet_Classic) { zipname = wxT("codelite-icons.zip"); } // Under linux, take into account the --prefix #ifdef __WXGTK__ wxString bitmapPath = wxString(INSTALL_DIR, wxConvUTF8); fn = wxFileName(bitmapPath, zipname); #else #ifdef USE_POSIX_LAYOUT wxString bitmapPath(clStandardPaths::Get().GetDataDir() + wxT(INSTALL_DIR)); fn = wxFileName(bitmapPath, zipname); #else fn = wxFileName(clStandardPaths::Get().GetDataDir(), zipname); #endif #endif if(m_manifest.empty() || m_toolbarsBitmaps.empty()) { m_zipPath = fn; if(m_zipPath.FileExists()) { doLoadManifest(); doLoadBitmaps(); } } wxFileName fnNewZip(clStandardPaths::Get().GetDataDir(), "codelite-bitmaps.zip"); if(fnNewZip.FileExists()) { clZipReader zip(fnNewZip); wxFileName tmpFolder(clStandardPaths::Get().GetTempDir(), ""); // Make sure we append the user name to the temporary user folder // this way, multiple CodeLite instances from different users can extract the // bitmaps to /tmp wxString bitmapFolder = "codelite-bitmaps"; bitmapFolder << "." << clGetUserName(); tmpFolder.AppendDir(bitmapFolder); if(tmpFolder.DirExists()) { tmpFolder.Rmdir(wxPATH_RMDIR_RECURSIVE); } tmpFolder.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL); // Extract all images into this folder zip.Extract("*", tmpFolder.GetPath()); // Load all the files into wxBitmap wxArrayString files; wxDir::GetAllFiles(tmpFolder.GetPath(), &files, "*.png"); for(size_t i = 0; i < files.size(); ++i) { wxFileName pngFile(files.Item(i)); if(pngFile.GetFullName().Contains("@2x")) { // No need to load the hi-res images manually, // this is done by wxWidgets continue; } clBitmap bmp; if(bmp.LoadFile(pngFile.GetFullPath(), wxBITMAP_TYPE_PNG)) { CL_DEBUG("Adding new image: %s", pngFile.GetName()); m_toolbarsBitmaps.insert(std::make_pair(pngFile.GetName(), bmp)); } } } }
/*! \brief Reads a PNG file and places the pixel data in the * passed array. * * \param filename The name of the file to read. * \param image The array of \ref Pixel data to read the image into. * \param width Variable used to return the width of the image. * \param height Variable used to return the height of the image. */ inline void readPNGFile(const std::string& filename, std::vector<uint8_t>& image, size_t& width, size_t& height, size_t& components) { std::ifstream pngFile(filename.c_str(), std::fstream::binary); if(!pngFile.is_open()) { std::stringstream strm; strm << "failed to open file '" << filename << "'"; throw std::runtime_error(strm.str().c_str()); } pngFile.exceptions(std::fstream::badbit | std::fstream::failbit); const size_t pngHeaderSize = 8; png_byte pngHeader[pngHeaderSize]; pngFile.read(reinterpret_cast<char*>(pngHeader), pngHeaderSize); if(png_sig_cmp(pngHeader, 0, pngHeaderSize)) { std::stringstream strm; strm << "failed to read '" << filename << "': not a png file"; throw std::runtime_error(strm.str().c_str()); } png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if(!png) throw std::runtime_error("failed to allocate png_struct"); png_infop pngInfo = png_create_info_struct(png); if(!pngInfo) { png_destroy_read_struct(&png, NULL, NULL); throw std::runtime_error("failed to allocate png_info_struct"); } png_infop pngEndInfo = png_create_info_struct(png); if(!pngEndInfo) { png_destroy_read_struct(&png, &pngInfo, NULL); throw std::runtime_error("failed to allocate png_info_struct"); } // set up error handling the hard way if(setjmp(png_jmpbuf(png))) { png_destroy_read_struct(&png, &pngInfo, &pngEndInfo); throw std::runtime_error("libpng: failed to set up io"); } png_set_read_fn(png, static_cast<void*>(&pngFile), detail::read); png_set_sig_bytes(png, pngHeaderSize); png_read_info(png, pngInfo); if(png_get_color_type(png, pngInfo) != PNG_COLOR_TYPE_RGBA && png_get_color_type(png, pngInfo) != PNG_COLOR_TYPE_RGB) { png_destroy_read_struct(&png, &pngInfo, &pngEndInfo); std::stringstream strm; strm << "unsupported color type in '" << filename << "'"; throw std::runtime_error(strm.str().c_str()); } width = png_get_image_width(png, pngInfo); height = png_get_image_height(png, pngInfo); components = png_get_channels(png, pngInfo); if ((components != 3) && (components != 4)) throw std::runtime_error("Unsupported number of components"); size_t bitDepth = png_get_bit_depth(png, pngInfo); if(bitDepth != 8) { png_destroy_read_struct(&png, &pngInfo, &pngEndInfo); std::stringstream strm; strm << "failed to read '" << filename << "': invalid bit " << "depth: " << bitDepth; throw std::runtime_error(strm.str().c_str()); } size_t bytesPerRow = png_get_rowbytes(png, pngInfo); png_bytep* pngRows = new png_bytep[height]; png_bytep pngData = 0; image.resize(height * width * components); size_t offset = 0; png_bytep basePointer = reinterpret_cast<png_bytep>(&image[0]); for(size_t row = 0; row < height; ++row) { pngRows[row] = basePointer + offset; offset += bytesPerRow; } // set up error handling the hard way if(setjmp(png_jmpbuf(png))) { png_destroy_read_struct(&png, &pngInfo, &pngEndInfo); delete[] pngData; delete[] pngRows; throw std::runtime_error("libpng: failed to read image"); } png_read_image(png, pngRows); png_read_end(png, pngEndInfo); // all went well, we can clean up now png_destroy_read_struct(&png, &pngInfo, &pngEndInfo); delete[] pngData; delete[] pngRows; }
/*! \brief Writes a PNG file using the pixel data in the passed * array. * * \param filename The name of the file to create/overwrite. * \param image The array of \ref Pixel data to write out. * \param width The width of the image. * \param height The height of the image. * \param compressionLevel The level of compression requested from the PNG library. * \param disableFiltering Prevent the PNG library from filtering the output. * \param flip Flips the vertical ordering of the image (to allow easy saving of OpenGL renderings). */ inline void writePNGFile(const std::string& filename, std::vector<uint8_t >& image, size_t width, size_t height, size_t components, int compressionLevel = PNG_COMPRESSION_TYPE_DEFAULT, bool disableFiltering = false, bool flip = false) { if(image.size() != width * height * components) { std::stringstream strm; strm << "invalid input to writePNGFile(): " << "size mismatch of input vector (is " << image.size() << ", should be " << width << "x" << height << " = " << width * height; throw std::runtime_error(strm.str().c_str()); } if(compressionLevel < 0 || compressionLevel > 9) { std::stringstream strm; strm << "invalid input to writePNGFile(): " << "valid compression levels range from 0 to 9 (default: " << PNG_COMPRESSION_TYPE_DEFAULT << ")"; throw std::runtime_error(strm.str().c_str()); } std::ofstream pngFile(filename.c_str(), std::fstream::binary); if(!pngFile.is_open()) { std::stringstream strm; strm << "failed to open file '" << filename << "'"; throw std::runtime_error(strm.str().c_str()); } pngFile.exceptions(std::fstream::badbit | std::fstream::failbit); png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if(!png) throw std::runtime_error("failed to allocate png_struct"); png_infop pngInfo = png_create_info_struct(png); if(!pngInfo) { png_destroy_write_struct(&png, NULL); throw std::runtime_error("failed to allocate png_info_struct"); } // set up error handling the hard way if(setjmp(png_jmpbuf(png))) { png_destroy_write_struct(&png, &pngInfo); throw std::runtime_error("libpng: failed to set up io"); } png_set_write_fn(png, static_cast<void*>(&pngFile), detail::write, detail::flush); switch (components) { case 3: png_set_IHDR(png, pngInfo, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_BASE); break; case 4: png_set_IHDR(png, pngInfo, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_BASE); break; default: throw std::runtime_error("Unsupported number of components"); } if(disableFiltering) png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE); if(compressionLevel != PNG_COMPRESSION_TYPE_DEFAULT) png_set_compression_level(png, compressionLevel); png_write_info(png, pngInfo); size_t bytesPerRow = png_get_rowbytes(png, pngInfo); png_bytep* pngRows = new png_bytep[height]; if(image.size() != (height * bytesPerRow)) { std::stringstream strm; strm << "writePNGFile(): invalid size of input data"; throw std::runtime_error(strm.str().c_str()); } size_t offset = 0; png_bytep basePointer = reinterpret_cast<png_bytep>(&image[0]); if (flip) for(int row = height - 1; row >= 0; --row) { pngRows[row] = basePointer + offset; offset += bytesPerRow; } else for(size_t row = 0; row < height; ++row) { pngRows[row] = basePointer + offset; offset += bytesPerRow; } // set up error handling the hard way if(setjmp(png_jmpbuf(png))) { png_destroy_write_struct(&png, &pngInfo); delete[] pngRows; throw std::runtime_error("libpng: failed to write image"); } png_write_image(png, pngRows); png_write_end(png, NULL); // all went well, we can clean up now png_destroy_write_struct(&png, &pngInfo); delete[] pngRows; }