๊ณผ์ :
Volume
#include <cs50.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.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 header[HEADER_SIZE];
/* There is only one header, so parse it just once */
fread(&header, sizeof(header), 1, input);
fwrite(&header, sizeof(header), 1, output);
// TODO: Read samples from input file and write updated data to output file
int16_t buffer;
while (fread(&buffer, sizeof(buffer), 1, input))
{
buffer *= factor;
fwrite(&buffer, sizeof(buffer), 1, output);
}
// Close files
fclose(input);
fclose(output);
}
Filter(less)
#include "helpers.h"
#include "math.h"
#include "string.h"
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
// Iterate over every row of the image
for (int i = 0; i < height; i++)
{
// Iterate over every column of the image
for (int j = 0; j < width; j++)
{
// Get RGB values
int red = image[i][j].rgbtRed;
int green = image[i][j].rgbtGreen;
int blue = image[i][j].rgbtBlue;
// Calculate average
// Round it to the nearest integer
// Using 3.0: a floating point division is required
int average = round((red + green + blue) / 3.0);
// Set each RGB values to the average value
image[i][j].rgbtRed = average;
image[i][j].rgbtGreen = average;
image[i][j].rgbtBlue = average;
}
}
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
// Iterate over every row of the image
for (int i = 0; i < height; i++)
{
// Iterate over every column of the image
for (int j = 0; j < width; j++)
{
// Get RGB values
int red = image[i][j].rgbtRed;
int green = image[i][j].rgbtGreen;
int blue = image[i][j].rgbtBlue;
// Get sepia values based on sepia formula
// Round value to the nearest integer
int sepiaRed = round(0.393 * red + 0.769 * green + 0.189 * blue);
int sepiaGreen = round(0.349 * red + 0.686 * green + 0.168 * blue);
int sepiaBlue = round(0.272 * red + 0.534 * green + 0.131 * blue);
// Cap values at 255 using fmin()
// Set each RGB values to their sepia values
image[i][j].rgbtRed = fmin(255, sepiaRed);
image[i][j].rgbtGreen = fmin(255, sepiaGreen);
image[i][j].rgbtBlue = fmin(255, sepiaBlue);
}
}
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
// Temporary storage
RGBTRIPLE temp;
// Iterate over every row of the image
for (int i = 0; i < height; i++)
{
// Iterate over every column that are less than width / 2
for (int j = 0; j < width / 2; j++)
{
// Swap pixels on horizontally opposite sides
temp = image[i][j];
image[i][j] = image[i][width - j - 1];
image[i][width - j - 1] = temp;
}
}
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// Temporary storage
RGBTRIPLE temp[height][width];
// Copying the image to keep an unaltered version to loop over
memcpy(temp, image, sizeof(RGBTRIPLE) * height * width);
// Iterate over every row of the image
for (int i = 0; i < height; i++)
{
// Iterate over every column of the image
for (int j = 0; j < width; j++)
{
// Initiate average counter at 0.0
// Gotta avoid the truncated integer problem
float average = 0.0;
// Initiate RGB values at 0
int red = 0;
int green = 0;
int blue = 0;
// Iterate over rows around current row
for (int k = -1; k <= 1; k++)
{
// Iterate over columns around current column
for (int l = -1; l <= 1; l++)
{
// If current row + next row are within bounds
// If current column + next column are within bounds
if (i + k != height &&
i + k != -1 &&
j + l != width &&
j + l != -1)
{
// Update RGB values to the sum both pixels' RGB values
red += temp[i + k][j + l].rgbtRed;
green += temp[i + k][j + l].rgbtGreen;
blue += temp[i + k][j + l].rgbtBlue;
// Increment average by one for one pixel in the sum
average++;
}
}
}
// Set each RGB values to their blurred values
image[i][j].rgbtRed = round(red / average);
image[i][j].rgbtGreen = round(green / average);
image[i][j].rgbtBlue = round(blue / average);
}
}
}
recover
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover card.raw");
return 1;
}
//Open file for reading
FILE *input_file = fopen(argv[1], "r");
//if check Input_file pointer fail to open then REturn error code "Could not open file"
if (input_file == NULL)
{
printf("Could not open file");
return 2;
}
//declare a variable to unsigned char to store 512 chunks array
unsigned char buffer[512];
//for the purpose of counting of image later in the loop
int count_image = 0;
//An uninitialize file pointer to use to output data gotten from input file
FILE *output_file = NULL;
char *filename = malloc(8 * sizeof(char));
//char filename[8];
/*Read 512 bytes from input_file and store on the buffer*/
while (fread(buffer, sizeof(char), 512, input_file))
{
//check if bytes is start of a JPEG
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
//write jpeg into file name in form 001.jpg, 002.jpg and so on
sprintf(filename, "%03i.jpg", count_image);
//open Out_file for writing
output_file = fopen(filename, "w");
//fwrite(buffer, sizeof(buffer), 1, output_file);
//count number of image found
count_image++;
}
//Check if output have been used for valid input
if (output_file != NULL)
{
fwrite(buffer, sizeof(char), 512, output_file);
}
}
free(filename);
fclose(output_file);
fclose(input_file);
return 0;
}
'Coding > [EdX] CS 50' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Week 3] Algorithms (0) | 2021.12.05 |
---|---|
[Week 2] Array (0) | 2021.12.05 |
[Week1] C (0) | 2021.11.29 |
[Week 0] Scratch (0) | 2021.11.23 |
Introduction to Mathematical Thinking ๋ก ๋ณธ๊ฒฉ ์์ (0) | 2021.10.08 |