From f043d1fa326f052e3ae62abd8e79885ff32e4ad9 Mon Sep 17 00:00:00 2001 From: David Mayerich Date: Fri, 17 Aug 2018 19:57:30 -0500 Subject: [PATCH] first commit --- CMakeLists.txt | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ FindOpenSlide.cmake | 25 +++++++++++++++++++++++++ FindSTIM.cmake | 27 +++++++++++++++++++++++++++ main.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 220 insertions(+), 0 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 FindOpenSlide.cmake create mode 100644 FindSTIM.cmake create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e77e9a6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,62 @@ +#Specify the version being used aswell as the language +cmake_minimum_required(VERSION 3.12) +cmake_policy(SET CMP0074 NEW) + +#Name your project here +project(slideconvert) + +#set the module directory +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}") + +#default to release mode +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif(NOT CMAKE_BUILD_TYPE) + +#build the executable in the binary directory on MS Visual Studio +if ( MSVC ) + SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}") + SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}") + SET( LIBRARY_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}") + SET( LIBRARY_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}") + add_definitions(-D_CRT_SECURE_NO_WARNINGS) + add_definitions(-D_SCL_SECURE_NO_WARNINGS) +endif ( MSVC ) +#MAYBE REMOVE----------------- +#set C++11 flags if using GCC +if( CMAKE_COMPILER_IS_GNUCC ) + SET( CMAKE_CXX_FLAGS "-std=c++11") + SET( CUDA_NVCC_FLAGS "-std=c++11") +endif( CMAKE_COMPILER_IS_GNUCC ) +#----------------------------- + +#find packages----------------------------------- + +find_package(OpenCV REQUIRED) +add_definitions(-DUSING_OPENCV) + +#find the pthreads package +find_package(Threads) + +#find the X11 package +find_package(X11) + +#find the STIM library +find_package(STIM REQUIRED) + +find_package(OpenSlide REQUIRED) + +add_executable(slideconvert + main.cpp +) + +#include include directories +include_directories(${OpenSlide_INCLUDE_DIRS} + ${OpenCV_INCLUDE_DIRS} + ${STIM_INCLUDE_DIRS} +) + +target_link_libraries(slideconvert + ${OpenSlide_LIBRARIES} + ${OpenCV_LIBS} +) \ No newline at end of file diff --git a/FindOpenSlide.cmake b/FindOpenSlide.cmake new file mode 100644 index 0000000..73cdddf --- /dev/null +++ b/FindOpenSlide.cmake @@ -0,0 +1,25 @@ +# A CMake find module for the OpenSlide microscopy file reader library. +# +# http://openslide.org +# +# Once done, this module will define +# OpenSlide_FOUND - system has OpenSlide +# OpenSlide_INCLUDE_DIRS - the OpenSlide include directory +# OpenSlide_LIBRARIES - link to these to use OpenSlide + +set(OpenSlide_ROOT $ENV{OpenSlide_ROOT}) +IF(NOT OpenSlide_ROOT) + MESSAGE("ERROR: OpenSlide_ROOT must be set!") +ENDIF(NOT OpenSlide_ROOT) + +FIND_PATH(OpenSlide_INCLUDE_DIRS DOC "Path to OpenSlide include directory." + NAMES openslide/openslide.h + PATHS ${OpenSlide_ROOT}/include) + +FIND_LIBRARY(OpenSlide_LIBRARIES DOC "Absolute path to OpenSlide library." + NAMES libopenslide.lib + PATHS ${OpenSlide_ROOT}/lib) +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(OpenSlide DEFAULT_MSG OpenSlide_LIBRARIES OpenSlide_INCLUDE_DIRS) + +mark_as_advanced(OpenSlide_INCLUDE_DIRS OpenSlide_LIBRARIES) \ No newline at end of file diff --git a/FindSTIM.cmake b/FindSTIM.cmake new file mode 100644 index 0000000..c05f9f6 --- /dev/null +++ b/FindSTIM.cmake @@ -0,0 +1,27 @@ +# finds the STIM library (downloads it if it isn't present) +# set STIMLIB_PATH to the directory containing the stim subdirectory (the stim repository) + +include(FindPackageHandleStandardArgs) + +set(STIM_ROOT $ENV{STIM_ROOT}) + +IF(NOT UNIX) + IF(NOT STIM_ROOT) + MESSAGE("ERROR: STIM_ROOT environment variable must be set!") + ENDIF(NOT STIM_ROOT) + + FIND_PATH(STIM_INCLUDE_DIRS DOC "Path to GLFW include directory." + NAMES stim/image/image.h + PATHS ${STIM_ROOT}) +ENDIF(NOT UNIX) + +find_package_handle_standard_args(STIM DEFAULT_MSG STIM_INCLUDE_DIRS) + +if(STIM_FOUND) + set(STIM_INCLUDE_DIRS ${STIM_INCLUDE_DIRS}) +elseif(STIM_FOUND) + message("STIM library not found. Set the STIM_ROOT environment variable to the STIM location.") + message("STIMLIB can be found here: https://git.stim.ee.uh.edu/codebase/stimlib") +endif(STIM_FOUND) + +find_package_handle_standard_args(STIM DEFAULT_MSG STIM_INCLUDE_DIRS) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..3f04598 --- /dev/null +++ b/main.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + +stim::arglist args; +int32_t level = 0; +int64_t x = 0; +int64_t y = 0; +int64_t sx, sy; +openslide_t* slide = NULL; + +std::string infile; +std::string outfile; + +void openSlide() { + slide = openslide_open(infile.c_str()); //open the slide file + if (slide == NULL) { //if the slide wasn't opened + throw std::runtime_error("ERROR: unable to open file"); + } +} +void closeSlide() { + openslide_close(slide); //close the slide file +} +//this function displays details about the specified image +void dispDetails() { + std::cout << "OpenSlide version: " << openslide_get_version() << std::endl; + openSlide(); + std::cout << "Details for slide file: " << infile << std::endl; + int32_t levels = openslide_get_level_count(slide); + int64_t w, h; + std::cout << "level sizes (in pixels):" << std::endl; + std::cout << "-------------------------------------" << std::endl; + for (int32_t l = 0; l < levels; l++) { + std::cout << "level " << l; + openslide_get_level_dimensions(slide, l, &w, &h); + std::cout << " [" << w << " x " << h << "]" << std::endl; + } + closeSlide(); +} + +void addArguments() { + args.add("help", "prints this help"); + args.add("level", "specify the level to output", "", "integer value between 0 and max"); + args.add("region", "region to save", "", "x-pos y-pos x-size y-size"); +} + +void readArguments() { + + if (args.nargs() > 0) infile = args.arg(0); //store the file name + + if (args.nargs() > 1) outfile = args.arg(1); //store the output file name + + if (args["level"]) { + level = args["level"].as_int(); //store the desired level to extract + } + else { //if the desired level isn't set, default to the smallest resolution image + openSlide(); + int32_t levels = openslide_get_level_count(slide); + level = levels - 1; + closeSlide(); + } + if (args["region"]) { + if (args["region"].nargs() == 4) { + x = args["region"].as_int(0); + y = args["region"].as_int(1); + sx = args["region"].as_int(2); + sy = args["region"].as_int(3); + } + else { + throw std::runtime_error("ERROR: --region requires 4 parameters"); + } + } + else { + openSlide(); + openslide_get_level_dimensions(slide, level, &sx, &sy); + closeSlide(); + } +} + +int main(int argc, char** argv) { + addArguments(); + args.parse(argc, argv); + readArguments(); + if (args.nargs() == 0 || args["help"]) { //if the user requests help or doesn't do anything, show usage + std::cout << "OpenSlide version: " << openslide_get_version() << std::endl; + std::cout << "Usage----------------" << std::endl; + std::cout << "save the full image at the lowest resolution: slideconvert slidefile.ndpi output.bmp" << std::endl; + std::cout << "save the level-3 (3x downsampling) image: slideconvert slidefile.ndpi output.bmp --level 3" << std::endl; + std::cout << "save a 1024x1024 region at full-resolution: slideconvert slidefile.ndpi output.bmp --level 0 --region 10000 15000 1024 1024" << std::endl; + std::cout << args.str(); //if no arguments are provided, show usage + return 0; + } + else if (args.nargs() == 1) { //if one argument is provided, display file information + dispDetails(); + return 0; + } + else if (args.nargs() == 2) { //if two arguments are provided, save the specified region + openSlide(); + cv::Mat I(sy, sx, CV_8UC4); //allocate an OpenCV image array + openslide_read_region(slide, (uint32_t*)I.data, x, y, level, sx, sy); + cv::imwrite(outfile, I); + closeSlide(); + } +} \ No newline at end of file -- libgit2 0.21.4