Untitled
#include "helpers.h" #include <math.h> void swap(RGBTRIPLE *px, RGBTRIPLE *px2); int threshold(int value); // Convert image to grayscale void grayscale(int height, int width, RGBTRIPLE image[height][width]) { int gray; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { RGBTRIPLE *px = &image[i][j]; gray = (((*px).rgbtBlue + (*px).rgbtGreen + (*px).rgbtRed + 1.5) / 3); (*px).rgbtBlue = gray; (*px).rgbtGreen = gray; (*px).rgbtRed = gray; } } } /* if (j > 200 && j < 300) { continue; } */ // Reflect image horizontally void reflect(int height, int width, RGBTRIPLE image[height][width]) { for (int i = 0; i < height; i++) { for (int j = 0; j < width / 2; j++) { swap(&image[i][j], &image[i][width - j - 1]); } } } // Blur image void blur(int height, int width, RGBTRIPLE image[height][width]) { int grid_size = 3; // odd int amt = (grid_size - 1) / 2; RGBTRIPLE tmp[height][width]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int blue_sum = 0, green_sum = 0, red_sum = 0; float count = 0; for (int ai = -amt; ai <= amt; ai++) { for (int aj = -amt; aj <= amt; aj++) { int ni = i + ai; int nj = j + aj; if (ni >= 0 && nj >= 0 && ni < height && nj < width) { blue_sum += image[ni][nj].rgbtBlue; green_sum += image[ni][nj].rgbtGreen; red_sum += image[ni][nj].rgbtRed; count++; } } } tmp[i][j].rgbtBlue = (blue_sum / count) + 0.5; tmp[i][j].rgbtGreen = (green_sum / count) + 0.5; tmp[i][j].rgbtRed = (red_sum / count) + 0.5; } } for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { image[i][j] = tmp[i][j]; } } } /*if (j > 200 && j < 300) { tmp[i][j] = image[i][j]; continue; }*/ // Detect edges void edges(int height, int width, RGBTRIPLE image[height][width]) { RGBTRIPLE tmp[height][width]; int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}; int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}}; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int GxBlue = 0, GxGreen = 0, GxRed = 0; int GyBlue = 0, GyGreen = 0, GyRed = 0; for (int ai = -1; ai <= 1; ai++) { for (int aj = -1; aj <= 1; aj++) { int ni = i + ai; int nj = j + aj; if (ni >= 0 && ni < height && nj >= 0 && nj < width) { // X - Direction GxBlue += image[ni][nj].rgbtBlue * Gx[ai + 1][aj + 1]; GxGreen += image[ni][nj].rgbtGreen * Gx[ai + 1][aj + 1]; GxRed += image[ni][nj].rgbtRed * Gx[ai + 1][aj + 1]; // Y - Direction GyBlue += image[ni][nj].rgbtBlue * Gy[ai + 1][aj + 1]; GyGreen += image[ni][nj].rgbtGreen * Gy[ai + 1][aj + 1]; GyRed += image[ni][nj].rgbtRed * Gy[ai + 1][aj + 1]; } } } tmp[i][j].rgbtBlue = threshold(round(sqrt((GxBlue * GxBlue) + (GyBlue * GyBlue)))); tmp[i][j].rgbtGreen = threshold(round(sqrt((GxGreen * GxGreen) + (GyGreen * GyGreen)))); tmp[i][j].rgbtRed = threshold(round(sqrt((GxRed * GxRed) + (GyRed * GyRed)))); } } for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { image[i][j] = tmp[i][j]; } } } // Swaps pixels void swap(RGBTRIPLE *px, RGBTRIPLE *px2) { RGBTRIPLE tmp = *px; *px = *px2; *px2 = tmp; } int threshold(int value) { // Ensures that the value stays between 0 and 255 (although it won't go beyond 0 unless there is // an error) return value > 255 ? 255 : (value < 0 ? 0 : value); // condition ? if ture : if false // You can also write it like this: /* if (value > 255) { return 255; } else if (value < 0) { return 0; } return value */ }
Leave a Comment