Commit a7b206ad authored by chertila1's avatar chertila1
Browse files

New file commit

parent e6173fd2
#ifndef ASSIGNMENT_IMAGE_ROTATION_BMP_H
#define ASSIGNMENT_IMAGE_ROTATION_BMP_H
#include <mm_malloc.h>
#include <stdint.h>
#include <stdio.h>
#include "../include/picture.h"
struct __attribute__((packed)) bmp_header {
uint16_t bfType;
uint32_t bfileSize;
uint32_t bfReserved;
uint32_t bOffBits;
uint32_t biSize;
uint32_t biWidth;
uint32_t biHeight;
uint16_t biPlanes;
uint16_t biBitCount;
uint32_t biCompression;
uint32_t biSizeImage;
uint32_t biXPelsPerMeter;
uint32_t biYPelsPerMeter;
uint32_t biClrUsed;
uint32_t biClrImportant;
};
enum read_status {
READ_OK = 0,
READ_ERROR,
SIGNATURE_INVALID,
HEADER_INVALID,
};
enum write_status {
WRITE_OK = 0,
WRITE_ERROR,
WRITE_CONTINUE
};
extern const char *read_status_message[];
extern const char *write_status_message[];
enum read_status from_bmp(FILE *in, struct picture *picture);
enum write_status to_bmp(FILE *out, const struct picture *picture);
#endif //ASSIGNMENT_IMAGE_ROTATION_BMP_H
#ifndef ASSIGNMENT_IMAGE_ROTATION_IO_H
#define ASSIGNMENT_IMAGE_ROTATION_IO_H
#include <stdbool.h>
#include <stdio.h>
enum io_return_code {
OPEN_SUCCESS = 0,
CLOSE_SUCCESS,
OPEN_FAILED,
CLOSE_FAILED,
FILE_CLOSED,
};
extern const char *io_return_code_string[];
enum io_return_code file_open(const char *file_name, FILE **file, const char *mode);
enum io_return_code file_close(FILE *file);
#endif //ASSIGNMENT_IMAGE_ROTATION_IO_H
#ifndef ASSIGNMENT_IMAGE_ROTATION_IMAGE_H
#define ASSIGNMENT_IMAGE_ROTATION_IMAGE_H
#include <stdbool.h>
#include <stdint.h>
struct pixel {
uint8_t r;
uint8_t g;
uint8_t b;
};
struct picture {
uint64_t width;
uint64_t height;
struct pixel *data;
};
struct picture some_picture(const uint64_t width, const uint64_t height, struct pixel *data);
struct picture picture_create(const uint64_t picture_width, const uint64_t picture_height);
void picture_destroy(const struct picture *picture);
#endif //ASSIGNMENT_IMAGE_ROTATION_IMAGE_H
#ifndef ASSIGNMENT_IMAGE_ROTATION_ROTATE_H
#define ASSIGNMENT_IMAGE_ROTATION_ROTATE_H
#include "../include/picture.h"
struct picture transform(struct picture source);
#endif //ASSIGNMENT_IMAGE_ROTATION_ROTATE_H
#include "../include/bmp.h"
#include "stdint.h"
#include <stdbool.h>
#define BFTYPE 19778
#define BIPLANES 1
#define BISIZE 40
#define BPC 24
#define COMPR 0
#define X_PPM 0
#define Y_PPM 0
#define IMP_COLORS 0
#define NUM_COLORS 0
#define HEADER_BITS 54
const char *read_status_message[] = {
[READ_OK] = "The read was successful",
[READ_ERROR] = "Read wasn't successful",
[SIGNATURE_INVALID] = "Signature of BMP file is invalid",
[HEADER_INVALID] = "BMP file header is invalid"
};
const char *write_status_message[] = {
[WRITE_OK] = "The write was successful",
[WRITE_ERROR] = "Unable to write BMP"
};
static size_t padding_size(const size_t width) {
return width % 4 == 0 ? 0 : 4 - ((width * sizeof(struct pixel)) % 4);
}
static enum read_status read_head(FILE *file, struct bmp_header *header) {
return fread(header, sizeof(struct bmp_header), 1, file);
}
static size_t picture_size(const struct picture *picture) {
return (picture->width * sizeof(struct pixel) + padding_size(picture->width)) * picture->height;
}
static size_t file_size(const struct picture *picture) {
return picture_size(picture) + sizeof(struct bmp_header);
}
static struct bmp_header create_header(const struct picture *picture) {
return (struct bmp_header) {
.bfType = BFTYPE,
.bfileSize = file_size(picture),
.bOffBits = HEADER_BITS,
.biSize = BISIZE,
.biWidth = picture->width,
.biHeight = picture->height,
.biPlanes = BIPLANES,
.biBitCount = BPC,
.biCompression = COMPR,
.biSizeImage = picture_size(picture),
.biXPelsPerMeter = X_PPM,
.biYPelsPerMeter = Y_PPM,
.biClrUsed = NUM_COLORS,
.biClrImportant = IMP_COLORS
};
}
static enum read_status header_check_valid(struct bmp_header *header) {
return header->bfType == 0x4D42;
}
enum read_status from_bmp(FILE *in, struct picture *picture) {
struct bmp_header header = {0};
if (!read_head(in, &header)) return HEADER_INVALID;
if (!header_check_valid(&header)) return SIGNATURE_INVALID;
*picture = picture_create(header.biWidth, header.biHeight);
const size_t padding = padding_size(picture->width);
for (size_t i = 0; i < picture->height; ++i) {
for (size_t j = 0; j < picture->width; ++j) {
if (!fread(&(picture->data[picture->width * i + j]), sizeof(struct pixel), 1, in))
return READ_ERROR;
}
if (fseek(in, (long) padding, SEEK_CUR))
return READ_ERROR;
}
return READ_OK;
}
enum write_status to_bmp(FILE *out, const struct picture *picture) {
struct bmp_header header = create_header(picture);
if (!fwrite(&header, sizeof(struct bmp_header), 1, out))
return WRITE_ERROR;
fseek(out, header.bOffBits, SEEK_SET);
const int8_t zero = 0;
const size_t padding = padding_size(picture->width);
if (picture->data != NULL) {
for (size_t i = 0; i < picture->height; ++i) {
if (fwrite(picture->data + i * picture->width, sizeof(struct pixel), picture->width, out) != picture->width)
return WRITE_ERROR;
for (size_t j = 0; j < padding; ++j) {
if (fwrite(&zero, 1, 1, out) != 1)
return WRITE_ERROR;
}
}
} else {
return WRITE_ERROR;
}
return WRITE_OK;
}
#include "../include/io.h"
const char *io_return_code_string[] = {
[OPEN_FAILED] = "Error occurred while opening the file",
[OPEN_SUCCESS] = "File opened",
[CLOSE_FAILED] = "Error occurred while closing the file",
[CLOSE_SUCCESS] = "File closed",
[FILE_CLOSED] = "Program can't deal with a closed file"
};
enum io_return_code file_open(const char *file_name, FILE **file, const char *mode) {
*file = fopen(file_name, mode);
if (!*file)
return OPEN_FAILED;
return OPEN_SUCCESS;
}
enum io_return_code file_close(FILE *file) {
if (!file)
return FILE_CLOSED;
if (fclose(file))
return CLOSE_FAILED;
return CLOSE_SUCCESS;
}
enum io_return_code file_read(const char *file_name, FILE **file) {
const char *mode = "r";
return file_open(file_name, file, mode);
}
enum io_return_code file_write(const char *file_name, FILE **file) {
const char *mode = "w";
return file_open(file_name, file, mode);
}
#ifndef ASSIGNMENT_IMAGE_ROTATION_IO_H
#define ASSIGNMENT_IMAGE_ROTATION_IO_H
#include <stdbool.h>
#include <stdio.h>
enum io_return_code {
OPEN_SUCCESS = 0,
CLOSE_SUCCESS,
OPEN_FAILED,
CLOSE_FAILED,
FILE_CLOSED,
};
extern const char *io_return_code_string[];
enum io_return_code file_open(const char *file_name, FILE **file, const char *mode);
enum io_return_code file_close(FILE *file);
#endif //ASSIGNMENT_IMAGE_ROTATION_IO_H
#include "../include/picture.h"
#include "mm_malloc.h"
#include <stdlib.h>
struct picture some_picture(const uint64_t width, const uint64_t height, struct pixel *data) {
return (struct picture) {.width = width, .height = height, .data = data};
}
struct picture picture_create(const uint64_t width, const uint64_t height) {
struct pixel *pixels = malloc(sizeof(struct pixel) * width * height);
return some_picture(width, height, pixels);
}
void picture_destroy(const struct picture *picture) {
free(picture->data);
}
#include "../include/rotate.h"
#include <mm_malloc.h>
static uint64_t pixel_index_output(size_t i, size_t j, struct picture source) {
return source.height * j + (source.height - 1 - i);
}
static uint64_t pixel_index_input(size_t i, size_t j, struct picture source) {
return i * source.width + j;
}
static struct picture rotate_left(const struct picture source, struct pixel *pixels) {
uint64_t input_index;
uint64_t output_index;
for (size_t i = 0; i < source.height; ++i) {
for (size_t j = 0; j < source.width; ++j) {
input_index = pixel_index_input(i, j, source);
output_index = pixel_index_output(i, j, source);
pixels[output_index] = source.data[input_index];
}
}
return some_picture(source.height, source.width, pixels);
}
struct picture transform(const struct picture source) {
if (source.data == NULL)
return some_picture(source.width, source.height, NULL);
struct pixel *pixels = malloc(sizeof(struct pixel) * source.width * source.height);
return rotate_left(source, pixels);
}
......@@ -5,7 +5,7 @@
#include "common.h"
#include "io.h"
void usage() {
void usage(void) {
fprintf(stderr,
"Usage: ./" EXECUTABLE_NAME " BMP_FILE_NAME1 BMP_FILE_NAME2\n");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment