public static BinarizedImage DownScaleImage(BinarizedImage a_rBinarizedImage, int a_iHorizontalSubDivs, int a_iVerticalSubDivs) { int iImageWidth = a_rBinarizedImage.Width; int iImageHeight = a_rBinarizedImage.Height; int iGridSubDivPerRow = (a_iVerticalSubDivs + 1); int iGridSubDivPerColumn = (a_iHorizontalSubDivs + 1); int iGridSubDivCount = iGridSubDivPerRow * iGridSubDivPerColumn; // Grid div dimensions float fGridSubDivPixelWidth = ((float)iImageWidth) / (float)iGridSubDivPerRow; float fGridSubDivPixelHeight = ((float)iImageHeight) / (float)iGridSubDivPerColumn; BinarizedImage oResizedBinarizedImage = new BinarizedImage(iGridSubDivPerRow, iGridSubDivPerColumn, false); // Iterate grid divs for (int iGridSubDivIndex = 0; iGridSubDivIndex < iGridSubDivCount; ++iGridSubDivIndex) { // ( X, Y ) grid div pos from grid div index (in grid div space) int iGridSubDivX = iGridSubDivIndex % iGridSubDivPerRow; int iGridSubDivY = iGridSubDivIndex / iGridSubDivPerRow; // Compute the pixel bounds for this subdivision int iStartX = Mathf.FloorToInt(iGridSubDivX * fGridSubDivPixelWidth); int iStartY = Mathf.FloorToInt(iGridSubDivY * fGridSubDivPixelHeight); int iEndX = Mathf.CeilToInt((iGridSubDivX + 1) * fGridSubDivPixelWidth); int iEndY = Mathf.CeilToInt((iGridSubDivY + 1) * fGridSubDivPixelHeight); int iWidth = iEndX - iStartX; int iHeight = iEndY - iStartY; // Set grid sub div as empty while no significant pixel found bool bEmptyGridSubDiv = true; float fGridSubDivAlphaMean = 0.0f; for (int iY = 0; iY < iHeight; ++iY) { for (int iX = 0; iX < iWidth; ++iX) { int iPixelIndex = a_rBinarizedImage.Coords2PixelIndex(iX + iStartX, iY + iStartY); // At least one pixel is significant => need to create the whole grid div if (a_rBinarizedImage[iPixelIndex]) { bEmptyGridSubDiv = false; } fGridSubDivAlphaMean += a_rBinarizedImage.GetAlphaValue(iPixelIndex); } } fGridSubDivAlphaMean /= iHeight * iWidth; int iResizedIndex = oResizedBinarizedImage.Coords2PixelIndex(iGridSubDivX, iGridSubDivY); oResizedBinarizedImage.SetInternalPixel(iResizedIndex, !bEmptyGridSubDiv, fGridSubDivAlphaMean); } return(oResizedBinarizedImage); }
public static BinarizedImage DownScaleImage(BinarizedImage a_rBinarizedImage, int a_iHorizontalSubDivs, int a_iVerticalSubDivs) { int iImageWidth = a_rBinarizedImage.Width; int iImageHeight = a_rBinarizedImage.Height; int iGridSubDivPerRow = ( a_iVerticalSubDivs + 1 ); int iGridSubDivPerColumn = ( a_iHorizontalSubDivs + 1 ); int iGridSubDivCount = iGridSubDivPerRow * iGridSubDivPerColumn; // Grid div dimensions float fGridSubDivPixelWidth = ( (float) iImageWidth ) / (float) iGridSubDivPerRow; float fGridSubDivPixelHeight = ( (float) iImageHeight ) / (float) iGridSubDivPerColumn; BinarizedImage oResizedBinarizedImage = new BinarizedImage( iGridSubDivPerRow, iGridSubDivPerColumn, false ); // Iterate grid divs for( int iGridSubDivIndex = 0; iGridSubDivIndex < iGridSubDivCount; ++iGridSubDivIndex ) { // ( X, Y ) grid div pos from grid div index (in grid div space) int iGridSubDivX = iGridSubDivIndex % iGridSubDivPerRow; int iGridSubDivY = iGridSubDivIndex / iGridSubDivPerRow; // Compute the pixel bounds for this subdivision int iStartX = Mathf.FloorToInt(iGridSubDivX * fGridSubDivPixelWidth); int iStartY = Mathf.FloorToInt(iGridSubDivY * fGridSubDivPixelHeight); int iEndX = Mathf.CeilToInt((iGridSubDivX + 1) * fGridSubDivPixelWidth); int iEndY = Mathf.CeilToInt((iGridSubDivY + 1) * fGridSubDivPixelHeight); int iWidth = iEndX - iStartX; int iHeight = iEndY - iStartY; // Set grid sub div as empty while no significant pixel found bool bEmptyGridSubDiv = true; float fGridSubDivAlphaMean = 0.0f; for( int iY = 0; iY < iHeight; ++iY ) { for( int iX = 0; iX < iWidth; ++iX ) { int iPixelIndex = a_rBinarizedImage.Coords2PixelIndex( iX + iStartX, iY + iStartY ); // At least one pixel is significant => need to create the whole grid div if( a_rBinarizedImage[ iPixelIndex ] ) { bEmptyGridSubDiv = false; } fGridSubDivAlphaMean += a_rBinarizedImage.GetAlphaValue(iPixelIndex); } } fGridSubDivAlphaMean /= iHeight * iWidth; int iResizedIndex = oResizedBinarizedImage.Coords2PixelIndex( iGridSubDivX, iGridSubDivY ); oResizedBinarizedImage.SetInternalPixel(iResizedIndex, !bEmptyGridSubDiv, fGridSubDivAlphaMean); } return oResizedBinarizedImage; }