Skip to content

Instantly share code, notes, and snippets.

@Jonathan-Adly
Last active June 11, 2020 16:07
Show Gist options
  • Select an option

  • Save Jonathan-Adly/39852a102d6c25bb130d6b5184c4a33a to your computer and use it in GitHub Desktop.

Select an option

Save Jonathan-Adly/39852a102d6c25bb130d6b5184c4a33a to your computer and use it in GitHub Desktop.
CS50 - PSET4
#include "helpers.h"
#include <math.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
float rgbgray_scale;
for (int i = 0; i < width; i++) //itirate over width
{
for (int j = 0; j < height; j++) //itirate over height
{
// average pixels and make theme equal to each other
rgbgray_scale = round ((image[j][i].rgbtBlue + image[j][i].rgbtGreen + image[j][i].rgbtRed) / 3.000);
image [j][i].rgbtBlue = rgbgray_scale;
image [j][i].rgbtGreen = rgbgray_scale;
image [j][i].rgbtRed = rgbgray_scale;
}
}
}
// Convert image to sepia
int limit(int RGB) // set max RGB value to 255
{
if (RGB > 255)
{
RGB = 255;
}
return RGB;
}
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
int sepiaBlue;
int sepiaGreen;
int sepiaRed;
for (int i = 0; i < width; i++) //itirate over width
{
for (int j = 0; j < height; j++) //itirate over height
{
// set image pixel to new sepia values
sepiaBlue = limit (round (.272 * image [j][i].rgbtRed + .534 * image [j][i].rgbtGreen + .131 * image [j][i].rgbtBlue));
sepiaGreen = limit (round (.349 * image [j][i].rgbtRed + .686 * image [j][i].rgbtGreen + .168 * image [j][i].rgbtBlue));
sepiaRed = limit (round (.393 * image [j][i].rgbtRed + .769 * image [j][i].rgbtGreen + .189 * image [j][i].rgbtBlue));
image [j][i].rgbtBlue = sepiaBlue;
image [j][i].rgbtGreen = sepiaGreen;
image [j][i].rgbtRed = sepiaRed;
}
}
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
int temp [3]; // temporary array to store values
for (int j = 0; j < height; j++) //itirate over height
{
for (int i = 0; i < width / 2; i++) //itirate over width
{
// swap
temp [0] = image [j][i].rgbtBlue;
temp [1] = image [j][i].rgbtGreen;
temp [2] = image [j][i].rgbtRed;
image [j][i].rgbtBlue = image [j][width - i - 1].rgbtBlue;
image [j][i].rgbtGreen = image [j][width - i - 1].rgbtGreen;
image [j][i].rgbtRed = image [j][width - i - 1].rgbtRed;
image [j][width - i - 1].rgbtBlue = temp [0];
image [j][width - i - 1].rgbtGreen = temp [1];
image [j][width - i - 1].rgbtRed = temp [2];
}
}
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
int sumBlue;
int sumGreen;
int sumRed;
float counter;
//create a temporary table of colors to not alter the calculations
RGBTRIPLE temp[height][width];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
sumBlue = 0;
sumGreen = 0;
sumRed = 0;
counter = 0.00;
// sums values of the pixel and 8 neighboring ones, skips iteration if it goes outside the pic
for (int k = -1; k < 2; k++)
{
if (j + k < 0 || j + k > height - 1)
{
continue;
}
for (int h = -1; h < 2; h++)
{
if (i + h < 0 || i + h > width - 1)
{
continue;
}
sumBlue += image[j + k][i + h].rgbtBlue;
sumGreen += image[j + k][i + h].rgbtGreen;
sumRed += image[j + k][i + h].rgbtRed;
counter++;
}
}
// averages the sum to make picture look blurrier
temp[j][i].rgbtBlue = round(sumBlue / counter);
temp[j][i].rgbtGreen = round(sumGreen / counter);
temp[j][i].rgbtRed = round(sumRed / counter);
}
}
//copies values from temporary table
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
image[j][i].rgbtBlue = temp[j][i].rgbtBlue;
image[j][i].rgbtGreen = temp[j][i].rgbtGreen;
image[j][i].rgbtRed = temp[j][i].rgbtRed;
}
}
}
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#define BLOCK_SIZE 512
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 2)
{
fprintf(stderr, "Usage: recover infile\n");
return 1;
}
// remember filenames
char *infile = argv[1];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
BYTE buffer[512];
int imageCount = 0;
char filename[8];
FILE *outptr = NULL;
while (true)
{
// read a block of the memory card image
size_t bytesRead = fread(buffer, sizeof(BYTE), BLOCK_SIZE, inptr);
// break out of the loop when we reach the end of the card image
if (bytesRead == 0 && feof(inptr))
{
break;
}
// check if we found a JPEG
bool containsJpegHeader = buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0;
// if we found a yet another JPEG, we must close the previous file
if (containsJpegHeader && outptr != NULL)
{
fclose(outptr);
imageCount++;
}
// if we found a JPEG, we need to open the file for writing
if (containsJpegHeader)
{
sprintf(filename, "%03i.jpg", imageCount);
outptr = fopen(filename, "w");
}
// write anytime we have an open file
if (outptr != NULL)
{
fwrite(buffer, sizeof(BYTE), bytesRead, outptr);
}
}
// close last jpeg file
fclose(outptr);
// close infile
fclose(inptr);
// success
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment