CS3413/Assignment9/src/main.c

52 lines
1.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include "png.h"
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <png file>\n", argv[0]);
exit(1);
}
if (strlen(argv[1]) > PATH_MAX) {
printf("Path too long\n");
exit(1);
}
char *path = argv[1];
char *png_buffer = load_file(get_fd(path));
if (!is_png(png_buffer)) {
printf("It's not a PNG file\n");
exit(1);
} else {
printf("It's a PNG file\n");
}
png_chunk **chunks = get_png_chunks(png_buffer);
free(png_buffer);
int size = get_number_of_chunks(chunks);
for (int i = 0; i < size; i++) {
// Check if header is IDAT or IEND
bool is_idat = memcmp(chunks[i]->type, "IDAT", 4) == 0;
bool is_iend = memcmp(chunks[i]->type, "IEND", 4) == 0;
if (is_idat || is_iend) {
printf("Found %.4s\n", chunks[i]->type);
if (is_idat) {
xor_data(chunks[i]);
}
} else {
printf("Found unknown: %.4s\n", chunks[i]->type);
}
printf("Chunk size is:%d\n", chunks[i]->length);
}
// Uncomment to not overwrite original file
//char new_path[PATH_MAX];
//getcwd(new_path, sizeof(new_path));
//strcat(new_path, "/output.png");
//write_png_chunks(new_path, chunks);
write_png_chunks(path, chunks);
destroy_chunks(chunks);
return EXIT_SUCCESS;
}