#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION #include #include #include #include #include #include #include "stb_image.h" #include "stb_image_write.h" //void flip_image_horizontally(unsigned char* data, int width, int height, int channels); void flip_image_vertically(unsigned char* data, int width, int height, int channels); bool STOP = false; void sigend( int ) { std::cout << std::endl << "close file monitoring" << std::endl; STOP = true; } /// Processes a file action class UpdateListener : public efsw::FileWatchListener { public: UpdateListener() {} std::string getActionName( efsw::Action action ) { switch ( action ) { case efsw::Actions::Add: return "Add"; case efsw::Actions::Modified: return "Modified"; case efsw::Actions::Delete: return "Delete"; case efsw::Actions::Moved: return "Moved"; default: return "Bad Action"; } } void handleFileAction( efsw::WatchID watchid, const std::string& dir, const std::string& filename, efsw::Action action, std::string oldFilename = "" ) override { std::cout << "Watch ID " << watchid << " DIR (" << dir + ") FILE (" + ( oldFilename.empty() ? "" : "from file " + oldFilename + " to " ) + filename + ") has event " << getActionName( action ) << std::endl; if (action == efsw::Actions::Add || action == efsw::Actions::Modified) { // 1. Ignore the event if it's a file WE created if (filename.find("flipped_") == 0) { return; } std::string fullPath = dir + filename; int width, height, channels; // Try to load unsigned char* image_data = stbi_load(fullPath.c_str(), &width, &height, &channels, 0); // If it fails because the file is still being written, wait and try one more time if (!image_data) { efsw::System::sleep(300); image_data = stbi_load(fullPath.c_str(), &width, &height, &channels, 0); } if (image_data) { std::cout << "Successfully loaded " << filename << " (" << width << "x" << height << ")" << std::endl; flip_image_vertically(image_data, width, height, channels); // Save flipped image std::string flipped_filename = dir + "flipped_" + filename.substr( 0, filename.length() - 3 ) + "png"; stbi_write_png( flipped_filename.c_str(), width, height, channels, image_data, width * channels ); //stbi_image_free(image_data); STOP = true; } } } }; efsw::WatchID handleWatchID( efsw::WatchID watchid ) { switch ( watchid ) { case efsw::Errors::FileNotFound: case efsw::Errors::FileRepeated: case efsw::Errors::FileOutOfScope: case efsw::Errors::FileRemote: case efsw::Errors::WatcherFailed: case efsw::Errors::Unspecified: { std::cout << efsw::Errors::Log::getLastErrorLog().c_str() << std::endl; break; } default: { std::cout << "Added WatchID: " << watchid << std::endl; } } return watchid; } void flip_image_vertically(unsigned char* data, int width, int height, int channels) { // ... (your vertical flip implementation from before) int row_stride = width * channels; unsigned char* temp_row = new unsigned char [row_stride]; for (int y = 0; y < height / 2; ++y) { unsigned char* top_row = data + y * row_stride; unsigned char* bottom_row = data + (height - 1 - y) * row_stride; std::memcpy(temp_row, top_row, row_stride); std::memcpy(top_row, bottom_row, row_stride); std::memcpy(bottom_row, temp_row, row_stride); } delete[] temp_row; } int main( int argc, char** argv ) { signal( SIGABRT, sigend ); signal( SIGINT, sigend ); signal( SIGTERM, sigend ); std::cout << "Press ^C to exit demo" << std::endl; bool commonTest = true; bool useGeneric = false; std::string path; if ( argc >= 2 ) { path = std::string( argv[1] ); if ( efsw::FileSystem::isDirectory( path ) ) { commonTest = false; } if ( argc >= 3 ) { if ( std::string( argv[2] ) == "true" ) { useGeneric = true; } } } UpdateListener* ul = new UpdateListener(); /// create the file watcher object efsw::FileWatcher fileWatcher( useGeneric ); fileWatcher.followSymlinks( false ); fileWatcher.allowOutOfScopeLinks( false ); if ( commonTest ) { std::string CurPath( efsw::System::getProcessPath() ); std::cout << "CurPath: " << CurPath.c_str() << std::endl; /// starts watching fileWatcher.watch(); /// add a watch to the system handleWatchID( fileWatcher.addWatch( CurPath + "test", ul, true ) ); } else { if ( fileWatcher.addWatch( path, ul, true ) > 0 ) { fileWatcher.watch(); std::cout << "Watching directory: " << path.c_str() << std::endl; if ( useGeneric ) { std::cout << "Using generic backend watcher" << std::endl; } } else { std::cout << "Error trying to watch directory: " << path.c_str() << std::endl; std::cout << efsw::Errors::Log::getLastErrorLog().c_str() << std::endl; } } while ( !STOP ) { efsw::System::sleep( 100 ); } return 0; }