diff --git a/src/img-viewer/imgviewer.cpp b/src/img-viewer/imgviewer.cpp index 30fffc5..fd9750b 100644 --- a/src/img-viewer/imgviewer.cpp +++ b/src/img-viewer/imgviewer.cpp @@ -228,4 +228,24 @@ void ImgViewer::print() } } +int ImgViewer::getPixelWidth() +{ + return file.size().width; +} + +int ImgViewer::getPixelHeight() +{ + return file.size().height; +} + +int ImgViewer::getTerminalWidth() +{ + return width; +} + +int ImgViewer::getTerminalHeight() +{ + return height; +} + } diff --git a/src/img-viewer/imgviewer.hpp b/src/img-viewer/imgviewer.hpp index e8f02b2..fa3ad24 100644 --- a/src/img-viewer/imgviewer.hpp +++ b/src/img-viewer/imgviewer.hpp @@ -19,6 +19,16 @@ class ImgViewer { ImgViewer(const std::string path); void transcode(const int width, const int height); void print(); + + /// @brief Get the width of the original image file. + int getPixelWidth(); + /// @brief Get the height of the original image file. + int getPixelHeight(); + + /// @brief Get the width of the transcoded image. + int getTerminalWidth(); + /// @brief Get the height of the transcoded image. + int getTerminalHeight(); }; } diff --git a/src/media-viewer/main.cpp b/src/media-viewer/main.cpp index b3d58ec..58a458d 100644 --- a/src/media-viewer/main.cpp +++ b/src/media-viewer/main.cpp @@ -11,9 +11,9 @@ int main(int argc, char** argv) { filesystem::path path; if (argc > 1) - path = filesystem::absolute(argv[1]); + path = filesystem::relative(argv[1]); else - path = filesystem::current_path(); + path = filesystem::relative(filesystem::current_path()); auto mv = MediaViewer(path); @@ -47,7 +47,7 @@ int main(int argc, char** argv) if (!viewing && std::toupper(kp.keyValue) == 'V') { viewing = true; - mv.view(&tu); + mv.view(); } // ESC diff --git a/src/media-viewer/mediaviewer.cpp b/src/media-viewer/mediaviewer.cpp index 074fa94..7e6d5be 100644 --- a/src/media-viewer/mediaviewer.cpp +++ b/src/media-viewer/mediaviewer.cpp @@ -131,25 +131,34 @@ File* MediaViewer::prev() return &files[filesIndex]; } -void MediaViewer::view(TermUtils* tu) +void MediaViewer::view() { 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); + auto pDims = std::array(); + pDims[0] = viewer.getPixelWidth(); + pDims[1] = viewer.getPixelHeight(); + auto tMax = TermUtils::getTerminalDimensions(); + + double aspectRatio = (double) pDims[0] / pDims[1]; + std::array tDims; + + // try maxing out height + tDims[1] = tMax[1]; + tDims[0] = 2 * aspectRatio * tDims[1]; + + // if that doesn't work, max out width + if (tDims[0] > tMax[0]) + { + tDims[0] = tMax[0]; + tDims[1] = 0.5 * tDims[0] / aspectRatio; + } + + viewer.transcode(tDims[0], tDims[1]); viewer.print(); break; } diff --git a/src/media-viewer/mediaviewer.hpp b/src/media-viewer/mediaviewer.hpp index 1e2fe69..d209e00 100644 --- a/src/media-viewer/mediaviewer.hpp +++ b/src/media-viewer/mediaviewer.hpp @@ -32,7 +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); + void view(); }; }