Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.7 kB
4
Indexable
Never
// This is just implementing the filters
// input handling is already given in a file by cs50 staff


#include "helpers.h"
#include <stdio.h> /////////////////////////////////////////////////////

void swap(RGBTRIPLE *px, RGBTRIPLE *px2);

// 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])
{
    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;
            int count = 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 && 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])
{
    return;
}


// Swaps pixels

void swap(RGBTRIPLE *px, RGBTRIPLE *px2)
{
    RGBTRIPLE tmp = *px;
    *px = *px2;
    *px2 = tmp;
}
Leave a Comment