Commit bf9eefd4 authored by Роман Мищенко's avatar Роман Мищенко
Browse files

tests passed

parent a7d71594
No related merge requests found
Pipeline #119755 failed with stages
in 6 seconds
......@@ -2,8 +2,7 @@
#define IMAGE_TRANSFORM_BMP_H
#include <image.h>
#include <read_status.h>
#include <write_status.h>
#include <io.h>
#include <stdint.h>
#include <stdio.h>
......@@ -32,7 +31,7 @@ bmp_header {
};
enum read_status from_bmp(FILE* in, struct image* img);
enum write_status to_bmp(FILE* file, struct image* img);
enum write_status to_bmp(FILE* dest, struct image* img);
#endif
......@@ -9,11 +9,12 @@ struct image {
struct pixel* data;
};
void none(struct image* init_pic, struct image* final_pic);
void cw_90(struct image* init_pic, struct image* final_pic);
void ccw_90(struct image* init_pic, struct image* final_pic);
void flip_v(struct image* init_pic, struct image* final_pic);
void flip_h(struct image* init_pic, struct image* final_pic);
void transform_image(struct image* first_image, struct image* second_image, char* option);
void none(struct image* first_image, struct image* second_image);
void cw_90(struct image* first_image, struct image* second_image);
void ccw_90(struct image* first_image, struct image* second_image);
void flip_v(struct image* first_image, struct image* second_image);
void flip_h(struct image* first_image, struct image* second_image);
#endif
......@@ -7,14 +7,12 @@ enum read_status {
READ_OK = 0,
READ_INVALID_SIGNATURE,
READ_INVALID_BITS,
READ_INVALID_HEADER,
READ_OPEN_FAIL
READ_INVALID_HEADER
};
enum write_status {
WRITE_OK = 0,
WRITE_ERROR,
WRITE_OPEN_FAIL
WRITE_ERROR
};
enum read_status read_img(char* path, struct image* img);
......
#ifndef IMAGE_TRANSFORM_UTILS_H
#define IMAGE_TRANSFORM_UTILS_H
#include <image.h>
void free_img_data(struct image* image);
#endif
#include <bmp.h>
#include <stdlib.h>
#include <io.h>
enum read_status validate(struct bmp_header* headers, FILE* source){
if(fread(headers, sizeof(struct bmp_header), 1, source) != 1 || headers->bfileSize <= HEADER_NORMAL_SIZE) return READ_INVALID_HEADER;
if(headers->biPlanes != 1 || headers->biBitCount != BITS_PER_PIXEL) return READ_INVALID_BITS;
if(headers->bfType != BMP) return READ_INVALID_SIGNATURE;
return READ_OK;
}
enum read_status from_bmp(FILE* source, struct image* img){
enum read_status status;
struct bmp_header headers;
status = validate(&headers, source);
img->data = malloc(headers.biHeight * headers.biWidth * sizeof(struct pixel));
if(!img->data) return READ_INVALID_BITS;
img->height = headers.biHeight;
img->width = headers.biWidth;
uint32_t padding_size = (4 - (img->width * 3) % 4) % 4;
for (size_t i = 0; i < img->height; i++) {
if(fread(img->data + i * img->width, sizeof(struct pixel), img->width, source) != img->width) return READ_INVALID_BITS;
fseek(source, padding_size, SEEK_CUR);
}
return status;
}
enum write_status to_bmp(FILE* dest, struct image* img){
struct bmp_header header = {0};
header.bfType = BMP;
header.bOffBits = HEADER_NORMAL_SIZE;
header.biSize = 40;
header.biHeight = img->height;
header.biWidth = img->width;
header.biPlanes = 1;
header.biBitCount = BITS_PER_PIXEL;
size_t padding_size = (4 - (header.biWidth * 3) % 4) % 4;
uint8_t padding_byte = 0;
header.biSizeImage = (header.biWidth * sizeof(struct pixel) + padding_size) * header.biHeight;
header.bfileSize = HEADER_NORMAL_SIZE + header.biSizeImage;
if(fwrite(&header, HEADER_NORMAL_SIZE, 1, dest) != 1)
return WRITE_ERROR;
for (size_t i = 0; i < img->height; i++) {
if (fwrite(img->data + i * img->width, sizeof(struct pixel), img->width, dest) != img->width)
return WRITE_ERROR;
for (size_t j = 0; j < padding_size; j++) {
if (fwrite(&padding_byte, 1, 1, dest) != 1)
return WRITE_ERROR;
}
}
free(img->data);
return WRITE_OK;
}
#include <bmp.h>
#include <image.h>
#include <io.h>
#include <stdio.h>
int main( int argc, char** argv ) {
(void) argc; (void) argv; // supress 'unused parameters' warning
if(argc != 4) {
fprintf(stderr, "Error: invalid count of arguments!");
return 1;
}
struct image first_image = {0};
FILE* source = fopen(argv[1], "rb");
if(!source) return 2;
enum read_status status_r = from_bmp(source, &first_image);
if(status_r != READ_OK) return 12;
fclose(source);
struct image second_image = {0};
transform_image(&first_image, &second_image, argv[3]);
FILE* destination = fopen(argv[2], "wb");
if(!destination) return 4;
enum write_status status_w = to_bmp(destination, &second_image);
if(status_w != WRITE_OK) return 3;
fclose(destination);
return 0;
}
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "image.h"
bool create_image(struct image* second_image, struct image* first_image){
second_image->height = first_image->height;
second_image->width = first_image->width;
second_image->data = malloc(first_image->height * first_image->width * sizeof(struct pixel));
return second_image->data != NULL;
}
void none(struct image* first_image, struct image* second_image){
if(!create_image(second_image, first_image)) return ;
for (int64_t i = 0; i < second_image->height; i++) {
for (int64_t j = 0; j < second_image->width; j++) {
second_image->data[i * second_image->width + j] = first_image->data[i * first_image->width + j];
}
}
}
void flip_h(struct image* first_image, struct image* second_image){
if(!create_image(second_image, first_image)) return ;
for (int64_t i = 0; i < first_image->height; i++) {
for (int64_t j = 0; j < first_image->width; j++) {
second_image->data[i * second_image->width + (first_image->width - 1 - j)] =
first_image->data[i * first_image->width + j];
}
}
}
void flip_v(struct image* first_image, struct image* second_image){
if(!create_image(second_image, first_image)) return ;
for (int64_t i = 0; i < first_image->height; i++) {
for (int64_t j = 0; j < first_image->width; j++) {
second_image->data[(first_image->height - 1 - i) * second_image->width + j] =
first_image->data[i * first_image->width + j];
}
}
}
void cw90(struct image* first_image, struct image* second_image){
second_image->width = first_image->height;
second_image->height = first_image->width;
second_image->data = malloc(second_image->height * second_image->width * sizeof(struct pixel));
if(!second_image->data) return;
for (int64_t i = 0; i < first_image->height; i++) {
for (int64_t j = 0; j < first_image->width; j++) {
second_image->data[j * second_image->width + i] =
first_image->data[i * first_image->width + (first_image->width - 1 - j)];
}
}
}
void ccw90(struct image* first_image, struct image* second_image){
second_image->width = first_image->height;
second_image->height = first_image->width;
second_image->data = malloc(second_image->width * second_image->height * sizeof(struct pixel));
if(!second_image->data) return;
for (int64_t i = 0; i < first_image->height; i++) {
for (int64_t j = 0; j < first_image->width; j++) {
second_image->data[j * second_image->width + first_image->height - 1 - i] =
first_image->data[i * first_image->width + j];
}
}
}
void transform_image(struct image* first_image, struct image* second_image, char* option){
if (strcmp(option, "none") == 0)
none(first_image, second_image);
if (strcmp(option, "fliph") == 0)
flip_h(first_image, second_image);
if (strcmp(option, "flipv") == 0)
flip_v(first_image, second_image);
if (strcmp(option, "ccw90") == 0)
ccw90(first_image, second_image);
if (strcmp(option, "cw90") == 0)
cw90(first_image, second_image);
free(first_image->data);
}
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