// Built by Peter A Noble  February, 2024 Email: panoble2017@gmail.com
// Copyright 2024
// works in example.cpp -->  g++ -std=c++17 -o box_images box_images2.cpp -I/opt/homebrew/opt/opencv/include/opencv4 -L/opt/homebrew/opt/opencv/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_imgproc -lopencv_videoio
// ./box_images img2.png
// g++ -std=c++17 crop_images.cpp -o crop_images -I/opt/homebrew/opt/opencv/include/opencv4 -L/opt/homebrew/opt/opencv/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_imgproc -lopencv_videoio
// ./crop_images living_3_dog_10465_3_white_80_Children_20_spectrum.png
// ./crop_images living_3_dog_10409_3_white_80_spectrum.png

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;


int main(int argc, char** argv) {
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <image_file_path>" << std::endl;
        return -1;
    }

//magick input.png -colorspace sRGB output.png

    // Load the image
    cv::Mat image = cv::imread(argv[1], cv::IMREAD_UNCHANGED);

    if (image.empty()) {
        std::cerr << "Error: Could not read the image." << std::endl;
        return -1;
    }

    // Extract the base name from the input file path
    std::string baseName = cv::samples::findFile(argv[1]);
    size_t found = baseName.find_last_of("/\\");
    std::string fileName = baseName.substr(found + 1);

    // Define the desired width and height for cropping
    int targetWidth = 1750; // Set your desired width
    int targetHeight = 920; // Set your desired height

    // Ensure that the target size is within the image bounds
    targetWidth = std::min(targetWidth, image.cols);
    targetHeight = std::min(targetHeight, image.rows);

    // Calculate the cropping region
    int startX = (image.cols - targetWidth) / 2;
    int startY = (image.rows - targetHeight) / 2;

    // Crop the image
    cv::Rect roi(startX, startY, targetWidth, targetHeight);
    cv::Mat croppedImage = image(roi).clone();

    // Save the cropped image with the modified filename
    std::string outputFileName = "cropped_" + fileName;
    cv::imwrite(outputFileName, croppedImage);

    return 0;
}


