<commit_before>#include "../stdafx.h"

#include "SLCImageProcess.h"

#include <vector>

namespace slcimage
{
	//====================================================================================================================
	//--------------------------------------------------------------------------------------------------------------------
	void DuplicateImage(const ATL::CImage& srcImage, ATL::CImage& dstImage)
	{
		assert(!srcImage.IsNull());
		if (srcImage == dstImage)		return;
		if (!dstImage.IsNull())         dstImage.Destroy();

		int srcBitsCount = srcImage.GetBPP();
		if (srcBitsCount == 32)	dstImage.Create(srcImage.GetWidth(), srcImage.GetHeight(), srcBitsCount, CImage::createAlphaChannel);
		else                    dstImage.Create(srcImage.GetWidth(), srcImage.GetHeight(), srcBitsCount, 0);

		if (srcImage.IsIndexed())	{
			// 8-bit: 0x100000000 = 256, in the same way, 4-bit: 0x10000 = 16, 2-bit: 0x100 = 4, 1-bit: 0x10 = 2
			int nColors = srcImage.GetMaxColorTableEntries();
			if (nColors > 0)	{
				std::vector<RGBQUAD> rgbColors(nColors);
				srcImage.GetColorTable(0, nColors, rgbColors.data());
				dstImage.SetColorTable(0, nColors, rgbColors.data());
			}
		}

		srcImage.BitBlt(dstImage.GetDC(), 0, 0, SRCCOPY);
		dstImage.ReleaseDC(); // Remember to dstImage.ReleaseDC() after dstImage.GetDC()
	};// End of DuplicateImage(const CImage& srcImage, CImage& destImage)


	//========================================================================================================================
	//========================================================================================================================
	//------------------------------------------------------------------------------------------------------------------------
	void StretchImageByWidth(const ATL::CImage& srcImage, int widthInPixel, ATL::CImage& dstImage)
	{
		assert(!srcImage.IsNull() && srcImage != dstImage && widthInPixel > 0);

		int srcBitsCount = srcImage.GetBPP();
		double imageAspectRatio = static_cast<double>( srcImage.GetHeight() ) / srcImage.GetWidth() ;
		
		// Get valid dstImageWidth and dstImageHeight
		int dstImageWidth = widthInPixel;
		double tmpDstImageHeight = imageAspectRatio * dstImageWidth;
		int dstImageHeight = tmpDstImageHeight > 1.0 ? static_cast<int>(tmpDstImageHeight) : 1;

		if (!dstImage.IsNull()) dstImage.Destroy();
		if (srcBitsCount == 32)	dstImage.Create(dstImageWidth, dstImageHeight, srcBitsCount, CImage::createAlphaChannel);
		else                    dstImage.Create(dstImageWidth, dstImageHeight, srcBitsCount, 0);

		// For 8bit CImage, we need to copy the ColorTable after image creation
		if (srcImage.IsIndexed()) {
			// 8-bit: 0x100000000 = 256, in the same way, 4-bit: 0x10000 = 16, 2-bit: 0x100 = 4, 1-bit: 0x10 = 2
			int nColors = srcImage.GetMaxColorTableEntries();
			if (nColors > 0) {
				std::vector<RGBQUAD> rgbColors(nColors);
				srcImage.GetColorTable(0, nColors, rgbColors.data());
				dstImage.SetColorTable(0, nColors, rgbColors.data());
			}
		}

		HDC dstHDC = dstImage.GetDC();
		SetStretchBltMode(dstHDC, COLORONCOLOR);
		srcImage.StretchBlt(dstHDC, 0, 0, dstImageWidth, dstImageHeight, SRCCOPY);
		dstImage.ReleaseDC(); // Remember to dstImage.ReleaseDC() after dstImage.GetDC()
	}// End of GetSmallerImageByWidth(const ATL::CImage& srcImage, int widthInPixel, ATL::CImage& dstImage)


	//====================================================================================================================
	//--------------------------------------------------------------------------------------------------------------------
	/// <summary>Converting 1Bit/4Bit image to 8Bit/24Bit, help low bit image loading for LibreImage</summary>
	void Convert8BitBelowToAbove(const ATL::CImage& srcImage, int dstBitPerPixel, ATL::CImage& dstImage)
	{
		assert(!srcImage.IsNull() && srcImage != dstImage && srcImage.GetBPP() <= 8 && srcImage.GetBPP() < dstBitPerPixel);
		assert(dstBitPerPixel == 1 || dstBitPerPixel == 4 || dstBitPerPixel == 8 // GrayScaled
			|| dstBitPerPixel == 24 || dstBitPerPixel == 32);                    // Colored

		if (!dstImage.IsNull())     dstImage.Destroy();
		dstImage.Create(srcImage.GetWidth(), srcImage.GetHeight(), dstBitPerPixel, 0);

		// For 8bit CImage, we need to copy the ColorTable after image creation
		if (dstImage.IsIndexed()) {
			// 8-bit: 0x100000000 = 256, in the same way, 4-bit: 0x10000 = 16, 2-bit: 0x100 = 4, 1-bit: 0x10 = 2
			int nColors = srcImage.GetMaxColorTableEntries();
			if (nColors > 0) {
				std::vector<RGBQUAD> rgbColors(nColors);
				srcImage.GetColorTable(0, nColors, rgbColors.data());
				dstImage.SetColorTable(0, nColors, rgbColors.data());
			}
		}

		srcImage.BitBlt(dstImage.GetDC(), 0, 0, SRCCOPY);
		dstImage.ReleaseDC(); // Remember to dstImage.ReleaseDC() after dstImage.GetDC()
	}

