Skip to content

Commit

Permalink
view images
Browse files Browse the repository at this point in the history
  • Loading branch information
Like4Schnitzel committed Feb 27, 2024
1 parent 56e5558 commit 96c563e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ target_link_libraries( img-viewer stdc++fs ${OpenCV_LIBS} )
add_executable (
media-viewer media-viewer/main.cpp media-viewer/mediaviewer.cpp
decoder/vtdidecoder.cpp
img-viewer/imgviewer.cpp
libs/termutils.cpp libs/variousutils.cpp libs/binaryutils.cpp libs/_kbhit.cpp
)
target_link_libraries( media-viewer stdc++fs ${OpenCV_LIBS} )
20 changes: 17 additions & 3 deletions src/media-viewer/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <ctype.h>
#include "mediaviewer.hpp"
#include "../libs/variousutils.hpp"
#include "../libs/termutils.hpp"
Expand All @@ -22,6 +23,7 @@ int main(int argc, char** argv)
return 0;
}

bool viewing = false;
TermUtils tu;
int fileIndex = 0;
cout << "Selected file: " << mv.current()->path << "\n" << flush;
Expand All @@ -34,16 +36,28 @@ int main(int argc, char** argv)
if (kp.keyDown)
{
// right arrow
if (kp.keyValue == 1792835)
if (!viewing && kp.keyValue == 1792835)
cout << "Selected file: " << mv.next()->path << "\n";

// left arrow
if (kp.keyValue == 1792836)
if (!viewing && kp.keyValue == 1792836)
cout << "Selected file: " << mv.prev()->path << "\n";

// V key
if (!viewing && std::toupper(kp.keyValue) == 'V')
{
viewing = true;
mv.view(&tu);
}

// ESC
if (kp.keyValue == 27)
break;
{
if (viewing)
viewing = false;
else
break;
}
}
}
tu.showInput();
Expand Down
72 changes: 46 additions & 26 deletions src/media-viewer/mediaviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,45 @@ MediaViewer::MediaViewer(const std::filesystem::path path)
dirPath = path.parent_path();
}

int i = 0;
auto unfilteredFiles = VariousUtils::getFilesInDir(dirPath);
sort(unfilteredFiles.begin(), unfilteredFiles.end(), [](dirent d1, dirent d2){return (std::string)d1.d_name < (std::string)d2.d_name;});
for (const auto file : unfilteredFiles)
{
auto dirPathCopy = dirPath;
std::string pathFileName = dirPath.concat(file.d_name);
dirPath = dirPathCopy;
std::string pathFileName = dirPath / file.d_name;

if (cv::VideoCapture(pathFileName).isOpened())
if (!cv::imread(pathFileName).empty())
{
if (checkForFile && pathFileName == path)
{
filesIndex = files.size();
}

File fileStruct;
fileStruct.path = pathFileName;
fileStruct.type = FileType::IMG;

files.push_back(fileStruct);
}
else if (cv::VideoCapture(pathFileName).isOpened())
{
File fileStruct;

std::string vtdiPath = ((std::string)pathFileName).substr(0, VariousUtils::rfind(pathFileName, '.')) + ".vtdi";
if (std::filesystem::exists(vtdiPath) && validVTDI(vtdiPath))
{
if (pathFileName == path)
if (checkForFile && pathFileName == path)
{
filesIndex = i;
filesIndex = files.size();
}

fileStruct.path = vtdiPath;
fileStruct.type = FileType::VIDTRANS;
}
else
{
if (pathFileName == path)
if (checkForFile && pathFileName == path)
{
filesIndex = i;
filesIndex = files.size();
}

fileStruct.path = pathFileName;
Expand All @@ -78,9 +88,9 @@ MediaViewer::MediaViewer(const std::filesystem::path path)
{
continue;
}
if (pathFileName == path)
if (checkForFile && pathFileName == path)
{
filesIndex = i;
filesIndex = files.size();
}

File fileStruct;
Expand All @@ -89,21 +99,6 @@ MediaViewer::MediaViewer(const std::filesystem::path path)

files.push_back(fileStruct);
}
else if (!cv::imread(pathFileName).empty())
{
if (pathFileName == path)
{
filesIndex = i;
}

File fileStruct;
fileStruct.path = pathFileName;
fileStruct.type = FileType::IMG;

files.push_back(fileStruct);
}

i++;
}
}

Expand Down Expand Up @@ -136,4 +131,29 @@ File* MediaViewer::prev()
return &files[filesIndex];
}

void MediaViewer::view(TermUtils* tu)
{
auto currentFile = current();
switch (currentFile->type)
{
case FileType::IMG:
{
int width, height;
auto viewer = ImgViewer(currentFile->path);
if (tu != nullptr)
(*tu).showInput();
std::cout << "Enter width: ";
std::cin >> width;
std::cout << "Enter height: ";
std::cin >> height;
std::cout << "\n";
if (tu != nullptr)
(*tu).hideInput();
viewer.transcode(width, height);
viewer.print();
break;
}
}
}

}
1 change: 1 addition & 0 deletions src/media-viewer/mediaviewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class MediaViewer {
/// @brief Move to the previous file in the list.
/// @return A pointer to the previous file in the list, or nullptr if the directory is empty.
File* prev();
void view(TermUtils* tu = nullptr);
};

}

0 comments on commit 96c563e

Please sign in to comment.