<commit_before>#include "ImageResizer.hpp"
#include "FAST/Data/Image.hpp"

namespace fast {


void ImageResizer::setWidth(int width) {
	if(width <= 0)
		throw Exception("Width must be larger than 0.");
	mSize.x() = width;
}

void ImageResizer::setHeight(int height) {
	if(height <= 0)
		throw Exception("Height must be larger than 0.");
	mSize.y() = height;
}

void ImageResizer::setDepth(int depth) {
	if(depth <= 0)
		throw Exception("Depth must be larger than 0.");
	mSize.z() = depth;
}

void ImageResizer::setSize(VectorXi size) {
	setWidth(size.x());
	setHeight(size.y());
	if(size.size() > 2)
		setDepth(size.z());
}

void ImageResizer::setPreserveAspectRatio(bool preserve) {
    mPreserveAspectRatio = preserve;
}

ImageResizer::ImageResizer() {
	createInputPort<Image>(0);
	createOutputPort<Image>(0);

    createOpenCLProgram(Config::getKernelSourcePath() + "Algorithms/ImageResizer/ImageResizer.cl");

	mSize = Vector3i::Zero();
    mPreserveAspectRatio = false;
}

void ImageResizer::execute() {
    Image::pointer input = getInputData<Image>();
    Image::pointer output = getOutputData<Image>();

    if(mSize.x() <= 0 || mSize.y() <= 0)
    	throw Exception("Desired size must be provided to ImageResizer");

    // Initialize output image
    if(input->getDimensions() == 2) {
        output->create(
                mSize.x(),
				mSize.y(),
                input->getDataType(),
                input->getNrOfComponents()
        );
    } else {
        if(mSize.z() == 0)
            throw Exception("Desired size must be provided to ImageResizer");
        output->create(
                mSize.cast<uint>(),
                input->getDataType(),
				input->getNrOfComponents()
        );
    }

    if(getMainDevice()->isHost()) {
        throw Exception("Not implemented yet.");
    } else {
        OpenCLDevice::pointer device = OpenCLDevice::pointer(getMainDevice());
        cl::Program program = getOpenCLProgram(device, "");
        cl::Kernel kernel;
        OpenCLImageAccess::pointer inputAccess = input->getOpenCLImageAccess(ACCESS_READ, device);
        if(input->getDimensions() == 2) {
            if(mPreserveAspectRatio) {
                float scale = (float)output->getWidth() / input->getWidth();
                output->setSpacing(scale*input->getSpacing());
                int newHeight = (int)round(input->getHeight()*scale);
                kernel = cl::Kernel(program, "resize2DpreserveAspect");
                kernel.setArg(2, newHeight);
            } else {
                kernel = cl::Kernel(program, "resize2D");
            }
            OpenCLImageAccess::pointer outputAccess = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
            kernel.setArg(0, *inputAccess->get2DImage());
            kernel.setArg(1, *outputAccess->get2DImage());
        } else {
            throw Exception("Not implemented yet.");
        	/*
            kernel = cl::Kernel(program, "gradient3D");
            kernel.setArg(0, *inputAccess->get3DImage());

            if(device->isWritingTo3DTexturesSupported()) {
                OpenCLImageAccess::pointer outputAccess = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
                kernel.setArg(1, *outputAccess->get3DImage());<commit_msg>fixing spacing in ImageResizer<commit_after>#include "ImageResizer.hpp"
#include "FAST/Data/Image.hpp"

namespace fast {


void ImageResizer::setWidth(int width) {
	if(width <= 0)
		throw Exception("Width must be larger than 0.");
	mSize.x() = width;
}

void ImageResizer::setHeight(int height) {
	if(height <= 0)
		throw Exception("Height must be larger than 0.");
	mSize.y() = height;
}

void ImageResizer::setDepth(int depth) {
	if(depth <= 0)
		throw Exception("Depth must be larger than 0.");
	mSize.z() = depth;
}

void ImageResizer::setSize(VectorXi size) {
	setWidth(size.x());
	setHeight(size.y());
	if(size.size() > 2)
		setDepth(size.z());
}

void ImageResizer::setPreserveAspectRatio(bool preserve) {
    mPreserveAspectRatio = preserve;
}

ImageResizer::ImageResizer() {
	createInputPort<Image>(0);
	createOutputPort<Image>(0);

    createOpenCLProgram(Config::getKernelSourcePath() + "Algorithms/ImageResizer/ImageResizer.cl");

	mSize = Vector3i::Zero();
    mPreserveAspectRatio = false;
}

void ImageResizer::execute() {
    Image::pointer input = getInputData<Image>();
    Image::pointer output = getOutputData<Image>();

    if(mSize.x() <= 0 || mSize.y() <= 0)
    	throw Exception("Desired size must be provided to ImageResizer");

    // Initialize output image
    if(input->getDimensions() == 2) {
        output->create(
                mSize.x(),
				mSize.y(),
                input->getDataType(),
                input->getNrOfComponents()
        );
    } else {
        if(mSize.z() == 0)
            throw Exception("Desired size must be provided to ImageResizer");
        output->create(
                mSize.cast<uint>(),
                input->getDataType(),
				input->getNrOfComponents()
        );
    }

    if(getMainDevice()->isHost()) {
        throw Exception("Not implemented yet.");
    } else {
        OpenCLDevice::pointer device = OpenCLDevice::pointer(getMainDevice());
        cl::Program program = getOpenCLProgram(device, "");
        cl::Kernel kernel;
        OpenCLImageAccess::pointer inputAccess = input->getOpenCLImageAccess(ACCESS_READ, device);
        if(input->getDimensions() == 2) {
            if(mPreserveAspectRatio) {
                float scale = (float)output->getWidth() / input->getWidth();
                output->setSpacing(scale*input->getSpacing());
                int newHeight = (int)round(input->getHeight()*scale);
                kernel = cl::Kernel(program, "resize2DpreserveAspect");
                kernel.setArg(2, newHeight);
            } else {
                output->setSpacing(Vector3f(
                    input->getSpacing().x()*((float)input->getWidth()/output->getWidth()),
                    input->getSpacing().y()*((float)input->getHeight()/output->getHeight()),
                    1.0f
                ));
                kernel = cl::Kernel(program, "resize2D");
            }
            OpenCLImageAccess::pointer outputAccess = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
            kernel.setArg(0, *inputAccess->get2DImage());
            kernel.setArg(1, *outputAccess->get2DImage());
        } else {
            throw Exception("Not implemented yet.");
        	/*
            kernel = cl::Kernel(program, "gradient3D");
            kernel.setArg(0, *inputAccess->get3DImage());

            if(device->isWritingTo3DTexturesSupported()) {
                OpenCLImageAccess::pointer outputAccess = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
                kernel.setArg(1, *outputAccess->get3DImage());
{'fraction_non_alphanumeric': 0.14413884394763937, 'fraction_numerical': 0.00867774672745992, 'mean_word_length': 2.801006148686417, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6197466', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1977, 'n_words': 362}