	void Convert8bitTo32Bit(const ATL::CImage& srcImage, ATL::CImage& dstImage)
	{
		assert(!srcImage.IsNull() && srcImage != dstImage && srcImage.GetBPP() == 8);

		if (!dstImage.IsNull())     dstImage.Destroy();
		dstImage.Create(srcImage.GetWidth(), srcImage.GetHeight(), 32, CImage::createAlphaChannel);
		
		// CImage::BitBlt won't work with alpha channel, we have to set pixel one by one
		for (int row = 0; row < dstImage.GetHeight(); row++)
		{
			for (int col = 0; col < dstImage.GetWidth(); col++)
			{
				BYTE* pixelEntry = static_cast<BYTE*>(dstImage.GetPixelAddress(col, row));
				const BYTE intensity = static_cast<const BYTE*>(srcImage.GetPixelAddress(col, row))[0];
				pixelEntry[0] = intensity;
				pixelEntry[1] = intensity;
				pixelEntry[2] = intensity;
				pixelEntry[3] = 0xFF;
			}
		}
	}


} // End of namespace slcimage
<commit_msg>Add DownSampling Algorithms for CImage Streth using IWICBitmap<commit_after>#include "../stdafx.h"

#include "SLCImageProcess.h"

#include <vector>

namespace slcimage
{
	//====================================================================================================================
	//--------------------------------------------------------------------------------------------------------------------
	void DuplicateImage(const ATL::CImage& srcImage, ATL::CImage& dstImage)
	{
		assert(!srcImage.IsNull());
		if (srcImage == dstImage)		return;
		if (!dstImage.IsNull())         dstImage.Destroy();

		int srcBitsCount = srcImage.GetBPP();
		if (srcBitsCount == 32)	dstImage.Create(srcImage.GetWidth(), srcImage.GetHeight(), srcBitsCount, CImage::createAlphaChannel);
		else                    dstImage.Create(srcImage.GetWidth(), srcImage.GetHeight(), srcBitsCount, 0);

		if (srcImage.IsIndexed())	{
			// 8-bit: 0x100000000 = 256, in the same way, 4-bit: 0x10000 = 16, 2-bit: 0x100 = 4, 1-bit: 0x10 = 2
			int nColors = srcImage.GetMaxColorTableEntries();
			if (nColors > 0)	{
				std::vector<RGBQUAD> rgbColors(nColors);
				srcImage.GetColorTable(0, nColors, rgbColors.data());
				dstImage.SetColorTable(0, nColors, rgbColors.data());
			}
		}

		srcImage.BitBlt(dstImage.GetDC(), 0, 0, SRCCOPY);
		dstImage.ReleaseDC(); // Remember to dstImage.ReleaseDC() after dstImage.GetDC()
	};// End of DuplicateImage(const CImage& srcImage, CImage& destImage)


