// Class1.cpp
#include "pch.h"
#include "ImageFilter.h"
#include <ppltasks.h>
#include <concurrent_vector.h>

#include <robuffer.h>
#include <wrl.h>


using namespace PhotoFilterLib;
using namespace concurrency;
using namespace Platform;
using namespace Platform::Collections;
using namespace Windows::Foundation::Collections;
using namespace Windows::Foundation;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml::Media::Imaging;
using namespace Windows::Storage::Pickers;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;



ImageFilter::ImageFilter()
{
}

Platform::Array<unsigned char>^ ImageFilter::AntiqueImage(const Platform::Array<unsigned char>^ buffer)
{
	auto pixels = ref new Platform::Array<unsigned char>(buffer->Length);
	byte* computeBuffer = new byte[buffer->Length];

	for (unsigned int x = 0; x < buffer->Length; x += 4)
	{
		int rgincrease = 100;
		int red = buffer[x] + rgincrease;
		int green = buffer[x + 1] + rgincrease;
		int blue = buffer[x + 2];
		int alpha = buffer[x + 3];

		if (red > 255)
			red = 255;
		if (green > 255)
			green = 255;

		computeBuffer[x] = red;
		computeBuffer[x + 1] = green;
		computeBuffer[x + 2] = blue;
		computeBuffer[x + 3] = alpha;
	}

	for (unsigned int x = 0; x < buffer->Length; x++)
	{
		pixels[x] = computeBuffer[x];
	}

	// Uncomment to free memory
	//delete[] computeBuffer;
	return pixels;
}

inline void ImageFilter::ThrowIfFailed(HRESULT hr)
{
	if (FAILED(hr))
	{
		throw Exception::CreateException(hr);
	}
}
