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

AM-64 Add support of JPG files reading #70

Merged
merged 8 commits into from
Apr 3, 2024
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
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get -y update && apt-get install -y

# Install the Clang compiler
RUN apt-get -y install git g++ cmake python3 valgrind gcovr libsqlite3-dev
RUN apt-get -y install git g++ cmake python3 valgrind gcovr libsqlite3-dev libjpeg-dev
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install packages
run: sudo apt -y install git g++ cmake libsqlite3-dev gcovr
run: sudo apt -y install git g++ cmake libsqlite3-dev gcovr libjpeg-dev
- name: Clone aquamarine repo
run: git clone https://github.com/MaksymT17/aquamarine.git
run: git clone --branch ${{ github.head_ref }} https://github.com/MaksymT17/aquamarine.git
- name: Build artifacts with Cmake
run: |
cd aquamarine
cd aquamarine &&
./build.sh

- name: Run unit test
run: |
cd aquamarine/unit_tests
./prepare_build.sh
cd aquamarine/unit_tests &&
./prepare_build.sh &&
cd build &&
make -j8 &&
./aquamarine_ut --gtest_output="xml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: Windows build check
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build-windows:
Expand All @@ -13,14 +11,23 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Clone aquamarine repo
run: git clone https://github.com/MaksymT17/aquamarine.git
run: git clone --branch ${{ github.head_ref }} https://github.com/MaksymT17/aquamarine.git

- name: Install packages
run: |
choco install sqlite --version=3.8.1
choco install libjpeg-turbo
shell: bash

- name: Build artifacts with Cmake
run: |
cd aquamarine
./build.sh
shell: bash

- name: Run unit tests
run: |
cd aquamarine/unit_tests
./prepare_build.sh
./make_and_run_tests.sh
./make_and_run_tests.sh
shell: bash
1 change: 1 addition & 0 deletions AmApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "analyze/algorithm/ImagePair.h"
#include <sstream>

#include <jpeglib.h>
namespace am
{
using namespace common::types;
Expand Down
4 changes: 2 additions & 2 deletions AmApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "extraction/BmpExtractor.h"
#include "configuration/ConfigurationReader.hpp"
#include "common/Logger.hpp"
#include "extraction/MultipleBmpExtractor.h"
#include "extraction/MultipleExtractor.h"
#include "database/DataBaseCommunicator.h"

namespace am
Expand All @@ -21,7 +21,7 @@ namespace am

private:
std::shared_ptr<am::common::Logger> loggerPtr;
extraction::MultipleBmpExtractor extractor;
extraction::MultipleExtractor extractor;
std::unique_ptr<analyze::algorithm::ObjectDetector> detector;

std::string base_img_path;
Expand Down
12 changes: 7 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 2.6)
cmake_minimum_required (VERSION 3.0)

project (aquamarine)

Expand All @@ -11,8 +11,9 @@ add_compile_options(-fno-rtti)

find_package (Threads)
find_package (SQLite3)
find_package (JPEG REQUIRED)

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${SQLite3_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${SQLite3_INCLUDE_DIRS} ${JPEG_INCLUDE_DIR})

set(AM_SOURCE_FILES
analyze/AffinityComparer.cpp
Expand All @@ -26,15 +27,16 @@ set(AM_SOURCE_FILES
analyze/algorithm/ImagePair.cpp
analyze/algorithm/movement/MovementDetector.cpp
extraction/BmpExtractor.cpp
extraction/MultipleBmpExtractor.cpp
extraction/JpgExtractor.cpp
extraction/MultipleExtractor.cpp
common/Timers.hpp
database/DataBaseCommunicator.cpp
AmApi.cpp
)

add_library(aquamarine_lib STATIC ${AM_SOURCE_FILES} )
add_library(aquamarine_lib STATIC ${AM_SOURCE_FILES} ${JPEG_LIBRARIES})

add_executable(aquamarine main.cpp)

target_link_libraries (aquamarine aquamarine_lib ${CMAKE_THREAD_LIBS_INIT} ${SQLite3_LIBRARIES})
target_link_libraries (aquamarine aquamarine_lib ${CMAKE_THREAD_LIBS_INIT} ${SQLite3_LIBRARIES} ${JPEG_LIBRARIES} )

2 changes: 1 addition & 1 deletion analyze/algorithm/ObjectBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace am::analyze::algorithm
class ObjectBase
{
public:
explicit ObjectBase(const size_t row, const size_t col) : mPixelsCount(1),
explicit ObjectBase(const size_t row, const size_t col) : mPixelsCount(0),
mLeft(col),
mMin_height(row),
mRight(col),
Expand Down
1 change: 1 addition & 0 deletions analyze/algorithm/ObjectRectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace am::analyze::algorithm
ObjectRectangle::ObjectRectangle(const size_t row, const size_t col)
: ObjectBase(row, col)
{
addPixel(row,col);
}

void ObjectRectangle::addPixel(const size_t row, const size_t col)
Expand Down
11 changes: 11 additions & 0 deletions common/exceptions/WrongFormatException.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include "common/exceptions/AmException.hpp"

namespace am::common::exceptions
{
class WrongFormatException : public AmException
{
public:
WrongFormatException(std::string &msg) : AmException(msg) {}
};
} // namespace am::common::exceptions
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

namespace am::extraction
{
class IMultipleBmpExtractor
class IMultipleExtractor
{
public:
~IMultipleBmpExtractor() = default;
~IMultipleExtractor() = default;

virtual std::vector<common::types::Matrix<common::types::Color24b>> readFiles(std::vector<std::string> &&fileNames) = 0;
};
Expand Down
51 changes: 51 additions & 0 deletions extraction/JpgExtractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#include "JpgExtractor.h"
#include "common/exceptions/FileAccessException.hpp"
#include "common/exceptions/AllocationException.hpp"
#include <future>
#include <vector>
#include <jpeglib.h>

namespace am::extraction
{
using namespace common::types;

Matrix<Color24b> JpgExtractor::readFile(const std::string &fileName)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;

FILE *infile = fopen(fileName.c_str(), "rb");
if (!infile)
{
std::string errMsg = "File '" + fileName + "' could not be found!";
throw common::exceptions::FileAccessException(errMsg);
}

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);

JSAMPLE* tempdata = new JSAMPLE[cinfo.output_width * cinfo.output_components];
Matrix<Color24b> data(cinfo.output_width, cinfo.output_height);
size_t rowid =0;
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, &tempdata, 1);
for (int j = 0; j < cinfo.output_width; j++)
{
//printf("[%d]c:%d_%d_%d ", j, tempdata[j*3],tempdata[j*3+1], tempdata[j*3+2] );
data(rowid, j) = {tempdata[j*cinfo.output_components],tempdata[j*cinfo.output_components+1], tempdata[j*cinfo.output_components+2]};
}
//printf("\n");
rowid++;
}
delete tempdata;
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return data;
}
} // namespace am::extraction
19 changes: 19 additions & 0 deletions extraction/JpgExtractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "common/types/Matrix.hpp"
#include "common/types/Color24b.hpp"
#include <string>