	//========================================================================================================================
	//========================================================================================================================
	//------------------------------------------------------------------------------------------------------------------------
	void StretchImageByWidth(const ATL::CImage& srcImage, int widthInPixel, ATL::CImage& dstImage)
	{
		assert(!srcImage.IsNull() && srcImage != dstImage && widthInPixel > 0);

		int srcBitsCount = srcImage.GetBPP();
		double imageAspectRatio = static_cast<double>( srcImage.GetHeight() ) / srcImage.GetWidth() ;
		
		// Get valid dstImageWidth and dstImageHeight
		int dstImageWidth = widthInPixel;
		double tmpDstImageHeight = imageAspectRatio * dstImageWidth;
		int dstImageHeight = tmpDstImageHeight > 1.0 ? static_cast<int>(tmpDstImageHeight) : 1;

		if (!dstImage.IsNull()) dstImage.Destroy();
		if (srcBitsCount == 32)	dstImage.Create(dstImageWidth, dstImageHeight, srcBitsCount, CImage::createAlphaChannel);
		else                    dstImage.Create(dstImageWidth, dstImageHeight, srcBitsCount, 0);

		// For 8bit CImage, we need to copy the ColorTable after image creation
		if (srcImage.IsIndexed()) {
			// 8-bit: 0x100000000 = 256, in the same way, 4-bit: 0x10000 = 16, 2-bit: 0x100 = 4, 1-bit: 0x10 = 2
			int nColors = srcImage.GetMaxColorTableEntries();
			if (nColors > 0) {
				std::vector<RGBQUAD> rgbColors(nColors);
				srcImage.GetColorTable(0, nColors, rgbColors.data());
				dstImage.SetColorTable(0, nColors, rgbColors.data());
			}
		}

		HDC dstHDC = dstImage.GetDC();
		SetStretchBltMode(dstHDC, COLORONCOLOR);
		srcImage.StretchBlt(dstHDC, 0, 0, dstImageWidth, dstImageHeight, SRCCOPY);
		dstImage.ReleaseDC(); // Remember to dstImage.ReleaseDC() after dstImage.GetDC()

		//========== Below is for DownSample Algorithms Testing ===============================================
		//----------     include #include <wincodec.h> before using IWICBitmapScaler   ------------------------

		//IWICImagingFactory *imagingFactory;
		//CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&imagingFactory));
		//IWICBitmap* srcIWICBitmap;
		//imagingFactory->CreateBitmapFromHBITMAP(srcImage, 0, WICBitmapUseAlpha, &srcIWICBitmap);
		//IWICBitmapScaler* bitmapScaler;
		//imagingFactory->CreateBitmapScaler(&bitmapScaler);
		//bitmapScaler->Initialize(srcIWICBitmap, dstImageWidth, dstImageHeight, WICBitmapInterpolationModeCubic);

		//WICRect rect = { 0, 0, dstImageWidth, 1 };
		//int stride = std::abs(dstImage.GetPitch());
		//BYTE* bufferEntry = static_cast<BYTE*>(dstImage.GetBits());

		//for (int i = 0; i < dstImageHeight; ++i)
		//{
		//	bitmapScaler->CopyPixels(&rect, stride, stride, bufferEntry);
		//	bufferEntry -= stride;
		//	rect.Y += 1;
		//}

		//bitmapScaler->Release();
		//imagingFactory->Release();

	}// End of GetSmallerImageByWidth(const ATL::CImage& srcImage, int widthInPixel, ATL::CImage& dstImage)


	//====================================================================================================================
	//--------------------------------------------------------------------------------------------------------------------
	/// <summary>Converting 1Bit/4Bit image to 8Bit/24Bit, help low bit image loading for LibreImage</summary>
	void Convert8BitBelowToAbove(const ATL::CImage& srcImage, int dstBitPerPixel, ATL::CImage& dstImage)
	{
		assert(!srcImage.IsNull() && srcImage != dstImage && srcImage.GetBPP() <= 8 && srcImage.GetBPP() < dstBitPerPixel);
		assert(dstBitPerPixel == 1 || dstBitPerPixel == 4 || dstBitPerPixel == 8 // GrayScaled
			|| dstBitPerPixel == 24 || dstBitPerPixel == 32);                    // Colored

		if (!dstImage.IsNull())     dstImage.Destroy();
		dstImage.Create(srcImage.GetWidth(), srcImage.GetHeight(), dstBitPerPixel, 0);

		// For 8bit CImage, we need to copy the ColorTable after image creation
		if (dstImage.IsIndexed()) {
			// 8-bit: 0x100000000 = 256, in the same way, 4-bit: 0x10000 = 16, 2-bit: 0x100 = 4, 1-bit: 0x10 = 2
			int nColors = srcImage.GetMaxColorTableEntries();
			if (nColors > 0) {
				std::vector<RGBQUAD> rgbColors(nColors);
				srcImage.GetColorTable(0, nColors, rgbColors.data());
				dstImage.SetColorTable(0, nColors, rgbColors.data());
			}
		}

		srcImage.BitBlt(dstImage.GetDC(), 0, 0, SRCCOPY);
		dstImage.ReleaseDC(); // Remember to dstImage.ReleaseDC() after dstImage.GetDC()
	}

	void Convert8bitTo32Bit(const ATL::CImage& srcImage, ATL::CImage& dstImage)
	{
		assert(!srcImage.IsNull() && srcImage != dstImage && srcImage.GetBPP() == 8);

		if (!dstImage.IsNull())     dstImage.Destroy();
		dstImage.Create(srcImage.GetWidth(), srcImage.GetHeight(), 32, CImage::createAlphaChannel);
		
		// CImage::BitBlt won't work with alpha channel, we have to set pixel one by one
		for (int row = 0; row < dstImage.GetHeight(); row++)
		{
			for (int col = 0; col < dstImage.GetWidth(); col++)
			{
				BYTE* pixelEntry = static_cast<BYTE*>(dstImage.GetPixelAddress(col, row));
				const BYTE intensity = static_cast<const BYTE*>(srcImage.GetPixelAddress(col, row))[0];
				pixelEntry[0] = intensity;
				pixelEntry[1] = intensity;
				pixelEntry[2] = intensity;
				pixelEntry[3] = 0xFF;
			}
		}
	}


} // End of namespace slcimage

{'fraction_non_alphanumeric': 0.26089723194400255, 'fraction_numerical': 0.025612472160356347, 'mean_word_length': 3.406940063091483, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, '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': '1789989', 'n_tokens_mistral': 4368, 'n_tokens_neox': 3779, 'n_words': 823}