Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/grayscale #12

Merged
merged 15 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install deps
run: |
sudo apt-get update -y -qq
sudo apt install libsdl1.2-dev libsdl-image1.2-dev libsdl2-dev
libsdl2-image-dev
- name: CMake the project
run: cmake .
- name: Make the projet
Expand Down
42 changes: 34 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@ cmake_minimum_required(VERSION 3.16)
project(CText C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")


find_package(SDL2 REQUIRED)
find_package(SDL REQUIRED)
find_package(SDL2_image REQUIRED)

include_directories(
${SDL2_INCLUDE_DIRS}
${SDL_INCLUDE_DIRS}
${SDL2_IMAGE_INCLUDE_DIRS}
)

add_executable(
CText src/main.c
CText src/neuralnetwork/main.c
src/neuralnetwork/neuron.c src/neuralnetwork/neuron.h
src/neuralnetwork/function.c src/neuralnetwork/function.h
src/neuralnetwork/layer.c src/neuralnetwork/layer.h
src/util.c src/util.h
src/display/pixel_operations.c src/display/pixel_operations.h
src/display/display.c src/display/display.h
src/preprocessing/grayscale.c src/preprocessing/grayscale.h
src/preprocessing/otsu.c src/preprocessing/otsu.h
)

add_executable(
Expand All @@ -21,17 +37,27 @@ add_executable(
src/test/test_method.c src/test/test_method.h
)

add_executable(
CText-display src/preprocessing/main.c
src/display/pixel_operations.c src/display/pixel_operations.h
src/display/display.c src/display/display.h
src/preprocessing/grayscale.c src/preprocessing/grayscale.h
src/preprocessing/otsu.c src/preprocessing/otsu.h
)

add_executable(
CText-seg
src/segmentation/segmentation.c src/segmentation/segmentation.h
)

target_link_libraries(CText m)
target_link_libraries(CText m ${SDL_LIBRARY} ${SDL2_LIBRARY} ${SDL2_IMAGE_LIBRARIES})
target_link_libraries(CText-display m ${SDL_LIBRARY} ${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARIES})
target_link_libraries(CText-tests m)

if ( CMAKE_COMPILER_IS_GNUCC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow -Wdouble-promotion -Wformat=2 -Wformat-truncation -Wformat-overflow -Wundef -fno-common -Wconversion")
endif()
if ( MSVC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
if (CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow -Wdouble-promotion -Wformat=2 -Wformat-truncation -Wformat-overflow -Wundef -fno-common -Wconversion")
endif ()
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif ()
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ git clone https://github.com/C-Text/CText.git
gcc
cmake
make
sdl
sdl2
sdl2_image

= Build =
cmake .
Expand Down
100 changes: 100 additions & 0 deletions cmake/FindSDL2_image.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

#.rst:
# FindSDL2_image
# -------------
#
# Locate SDL2_image library
#
# This module defines:
#
# ::
#
# SDL2_IMAGE_LIBRARIES, the name of the library to link against
# SDL2_IMAGE_INCLUDE_DIRS, where to find the headers
# SDL2_IMAGE_FOUND, if false, do not try to link against
# SDL2_IMAGE_VERSION_STRING - human-readable string containing the
# version of SDL2_image
#
#
#
# For backward compatibility the following variables are also set:
#
# ::
#
# SDL2IMAGE_LIBRARY (same value as SDL2_IMAGE_LIBRARIES)
# SDL2IMAGE_INCLUDE_DIR (same value as SDL2_IMAGE_INCLUDE_DIRS)
# SDL2IMAGE_FOUND (same value as SDL2_IMAGE_FOUND)
#
#
#
# $SDLDIR is an environment variable that would correspond to the
# ./configure --prefix=$SDLDIR used in building SDL.
#
# Created by Eric Wing. This was influenced by the FindSDL.cmake
# module, but with modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).

if(NOT SDL2_IMAGE_INCLUDE_DIR AND SDL2IMAGE_INCLUDE_DIR)
set(SDL2_IMAGE_INCLUDE_DIR ${SDL2IMAGE_INCLUDE_DIR} CACHE PATH "directory cache entry initialized from old variable name")
endif()
find_path(SDL2_IMAGE_INCLUDE_DIR SDL_image.h
HINTS
ENV SDL2IMAGEDIR
ENV SDL2DIR
${SDL2_DIR}
PATH_SUFFIXES SDL2
# path suffixes to search inside ENV{SDL2DIR}
include/SDL2 include
)

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(VC_LIB_PATH_SUFFIX lib/x64)
else()
set(VC_LIB_PATH_SUFFIX lib/x86)
endif()

if(NOT SDL2_IMAGE_LIBRARY AND SDL2IMAGE_LIBRARY)
set(SDL2_IMAGE_LIBRARY ${SDL2IMAGE_LIBRARY} CACHE FILEPATH "file cache entry initialized from old variable name")
endif()
find_library(SDL2_IMAGE_LIBRARY
NAMES SDL2_image
HINTS
ENV SDL2IMAGEDIR
ENV SDL2DIR
${SDL2_DIR}
PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
)

if(SDL2_IMAGE_INCLUDE_DIR AND EXISTS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h")
file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h" SDL2_IMAGE_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL2_IMAGE_MAJOR_VERSION[ \t]+[0-9]+$")
file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h" SDL2_IMAGE_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL2_IMAGE_MINOR_VERSION[ \t]+[0-9]+$")
file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h" SDL2_IMAGE_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL2_IMAGE_PATCHLEVEL[ \t]+[0-9]+$")
string(REGEX REPLACE "^#define[ \t]+SDL2_IMAGE_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MAJOR "${SDL2_IMAGE_VERSION_MAJOR_LINE}")
string(REGEX REPLACE "^#define[ \t]+SDL2_IMAGE_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MINOR "${SDL2_IMAGE_VERSION_MINOR_LINE}")
string(REGEX REPLACE "^#define[ \t]+SDL2_IMAGE_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_PATCH "${SDL2_IMAGE_VERSION_PATCH_LINE}")
set(SDL2_IMAGE_VERSION_STRING ${SDL2_IMAGE_VERSION_MAJOR}.${SDL2_IMAGE_VERSION_MINOR}.${SDL2_IMAGE_VERSION_PATCH})
unset(SDL2_IMAGE_VERSION_MAJOR_LINE)
unset(SDL2_IMAGE_VERSION_MINOR_LINE)
unset(SDL2_IMAGE_VERSION_PATCH_LINE)
unset(SDL2_IMAGE_VERSION_MAJOR)
unset(SDL2_IMAGE_VERSION_MINOR)
unset(SDL2_IMAGE_VERSION_PATCH)
endif()

set(SDL2_IMAGE_LIBRARIES ${SDL2_IMAGE_LIBRARY})
set(SDL2_IMAGE_INCLUDE_DIRS ${SDL2_IMAGE_INCLUDE_DIR})

include(FindPackageHandleStandardArgs)

FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_image
REQUIRED_VARS SDL2_IMAGE_LIBRARIES SDL2_IMAGE_INCLUDE_DIRS
VERSION_VAR SDL2_IMAGE_VERSION_STRING)

# for backward compatibility
set(SDL2IMAGE_LIBRARY ${SDL2_IMAGE_LIBRARIES})
set(SDL2IMAGE_INCLUDE_DIR ${SDL2_IMAGE_INCLUDE_DIRS})
set(SDL2IMAGE_FOUND ${SDL2_IMAGE_FOUND})

mark_as_advanced(SDL2_IMAGE_LIBRARY SDL2_IMAGE_INCLUDE_DIR)
84 changes: 84 additions & 0 deletions src/display/display.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <err.h>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"


/**
*Author: Lise Giraud
*Date: 20/10/2020
*/

/**
*Init only the video part.
*
*@throw error message if it fail.
*/
void init_sdl() {
if (SDL_Init(SDL_INIT_VIDEO) == -1)
errx(1, "Could not initialize SDL: %s.\n", SDL_GetError());
}

/**
*Load an image from a given path using SDL_image with format detection.
*
*@throw error if there is no image at the given path.
*@param path: the path of the image to display.
*
*@return image found at the given path.
*/
SDL_Surface *load_image(char *path) {
SDL_Surface *img;

img = IMG_Load(path);
if (!img)
errx(3, "can't load %s: %s", path, IMG_GetError());

return img;
}

/**
*Displays a given image in a window.
*
*@throw error if screen is null (cannot set video mode).
*@param img the image to display in a window.
*
@return screen: a displayed image in a window.
*/
SDL_Surface *display_image(SDL_Surface *img) {
SDL_Surface *screen;

// Set the window to the same size as the image
screen = SDL_SetVideoMode(img->w, img->h, 0, SDL_SWSURFACE | SDL_ANYFORMAT);
if (screen == NULL) {
// error management
errx(1, "Couldn't set %dx%d video mode: %s\n",
img->w, img->h, SDL_GetError());
}

// Blit onto the screen surface
if (SDL_BlitSurface(img, NULL, screen, NULL) < 0)
warnx("BlitSurface error: %s\n", SDL_GetError());

// Update the screen
SDL_UpdateRect(screen, 0, 0, img->w, img->h);

// return the screen for further uses
return screen;
}

/**
*Wait for a key of the keyboard to be pressed before executing any action.
*/
void wait_for_keypressed() {
SDL_Event event;

// Wait for a key to be down.
do {
SDL_PollEvent(&event);
} while (event.type != SDL_KEYDOWN);

// Wait for a key to be up.
do {
SDL_PollEvent(&event);
} while (event.type != SDL_KEYUP);
}
41 changes: 41 additions & 0 deletions src/display/display.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
*Author: Lise Giraud
*Date: 20/10/2020
*File's name: display.h
*/
#ifndef CTEXT_SRC_DISPLAY_DISPLAY_H_
#define CTEXT_SRC_DISPLAY_DISPLAY_H_

#include <SDL/SDL.h>
/**
*Init only the video part.
*@throw error message if it fail.
*/
void init_sdl();

/**
*Load an image from a given path using SDL_image with format detection.
*
*@throw error if there is no image at the given path.
*@param path: the path of the image to display.
*
*@return image found at the given path.
*/
SDL_Surface *load_image(char *path);

/**
*Displays a given image in a window.
*
*@throw error if screen is null (cannot set video mode).
*@param img the image to display in a window.
*
@return screen: a displayed image in a window.
*/
SDL_Surface *display_image(SDL_Surface *img);

/**
*Wait for a key of the keyboard to be pressed before executing any action.
*/
void wait_for_keypressed();

#endif //CTEXT_SRC_DISPLAY_DISPLAY_H_
Loading