volume.c Code
// Modifies the volume of an audio file
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
// Number of bytes in .wav header
const int HEADER_SIZE = 44;
int main(int argc, char *argv[])
{
// Check command-line arguments
if (argc != 4)
{
printf("Usage: ./volume input.wav output.wav factor\n");
return 1;
}
// Open files and determine scaling factor
FILE *input = fopen(argv[1], "r");
if (input == NULL)
{
printf("Could not open file.\n");
return 1;
}
FILE *output = fopen(argv[2], "w");
if (output == NULL)
{
printf("Could not open file.\n");
return 1;
}
float factor = atof(argv[3]);
// TODO: Copy header from input file to output file
uint8_t buffer[HEADER_SIZE];
fread(buffer, HEADER_SIZE, 1, input);
fwrite(buffer, HEADER_SIZE, 1, output);
// TODO: Read samples from input file and write updated data to output file
int16_t sample;
while (fread(&sample, sizeof(int16_t), 1, input) != 0)
{
sample = sample * factor;
fwrite(&sample, sizeof(int16_t), 1, output);
}
// Close files
fclose(input);
fclose(output);
}
helpers.c Code
#include "helpers.h"
#include <math.h>
#include <stdio.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
/*
to achive the gray and brightnes of each color we would need the following:
1. add all color values together
2. divide by 3 (cause three values have been added)
3. set new the value on all of the colors
*/
int gray = 0;
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
// do step 1. and 2. (and round to the nearst integer)
gray = round((image[row][column].rgbtBlue + image[row][column].rgbtGreen +
image[row][column].rgbtRed) /
3.0);
// do step 3.
image[row][column].rgbtBlue = gray;
image[row][column].rgbtGreen = gray;
image[row][column].rgbtRed = gray;
}
}
return;
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
/*
to achieve need of formula like:
sepiaRed = .393 * originalRed + .769 * originalGreen + .189 * originalBlue
sepiaGreen = .349 * originalRed + .686 * originalGreen + .168 * originalBlue
sepiaBlue = .272 * originalRed + .534 * originalGreen + .131 * originalBlue
making sure that the values doesnt exceed 255 otherwise we wont get the desired color
*/
int sepiaRed = 0;
int sepiaGreen = 0;
int sepiaBlue = 0;
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
// the formula to turn colors sepia-feeling
sepiaRed =
round(0.393 * image[row][column].rgbtRed + 0.769 * image[row][column].rgbtGreen +
0.189 * image[row][column].rgbtBlue);
if (sepiaRed > 255)
{
printf("updating sepia value: %i\n", sepiaRed);
sepiaRed = 255;
printf("to: %i\n", sepiaRed);
}
sepiaGreen =
round(0.349 * image[row][column].rgbtRed + 0.686 * image[row][column].rgbtGreen +
0.168 * image[row][column].rgbtBlue);
if (sepiaGreen > 255)
{
printf("updating sepia value: %i\n", sepiaGreen);
sepiaGreen = 255;
printf("to: %i\n", sepiaGreen);
}
sepiaBlue =
round(0.272 * image[row][column].rgbtRed + 0.534 * image[row][column].rgbtGreen +
0.131 * image[row][column].rgbtBlue);
if (sepiaBlue > 255)
{
printf("updating sepia value: %i\n", sepiaBlue);
sepiaBlue = 255;
printf("to: %i\n", sepiaBlue);
}
image[row][column].rgbtBlue = sepiaBlue;
image[row][column].rgbtGreen = sepiaGreen;
image[row][column].rgbtRed = sepiaRed;
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
/*
colors that are to the left should be at the right and vise versa
to achive that:
1.set a starting point for the left side, which ends at the end of the right side
2.go below and before you do step 1. again restart the starting point
*/
int i = 1;
RGBTRIPLE imagetmp[height][width];
for (int row = 0; row < height; row++)
{
for (int column = 0; column < (width / 2); column++)
{
imagetmp[row][column] = image[row][column];
// printf("before: image[row][column] { Red: %i Green: %i Blue: %i } =
// image[row][width-i] { Red: %i Green: %i Blue: %i }\n",
// image[row][column].rgbtRed,image[row][column].rgbtGreen,
// image[row][column].rgbtBlue,image[row][width-i].rgbtRed,image[row][width-i].rgbtGreen,
// image[row][width-i].rgbtBlue);
image[row][column] = image[row][width - i];
// printf("after: image[row][column] { Red: %i Green: %i Blue: %i } =
// image[row][width-i] { Red: %i Green: %i Blue: %i }\n",
// image[row][column].rgbtRed,image[row][column].rgbtGreen,
// image[row][column].rgbtBlue,image[row][width-i].rgbtRed,image[row][width-i].rgbtGreen,
// image[row][width-i].rgbtBlue);
image[row][width - i] = imagetmp[row][column];
// printf("image[row][width-i] { Red: %i Green: %i Blue: %i } = imagetmp[row][column]
// { Red: %i Green: %i Blue: %i }\n",
// image[row][width-i].rgbtRed,image[row][width-i].rgbtGreen,
// image[row][width-i].rgbtBlue,imagetmp[row][column].rgbtRed,imagetmp[row][column].rgbtGreen,
// imagetmp[row][column].rgbtBlue);
// printf("column: %i\nrtl: %i\n", column, i);
i++;
}
i = 1;
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
/*
1. Go back on the row by one
2. Add each rgbt together, get the average of them than go into next column, repeat until third
column is finished then
3. Go next row
4. Go back to step 1.
5. Row 3? than stop calculating
*/
RGBTRIPLE imagetmp[height][width];
int averageBlue = 0;
int averageGreen = 0;
int averageRed = 0;
int outside = 0;
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
imagetmp[row][column] = image[row][column]; // copying orginal pixels
}
}
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
for (int ri = 0; ri < 3; ri++)
{
for (int ci = 0; ci < 3; ci++)
{
if ((((row - 1) + ri) >= 0) && (((row - 1) + ri) < height) &&
(((column - 1) + ci) >= 0) && (((column - 1) + ci) < width))
{
// Averaging Blue
averageBlue =
averageBlue + imagetmp[(row - 1) + ri][(column - 1) + ci].rgbtBlue;
// Averaging Green
averageGreen =
averageGreen + imagetmp[(row - 1) + ri][(column - 1) + ci].rgbtGreen;
// Averaging Red
averageRed =
averageRed + imagetmp[(row - 1) + ri][(column - 1) + ci].rgbtRed;
}
else
{
outside++;
}
}
}
image[row][column].rgbtBlue = round(averageBlue / (9.0 - outside));
image[row][column].rgbtGreen = round(averageGreen / (9.0 - outside));
image[row][column].rgbtRed = round(averageRed / (9.0 - outside));
averageBlue = 0;
averageGreen = 0;
averageRed = 0;
outside = 0;
}
}
return;
}
Recover.c Code
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
const int BLOCKSIZE = 512;
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
FILE *input = fopen(argv[1], "r");
if (input == NULL)
{
printf("image can not be opened\n");
return 1;
}
// store exactly 1 byte at a time (unsigned int of 8!)
// OPEN:
// how to compare those 0xff, 0xd8 and so on??
// how to comapre the 4th byte, of 0xe to the final one: Usage of arthitmic, understanding
uint8_t buffer[BLOCKSIZE];
int wrote = 0;
int order = 0; // for file quantity
char files[999];
FILE *pic;
while (fread(buffer, sizeof(uint8_t), BLOCKSIZE, input) == BLOCKSIZE)
/*
Implicatione as psudeocode:
1.look for jpeg signutres
2.create a new file for writing if found one fill it with the bytes of the memory card
3. close it if another signature found
When reading (into a buffer) instead of 1 Byte
at a time, usage of 512Byte is suggested
So signutres will be checked in the first 4 bytes of those 512 Bytes blocks
*/
{
if ((buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
(buffer[3] & 0xf0) ==
0xe0)) // 0x means the follwing is a hexa digit so ff equals 1111 1111
{
if (order != 0)
{
fclose(pic);
}
sprintf(files, "%03i.jpg", order);
pic = fopen(files, "w");
// printf("%i (order), %s (files)\n",order,files);
order++;
}
if (order != 0)
{
wrote = fwrite(buffer, sizeof(uint8_t), BLOCKSIZE, pic);
}
}
fclose(pic);
fclose(input);
return 0;
}