├── .gitattributes ├── LICENSE.txt ├── README.md ├── libmin ├── CMakeLists.txt ├── cmake │ ├── CudaAutodetectCompute.cmake │ ├── CudaComputeTargetFlags.cmake │ ├── DeclareDPIAware.manifest │ ├── FindLibmin.cmake │ └── HelpersBootstrap.cmake ├── include │ ├── GL │ │ ├── eglew.h │ │ ├── glew.h │ │ ├── glxew.h │ │ └── wglew.h │ ├── camera3d.h │ ├── common_cuda.h │ ├── common_defs.h │ ├── cutil_math.h │ ├── dataptr.h │ ├── datax.h │ ├── directory.h │ ├── event.h │ ├── event_system.h │ ├── file_png.h │ ├── file_tga.h │ ├── httplib.h │ ├── image.h │ ├── image_info.h │ ├── imageformat.h │ ├── imageformat_bmp.h │ ├── imageformat_generic.h │ ├── imageformat_jpg.h │ ├── imageformat_png.h │ ├── imageformat_tga.h │ ├── imageformat_tiff.h │ ├── main_includes.h │ ├── nvToolsExt.h │ ├── nv_gui.h │ ├── quaternion.h │ ├── string_helper.h │ ├── timex.h │ ├── vec.h │ └── widget.h ├── mains │ ├── main.h │ ├── main_android.cpp │ ├── main_win.cpp │ └── main_x11.cpp └── src │ ├── camera3d.cpp │ ├── common_cuda.cpp │ ├── common_defs.cpp │ ├── dataptr.cpp │ ├── datax.cpp │ ├── directory.cpp │ ├── event.cpp │ ├── event_system.cpp │ ├── file_png.cpp │ ├── file_tga.cpp │ ├── glew.c │ ├── glewinfo.c │ ├── image.cpp │ ├── image_bgr24.cpp │ ├── image_bw16.cpp │ ├── image_bw32.cpp │ ├── image_bw8.cpp │ ├── image_f8.cpp │ ├── image_info.cpp │ ├── image_rgb24.cpp │ ├── image_rgba32.cpp │ ├── imageformat.cpp │ ├── imageformat_bmp.cpp │ ├── imageformat_generic.cpp │ ├── imageformat_jpg.cpp │ ├── imageformat_png.cpp │ ├── imageformat_tga.cpp │ ├── imageformat_tiff.cpp │ ├── nv_gui.cpp │ ├── quaternion.cpp │ ├── string_helper.cpp │ ├── timex.cpp │ ├── vec.cpp │ └── widget.cpp ├── math_voxelizer ├── CMakeLists.txt ├── assets │ ├── arial_256.bin │ └── arial_256.tga └── main_voxelizer.cpp └── voxelizer.jpg /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Voxelizer 3 | 4 | Copyright Rama Hoetzlein (c) 2019 5 | This work is licensed as CC0, public domain with no restrictions 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![image](https://github.com/ramakarl/voxelizer/blob/master/voxelizer.jpg) 2 | 3 | ## Voxelizer 4 | ------------------------------------------------ 5 | by Rama Karl Hoetzlein, http://ramakarl.com
6 | This code is licensed CC-BY (c) 2021 7 |

8 | The interface lets you move the vertices of the triangle.
9 | Use 1,2,3,4 keys to change the Algorithm. 10 |

11 | Code gives the basic math for the 3D voxelization of a triangle. 12 |

13 | Four techniques are provided. The first three are triangle-box intersection tests, and test the triangle against every voxel in the domain O(V^3). The last is a direct DDA Rasterizer, which only visits the voxels touched by the triangle O(Vt). 14 |

15 | 1- **Explicit edge tests**: Use edge fuctions to explicitly test each edge of triangle. 27 tests/voxel.
16 | 2- **Schwarz-Seidel tests**: Recent paper that uses a reduced, efficient set of edge tests. 9 tests/voxel.
17 | 3- **Akenine-Moller tests**: The first paper to provide efficient, exact voxelization. 18 tests/voxel.
18 | 4- **DDA Rasterizer**: Starts at one corner and scans along the edges, performing a DDA (2D differential analyzer) to fill the interior of the triangle in 3D. This technique is much faster as it only visits voxels on the triangle.
19 | 20 | Sources:
21 | Schwarz & Seidel, 2010, Fast Parallel Surface and Solid Voxelization on GPUs, http://research.michael-schwarz.com/publ/files/vox-siga10.pdf
22 | Akenine-Moller, 2001, Fast 3D Triangle-Box Overlap Testing, https://fileadmin.cs.lth.se/cs/Personal/Tomas_Akenine-Moller/pubs/tribox.pdf
23 | Pineda, 1998, A Parallel Algorithm for Polygon Rasterization (Edge Functions), https://www.cs.drexel.edu/~david/Classes/Papers/comp175-06-pineda.pdf
24 | 25 | Citation of this code when used in papers or projects is appreciated:
26 | 2019. Hoetzlein, Rama. *Voxelization of a triangle in 3D*. https://github.com/ramakarl/voxelizer 27 | 28 | ### How to Build 29 | 30 | 1. Run cmake in \libmin 31 | 2. Build the solution for libmin 32 | 3. Run cmake in \math_voxelizer 33 | 4. During cmake it should find libmin. If not, set the LIBMIN_ROOT_DIR 34 | 4. Build the solution. Run it. 35 | 36 |

37 | Rama Hoetzlein (c) 2019 38 | 39 | -------------------------------------------------------------------------------- /libmin/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | set(PROJNAME libmin) 4 | Project(${PROJNAME}) 5 | 6 | Message("-------------------------------") 7 | Message("PROJECT NAME: ${PROJNAME}") 8 | 9 | ##################################################################################### 10 | # Bootstrap 11 | # 12 | set( BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) 13 | find_path ( CMAKE_HELPERS_PATH "HelpersBootstrap.cmake" HINTS ${BASE_DIRECTORY}/cmake/ ) 14 | if ( ${CMAKE_HELPERS_PATH} STREQUAL "HELPERS-NOTFOUND" ) 15 | message ( FATAL_ERROR "\n Please set the CMAKE_HELPERS_PATH to location of HelpersBootstrap.cmake" ) 16 | endif() 17 | include( ${CMAKE_HELPERS_PATH}/HelpersBootstrap.cmake ) # Cross-Platform functions 18 | include_directories ("${CMAKE_CURRENT_SOURCE_DIR}/src") 19 | include_directories ("${CMAKE_CURRENT_SOURCE_DIR}/include") 20 | 21 | ##################################################################################### 22 | # 23 | # LIBMIN - Options 24 | # 25 | add_definitions ( -DLIBHELP_EXPORTS) 26 | 27 | add_definitions ( -DGLEW_STATIC) 28 | 29 | OPTION (BUILD_OPENGL "Build with OpenGL" ON) 30 | if (BUILD_OPENGL) 31 | add_definitions(-DUSE_OPENGL) # Use OpenGL 32 | 33 | IF (WIN32) 34 | LIST(APPEND LIBRARIES_OPTIMIZED "opengl32.lib" ) 35 | LIST(APPEND LIBRARIES_DEBUG "opengl32.lib" ) 36 | ENDIF() 37 | 38 | endif() 39 | 40 | ##################################################################################### 41 | # Source files for this project 42 | # 43 | file(GLOB SOURCE_FILES src/*.cpp src/*.c) 44 | file(GLOB INCLUDE_FILES include/*.hpp include/*.h) 45 | 46 | ##################################################################################### 47 | # Library paths 48 | # 49 | # CMAKE_INSTALL_PREFIX -- path where library will be installed to 50 | 51 | if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) 52 | if ( WIN32 ) 53 | get_filename_component ( _instpath "${CMAKE_CURRENT_BINARY_DIR}" REALPATH ) 54 | else() 55 | get_filename_component ( _instpath "/usr/local/libhelp" REALPATH ) 56 | endif() 57 | set ( CMAKE_INSTALL_PREFIX ${_instpath} CACHE PATH "default install path" FORCE) 58 | endif() 59 | 60 | get_filename_component( LIB_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} REALPATH) 61 | get_filename_component( INSTALL_BIN_PATH ${CMAKE_INSTALL_PREFIX}/bin REALPATH) 62 | get_filename_component( INSTALL_INC_PATH ${CMAKE_INSTALL_PREFIX}/include REALPATH) 63 | get_filename_component( SHARE_PATH ${CMAKE_INSTALL_PREFIX}/lib REALPATH) 64 | 65 | set ( EXECUTABLE_OUTPUT_PATH ${INSTALL_BIN_PATH} CACHE PATH "" FORCE ) 66 | 67 | 68 | ##################################################################################### 69 | # Library output 70 | # 71 | unset ( ALL_SOURCE_FILES ) 72 | list( APPEND ALL_SOURCE_FILES ${SOURCE_FILES} ) 73 | list( APPEND ALL_SOURCE_FILES ${INCLUDE_FILES} ) 74 | list( APPEND ALL_SOURCE_FILES ${PACKAGE_SOURCE_FILES} ) 75 | 76 | # Definitions 77 | add_definitions(-DLIBHELP_EXPORTS) # Export dll symbols 78 | 79 | # Set the library type 80 | OPTION (BUILD_SHARED_LIBS "Build Shared Libraries" ON) 81 | set (LIB_TYPE STATIC) 82 | if (BUILD_SHARED_LIBS) 83 | set (LIB_TYPE SHARED) 84 | endif() 85 | 86 | if( WIN32 AND NOT GLUT_FOUND) 87 | add_definitions(/wd4267) #remove size_t to int warning 88 | add_definitions(/wd4996) #remove printf warning 89 | add_definitions(/wd4244) #remove double to float conversion warning 90 | add_definitions(/wd4305) #remove double to float truncation warning 91 | add_library (${PROJNAME} ${LIB_TYPE} ${ALL_SOURCE_FILES} ${CUDA_FILES} ) 92 | else() 93 | add_library (${PROJNAME} ${LIB_TYPE} ${ALL_SOURCE_FILES} ${CUDA_FILES} ${PTX_FILES} ) 94 | endif() 95 | 96 | # debug and relase libs 97 | set ( CMAKE_DEBUG_POSTFIX "d" CACHE STRING "" ) 98 | set_target_properties( ${PROJNAME} PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}) 99 | 100 | set_target_properties( ${PROJNAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${LIB_OUTPUT_PATH} ) 101 | set_target_properties( ${PROJNAME} PROPERTIES VS_INTERMEDIATE_DIRECTORY_DEBUG ${LIB_OUTPUT_PATH}/Debug ) 102 | set_target_properties( ${PROJNAME} PROPERTIES VS_INTERMEDIATE_DIRECTORY_RELEASE ${LIB_OUTPUT_PATH}/Release ) 103 | set_target_properties( ${PROJNAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${LIB_OUTPUT_PATH} ) 104 | set_target_properties( ${PROJNAME} PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE) 105 | 106 | message ( STATUS "Output: ${LIB_OUTPUT_PATH}" ) 107 | message ( STATUS "Installed Bin: ${INSTALL_BIN_PATH}" ) 108 | message ( STATUS "Installed Inc: ${INSTALL_INC_PATH}" ) 109 | 110 | ##################################################################################### 111 | # Linkage 112 | # 113 | _LINK ( PROJECT ${PROJNAME} OPT ${LIBRARIES_OPTIMIZED} DEBUG ${LIBRARIES_DEBUG} PLATFORM ${PLATFORM_LIBRARIES} ) 114 | 115 | ################################################################ 116 | # Windows specific 117 | # 118 | if ( WIN32 ) 119 | # instruct CMake to automatically build INSTALL project in Visual Studio 120 | set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1) 121 | # visual studio source groups 122 | source_group(Include FILES 123 | ${INCLUDE_FILES} 124 | ) 125 | source_group(Source FILES 126 | ${SOURCE_FILES} 127 | ) 128 | endif() 129 | 130 | ################################################################ 131 | # Install Binaries 132 | # 133 | file (COPY "${CMAKE_CURRENT_SOURCE_DIR}/include/GL" DESTINATION ${INSTALL_INC_PATH} ) 134 | file (COPY "${CMAKE_CURRENT_SOURCE_DIR}/src/glew.c" DESTINATION ${INSTALL_INC_PATH} ) 135 | install ( FILES ${INCLUDE_FILES} DESTINATION ${INSTALL_INC_PATH} ) 136 | 137 | install ( FILES ${INSTALL_LIST} DESTINATION ${INSTALL_BIN_PATH} ) 138 | install ( FILES $ DESTINATION ${INSTALL_BIN_PATH} OPTIONAL ) 139 | install ( FILES $ DESTINATION ${INSTALL_BIN_PATH} ) 140 | _INSTALL ( FILES ${PACKAGE_DLLS} DESTINATION ${INSTALL_BIN_PATH} ) 141 | install ( TARGETS ${PROJNAME} DESTINATION ${INSTALL_BIN_PATH} ) 142 | 143 | ################################# 144 | # Done 145 | message ( STATUS "\nLIBMIN INSTALL PATH: ${CMAKE_INSTALL_PREFIX}" ) 146 | message ( STATUS "LIBMIN will be installed to this path post-build (win32) or during make install (linux)") 147 | 148 | 149 | -------------------------------------------------------------------------------- /libmin/cmake/CudaAutodetectCompute.cmake: -------------------------------------------------------------------------------- 1 | function(REMOVE_DUPES ARG_STR OUTPUT) 2 | set(ARG_LIST ${ARG_STR}) 3 | separate_arguments(ARG_LIST) 4 | list(REMOVE_DUPLICATES ARG_LIST) 5 | string (REGEX REPLACE "([^\\]|^);" "\\1 " _TMP_STR "${ARG_LIST}") 6 | string (REGEX REPLACE "[\\](.)" "\\1" _TMP_STR "${_TMP_STR}") #fixes escaping 7 | set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE) 8 | endfunction() 9 | 10 | 11 | ################################################################################################ 12 | # A function for automatic detection of GPUs installed (if autodetection is enabled) 13 | # Usage: 14 | # detect_installed_gpus(out_variable) 15 | function(detect_installed_gpus out_variable) 16 | if(NOT CUDA_gpu_detect_output) 17 | set(__cufile ${PROJECT_BINARY_DIR}/detect_cuda_archs.cu) 18 | 19 | file(WRITE ${__cufile} "" 20 | "#include \n" 21 | "int main()\n" 22 | "{\n" 23 | " int count = 0;\n" 24 | " if (cudaSuccess != cudaGetDeviceCount(&count)) return -1;\n" 25 | " if (count == 0) return -1;\n" 26 | " for (int device = 0; device < count; ++device)\n" 27 | " {\n" 28 | " cudaDeviceProp prop;\n" 29 | " if (cudaSuccess == cudaGetDeviceProperties(&prop, device))\n" 30 | " std::printf(\"%d.%d \", prop.major, prop.minor);\n" 31 | " }\n" 32 | " return 0;\n" 33 | "}\n") 34 | 35 | execute_process(COMMAND "${CUDA_NVCC_EXECUTABLE}" "--run" "${__cufile}" 36 | WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/CMakeFiles/" 37 | RESULT_VARIABLE __nvcc_res OUTPUT_VARIABLE __nvcc_out 38 | ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) 39 | 40 | if(__nvcc_res EQUAL 0) 41 | string(REPLACE "2.1" "2.1(2.0)" __nvcc_out "${__nvcc_out}") 42 | message("BEFORE DUPE ${__nvcc_out}!!") 43 | REMOVE_DUPES(${__nvcc_out} dedup) 44 | message("AFTER DUPE ${dedup}") 45 | set(CUDA_gpu_detect_output ${dedup} CACHE INTERNAL "Returned GPU architectures from detect_gpus tool" FORCE) 46 | endif() 47 | endif() 48 | 49 | if(NOT CUDA_gpu_detect_output) 50 | message(STATUS "Automatic GPU detection failed. Building for all known architectures.") 51 | set(${out_variable} ${CUDA_known_gpu_archs} PARENT_SCOPE) 52 | else() 53 | set(${out_variable} ${CUDA_gpu_detect_output} PARENT_SCOPE) 54 | endif() 55 | endfunction() 56 | 57 | -------------------------------------------------------------------------------- /libmin/cmake/CudaComputeTargetFlags.cmake: -------------------------------------------------------------------------------- 1 | # 2 | # Compute target flags macros by Anatoly Baksheev 3 | # 4 | # Usage in CmakeLists.txt: 5 | # include(CudaComputeTargetFlags.cmake) 6 | # APPEND_TARGET_ARCH_FLAGS() 7 | 8 | #compute flags macros 9 | MACRO(CUDA_COMPUTE_TARGET_FLAGS arch_bin arch_ptx cuda_nvcc_target_flags) 10 | string(REGEX REPLACE "\\." "" ARCH_BIN_WITHOUT_DOTS "${${arch_bin}}") 11 | string(REGEX REPLACE "\\." "" ARCH_PTX_WITHOUT_DOTS "${${arch_ptx}}") 12 | 13 | set(cuda_computer_target_flags_temp "") 14 | 15 | # Tell NVCC to add binaries for the specified GPUs 16 | string(REGEX MATCHALL "[0-9()]+" ARCH_LIST "${ARCH_BIN_WITHOUT_DOTS}") 17 | foreach(ARCH IN LISTS ARCH_LIST) 18 | if (ARCH MATCHES "([0-9]+)\\(([0-9]+)\\)") 19 | # User explicitly specified PTX for the concrete BIN 20 | set(cuda_computer_target_flags_temp ${cuda_computer_target_flags_temp} -gencode arch=compute_${CMAKE_MATCH_2},code=sm_${CMAKE_MATCH_1}) 21 | else() 22 | # User didn't explicitly specify PTX for the concrete BIN, we assume PTX=BIN 23 | set(cuda_computer_target_flags_temp ${cuda_computer_target_flags_temp} -gencode arch=compute_${ARCH},code=sm_${ARCH}) 24 | endif() 25 | endforeach() 26 | 27 | # Tell NVCC to add PTX intermediate code for the specified architectures 28 | string(REGEX MATCHALL "[0-9]+" ARCH_LIST "${ARCH_PTX_WITHOUT_DOTS}") 29 | foreach(ARCH IN LISTS ARCH_LIST) 30 | set(cuda_computer_target_flags_temp ${cuda_computer_target_flags_temp} -gencode arch=compute_${ARCH},code=compute_${ARCH}) 31 | endforeach() 32 | 33 | set(${cuda_nvcc_target_flags} ${cuda_computer_target_flags_temp}) 34 | ENDMACRO() 35 | 36 | MACRO(APPEND_TARGET_ARCH_FLAGS) 37 | set(cuda_nvcc_target_flags "") 38 | CUDA_COMPUTE_TARGET_FLAGS(CUDA_ARCH_BIN CUDA_ARCH_PTX cuda_nvcc_target_flags) 39 | if (cuda_nvcc_target_flags) 40 | message(STATUS "CUDA NVCC target flags: ${cuda_nvcc_target_flags}") 41 | list(APPEND CUDA_NVCC_FLAGS ${cuda_nvcc_target_flags}) 42 | endif() 43 | ENDMACRO() -------------------------------------------------------------------------------- /libmin/cmake/DeclareDPIAware.manifest: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | true/PM 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /libmin/cmake/FindLibmin.cmake: -------------------------------------------------------------------------------- 1 | 2 | # 3 | # Find LibHelp 4 | # 5 | 6 | unset(LIBMIN_FOUND CACHE) 7 | unset(LIBMIN_INC_DIR CACHE) 8 | 9 | if ( NOT DEFINED LIBMIN_ROOT_DIR ) 10 | if (WIN32) 11 | get_filename_component ( BASEDIR "${CMAKE_CURRENT_BINARY_DIR}/../libmin" REALPATH ) 12 | else() 13 | get_filename_component ( BASEDIR "/usr/local/libmin/" REALPATH ) 14 | endif() 15 | set ( LIBMIN_ROOT_DIR ${BASEDIR} CACHE PATH "Location of libmin" FORCE) 16 | endif() 17 | message ( STATUS "Searching for libhelp at.. ${LIBMIN_ROOT_DIR}") 18 | set( LIBMIN_FOUND "YES" ) 19 | 20 | if ( LIBMIN_ROOT_DIR ) 21 | 22 | #-- Paths 23 | set ( LIBMIN_INC_DIR "${LIBMIN_ROOT_DIR}/include" CACHE PATH "Path to include files" FORCE) 24 | set ( LIBMIN_LIB_DIR "${LIBMIN_ROOT_DIR}/bin" CACHE PATH "Path to libraries" FORCE) 25 | 26 | #-------- Locate Header files 27 | set ( OK_H "0" ) 28 | _FIND_FILE ( LIBMIN_FILES LIBMIN_INC_DIR "common_defs.h" "common_defs.h" OK_H ) 29 | _FIND_FILE ( LIBMIN_FILES LIBMIN_INC_DIR "nv_gui.h" "nv_gui.h" OK_H ) 30 | _FIND_FILE ( LIBMIN_FILES LIBMIN_INC_DIR "vec.h" "vec.h" OK_H ) 31 | _FIND_FILE ( LIBMIN_FILES LIBMIN_INC_DIR "timex.h" "timex.h" OK_H ) 32 | if ( OK_H EQUAL 4 ) 33 | message ( STATUS " Found. Libhelp header files. ${LIBMIN_INCLUDE_DIR}" ) 34 | else() 35 | message ( " NOT FOUND. Libhelp Header files" ) 36 | set ( LIBMIN_FOUND "NO" ) 37 | endif () 38 | 39 | #-------- Locate Library 40 | set ( LIST_DLL "" ) 41 | set ( LIST_DEBUG "" ) 42 | set ( LIST_REL "" ) 43 | set ( OK_DLL 0 ) 44 | set ( OK_LIB 0 ) 45 | _FIND_FILE ( LIST_DEBUG LIBMIN_LIB_DIR "libmind.lib" "libmind.so" OK_LIB ) 46 | _FIND_FILE ( LIST_DLL LIBMIN_LIB_DIR "libmind.dll" "" OK_DLL ) 47 | 48 | _FIND_FILE ( LIST_REL LIBMIN_LIB_DIR "libmin.lib" "libmin.so" OK_LIB ) 49 | _FIND_FILE ( LIST_DLL LIBMIN_LIB_DIR "libmin.dll" "" OK_DLL ) 50 | 51 | if ( (${OK_DLL} GREATER_EQUAL 1) AND (${OK_LIB} GREATER_EQUAL 1) ) 52 | message ( STATUS " Found. Libhelp Library. ${LIBMIN_LIB_DIR}" ) 53 | else() 54 | set ( LIBMIN_FOUND "NO" ) 55 | message ( " NOT FOUND. Libhelp Library. (so/dll or lib missing)" ) 56 | endif() 57 | 58 | endif() 59 | 60 | if ( ${LIBMIN_FOUND} STREQUAL "NO" ) 61 | message( FATAL_ERROR " 62 | Please set LIBMIN_ROOT_DIR to the root location 63 | of installed Libhelp library containing LIBMIN_full.lib/dll 64 | Not found at LIBMIN_ROOT_DIR: ${LIBMIN_ROOT_DIR}\n" 65 | ) 66 | endif() 67 | 68 | set ( LIBMIN_DLLS ${LIST_DLL} CACHE INTERNAL "" FORCE) 69 | set ( LIBMIN_DEBUG ${LIST_DEBUG} CACHE INTERNAL "" FORCE) 70 | set ( LIBMIN_REL ${LIST_REL} CACHE INTERNAL "" FORCE) 71 | 72 | #-- We do not want user to modified these vars, but helpful to show them 73 | message ( STATUS " LIBMIN_ROOT_DIR: ${LIBMIN_ROOT_DIR}" ) 74 | message ( STATUS " LIBMIN_LIB_DIR: ${LIBMIN_LIB_DIR}" ) 75 | message ( STATUS " LIBMIN_DLL: ${LIBMIN_DLLS}" ) 76 | message ( STATUS " LIBMIN_DEBUG: ${LIBMIN_DEBUG}" ) 77 | message ( STATUS " LIBMIN_REL: ${LIBMIN_REL}" ) 78 | 79 | mark_as_advanced(LIBMIN_FOUND) 80 | 81 | 82 | -------------------------------------------------------------------------------- /libmin/include/camera3d.h: -------------------------------------------------------------------------------- 1 | //-------------------------------------------------------------------------------- 2 | // NVIDIA(R) GVDB VOXELS 3 | // Copyright 2017, NVIDIA Corporation. 4 | // 5 | // Redistribution and use in source and binary forms, with or without modification, 6 | // are permitted provided that the following conditions are met: 7 | // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 9 | // in the documentation and/or other materials provided with the distribution. 10 | // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived 11 | // from this software without specific prior written permission. 12 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 13 | // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 14 | // SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 15 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 16 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 17 | // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 18 | // 19 | // Version 1.0: Rama Hoetzlein, 5/1/2017 20 | //---------------------------------------------------------------------------------- 21 | 22 | 23 | #include "vec.h" 24 | 25 | #ifndef DEF_PIVOTX 26 | #define DEF_PIVOTX 27 | 28 | class HELPAPI PivotX { 29 | public: 30 | PivotX() { from_pos.Set(0,0,0); to_pos.Set(0,0,0); ang_euler.Set(0,0,0); scale.Set(1,1,1); trans.Identity(); } 31 | PivotX( Vector3DF& f, Vector3DF& t, Vector3DF& s, Vector3DF& a) { from_pos=f; to_pos=t; scale=s; ang_euler=a; } 32 | 33 | void setPivot ( float x, float y, float z, float rx, float ry, float rz ); 34 | void setPivot ( Vector3DF& pos, Vector3DF& ang ) { from_pos = pos; ang_euler = ang; } 35 | void setPivot ( PivotX piv ) { from_pos = piv.from_pos; to_pos = piv.to_pos; ang_euler = piv.ang_euler; updateTform(); } 36 | void setPivot ( PivotX& piv ) { from_pos = piv.from_pos; to_pos = piv.to_pos; ang_euler = piv.ang_euler; updateTform(); } 37 | 38 | void setIdentity () { from_pos.Set(0,0,0); to_pos.Set(0,0,0); ang_euler.Set(0,0,0); scale.Set(1,1,1); trans.Identity(); } 39 | 40 | void setAng ( float rx, float ry, float rz ) { ang_euler.Set(rx,ry,rz); updateTform(); } 41 | void setAng ( Vector3DF& a ) { ang_euler = a; updateTform(); } 42 | 43 | void setPos ( float x, float y, float z ) { from_pos.Set(x,y,z); updateTform(); } 44 | void setPos ( Vector3DF& p ) { from_pos = p; updateTform(); } 45 | 46 | void setToPos ( float x, float y, float z ) { to_pos.Set(x,y,z); updateTform(); } 47 | 48 | void updateTform (); 49 | void setTform ( Matrix4F& t ) { trans = t; } 50 | inline Matrix4F& getTform () { return trans; } 51 | inline float* getTformData () { return trans.GetDataF(); } 52 | 53 | // Pivot 54 | PivotX getPivot () { return PivotX(from_pos, to_pos, scale, ang_euler); } 55 | Vector3DF& getPos () { return from_pos; } 56 | Vector3DF& getToPos () { return to_pos; } 57 | Vector3DF& getAng () { return ang_euler; } 58 | Vector3DF getDir () { 59 | return to_pos - from_pos; 60 | } 61 | void getPreciseEye (Vector3DF& hi, Vector3DF& lo) 62 | { 63 | //-- should use a double precision camera position as input 64 | // see: https://prideout.net/emulating-double-precision 65 | hi = from_pos; 66 | lo.Set(0,0,0); 67 | } 68 | Vector3DF from_pos; 69 | Vector3DF to_pos; 70 | Vector3DF scale; 71 | Vector3DF ang_euler; 72 | Matrix4F trans; 73 | 74 | //Quatern ang_quat; 75 | //Quatern dang_quat; 76 | }; 77 | 78 | #endif 79 | 80 | #ifndef DEF_CAMERA_3D 81 | #define DEF_CAMERA_3D 82 | 83 | #define DEG_TO_RAD (3.141592/180.0) 84 | 85 | class HELPAPI Camera3D : public PivotX { 86 | public: 87 | enum eProjection { 88 | Perspective = 0, 89 | Parallel = 1 90 | }; 91 | Camera3D (); 92 | void Copy ( Camera3D& op ); 93 | 94 | void draw_gl(); 95 | 96 | // Camera settings 97 | void setAspect ( float asp ) { mAspect = asp; updateMatricies(); } 98 | void setPos ( float x, float y, float z ) { from_pos.Set(x,y,z); updateMatricies(); } 99 | void setToPos ( float x, float y, float z ) { to_pos.Set(x,y,z); updateMatricies(); } 100 | void setFov (float fov) { mFov = fov; updateMatricies(); } 101 | void setNearFar (float n, float f ) { mNear = n; mFar = f; updateMatricies(); } 102 | void setDist ( float d ) { mOrbitDist = d; updateMatricies(); } 103 | void setTile ( float x1, float y1, float x2, float y2 ) { mTile.Set ( x1, y1, x2, y2 ); updateMatricies(); } 104 | void setSize ( float w, float h ) { mXres=w; mYres=h; } 105 | void setProjection (eProjection proj_type); 106 | void setModelMatrix ( float* mtx ); 107 | void setViewMatrix ( float* mtx, float* invmtx ); 108 | void setProjMatrix ( float* mtx, float* invmtx ); 109 | void setMatrices (const float* view_mtx, const float* proj_mtx, Vector3DF model_pos ); 110 | 111 | // Camera motion 112 | void setOrbit ( float ax, float ay, float az, Vector3DF tp, float dist, float dolly ); 113 | void setOrbit ( Vector3DF angs, Vector3DF tp, float dist, float dolly ); 114 | void setAngles ( float ax, float ay, float az ); 115 | void moveOrbit ( float ax, float ay, float az, float dist ); 116 | void moveToPos ( float tx, float ty, float tz ); 117 | void moveRelative ( float dx, float dy, float dz ); 118 | 119 | // Frustum testing 120 | bool pointInFrustum ( float x, float y, float z ); 121 | bool boxInFrustum ( Vector3DF bmin, Vector3DF bmax); 122 | float calculateLOD ( Vector3DF pnt, float minlod, float maxlod, float maxdist ); 123 | 124 | // Utility functions 125 | void updateMatricies (bool compute_view=false); // Updates camera axes and projection matricies 126 | void updateFrustum (); // Updates frustum planes 127 | Vector3DF inverseRay ( float x, float y, float z=1.0 ); 128 | Vector3DF inverseRayProj ( float x, float y, float z ); 129 | Vector4DF project ( Vector3DF& p ); 130 | Vector4DF project ( Vector3DF& p, Matrix4F& vm ); // Project point - override view matrix 131 | 132 | void getVectors ( Vector3DF& dir, Vector3DF& up, Vector3DF& side ) { dir = dir_vec; up = up_vec; side = side_vec; } 133 | void getBounds ( float dst, Vector3DF& min, Vector3DF& max ); 134 | float getNear () { return mNear; } 135 | float getFar () { return mFar; } 136 | float getFov () { return mFov; } 137 | float getDolly() { return mDolly; } 138 | float getOrbitDist() { return mOrbitDist; } 139 | Vector3DF& getUpDir () { return up_dir; } 140 | Vector4DF& getTile () { return mTile; } 141 | Matrix4F& getInvViewProjMatrix () { return invviewproj_matrix; } 142 | Matrix4F& getViewMatrix () { return view_matrix; } 143 | Matrix4F& getInvView () { return invrot_matrix; } 144 | Matrix4F& getRotateMatrix () { return rotate_matrix; } 145 | Matrix4F& getProjMatrix () { return tileproj_matrix; } 146 | Matrix4F& getFullProjMatrix () { return proj_matrix; } 147 | Matrix4F& getModelMatrix() { return model_matrix; } 148 | Matrix4F& getMVMatrix() { return mv_matrix; } 149 | float getAspect () { return mAspect; } 150 | Vector3DF getU (); 151 | Vector3DF getV (); 152 | Vector3DF getW (); 153 | float getDu (); 154 | float getDv (); 155 | 156 | 157 | public: 158 | eProjection mProjType; // Projection type 159 | 160 | // Camera Parameters // NOTE: Pivot maintains camera from and orientation 161 | float mDolly; // Camera to distance 162 | float mOrbitDist; 163 | float mFov, mAspect; // Camera field-of-view 164 | float mNear, mFar; // Camera frustum planes 165 | float mXres, mYres; 166 | Vector3DF dir_vec, side_vec, up_vec; // Camera aux vectors (W, V, and U) 167 | Vector3DF up_dir; 168 | Vector4DF mTile; 169 | 170 | // Transform Matricies 171 | Matrix4F invviewproj_matrix; 172 | Matrix4F rotate_matrix; // Vr matrix (rotation only) 173 | Matrix4F view_matrix; // V matrix (rotation + translation) 174 | Matrix4F proj_matrix; // P matrix 175 | Matrix4F invrot_matrix; // Vr^-1 matrix 176 | Matrix4F invproj_matrix; 177 | Matrix4F tileproj_matrix; // tiled projection matrix 178 | Matrix4F model_matrix; 179 | Matrix4F mv_matrix; 180 | float frustum[6][4]; // frustum plane equations 181 | 182 | bool mOps[8]; 183 | int mWire; 184 | 185 | Vector3DF origRayWorld; 186 | Vector4DF tlRayWorld; 187 | Vector4DF trRayWorld; 188 | Vector4DF blRayWorld; 189 | Vector4DF brRayWorld; 190 | }; 191 | 192 | typedef Camera3D Light; 193 | 194 | #endif 195 | -------------------------------------------------------------------------------- /libmin/include/common_cuda.h: -------------------------------------------------------------------------------- 1 | 2 | #ifdef USE_CUDA 3 | #ifndef DEF_MAIN_CUDA 4 | #define DEF_MAIN_CUDA 5 | 6 | 7 | #include 8 | #include 9 | 10 | #define DEV_FIRST -1 11 | #define DEV_CURRENT -2 12 | #define DEV_EXISTING -3 13 | 14 | HELPAPI bool cuCheck(CUresult launch_stat, char* method, char* apicall, char* arg, bool bDebug); 15 | HELPAPI void cuStart(int devsel, CUcontext ctxsel, CUdevice& dev, CUcontext& ctx, CUstream* strm, bool verbose); 16 | HELPAPI void cuGetMemUsage(int& total, int& used, int& free); 17 | 18 | #endif 19 | #endif -------------------------------------------------------------------------------- /libmin/include/common_defs.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef DEF_COMMON 4 | #define DEF_COMMON 5 | 6 | #pragma warning ( disable: 4005) 7 | 8 | #ifdef _WIN32 9 | 10 | #define WIN32_LEAN_AND_MEAN 11 | #include 12 | #undef WIN32_LEAN_AND_MEAN 13 | 14 | #pragma warning ( disable : 4800 ) // cast to bool performance warning 15 | #pragma warning ( disable : 4996 ) // fopen_s, strcpy_s (not linux compatible) 16 | #pragma warning ( disable : 4244 ) // conversion from double to float 17 | #pragma warning ( disable : 4305 ) // truncation from double to float (constants) 18 | #pragma warning ( disable : 4251 ) // STL objects inside DLL-interface classes 19 | 20 | #if !defined ( LIBHELP_STATIC ) 21 | #if defined ( LIBHELP_EXPORTS ) // inside DLL 22 | #if defined(_WIN32) || defined(__CYGWIN__) 23 | #define HELPAPI __declspec(dllexport) 24 | #else 25 | #define HELPAPI __attribute__((visibility("default"))) 26 | #endif 27 | #else // outside DLL 28 | #if defined(_WIN32) || defined(__CYGWIN__) 29 | #define HELPAPI __declspec(dllimport) 30 | #else 31 | #define HELPAPI //https://stackoverflow.com/questions/2164827/explicitly-exporting-shared-library-functions-in-linux 32 | #endif 33 | #endif 34 | #else 35 | #define HELP_API 36 | #endif 37 | 38 | #include "inttypes.h" 39 | 40 | #define ALIGN(x) __declspec(align(x)) 41 | #define CACHE_ALIGNED __declspec(align(64)) 42 | 43 | typedef signed char sint8_t; 44 | typedef signed short sint16_t; 45 | typedef signed int sint32_t; 46 | typedef signed long sint64_t; 47 | 48 | typedef unsigned char uchar; 49 | typedef uint64_t xlong; 50 | typedef uint8_t XCHAR; 51 | typedef uint8_t XBYTE; 52 | typedef uint16_t XBYTE2; 53 | typedef uint32_t XBYTE4; 54 | typedef uint64_t XBYTE8; 55 | typedef sint8_t schar; 56 | typedef sint16_t sshort; 57 | typedef sint32_t sint; 58 | typedef sint64_t slong; 59 | typedef uint8_t uchar; 60 | typedef uint16_t ushort; 61 | typedef uint32_t uint; 62 | typedef uint64_t uxlong; // note: keyword 'ulong' cannot be used with NV_ARM. 'slong' is signed, dont use here 63 | 64 | #define FALSE 0 65 | #define TRUE 1 66 | 67 | // DWORD included from windows.h (32-bit unsigned int) 68 | 69 | #else // ANDOID and linux 70 | 71 | #define ALIGN(x) __attribute__ ((aligned(x))) 72 | #define CACHE_ALIGNED __attribute__ ((aligned(64))) 73 | 74 | #include "inttypes.h" 75 | 76 | // typedef __s64 xlong; 77 | typedef unsigned long long xlong; 78 | typedef unsigned char XCHAR; 79 | typedef unsigned char XBYTE; // 8-bit 80 | typedef unsigned short XBYTE2; // 16-bit 81 | typedef unsigned long XBYTE4; // 32-bit 82 | typedef long long XBYTE8; // 64-bit 83 | typedef XBYTE4 DWORD; 84 | 85 | #define FALSE 0 86 | #define TRUE 1 87 | 88 | typedef uint8_t uchar; 89 | typedef uint16_t ushort; 90 | typedef uint32_t uint; 91 | typedef uint64_t uxlong; 92 | 93 | typedef int8_t schar; 94 | typedef int16_t sshort; 95 | typedef int32_t sint; 96 | typedef int64_t slong; 97 | 98 | // avoids Clang warnings 99 | #define __cdecl 100 | #define __stdcall 101 | 102 | #include // for va_start, va_args 103 | 104 | #endif 105 | 106 | typedef float f32; 107 | typedef double f64; 108 | 109 | const f32 ROUNDING_ERROR_f32 = 0.000001f; 110 | const f64 ROUNDING_ERROR_f64 = 0.00000001; 111 | const f64 PI64 = 3.1415926535897932384626433832795028841971693993751; 112 | 113 | 114 | //--- OpenGL include 115 | #ifdef USE_OPENGL 116 | #if defined(__ANDROID__) 117 | #include 118 | #include 119 | #elif defined(__linux__) 120 | 121 | #elif defined(_WIN32) 122 | #include 123 | #include 124 | #endif 125 | #endif 126 | 127 | // Universal functions 128 | #include 129 | #include 130 | HELPAPI void checkMem( xlong& total, xlong& used, xlong& app); 131 | HELPAPI char getPathDelim(); 132 | HELPAPI void addSearchPath ( const char* path ); 133 | HELPAPI bool getFileLocation ( const char* filename, char* outpath ); 134 | HELPAPI bool getFileLocation ( const char* filename, char* outpath, std::vector paths ); 135 | HELPAPI bool getFileLocation ( const std::string filename, std::string &outpath ); 136 | HELPAPI unsigned long getFileSize ( const std::string filename ); 137 | HELPAPI unsigned long getFilePos ( FILE* fp ); 138 | HELPAPI void dbgprintf(const char * fmt, ...); 139 | 140 | // Basic OpenGL interface 141 | HELPAPI void checkGL(const char* msg); 142 | HELPAPI void initTexGL(); 143 | HELPAPI void clearGL(); 144 | HELPAPI void createTexGL(int& glid, int w, int h, int clamp = 0x812D, int fmt = 0x8058, int typ = 0x1401, int filter = 0x2601); // defaults: GL_CLAMP_TO_BORDER, GL_RGBA8, GL_UNSIGNED_BYTE, GL_LINEAR 145 | HELPAPI void renderTexGL(int w, int h, int glid, char inv1 = 0); 146 | HELPAPI void renderTexGL(float x1, float y1, float x2, float y2, int glid1, char inv1 = 0); 147 | HELPAPI void compositeTexGL(float blend, int w, int h, int glid1, int glid2, char inv1 = 0, char inv2 = 0); // composite two textures 148 | 149 | struct HELPAPI TexInterface { 150 | int prog[3]; 151 | int vshader[3]; 152 | int fshader[3]; 153 | int vbo[3]; 154 | int utex1[3]; 155 | int utex2[3]; 156 | int utexflags[3]; 157 | int ucoords[3]; 158 | int uscreen[3]; 159 | }; 160 | 161 | HELPAPI void strncpy_sc ( char *dst, const char *src, size_t len); // cross-platform 162 | HELPAPI void strncpy_sc (char *dst, size_t dstsz, const char *src, size_t count ); // cross-platform 163 | 164 | // mathematical macros & inlines 165 | #ifndef imax 166 | #define imax(a,b) (((a) > (b)) ? (a) : (b)) 167 | #endif 168 | #ifndef imin 169 | #define imin(a,b) (((a) < (b)) ? (a) : (b)) 170 | #endif 171 | inline f64 clamp(f64 value, f64 low, f64 high) { return imin(imax(value, low), high); } 172 | 173 | inline bool fequal(double a, double b, double eps) { return (fabs(a - b) < eps); } 174 | 175 | inline float fast_inv_squareroot (float number) { 176 | long i; 177 | float x2, y; 178 | const float threehalfs = 1.5F; 179 | x2 = number * 0.5F; y = number; 180 | i = *(long*)&y; i = 0x5f3759df - (i >> 1); 181 | y = *(float*)&i; y = y * (threehalfs - (x2 * y * y)); 182 | #ifndef Q3_VM 183 | #ifdef __linux__ 184 | assert(!isnan(y)); 185 | #endif 186 | #endif 187 | return y; 188 | } 189 | 190 | // color storage in 4-byte uint 191 | typedef uint32_t CLRVAL; 192 | #ifndef COLOR 193 | #define COLOR(r,g,b) ( (uint(r*255.0f)<<24) | (uint(g*255.0f)<<16) | (uint(b*255.0f)<<8) ) 194 | #endif 195 | #ifndef COLORA 196 | #define COLORA(r,g,b,a) ( (uint(a*255.0f)<<24) | (uint(b*255.0f)<<16) | (uint(g*255.0f)<<8) | uint(r*255.0f) ) 197 | #endif 198 | #define ALPH(c) (float((c>>24) & 0xFF)/255.0) 199 | #define BLUE(c) (float((c>>16) & 0xFF)/255.0) 200 | #define GRN(c) (float((c>>8) & 0xFF)/255.0) 201 | #define RED(c) (float( c & 0xFF)/255.0) 202 | #ifndef CLRVEC 203 | #define CLRVEC(c) ( Vector4DF( RED(c), GRN(c), BLUE(c), ALPH(c) ) ) 204 | #endif 205 | #ifndef VECCLR 206 | #define VECCLR(v) ( COLORA( v.x, v.y, v.z, v.w ) ) 207 | #endif 208 | 209 | // math defs 210 | #ifndef PI 211 | #define PI (3.14159265358979f) // sometimes useful :) 212 | #endif 213 | #ifndef DEGtoRAD 214 | #define DEGtoRAD (3.14159265358979f/180.0f) 215 | #endif 216 | #ifndef RADtoDEG 217 | #define RADtoDEG (180.0f/3.14159265358979f) 218 | #endif 219 | 220 | #endif 221 | -------------------------------------------------------------------------------- /libmin/include/dataptr.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef DEF_DATAPTR_H 3 | #define DEF_DATAPTR_H 4 | 5 | #include "common_defs.h" 6 | #include 7 | #include 8 | #include 9 | #include "vec.h" 10 | #ifdef USE_CUDA 11 | #include "cuda.h" 12 | #define PUSH_CTX cuCtxPushCurrent(cuCtx); 13 | #define POP_CTX CUcontext pctx; cuCtxPopCurrent(&pctx); 14 | #else 15 | #define PUSH_CTX 16 | #define POP_CTX 17 | #endif 18 | 19 | #define DT_MISC 0 20 | #define DT_UCHAR 1 // 8-bit 21 | #define DT_USHORT 2 // 16-bit 22 | #define DT_UCHAR3 3 // 24-bit 23 | 24 | #define DT_UCHAR4 4 // 32-bit, 4 bytes, 4*sizeof(char) 25 | #define DT_INT 5 // 32-bit, 4 bytes, 1*sizeof(int32_t) 26 | #define DT_UINT 6 // 32-bit, 4 bytes, 1*sizeof(uint32_t) 27 | #define DT_FLOAT 7 // 32-bit, 4 bytes, 1*sizeof(float) 28 | 29 | #define DT_UINT64 8 // 64-bit, 8 bytes, 1*sizeof(uint64_t) 30 | #define DT_FLOAT3 12 // 96-bit, 12 bytes, 3*sizeof(float) 31 | #define DT_FLOAT4 16 // 128-bit, 16 bytes, 4*sizeof(float) 32 | 33 | #define DT_CPU 1 // use flags 34 | #define DT_CUMEM 2 35 | #define DT_CUARRAY 4 36 | #define DT_GLTEX 8 37 | #define DT_GLVBO 16 38 | 39 | class HELPAPI DataPtr { 40 | public: 41 | DataPtr() { mNum=0; mMax=0; mStride=0; mUseRes.Set(0,0,0); mUseType=DT_MISC; mUseFlags=DT_MISC; 42 | mSize=0; mCpu=0; 43 | #ifdef USE_CUDA 44 | mGpu=0; mGrsc=0; mGarray=0; mGLID=-1; mGtex = -1; mGsurf = -1; 45 | #endif 46 | } 47 | ~DataPtr(); 48 | 49 | void Resize ( int stride, uint64_t cnt, char* dat=0x0, uchar dest_flags=DT_CPU ); 50 | int Append ( int stride, uint64_t cnt, char* dat=0x0, uchar dest_flags=DT_CPU ); 51 | void SetUsage ( uchar dt, uchar flags=DT_MISC, Vector3DI res = Vector3DI(-1,-1,-1) ); // special usage (2D,3D,GLtex,GLvbo,etc.) 52 | void UpdateUsage ( uchar flags ); 53 | void ReallocateCPU ( uint64_t oldsz, uint64_t newsz ); 54 | void FillBuffer ( uchar v ); 55 | void CopyTo ( DataPtr* dest, uchar dest_flags ); 56 | void Commit (); 57 | void Retrieve (); 58 | void Clear (); 59 | 60 | int getStride ( uchar dtype ); 61 | uint64_t getDataSz ( int cnt, int stride ) { return (uint64_t) cnt * stride; } 62 | 63 | int getNum() { return mNum; } 64 | int getMax() { return mMax; } 65 | char* getData() { return mCpu; } 66 | #ifdef USE_CUDA 67 | CUdeviceptr getGPU() { return mGpu; } 68 | #endif 69 | char* getPtr(int n) { return mCpu + n*mStride; } 70 | 71 | public: 72 | uint64_t mNum, mMax, mSize; 73 | int mStride; 74 | uchar mRefID, mUseType, mUseFlags; // usage 75 | Vector3DI mUseRes; 76 | bool bCpu, bGpu; 77 | char* mCpu; 78 | 79 | int mGLID; // OpenGL 80 | 81 | #ifdef USE_CUDA 82 | CUdeviceptr mGpu; // CUDA 83 | CUgraphicsResource mGrsc; // CUDA-GL interop 84 | CUarray mGarray; 85 | CUtexObject mGtex; // CUDA texture/surface interop 86 | CUsurfObject mGsurf; 87 | #endif 88 | 89 | static int mFBO; 90 | }; 91 | 92 | #endif 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /libmin/include/directory.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef DEF_DIR_OBJECT 3 | #define DEF_DIR_OBJECT 4 | 5 | #include "common_defs.h" 6 | #include 7 | #include 8 | 9 | #define FILE_TYPE_DIR 0 10 | #define FILE_TYPE_FILE 1 11 | 12 | // Hmmm - have to factor this specific stuff out. 13 | #define FILE_TYPE_BLOCKSAVE 2 14 | #define FILE_TYPE_BLOCKLEV 3 15 | 16 | typedef struct { 17 | int type; 18 | std::string text; 19 | std::string extension; 20 | int length; 21 | } dir_list_element; 22 | 23 | typedef std::vector< dir_list_element > dir_list; 24 | 25 | typedef struct { 26 | std::string text; 27 | float length; 28 | } text_element_t; 29 | 30 | typedef std::vector< text_element_t > elem_vec_t; 31 | 32 | 33 | class HELPAPI Directory { 34 | public: 35 | Directory (); 36 | 37 | void LoadDir ( std::string path, std::string filter ); 38 | int CreateDir ( std::string path ); 39 | int getNumFiles () { return (int) mList.size(); } 40 | dir_list_element getFile ( int n ) { return mList[n]; } 41 | bool isDir ( int n ) { return mList[n].type==0; } 42 | 43 | // Static functions 44 | static dir_list DirList( std::string path, std::string filter ); 45 | static bool FileExists( std::string filename ); 46 | static std::string ws2s(const std::wstring& s); 47 | static std::wstring s2ws(const std::string& s); 48 | static dir_list GetFileItems( dir_list input); 49 | static dir_list GetDirectoryItems( dir_list input); 50 | static std::string NormalizeSlashies( std::string path ); 51 | static std::string GetExtension( std::string path ); 52 | static std::string GetExecutablePath(); 53 | static std::string GetCollapsedPath( std::string path ); 54 | static std::string gPathDelim; 55 | 56 | std::string getPath () { return mPath; } 57 | std::string getFilter () { return mFileFilter; } 58 | 59 | private: 60 | 61 | std::string mPath; 62 | std::string mFileFilter; 63 | 64 | 65 | dir_list mList; 66 | 67 | }; 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /libmin/include/event.h: -------------------------------------------------------------------------------- 1 | //--------------------------------------------------------------------- 2 | // 3 | // Event System 4 | // Quanta Sciences, Rama Hoetzlein (c) 2007-2020 5 | // 6 | //--------------------------------------------------------------------- 7 | 8 | 9 | #ifndef DEF_EVENT_H 10 | #define DEF_EVENT_H 11 | 12 | #include "common_defs.h" 13 | #include "vec.h" 14 | #include "timex.h" 15 | #include 16 | 17 | #define NULL_TARGET 65535 18 | #define ID(y) ( (const xlong) *( (const xlong*) y ) ) // for 64-bit names, but not a const expression 19 | 20 | // Event typedefs 21 | typedef TimeX timeStamp_t; 22 | typedef uint32_t eventStr_t; 23 | typedef unsigned long netIP; 24 | typedef signed int netPort; 25 | typedef signed int netSock; 26 | typedef uint16_t sysID_t; // locally assigned ID 27 | typedef uint32_t objType; 28 | typedef uint8_t datType; 29 | 30 | class EventPool; 31 | 32 | // Event names 33 | std::string nameToStr ( eventStr_t name ); 34 | eventStr_t strToName ( std::string str ); 35 | 36 | // Event 37 | struct HELPAPI CACHE_ALIGNED Event { 38 | public: 39 | Event (); 40 | Event ( const Event& src ); 41 | Event& operator= ( const Event* op ); // assignment (not acquire) 42 | Event& operator= ( const Event& op ); 43 | ~Event (); 44 | void copyEventVars ( Event* dst, const Event* src ); 45 | void acquire ( Event& esrc); // acquire - transfer of ownership 46 | 47 | // Event Accessors 48 | std::string getNameStr (); 49 | std::string getSysStr (); 50 | inline eventStr_t getName () { return mName; } 51 | inline eventStr_t getTarget () { return mTarget; } 52 | inline sysID_t getTargetID () { return mTargetID; } 53 | inline timeStamp_t getTimeStamp() { return mTimeStamp; } 54 | inline EventPool* getPool() { return mOwner; } 55 | inline void set ( eventStr_t targ, eventStr_t name ) { mTarget = targ; mName = name; } 56 | inline void setName ( eventStr_t x ) { mName = x; } 57 | inline void setTarget ( eventStr_t x ) { mTarget = x; } 58 | inline void setTargetID ( sysID_t t ) { mTargetID = t; } 59 | inline void setTimeStamp ( timeStamp_t t ) { mTimeStamp = t; } 60 | 61 | // Event Reference counting 62 | inline int incRefs () { return ++mRefs; } 63 | inline int decRefs () { return --mRefs; } 64 | inline int getRefs () { return mRefs; } 65 | 66 | // Data Access 67 | void startRead (); 68 | void startWrite (); 69 | void expand ( int s ); 70 | char* serialize (); 71 | void deserialize ( char* buf, int len ); 72 | void rescope ( char* scope ) { memcpy ( mScope, scope, 4 ); mScope[4]='\0'; } 73 | //int getEventLenOffs () { return int((char*) &mDataLen - (char*) &mTarget); } 74 | char* getData () { return mData; } 75 | char* getPos() { return mPos; } 76 | unsigned long getPosInt() { return mPos - mData; } 77 | char* getEndPos () { return getData() + mMax; } 78 | bool isEnd () { return mPos >= getData() + mDataLen; } 79 | bool isEmpty () { return mData == 0x0; } 80 | int getDataLength () { return mDataLen; } // daata payload 81 | int getPayloadLength() { return mDataLen; } // synonym 82 | int getSerializedLength () { return mDataLen + Event::staticSerializedHeaderSize(); } // length of network packet data 83 | char* getSerializedData () { return mData - Event::staticSerializedHeaderSize(); } 84 | 85 | // Get/set 86 | inline void setDataLength (int n ) { mDataLen = n; } 87 | inline void setPos (int offs) { mPos = mData + offs; } 88 | inline void setPos (char* pos ) { mPos = pos; } 89 | inline timeStamp_t getTime () { return mTimeStamp; } 90 | inline void setTime ( timeStamp_t t ) { mTimeStamp = t; } 91 | void setTime ( unsigned long t ); 92 | inline void setConnection ( unsigned short id ) { mConnection = id; } 93 | inline ushort getConnection () { return mConnection; } 94 | inline void setSrcIP ( netIP ip ) { mSrcIP = ip; } 95 | inline void setSrcSock ( netSock s ) { mSrcSock = s; } 96 | inline netIP getSrcIP () { return mSrcIP; } 97 | inline netSock getSrcSock () { return mSrcSock; } 98 | 99 | // Data Attach/Retrieve 100 | void attachBool (bool b); 101 | void attachInt (int i); 102 | void attachShort (signed short i ); 103 | void attachUChar (unsigned char i ); 104 | void attachUShort (unsigned short i ); 105 | void attachULong (unsigned long i ); 106 | void attachUInt (unsigned int i ); 107 | void attachInt64 (xlong i); 108 | void attachFloat (float f); 109 | void attachDouble (double d); 110 | void attachStr (std::string str ); 111 | void attachPrintf (const char* format, ... ); 112 | void attachVec4 (Vector4DF i ); 113 | void attachMem (char* buf, int len ); 114 | void attachBuf (char* buf, int len ); 115 | void attachBufAtPos (int pos, char* buf, int len ); 116 | void writeUShort (int pos, unsigned short i ); // does not increase length 117 | void attachFromFile (FILE* fp, int len ); 118 | 119 | bool getBool (); 120 | int getInt (); 121 | signed short getShort (); 122 | unsigned char getUChar (); 123 | unsigned short getUShort (); 124 | unsigned int getUInt (); 125 | unsigned long getULong (); 126 | xlong getInt64 (); 127 | float getFloat (); 128 | double getDouble (); 129 | Vector4DF getVec4 (); 130 | std::string getStr (); 131 | void getStr (char* str ); 132 | void getMem (char* buf, int len ); 133 | void getBuf (char* buf, int len ); 134 | void getBufAtPos (int pos, char* buf, int len ); 135 | 136 | // Serialized header length. Must match platform size of member vars 137 | static int staticSerializedHeaderSize() { return 2*sizeof(int) + 2*sizeof(eventStr_t) + sizeof(timeStamp_t); } 138 | static int staticOffsetLenInfo() { return 0; } // <-- assumes mDataLen is first 139 | 140 | // **** NOTE *** 141 | // !! ORDER OF MEMBERS IS IMPORTANT HERE !! 142 | // Serialized first. mDataLen first member. 143 | 144 | // Serialized members 145 | // Header: Offset Bytes 146 | int mDataLen; // Event length 32+ 0 4 // <-- offsetDataLen (!) 147 | eventStr_t mName; // Event name 32+ 4 4 148 | eventStr_t mTarget; // Event target system 32+ 8 4 149 | int mConnection; // Event connection 32+ 12 4 150 | timeStamp_t mTimeStamp; // Event time stamp 32+ 16 8 151 | // 56 24 byte // <-- offsetHeader (!) 152 | // Data payload is pre-pended with serialized members 153 | char* mData; // Data payload (elsewhere in memory) 154 | 155 | // Non-serialized members 156 | ushort mRefs; // Ref counting (max: 65535) 157 | ushort mSrcSock; // Source Socket (max: 65535) 158 | netIP mSrcIP; // Source IP 159 | sysID_t mTargetID; // Target ID 160 | int mMax; // Data max 161 | EventPool* mOwner; // Memory pool owner 162 | bool bOwn; // Owner info 163 | bool bDestroy; // Destroy 164 | char mScope[5]; // Scope info 165 | char* mPos; // Data pos 166 | }; 167 | 168 | 169 | 170 | #endif 171 | -------------------------------------------------------------------------------- /libmin/include/event_system.h: -------------------------------------------------------------------------------- 1 | //--------------------------------------------------------------------- 2 | // 3 | // Event System 4 | // Quanta Sciences, Rama Hoetzlein (c) 2007-2020 5 | // 6 | //--------------------------------------------------------------------- 7 | 8 | 9 | //#define BUILD_EVENT_POOLING // Enable/disable compiliation of Event Pooling 10 | 11 | //#define USE_EVENT_POOLING // Enable or disable event pooling 12 | 13 | #ifndef DEF_EVENT_SYSTEM_H 14 | #define DEF_EVENT_SYSTEM_H 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "event.h" 21 | 22 | // Static buffer (temporary) 23 | static char mbuf [ 16384 ]; 24 | 25 | #define EVENT_LEN_OFFSET 20 26 | 27 | // Event memory management [required] 28 | char* new_event_data ( size_t size, int& max, EventPool* pool ); 29 | Event new_event ( size_t size, eventStr_t targ, eventStr_t name, eventStr_t state, EventPool* pool ); 30 | void expand_event ( Event& e, size_t size ); 31 | void delete_event (Event& e); // aka. free_event 32 | void free_event_data ( char* data, EventPool* pool ); 33 | 34 | // Event queue - maintains a queue of events 35 | class HELPAPI EventQueue { 36 | public: 37 | EventQueue (); 38 | 39 | void clear (); 40 | void push ( Event& e ); 41 | void push_back (Event& e ); 42 | void pop (); 43 | inline Event front () { return mList.front(); } 44 | inline Event back () { return mList.back(); } 45 | int size () { return (int) mList.size(); } 46 | 47 | //-- debugging 48 | void startTrace ( char* fn ); 49 | void trace (); 50 | EventQueue& operator= ( EventQueue &op ); 51 | 52 | private: 53 | std::queue < Event > mList; 54 | FILE* mTraceFile; 55 | }; 56 | 57 | 58 | #ifdef BUILD_EVENT_POOLING 59 | //------------ Event Pooling [optional] 60 | // 61 | // Event Pooling was designed for very fast event memory allocations. 62 | // Whether it is actually faster than malloc/free must be evaluated per platform. 63 | // The technique uses ten bins of power-of-two fixed width memory pools. 64 | // Each set contains a linked list of pools. Within a single pool, events 65 | // are assigned sequentially. When an event is deleted/expanded, it is removed 66 | // from a pool and assigned to a different one. When all event slots in pools have 67 | // been used, a new pool inserted in the linked list. When a pool is depleted 68 | // then it is removed from the linked list. (Rama Hoetzlein) 69 | // 70 | // Limitations: 71 | // - 10 bins. (limited by log table lookup) 72 | // - 64 min item size. (must be power of two) 73 | // - 32768 max item size. (largest bin, limited by log table lookup, and efficient use of 65544 block size.) 74 | // - 65544 block size. (ideal size to give 4x 16386 blocks, 2x 32770 blocks) 75 | // - 64 byte header. (size of MBlock struct below, plus little extra) 76 | 77 | #define BIN_CNT 10 // Bins: 0..BIN_CNT-1 78 | 79 | #define MIN_WIDTH 64 // Smallest bin size. Minimum block width. 80 | #define MIN_WIDTH_BITS 6 // (Why: Pool header is 64) 81 | 82 | #define MAX_BINS 8 // Largest bin number. (0..MAX_BINS). Limited by lookup table. 83 | #define MAX_POOL_SIZE 16384 // 2^(MAX_BINS+6) = 2^(8+6) = 16384 84 | 85 | #define BLOCK_SIZE 65536 // Total block size 86 | 87 | // Definitions: 88 | // Pool - Multiple blocks organized by bin 89 | // Bin - Linked list of blocks of a particular width 90 | // Block - List of items of a specific width 91 | // Item - Unit of allocation. 92 | 93 | // Memory Pool structure 94 | // - First item in a block is the MBlock Header 95 | // - Each item in a block has a freeword at the end of it (uint32_t) 96 | // Block Size Bin Item Width Item Count - Item Width = Data width + freeword (4 bytes) 97 | // 65536 0 64 1024 98 | // 65536 1 128 512 99 | // 65536 2 256 256 100 | // 65536 3 512 128 101 | // 65536 4 1024 64 102 | // 65536 5 2048 32 103 | // 65536 6 4096 16 104 | // 65536 7 8192 8 105 | // 65536 8 16384 4 106 | // 65536 9 32768 2 107 | 108 | typedef char* itemPtr; 109 | 110 | __declspec(align(64)) struct MBlock { 111 | uint32_t mMagic; 112 | itemPtr mPos; // Position of next alloc item 4 bytes 113 | itemPtr mEnd; // End of block position 4 bytes 114 | int mBin; // Bin number 4 bytes 115 | int mUsed; // Number of used items in block 4 bytes 116 | int mWidth; // Width of bin 4 bytes 117 | int mCount; // Maximum items supported 4 bytes 118 | MBlock* mPrev; // Previous block (linked list) 8 bytes 119 | MBlock* mNext; // Next block (linked list) 8 bytes 120 | bool mbFull; // Is block in a full list? 1 byte 121 | char mData; // Start of data 1 byte 122 | }; 123 | typedef MBlock* blockPtr; 124 | 125 | class HELPAPI EventPool { 126 | public: 127 | EventPool(); 128 | ~EventPool(); 129 | void clear (); 130 | 131 | // User functions 132 | void* allocItem ( int size ); 133 | void freeItem ( void* item ); 134 | 135 | blockPtr addBlock ( int bin ); 136 | blockPtr makeFull ( blockPtr block ); 137 | blockPtr makeFree ( blockPtr block ); 138 | void setBlockHeader ( blockPtr block, int wid, int count, int bin ); 139 | 140 | unsigned int getNumBins () { return BIN_CNT; } 141 | int getHeaderSize () { return sizeof(MBlock); } 142 | int getBinWidth ( int bin ) { return ( 1 << (bin + MIN_WIDTH_BITS) ); } 143 | 144 | int getBlockSize ( int bin ) { return BLOCK_SIZE; } // total block size (not including header) 145 | int getBlockWidth ( int bin ) { return getBinWidth( bin ) ; } // size of block.. power of 2 146 | int getItemCount ( int bin ) { return (BLOCK_SIZE / getBlockWidth ( bin )) - 1; } // number of items in block 147 | int getItemWidth ( int bin ) { return getBinWidth ( bin ) - sizeof(int); } // max size of item in block 148 | int getItemMaxSize ( int sz ) { return getItemWidth( getBin(sz) ); } // maximum payload size (given intial size) 149 | 150 | int getAllocated ( int bin ); 151 | 152 | void print () { integrity (); } 153 | void integrity (); 154 | int checkBlock ( blockPtr block ); 155 | 156 | blockPtr getFullBin (int n ) { return mFullBins[n]; } 157 | blockPtr getEmptyBin ( int n ) { return mEmptyBins[n]; } 158 | unsigned char getBin ( unsigned int v ); 159 | unsigned int getBinWidth ( unsigned char b ); 160 | 161 | private: 162 | MBlock* mFullBins[BIN_CNT]; 163 | MBlock* mEmptyBins[BIN_CNT]; 164 | 165 | static const char logtable512[]; 166 | 167 | }; 168 | #else 169 | // Not building EventPool. Define empty class. 170 | #define MAX_POOL_SIZE 0 171 | class EventPool { 172 | }; 173 | #endif 174 | 175 | #endif // DEF_EVENT_SYSTEM_H 176 | -------------------------------------------------------------------------------- /libmin/include/file_tga.h: -------------------------------------------------------------------------------- 1 | //-------------------------------------------------------------------------------- 2 | // NVIDIA(R) GVDB VOXELS 3 | // Copyright 2017, NVIDIA Corporation. 4 | // 5 | // Redistribution and use in source and binary forms, with or without modification, 6 | // are permitted provided that the following conditions are met: 7 | // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 9 | // in the documentation and/or other materials provided with the distribution. 10 | // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived 11 | // from this software without specific prior written permission. 12 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 13 | // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 14 | // SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 15 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 16 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 17 | // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 18 | // 19 | // Version 1.0: Rama Hoetzlein, 5/1/2017 20 | //---------------------------------------------------------------------------------- 21 | 22 | #ifndef LOAD_TGA 23 | 24 | #include 25 | #include "common_defs.h" 26 | 27 | class HELPAPI TGA { 28 | public: 29 | enum TGAFormat 30 | { 31 | RGB = 0x1907, 32 | RGBA = 0x1908, 33 | ALPHA = 0x1906, 34 | UNKNOWN = -1 35 | }; 36 | 37 | enum TGAError 38 | { 39 | TGA_NO_ERROR = 1, // No error 40 | TGA_FILE_NOT_FOUND, // File was not found 41 | TGA_BAD_IMAGE_TYPE, // Color mapped image or image is not uncompressed 42 | TGA_BAD_DIMENSION, // Dimension is not a power of 2 43 | TGA_BAD_BITS, // Image bits is not 8, 24 or 32 44 | TGA_BAD_DATA // Image data could not be loaded 45 | }; 46 | 47 | TGA(void) : 48 | m_texFormat(TGA::UNKNOWN), 49 | m_nImageWidth(0), 50 | m_nImageHeight(0), 51 | m_nImageBits(0), 52 | m_nImageData(NULL) {} 53 | 54 | ~TGA(void); 55 | 56 | TGA::TGAError load( const char *name ); 57 | TGA::TGAError saveFromExternalData( const char *name, int w, int h, TGAFormat fmt, const unsigned char *externalImage ); 58 | 59 | TGAFormat m_texFormat; 60 | int m_nImageWidth; 61 | int m_nImageHeight; 62 | int m_nImageBits; 63 | unsigned char * m_nImageData; 64 | 65 | private: 66 | 67 | int returnError(FILE *s, int error); 68 | unsigned char *getRGBA(FILE *s, int size); 69 | unsigned char *getRGB(FILE *s, int size); 70 | unsigned char *getGray(FILE *s, int size); 71 | void writeRGBA(FILE *s, const unsigned char *externalImage, int size); 72 | void writeRGB(FILE *s, const unsigned char *externalImage, int size); 73 | void writeGrayAsRGB(FILE *s, const unsigned char *externalImage, int size); 74 | void writeGray(FILE *s, const unsigned char *externalImage, int size); 75 | }; 76 | 77 | #endif -------------------------------------------------------------------------------- /libmin/include/image_info.h: -------------------------------------------------------------------------------- 1 | // **** Image includes 2 | #ifndef DEF_IMAGE_INFO 3 | #define DEF_IMAGE_INFO 4 | 5 | #include "common_defs.h" 6 | #include 7 | #ifdef DEBUG_HEAP 8 | #ifndef _CRTDBG_MAP_ALLOC 9 | #define _CRTDBG_MAP_ALLOC 10 | #endif 11 | #include 12 | #include 13 | #else 14 | #include 15 | #endif 16 | 17 | #define IMGCUBE 0 18 | 19 | typedef unsigned char XBYTE; 20 | typedef unsigned char uchar; 21 | typedef unsigned short XBYTE2; 22 | //typedef unsigned short XBYTE4; 23 | 24 | class HELPAPI ImageOp { 25 | public: 26 | enum Format { 27 | FmtNone = 0, 28 | BW1 = 1, 29 | BW8, 30 | BW16, 31 | BW32, 32 | RGB8, 33 | RGB12, 34 | RGB16, 35 | RGB24, 36 | BGR24, 37 | RGBA16, 38 | RGBA24, 39 | RGBA32, 40 | BGR8, 41 | I420, 42 | IYUV, 43 | F8, 44 | F16, 45 | Custom 46 | }; 47 | enum Filter { 48 | NoFilter = 0, 49 | Linear = 1, 50 | MipNoFilter = 2, 51 | MipLinear = 3 52 | }; 53 | enum Usage { 54 | Light = 0, 55 | Medium = 1, 56 | Heavy = 2 57 | }; 58 | enum Flags { 59 | // Format flags 60 | Color = 1, // BW or color? 61 | Masked = 2, // 0-value is mask? 62 | Alpha = 4, // Alpha channel? 63 | AlphaMerged = 8, // Alpha channel is merged? 64 | Depth = 16, // Depth channel? 65 | DepthMerged = 32, // Depth channel is merged? 66 | Indexed = 64, // Indexed pixels? 67 | Lowclr = 128, // Low-color pixels (12-bit)? 68 | Highclr = 256, // High-color pixels (24-bit)? 69 | Trueclr = 512, // True-color pixels (32-bit)? 70 | // Extended usage flags 71 | Channels = 1024, // Other channels present? 72 | FilterLo = 2048, // Linear filter? 73 | FilterHi = 4096, // Mipmap filter? 74 | UseLo = 8192, // Usage method 75 | UseHi = 16384, 76 | Cube = 32768 // Cube map? 77 | }; 78 | enum ImageStatus { 79 | Ready = 0, 80 | NeedsUpdate = 1, 81 | NeedsRebuild = 2, 82 | NotReady = 3 83 | }; 84 | enum FormatStatus { 85 | Idle = 0, 86 | Loading = 1, 87 | Saving = 2, 88 | Successs = 3, // Yesss - the extra 's' is necesssary because gcc uses the 'Success' keyword. 89 | DepthNotSupported = 4, 90 | FeatureNotSupported = 5, 91 | InvalidFile = 6, 92 | LibVersion = 7, 93 | FileNotFound = 8, 94 | NotImplemented = 9, 95 | LoadOk = 10, 96 | LoadDone = 11, 97 | LoadNotReady = 12 98 | }; 99 | }; 100 | 101 | // Image Info Structure 102 | struct HELPAPI ImageInfo { 103 | 104 | ImageInfo ( int xr, int yr, ImageOp::Format ef ); 105 | ImageInfo (); 106 | void Reset (); 107 | 108 | ImageInfo& operator= ( const ImageInfo& op ) { 109 | eFormat = op.eFormat; 110 | eFlags = op.eFlags; 111 | mXres = op.mXres; 112 | mYres = op.mYres; 113 | mBitsPerPix = op.mBitsPerPix; 114 | mBytesPerRow = op.mBytesPerRow; 115 | return *this; 116 | } 117 | 118 | void SetFormat ( int x, int y, ImageOp::Format ef) { 119 | mXres = x; mYres = y; 120 | eFlags = GetFlags ( ef ); 121 | eFormat = ef; 122 | mBitsPerPix = GetBitsPerPix ( ef ); 123 | mBytesPerRow = GetBytesPerRow ( x, ef ); 124 | 125 | } 126 | void SetEqual ( ImageOp::Flags ef, unsigned int src_flags ) { SetFlag ( eFlags, ef, ( (src_flags & ((unsigned int) ef)) == (unsigned int) ef ) );} 127 | void SetFilter ( ImageOp::Filter ef ); 128 | void SetUsage ( ImageOp::Usage ef ); 129 | ImageOp::Filter GetFilter (); 130 | ImageOp::Usage GetUsage (); 131 | bool IsTransparent () { return HasFlag ( eFlags, ImageOp::Alpha); } 132 | bool IsCube () { return HasFlag ( eFlags, ImageOp::Cube ); } 133 | bool HasPowerSize (); 134 | 135 | unsigned int GetFlags () { return eFlags; } // member var 136 | unsigned int GetFlags (ImageOp::Format ef); // generic 137 | void SetFlag ( unsigned int& flags, ImageOp::Flags f, bool val) { if (val) flags |= (unsigned int) f; else flags &= ~(unsigned int) f; } 138 | bool HasFlag ( unsigned int& flags, ImageOp::Flags f ) { return (flags & (unsigned int) f)==0 ? false : true; } 139 | bool NoFilter() { return !HasFlag ( eFlags, ImageOp::FilterLo ); } 140 | bool MipFilter() { return HasFlag ( eFlags, ImageOp::FilterHi ); } 141 | 142 | //void SetBitsPerPixel (int x); 143 | inline unsigned char GetBitsPerPix () { return mBitsPerPix; } 144 | unsigned char GetBitsPerPix(ImageOp::Format ef); 145 | 146 | inline int GetBytesPerPix () { return mBitsPerPix >> 3; } 147 | inline int GetBytesPerPix (ImageOp::Format ef ) { return GetBitsPerPix(ef) >> 3; } 148 | 149 | unsigned char GetDataType (ImageOp::Format ef); 150 | 151 | inline unsigned long GetBytesPerRow () { return mBytesPerRow; } 152 | inline unsigned long GetBytesPerRow (int x, ImageOp::Format ef ) { return GetBitsPerPix(ef)*x >> 3; } 153 | 154 | inline unsigned long GetSize () { return (unsigned long) mBitsPerPix * (unsigned long) mXres * mYres >> 3; } 155 | inline unsigned long GetSize (int x, int y, ImageOp::Format ef ) { return GetBitsPerPix(ef) * (unsigned long) x * (unsigned long) y >> 3; } 156 | 157 | // Query functions 158 | int QuerySmallestPower (int x); 159 | void QueryPowerSize (int &x, int &y); 160 | bool QueryClip ( int& sx1, int& sy1, int& sx2, int& sy2, int cx1, int cy1, int cx2, int cy2 ); 161 | void QueryRect ( ImageOp::Format f, int src_x, XBYTE* src, int sx1, int sy1, int sx2, int sy2, XBYTE*& src_start, XBYTE*& src_end, int& src_wid, int& src_pitch ); 162 | bool QueryPaste ( ImageOp::Format src_f, int src_x, int src_y, XBYTE* src_dat, int sx1, int sy1, int sx2, int sy2, 163 | ImageOp::Format dest_f, int dest_x, int dest_y, XBYTE* dest_dat, int dx1, int dy1, 164 | XBYTE*& src_start, XBYTE*& src_end, int& src_wid, int& src_pitch, 165 | XBYTE*& dest_start, XBYTE*& dest_end, int& dest_wid, int& dest_pitch); 166 | 167 | // Data 168 | ImageOp::Format eFormat; // Image Format 169 | unsigned int eFlags; // Image Flags 170 | unsigned char mBitsPerPix; // BPP = Bits-per-Pixel 171 | unsigned long mBytesPerRow; // Bytes per Row = BPP*Xres >> 3 172 | // Bytes per Pixel = BPP >> 3 173 | // Size = mBPP*Xres*Yres >> 3 174 | int mXres, mYres; // Image Resolution 175 | }; 176 | 177 | // Image Format Params 178 | struct ImageFormatParams { 179 | // JPEG 180 | int iQuality; // 0 to 100 181 | float fScaling; // 1/2,1/4,1/8,1 182 | 183 | }; 184 | 185 | #endif 186 | 187 | -------------------------------------------------------------------------------- /libmin/include/imageformat.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef DEF_IMAGEFORMAT 3 | #define DEF_IMAGEFORMAT 4 | 5 | #include "image.h" 6 | #include "image_info.h" 7 | #include 8 | #include 9 | #include 10 | 11 | #define FILE_NAMELEN 512 12 | 13 | class HELPAPI CImageFormat { 14 | public: 15 | CImageFormat (); 16 | ~CImageFormat (); 17 | 18 | virtual bool Save (char *filename, Image* pImg) {return false;} 19 | virtual bool Load (char *filename, Image* pImg) {return false;} 20 | virtual bool LoadIncremental ( const char* filename, Image* pImg) { return false; } 21 | virtual ImageOp::FormatStatus LoadNextRow () { return ImageOp::LoadNotReady; } 22 | 23 | #if defined(BUILD_VC) 24 | bool TransferBitmap ( HBITMAP hBmp ); 25 | #endif 26 | // Helper functions 27 | int GetWidth ( Image* img ) { return img->GetWidth(); } 28 | int GetHeight ( Image* img ) { return img->GetHeight(); } 29 | ImageOp::Format GetFormat ( Image* img ) { return img->GetFormat(); } 30 | int GetBitsPerPixel ( Image* img ) { return img->GetBitsPerPixel ();} 31 | int GetBytesPerPixel ( Image* img ) { return img->GetBytesPerPixel ();} 32 | int GetBytesPerRow ( Image* img ) { return img->GetBytesPerRow ();} 33 | uchar* GetData ( Image* img ) { return img->GetData (); } 34 | void CreateImage ( Image*& img, int xr, int yr, ImageOp::Format fm ); 35 | 36 | ImageOp::FormatStatus GetStatus () { return m_eStatus; } 37 | void GetStatusMsg (char* buf); 38 | 39 | void StartLoad ( char* filename, Image* pImg ); 40 | void FinishLoad (); 41 | 42 | protected: 43 | Image* m_pOrigImage; // Original image. (ImageFormat does not own it) 44 | Image* m_pNewImage; 45 | char m_Filename[FILE_NAMELEN]; 46 | ImageOp::FormatStatus m_eStatus; 47 | 48 | // General format data 49 | int m_Xres, m_Yres; 50 | int m_BytesPerRow; 51 | int m_BitsPerPixel; 52 | }; 53 | 54 | 55 | #endif 56 | 57 | 58 | -------------------------------------------------------------------------------- /libmin/include/imageformat_bmp.h: -------------------------------------------------------------------------------- 1 | 2 | #ifdef BUILD_BMP 3 | 4 | #ifndef DEF_IMAGEFORMAT_BMP 5 | #define DEF_IMAGEFORMAT_BMP 6 | 7 | #include "imageformat.h" 8 | 9 | class HELPAPI CImageFormatBmp : public CImageFormat { 10 | public: 11 | virtual bool Load (char *filename, Image* pImg ); 12 | virtual bool Save (char *filename, Image* pImg ); 13 | 14 | int readPaletteBMP ( FILE* fp, RGBQUAD*& palette, int bit_count ); 15 | 16 | private: 17 | bool LoadBmp (char *filename); 18 | bool SaveBmp (char *filename); 19 | }; 20 | 21 | #endif 22 | 23 | #endif 24 | 25 | 26 | -------------------------------------------------------------------------------- /libmin/include/imageformat_generic.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef DEF_IMAGEFORMAT_GENERIC 3 | #define DEF_IMAGEFORMAT_GENERIC 4 | 5 | #pragma comment(lib, "ole32.lib") 6 | 7 | #include "imageformat.h" 8 | 9 | //--------- MUST BE UPDATED 10 | /* class CImageFormatGeneric : public CImageFormat { 11 | public: 12 | CImageFormatGeneric (); 13 | 14 | virtual bool Load (char *filename, const Image* pOrigImg, Image*& pNewImg); 15 | virtual bool Save (char *filename, const Image* pOrigImg); 16 | private: 17 | bool LoadGeneric (char *filename); 18 | bool SaveGeneric (char *filename); 19 | 20 | }; 21 | */ 22 | 23 | #endif 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /libmin/include/imageformat_jpg.h: -------------------------------------------------------------------------------- 1 | 2 | #ifdef BUILD_JPG 3 | 4 | #ifndef DEF_IMAGEFORMAT_JPG 5 | #define DEF_IMAGEFORMAT_JPG 6 | 7 | #include "imageformat.h" 8 | 9 | #include "libjpg/jpeglib.h" 10 | #include "libjpg/jerror.h" 11 | #include 12 | 13 | struct extended_error_mgr { 14 | struct jpeg_error_mgr pub; 15 | jmp_buf setjmp_buffer; 16 | }; 17 | 18 | typedef struct extended_error_mgr * extended_error_ptr; 19 | 20 | class HELPAPI CImageFormatJpg : public CImageFormat { 21 | public: 22 | virtual bool Load (char *filename, Image* pImg ); 23 | virtual bool Save (char *filename, Image* pImg ); 24 | virtual bool LoadIncremental ( char* filename, Image* pImg ); 25 | virtual ImageOp::FormatStatus LoadNextRow (); 26 | 27 | private: 28 | bool LoadJpg (char *filename, bool bIncremental); 29 | bool SaveJpg (char *filename, int iQuality); 30 | 31 | jpeg_compress_struct m_jpeg_cinfo; 32 | jpeg_decompress_struct m_jpeg_dinfo; 33 | extended_error_mgr m_jerr; 34 | FILE* m_jpeg_file; 35 | XBYTE* m_pDest; 36 | }; 37 | 38 | #endif 39 | 40 | #endif -------------------------------------------------------------------------------- /libmin/include/imageformat_png.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef DEF_IMAGEFORMAT_PNG 4 | #define DEF_IMAGEFORMAT_PNG 5 | 6 | #include "imageformat.h" 7 | 8 | #define PNG_SKIP_SETJMP_CHECK 9 | #include "file_png.h" // libpng. To install: apt-get install libpng12-dev 10 | 11 | class HELPAPI CImageFormatPng : public CImageFormat { 12 | public: 13 | virtual bool Load (char *filename, Image* pImg ); 14 | virtual bool Save (char *filename, Image* pImg ); 15 | 16 | private: 17 | bool LoadPng (char *filename ); 18 | bool SavePng (char *filename ); 19 | 20 | FILE* m_png_file; 21 | }; 22 | 23 | #endif 24 | 25 | 26 | -------------------------------------------------------------------------------- /libmin/include/imageformat_tga.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef DEF_IMAGEFORMAT_TGA 3 | #define DEF_IMAGEFORMAT_TGA 4 | 5 | #include "imageformat.h" 6 | 7 | class HELPAPI CImageFormatTga : public CImageFormat { 8 | public: 9 | virtual bool Load (char *filename, Image* pImg ); 10 | virtual bool Save (char *filename, Image* pImg ); 11 | 12 | private: 13 | unsigned char *getRGBA(FILE *s, unsigned char* rgba, int size); 14 | unsigned char *getRGB(FILE *s, unsigned char* rgb, int size); 15 | unsigned char *getGray(FILE *s, unsigned char* grayData, int size); 16 | void writeRGBA(FILE *s, const unsigned char *externalImage, int size); 17 | void writeRGB(FILE *s, const unsigned char *externalImage, int size); 18 | void writeGrayAsRGB(FILE *s, const unsigned char *externalImage, int size); 19 | void writeGray(FILE *s, const unsigned char *externalImage, int size); 20 | 21 | bool LoadTga (char *filename); 22 | //bool SaveTga (char *filename); 23 | }; 24 | 25 | #endif 26 | 27 | 28 | -------------------------------------------------------------------------------- /libmin/include/imageformat_tiff.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef DEF_IMAGEFORMAT_TIFF 4 | #define DEF_IMAGEFORMAT_TIFF 5 | 6 | #include "imageformat.h" 7 | #include "event.h" 8 | 9 | const int TIFF_BUFFER = (32767); 10 | const int TIFF_BYTEORDER = 0x4949; 11 | const int TIFF_MAGIC = 42; 12 | 13 | // Tiff Save settings 14 | const int TIFF_SAVE_ENTRIES = 10; 15 | const int TIFF_SAVE_SIZEHEAD = 8; // 8 bytes 16 | const int TIFF_SAVE_SIZEIFD = (2 + (TIFF_SAVE_ENTRIES*12) + 4); 17 | const int TIFF_SAVE_SIZEBPC = (2*3); // 3 ints 18 | 19 | const int TIFF_SAVE_POSHEAD = 0; 20 | const int TIFF_SAVE_POSIFD = (TIFF_SAVE_POSHEAD + TIFF_SAVE_SIZEHEAD); 21 | const int TIFF_SAVE_POSBPC = (TIFF_SAVE_POSIFD + TIFF_SAVE_SIZEIFD); 22 | const int TIFF_SAVE_POSOFFSETS = (TIFF_SAVE_POSBPC + TIFF_SAVE_SIZEBPC); 23 | 24 | 25 | class HELPAPI CImageFormatTiff : public CImageFormat { 26 | public: 27 | enum TiffStatus { 28 | TifOk = 0, 29 | TifNoMagic = 1, 30 | TifLzwNotSupported = 2, 31 | TifNonRgbNotSupported = 3, 32 | TifUnknownTiffMode = 4 33 | }; 34 | enum TiffTag { 35 | TifNewSubType = 254, 36 | TifSubType = 255, 37 | TifImageWidth = 256, 38 | TifImageHeight = 257, 39 | TifBitsPerSample = 258, 40 | TifCompression = 259, 41 | TifPhotometric = 262, 42 | TifThresholding = 263, 43 | TifDocname = 269, 44 | TifStripOffsets = 273, 45 | TifOrientation = 274, 46 | TifSamplesPerPixel = 277, 47 | TifRowsPerStrip = 278, 48 | TifStripByteCounts = 279, 49 | TifXres = 282, 50 | TifYres = 283, 51 | TifPlanarConfiguration = 284, 52 | TifResUnit = 296, 53 | TifColorMap = 320, 54 | TifExtraSamples = 338 55 | }; 56 | enum TiffCompression { 57 | TifCompNone = 1, 58 | TifCompLzw = 5, 59 | TifCompPackbits = 32773 60 | }; 61 | enum TiffPhotometric { 62 | TifWhiteZero = 0, 63 | TifBlackZero = 1, 64 | TifRgb = 2, 65 | TifPalette = 3, 66 | TifTrans = 4, 67 | TifCmyk = 5 68 | }; 69 | enum TiffType { 70 | TifByte = 1, 71 | TifAscii = 2, 72 | TifShort = 3, 73 | TifLong = 4, 74 | TifRational = 5 75 | }; 76 | enum TiffMode { 77 | TifBw = 0, 78 | TifGrayscale = 1, 79 | TifColor = 2, 80 | TifIndex = 3 81 | }; 82 | enum TiffChannel { 83 | TifGray = 0, 84 | TifRed = 1, 85 | TifGreen = 2, 86 | TifBlue = 3, 87 | TifAlpha = 4 88 | }; 89 | 90 | CImageFormatTiff (); 91 | 92 | virtual bool Load (char *filename, Image* pImg ); 93 | virtual bool Save (char *filename, Image* pImg ); 94 | void DebugTif ( bool v ) { m_DebugTif = v; } 95 | 96 | private: 97 | bool LoadTiff (char *filename); 98 | bool LoadTiffDirectory (); 99 | bool LoadTiffEntry (); 100 | void LoadTiffStrips (); 101 | void LoadTiffData (unsigned long count, unsigned long offset, int row); 102 | 103 | bool SaveTiff (char *filename); 104 | bool SaveTiffDirectory (); 105 | bool SaveTiffExtras (enum TiffTag eTag); 106 | bool SaveTiffEntry ( enum TiffTag eTag); 107 | bool SaveTiffData (); 108 | 109 | void ComputeMinMaxData (unsigned long count, unsigned long offset, int row); 110 | void ComputeMinMax (); 111 | 112 | FILE* m_Tif; 113 | Event m_Buf; 114 | 115 | TiffStatus m_eTiffStatus; 116 | TiffTag m_eTag; 117 | TiffCompression m_eCompression; 118 | TiffPhotometric m_ePhoto; 119 | TiffType m_eType; 120 | TiffMode m_eMode; 121 | 122 | bool m_bHasAlpha; 123 | int m_NumChannels; 124 | unsigned long m_BitsPerChannelOffset; 125 | int m_BitsPerChannel[5]; 126 | unsigned long m_NumStrips; 127 | unsigned long m_StripOffsets; 128 | unsigned long m_StripCounts; 129 | int m_RowsPerStrip; 130 | int m_PlanarConfig; 131 | bool m_DebugTif; 132 | 133 | XBYTE4 m_Min, m_Max; 134 | }; 135 | 136 | #endif 137 | 138 | 139 | 140 | -------------------------------------------------------------------------------- /libmin/include/main_includes.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef MAIN_INCLUDES 3 | #define MAIN_INCLUDES 4 | 5 | enum AppEnum { 6 | BUTTON_NONE = 0, // mouse states 7 | BUTTON_PRESS = 1, 8 | BUTTON_RELEASE = 2, 9 | BUTTON_REPEAT = 3, 10 | 11 | BUTTON_LEFT = 4, // mouse buttons 12 | BUTTON_RIGHT = 5, 13 | BUTTON_MIDDLE = 6, 14 | 15 | ACTION_DOWN = 0, // mobile actions 16 | ACTION_MOVE = 1, 17 | ACTION_UP = 2, 18 | ACTION_CANCEL = 3, 19 | GESTURE_SINGLE_TAP = 4, 20 | GESTURE_DOUBLE_TAP = 5, 21 | GESTURE_SCALE_BEGIN = 6, 22 | GESTURE_SCALE = 7, 23 | GESTURE_SCALE_END = 8, 24 | ACTION_GLIDE = 9, 25 | SOFT_KEY_PRESS = 10, 26 | 27 | EVT_XTARGET = 0, 28 | EVT_YTARGET = 1, 29 | EVT_XFOCUS = 2, 30 | EVT_YFOCUS = 3, 31 | EVT_XSPAN = 4, 32 | EVT_YSPAN = 5, 33 | 34 | UNDEF = 255 35 | }; 36 | struct guiEvent { 37 | int typeOrdinal; 38 | float xtarget; 39 | float ytarget; 40 | float xfocus; // the center of a pinch gesture 41 | float yfocus; 42 | float xspan; // the width of a pinch gesture 43 | float yspan; 44 | }; 45 | 46 | // non-character keys (non printing) 47 | #define KEY_UNKNOWN -1 48 | #define KEY_BACKSPACE 8 // keys with ascii equivalents 49 | #define KEY_TAB 9 50 | #define KEY_ENTER 13 51 | #define KEY_ESCAPE 27 52 | #define KEY_DELETE 127 53 | 54 | #define KEY_WORLD_1 161 // virtual keys (non-ascii) 55 | #define KEY_WORLD_2 162 56 | #define KEY_INSERT 260 57 | #define KEY_RIGHT 262 58 | #define KEY_LEFT 263 59 | #define KEY_DOWN 264 60 | #define KEY_UP 265 61 | #define KEY_PAGE_UP 266 62 | #define KEY_PAGE_DOWN 267 63 | #define KEY_HOME 268 64 | #define KEY_END 269 65 | #define KEY_CAPS_LOCK 280 66 | #define KEY_SCROLL_LOCK 281 67 | #define KEY_NUM_LOCK 282 68 | #define KEY_PRINT_SCREEN 283 69 | #define KEY_PAUSE 284 70 | #define KEY_F1 290 71 | #define KEY_F2 291 72 | #define KEY_F3 292 73 | #define KEY_F4 293 74 | #define KEY_F5 294 75 | #define KEY_F6 295 76 | #define KEY_F7 296 77 | #define KEY_F8 297 78 | #define KEY_F9 298 79 | #define KEY_F10 299 80 | #define KEY_F11 300 81 | #define KEY_F12 301 82 | #define KEY_F13 302 83 | #define KEY_F14 303 84 | #define KEY_F15 304 85 | #define KEY_F16 305 86 | #define KEY_F17 306 87 | #define KEY_F18 307 88 | #define KEY_F19 308 89 | #define KEY_F20 309 90 | #define KEY_KP_0 320 91 | #define KEY_KP_1 321 92 | #define KEY_KP_2 322 93 | #define KEY_KP_3 323 94 | #define KEY_KP_4 324 95 | #define KEY_KP_5 325 96 | #define KEY_KP_6 326 97 | #define KEY_KP_7 327 98 | #define KEY_KP_8 328 99 | #define KEY_KP_9 329 100 | #define KEY_KP_DECIMAL 330 101 | #define KEY_KP_DIVIDE 331 102 | #define KEY_KP_MULTIPLY 332 103 | #define KEY_KP_SUBTRACT 333 104 | #define KEY_KP_ADD 334 105 | #define KEY_KP_ENTER 335 106 | #define KEY_KP_EQUAL 336 107 | #define KEY_LEFT_SHIFT 340 108 | #define KEY_LEFT_CONTROL 341 109 | #define KEY_LEFT_ALT 342 110 | #define KEY_LEFT_SUPER 343 111 | #define KEY_RIGHT_SHIFT 344 112 | #define KEY_RIGHT_CONTROL 345 113 | #define KEY_RIGHT_ALT 346 114 | #define KEY_RIGHT_SUPER 347 115 | #define KEY_MENU 348 116 | #define KMOD_SHIFT 0x0001 117 | #define KMOD_CONTROL 0x0002 118 | #define KMOD_ALT 0x0004 119 | #define KMOD_SUPER 0x0008 120 | 121 | #endif -------------------------------------------------------------------------------- /libmin/include/quaternion.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt 2 | // This file is part of the "Irrlicht Engine". 3 | // For conditions of distribution and use, see copyright notice in irrlicht.h 4 | 5 | /* 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any damages 8 | arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any purpose, 11 | including commercial applications, and to alter it and redistribute it 12 | freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you must not 15 | claim that you wrote the original software. If you use this software 16 | in a product, an acknowledgment in the product documentation would be 17 | appreciated but is not required. 18 | 2. Altered source versions must be plainly marked as DYNAMICsuch, and must not be 19 | misrepresented as being the original software. 20 | 3. This notice may not be removed or altered from any source distribution. 21 | 22 | Please note that the Irrlicht Engine is based in part on the work of the 23 | Independent JPEG Group, the zlib and the libPng. This means that if you use 24 | the Irrlicht Engine in your product, you must acknowledge somewhere in your 25 | documentation that you've used the IJG code. It would also be nice to mention 26 | that you use the Irrlicht Engine, the zlib and libPng. See the README files 27 | in the jpeglib, the zlib and libPng for further informations. 28 | */ 29 | 30 | #ifndef QUATERNION_H 31 | #define QUATERNION_H 32 | 33 | #include "common_defs.h" 34 | #include "vec.h" 35 | //#include "matrix.h" 36 | 37 | #define EPS 0.00001 38 | 39 | // Quaternion 40 | // - concisely express an orientation 41 | // - or a rotation transformation 42 | class HELPAPI Quaternion { 43 | public: 44 | //! Default Constructor 45 | Quaternion() : X(0.0f), Y(0.0f), Z(0.0f), W(1.0f) {} 46 | 47 | //! Constructor 48 | inline Quaternion(f32 x, f32 y, f32 z, f32 w) { set(x, y, z, w); } 49 | 50 | //! Constructor which converts euler angles (radians) to a Quaternion 51 | inline Quaternion(f32 x, f32 y, f32 z) { set(x, y, z); } 52 | inline void fromEuler (Vector3DF angs) { set(angs.x, angs.y, angs.z); } 53 | 54 | //! Constructor which converts euler angles (radians) to a Quaternion 55 | inline Quaternion(const Vector3DF& angs) { set(angs.x, angs.y, angs.z); } 56 | inline Quaternion(const Vector3DF& vec, float w ) { set(vec.x, vec.y, vec.z, w); } 57 | 58 | //! Constructor which converts a matrix to a Quaternion 59 | inline Quaternion(const Matrix4F& mat) { (*this) = mat; } 60 | 61 | //! Equalilty operator 62 | inline bool operator==(const Quaternion& other) const { return ((X == other.X) && (Y == other.Y) && (Z == other.Z) && (W == other.W)); } 63 | bool equals(const Quaternion& other, const f32 tolerance = ROUNDING_ERROR_f32) const; 64 | 65 | //! inequality operator 66 | inline bool operator!=(const Quaternion& other) const { return !(*this == other); } 67 | 68 | //! Assignment operator 69 | inline Quaternion& operator=(const Quaternion& other) { X = other.X; Y = other.Y; Z = other.Z; W = other.W; return *this; } 70 | 71 | // Mathematical operators 72 | Quaternion& operator=(const Matrix4F& other); // Matrix assignment operator 73 | Quaternion operator+(const Quaternion& other) const; // Add operator 74 | Quaternion operator*(const Quaternion& other) const; // Multiplication operator 75 | Quaternion& operator*=(const Quaternion& other); // Multiplication operator 76 | Quaternion operator*(f32 s) const; // Multiplication operator with scalar 77 | Quaternion& operator*=(f32 s); // Multiplication operator with scalar 78 | Vector3DF operator*(const Vector3DF& v) const; // Multiplication with vector, giving rotated vector 79 | f32 dotProduct(const Quaternion& other) const; // Dot product of two quaternions 80 | 81 | // Set operators 82 | inline Quaternion& set(f32 x, f32 y, f32 z, f32 w) { X = x; Y = y; Z = z; W = w; return *this; } 83 | inline Quaternion& set(const Quaternion& quat) { return (*this = quat); } 84 | Quaternion& set(f32 x, f32 y, f32 z); // Sets new Quaternion based on euler angles (radians) 85 | Quaternion& set(const Vector3DF& vec); // Sets new Quaternion based on euler angles (radians) 86 | inline Quaternion& Identity() { W = 1.f; X = 0.f; Y = 0.f; Z = 0.f; return *this; } // Set identity 87 | 88 | // Fix Quaternion within rounding tolerance 89 | void Quaternion::fixround(); 90 | 91 | //! Normalizes the Quaternion 92 | inline Quaternion& normalize(); 93 | 94 | // Get operators 95 | Matrix4F getMatrix() const; // Creates a matrix from this Quaternion 96 | void getMatrix( Matrix4F &dest, const Vector3DF &translation=Vector3DF() ) const; // Creates a matrix from this Quaternion 97 | void getMatrixCenter( Matrix4F &dest, const Vector3DF ¢er, const Vector3DF &translation ) const; 98 | void getMatrix_transposed( Matrix4F &dest ) const; // Creates a matrix from this Quaternion 99 | 100 | // Inverse operators 101 | inline Quaternion& makeInverse() { X = -X; Y = -Y; Z = -Z; return *this; } // invert this Quaternion 102 | inline Quaternion operator~() { return Quaternion(-X, -Y, -Z, W); } // return inverse, W unchanged 103 | inline Quaternion inverse () { return Quaternion(-X, -Y, -Z, W); } // return inverse, W unchanged 104 | Quaternion conjInverse (); // return conjugate inverse 105 | Quaternion negative() { return Quaternion(X, Y, Z, -W); } // return negative quaternion (-W) 106 | Quaternion& dual(Vector3DF pos); // return dual quaternion 107 | 108 | // Construct quaternion from orthonormal basis 109 | Quaternion& toBasis (Vector3DF c1, Vector3DF c2, Vector3DF c3 ); 110 | 111 | // Linear interpolation - Set this Quaternion to the linear interpolation 112 | // q1 = First Quaternion, q2 = Second quaternion, time = parametric value from 0 to 1 113 | Quaternion& lerp(Quaternion q1, Quaternion q2, f32 time); 114 | 115 | // Spherical interpolation - Set this Quaternion to the result of the spherical interpolation 116 | // q1 = First Quaternion, q2 = Second quaternion, time = parametric value from 0 to 1 117 | // threshold = avoid inaccuracies at the end (time=1) the interpolation switches to linear interpolation 118 | // at some point. This value defines how much of the remaining interpolation will be calculated with lerp. 119 | // Everything from 1-threshold up will be linear interpolation. 120 | Quaternion& slerp(Quaternion q1, Quaternion q2, f32 time, f32 threshold=.05f); 121 | 122 | // Create Quaternion from rotation angle and rotation axis. 123 | // Axis must be unit length. The Quaternion representing the rotation is 124 | // q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k), where angle = Rotation Angle in radians, axis = Rotation axis 125 | Quaternion& fromAngleAxis (f32 angle, const Vector3DF& axis); 126 | Quaternion& changeAngle (f32 angle); 127 | 128 | // Fills an angle (radians) around an axis (unit vector) 129 | void toAngleAxis (f32 &angle, Vector3DF& axis) const; 130 | inline f64 getAngle () { return (W>1.0) ? 0 : 2.0f * acosf(W) * RADtoDEG; } 131 | 132 | // Output this Quaternion to an euler angle (radians) 133 | void toEuler(Vector3DF& euler) const; 134 | 135 | // Rotate a vector by this Quaternion 136 | Vector3DF rotateVec(Vector3DF v); 137 | 138 | // Set Quaternion to represent a rotation from one vector to another. 139 | void rotationFromTo(Vector3DF from, Vector3DF to); 140 | 141 | // Quaternion elements. 142 | f32 X; // vectorial (imaginary) part 143 | f32 Y; 144 | f32 Z; 145 | f32 W; // real part 146 | }; 147 | 148 | // Dual Quaternion 149 | // - whereas quaternions express orientation/rotation 150 | // - dual quats can express position and orientation, the.. 151 | // - complete affine transform of an object, but easily separated into pos & rotation 152 | class Dualquat { 153 | public: 154 | inline Dualquat() { m_real.set(0, 0, 0, 1); m_dual.set(0, 0, 0, 0); } 155 | inline Dualquat(Quaternion real, Quaternion dual) { // set to another Dualquat 156 | m_real = real; 157 | m_dual = dual; 158 | } 159 | inline void zero() { m_real.set(0, 0, 0, 0); m_dual.set(0, 0, 0, 0); } 160 | inline void identity() { m_real.set(0, 0, 0, 1); m_dual.set(0, 0, 0, 0); } 161 | 162 | Dualquat(Vector3DF pos) { // set from position 163 | m_real.set(0, 0, 0, 1); 164 | m_dual = m_real.dual(pos); // already normalized 165 | } 166 | Dualquat(Quaternion rot, Vector3DF pos) { // set from a rotation and position 167 | m_real = rot.normalize(); 168 | m_dual = m_real.dual(pos); // already normalized 169 | } 170 | Dualquat operator* (float f) { 171 | return Dualquat(m_real * f, m_dual * f); 172 | } 173 | Dualquat operator* (Dualquat& op) { 174 | return Dualquat(op.m_real * m_real, op.m_dual * m_real + op.m_real * m_dual); 175 | } 176 | Dualquat operator+ (Dualquat& op) { 177 | return Dualquat(m_real + op.m_real, m_dual + op.m_dual); 178 | } 179 | Dualquat inverse() { // invert the dualquat 180 | return Dualquat(m_real.inverse(), m_dual.inverse()); 181 | } 182 | Dualquat& normalize() { 183 | float mag = m_real.dotProduct(m_real); 184 | if (mag < 0.0000001f) return *this; 185 | m_real *= 1.0f / mag; 186 | m_dual *= 1.0f / mag; 187 | return *this; 188 | } 189 | // return the orientation (as a quaternion) 190 | Quaternion getRotate() { return m_real; } 191 | 192 | // return the translation (as a vector) 193 | Vector3DF getTranslate() { Quaternion t = (m_dual * 2.0f) * m_real.inverse(); return Vector3DF(t.X, t.Y, t.Z); } 194 | 195 | // Dualquat elements. 196 | Quaternion m_real; 197 | Quaternion m_dual; 198 | }; 199 | 200 | #endif -------------------------------------------------------------------------------- /libmin/include/string_helper.h: -------------------------------------------------------------------------------- 1 | //-------------------------------------------------------------------------------- 2 | // NVIDIA(R) GVDB VOXELS 3 | // Copyright 2017, NVIDIA Corporation 4 | // 5 | // Redistribution and use in source and binary forms, with or without modification, 6 | // are permitted provided that the following conditions are met: 7 | // 1. Redistributions of source code must retain the above copyright notice, this 8 | // list of conditions and the following disclaimer. 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, this 10 | // list of conditions and the following disclaimer in the documentation and/or 11 | // other materials provided with the distribution. 12 | // 3. Neither the name of the copyright holder nor the names of its contributors may 13 | // be used to endorse or promote products derived from this software without specific 14 | // prior written permission. 15 | // 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 19 | // SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 23 | // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 24 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | // 26 | // Version 1.0: Rama Hoetzlein, 5/1/2017 27 | //---------------------------------------------------------------------------------- 28 | 29 | #ifndef DEF_STRING_HELPER 30 | #define DEF_STRING_HELPER 31 | 32 | #include "common_defs.h" 33 | #include "vec.h" 34 | #include 35 | #include 36 | 37 | #ifndef DEF_OBJTYPE 38 | typedef uint32_t objType; 39 | #endif 40 | HELPAPI std::string strFilebase ( std::string str ); // basename of a file (minus ext) 41 | HELPAPI std::string strFilepath ( std::string str ); // path of a file 42 | 43 | // convert 44 | HELPAPI bool isFloat (std::string s); // fast 45 | HELPAPI int strToI (std::string s); 46 | HELPAPI float strToF (std::string s); 47 | HELPAPI double strToD (std::string s); 48 | HELPAPI float strToDateF( std::string s, int mp=0, int mc=2, int dp=3, int dc=2, int yp=6, int yc=4 ); 49 | HELPAPI void strFromDateF ( float f, int& m, int& d, int& y ); 50 | HELPAPI unsigned char strToC ( std::string s ); 51 | HELPAPI unsigned long strToUL ( std::string s ); 52 | HELPAPI unsigned long strToID ( std::string str ); // should only be used for 4-byte literals. for actual unsigned long see strToUL 53 | 54 | HELPAPI std::string cToStr ( char c ); 55 | HELPAPI std::string iToStr ( int i ); 56 | HELPAPI std::string fToStr ( float f ); 57 | HELPAPI std::string xlToStr ( uint64_t v ); 58 | HELPAPI objType strToType ( std::string str ); 59 | HELPAPI std::string typeToStr ( objType t ); 60 | HELPAPI std::string wsToStr ( const std::wstring& str ); 61 | HELPAPI std::wstring strToWs (const std::string& s); 62 | 63 | HELPAPI bool strToVec(std::string& str, std::string lsep, std::string insep, std::string rsep, float* vec, int cpt = 3); 64 | HELPAPI bool strToVec3(std::string& str, std::string lsep, std::string insep, std::string rsep, float* vec); 65 | HELPAPI bool strToVec4(std::string& str, std::string lsep, std::string insep, std::string rsep, float* vec); 66 | HELPAPI Vector3DF strToVec3(std::string str, std::string sep); 67 | HELPAPI Vector4DF strToVec4(std::string str, std::string sep); 68 | 69 | //----------- Boolean returns 70 | 71 | HELPAPI bool strSplit ( std::string str, std::string sep, std::string& left, std::string& right ); // "left,right" --> left="left", right="right", str=unchanged 72 | HELPAPI bool strSplitLeft(std::string str, std::string sep, std::string& key, std::string& val); // "left,right" --> key="left", val="right", str=unchanged 73 | HELPAPI bool strParseOut(std::string str, std::string lsep, std::string rsep, std::string& result, std::string& rest); // "data<1492> | time", <> --> result 1492, str="date | time" 74 | HELPAPI bool strParseKeyVal(std::string& str, std::string lsep, std::string rsep, std::string& key, std::string& val); // "obj,more" --> key="obj", val="car", str="more" 75 | HELPAPI bool strFileSplit(std::string str, std::string& path, std::string& name, std::string& ext); 76 | HELPAPI int strSplitMultiple(std::string str, std::string sep, std::vector& list); // obj1,obj2,obj3.. list={ob1, obj2, obj3} 77 | 78 | //----------- String returns 79 | 80 | HELPAPI std::string strSplitLeft ( std::string& str, std::string sep ); // "left,right" --> return "left", str="right" 81 | HELPAPI std::string strSplitRight ( std::string& str, std::string sep ); // "left,right" --> return "right", str="left" 82 | HELPAPI std::string strParseOut(std::string& str, std::string lsep, std::string rsep = " "); // "data<1492> | time", <> --> return 1492, str="date | time" 83 | 84 | 85 | 86 | 87 | //----------- original api 88 | HELPAPI std::string strParse ( std::string str, std::string lstr, std::string rstr, std::string lsep, std::string rsep ); 89 | HELPAPI std::string strParseArg ( std::string& tag, std::string valsep, std::string sep, std::string& str ); 90 | HELPAPI std::string strParseFirst ( std::string& str, std::string sep, std::string others, char& ch ); 91 | 92 | HELPAPI bool strGet ( std::string str, std::string& result, std::string lsep, std::string rsep ); 93 | HELPAPI bool strGet ( const std::string& s, std::string lsep, std::string rsep, std::string& result, size_t& pos ); 94 | 95 | HELPAPI bool strSub ( std::string str, int first, int cnt, std::string cmp ); 96 | HELPAPI std::string strReplace ( std::string str, std::string src, std::string dest ); 97 | HELPAPI bool strReplace ( std::string& str, std::string src, std::string dest, int& cnt ); 98 | HELPAPI int strExtract ( std::string& str, std::vector& list ); 99 | HELPAPI int strFindFromList ( std::string str, std::vector& list, int& pos ); 100 | HELPAPI bool strEmpty ( const std::string& s); 101 | 102 | //---------- Trimming 103 | HELPAPI std::string strLTrim ( std::string str ); 104 | HELPAPI std::string strRTrim ( std::string str ); 105 | HELPAPI std::string strTrim ( std::string str ); 106 | HELPAPI std::string strTrim ( std::string str, std::string ch ); 107 | HELPAPI std::string strLeft ( std::string str, int n ); 108 | HELPAPI std::string strRight ( std::string str, int n ); 109 | HELPAPI std::string strLeftOf ( std::string str, std::string sep ); 110 | HELPAPI std::string strMidOf ( std::string str, std::string sep ); 111 | HELPAPI std::string strRightOf ( std::string str, std::string sep ); 112 | 113 | // alphanumeric 114 | HELPAPI bool strIsNum ( std::string str, float& f ); 115 | HELPAPI float strToNum ( std::string str ); 116 | HELPAPI int strCount ( std::string& str, char ch ); 117 | 118 | HELPAPI bool readword ( char *line, char delim, char *word, int max_size ); 119 | HELPAPI std::string readword ( char *line, char delim ); 120 | 121 | #endif -------------------------------------------------------------------------------- /libmin/include/widget.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef DEF_WIDGET 4 | #define DEF_WIDGET 5 | 6 | #include "nv_gui.h" 7 | #include "event_system.h" 8 | #include "vec.h" 9 | #include "quaternion.h" 10 | #include "main_includes.h" 11 | 12 | #define BTN_OFF 0 13 | #define BTN_ON 1 14 | 15 | #define ACT_OFF 0 16 | #define ACT_X 1 17 | #define ACT_Y 2 18 | #define ACT_Z 3 19 | #define ACT_ROTX 4 20 | #define ACT_ROTY 5 21 | #define ACT_ROTZ 6 22 | #define ACT_SCALE 7 23 | 24 | #define OP_EMPTY 0 25 | #define OP_ON 1 26 | #define OP_ENABLED 2 27 | #define OP_VISIBLE 4 28 | #define OP_INVISIBLE 8 29 | #define OP_FIT_W 16 30 | #define OP_FIT_H 32 31 | #define OP_TEXT 64 32 | #define OP_TEXT_ENTRY 128 33 | #define OP_CLICK 256 34 | #define OP_BUTTON 256 35 | #define OP_TOGGLE 512 36 | #define OP_SLIDER 1024 37 | #define OP_BOX 2048 // 3D options 38 | #define OP_MOVE 4096 39 | #define OP_SCALE 8192 40 | #define OP_ROTATE 16384 41 | 42 | struct HELPAPI Widget { 43 | Vector4DF rpos; // relative position 44 | Vector4DF pos; // abs. screen coordinates 45 | ushort opts; 46 | Vector4DF foreClr; // foreground (text) 47 | Vector4DF backClr; // background 48 | std::string text; 49 | std::string textEntry; 50 | nvImg* img; // icon 51 | ushort buttonType; // button type 52 | ushort buttonState; // button state 53 | ushort triggered; 54 | ushort parent; 55 | ushort child, next; 56 | float* val; 57 | Vector3DF vrange; 58 | }; 59 | 60 | struct HELPAPI Widget3D { 61 | void UpdateXform() { 62 | xform.Identity(); 63 | xform.Translate ( pos ); 64 | xform.Rotate ( rot.getMatrix() ); 65 | xform.Scale ( scale ); 66 | xform.Translate ( pivot ); 67 | } 68 | ushort opts; 69 | Vector3DF rpos; // relative position [0,1] 70 | Vector3DF pos; // absolute position [Xres,Yres] 71 | Quaternion rot; 72 | Vector3DF scale; 73 | Vector3DF pivot; 74 | Vector3DI sel; 75 | Matrix4F xform; 76 | Vector4DF clr; 77 | std::string text; 78 | }; 79 | 80 | class HELPAPI Widgets { 81 | public: 82 | Widgets(); 83 | void ClearAll ( EventPool* pool ); 84 | 85 | // 2D Setup 86 | void Init ( int w, int h ); 87 | void Create ( std::string txt, ushort ops, Vector4DF rpos, Vector4DF fclr, Vector4DF bclr, std::string img=""); 88 | int AddWidget ( bool is3D=false ); 89 | void Arrange (); 90 | void SetPos ( int i, Vector4DF pos ); 91 | void SetOption ( int start, int end, ushort op, bool on_off ); 92 | void SetColor ( int i, Vector4DF bclr, Vector4DF border ); 93 | void SetImage ( int i, std::string png_name ); 94 | void SetText ( int i, std::string txt ); 95 | void SetTextEntry ( int i, std::string txt ); 96 | void SetButtonType ( int i, int typ ); 97 | void SetButton ( int i, int state ); 98 | void SetVisible ( int i, bool vis ); 99 | void SetVisible ( int start, int end, bool vis ); // make multiple visible 100 | void SetSlider ( int i, float* v, float vmin, float vmax); 101 | void SetFocus ( int i=-1 ) { mFocus = i; } 102 | bool hasOp ( Widget* w, ushort op ) { return (w->opts & op)!=0; } 103 | Widget* getWidget(int i) { return &mWidgets[i]; } 104 | 105 | // 2D Drawing 106 | void Draw (); 107 | 108 | // Event handling 109 | bool OnMouse ( AppEnum button, AppEnum state, int mods, int x, int y, Widget3D& dw, bool& finish); // called from app on mouse activity 110 | bool OnMotion ( AppEnum button, int x, int y, int dx, int dy ); 111 | bool OnKeyboard ( int key ); 112 | void PushEvent ( eventStr_t name, int w, int k ); // called by widgets to push event back to app 113 | bool hasEvents (); 114 | Event getNextEvent(); // get next outgoing event to app 115 | 116 | // 3D Widgets 117 | void Create3D ( ushort ops, Vector3DF pos, Vector3DF angs, Vector3DF scal, Vector4DF clr, std::string txt ); 118 | void Draw3D (); 119 | void SetOpt3D ( int w, ushort opt, bool on ); 120 | bool hasOpt3D ( int w, ushort op ) { return (mWidgets3D[w].opts & op)!=0; } 121 | void SetRes ( int x, int y ) { mXres=x; mYres=y; Arrange(); } 122 | void SetCamera3D ( Camera3D* cam ) { mCam = cam; } 123 | bool OnAction3D ( int w, int x, int y ); 124 | bool OnUpdate3D ( int w, int x, int y ); 125 | void SetText3D (int w, std::string txt) { mWidgets3D[w].text = txt; } 126 | bool IntersectBox3D ( Vector3DF pos, Vector3DF piv, Vector3DF scale, Quaternion rot, float x, float y ); 127 | Widget3D* getWidget3D( int i ) { return &mWidgets3D[i]; } 128 | bool isActive3D() { return mActWidget >= 0; } 129 | void SetActiveWidget3D (int i=-1, std::string name="", Vector4DF clr=Vector4DF(1,1,1,1)); 130 | void DisableWidget3D(); 131 | Vector3DF getCenter3D ( int i ) { return mWidgets3D[i].pos; } 132 | 133 | public: 134 | int mXres, mYres; 135 | std::vector< Widget > mWidgets; 136 | std::vector< Widget3D > mWidgets3D; 137 | int mFocus, mFocusPos; 138 | float mFocusX; 139 | 140 | EventPool* mPool; 141 | EventQueue mOutEvents; 142 | EventQueue mInEvents; 143 | 144 | Camera3D* mCam; 145 | char mAct; 146 | int mActWidget; 147 | Vector3DF mActP, mP2; 148 | Widget3D mActWgOrig; 149 | float mWidgetSize3D; 150 | Vector3DI mMod; 151 | }; 152 | 153 | HELPAPI extern Widgets* gInterface; 154 | 155 | #endif -------------------------------------------------------------------------------- /libmin/mains/main.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef __MAIN_H__ 4 | #define __MAIN_H__ 5 | 6 | // Application lifecycle: 7 | // def. Calls made to the base Application will be defined as 'app functions'. 8 | // def. Calls made to the derived user class will be defined as 'user functions'. 9 | // def. Calls made from the Java/JNI into a native hook will be defined as 'native functions'. 10 | // 11 | // Lifecycle: 12 | // 1. User instantiates a derived class MyApp inheriting from Application. e.g. class MyApp : public Application {..} 13 | // 2. Application constructor is called. This sets the global pApp to access the Application abstractly. 14 | // 3. The OS-platform invokes hooks which start the process. e.g. WinMain or Android onCreate 15 | // 4. The user function startup() is called, which invoked appStartup with desired configuration variables 16 | // 5. The OS-platform calls appStartWindow 17 | // 6. appStartWindow is handed context variables from the OS/Native system. e.g HWND or ANativeWindow, JavaVM, etc. 18 | // 7. appStartWindow creates a new OSWindow to store these platform-specific globals 19 | // 8. appStartWindow calls appCreateGL to create an OpenGL context & surface 20 | // 9. The OpenGL context, surface and display are also stored in the OSWindow variables 21 | // 10. appStartWindow calls user init() if this is the first invocation 22 | // 11. appStartWindow calls user activate() to handle repeated changes to the display surface 23 | // 12. appStartWindow calls enable_nvgui to start the 2D drawing framework 24 | // 13. The OS-platform OR the Application prepare a main loop 25 | // 14. appRun is called repeatedly, which makes calls to the user display() to perform draw updates 26 | // 15. The OS-platform OR the Application handle mouse & keyboard events, which are passed to appHandleEvent 27 | // 16. appHandleEvent processes mouse deltas and calls the user mouse() and motion() functions 28 | // 17. The OS-platform calls addStopWindow if the app is backgrounded, loses focus, or is re-oriented 29 | // 18. Once the application resumes, the OS-platform will call appStartWindow again. 30 | // 19. Upon termination appShutdown is called 31 | 32 | #pragma warning(disable:4996) // preventing snprintf >> _snprintf_s 33 | 34 | #include "common_defs.h" 35 | #include "main_includes.h" 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | // trick for pragma message so we can write: 42 | // #pragma message(__FILE__"("S__LINE__"): blah") 43 | #define S__(x) #x 44 | #define S_(x) S__(x) 45 | #define S__LINE__ S_(__LINE__) 46 | 47 | #ifdef DEBUG_HEAP 48 | #define _CRTDBG_MAP_ALLOC 49 | #include 50 | #include 51 | #else 52 | #include 53 | #endif 54 | 55 | #ifdef WIN32 56 | #ifdef MEMORY_LEAKS_CHECK 57 | # pragma message("build will Check for Memory Leaks!") 58 | # define _CRTDBG_MAP_ALLOC 59 | # include 60 | # include 61 | inline void* operator new(size_t size, const char *file, int line) 62 | { 63 | return ::operator new(size, 1, file, line); 64 | } 65 | 66 | inline void __cdecl operator delete(void *ptr, const char *file, int line) 67 | { 68 | ::operator delete(ptr, _NORMAL_BLOCK, file, line); 69 | } 70 | 71 | #define DEBUG_NEW new( __FILE__, __LINE__) 72 | #define MALLOC_DBG(x) _malloc_dbg(x, 1, __FILE__, __LINE__); 73 | #define malloc(x) MALLOC_DBG(x) 74 | #define new DEBUG_NEW 75 | #endif 76 | #endif 77 | 78 | 79 | 80 | typedef void (*OSProc)(void); 81 | class OSWindow; // Forward reference. Described specifically by each platform. 82 | struct Event; 83 | 84 | //----------------- to be declared in the code of the sample: so the sample can decide how to display messages 85 | class Application { 86 | public: 87 | Application(); // this initializes the global pApp 88 | 89 | // Application-level functions 90 | // - abstraction virtual, available for app to override 91 | virtual void startup() {} 92 | virtual bool init() { return true; } 93 | virtual bool activate() { return true; } 94 | virtual void shutdown() {} 95 | virtual void reshape(int w, int h) { } 96 | virtual void on_arg(int i, std::string arg, std::string val ) {} 97 | virtual void mouse( AppEnum button, AppEnum action, int mods, int x, int y) {} 98 | virtual void motion( AppEnum button, int x, int y, int dx, int dy) {} 99 | virtual void mousewheel(int delta) {} 100 | virtual void keyboard(int keycode, AppEnum action, int mods, int x, int y) {} 101 | virtual void keyboardchar(unsigned char key, int mods, int x, int y) {} 102 | virtual void display() {} 103 | virtual bool begin() { return true; } 104 | virtual void end() {} 105 | virtual void checkpoint() {} 106 | 107 | #ifdef USE_NETWORK 108 | virtual void on_event( Event* e ) {} // Events 109 | void appSendEventToApp ( Event* e ); 110 | #endif 111 | 112 | // App Context 113 | struct ContextFlags { 114 | int major, minor, MSAA, depth, stencil; 115 | bool debug, robust, core, forward, stereo; 116 | ContextFlags(int _major=3, int _minor=0, bool _core=true, int _MSAA=1, int _depth=24, int _stencil=8,bool _debug=false, bool _robust=false, bool _forward=false, bool _stereo=false) 117 | { 118 | major = _major; minor = _minor; MSAA = _MSAA; depth = _depth; stencil = _stencil; core = _core; debug = _debug; robust = _robust; 119 | forward = _forward; stereo = _stereo; 120 | } 121 | }; 122 | 123 | // Hardware/Platform specific 124 | // - these functions are implemented differently in main_win, main_x11, main_android.cpp 125 | // - functions are listed here generally in order they are called 126 | 127 | bool appStart( const std::string &name, const std::string& shortname, int width, int height, int Major, int Minor, int MSAA=16, bool GLDebug=false ); 128 | void appHandleArgs (int argc, char** argv); 129 | bool appStartWindow ( void* arg1=0, void* arg2=0, void* arg3=0, void* arg4=0 ); 130 | bool appCreateGL (const Application::ContextFlags *cflags, int& width, int& height); 131 | bool appInitGL (); 132 | void appRun(); 133 | void appSwapBuffers(); 134 | void appPostRedisplay(int n=1) { m_renderCnt=n; } 135 | void appResizeWindow ( int w, int h ); 136 | void appForegroundWindow(); 137 | void appMaximize(); 138 | void appMinimize(); 139 | void appRestore(); 140 | void appPostQuit(); 141 | bool appStopWindow (); 142 | void appShutdown (); 143 | 144 | // Input functions 145 | void appUpdateMouse ( float mx, float my, AppEnum button, AppEnum state); 146 | void appUpdateMouse ( float mx, float my ); 147 | void appHandleEvent ( guiEvent g ); 148 | void appSetKeyPress ( int key, bool state ); 149 | void appOpenKeyboard (); 150 | void appCloseKeyboard (); 151 | void appOpenBrowser ( std::string app, std::string query ); 152 | 153 | // Set functions 154 | void appSetTitle(const char* title); 155 | void appSetFullscreen ( bool fullscreen ); 156 | void appSetVSync(bool state); 157 | void appSwapInterval(int i); 158 | void appSaveFrame ( char* fname ); 159 | 160 | // Accessors 161 | bool isActive(); 162 | bool onPress(int key) { return m_keyPressed[key] && m_keyToggled[key]; } 163 | inline void setWinSz(int w, int h) { m_winSz[0]=w; m_winSz[1]=h; } 164 | inline const int* getWinSz() const { return m_winSz; } 165 | inline int getWidth() const { return m_winSz[0]; } 166 | inline int getHeight() const { return m_winSz[1]; } 167 | inline const int getWheel() const { return m_wheel; } 168 | inline int getMods() const { return m_mods; } 169 | bool getKeyPress(int key) { return m_keyPressed[key]; } 170 | void setKeyPress(int key, bool v) { m_keyPressed[key] = v; } 171 | inline int getKeyMods(); 172 | inline void setMods(int m) { m_mods = m; } 173 | inline float getX() { return m_mouseX; } 174 | inline float getY() { return m_mouseY; } 175 | inline float getDX() { return m_dX; } 176 | inline float getDY() { return m_dY; } 177 | inline bool isFirstFrame() { return m_display_frame==0; } 178 | inline int getDisplayFrame() { return m_display_frame; } 179 | 180 | public: 181 | 182 | std::string m_title; 183 | 184 | OSWindow* m_win; 185 | int m_renderCnt; 186 | 187 | float m_mouseX, m_mouseY; // mouse motion 188 | float m_lastX, m_lastY; 189 | float m_spanX, m_spanY; 190 | float m_dX, m_dY; 191 | float m_startX, m_startY; 192 | AppEnum m_mouseButton; 193 | AppEnum m_mouseState; 194 | int m_wheel; 195 | 196 | int m_winSz[4]; // window info 197 | int m_mods; 198 | ContextFlags m_cflags; 199 | bool m_doSwap; 200 | bool m_startup; 201 | bool m_running; 202 | bool m_active; 203 | bool m_vsync; 204 | bool m_fullscreen; 205 | 206 | bool m_keyPressed[ 400 ]; // keyboard 207 | bool m_keyToggled[ 400 ]; 208 | 209 | int m_display_frame; // frame capture 210 | int m_golden_frame; 211 | 212 | unsigned int m_debugFilter; 213 | }; 214 | 215 | // External define (for inclusion in other headers) 216 | // 217 | extern Application* pApp; 218 | 219 | #endif 220 | -------------------------------------------------------------------------------- /libmin/src/common_cuda.cpp: -------------------------------------------------------------------------------- 1 | #ifdef USE_CUDA 2 | 3 | #include "common_defs.h" 4 | #include "common_cuda.h" 5 | #include 6 | 7 | bool cuCheck(CUresult launch_stat, char* method, char* apicall, char* arg, bool bDebug) 8 | { 9 | CUresult kern_stat = CUDA_SUCCESS; 10 | 11 | if (bDebug) { 12 | kern_stat = cuCtxSynchronize(); 13 | } 14 | if (kern_stat != CUDA_SUCCESS || launch_stat != CUDA_SUCCESS) { 15 | const char* launch_statmsg = ""; 16 | const char* kern_statmsg = ""; 17 | cuGetErrorString(launch_stat, &launch_statmsg); 18 | cuGetErrorString(kern_stat, &kern_statmsg); 19 | dbgprintf("------- CUDA ERROR:\n"); 20 | dbgprintf(" Launch status: %s\n", launch_statmsg); 21 | dbgprintf(" Kernel status: %s\n", kern_statmsg); 22 | dbgprintf(" Caller: Particles::%s\n", method); 23 | dbgprintf(" Call: %s\n", apicall); 24 | dbgprintf(" Args: %s\n", arg); 25 | 26 | if (bDebug) { 27 | dbgprintf(" Generating assert to examine call stack.\n"); 28 | assert(0); // debug - trigger break (see call stack) 29 | } 30 | else { 31 | exit(-1); 32 | } 33 | return false; 34 | } 35 | return true; 36 | } 37 | 38 | void cuStart(int devsel, CUcontext ctxsel, CUdevice& dev, CUcontext& ctx, CUstream* strm, bool verbose) 39 | { 40 | int version = 0; 41 | char name[128]; 42 | 43 | int cnt = 0; 44 | CUdevice dev_id; 45 | cuInit(0); 46 | 47 | //--- List devices 48 | cuDeviceGetCount(&cnt); 49 | 50 | if (cnt == 0) { 51 | dbgprintf("ERROR: No CUDA devices found.\n"); 52 | dev = NULL; ctx = NULL; 53 | exit(-1); 54 | return; 55 | } 56 | if (verbose) dbgprintf(" Device List:\n"); 57 | for (int n = 0; n < cnt; n++) { 58 | cuDeviceGet(&dev_id, n); 59 | cuDeviceGetName(name, 128, dev_id); 60 | 61 | int w1, h1, d1; 62 | cuDeviceGetAttribute(&w1, CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_WIDTH, dev_id); 63 | cuDeviceGetAttribute(&h1, CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_HEIGHT, dev_id); 64 | cuDeviceGetAttribute(&d1, CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_DEPTH, dev_id); 65 | 66 | if (verbose) dbgprintf(" %d. %s maxtex (%d,%d,%d)\n", n, name, w1, h1, d1); 67 | } 68 | 69 | if (devsel == DEV_CURRENT) { 70 | //--- Get currently active context 71 | cuCheck(cuCtxGetCurrent(&ctx), "", "cuCtxGetCurrent", "", false); 72 | cuCheck(cuCtxGetDevice(&dev), "", "cuCtxGetDevice", "", false); 73 | } 74 | if (devsel == DEV_EXISTING) { 75 | //--- Use existing context passed in 76 | ctx = ctxsel; 77 | cuCheck(cuCtxSetCurrent(ctx), "", "cuCtxSetCurrent", "", false); 78 | cuCheck(cuCtxGetDevice(&dev), "", "cuCtxGetDevice", "", false); 79 | } 80 | if (devsel == DEV_FIRST || devsel >= cnt) devsel = 0; 81 | if (devsel >= cnt) devsel = 0; // Fallback to dev 0 if addition GPUs not found 82 | 83 | if (devsel >= 0) { 84 | //--- Create new context with Driver API 85 | cuCheck(cuDeviceGet(&dev, devsel), "", "cuDeviceGet", "", false); 86 | cuCheck(cuCtxCreate(&ctx, CU_CTX_SCHED_AUTO, dev), "", "cuCtxCreate", "", false); 87 | } 88 | cuDeviceGetName(name, 128, dev); 89 | if (verbose) dbgprintf(" Using Device: %d, %s, Context: %p\n", (int)dev, name, (void*)ctx); 90 | 91 | cuCtxSetCurrent(NULL); 92 | cuCtxSetCurrent(ctx); 93 | } 94 | 95 | void cuGetMemUsage(int& total, int& used, int& free) 96 | { 97 | size_t f, t; 98 | cuMemGetInfo(&f, &t); 99 | free = f / size_t(1024) * size_t(1024); // MB 100 | total = t / size_t(1024) * size_t(1024); 101 | used = total - free; // used 102 | } 103 | 104 | #endif 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /libmin/src/directory.cpp: -------------------------------------------------------------------------------- 1 | #include "directory.h" 2 | 3 | #include "string_helper.h" 4 | 5 | #include 6 | 7 | #ifdef _WIN32 8 | 9 | #include 10 | 11 | #else 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #endif 23 | 24 | std::string Directory::gPathDelim = "\\"; 25 | 26 | Directory::Directory () 27 | { 28 | mPath = ""; 29 | #ifdef _MSC_VER 30 | gPathDelim = "\\"; 31 | #else 32 | gPathDelim = "/"; 33 | #endif 34 | } 35 | 36 | std::string Directory::NormalizeSlashies( std::string path ) 37 | { 38 | #ifdef _MSC_VER 39 | strReplace( path, "/", "\\" ); 40 | if ( strRight(path,1).at(0) != '\\' ) path += "\\"; 41 | return path; 42 | #else 43 | strReplace( path, "\\", "/" ); 44 | if ( strRight(path,1).at(0) != '/' ) path += "/"; 45 | return path; 46 | #endif 47 | } 48 | 49 | 50 | dir_list Directory::GetDirectoryItems( dir_list input ) 51 | { 52 | dir_list out; 53 | 54 | for ( unsigned int i=0; i < input.size(); i++ ) { 55 | if ( input[i].type == FILE_TYPE_DIR && input[i].text != "." ) out.push_back( input[i] ); 56 | } 57 | return out; 58 | } 59 | 60 | dir_list Directory::GetFileItems( dir_list input ) 61 | { 62 | dir_list out; 63 | 64 | for ( unsigned int i=0; i < input.size(); i++ ){ 65 | if ( input[i].type == FILE_TYPE_FILE && input[i].length >= 0 ) out.push_back( input[i] ); 66 | } 67 | return out; 68 | } 69 | 70 | 71 | std::string Directory::GetExtension( std::string path ) 72 | { 73 | path = Directory::NormalizeSlashies( path ); 74 | 75 | std::vector< std::string > temp; 76 | int cnt = strSplitMultiple ( path, ".", temp ); 77 | 78 | if ( cnt > 1 ) { 79 | return temp[ cnt-1 ]; 80 | } 81 | return ""; 82 | } 83 | 84 | 85 | bool Directory::FileExists( std::string filename ) 86 | { 87 | struct stat stFileInfo; 88 | bool exists = false; 89 | int intStat; 90 | 91 | intStat = stat( filename.c_str(), &stFileInfo ); 92 | 93 | if(intStat == 0) { 94 | exists = true; 95 | } 96 | 97 | return( exists ); 98 | } 99 | 100 | 101 | #ifdef _MSC_VER 102 | 103 | int Directory::CreateDir ( std::string path ) 104 | { 105 | path = Directory::NormalizeSlashies( path ); 106 | 107 | int out = 0; 108 | 109 | std::vector< std::string > pathSet; 110 | 111 | int cnt = strSplitMultiple ( path, Directory::gPathDelim, pathSet ); 112 | 113 | std::string currPath = ""; 114 | 115 | std::vector< std::string >::iterator it; 116 | 117 | for ( it = pathSet.begin() ; it < pathSet.end(); it++ ) { 118 | 119 | out = CreateDirectory ( (currPath + *it).c_str() , NULL ); 120 | 121 | currPath += *it + Directory::gPathDelim ; 122 | 123 | if ( out == 0 ) { 124 | DWORD d = GetLastError(); 125 | if ( d == ERROR_PATH_NOT_FOUND ) { return 0; } 126 | if ( d == ERROR_ALREADY_EXISTS ) { /* continue */ } 127 | } 128 | 129 | } 130 | 131 | return out; 132 | } 133 | 134 | 135 | std::string Directory::GetCollapsedPath( std::string path ) 136 | { 137 | // Create a fully resolved path 138 | path = GetExecutablePath() + "/" + path; 139 | 140 | // Normalize slashes (\ vs /) 141 | path = Directory::NormalizeSlashies( path ); 142 | 143 | elem_vec_t out; 144 | std::vector< std::string > temp; 145 | std::string mFileFound = ""; 146 | 147 | // Split path 148 | int cnt = strSplitMultiple ( path, Directory::gPathDelim, temp ); 149 | 150 | for ( unsigned int i = 0; i < temp.size(); i++ ) { 151 | if ( ( temp[i].compare("..") == 0 ) && out.size() > 0 ) { 152 | out.pop_back(); 153 | } else if ( temp[i].size() > 0 && temp[i].find(".") == -1 ) { 154 | text_element_t element; 155 | element.text = temp[i]; 156 | element.length = -1; 157 | out.push_back( element ); 158 | } 159 | 160 | if ( temp[i].find(".") != -1 ) { 161 | mFileFound = temp[i]; 162 | } 163 | } 164 | 165 | std::string outStr = ""; 166 | 167 | for ( unsigned int k = 0; k < out.size(); k++ ) 168 | { 169 | outStr += out[k].text; 170 | outStr += Directory::gPathDelim; 171 | } 172 | 173 | // remember to remove the last extraneous slashies 174 | return outStr.substr( 0, outStr.length() - 1 ); 175 | } 176 | 177 | std::string Directory::GetExecutablePath() 178 | { 179 | LPTSTR szAppPath[MAX_PATH]; 180 | 181 | std::string strAppDirectory; 182 | 183 | ::GetModuleFileName(NULL, szAppPath[0], (sizeof(szAppPath) - 1)/sizeof(TCHAR)); 184 | 185 | // Extract directory 186 | strAppDirectory = (char*) szAppPath; // use ws2s for conversion to wchar 187 | strAppDirectory = strAppDirectory.substr(0, strAppDirectory.rfind("\\")); 188 | 189 | strAppDirectory = strReplace( strAppDirectory, "\\", "/" ); 190 | 191 | return strAppDirectory; 192 | } 193 | 194 | std::string Directory::ws2s(const std::wstring& s) 195 | { 196 | int len; 197 | int slength = (int)s.length() + 1; 198 | len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0); 199 | char* buf = (char*) malloc ( len ); // temporary, don't register with memory checker 200 | WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, buf, len, 0, 0); 201 | std::string r(buf); 202 | free ( buf ); 203 | return r; 204 | } 205 | 206 | std::wstring Directory::s2ws(const std::string& s) 207 | { 208 | int len; 209 | int slength = (int)s.length() + 1; 210 | len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 211 | wchar_t* buf = (wchar_t*) malloc ( len *sizeof(wchar_t) ); // temporary, don't register with memory checker 212 | MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len); 213 | std::wstring r(buf); 214 | free ( buf ); 215 | return r; 216 | } 217 | 218 | void Directory::LoadDir ( std::string path, std::string ext ) 219 | { 220 | mPath = path; 221 | mFileFilter = ext; 222 | mList = DirList ( path, ext ); 223 | } 224 | 225 | dir_list Directory::DirList( std::string path, std::string ext ) 226 | { 227 | path = Directory::NormalizeSlashies( path ) + ext; 228 | 229 | //NOTE this is not unicode compliant 230 | WIN32_FIND_DATAA fileData; 231 | HANDLE hFind = INVALID_HANDLE_VALUE; 232 | LARGE_INTEGER filesize; 233 | 234 | DWORD dwError=0; 235 | 236 | dir_list out; 237 | 238 | hFind = FindFirstFileA( path.c_str(), &fileData ); 239 | 240 | if ((int) INVALID_HANDLE_VALUE == (int) hFind) 241 | { 242 | out.clear(); 243 | return out; 244 | } 245 | 246 | do 247 | { 248 | if (( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) && !( fileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN )) 249 | { 250 | dir_list_element e; 251 | e.length = 0; 252 | e.text = fileData.cFileName; 253 | e.extension = "DIR"; 254 | e.type = FILE_TYPE_DIR; 255 | out.push_back( e ); 256 | } 257 | else 258 | { 259 | filesize.LowPart = fileData.nFileSizeLow; 260 | filesize.HighPart = fileData.nFileSizeHigh; 261 | 262 | dir_list_element e; 263 | e.length = (int) filesize.QuadPart; 264 | e.text = fileData.cFileName; 265 | e.extension = strSplitRight ( e.text, "." ); 266 | e.type = FILE_TYPE_FILE; 267 | out.push_back( e ); 268 | } 269 | } 270 | while (FindNextFileA(hFind, &fileData) != 0); 271 | 272 | dwError = GetLastError(); 273 | if (dwError != ERROR_NO_MORE_FILES) 274 | { 275 | // 276 | } 277 | 278 | FindClose(hFind); 279 | return out; 280 | } 281 | #endif 282 | 283 | #ifdef BUILD_GCC 284 | //#ifdef __LINUX 285 | 286 | // DUMMY!!! 287 | int Directory::CreatePath( std::string path ) { 288 | return 0; 289 | } 290 | 291 | std::string Directory::GetExecutablePath() 292 | { 293 | char result[ PATH_MAX ]; 294 | ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX ); 295 | return std::string( result, (count > 0) ? count : 0 ); 296 | } 297 | 298 | /**************** 299 | * PROBABLY NOT WORKING!! 300 | ****************/ 301 | dir_list Directory::DirList( std::string path ) 302 | { 303 | path = Directory::NormalizeSlashies( path ); 304 | 305 | dir_list out; 306 | 307 | DIR *dp; 308 | struct dirent *dirp; 309 | if( ( dp = opendir( path.c_str() ) ) == NULL ) { 310 | //cout << "Error(" << errno << ") opening " << dir << endl; 311 | printf("Error(%i) opening\n", errno); 312 | //return dir_list; 313 | return out; 314 | } 315 | 316 | while ( (dirp = readdir( dp ) ) != NULL ) { 317 | 318 | dir_list_element e; 319 | e.length = dirp->d_reclen; 320 | e.text = std::string( dirp->d_name ); 321 | //e.extension = "DIR"; 322 | //e.type = FILE_TYPE_DIR; 323 | out.push_back( e ); 324 | 325 | } 326 | 327 | closedir( dp ); 328 | //return 0; 329 | return out; 330 | } 331 | 332 | std::string Directory::GetCollapsedPath( std::string path ) 333 | { 334 | path = Directory::NormalizeSlashies( path ); 335 | 336 | elem_vec_t out; 337 | std::vector< std::string > temp; 338 | 339 | std::string mFileFound = ""; 340 | 341 | temp = StringHelper::SplitString( path, Directory::mPathDelim); 342 | 343 | for ( unsigned int i = 0; i < temp.size(); i++ ) 344 | { 345 | if ( ( temp[i].compare("..") == 0 ) && out.size() > 0 ) { 346 | out.pop_back(); 347 | } else if ( temp[i].size() > 0 && temp[i].find(".") == -1 ) { 348 | text_element_t element; 349 | element.text = temp[i]; 350 | element.length = -1; 351 | out.push_back( element ); 352 | } 353 | 354 | if ( temp[i].find(".") != -1 ) { 355 | mFileFound = temp[i]; 356 | } 357 | } 358 | 359 | std::string outStr = ""; 360 | 361 | for ( unsigned int k = 0; k < out.size(); k++ ) 362 | { 363 | outStr += out[k].text; 364 | outStr += Directory::mPathDelim; 365 | } 366 | 367 | // remember to remove the last extraneous slashies 368 | return outStr.substr( 0, outStr.length() - 1 ); 369 | } 370 | 371 | 372 | #endif 373 | -------------------------------------------------------------------------------- /libmin/src/file_tga.cpp: -------------------------------------------------------------------------------- 1 | //-------------------------------------------------------------------------------- 2 | // NVIDIA(R) GVDB VOXELS 3 | // Copyright 2017, NVIDIA Corporation. 4 | // 5 | // Redistribution and use in source and binary forms, with or without modification, 6 | // are permitted provided that the following conditions are met: 7 | // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 9 | // in the documentation and/or other materials provided with the distribution. 10 | // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived 11 | // from this software without specific prior written permission. 12 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 13 | // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 14 | // SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 15 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 16 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 17 | // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 18 | // 19 | // Version 1.0: Rama Hoetzlein, 5/1/2017 20 | //---------------------------------------------------------------------------------- 21 | 22 | 23 | //------------------------------------------------ TGA FORMAT 24 | #ifndef TGA_NOIMPL 25 | 26 | #include "file_tga.h" 27 | 28 | #ifdef DEBUG_HEAP 29 | #define _CRTDBG_MAP_ALLOC 30 | #include 31 | #include 32 | #else 33 | #include 34 | #endif 35 | 36 | 37 | TGA::~TGA( void ) 38 | { 39 | if( m_nImageData != NULL ) 40 | { 41 | free( m_nImageData ); 42 | m_nImageData = NULL; 43 | } 44 | } 45 | 46 | int TGA::returnError( FILE *s, int error ) 47 | { 48 | // Called when there is an error loading the .tga texture file. 49 | fclose( s ); 50 | return error; 51 | } 52 | 53 | unsigned char *TGA::getRGBA( FILE *s, int size ) 54 | { 55 | // Read in RGBA data for a 32bit image. 56 | unsigned char *rgba; 57 | unsigned char temp; 58 | int bread; 59 | int i; 60 | 61 | rgba = (unsigned char *) malloc( size * 4 ); 62 | 63 | if( rgba == NULL ) 64 | return 0; 65 | 66 | bread = (int) fread( rgba, sizeof (unsigned char), size * 4, s ); 67 | 68 | // TGA is stored in BGRA, make it RGBA 69 | if( bread != size * 4 ) 70 | { 71 | free( rgba ); 72 | return 0; 73 | } 74 | 75 | for( i = 0; i < size * 4; i += 4 ) 76 | { 77 | temp = rgba[i]; 78 | rgba[i] = rgba[i + 2]; 79 | rgba[i + 2] = temp; 80 | } 81 | 82 | m_texFormat = TGA::RGBA; 83 | return rgba; 84 | } 85 | 86 | unsigned char *TGA::getRGB( FILE *s, int size ) 87 | { 88 | // Read in RGB data for a 24bit image. 89 | unsigned char *rgb; 90 | unsigned char temp; 91 | int bread; 92 | int i; 93 | 94 | rgb = (unsigned char*)malloc( size * 3 ); 95 | 96 | if( rgb == NULL ) 97 | return 0; 98 | 99 | bread = (int)fread( rgb, sizeof (unsigned char), size * 3, s ); 100 | 101 | if(bread != size * 3) 102 | { 103 | free( rgb ); 104 | return 0; 105 | } 106 | 107 | // TGA is stored in BGR, make it RGB 108 | for( i = 0; i < size * 3; i += 3 ) 109 | { 110 | temp = rgb[i]; 111 | rgb[i] = rgb[i + 2]; 112 | rgb[i + 2] = temp; 113 | } 114 | 115 | m_texFormat = TGA::RGB; 116 | 117 | return rgb; 118 | } 119 | 120 | unsigned char *TGA::getGray( FILE *s, int size ) 121 | { 122 | // Gets the grayscale image data. Used as an alpha channel. 123 | unsigned char *grayData; 124 | int bread; 125 | 126 | grayData = (unsigned char*)malloc( size ); 127 | 128 | if( grayData == NULL ) 129 | return 0; 130 | 131 | bread = (int)fread( grayData, sizeof (unsigned char), size, s ); 132 | 133 | if( bread != size ) 134 | { 135 | free( grayData ); 136 | return 0; 137 | } 138 | 139 | m_texFormat = TGA::ALPHA; 140 | 141 | return grayData; 142 | } 143 | 144 | #pragma warning(disable: 4996) 145 | 146 | TGA::TGAError TGA::load( const char *name ) 147 | { 148 | // Loads up a targa file. Supported types are 8,24 and 32 149 | // uncompressed images. 150 | unsigned char type[4]; 151 | unsigned char info[7]; 152 | FILE *s = NULL; 153 | int size = 0; 154 | 155 | if( !(s = fopen( name, "rb" )) ) 156 | return TGA_FILE_NOT_FOUND; 157 | 158 | fread( &type, sizeof (char), 3, s ); // Read in colormap info and image type, byte 0 ignored 159 | fseek( s, 12, SEEK_SET); // Seek past the header and useless info 160 | fread( &info, sizeof (char), 6, s ); 161 | 162 | if( type[1] != 0 || (type[2] != 2 && type[2] != 3) ) 163 | returnError( s, TGA_BAD_IMAGE_TYPE ); 164 | 165 | m_nImageWidth = info[0] + info[1] * 256; 166 | m_nImageHeight = info[2] + info[3] * 256; 167 | m_nImageBits = info[4]; 168 | 169 | size = m_nImageWidth * m_nImageHeight; 170 | 171 | // Make sure we are loading a supported type 172 | if( m_nImageBits != 32 && m_nImageBits != 24 && m_nImageBits != 8 ) 173 | returnError( s, TGA_BAD_BITS ); 174 | 175 | if( m_nImageBits == 32 ) 176 | m_nImageData = getRGBA( s, size ); 177 | else if( m_nImageBits == 24 ) 178 | m_nImageData = getRGB( s, size ); 179 | else if( m_nImageBits == 8 ) 180 | m_nImageData = getGray( s, size ); 181 | 182 | // No image data 183 | if( m_nImageData == NULL ) 184 | returnError( s, TGA_BAD_DATA ); 185 | 186 | fclose( s ); 187 | 188 | return TGA_NO_ERROR; 189 | } 190 | 191 | void TGA::writeRGBA( FILE *s, const unsigned char *externalImage, int size ) 192 | { 193 | // Read in RGBA data for a 32bit image. 194 | unsigned char *rgba; 195 | int bread; 196 | int i; 197 | 198 | rgba = (unsigned char *)malloc( size * 4 ); 199 | 200 | // switch RGBA to BGRA 201 | for( i = 0; i < size * 4; i += 4 ) 202 | { 203 | rgba[i + 0] = externalImage[i + 2]; 204 | rgba[i + 1] = externalImage[i + 1]; 205 | rgba[i + 2] = externalImage[i + 0]; 206 | rgba[i + 3] = externalImage[i + 3]; 207 | } 208 | 209 | bread = (int)fwrite( rgba, sizeof (unsigned char), size * 4, s ); 210 | free( rgba ); 211 | } 212 | 213 | void TGA::writeRGB( FILE *s, const unsigned char *externalImage, int size ) 214 | { 215 | // Read in RGBA data for a 32bit image. 216 | unsigned char *rgb; 217 | int bread; 218 | int i; 219 | 220 | rgb = (unsigned char *)malloc( size * 3 ); 221 | 222 | // switch RGB to BGR 223 | for( i = 0; i < size * 3; i += 3 ) 224 | { 225 | rgb[i + 0] = externalImage[i + 2]; 226 | rgb[i + 1] = externalImage[i + 1]; 227 | rgb[i + 2] = externalImage[i + 0]; 228 | } 229 | 230 | bread = (int)fwrite( rgb, sizeof (unsigned char), size * 3, s ); 231 | free( rgb ); 232 | } 233 | 234 | void TGA::writeGrayAsRGB( FILE *s, const unsigned char *externalImage, int size ) 235 | { 236 | // Read in RGBA data for a 32bit image. 237 | unsigned char *rgb; 238 | int bread; 239 | int i; 240 | 241 | rgb = (unsigned char *)malloc( size * 3 ); 242 | 243 | // switch RGB to BGR 244 | int j = 0; 245 | for( i = 0; i < size * 3; i += 3, j++ ) 246 | { 247 | rgb[i + 0] = externalImage[j]; 248 | rgb[i + 1] = externalImage[j]; 249 | rgb[i + 2] = externalImage[j]; 250 | } 251 | 252 | bread = (int)fwrite( rgb, sizeof (unsigned char), size * 3, s ); 253 | free( rgb ); 254 | } 255 | 256 | void TGA::writeGray( FILE *s, const unsigned char *externalImage, int size ) 257 | { 258 | // Gets the grayscale image data. Used as an alpha channel. 259 | int bread; 260 | 261 | bread = (int)fwrite( externalImage, sizeof (unsigned char), size, s ); 262 | } 263 | 264 | TGA::TGAError TGA::saveFromExternalData( const char *name, int w, int h, TGA::TGAFormat fmt, const unsigned char *externalImage ) 265 | { 266 | static unsigned char type[] = {0,0,2}; 267 | static unsigned char dummy[] = {0,0,0,0,0,0,0,0,0}; 268 | static unsigned char info[] = {0,0,0,0,0,0}; 269 | FILE *s = NULL; 270 | int size = 0; 271 | 272 | if( !(s = fopen( name, "wb" )) ) 273 | return TGA_FILE_NOT_FOUND; 274 | 275 | fwrite( type, sizeof (char), 3, s ); // Read in colormap info and image type, byte 0 ignored 276 | fwrite( dummy, sizeof (char), 9, s ); // Read in colormap info and image type, byte 0 ignored 277 | 278 | info[0] = w & 0xFF; 279 | info[1] = (w>>8) & 0xFF; 280 | info[2] = h & 0xFF; 281 | info[3] = (h>>8) & 0xFF; 282 | switch(fmt) 283 | { 284 | case ALPHA: 285 | info[4] = 8; 286 | break; 287 | case RGB: 288 | info[4] = 24; 289 | break; 290 | case RGBA: 291 | info[4] = 32; 292 | break; 293 | } 294 | fwrite( info, sizeof (char), 6, s ); 295 | 296 | size = w*h; 297 | switch(fmt) 298 | { 299 | case ALPHA: 300 | writeGray(s, externalImage, size); 301 | break; 302 | case RGB: 303 | writeGrayAsRGB/*writeRGB*/(s, externalImage, size); 304 | break; 305 | case RGBA: 306 | writeRGBA(s, externalImage, size); 307 | break; 308 | } 309 | 310 | fclose( s ); 311 | 312 | return TGA_NO_ERROR; 313 | } 314 | 315 | #endif // #ifndef TGA_NOIMPL 316 | 317 | -------------------------------------------------------------------------------- /libmin/src/image_bgr24.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "image.h" 3 | 4 | #include 5 | 6 | void Image::SetPixelBGR24 (int x, int y, XBYTE r, XBYTE g, XBYTE b, XBYTE a) 7 | { 8 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 9 | XBYTE* pix = (XBYTE*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->mBitsPerPix >> 3 ); 10 | *pix++ = b; 11 | *pix++ = g; 12 | *pix++ = r; 13 | // setNotify ( 1 ); // *** NOTIFY(1) 14 | } 15 | } 16 | 17 | void Image::GetPixelBGR24 (int x, int y, XBYTE& r, XBYTE& g, XBYTE& b, XBYTE& a ) 18 | { 19 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 20 | XBYTE* pix = (XBYTE*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->mBitsPerPix >> 3); 21 | b = *pix++; 22 | g = *pix++; 23 | r = *pix++; 24 | a = 255; 25 | } 26 | } 27 | 28 | void Image::FillBGR24 (XBYTE r, XBYTE g, XBYTE b, XBYTE a) 29 | { 30 | // Temporary fill buffer 31 | assert ( getInfo()->mBytesPerRow <= 16384 ); 32 | for (uint x=0; x < getInfo()->mBytesPerRow ;) { 33 | fillbuf[x++] = b; 34 | fillbuf[x++] = g; 35 | fillbuf[x++] = r; 36 | } 37 | 38 | XBYTE *dest_pix, *dest_pix_stop; 39 | dest_pix = (XBYTE*) GetData(); 40 | dest_pix_stop = dest_pix + getInfo()->GetSize(); 41 | for (; dest_pix < dest_pix_stop;) { 42 | memcpy (dest_pix, fillbuf, getInfo()->mBytesPerRow ); 43 | dest_pix += getInfo()->mBytesPerRow; 44 | } 45 | // setNotify ( 1 ); // *** NOTIFY(1) 46 | } 47 | 48 | 49 | void Image::RemapBGR24 ( unsigned int vmin, unsigned int vmax ) 50 | { 51 | XBYTE* src = (XBYTE*) GetData(); 52 | XBYTE* src_stop = src + (getInfo()->mXres*getInfo()->mYres); 53 | 54 | unsigned long mMin, mMax; 55 | mMin = (unsigned long(1) << 16)-1; 56 | mMax = 0; 57 | for (; src < src_stop; ) { 58 | if ( *src < mMin ) mMin = *src; 59 | if ( *src > mMax ) mMax = *src; 60 | src++; 61 | } 62 | if ( vmin == vmax ) return; 63 | 64 | src = (XBYTE*) GetData(); 65 | unsigned int vdelta = vmax-vmin; 66 | for (; src < src_stop; ) { 67 | *src = (XBYTE) ( float(vmin) + float(*src - mMin)*vdelta / (mMax-mMin) ); 68 | src++; 69 | } 70 | } 71 | 72 | // Reformat - Reformats given pixel format to 73 | // another pixel format 74 | void Image::ReformatBGR24 ( ImageOp::Format eFormat ) 75 | { 76 | Image* new_img = new Image ( getInfo()->mXres, getInfo()->mYres, eFormat ) ; 77 | 78 | XBYTE* src = (XBYTE*) GetData(); 79 | XBYTE* src_stop = src + getInfo()->GetSize(); 80 | XBYTE* dest = new_img->GetData (); 81 | 82 | if ( eFormat==ImageOp::RGBA32 ) { // Target format RGBA32 83 | for (; src < src_stop; ) { 84 | *dest++ = *(src++ + 2); 85 | *dest++ = *src++; 86 | *dest++ = *(src++ - 2); 87 | *dest++ = 255; 88 | } 89 | } else if ( eFormat==ImageOp::RGB24 ) { // Target format RGB24 90 | for (; src < src_stop; ) { 91 | *dest++ = *(src++ + 2); 92 | *dest++ = *src++; 93 | *dest++ = *(src++ - 2); 94 | } 95 | } 96 | TransferFrom ( new_img ); 97 | delete ( new_img ); 98 | } 99 | 100 | // Paste - Pastes a portion of an image into another image. 101 | // Subselection: yes - Allows sub-selection of source 102 | // Cropping: yes - Allows cropping into target 103 | // Offsetting: yes - Allows offsetting into target 104 | // Scaling: no - Allows rescaling of source 105 | // Filtering: no - Allows filtering of source 106 | // Rotation: no - Allows rotation of source 107 | void Image::PasteBGR24 ( int x1, int y1, int x2, int y2, int offx, int offy, XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 108 | { 109 | XBYTE *src, *src_start, *src_end; 110 | XBYTE *dest_start, *dest_end; 111 | int src_wid, src_pitch; 112 | int dest_wid, dest_pitch, dest_bpr; 113 | 114 | 115 | if ( dest_format==ImageOp::RGB24 ) { 116 | if ( getInfo()->QueryPaste ( GetFormat(), GetWidth(), GetHeight(), GetData(), x1, y1, x2, y2, 117 | dest_format, destx, desty, dest, offx, offy, 118 | src_start, src_end, src_wid, src_pitch, 119 | dest_start, dest_end, dest_wid, dest_pitch) ) 120 | { 121 | dest_bpr = GetBytesPerRow ( destx, desty, dest_format ); 122 | for (src = src_start, dest = dest_start; src < src_end;) { 123 | memcpy (dest, src, src_wid); 124 | src += GetBytesPerRow (); 125 | dest += dest_bpr; 126 | } 127 | } 128 | } 129 | } 130 | 131 | // Scale - Rescales an image into another image 132 | // Subselection: no - Allows sub-selection of source 133 | // Cropping: no - Allows cropping into target 134 | // Offsetting: no - Allows offsetting into target 135 | // Scaling: yes - Allows rescaling of source 136 | // Filtering: yes - Allows filtering of source 137 | // Rotation: no - Allows rotation of source 138 | // Limitations: Rescaled size must match target buffer 139 | void Image::ScaleBGR24 ( XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 140 | { 141 | assert ( GetFormat()==dest_format ); 142 | 143 | // Limitation: Rescaled size must match target buffer 144 | int nx = destx; 145 | int ny = desty; 146 | 147 | // Compute pixel addresses 148 | int dest_bpr = GetBytesPerRow(destx,desty,dest_format); 149 | XBYTE* src; // Current pixel in source 150 | XBYTE* src_row; // Current row in source (start of row) 151 | XBYTE* dest_rowend = dest + dest_bpr; // End of row in dest 152 | XBYTE* dest_end = dest + GetSize(destx,desty,dest_format); // End of entire image in dest 153 | double delta_x = double(getInfo()->mXres-2) / double(nx); // Filtering distances 154 | double delta_y = double(getInfo()->mYres-2) / double(ny); 155 | int iDiffX = int(delta_x*0.5) * 3; // Filtering deltas (in bytes) 156 | int iDiffY = getInfo()->GetBytesPerRow(); 157 | float sx, sy; // Current pixel (with sub-pixel accuracy) 158 | 159 | sy = 1.0; 160 | for (; dest < dest_end;) { 161 | src_row = (XBYTE*) GetData() + int(sy) * getInfo()->GetBytesPerRow(); 162 | sx = 1.0; 163 | for (; dest < dest_rowend;) { 164 | src = src_row + (int) sx*3; 165 | // Filtering (Fast, poor quality but better than nothing) 166 | // RED 167 | *dest = *(src - iDiffX) >> 3; // left 168 | *dest += *(src) >> 1; // center 169 | *dest += *(src+iDiffX) >> 3; // right 170 | *dest += *(src-iDiffY) >> 3; // up 171 | *dest++ += *(src+iDiffY) >> 3; // down 172 | src++; 173 | 174 | // GREEN 175 | *dest = *(src - iDiffX) >> 3; // left 176 | *dest += *(src) >> 1; // center 177 | *dest += *(src+iDiffX) >> 3; // right 178 | *dest += *(src-iDiffY) >> 3; // up 179 | *dest++ += *(src+iDiffY) >> 3; // down 180 | src++; 181 | 182 | // BLUE 183 | *dest = *(src - iDiffX) >> 3; // left 184 | *dest += *(src) >> 1; // center 185 | *dest += *(src+iDiffX) >> 3; // right 186 | *dest += *(src-iDiffY) >> 3; // up 187 | *dest++ += *(src+iDiffY) >> 3; // down 188 | 189 | sx += (float) delta_x; 190 | } 191 | sy += (float) delta_y; 192 | dest_rowend += dest_bpr; 193 | } 194 | } 195 | 196 | // Alpha Paste - Copies alpha from another source 197 | void Image::AlphaBGR24 ( int x1, int y1, int x2, int y2, XBYTE* src, ImageOp::Format src_format, int src_x, int src_y ) 198 | { 199 | // No alpha channel ! 200 | 201 | return; 202 | } 203 | 204 | -------------------------------------------------------------------------------- /libmin/src/image_bw16.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "image.h" 3 | 4 | #include 5 | 6 | 7 | void Image::SetPixelBW16 (int x, int y, XBYTE r, XBYTE g, XBYTE b, XBYTE a) 8 | { 9 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 10 | XBYTE2* pix = (XBYTE2*) (GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() )); 11 | *pix = r; 12 | // setNotify ( 1 ); // *** NOTIFY(1) 13 | } 14 | } 15 | void Image::GetPixelBW16 (int x, int y, XBYTE& r, XBYTE& g, XBYTE& b, XBYTE& a ) 16 | { 17 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 18 | XBYTE* pix = (XBYTE*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() ); 19 | r = *(pix+1); 20 | g = *pix; 21 | b = 0; 22 | a = 255; 23 | } 24 | } 25 | 26 | void Image::FillBW16 (XBYTE r, XBYTE g, XBYTE b, XBYTE a) 27 | { 28 | // Temporary fill buffer 29 | assert ( getInfo()->GetBytesPerRow() <= 16384 ); 30 | for (uint x=0; x < getInfo()->GetBytesPerRow();) 31 | fillbuf[x++] = r; 32 | 33 | XBYTE2 *dest_pix, *dest_pix_stop; 34 | dest_pix = (XBYTE2*) GetData(); 35 | dest_pix_stop = dest_pix + getInfo()->GetSize(); 36 | for (; dest_pix < dest_pix_stop;) { 37 | memcpy (dest_pix, fillbuf, getInfo()->GetBytesPerRow()); 38 | dest_pix += getInfo()->GetBytesPerRow(); 39 | } 40 | // setNotify ( 1 ); // *** NOTIFY(1) 41 | } 42 | 43 | 44 | void Image::RemapBW16 ( unsigned int vmin, unsigned int vmax ) 45 | { 46 | XBYTE2* src = (XBYTE2*) GetData(); 47 | XBYTE2* src_stop = src + (getInfo()->mXres*getInfo()->mYres); 48 | 49 | unsigned long mMin, mMax; 50 | mMin = ((unsigned long) 1 << 16)-1; 51 | mMax = 0; 52 | for (; src < src_stop; ) { 53 | if ( *src < mMin ) mMin = *src; 54 | if ( *src > mMax ) mMax = *src; 55 | src++; 56 | } 57 | if ( vmin == vmax ) return; 58 | 59 | src = (XBYTE2*) GetData(); 60 | unsigned int vdelta = vmax-vmin; 61 | for (; src < src_stop; ) { 62 | *src = (XBYTE2) ( float(vmin) + float(*src - mMin)*vdelta / (mMax-mMin) ); 63 | src++; 64 | } 65 | } 66 | 67 | // Reformat - Reformats given pixel format to 68 | // another pixel format 69 | void Image::ReformatBW16 ( ImageOp::Format eFormat ) 70 | { 71 | Image* new_img = new Image ( getInfo()->mXres, getInfo()->mYres, eFormat ); 72 | 73 | XBYTE2* src = (XBYTE2*) GetData(); 74 | XBYTE2* src_stop = src + (getInfo()->mXres*getInfo()->mYres); 75 | 76 | switch ( eFormat ) { 77 | case ImageOp::BW16: 78 | break; 79 | case ImageOp::BW32: { 80 | XBYTE4* dest = (XBYTE4*) new_img->GetData (); 81 | for (; src < src_stop; ) { 82 | *dest++ = *src++; 83 | } 84 | } break; 85 | case ImageOp::RGB24: { 86 | XBYTE* dest = (XBYTE*) new_img->GetData (); 87 | for (; src < src_stop; ) { 88 | *dest++ = (XBYTE) *src; 89 | *dest++ = (XBYTE) *src; 90 | *dest++ = (XBYTE) *src++; 91 | } 92 | } break; 93 | case ImageOp::BGR24: { 94 | XBYTE* dest = (XBYTE*) new_img->GetData (); 95 | for (; src < src_stop; ) { 96 | *dest++ = (XBYTE) *src; 97 | *dest++ = (XBYTE) *src; 98 | *dest++ = (XBYTE) *src++; 99 | } 100 | } break; 101 | case ImageOp::RGBA32: { 102 | XBYTE* dest = (XBYTE*) new_img->GetData (); 103 | for (; src < src_stop; ) { 104 | *dest++ = (XBYTE) *src; 105 | *dest++ = (XBYTE) *src; 106 | *dest++ = (XBYTE) *src++; 107 | *dest++ = 255; 108 | } 109 | } break; 110 | }; 111 | TransferFrom ( new_img ); 112 | delete ( new_img ); 113 | } 114 | 115 | // Paste - Pastes a portion of an image into another image. 116 | // Subselection: yes - Allows sub-selection of source 117 | // Cropping: yes - Allows cropping into target 118 | // Offsetting: yes - Allows offsetting into target 119 | // Scaling: no - Allows rescaling of source 120 | // Filtering: no - Allows filtering of source 121 | // Rotation: no - Allows rotation of source 122 | void Image::PasteBW16 ( int x1, int y1, int x2, int y2, int offx, int offy, XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 123 | { 124 | XBYTE *src, *src_start, *src_end; 125 | XBYTE *dest_start, *dest_end, *dest_row; 126 | int src_wid, src_pitch; 127 | int dest_wid, dest_pitch, dest_bpr; 128 | 129 | if ( getInfo()->QueryPaste ( GetFormat(), GetWidth(), GetHeight(), GetData(), x1, y1, x2, y2, 130 | dest_format, destx, desty, dest, offx, offy, 131 | src_start, src_end, src_wid, src_pitch, 132 | dest_start, dest_end, dest_wid, dest_pitch) ) { 133 | if ( dest_format==ImageOp::BW8 ) { 134 | dest_bpr = getInfo()->GetBytesPerRow(); 135 | for (src = src_start, dest = dest_start; dest < dest_end;) { 136 | memcpy (dest, src, src_wid); 137 | src += GetBytesPerRow (); 138 | dest += dest_bpr; 139 | } 140 | } else if ( dest_format==ImageOp::RGB24 ) { 141 | dest_bpr = getInfo()->GetBytesPerRow(); 142 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 143 | for ( ; dest < dest_row; ) { 144 | *dest++ = *src; 145 | *dest++ = *src; 146 | *dest++ = *src++; 147 | } 148 | dest_row += dest_bpr; 149 | dest += dest_pitch; 150 | src += src_pitch; 151 | } 152 | } else if ( dest_format==ImageOp::RGBA32 ) { 153 | dest_bpr = getInfo()->GetBytesPerRow(); 154 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 155 | for ( ; dest < dest_row; ) { 156 | *dest++ = *src; 157 | *dest++ = *src; 158 | *dest++ = *src++; 159 | *dest++ = 255; 160 | } 161 | dest_row += dest_bpr; 162 | dest += dest_pitch; 163 | src += src_pitch; 164 | } 165 | } 166 | } 167 | } 168 | 169 | // Scale - Rescales an image into another image 170 | // Subselection: no - Allows sub-selection of source 171 | // Cropping: no - Allows cropping into target 172 | // Offsetting: no - Allows offsetting into target 173 | // Scaling: yes - Allows rescaling of source 174 | // Filtering: yes - Allows filtering of source 175 | // Rotation: no - Allows rotation of source 176 | // Limitations: Rescaled size must match target buffer 177 | 178 | #define BPX 1 // bytes per pixel (fast define) 179 | 180 | void Image::ScaleBW16 ( XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 181 | { 182 | assert ( GetFormat()==dest_format ); 183 | 184 | // Limitation: Rescaled size must match target buffer 185 | int nx = destx; 186 | int ny = desty; 187 | 188 | // Source addressing 189 | XBYTE* src; // Current pixel in source 190 | XBYTE* src_row; // Current row in source (start of row) 191 | 192 | // Destination addressing 193 | int dest_bpr = GetBytesPerRow(destx,desty,dest_format); 194 | XBYTE* dest_rowend = dest + dest_bpr; // End of row in dest 195 | XBYTE* dest_end = dest + GetSize(destx,desty,dest_format); // End of entire image in dest 196 | double delta_x = double(getInfo()->mXres-2) / double(nx); // Filtering distances 197 | double delta_y = double(getInfo()->mYres-2) / double(ny); 198 | int iDiffX = int(delta_x*0.5); // Filtering deltas (in bytes of source data) 199 | int iDiffY = getInfo()->GetBytesPerRow(); 200 | float sx, sy; // Current pixel (with sub-pixel accuracy) 201 | 202 | sy = 1.0; 203 | for (; dest < dest_end;) { 204 | src_row = (XBYTE*) GetData() + int(sy) * getInfo()->GetBytesPerRow(); 205 | sx = 1.0; 206 | for (; dest < dest_rowend;) { 207 | src = src_row + (int) sx; 208 | // Filtering (Fast, poor quality but better than nothing) 209 | // BW 210 | *dest = *(src - iDiffX) >> 3; // left 211 | *dest += *(src) >> 1; // center 212 | *dest += *(src+iDiffX) >> 3; // right 213 | *dest += *(src-iDiffY) >> 3; // up 214 | *dest++ += *(src+iDiffY) >> 3; // down 215 | sx += (float) delta_x; 216 | } 217 | sy += (float) delta_y; 218 | dest_rowend += dest_bpr; 219 | } 220 | } 221 | 222 | // Alpha Paste - Copies alpha from another source 223 | void Image::AlphaBW16 ( int x1, int y1, int x2, int y2, XBYTE* src, ImageOp::Format src_format, int src_x, int src_y ) 224 | { 225 | XBYTE *src_start, *src_end; 226 | XBYTE *dest, *dest_start, *dest_end, *dest_row; 227 | int src_wid, src_pitch; 228 | int dest_wid, dest_pitch, dest_bpr; 229 | 230 | // Source is the alpha image (any format) 231 | // Dest is 'this' image (RGBA32) 232 | if ( getInfo()->QueryPaste ( src_format, src_x, src_y, src, x1, y1, x2, y2, 233 | GetFormat(), GetWidth(), GetHeight(), GetData(), 0, 0, 234 | src_start, src_end, src_wid, src_pitch, 235 | dest_start, dest_end, dest_wid, dest_pitch) ) { 236 | if ( src_format==ImageOp::BW8 ) { 237 | dest_bpr = getInfo()->GetBytesPerRow(); 238 | for (src = src_start, dest = dest_start; dest < dest_end;) { 239 | memcpy (dest, src, src_wid); 240 | src += GetBytesPerRow (); 241 | dest += dest_bpr; 242 | } 243 | } else if ( src_format==ImageOp::RGB24 ) { 244 | dest_bpr = getInfo()->GetBytesPerRow(); 245 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 246 | for ( ; dest < dest_row; ) { 247 | *dest++ = *src; 248 | src += 3; 249 | } 250 | dest_row += dest_bpr; 251 | dest += dest_pitch; 252 | src += src_pitch; 253 | } 254 | } else if ( src_format==ImageOp::RGBA32 ) { 255 | dest_bpr = getInfo()->GetBytesPerRow(); 256 | for (src = src_start+3, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 257 | for ( ; dest < dest_row; ) { 258 | *dest++ = *src; 259 | src += 4; 260 | } 261 | dest_row += dest_bpr; 262 | dest += dest_pitch; 263 | src += src_pitch; 264 | } 265 | } 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /libmin/src/image_bw32.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "image.h" 3 | 4 | #include 5 | 6 | 7 | void Image::SetPixelBW32 (int x, int y, XBYTE r, XBYTE g, XBYTE b, XBYTE a) 8 | { 9 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 10 | XBYTE4* pix = (XBYTE4*) (GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() )); 11 | *pix = r; 12 | // setNotify ( 1 ); // *** NOTIFY(1) 13 | } 14 | } 15 | void Image::GetPixelBW32 (int x, int y, XBYTE& r, XBYTE& g, XBYTE& b, XBYTE& a ) 16 | { 17 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 18 | XBYTE* pix = (XBYTE*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() ); 19 | r = *(pix+3); 20 | g = *(pix+2); 21 | b = *(pix+1); 22 | a = *pix; 23 | } 24 | } 25 | 26 | void Image::FillBW32 (XBYTE r, XBYTE g, XBYTE b, XBYTE a) 27 | { 28 | // Temporary fill buffer 29 | assert ( getInfo()->GetBytesPerRow() <= 16384 ); 30 | for (uint x=0; x < getInfo()->GetBytesPerRow();) 31 | fillbuf[x++] = r; 32 | 33 | XBYTE2 *dest_pix, *dest_pix_stop; 34 | dest_pix = (XBYTE2*) GetData(); 35 | dest_pix_stop = dest_pix + getInfo()->GetSize(); 36 | for (; dest_pix < dest_pix_stop;) { 37 | memcpy (dest_pix, fillbuf, getInfo()->GetBytesPerRow() ); 38 | dest_pix += getInfo()->GetBytesPerRow(); 39 | } 40 | // setNotify ( 1 ); // *** NOTIFY(1) 41 | } 42 | 43 | void Image::RemapBW32 ( unsigned int vmin, unsigned int vmax ) 44 | { 45 | XBYTE4* src = (XBYTE4*) GetData(); 46 | XBYTE4* src_stop = src + (getInfo()->mXres*getInfo()->mYres); 47 | 48 | unsigned long mMin, mMax; 49 | mMin = ((unsigned long) 1 << 16)-1; 50 | mMax = 0; 51 | for (; src < src_stop; ) { 52 | if ( *src < mMin ) mMin = *src; 53 | if ( *src > mMax ) mMax = *src; 54 | src++; 55 | } 56 | if ( vmin == vmax ) return; 57 | 58 | src = (XBYTE4*) GetData(); 59 | unsigned int vdelta = vmax-vmin; 60 | for (; src < src_stop; ) { 61 | *src = (XBYTE4) ( float(vmin) + float(*src - mMin)*vdelta / (mMax-mMin) ); 62 | src++; 63 | } 64 | } 65 | 66 | 67 | // Reformat - Reformats given pixel format to 68 | // another pixel format 69 | void Image::ReformatBW32 ( ImageOp::Format eFormat ) 70 | { 71 | Image* new_img = new Image ( getInfo()->mXres, getInfo()->mYres, eFormat ); 72 | 73 | XBYTE4* src = (XBYTE4*) GetData(); 74 | XBYTE4* src_stop = src + (getInfo()->mXres*getInfo()->mYres); 75 | 76 | switch ( eFormat ) { 77 | case ImageOp::BW16: { 78 | XBYTE2* dest = (XBYTE2*) new_img->GetData (); 79 | for (; src < src_stop; ) { 80 | *dest++ = (XBYTE2) *src++; 81 | } 82 | } break; 83 | case ImageOp::RGB24: { 84 | XBYTE* dest = (XBYTE*) new_img->GetData (); 85 | for (; src < src_stop; ) { 86 | *dest++ = (XBYTE) *src; 87 | *dest++ = (XBYTE) *src; 88 | *dest++ = (XBYTE) *src++; 89 | } 90 | } break; 91 | case ImageOp::BGR24: { 92 | XBYTE* dest = (XBYTE*) new_img->GetData (); 93 | for (; src < src_stop; ) { 94 | *dest++ = (XBYTE) *src; 95 | *dest++ = (XBYTE) *src; 96 | *dest++ = (XBYTE) *src++; 97 | } 98 | } break; 99 | case ImageOp::RGBA32: { 100 | XBYTE* dest = (XBYTE*) new_img->GetData (); 101 | for (; src < src_stop; ) { 102 | *dest++ = (XBYTE) *src; 103 | *dest++ = (XBYTE) *src; 104 | *dest++ = (XBYTE) *src++; 105 | *dest++ = 255; 106 | } 107 | } break; 108 | }; 109 | TransferFrom ( new_img ); 110 | delete ( new_img ); 111 | } 112 | 113 | // Paste - Pastes a portion of an image into another image. 114 | // Subselection: yes - Allows sub-selection of source 115 | // Cropping: yes - Allows cropping into target 116 | // Offsetting: yes - Allows offsetting into target 117 | // Scaling: no - Allows rescaling of source 118 | // Filtering: no - Allows filtering of source 119 | // Rotation: no - Allows rotation of source 120 | void Image::PasteBW32 ( int x1, int y1, int x2, int y2, int offx, int offy, XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 121 | { 122 | XBYTE *src, *src_start, *src_end; 123 | XBYTE *dest_start, *dest_end, *dest_row; 124 | int src_wid, src_pitch; 125 | int dest_wid, dest_pitch, dest_bpr; 126 | 127 | if ( getInfo()->QueryPaste ( GetFormat(), GetWidth(), GetHeight(), GetData(), x1, y1, x2, y2, 128 | dest_format, destx, desty, dest, offx, offy, 129 | src_start, src_end, src_wid, src_pitch, 130 | dest_start, dest_end, dest_wid, dest_pitch) ) { 131 | if ( dest_format==ImageOp::BW8 ) { 132 | dest_bpr = getInfo()->GetBytesPerRow(); 133 | for (src = src_start, dest = dest_start; dest < dest_end;) { 134 | memcpy (dest, src, src_wid); 135 | src += GetBytesPerRow (); 136 | dest += dest_bpr; 137 | } 138 | } else if ( dest_format==ImageOp::RGB24 ) { 139 | dest_bpr = getInfo()->GetBytesPerRow(); 140 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 141 | for ( ; dest < dest_row; ) { 142 | *dest++ = *src; 143 | *dest++ = *src; 144 | *dest++ = *src++; 145 | } 146 | dest_row += dest_bpr; 147 | dest += dest_pitch; 148 | src += src_pitch; 149 | } 150 | } else if ( dest_format==ImageOp::RGBA32 ) { 151 | dest_bpr = getInfo()->GetBytesPerRow(); 152 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 153 | for ( ; dest < dest_row; ) { 154 | *dest++ = *src; 155 | *dest++ = *src; 156 | *dest++ = *src++; 157 | *dest++ = 255; 158 | } 159 | dest_row += dest_bpr; 160 | dest += dest_pitch; 161 | src += src_pitch; 162 | } 163 | } 164 | } 165 | } 166 | 167 | // Scale - Rescales an image into another image 168 | // Subselection: no - Allows sub-selection of source 169 | // Cropping: no - Allows cropping into target 170 | // Offsetting: no - Allows offsetting into target 171 | // Scaling: yes - Allows rescaling of source 172 | // Filtering: yes - Allows filtering of source 173 | // Rotation: no - Allows rotation of source 174 | // Limitations: Rescaled size must match target buffer 175 | 176 | #define BPX 1 // bytes per pixel (fast define) 177 | 178 | void Image::ScaleBW32 ( XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 179 | { 180 | assert ( GetFormat()==dest_format ); 181 | 182 | // Limitation: Rescaled size must match target buffer 183 | int nx = destx; 184 | int ny = desty; 185 | 186 | // Source addressing 187 | XBYTE* src; // Current pixel in source 188 | XBYTE* src_row; // Current row in source (start of row) 189 | 190 | // Destination addressing 191 | int dest_bpr = GetBytesPerRow(destx,desty,dest_format); 192 | XBYTE* dest_rowend = dest + dest_bpr; // End of row in dest 193 | XBYTE* dest_end = dest + GetSize(destx,desty,dest_format); // End of entire image in dest 194 | double delta_x = double(getInfo()->mXres-2) / double(nx); // Filtering distances 195 | double delta_y = double(getInfo()->mYres-2) / double(ny); 196 | int iDiffX = int(delta_x*0.5); // Filtering deltas (in bytes of source data) 197 | int iDiffY = getInfo()->GetBytesPerRow(); 198 | float sx, sy; // Current pixel (with sub-pixel accuracy) 199 | 200 | sy = 1.0; 201 | for (; dest < dest_end;) { 202 | src_row = (XBYTE*) GetData() + int(sy) * getInfo()->GetBytesPerRow(); 203 | sx = 1.0; 204 | for (; dest < dest_rowend;) { 205 | src = src_row + (int) sx; 206 | // Filtering (Fast, poor quality but better than nothing) 207 | // BW 208 | *dest = *(src - iDiffX) >> 3; // left 209 | *dest += *(src) >> 1; // center 210 | *dest += *(src+iDiffX) >> 3; // right 211 | *dest += *(src-iDiffY) >> 3; // up 212 | *dest++ += *(src+iDiffY) >> 3; // down 213 | sx += (float) delta_x; 214 | } 215 | sy += (float) delta_y; 216 | dest_rowend += dest_bpr; 217 | } 218 | } 219 | 220 | // Alpha Paste - Copies alpha from another source 221 | void Image::AlphaBW32 ( int x1, int y1, int x2, int y2, XBYTE* src, ImageOp::Format src_format, int src_x, int src_y ) 222 | { 223 | XBYTE *src_start, *src_end; 224 | XBYTE *dest, *dest_start, *dest_end, *dest_row; 225 | int src_wid, src_pitch; 226 | int dest_wid, dest_pitch, dest_bpr; 227 | 228 | // Source is the alpha image (any format) 229 | // Dest is 'this' image (RGBA32) 230 | if ( getInfo()->QueryPaste ( src_format, src_x, src_y, src, x1, y1, x2, y2, 231 | GetFormat(), GetWidth(), GetHeight(), GetData(), 0, 0, 232 | src_start, src_end, src_wid, src_pitch, 233 | dest_start, dest_end, dest_wid, dest_pitch) ) { 234 | if ( src_format==ImageOp::BW8 ) { 235 | dest_bpr = getInfo()->GetBytesPerRow(); 236 | for (src = src_start, dest = dest_start; dest < dest_end;) { 237 | memcpy (dest, src, src_wid); 238 | src += GetBytesPerRow (); 239 | dest += dest_bpr; 240 | } 241 | } else if ( src_format==ImageOp::RGB24 ) { 242 | dest_bpr = getInfo()->GetBytesPerRow(); 243 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 244 | for ( ; dest < dest_row; ) { 245 | *dest++ = *src; 246 | src += 3; 247 | } 248 | dest_row += dest_bpr; 249 | dest += dest_pitch; 250 | src += src_pitch; 251 | } 252 | } else if ( src_format==ImageOp::RGBA32 ) { 253 | dest_bpr = getInfo()->GetBytesPerRow(); 254 | for (src = src_start+3, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 255 | for ( ; dest < dest_row; ) { 256 | *dest++ = *src; 257 | src += 4; 258 | } 259 | dest_row += dest_bpr; 260 | dest += dest_pitch; 261 | src += src_pitch; 262 | } 263 | } 264 | } 265 | } 266 | -------------------------------------------------------------------------------- /libmin/src/image_bw8.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "image.h" 3 | 4 | #include 5 | 6 | 7 | void Image::SetPixelBW8 (int x, int y, XBYTE r, XBYTE g, XBYTE b, XBYTE a) 8 | { 9 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 10 | XBYTE* pix = (XBYTE*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() ); 11 | *pix = r; 12 | // setNotify ( 1 ); // *** NOTIFY(1) 13 | } 14 | } 15 | void Image::GetPixelBW8 (int x, int y, XBYTE& r, XBYTE& g, XBYTE& b, XBYTE& a ) 16 | { 17 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 18 | XBYTE* pix = (XBYTE*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() ); 19 | r = *pix; 20 | g = b = r; 21 | a = 255; 22 | } 23 | } 24 | 25 | void Image::FillBW8 (XBYTE r, XBYTE g, XBYTE b, XBYTE a) 26 | { 27 | // Temporary fill buffer 28 | assert ( getInfo()->GetBytesPerRow() <= 16384 ); 29 | for (uint x=0; x < getInfo()->GetBytesPerRow();) 30 | fillbuf[x++] = r; 31 | 32 | XBYTE *dest_pix, *dest_pix_stop; 33 | dest_pix = (XBYTE*) GetData(); 34 | dest_pix_stop = dest_pix + getInfo()->GetSize(); 35 | for (; dest_pix < dest_pix_stop;) { 36 | memcpy (dest_pix, fillbuf, getInfo()->GetBytesPerRow()); 37 | dest_pix += getInfo()->GetBytesPerRow(); 38 | } 39 | // setNotify ( 1 ); // *** NOTIFY(1) 40 | } 41 | 42 | void Image::RemapBW8 ( unsigned int vmin, unsigned int vmax ) 43 | { 44 | XBYTE* src = (XBYTE*) GetData(); 45 | XBYTE* src_stop = src + (getInfo()->mXres*getInfo()->mYres); 46 | 47 | unsigned long mMin, mMax; 48 | mMin = ((unsigned long) 1 << 16)-1; 49 | mMax = 0; 50 | for (; src < src_stop; ) { 51 | if ( *src < mMin ) mMin = *src; 52 | if ( *src > mMax ) mMax = *src; 53 | src++; 54 | } 55 | if ( vmin == vmax ) return; 56 | 57 | src = (XBYTE*) GetData(); 58 | unsigned int vdelta = vmax-vmin; 59 | for (; src < src_stop; ) { 60 | *src = (XBYTE) ( float(vmin) + float(*src - mMin)*vdelta / (mMax-mMin) ); 61 | src++; 62 | } 63 | } 64 | 65 | // Reformat - Reformats given pixel format to 66 | // another pixel format 67 | void Image::ReformatBW8 ( ImageOp::Format eFormat ) 68 | { 69 | Image* new_img = new Image ( getInfo()->mXres, getInfo()->mYres, eFormat ); 70 | 71 | XBYTE* src = (XBYTE*) GetData(); 72 | XBYTE* src_stop = src + getInfo()->GetSize(); 73 | XBYTE* dest = new_img->GetData (); 74 | 75 | if ( eFormat==ImageOp::RGB24 ) { // Target format RGB24 76 | for (; src < src_stop; ) { 77 | *dest++ = *src; 78 | *dest++ = *src; 79 | *dest++ = *src++; 80 | } 81 | } else if ( eFormat==ImageOp::BGR24 ) { // Target format BGR24 82 | for (; src < src_stop; ) { 83 | *dest++ = *src; 84 | *dest++ = *src; 85 | *dest++ = *src++; 86 | } 87 | } else if ( eFormat==ImageOp::RGBA32 ) { // Target format RGBA32 88 | for (; src < src_stop; ) { 89 | *dest++ = *src; 90 | *dest++ = *src; 91 | *dest++ = *src++; 92 | *dest++ = 255; 93 | } 94 | } 95 | TransferFrom ( new_img ); 96 | delete ( new_img ); 97 | } 98 | 99 | // Paste - Pastes a portion of an image into another image. 100 | // Subselection: yes - Allows sub-selection of source 101 | // Cropping: yes - Allows cropping into target 102 | // Offsetting: yes - Allows offsetting into target 103 | // Scaling: no - Allows rescaling of source 104 | // Filtering: no - Allows filtering of source 105 | // Rotation: no - Allows rotation of source 106 | void Image::PasteBW8 ( int x1, int y1, int x2, int y2, int offx, int offy, XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 107 | { 108 | XBYTE *src, *src_start, *src_end; 109 | XBYTE *dest_start, *dest_end, *dest_row; 110 | int src_wid, src_pitch; 111 | int dest_wid, dest_pitch, dest_bpr; 112 | 113 | if ( getInfo()->QueryPaste ( GetFormat(), GetWidth(), GetHeight(), GetData(), x1, y1, x2, y2, 114 | dest_format, destx, desty, dest, offx, offy, 115 | src_start, src_end, src_wid, src_pitch, 116 | dest_start, dest_end, dest_wid, dest_pitch) ) { 117 | if ( dest_format==ImageOp::BW8 ) { 118 | dest_bpr = getInfo()->GetBytesPerRow(); 119 | for (src = src_start, dest = dest_start; dest < dest_end;) { 120 | memcpy (dest, src, src_wid); 121 | src += GetBytesPerRow (); 122 | dest += dest_bpr; 123 | } 124 | } else if ( dest_format==ImageOp::RGB24 ) { 125 | dest_bpr = getInfo()->GetBytesPerRow(); 126 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 127 | for ( ; dest < dest_row; ) { 128 | *dest++ = *src; 129 | *dest++ = *src; 130 | *dest++ = *src++; 131 | } 132 | dest_row += dest_bpr; 133 | dest += dest_pitch; 134 | src += src_pitch; 135 | } 136 | } else if ( dest_format==ImageOp::RGBA32 ) { 137 | dest_bpr = getInfo()->GetBytesPerRow(); 138 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 139 | for ( ; dest < dest_row; ) { 140 | *dest++ = *src; 141 | *dest++ = *src; 142 | *dest++ = *src++; 143 | *dest++ = 255; 144 | } 145 | dest_row += dest_bpr; 146 | dest += dest_pitch; 147 | src += src_pitch; 148 | } 149 | } 150 | } 151 | } 152 | 153 | // Scale - Rescales an image into another image 154 | // Subselection: no - Allows sub-selection of source 155 | // Cropping: no - Allows cropping into target 156 | // Offsetting: no - Allows offsetting into target 157 | // Scaling: yes - Allows rescaling of source 158 | // Filtering: yes - Allows filtering of source 159 | // Rotation: no - Allows rotation of source 160 | // Limitations: Rescaled size must match target buffer 161 | 162 | #define BPX 1 // bytes per pixel (fast define) 163 | 164 | void Image::ScaleBW8 ( XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 165 | { 166 | assert ( GetFormat()==dest_format ); 167 | 168 | // Limitation: Rescaled size must match target buffer 169 | int nx = destx; 170 | int ny = desty; 171 | 172 | // Source addressing 173 | XBYTE* src; // Current pixel in source 174 | XBYTE* src_row; // Current row in source (start of row) 175 | 176 | // Destination addressing 177 | int dest_bpr = GetBytesPerRow(destx,desty,dest_format); 178 | XBYTE* dest_rowend = dest + dest_bpr; // End of row in dest 179 | XBYTE* dest_end = dest + GetSize(destx,desty,dest_format); // End of entire image in dest 180 | double delta_x = double(getInfo()->mXres-2) / double(nx); // Filtering distances 181 | double delta_y = double(getInfo()->mYres-2) / double(ny); 182 | int iDiffX = int(delta_x*0.5); // Filtering deltas (in bytes of source data) 183 | int iDiffY = getInfo()->GetBytesPerRow(); 184 | float sx, sy; // Current pixel (with sub-pixel accuracy) 185 | 186 | sy = 1.0; 187 | for (; dest < dest_end;) { 188 | src_row = (XBYTE*) GetData() + int(sy) * getInfo()->GetBytesPerRow(); 189 | sx = 1.0; 190 | for (; dest < dest_rowend;) { 191 | src = src_row + (int) sx; 192 | // Filtering (Fast, poor quality but better than nothing) 193 | // BW 194 | *dest = *(src - iDiffX) >> 3; // left 195 | *dest += *(src) >> 1; // center 196 | *dest += *(src+iDiffX) >> 3; // right 197 | *dest += *(src-iDiffY) >> 3; // up 198 | *dest++ += *(src+iDiffY) >> 3; // down 199 | sx += (float) delta_x; 200 | } 201 | sy += (float) delta_y; 202 | dest_rowend += dest_bpr; 203 | } 204 | } 205 | 206 | // Alpha Paste - Copies alpha from another source 207 | void Image::AlphaBW8 ( int x1, int y1, int x2, int y2, XBYTE* src, ImageOp::Format src_format, int src_x, int src_y ) 208 | { 209 | XBYTE *src_start, *src_end; 210 | XBYTE *dest, *dest_start, *dest_end, *dest_row; 211 | int src_wid, src_pitch; 212 | int dest_wid, dest_pitch, dest_bpr; 213 | 214 | // Source is the alpha image (any format) 215 | // Dest is 'this' image (RGBA32) 216 | if ( getInfo()->QueryPaste ( src_format, src_x, src_y, src, x1, y1, x2, y2, 217 | GetFormat(), GetWidth(), GetHeight(), GetData(), 0, 0, 218 | src_start, src_end, src_wid, src_pitch, 219 | dest_start, dest_end, dest_wid, dest_pitch) ) { 220 | if ( src_format==ImageOp::BW8 ) { 221 | dest_bpr = getInfo()->GetBytesPerRow(); 222 | for (src = src_start, dest = dest_start; dest < dest_end;) { 223 | memcpy (dest, src, src_wid); 224 | src += GetBytesPerRow (); 225 | dest += dest_bpr; 226 | } 227 | } else if ( src_format==ImageOp::RGB24 ) { 228 | dest_bpr = getInfo()->GetBytesPerRow(); 229 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 230 | for ( ; dest < dest_row; ) { 231 | *dest++ = *src; 232 | src += 3; 233 | } 234 | dest_row += dest_bpr; 235 | dest += dest_pitch; 236 | src += src_pitch; 237 | } 238 | } else if ( src_format==ImageOp::RGBA32 ) { 239 | dest_bpr = getInfo()->GetBytesPerRow(); 240 | for (src = src_start+3, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 241 | for ( ; dest < dest_row; ) { 242 | *dest++ = *src; 243 | src += 4; 244 | } 245 | dest_row += dest_bpr; 246 | dest += dest_pitch; 247 | src += src_pitch; 248 | } 249 | } 250 | } 251 | } 252 | -------------------------------------------------------------------------------- /libmin/src/image_f8.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "image.h" 3 | 4 | #include 5 | 6 | 7 | void Image::SetPixelF8 (int x, int y, XBYTE r, XBYTE g, XBYTE b, XBYTE a) 8 | { 9 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 10 | float* pix = (float*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() ); 11 | *pix = r; 12 | // setNotify ( 1 ); // *** NOTIFY(1) 13 | } 14 | } 15 | 16 | 17 | void Image::GetPixelF8 (int x, int y, XBYTE& r, XBYTE& g, XBYTE& b, XBYTE& a ) 18 | { 19 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 20 | float* pix = (float*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() ); 21 | r = (XBYTE) *pix; 22 | g = b = r; 23 | a = 255; 24 | } 25 | } 26 | 27 | void Image::FillF8 (XBYTE r, XBYTE g, XBYTE b, XBYTE a) 28 | { 29 | // Temporary fill buffer 30 | assert ( getInfo()->GetBytesPerRow() <= 16384 ); 31 | for (uint x=0; x < getInfo()->GetBytesPerRow();) 32 | fillbuf[x++] = r; 33 | 34 | float* dest_pix, *dest_pix_stop; 35 | dest_pix = (float*) GetData(); 36 | dest_pix_stop = dest_pix + getInfo()->GetSize(); 37 | for (; dest_pix < dest_pix_stop;) { 38 | memcpy (dest_pix, fillbuf, getInfo()->GetBytesPerRow()); 39 | dest_pix += getInfo()->GetBytesPerRow(); 40 | } 41 | // setNotify ( 1 ); // *** NOTIFY(1) 42 | } 43 | 44 | void Image::RemapF8 ( unsigned int vmin, unsigned int vmax ) 45 | { 46 | float* src = (float*) GetData(); 47 | float* src_stop = src + (getInfo()->mXres*getInfo()->mYres); 48 | 49 | unsigned long mMin, mMax; 50 | mMin = ((unsigned long) 1 << 16)-1; 51 | mMax = 0; 52 | for (; src < src_stop; ) { 53 | if ( *src < mMin ) mMin = (unsigned long) *src; 54 | if ( *src > mMax ) mMax = (unsigned long) *src; 55 | src++; 56 | } 57 | if ( vmin == vmax ) return; 58 | 59 | src = (float*) GetData(); 60 | unsigned int vdelta = vmax-vmin; 61 | for (; src < src_stop; ) { 62 | *src = (XBYTE) ( float(vmin) + float(*src - mMin)*vdelta / (mMax-mMin) ); 63 | src++; 64 | } 65 | } 66 | 67 | // Reformat - Reformats given pixel format to 68 | // another pixel format 69 | void Image::ReformatF8 ( ImageOp::Format eFormat ) 70 | { 71 | Image* new_img = new Image ( getInfo()->mXres, getInfo()->mYres, eFormat ); 72 | 73 | XBYTE* src = (XBYTE*) GetData(); 74 | XBYTE* src_stop = src + getInfo()->GetSize(); 75 | XBYTE* dest = new_img->GetData (); 76 | 77 | if ( eFormat==ImageOp::RGB24 ) { // Target format RGB24 78 | for (; src < src_stop; ) { 79 | *dest++ = *src; 80 | *dest++ = *src; 81 | *dest++ = *src++; 82 | } 83 | } else if ( eFormat==ImageOp::BGR24 ) { // Target format BGR24 84 | for (; src < src_stop; ) { 85 | *dest++ = *src; 86 | *dest++ = *src; 87 | *dest++ = *src++; 88 | } 89 | } else if ( eFormat==ImageOp::RGBA32 ) { // Target format RGBA32 90 | for (; src < src_stop; ) { 91 | *dest++ = *src; 92 | *dest++ = *src; 93 | *dest++ = *src++; 94 | *dest++ = 255; 95 | } 96 | } 97 | TransferFrom ( new_img ); 98 | delete ( new_img ); 99 | } 100 | 101 | // Paste - Pastes a portion of an image into another image. 102 | // Subselection: yes - Allows sub-selection of source 103 | // Cropping: yes - Allows cropping into target 104 | // Offsetting: yes - Allows offsetting into target 105 | // Scaling: no - Allows rescaling of source 106 | // Filtering: no - Allows filtering of source 107 | // Rotation: no - Allows rotation of source 108 | void Image::PasteF8 ( int x1, int y1, int x2, int y2, int offx, int offy, XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 109 | { 110 | XBYTE *src, *src_start, *src_end; 111 | XBYTE *dest_start, *dest_end, *dest_row; 112 | int src_wid, src_pitch; 113 | int dest_wid, dest_pitch, dest_bpr; 114 | 115 | if ( getInfo()->QueryPaste ( GetFormat(), GetWidth(), GetHeight(), GetData(), x1, y1, x2, y2, 116 | dest_format, destx, desty, dest, offx, offy, 117 | src_start, src_end, src_wid, src_pitch, 118 | dest_start, dest_end, dest_wid, dest_pitch) ) { 119 | if ( dest_format==ImageOp::BW8 ) { 120 | dest_bpr = getInfo()->GetBytesPerRow(); 121 | for (src = src_start, dest = dest_start; dest < dest_end;) { 122 | memcpy (dest, src, src_wid); 123 | src += GetBytesPerRow (); 124 | dest += dest_bpr; 125 | } 126 | } else if ( dest_format==ImageOp::RGB24 ) { 127 | dest_bpr = getInfo()->GetBytesPerRow(); 128 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 129 | for ( ; dest < dest_row; ) { 130 | *dest++ = *src; 131 | *dest++ = *src; 132 | *dest++ = *src++; 133 | } 134 | dest_row += dest_bpr; 135 | dest += dest_pitch; 136 | src += src_pitch; 137 | } 138 | } else if ( dest_format==ImageOp::RGBA32 ) { 139 | dest_bpr = getInfo()->GetBytesPerRow(); 140 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 141 | for ( ; dest < dest_row; ) { 142 | *dest++ = *src; 143 | *dest++ = *src; 144 | *dest++ = *src++; 145 | *dest++ = 255; 146 | } 147 | dest_row += dest_bpr; 148 | dest += dest_pitch; 149 | src += src_pitch; 150 | } 151 | } 152 | } 153 | } 154 | 155 | // Scale - Rescales an image into another image 156 | // Subselection: no - Allows sub-selection of source 157 | // Cropping: no - Allows cropping into target 158 | // Offsetting: no - Allows offsetting into target 159 | // Scaling: yes - Allows rescaling of source 160 | // Filtering: yes - Allows filtering of source 161 | // Rotation: no - Allows rotation of source 162 | // Limitations: Rescaled size must match target buffer 163 | 164 | #define BPX 1 // bytes per pixel (fast define) 165 | 166 | void Image::ScaleF8 ( XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 167 | { 168 | assert ( GetFormat()==dest_format ); 169 | 170 | // Limitation: Rescaled size must match target buffer 171 | int nx = destx; 172 | int ny = desty; 173 | 174 | // Source addressing 175 | XBYTE* src; // Current pixel in source 176 | XBYTE* src_row; // Current row in source (start of row) 177 | 178 | // Destination addressing 179 | int dest_bpr = GetBytesPerRow(destx,desty,dest_format); 180 | XBYTE* dest_rowend = dest + dest_bpr; // End of row in dest 181 | XBYTE* dest_end = dest + GetSize(destx,desty,dest_format); // End of entire image in dest 182 | double delta_x = double(getInfo()->mXres-2) / double(nx); // Filtering distances 183 | double delta_y = double(getInfo()->mYres-2) / double(ny); 184 | int iDiffX = int(delta_x*0.5); // Filtering deltas (in bytes of source data) 185 | int iDiffY = getInfo()->GetBytesPerRow(); 186 | float sx, sy; // Current pixel (with sub-pixel accuracy) 187 | 188 | sy = 1.0; 189 | for (; dest < dest_end;) { 190 | src_row = (XBYTE*) GetData() + int(sy) * getInfo()->GetBytesPerRow(); 191 | sx = 1.0; 192 | for (; dest < dest_rowend;) { 193 | src = src_row + (int) sx; 194 | // Filtering (Fast, poor quality but better than nothing) 195 | // BW 196 | *dest = *(src - iDiffX) >> 3; // left 197 | *dest += *(src) >> 1; // center 198 | *dest += *(src+iDiffX) >> 3; // right 199 | *dest += *(src-iDiffY) >> 3; // up 200 | *dest++ += *(src+iDiffY) >> 3; // down 201 | sx += (float) delta_x; 202 | } 203 | sy += (float) delta_y; 204 | dest_rowend += dest_bpr; 205 | } 206 | } 207 | 208 | // Alpha Paste - Copies alpha from another source 209 | void Image::AlphaF8 ( int x1, int y1, int x2, int y2, XBYTE* src, ImageOp::Format src_format, int src_x, int src_y ) 210 | { 211 | XBYTE *src_start, *src_end; 212 | XBYTE *dest, *dest_start, *dest_end, *dest_row; 213 | int src_wid, src_pitch; 214 | int dest_wid, dest_pitch, dest_bpr; 215 | 216 | // Source is the alpha image (any format) 217 | // Dest is 'this' image (RGBA32) 218 | if ( getInfo()->QueryPaste ( src_format, src_x, src_y, src, x1, y1, x2, y2, 219 | GetFormat(), GetWidth(), GetHeight(), GetData(), 0, 0, 220 | src_start, src_end, src_wid, src_pitch, 221 | dest_start, dest_end, dest_wid, dest_pitch) ) { 222 | if ( src_format==ImageOp::BW8 ) { 223 | dest_bpr = getInfo()->GetBytesPerRow(); 224 | for (src = src_start, dest = dest_start; dest < dest_end;) { 225 | memcpy (dest, src, src_wid); 226 | src += GetBytesPerRow (); 227 | dest += dest_bpr; 228 | } 229 | } else if ( src_format==ImageOp::RGB24 ) { 230 | dest_bpr = getInfo()->GetBytesPerRow(); 231 | for (src = src_start, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 232 | for ( ; dest < dest_row; ) { 233 | *dest++ = *src; 234 | src += 3; 235 | } 236 | dest_row += dest_bpr; 237 | dest += dest_pitch; 238 | src += src_pitch; 239 | } 240 | } else if ( src_format==ImageOp::RGBA32 ) { 241 | dest_bpr = getInfo()->GetBytesPerRow(); 242 | for (src = src_start+3, dest = dest_start, dest_row = dest_start+dest_wid; dest < dest_end;) { 243 | for ( ; dest < dest_row; ) { 244 | *dest++ = *src; 245 | src += 4; 246 | } 247 | dest_row += dest_bpr; 248 | dest += dest_pitch; 249 | src += src_pitch; 250 | } 251 | } 252 | } 253 | } 254 | -------------------------------------------------------------------------------- /libmin/src/image_info.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "image_info.h" 3 | #include "datax.h" // for DT_ types 4 | 5 | ImageInfo::ImageInfo ( int xr, int yr, ImageOp::Format ef ) 6 | { 7 | SetFormat ( xr, yr, ef ); 8 | } 9 | ImageInfo::ImageInfo () 10 | { 11 | Reset (); 12 | } 13 | 14 | void ImageInfo::Reset () 15 | { 16 | mXres = 0; mYres = 0; 17 | eFlags = 0; 18 | } 19 | 20 | 21 | // Flags - options based on format 22 | unsigned int ImageInfo::GetFlags ( ImageOp::Format ef ) 23 | { 24 | // Format Color, Masked, Alpha, AMerg, Depth, DMerged, Indexed, Low, High, True, Channels 25 | // BW1 - - - - - - T - - - - 26 | // BW8 - - - - - - - T - - - 27 | // RGB8 T - - - - - T - - - - 28 | // RGB12 T - - - - - - T - - - 29 | // RGB16 T - - - - - - - T - - 30 | // RGB24 T - - - - - - - - T - 31 | // RGBA8 T - T T - - T - - - - 32 | // RGBA16 T - T T - - - T - - - 33 | // RGBA24 T - T T - - - - T - - 34 | // RGBA32 T - T T - - - - - T - 35 | unsigned int flags = 0; 36 | switch (ef) { 37 | case ImageOp::RGB24: case ImageOp::BGR24: { 38 | SetFlag ( flags, ImageOp::Color, true); 39 | SetFlag ( flags, ImageOp::Alpha, false); 40 | SetFlag ( flags, ImageOp::AlphaMerged, true); 41 | SetFlag ( flags, ImageOp::Depth, false); 42 | SetFlag ( flags, ImageOp::DepthMerged, true); 43 | SetFlag ( flags, ImageOp::Trueclr, true); 44 | } break; 45 | case ImageOp::RGBA32: { 46 | SetFlag ( flags, ImageOp::Color, true); 47 | SetFlag ( flags, ImageOp::Trueclr, true); 48 | SetFlag ( flags, ImageOp::Alpha, true); 49 | SetFlag ( flags, ImageOp::AlphaMerged, true); 50 | SetFlag ( flags, ImageOp::Depth, false); 51 | SetFlag ( flags, ImageOp::DepthMerged, true); 52 | } break; 53 | case ImageOp::BW8: { 54 | SetFlag ( flags, ImageOp::Color, false); 55 | SetFlag ( flags, ImageOp::Alpha, false); 56 | SetFlag ( flags, ImageOp::Lowclr, true); 57 | } break; 58 | case ImageOp::BW16: { 59 | SetFlag ( flags, ImageOp::Color, false); 60 | SetFlag ( flags, ImageOp::Alpha, false); 61 | SetFlag ( flags, ImageOp::Highclr, true); 62 | } break; 63 | case ImageOp::BW32: { 64 | SetFlag ( flags, ImageOp::Color, false); 65 | SetFlag ( flags, ImageOp::Alpha, false); 66 | SetFlag ( flags, ImageOp::Trueclr, true); 67 | } break; 68 | case ImageOp::F8: { 69 | SetFlag ( flags, ImageOp::Color, false); 70 | SetFlag ( flags, ImageOp::Alpha, false); 71 | SetFlag ( flags, ImageOp::AlphaMerged, true); 72 | SetFlag ( flags, ImageOp::Depth, false); 73 | SetFlag ( flags, ImageOp::DepthMerged, true); 74 | SetFlag ( flags, ImageOp::Trueclr, true); 75 | } break; 76 | } 77 | return flags; 78 | } 79 | 80 | // Bits per pixel - value depends on format 81 | unsigned char ImageInfo::GetBitsPerPix (ImageOp::Format ef) 82 | { 83 | switch (ef) { 84 | case ImageOp::RGB24: case ImageOp::BGR24: return 24; break; 85 | case ImageOp::RGBA32: return 32; break; 86 | case ImageOp::BW8: return 8; break; 87 | case ImageOp::BW16: return 16; break; 88 | case ImageOp::BW32: return 32; break; 89 | case ImageOp::F8: return 32; break; 90 | } 91 | return 0; 92 | } 93 | 94 | 95 | // Data type - value depends on format 96 | unsigned char ImageInfo::GetDataType (ImageOp::Format ef) 97 | { 98 | switch (ef) { 99 | case ImageOp::BW8: return DT_UCHAR; break; 100 | case ImageOp::RGB24: case ImageOp::BGR24: return DT_UCHAR3; break; 101 | case ImageOp::RGBA32: return DT_UCHAR4; break; 102 | case ImageOp::BW16: return DT_USHORT; break; 103 | case ImageOp::BW32: return DT_UINT; break; 104 | case ImageOp::F8: return DT_FLOAT; break; 105 | } 106 | return 0; 107 | } 108 | 109 | // Filtering Options 110 | void ImageInfo::SetFilter ( ImageOp::Filter ef ) 111 | { 112 | switch (ef) { 113 | case ImageOp::NoFilter: SetFlag ( eFlags, ImageOp::FilterLo, false ); SetFlag ( eFlags, ImageOp::FilterHi, false ); break; 114 | case ImageOp::Linear: SetFlag ( eFlags, ImageOp::FilterLo, true ); SetFlag ( eFlags, ImageOp::FilterHi, false ); break; 115 | case ImageOp::MipNoFilter: SetFlag ( eFlags, ImageOp::FilterLo, false ); SetFlag ( eFlags, ImageOp::FilterHi, true ); break; 116 | case ImageOp::MipLinear: SetFlag ( eFlags, ImageOp::FilterLo, true); SetFlag ( eFlags, ImageOp::FilterHi, true ); break; 117 | }; 118 | } 119 | 120 | ImageOp::Filter ImageInfo::GetFilter () 121 | { 122 | if (HasFlag (eFlags, ImageOp::FilterHi)) { 123 | // Mipmap 124 | if (HasFlag (eFlags, ImageOp::FilterLo)) return ImageOp::MipLinear; 125 | else return ImageOp::MipNoFilter; 126 | } else { 127 | // Standard 128 | if (HasFlag (eFlags, ImageOp::FilterLo)) return ImageOp::Linear; 129 | else return ImageOp::NoFilter; 130 | } 131 | } 132 | 133 | // Get smallest power greater than x 134 | int ImageInfo::QuerySmallestPower (int x) 135 | { 136 | int i = 1; 137 | for (; i < x; i <<= 1); 138 | return i; 139 | } 140 | 141 | bool ImageInfo::QueryClip ( int& sx1, int& sy1, int& sx2, int& sy2, int cx1, int cy1, int cx2, int cy2 ) 142 | { 143 | if (sx1 <= cx2 && sx2 >= cx1) { 144 | if (sy1 <= cy2 && sy2 >= cy1) { 145 | if (sx1 < cx1) sx1 = cx1; 146 | if (sx2 > cx2) sx2 = cx2; 147 | if (sy1 < cy1) sy1 = cy1; 148 | if (sy2 > cy2) sy2 = cy2; 149 | return true; 150 | } 151 | } 152 | return false; 153 | } 154 | 155 | void ImageInfo::QueryRect (ImageOp::Format f, int src_x, XBYTE* src, int sx1, int sy1, int sx2, int sy2, 156 | XBYTE*& src_start, XBYTE*& src_end, int& src_wid, int& src_pitch) 157 | { 158 | // First row of rectangle 159 | src_start = src + (sy1 * (unsigned long) src_x + sx1) * GetBytesPerPix ( f ); 160 | 161 | // Last row of rectangle 162 | src_end = src + (sy2 * (unsigned long) src_x + sx1) * GetBytesPerPix ( f ); 163 | 164 | // Width of rectangle (in bytes) 165 | src_wid = GetBytesPerPix ( f ) * (unsigned long) (sx2-sx1+1); // width of row (in bytes) 166 | 167 | // Pitch between rows (in bytes), from end of last span to beginning of next span 168 | src_pitch = GetBytesPerPix ( f ) * (unsigned long) src_x - src_wid; 169 | } 170 | 171 | // QueryPaste 172 | // Source is the image to be copies. Dest is the target paste area. 173 | // This functions does the following: 174 | // - Clips source rectangle to source image size (determine valid source sub-region) 175 | // - Clips dest rectangle to destination image size 176 | // - Computes source start/end offsets for rectangle (for any pixel format) 177 | // - Computes dest start/end offsets for rectangle (for any pixel format) 178 | bool ImageInfo::QueryPaste (ImageOp::Format src_f, int src_x, int src_y, XBYTE* src_dat, int sx1, int sy1, int sx2, int sy2, 179 | ImageOp::Format dest_f, int dest_x, int dest_y, XBYTE* dest_dat, int dx1, int dy1, 180 | XBYTE*& src_start, XBYTE*& src_end, int& src_wid, int& src_pitch, 181 | XBYTE*& dest_start, XBYTE*& dest_end, int& dest_wid, int& dest_pitch) 182 | { 183 | int sxl1 = sx1, syl1= sy1; 184 | if (QueryClip (sx1, sy1, sx2, sy2, 0, 0, src_x-1, src_y-1)) { 185 | dx1 += sx1 - sxl1; 186 | dy1 += sy1 - syl1; 187 | int dxl1 = dx1, dyl1 = dy1; 188 | int dx2 = dx1 + (sx2-sx1); 189 | int dy2 = dy1 + (sy2-sy1); 190 | if (QueryClip (dx1, dy1, dx2, dy2, 0, 0, dest_x-1, dest_y-1)) { 191 | sx1 += dx1 - dxl1; 192 | sy1 += dy1 - dyl1; 193 | sx2 = sx1 + (dx2 - dx1); 194 | sy2 = sy1 + (dy2 - dy1); 195 | QueryRect (src_f, src_x, src_dat, sx1, sy1, sx2, sy2, src_start, src_end, src_wid, src_pitch); 196 | QueryRect (dest_f, dest_x, dest_dat, dx1, dy1, dx2, dy2, dest_start, dest_end, dest_wid, dest_pitch); 197 | return true; 198 | } 199 | } 200 | return false; 201 | } 202 | 203 | void ImageInfo::QueryPowerSize (int &x, int &y) 204 | { 205 | x = QuerySmallestPower (mXres); 206 | y = QuerySmallestPower (mYres); 207 | } 208 | -------------------------------------------------------------------------------- /libmin/src/image_rgb24.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "image.h" 3 | 4 | #include 5 | 6 | void Image::SetPixelRGB24 (int x, int y, XBYTE r, XBYTE g, XBYTE b, XBYTE a) 7 | { 8 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 9 | XBYTE* pix = (XBYTE*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() ); 10 | *pix++ = r; 11 | *pix++ = g; 12 | *pix++ = b; 13 | // setNotify ( 1 ); // *** NOTIFY(1) 14 | } 15 | } 16 | void Image::GetPixelRGB24 (int x, int y, XBYTE& r, XBYTE& g, XBYTE& b, XBYTE& a ) 17 | { 18 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 19 | XBYTE* pix = (XBYTE*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() ); 20 | r = *pix++; 21 | g = *pix++; 22 | b = *pix++; 23 | a = 255; 24 | } 25 | } 26 | 27 | void Image::FillRGB24 (XBYTE r, XBYTE g, XBYTE b, XBYTE a) 28 | { 29 | // Temporary fill buffer 30 | assert ( getInfo()->GetBytesPerRow() <= 16384 ); 31 | for (uint x=0; x < getInfo()->GetBytesPerRow();) { 32 | fillbuf[x++] = r; 33 | fillbuf[x++] = g; 34 | fillbuf[x++] = b; 35 | } 36 | 37 | XBYTE *dest_pix, *dest_pix_stop; 38 | dest_pix = (XBYTE*) GetData(); 39 | dest_pix_stop = dest_pix + getInfo()->GetSize(); 40 | for (; dest_pix < dest_pix_stop;) { 41 | memcpy (dest_pix, fillbuf, getInfo()->GetBytesPerRow()); 42 | dest_pix += getInfo()->GetBytesPerRow(); 43 | } 44 | // setNotify ( 1 ); // *** NOTIFY(1) 45 | } 46 | 47 | 48 | void Image::RemapRGB24 ( unsigned int vmin, unsigned int vmax ) 49 | { 50 | XBYTE* src = (XBYTE*) GetData(); 51 | XBYTE* src_stop = src + (getInfo()->mXres*getInfo()->mYres); 52 | 53 | unsigned long mMin, mMax; 54 | mMin = ((unsigned long) 1 << 16)-1; 55 | mMax = 0; 56 | for (; src < src_stop; ) { 57 | if ( *src < mMin ) mMin = *src; 58 | if ( *src > mMax ) mMax = *src; 59 | src++; 60 | } 61 | if ( vmin == vmax ) return; 62 | 63 | src = (XBYTE*) GetData(); 64 | unsigned int vdelta = vmax-vmin; 65 | for (; src < src_stop; ) { 66 | *src = (XBYTE) ( float(vmin) + float(*src - mMin)*vdelta / (mMax-mMin) ); 67 | src++; 68 | } 69 | } 70 | 71 | 72 | // Reformat - Reformats given pixel format to 73 | // another pixel format 74 | void Image::ReformatRGB24 ( ImageOp::Format eFormat ) 75 | { 76 | Image* new_img = new Image ( getInfo()->mXres, getInfo()->mYres, eFormat ); 77 | 78 | XBYTE* src = (XBYTE*) GetData(); 79 | XBYTE* src_stop = src + getInfo()->GetSize(); 80 | XBYTE* dest = new_img->GetData (); 81 | 82 | if ( eFormat==ImageOp::RGBA32 ) { // Target format RGBA32 83 | for (; src < src_stop; ) { 84 | *dest++ = *src++; 85 | *dest++ = *src++; 86 | *dest++ = *src++; 87 | *dest++ = 255; 88 | } 89 | } else if ( eFormat==ImageOp::BGR24 ) { // Target format BGR24 90 | for (; src < src_stop; ) { 91 | *dest++ = *(src++ + 2); 92 | *dest++ = *src++; 93 | *dest++ = *(src++ - 2); 94 | } 95 | } else if ( eFormat==ImageOp::BW8 ) { // Target format BW8 96 | for (; src < src_stop; ) { 97 | *dest++ = (*src + int(*(src+1)) + *(src+2)) / 3 ; 98 | src += 3; 99 | } 100 | } 101 | TransferFrom ( new_img ); 102 | delete ( new_img ); 103 | } 104 | 105 | // Paste - Pastes a portion of an image into another image. 106 | // Subselection: yes - Allows sub-selection of source 107 | // Cropping: yes - Allows cropping into target 108 | // Offsetting: yes - Allows offsetting into target 109 | // Scaling: no - Allows rescaling of source 110 | // Filtering: no - Allows filtering of source 111 | // Rotation: no - Allows rotation of source 112 | void Image::PasteRGB24 ( int x1, int y1, int x2, int y2, int offx, int offy, XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 113 | { 114 | XBYTE *src, *src_start, *src_end; 115 | XBYTE *dest_start, *dest_end; 116 | int src_wid, src_pitch; 117 | int dest_wid, dest_pitch, dest_bpr; 118 | 119 | 120 | if ( dest_format==ImageOp::RGB24 ) { 121 | if ( getInfo()->QueryPaste ( GetFormat(), GetWidth(), GetHeight(), GetData(), x1, y1, x2, y2, 122 | dest_format, destx, desty, dest, offx, offy, 123 | src_start, src_end, src_wid, src_pitch, 124 | dest_start, dest_end, dest_wid, dest_pitch) ) 125 | { 126 | dest_bpr = GetBytesPerRow ( destx, desty, dest_format ); 127 | for (src = src_start, dest = dest_start; src < src_end;) { 128 | memcpy (dest, src, src_wid); 129 | src += GetBytesPerRow (); 130 | dest += dest_bpr; 131 | } 132 | } 133 | } 134 | } 135 | 136 | // Scale - Rescales an image into another image 137 | // Subselection: no - Allows sub-selection of source 138 | // Cropping: no - Allows cropping into target 139 | // Offsetting: no - Allows offsetting into target 140 | // Scaling: yes - Allows rescaling of source 141 | // Filtering: yes - Allows filtering of source 142 | // Rotation: no - Allows rotation of source 143 | // Limitations: Rescaled size must match target buffer 144 | void Image::ScaleRGB24 ( XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 145 | { 146 | assert ( GetFormat()==dest_format ); 147 | 148 | // Limitation: Rescaled size must match target buffer 149 | int nx = destx; 150 | int ny = desty; 151 | 152 | // Compute pixel addresses 153 | int dest_bpr = GetBytesPerRow(destx,desty,dest_format); 154 | XBYTE* src; // Current pixel in source 155 | XBYTE* src_row; // Current row in source (start of row) 156 | XBYTE* dest_rowend = dest + dest_bpr; // End of row in dest 157 | XBYTE* dest_end = dest + GetSize(destx,desty,dest_format); // End of entire image in dest 158 | double delta_x = double(getInfo()->mXres-2) / double(nx); // Filtering distances 159 | double delta_y = double(getInfo()->mYres-2) / double(ny); 160 | int iDiffX = int(delta_x*0.5) * 3; // Filtering deltas (in bytes) 161 | int iDiffY = getInfo()->GetBytesPerRow(); 162 | float sx, sy; // Current pixel (with sub-pixel accuracy) 163 | 164 | sy = 1.0; 165 | for (; dest < dest_end;) { 166 | src_row = (XBYTE*) GetData() + int(sy) * getInfo()->GetBytesPerRow(); 167 | sx = 1.0; 168 | for (; dest < dest_rowend;) { 169 | src = src_row + (int) sx*3; 170 | // Filtering (Fast, poor quality but better than nothing) 171 | // RED 172 | *dest = *(src - iDiffX) >> 3; // left 173 | *dest += *(src) >> 1; // center 174 | *dest += *(src+iDiffX) >> 3; // right 175 | *dest += *(src-iDiffY) >> 3; // up 176 | *dest++ += *(src+iDiffY) >> 3; // down 177 | src++; 178 | 179 | // GREEN 180 | *dest = *(src - iDiffX) >> 3; // left 181 | *dest += *(src) >> 1; // center 182 | *dest += *(src+iDiffX) >> 3; // right 183 | *dest += *(src-iDiffY) >> 3; // up 184 | *dest++ += *(src+iDiffY) >> 3; // down 185 | src++; 186 | 187 | // BLUE 188 | *dest = *(src - iDiffX) >> 3; // left 189 | *dest += *(src) >> 1; // center 190 | *dest += *(src+iDiffX) >> 3; // right 191 | *dest += *(src-iDiffY) >> 3; // up 192 | *dest++ += *(src+iDiffY) >> 3; // down 193 | 194 | sx += (float) delta_x; 195 | } 196 | sy += (float) delta_y; 197 | dest_rowend += dest_bpr; 198 | } 199 | } 200 | 201 | // Alpha Paste - Copies alpha from another source 202 | void Image::AlphaRGB24 ( int x1, int y1, int x2, int y2, XBYTE* src, ImageOp::Format src_format, int src_x, int src_y ) 203 | { 204 | // No alpha channel ! 205 | 206 | return; 207 | } 208 | 209 | -------------------------------------------------------------------------------- /libmin/src/image_rgba32.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include "image.h" 5 | 6 | void Image::SetPixelRGBA32 (int x, int y, XBYTE r, XBYTE g, XBYTE b, XBYTE a) 7 | { 8 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 9 | XBYTE* pix = (XBYTE*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() ); 10 | *pix++ = r; *pix++ = g; 11 | *pix++ = b; *pix++ = a; 12 | // setNotify ( 1 ); // *** NOTIFY(1) 13 | } 14 | } 15 | void Image::GetPixelRGBA32 (int x, int y, XBYTE& r, XBYTE& g, XBYTE& b, XBYTE& a ) 16 | { 17 | if ( x>=0 && y>=0 && x < getInfo()->mXres && y < getInfo()->mYres ) { 18 | XBYTE* pix = (XBYTE*) GetData() + ( (y * getInfo()->mXres + x) * getInfo()->GetBytesPerPix() ); 19 | r = *pix++; 20 | g = *pix++; 21 | b = *pix++; 22 | a = *pix++; 23 | } 24 | } 25 | 26 | void Image::FillRGBA32 (XBYTE r, XBYTE g, XBYTE b, XBYTE a) 27 | { 28 | // Temporary fill buffer 29 | assert ( getInfo()->GetBytesPerRow() <= 16384 ); 30 | for (uint x=0; x < getInfo()->GetBytesPerRow();) { 31 | fillbuf[x++] = r; 32 | fillbuf[x++] = g; 33 | fillbuf[x++] = b; 34 | fillbuf[x++] = a; 35 | } 36 | 37 | XBYTE *dest_pix, *dest_pix_stop; 38 | dest_pix = (XBYTE*) GetData(); 39 | dest_pix_stop = dest_pix + getInfo()->GetSize(); 40 | for (; dest_pix < dest_pix_stop;) { 41 | memcpy (dest_pix, fillbuf, getInfo()->GetBytesPerRow()); 42 | dest_pix += getInfo()->GetBytesPerRow(); 43 | } 44 | // setNotify ( 1 ); // *** NOTIFY(1) 45 | } 46 | 47 | void Image::RemapRGBA32 ( unsigned int vmin, unsigned int vmax ) 48 | { 49 | XBYTE* src = (XBYTE*) GetData(); 50 | XBYTE* src_stop = src + (getInfo()->mXres*getInfo()->mYres); 51 | 52 | unsigned long mMin, mMax; 53 | mMin = ( ((unsigned long) 1 ) << 16)-1; 54 | mMax = 0; 55 | for (; src < src_stop; ) { 56 | if ( *src < mMin ) mMin = *src; 57 | if ( *src > mMax ) mMax = *src; 58 | src++; 59 | } 60 | if ( vmin == vmax ) return; 61 | 62 | src = (XBYTE*) GetData(); 63 | unsigned int vdelta = vmax-vmin; 64 | for (; src < src_stop; ) { 65 | *src = (XBYTE) ( float(vmin) + float(*src - mMin)*vdelta / (mMax-mMin) ); 66 | src++; 67 | } 68 | } 69 | 70 | 71 | // Reformat - Reformats given pixel format to 72 | // another pixel format 73 | void Image::ReformatRGBA32 ( ImageOp::Format eFormat ) 74 | { 75 | Image* new_img = new Image ( getInfo()->mXres, getInfo()->mYres, eFormat ) ; 76 | 77 | XBYTE* src = (XBYTE*) GetData(); 78 | XBYTE* src_stop = src + getInfo()->GetSize(); 79 | XBYTE* dest = new_img->GetData (); 80 | 81 | if ( eFormat==ImageOp::RGB24 ) { // Target format RGB24 82 | for (; src < src_stop; ) { 83 | *dest++ = *src++; 84 | *dest++ = *src++; 85 | *dest++ = *src++; 86 | src++; 87 | } 88 | } else if ( eFormat==ImageOp::BGR24 ) { // Target format BGR24 89 | for (; src < src_stop; ) { 90 | *dest++ = *(src++ + 2); 91 | *dest++ = *src++; 92 | *dest++ = *(src++ - 2); 93 | src++; 94 | } 95 | } 96 | TransferFrom ( new_img ); 97 | delete ( new_img ); 98 | } 99 | 100 | // Paste - Pastes a portion of an image into another image. 101 | // Subselection: yes - Allows sub-selection of source 102 | // Cropping: yes - Allows cropping into target 103 | // Offsetting: yes - Allows offsetting into target 104 | // Scaling: no - Allows rescaling of source 105 | // Filtering: no - Allows filtering of source 106 | // Rotation: no - Allows rotation of source 107 | void Image::PasteRGBA32 ( int x1, int y1, int x2, int y2, int offx, int offy, XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 108 | { 109 | XBYTE *src, *src_start, *src_end; 110 | XBYTE *dest_start, *dest_end; 111 | int src_wid, src_pitch; 112 | int dest_wid, dest_pitch, dest_bpr; 113 | 114 | 115 | if ( dest_format==ImageOp::RGBA32 ) { 116 | if ( getInfo()->QueryPaste ( GetFormat(), GetWidth(), GetHeight(), GetData(), x1, y1, x2, y2, 117 | dest_format, destx, desty, dest, offx, offy, 118 | src_start, src_end, src_wid, src_pitch, 119 | dest_start, dest_end, dest_wid, dest_pitch) ) 120 | { 121 | dest_bpr = GetBytesPerRow ( destx, desty, dest_format ); 122 | for (src = src_start, dest = dest_start; src < src_end;) { 123 | memcpy (dest, src, src_wid); 124 | src += GetBytesPerRow (); 125 | dest += dest_bpr; 126 | } 127 | } 128 | } 129 | } 130 | 131 | // Scale - Rescales an image into another image 132 | // Subselection: no - Allows sub-selection of source 133 | // Cropping: no - Allows cropping into target 134 | // Offsetting: no - Allows offsetting into target 135 | // Scaling: yes - Allows rescaling of source 136 | // Filtering: yes - Allows filtering of source 137 | // Rotation: no - Allows rotation of source 138 | // Limitations: Rescaled size must match target buffer 139 | 140 | #define BPX 4 // bytes per pixel (fast define) 141 | 142 | void Image::ScaleRGBA32 ( XBYTE* dest, ImageOp::Format dest_format, int destx, int desty ) 143 | { 144 | assert ( GetFormat()==dest_format ); 145 | 146 | // Limitation: Rescaled size must match target buffer 147 | int nx = destx; 148 | int ny = desty; 149 | 150 | // Compute pixel addresses 151 | int dest_bpr = GetBytesPerRow(destx,desty,dest_format); 152 | XBYTE* src; // Current pixel in source 153 | XBYTE* src_row; // Current row in source (start of row) 154 | XBYTE* dest_rowend = dest + dest_bpr; // End of row in dest 155 | XBYTE* dest_end = dest + GetSize(destx,desty,dest_format); // End of entire image in dest 156 | double delta_x = double(getInfo()->mXres-2) / double(nx); // Filtering distances 157 | double delta_y = double(getInfo()->mYres-2) / double(ny); 158 | int iDiffX = int(delta_x*0.5) * BPX; // Filtering deltas (in bytes) 159 | int iDiffY = getInfo()->GetBytesPerRow(); 160 | float sx, sy; // Current pixel (with sub-pixel accuracy) 161 | 162 | sy = 1.0; 163 | for (; dest < dest_end;) { 164 | src_row = (XBYTE*) GetData() + int(sy) * getInfo()->GetBytesPerRow(); 165 | sx = 1.0; 166 | for (; dest < dest_rowend;) { 167 | src = src_row + (int) sx * BPX; 168 | // Filtering (Fast, poor quality but better than nothing) 169 | // RED 170 | *dest = *(src - iDiffX) >> 3; // left 171 | *dest += *(src) >> 1; // center 172 | *dest += *(src+iDiffX) >> 3; // right 173 | *dest += *(src-iDiffY) >> 3; // up 174 | *dest++ += *(src+iDiffY) >> 3; // down 175 | src++; 176 | 177 | // GREEN 178 | *dest = *(src - iDiffX) >> 3; // left 179 | *dest += *(src) >> 1; // center 180 | *dest += *(src+iDiffX) >> 3; // right 181 | *dest += *(src-iDiffY) >> 3; // up 182 | *dest++ += *(src+iDiffY) >> 3; // down 183 | src++; 184 | 185 | // BLUE 186 | *dest = *(src - iDiffX) >> 3; // left 187 | *dest += *(src) >> 1; // center 188 | *dest += *(src+iDiffX) >> 3; // right 189 | *dest += *(src-iDiffY) >> 3; // up 190 | *dest++ += *(src+iDiffY) >> 3; // down 191 | src++; 192 | 193 | // ALPHA 194 | *dest = *(src - iDiffX) >> 3; // left 195 | *dest += *(src) >> 1; // center 196 | *dest += *(src+iDiffX) >> 3; // right 197 | *dest += *(src-iDiffY) >> 3; // up 198 | *dest++ += *(src+iDiffY) >> 3; // down 199 | sx += (float) delta_x; 200 | } 201 | sy += (float) delta_y; 202 | dest_rowend += dest_bpr; 203 | } 204 | } 205 | 206 | 207 | // Alpha Paste - Copies alpha from another source 208 | void Image::AlphaRGBA32 ( int x1, int y1, int x2, int y2, XBYTE* src, ImageOp::Format src_format, int src_x, int src_y ) 209 | { 210 | XBYTE *src_start, *src_end; 211 | XBYTE *dest, *dest_start, *dest_end, *dest_row; 212 | int src_wid, src_pitch; 213 | int dest_wid, dest_pitch, dest_bpr; 214 | 215 | // Source is the alpha image (any format) 216 | // Dest is 'this' image (RGBA32) 217 | if ( getInfo()->QueryPaste ( src_format, src_x, src_y, src, x1, y1, x2, y2, 218 | GetFormat(), GetWidth(), GetHeight(), GetData(), 0, 0, 219 | src_start, src_end, src_wid, src_pitch, 220 | dest_start, dest_end, dest_wid, dest_pitch) ) { 221 | if ( src_format==ImageOp::BW8 ) { 222 | dest_bpr = getInfo()->GetBytesPerRow(); 223 | for (src = src_start, dest = dest_start+3, dest_row = dest_start+dest_wid; dest < dest_end;) { 224 | for ( ; dest < dest_row; ) { 225 | *dest = *src++; 226 | dest += 4; 227 | } 228 | dest_row += dest_bpr; 229 | dest += dest_pitch; 230 | src += src_pitch; 231 | } 232 | } else if ( src_format==ImageOp::RGB24 ) { 233 | dest_bpr = getInfo()->GetBytesPerRow(); 234 | for (src = src_start, dest = dest_start+3, dest_row = dest_start+dest_wid; dest < dest_end;) { 235 | for ( ; dest < dest_row; ) { 236 | *dest = *src; 237 | src += 3; 238 | dest += 4; 239 | } 240 | dest_row += dest_bpr; 241 | dest += dest_pitch; 242 | src += src_pitch; 243 | } 244 | } else if ( src_format==ImageOp::RGBA32 ) { 245 | dest_bpr = getInfo()->GetBytesPerRow(); 246 | for (src = src_start+3, dest = dest_start+3, dest_row = dest_start+dest_wid; dest < dest_end;) { 247 | for ( ; dest < dest_row; ) { 248 | *dest = *src; 249 | src += 4; 250 | dest += 4; 251 | } 252 | dest_row += dest_bpr; 253 | dest += dest_pitch; 254 | src += src_pitch; 255 | } 256 | } 257 | } 258 | } 259 | -------------------------------------------------------------------------------- /libmin/src/imageformat_generic.cpp: -------------------------------------------------------------------------------- 1 | //************************************** 2 | // 3 | // GENERIC Format 4 | // 5 | // NOTE: CURRENT CAPABILITIES: 6 | // - Uses OLE to load JPG and GIF foramts 7 | // 8 | //************************************** 9 | 10 | #include 11 | 12 | #include "imageformat_generic.h" 13 | 14 | //--------- MUST BE UPDATED 15 | /* 16 | bool CImageFormatGeneric::Load (char *filename, const Image* pOrigImg, Image*& pNewImg) 17 | { 18 | m_pOrigImage = (Image*) pOrigImg; 19 | m_pNewImage = 0x0; 20 | m_eStatus = FormatStatus::Loading; 21 | strcpy (m_Filename, filename); 22 | bool result = LoadGeneric ( filename ); 23 | pNewImg = m_pNewImage; 24 | return result; 25 | } 26 | 27 | bool CImageFormatGeneric::Save (char *filename, const Image* pOrigImg) 28 | { 29 | m_pOrigImage = (Image*) pOrigImg; 30 | m_eStatus = FormatStatus::Saving; 31 | strcpy (m_Filename, filename); 32 | return SaveGeneric ( filename ); 33 | } 34 | 35 | CImageFormatGeneric::CImageFormatGeneric () 36 | { 37 | } 38 | 39 | bool CImageFormatGeneric::LoadGeneric ( char *filename ) 40 | { 41 | IPicture* picture; 42 | IStream* stream; 43 | HGLOBAL hGlobal; 44 | FILE* file; 45 | 46 | // Open file to load 47 | file = fopen ( filename, "rb" ); // open file in read only mode 48 | if (file == NULL) { 49 | m_eStatus = FormatStatus::FileNotFound; 50 | return false; 51 | } 52 | 53 | // Allocates global memory same size as file to be loaded 54 | fseek ( file, 0, SEEK_END); 55 | int iFileSize = ftell ( file ); 56 | fseek ( file, 0, SEEK_SET); 57 | hGlobal = GlobalAlloc(GPTR, iFileSize); 58 | if ( hGlobal == NULL) { 59 | fclose ( file ); 60 | m_eStatus = FormatStatus::InvalidFile; 61 | return false; 62 | } 63 | // Read file data into global memory 64 | fread ( (void*) hGlobal, 1, iFileSize, file); 65 | fclose ( file ); 66 | 67 | // Create a stream on global memory 68 | CreateStreamOnHGlobal( hGlobal, false, &stream); 69 | if ( stream == NULL ) { 70 | GlobalFree ( hGlobal ); 71 | m_eStatus = FormatStatus::InvalidFile; 72 | return false; 73 | } 74 | 75 | // Decompress and load the JPG or GIF into Picture COM object 76 | OleLoadPicture( stream, 0, false, IID_IPicture, (void**)&picture ); 77 | if ( picture == NULL ) { 78 | stream->Release(); 79 | GlobalFree( hGlobal ); 80 | m_eStatus = FormatStatus::InvalidFile; 81 | return false; 82 | } 83 | 84 | // Release the stream 85 | stream->Release(); 86 | 87 | // Free the global memory 88 | GlobalFree( hGlobal ); 89 | 90 | // Get a Windows bitmap from the Picture COM object 91 | HBITMAP hSrcBitmap = 0; 92 | picture->get_Handle( (unsigned int*)&hSrcBitmap ); 93 | if ( hSrcBitmap == 0 ) { 94 | m_eStatus = FormatStatus::InvalidFile; 95 | return false; 96 | } 97 | 98 | // Copy Windows bitmap into a new one (why?) 99 | HBITMAP hDestBitmap = (HBITMAP) CopyImage ( hSrcBitmap, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG); 100 | if ( hDestBitmap == 0) { 101 | m_eStatus = FormatStatus::InvalidFile; 102 | return false; 103 | } 104 | 105 | // Success up to this point! 106 | // Release the Picture COM object 107 | picture->Release(); 108 | 109 | // Transfer Windows bitmap into ImageX 110 | // (note: Format status will be set by TransferBitmap) 111 | bool bSuccess = TransferBitmap ( hDestBitmap ); 112 | 113 | // Delete Windows bitmaps 114 | if ( hSrcBitmap ) DeleteObject ( hSrcBitmap ); 115 | if ( hDestBitmap ) DeleteObject ( hDestBitmap ); 116 | 117 | return bSuccess; 118 | } 119 | 120 | bool CImageFormatGeneric::SaveGeneric ( char *filename ) 121 | { 122 | // Not implemented. 123 | m_eStatus = FormatStatus::NotImplemented; 124 | return false; 125 | } 126 | */ 127 | -------------------------------------------------------------------------------- /libmin/src/imageformat_png.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | //************************************** 4 | // 5 | // PNG Formats 6 | // 7 | //************************************** 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "common_defs.h" 16 | #include "image.h" 17 | #include "imageformat_png.h" 18 | 19 | //#include "pnginfo.h" // for PNG 1.5 20 | #include "file_png.h" 21 | 22 | bool CImageFormatPng::Load ( char *filename, Image* pImg ) 23 | { 24 | StartLoad ( filename, pImg ); 25 | bool result = LoadPng ( filename ); 26 | if (result) FinishLoad (); 27 | return result; 28 | } 29 | 30 | bool CImageFormatPng::Save (char *filename, Image* pImg) 31 | { 32 | m_pOrigImage = pImg; 33 | m_eStatus = ImageOp::Saving; 34 | #ifdef WIN32 35 | strcpy_s (m_Filename, FILE_NAMELEN, filename); 36 | #else 37 | strcpy (m_Filename, filename); 38 | #endif 39 | return SavePng ( filename ); 40 | } 41 | 42 | 43 | /* -- FOR PNG 1.5 44 | typedef jmp_buf* (*png_set_longjmp_fnPtr)(png_structp png_ptr, png_longjmp_ptr longjmp_fn, size_t jmp_buf_size); 45 | png_set_longjmp_fnPtr mypng_set_longjmp_fnPtr = 0; 46 | 47 | extern "C" { 48 | jmp_buf* png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn, size_t jmp_buf_size) 49 | { 50 | if (mypng_set_longjmp_fnPtr) { return (*mypng_set_longjmp_fnPtr)(png_ptr, longjmp_fn, jmp_buf_size); } 51 | return 0; 52 | } 53 | }*/ 54 | 55 | bool CImageFormatPng::LoadPng (char *filename ) 56 | { 57 | bool bGrey = false; 58 | 59 | //-------------------------- Using PNG helpers 60 | std::vector out; 61 | unsigned int nx, ny; 62 | unsigned error = lodepng::decode( out, nx, ny, filename, (bGrey ? LCT_GREY : LCT_RGBA), (bGrey ? 16 : 8)); 63 | 64 | if (error) { 65 | //nvprintf("png read error: %s\n", lodepng_error_text(error)); 66 | return false; 67 | } 68 | 69 | // Determine output parameters 70 | char color_type = 6; 71 | ImageOp::Format eNewFormat; 72 | switch (color_type) { 73 | case 0: eNewFormat = ImageOp::BW8; break; 74 | case 2: eNewFormat = ImageOp::RGB24; break; 75 | case 6: eNewFormat = ImageOp::RGBA32; break; 76 | default: 77 | m_eStatus = ImageOp::DepthNotSupported; return false; 78 | break; 79 | } 80 | // Allocate new image space or use existing one 81 | CreateImage(m_pNewImage, nx, ny, eNewFormat); 82 | 83 | int stride = m_pNewImage->GetBytesPerRow(); 84 | XBYTE *dest = GetData(m_pNewImage); 85 | for (uint y = 0; y < ny; y++) 86 | memcpy(dest + y*stride, &out[y*stride], stride); 87 | 88 | 89 | /* 90 | //-------------------------- Using libpng library 91 | png_byte color_type; 92 | png_byte bit_depth; 93 | png_structp png_ptr; 94 | png_infop info_ptr; 95 | int number_of_passes; 96 | png_bytep * row_pointers; 97 | 98 | // open file 99 | #ifdef WIN32 100 | fopen_s ( &m_png_file, filename, "rb"); 101 | #else 102 | m_png_file = fopen ( filename, "rb"); 103 | #endif 104 | 105 | // Error opening (does not exist, etc.) 106 | if ( m_png_file == 0x0 ) { 107 | m_eStatus = ImageOp::FileNotFound; 108 | return false; 109 | } 110 | 111 | // check for png header 112 | char header[8]; // 8 is the maximum size that can be checked 113 | fread(header, 1, 8, m_png_file ); 114 | if (png_sig_cmp( (png_bytep) header, 0, 8 ) ) { 115 | m_eStatus = ImageOp::InvalidFile; 116 | return false; 117 | } 118 | // initialize stuff 119 | png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); 120 | if (!png_ptr) { m_eStatus = ImageOp::LibVersion; return false; } 121 | 122 | // Set custom error handlers 123 | png_set_error_fn ( png_ptr, png_get_error_ptr(png_ptr), user_error_fn, user_warning_fn ); 124 | 125 | info_ptr = png_create_info_struct( png_ptr ); 126 | if ( !info_ptr ) { 127 | png_destroy_read_struct(&png_ptr, NULL, NULL); 128 | m_eStatus = ImageOp::LibVersion; 129 | return false; 130 | } 131 | 132 | if ( setjmp( png_jmpbuf(png_ptr) ) ) { 133 | png_destroy_read_struct ( &png_ptr, &info_ptr, NULL ); 134 | m_eStatus = ImageOp::LibVersion; 135 | return false; 136 | } 137 | png_init_io(png_ptr, m_png_file ); 138 | png_set_sig_bytes(png_ptr, 8); 139 | png_read_info(png_ptr, info_ptr); 140 | png_set_strip_16(png_ptr); // STRIP 16-bit down to 8-bit 141 | 142 | if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) // EXPAND PALETTES TO RGB 143 | png_set_palette_to_rgb(png_ptr); 144 | 145 | number_of_passes = png_set_interlace_handling(png_ptr); 146 | 147 | png_read_update_info(png_ptr, info_ptr); 148 | 149 | // read file 150 | if (setjmp(png_jmpbuf(png_ptr))) { m_eStatus = ImageOp::InvalidFile; return false; } 151 | 152 | int row_bytes = png_get_rowbytes(png_ptr, info_ptr); 153 | png_bytep row = (png_bytep) png_malloc ( png_ptr, row_bytes ); // Allocate memory in PNG memory space 154 | for (int y=0; y < ny; y++ ) { 155 | png_read_row ( png_ptr, row, NULL ); 156 | memcpy ( dest + y*row_bytes, row, row_bytes ); // Copy data from PNG memory to Luna Image 157 | } 158 | png_free ( png_ptr, row ); // Free PNG memory 159 | 160 | // Close image file 161 | fclose (m_png_file);*/ 162 | 163 | 164 | return true; 165 | } 166 | 167 | bool CImageFormatPng::SavePng (char *filename) 168 | { 169 | int ch = 3; 170 | 171 | switch ( GetFormat ( m_pOrigImage ) ) { 172 | case ImageOp::BW8: ch = 1; break; 173 | case ImageOp::BW16: ch = 1; break; 174 | case ImageOp::BW32: ch = 1; break; 175 | case ImageOp::RGB8: ch = 3; break; 176 | case ImageOp::RGBA32: ch = 4; break; 177 | }; 178 | 179 | save_png ( filename, GetData(m_pOrigImage), GetWidth(m_pOrigImage), GetHeight(m_pOrigImage), ch ); 180 | return true; 181 | 182 | 183 | /* 184 | //----------------------------- Using PNG library 185 | // Open file for output 186 | FILE* png_file; 187 | fopen_s ( &png_file, filename, "wb" ); 188 | if ( png_file == NULL) { 189 | m_eStatus = ImageOp::InvalidFile; 190 | return false; 191 | } 192 | 193 | // initialize stuff 194 | png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 195 | 196 | if (!png_ptr) { 197 | m_eStatus = ImageOp::InvalidFile; 198 | return false; 199 | } 200 | 201 | info_ptr = png_create_info_struct(png_ptr); 202 | if (!info_ptr) { 203 | m_eStatus = ImageOp::InvalidFile; 204 | return false; 205 | } 206 | 207 | if (setjmp(png_jmpbuf(png_ptr))) { 208 | m_eStatus = ImageOp::InvalidFile; 209 | return false; 210 | } 211 | 212 | png_init_io( png_ptr, png_file ); 213 | 214 | int width = m_pOrigImage->GetWidth(); 215 | int height = m_pOrigImage->GetHeight(); 216 | png_byte bit_depth = 8; 217 | png_byte color_type = PNG_COLOR_TYPE_RGB; 218 | png_set_IHDR( png_ptr, info_ptr, width, height, 219 | bit_depth, color_type, PNG_INTERLACE_NONE, 220 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); 221 | 222 | png_write_info(png_ptr, info_ptr); 223 | 224 | // write row data 225 | XBYTE *src = GetData ( m_pOrigImage ); 226 | int row_bytes = png_get_rowbytes(png_ptr, info_ptr); 227 | png_bytep row = (png_bytep) png_malloc( png_ptr, row_bytes ); // Allocate memory in PNG memory space 228 | for (int y=0; y < height; y++) { 229 | memcpy ( row, src + (height-1-y)*row_bytes, row_bytes ); // Copy data Luna Image to PNG memory 230 | png_write_row ( png_ptr, row ); 231 | } 232 | png_free ( png_ptr, row ); // Free PNG memory 233 | 234 | png_write_end( png_ptr, NULL ); 235 | 236 | if ( info_ptr != NULL ) png_free_data ( png_ptr, info_ptr, PNG_FREE_ALL, -1 ); 237 | if ( png_ptr != NULL ) png_destroy_write_struct ( &png_ptr, (png_infopp) NULL ); 238 | 239 | fclose( png_file ); 240 | */ 241 | } 242 | 243 | -------------------------------------------------------------------------------- /libmin/src/imageformat_tga.cpp: -------------------------------------------------------------------------------- 1 | //************************************** 2 | // 3 | // TGA Format 4 | // 5 | //************************************** 6 | 7 | #include "imageformat_tga.h" 8 | 9 | bool CImageFormatTga::Load ( char *filename, Image* pImg ) 10 | { 11 | StartLoad ( filename, pImg ); 12 | bool result = LoadTga ( filename ); 13 | if (result) FinishLoad (); 14 | return result; 15 | } 16 | 17 | bool CImageFormatTga::Save (char *filename, Image* pImg) 18 | { 19 | m_pOrigImage = pImg; 20 | m_pNewImage = 0x0; 21 | m_eStatus = ImageOp::Saving; 22 | strcpy (m_Filename, filename); 23 | //return SaveTga (filename); 24 | return false; 25 | } 26 | 27 | 28 | unsigned char *CImageFormatTga::getRGBA( FILE *s, unsigned char* rgba, int size ) 29 | { 30 | // Read in RGBA data for a 32bit image. 31 | unsigned char temp; 32 | int bread; 33 | int i; 34 | 35 | if( rgba == NULL ) 36 | return 0; 37 | 38 | bread = (int) fread( rgba, sizeof (unsigned char), size, s ); 39 | 40 | // TGA is stored in BGRA, make it RGBA 41 | if( bread != size ) 42 | { 43 | free( rgba ); 44 | return 0; 45 | } 46 | 47 | for( i = 0; i < size; i += 4 ) 48 | { 49 | temp = rgba[i]; 50 | rgba[i] = rgba[i + 2]; 51 | rgba[i + 2] = temp; 52 | } 53 | 54 | return rgba; 55 | } 56 | 57 | unsigned char *CImageFormatTga::getRGB( FILE *s, unsigned char* rgb, int size ) 58 | { 59 | // Read in RGB data for a 24bit image. 60 | unsigned char temp; 61 | int bread; 62 | int i; 63 | 64 | if( rgb == NULL ) 65 | return 0; 66 | 67 | //printf ( 'tga ', INFO, "%p %p %d\n", s, rgb, size ); 68 | 69 | bread = (int) fread( rgb, sizeof (unsigned char), size, s ); 70 | 71 | if(bread != size) 72 | { 73 | free( rgb ); 74 | return 0; 75 | } 76 | 77 | // TGA is stored in BGR, make it RGB 78 | for( i = 0; i < size; i += 3 ) 79 | { 80 | temp = rgb[i]; 81 | rgb[i] = rgb[i + 2]; 82 | rgb[i + 2] = temp; 83 | } 84 | 85 | return rgb; 86 | } 87 | 88 | unsigned char *CImageFormatTga::getGray( FILE *s, unsigned char* grayData, int size ) 89 | { 90 | // Gets the grayscale image data. Used as an alpha channel. 91 | int bread; 92 | 93 | if( grayData == NULL ) 94 | return 0; 95 | 96 | bread = (int)fread( grayData, sizeof (unsigned char), size, s ); 97 | 98 | if( bread != size ) 99 | { 100 | free( grayData ); 101 | return 0; 102 | } 103 | return grayData; 104 | } 105 | 106 | void CImageFormatTga::writeRGBA( FILE *s, const unsigned char *externalImage, int size ) 107 | { 108 | // Read in RGBA data for a 32bit image. 109 | unsigned char *rgba; 110 | int bread; 111 | int i; 112 | 113 | rgba = (unsigned char *) malloc( size * 4 ); 114 | 115 | // switch RGBA to BGRA 116 | for( i = 0; i < size * 4; i += 4 ) 117 | { 118 | rgba[i + 0] = externalImage[i + 2]; 119 | rgba[i + 1] = externalImage[i + 1]; 120 | rgba[i + 2] = externalImage[i + 0]; 121 | rgba[i + 3] = externalImage[i + 3]; 122 | } 123 | 124 | bread = (int) fwrite( rgba, sizeof (unsigned char), size * 4, s ); 125 | free( rgba ); 126 | } 127 | 128 | void CImageFormatTga::writeRGB( FILE *s, const unsigned char *externalImage, int size ) 129 | { 130 | // Read in RGBA data for a 32bit image. 131 | unsigned char *rgb; 132 | int bread; 133 | int i; 134 | 135 | rgb = (unsigned char *)malloc( size * 3 ); 136 | 137 | // switch RGB to BGR 138 | for( i = 0; i < size * 3; i += 3 ) 139 | { 140 | rgb[i + 0] = externalImage[i + 2]; 141 | rgb[i + 1] = externalImage[i + 1]; 142 | rgb[i + 2] = externalImage[i + 0]; 143 | } 144 | 145 | bread = (int)fwrite( rgb, sizeof (unsigned char), size * 3, s ); 146 | free( rgb ); 147 | } 148 | 149 | void CImageFormatTga::writeGrayAsRGB( FILE *s, const unsigned char *externalImage, int size ) 150 | { 151 | // Read in RGBA data for a 32bit image. 152 | unsigned char *rgb; 153 | int bread; 154 | int i; 155 | 156 | rgb = (unsigned char *)malloc( size * 3 ); 157 | 158 | // switch RGB to BGR 159 | int j = 0; 160 | for( i = 0; i < size * 3; i += 3, j++ ) 161 | { 162 | rgb[i + 0] = externalImage[j]; 163 | rgb[i + 1] = externalImage[j]; 164 | rgb[i + 2] = externalImage[j]; 165 | } 166 | 167 | bread = (int)fwrite( rgb, sizeof (unsigned char), size * 3, s ); 168 | free( rgb ); 169 | } 170 | 171 | void CImageFormatTga::writeGray( FILE *s, const unsigned char *externalImage, int size ) 172 | { 173 | // Gets the grayscale image data. Used as an alpha channel. 174 | int bread; 175 | 176 | bread = (int)fwrite( externalImage, sizeof (unsigned char), size, s ); 177 | } 178 | 179 | 180 | 181 | bool CImageFormatTga::LoadTga ( char* filename ) 182 | { 183 | // Loads up a targa file. Supported types are 8, 24 and 32 184 | // uncompressed images. 185 | unsigned char type[4]; 186 | unsigned char info[7]; 187 | FILE *tga = NULL; 188 | int size = 0; 189 | 190 | if( !(tga = fopen( filename, "rb" )) ) { 191 | m_eStatus = ImageOp::FileNotFound; 192 | return false; 193 | } 194 | 195 | fread( &type, sizeof (char), 3, tga ); // Read in colormap info and image type, byte 0 ignored 196 | fseek( tga, 12, SEEK_SET); // Seek past the header and useless info 197 | fread( &info, sizeof (char), 6, tga ); 198 | 199 | if( type[1] != 0 || (type[2] != 2 && type[2] != 3) ) { 200 | m_eStatus = ImageOp::InvalidFile; 201 | return false; 202 | } 203 | 204 | m_Xres = info[0] + info[1] * 256; 205 | m_Yres = info[2] + info[3] * 256; 206 | m_BitsPerPixel = info[4]; 207 | 208 | // Make sure we are loading a supported type 209 | if( m_BitsPerPixel != 32 && m_BitsPerPixel != 24 && m_BitsPerPixel != 8 ) { 210 | m_eStatus = ImageOp::InvalidFile; 211 | return false; 212 | } 213 | ImageOp::Format eNewFormat; 214 | 215 | size = m_Xres * m_Yres; 216 | 217 | switch ( m_BitsPerPixel ) { 218 | case 32: eNewFormat = ImageOp::RGBA32; size *= 4; break; 219 | case 24: eNewFormat = ImageOp::RGB24; size *= 3; break; 220 | case 8: eNewFormat = ImageOp::RGB24; break; 221 | }; 222 | 223 | // Allocate image 224 | CreateImage ( m_pNewImage, m_Xres, m_Yres, eNewFormat ); 225 | unsigned char* buf = GetData ( m_pNewImage ); 226 | if ( buf == 0x0 ) { 227 | printf ( "TGA Error: No new image data created.\n" ); 228 | } 229 | 230 | // Read data 231 | //printf ( "Load TGA: %d x %d x %d, %d\n", m_Xres, m_Yres, m_BitsPerPixel, size ); 232 | switch ( m_BitsPerPixel ) { 233 | case 32: getRGBA( tga, buf, size ); break; 234 | case 24: getRGB( tga, buf, size ); break; 235 | case 8: getGray( tga, buf, size ); break; 236 | }; 237 | 238 | fclose( tga ); 239 | 240 | return true; 241 | } 242 | 243 | 244 | /* 245 | TGA::TGAError ImageFormatTga::saveFromExternalData( const char *name, int w, int h, TGA::TGAFormat fmt, const unsigned char *externalImage ) 246 | { 247 | static unsigned char type[] = {0,0,2}; 248 | static unsigned char dummy[] = {0,0,0,0,0,0,0,0,0}; 249 | static unsigned char info[] = {0,0,0,0,0,0}; 250 | FILE *s = NULL; 251 | int size = 0; 252 | 253 | if( !(s = fopen( name, "wb" )) ) 254 | return TGA_FILE_NOT_FOUND; 255 | 256 | fwrite( type, sizeof (char), 3, s ); // Read in colormap info and image type, byte 0 ignored 257 | fwrite( dummy, sizeof (char), 9, s ); // Read in colormap info and image type, byte 0 ignored 258 | 259 | info[0] = w & 0xFF; 260 | info[1] = (w>>8) & 0xFF; 261 | info[2] = h & 0xFF; 262 | info[3] = (h>>8) & 0xFF; 263 | switch(fmt) 264 | { 265 | case ALPHA: 266 | info[4] = 8; 267 | break; 268 | case RGB: 269 | info[4] = 24; 270 | break; 271 | case RGBA: 272 | info[4] = 32; 273 | break; 274 | } 275 | fwrite( info, sizeof (char), 6, s ); 276 | 277 | size = w*h; 278 | switch(fmt) 279 | { 280 | case ALPHA: 281 | writeGray(s, externalImage, size); 282 | break; 283 | case RGB: 284 | //writeGrayAsRGB(s, externalImage, size); 285 | break; 286 | case RGBA: 287 | writeRGBA(s, externalImage, size); 288 | break; 289 | } 290 | 291 | fclose( s ); 292 | 293 | return TGA_NO_ERROR; 294 | }*/ 295 | 296 | 297 | -------------------------------------------------------------------------------- /math_voxelizer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | set(PROJNAME math_voxelizer) 3 | Project(${PROJNAME}) 4 | Message(STATUS "-------------------------------") 5 | Message(STATUS "Processing Project ${PROJNAME}:") 6 | 7 | ##################################################################################### 8 | # Bootstrap 9 | # 10 | set( BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) 11 | find_path ( CMAKE_HELPERS_PATH "HelpersBootstrap.cmake" HINTS ${BASE_DIRECTORY}/../libmin/cmake/ ) 12 | if ( ${CMAKE_HELPERS_PATH} STREQUAL "HELPERS-NOTFOUND" ) 13 | message ( FATAL_ERROR "\n Please set the CMAKE_HELPERS_PATH to location of HelpersBootstrap.cmake" ) 14 | endif() 15 | include( ${CMAKE_HELPERS_PATH}/HelpersBootstrap.cmake ) # Cross-Platform functions 16 | include_directories ("${CMAKE_CURRENT_SOURCE_DIR}/src") 17 | include_directories ("${CMAKE_CURRENT_SOURCE_DIR}/include") 18 | 19 | ##################################################################################### 20 | # Options 21 | 22 | _REQUIRE_MAIN() 23 | 24 | #--- symbols in release mode 25 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi" CACHE STRING "" FORCE) 26 | set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF" CACHE STRING "" FORCE) 27 | 28 | #--- OpenGL 29 | OPTION (BUILD_OPENGL "Build with OpenGL" ON) 30 | if (BUILD_OPENGL) 31 | find_package(OpenGL) 32 | add_definitions(-DUSE_OPENGL) # Use OpenGL 33 | IF (WIN32) 34 | LIST(APPEND LIBRARIES_OPTIMIZED "opengl32.lib" ) 35 | LIST(APPEND LIBRARIES_DEBUG "opengl32.lib" ) 36 | ENDIF() 37 | add_definitions(-DGLEW_STATIC) 38 | file(GLOB COMMON_SOURCE_FILES "${LIBMIN_INC_DIR}/glew.c" ) # Use GLEW (static) 39 | message ( "GLEW: ${COMMON_SOURCE_FILES}" ) 40 | endif() 41 | 42 | ##################################################################################### 43 | # Include LIBMIN 44 | # 45 | find_package(Libmin) 46 | 47 | if (LIBMIN_FOUND) 48 | add_definitions(-DUSE_LIBMIN) 49 | include_directories(${LIBMIN_INC_DIR}) 50 | include_directories(${LIBRARIES_INC_DIR}) 51 | LIST( APPEND LIBRARIES_OPTIMIZED "${LIBMIN_LIB_DIR}/${LIBMIN_REL}") 52 | LIST( APPEND LIBRARIES_DEBUG "${LIBMIN_LIB_DIR}/${LIBMIN_DEBUG}") 53 | _EXPANDLIST( OUTPUT PACKAGE_DLLS SOURCE ${LIBMIN_LIB_DIR} FILES ${LIBMIN_DLLS} ) 54 | message ( STATUS " ---> Using LIBMIN") 55 | endif() 56 | 57 | ###################### 58 | # CMAKE_INSTALL_PREFIX -- path where library will be installed to 59 | 60 | if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) 61 | if ( WIN32 ) 62 | get_filename_component ( _instpath "${CMAKE_CURRENT_BINARY_DIR}" REALPATH ) 63 | else() 64 | get_filename_component ( _instpath "/usr/local/shapes" REALPATH ) 65 | endif() 66 | set ( CMAKE_INSTALL_PREFIX ${_instpath} CACHE PATH "default install path" FORCE) 67 | endif() 68 | 69 | ##################################################################################### 70 | # Asset Path 71 | # 72 | if ( NOT DEFINED ASSET_PATH ) 73 | get_filename_component ( _assets "${BASE_DIRECTORY}/assets" REALPATH ) 74 | set ( ASSET_PATH ${_assets} CACHE PATH "Full path to /assets" ) 75 | add_definitions( -DASSET_PATH="${ASSET_PATH}/" ) 76 | endif() 77 | file(GLOB GLSL_FILES ${ASSET_PATH}/*.glsl ) 78 | add_definitions(-DASSET_PATH="${ASSET_PATH}/") 79 | 80 | ##################################################################################### 81 | # Executable 82 | # 83 | file(GLOB MAIN_FILES *.cpp *.c *.h ) 84 | 85 | unset ( ALL_SOURCE_FILES ) 86 | 87 | list( APPEND ALL_SOURCE_FILES ${MAIN_FILES} ) 88 | list( APPEND ALL_SOURCE_FILES ${COMMON_SOURCE_FILES} ) 89 | list( APPEND ALL_SOURCE_FILES ${PACKAGE_SOURCE_FILES} ) 90 | list( APPEND ALL_SOURCE_FILES ${UTIL_SOURCE_FILES} ) 91 | 92 | if ( NOT DEFINED WIN32 ) 93 | set(libdeps GL GLEW X11) 94 | LIST(APPEND LIBRARIES_OPTIMIZED ${libdeps}) 95 | LIST(APPEND LIBRARIES_DEBUG ${libdeps}) 96 | ENDIF() 97 | include_directories ("${CMAKE_CURRENT_SOURCE_DIR}") 98 | 99 | add_executable (${PROJNAME} ${ALL_SOURCE_FILES} ${GLSL_FILES} ) 100 | 101 | set_property ( TARGET ${PROJNAME} APPEND PROPERTY DEPENDS ) 102 | 103 | #--- debug and release exe 104 | set ( CMAKE_DEBUG_POSTFIX "d" CACHE STRING "" ) 105 | set_target_properties( ${PROJNAME} PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}) 106 | 107 | ##################################################################################### 108 | # Additional Libraries 109 | # 110 | _LINK ( PROJECT ${PROJNAME} OPT ${LIBRARIES_OPTIMIZED} DEBUG ${LIBRARIES_DEBUG} PLATFORM ${PLATFORM_LIBRARIES} ) 111 | 112 | ##################################################################################### 113 | # Windows specific 114 | # 115 | _MSVC_PROPERTIES() 116 | source_group("Source Files" FILES ${MAIN_FILES} ${COMMON_SOURCE_FILES} ${PACKAGE_SOURCE_FILES}) 117 | 118 | ##################################################################################### 119 | # Install Binaries 120 | # 121 | # 122 | _DEFAULT_INSTALL_PATH() 123 | 124 | # *NOTE*: file COPY is at cmake-time, not compile-time. Need to replace with add_custom_command -E copy_directory (my own _COPY) 125 | 126 | file (COPY "${CMAKE_CURRENT_SOURCE_DIR}/assets" DESTINATION ${EXECUTABLE_OUTPUT_PATH} ) # assets folder 127 | _INSTALL ( FILES ${SHADERS} DESTINATION "${EXECUTABLE_OUTPUT_PATH}/assets" ) # shaders 128 | _INSTALL ( FILES ${PACKAGE_DLLS} DESTINATION ${EXECUTABLE_OUTPUT_PATH} ) # DLLs 129 | install ( FILES $ DESTINATION ${EXECUTABLE_OUTPUT_PATH} OPTIONAL ) # PDB 130 | 131 | install ( FILES ${INSTALL_LIST} DESTINATION ${EXECUTABLE_OUTPUT_PATH} ) # exe, pdb 132 | 133 | ########################### 134 | # Done 135 | message ( STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}" ) 136 | message ( STATUS "CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}" ) 137 | message ( STATUS "EXECUTABLE_OUTPUT_PATH: ${EXECUTABLE_OUTPUT_PATH}" ) 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /math_voxelizer/assets/arial_256.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ramakarl/voxelizer/f1f2e56133bca41ca967ec933bb62298b9c71bf7/math_voxelizer/assets/arial_256.bin -------------------------------------------------------------------------------- /math_voxelizer/assets/arial_256.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ramakarl/voxelizer/f1f2e56133bca41ca967ec933bb62298b9c71bf7/math_voxelizer/assets/arial_256.tga -------------------------------------------------------------------------------- /voxelizer.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ramakarl/voxelizer/f1f2e56133bca41ca967ec933bb62298b9c71bf7/voxelizer.jpg --------------------------------------------------------------------------------