namespace am::extraction
{
/// JpgExtractor class for RGB data extraction from JPG/JPEG file
class JpgExtractor
{
public:
JpgExtractor() = default;
~JpgExtractor() = default;

static common::types::Matrix<common::types::Color24b> readFile(const std::string &filePath);
};

}
34 changes: 0 additions & 34 deletions extraction/MultipleBmpExtractor.cpp

This file was deleted.

55 changes: 55 additions & 0 deletions extraction/MultipleExtractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

#include "MultipleExtractor.h"
#include <future>
#include "BmpExtractor.h"
#include "JpgExtractor.h"
#include "common/types/Color24b.hpp"

#include <algorithm>
#include "common/exceptions/WrongFormatException.hpp"

namespace am
{
namespace extraction
{
using namespace common::types;

MultipleExtractor::MultipleExtractor(std::shared_ptr<am::common::Logger> &logger)
: mLogger(logger)
{
}

std::vector<Matrix<Color24b>> MultipleExtractor::readFiles(std::vector<std::string> &&fileNames)
{
std::vector<Matrix<Color24b>> result;
result.reserve(fileNames.size());
std::vector<std::future<Matrix<Color24b>>> futures;

for (size_t i = 0; i < fileNames.size(); ++i)
{
std::string file_ext = fileNames[i].substr(fileNames[i].find_last_of(".") + 1);
std::transform(file_ext.begin(), file_ext.end(), file_ext.begin(),
[](unsigned char c)
{ return std::tolower(c); });
if(file_ext =="jpg" || file_ext =="jpeg" || file_ext =="jpe")
futures.emplace_back(std::async(std::launch::deferred, JpgExtractor::readFile, fileNames[i]));
else if(file_ext =="bmp")
futures.emplace_back(std::async(std::launch::deferred, BmpExtractor::readFile, fileNames[i]));
else{
std::string errorMsg("WrongFormatException on data extraction from file(allowed jpeg/bmp)! File: ");
errorMsg.append(fileNames[i]);
throw am::common::exceptions::WrongFormatException(errorMsg);
}


mLogger->info("Reading of file:%s has been added in extraction queue.", fileNames[i].c_str());
}

for (auto &e : futures)
{
result.emplace_back(e.get());
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
#include "common/types/Matrix.hpp"
#include "common/types/Color24b.hpp"
#include "common/Logger.hpp"
#include "IMultipleBmpExtractor.h"
#include "IMultipleExtractor.h"

namespace am::extraction
{
// class for multiple reading files, given vector of fieNames will be
// fullfilled as return value with relative data from files
// Usage of async calls can reduce extraction time.
class MultipleBmpExtractor : public IMultipleBmpExtractor
class MultipleExtractor : public IMultipleExtractor
{

public:
explicit MultipleBmpExtractor(std::shared_ptr<am::common::Logger> &logger);
~MultipleBmpExtractor() = default;
MultipleExtractor(std::shared_ptr<am::common::Logger> &logger);
~MultipleExtractor() = default;

// fill up the Matrices for each file provided in the input parameter
virtual std::vector<common::types::Matrix<common::types::Color24b>> readFiles(std::vector<std::string> &&fileNames) override;

private:
std::shared_ptr<am::common::Logger> mLogger;
};
} // namespace am::extraction
}
Binary file added inputs/10x10_2obj.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/10x10_clean.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/_DSC4097.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inputs/_DSC4098.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions inputs/configuration.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Affinity_Threshold: 75
Minimum_Pixels_In_Object: 3
Pixel_Step: 2
Minimum_Pixels_In_Object: 1
Pixel_Step: 1
Calculation_Time_Limit: 50
Idle_Timeout: 5
Threads_Multiplier: 10.0
Loading
Loading