Commit e1778b8b authored by @StriderOne's avatar @StriderOne
Browse files

with file.c and catching errors in files

parent 703fb9f0
No related merge requests found
Pipeline #29024 failed with stages
in 21 seconds
......@@ -5,6 +5,8 @@
"string": "c",
"string_view": "c",
"stdio.h": "c",
"image.h": "c"
"image.h": "c",
"*.tcc": "c",
"fstream": "c"
}
}
\ No newline at end of file
No preview for this file type
File added
obj/solution/file.o: solution/src/file.c solution/include/file.h \
/usr/include/stdio.h \
/usr/include/x86_64-linux-gnu/bits/libc-header-start.h \
/usr/include/features.h /usr/include/stdc-predef.h \
/usr/include/x86_64-linux-gnu/sys/cdefs.h \
/usr/include/x86_64-linux-gnu/bits/wordsize.h \
/usr/include/x86_64-linux-gnu/bits/long-double.h \
/usr/include/x86_64-linux-gnu/gnu/stubs.h \
/usr/include/x86_64-linux-gnu/gnu/stubs-64.h \
/usr/lib/llvm-10/lib/clang/10.0.0/include/stddef.h \
/usr/lib/llvm-10/lib/clang/10.0.0/include/stdarg.h \
/usr/include/x86_64-linux-gnu/bits/types.h \
/usr/include/x86_64-linux-gnu/bits/timesize.h \
/usr/include/x86_64-linux-gnu/bits/typesizes.h \
/usr/include/x86_64-linux-gnu/bits/time64.h \
/usr/include/x86_64-linux-gnu/bits/types/__fpos_t.h \
/usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \
/usr/include/x86_64-linux-gnu/bits/types/__fpos64_t.h \
/usr/include/x86_64-linux-gnu/bits/types/__FILE.h \
/usr/include/x86_64-linux-gnu/bits/types/FILE.h \
/usr/include/x86_64-linux-gnu/bits/types/struct_FILE.h \
/usr/include/x86_64-linux-gnu/bits/stdio_lim.h \
/usr/include/x86_64-linux-gnu/bits/sys_errlist.h
solution/include/file.h:
/usr/include/stdio.h:
/usr/include/x86_64-linux-gnu/bits/libc-header-start.h:
/usr/include/features.h:
/usr/include/stdc-predef.h:
/usr/include/x86_64-linux-gnu/sys/cdefs.h:
/usr/include/x86_64-linux-gnu/bits/wordsize.h:
/usr/include/x86_64-linux-gnu/bits/long-double.h:
/usr/include/x86_64-linux-gnu/gnu/stubs.h:
/usr/include/x86_64-linux-gnu/gnu/stubs-64.h:
/usr/lib/llvm-10/lib/clang/10.0.0/include/stddef.h:
/usr/lib/llvm-10/lib/clang/10.0.0/include/stdarg.h:
/usr/include/x86_64-linux-gnu/bits/types.h:
/usr/include/x86_64-linux-gnu/bits/timesize.h:
/usr/include/x86_64-linux-gnu/bits/typesizes.h:
/usr/include/x86_64-linux-gnu/bits/time64.h:
/usr/include/x86_64-linux-gnu/bits/types/__fpos_t.h:
/usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h:
/usr/include/x86_64-linux-gnu/bits/types/__fpos64_t.h:
/usr/include/x86_64-linux-gnu/bits/types/__FILE.h:
/usr/include/x86_64-linux-gnu/bits/types/FILE.h:
/usr/include/x86_64-linux-gnu/bits/types/struct_FILE.h:
/usr/include/x86_64-linux-gnu/bits/stdio_lim.h:
/usr/include/x86_64-linux-gnu/bits/sys_errlist.h:
No preview for this file type
No preview for this file type
......@@ -36,7 +36,8 @@ obj/solution/main.o: solution/src/main.c solution/include/bmp.h \
/usr/include/x86_64-linux-gnu/bits/types/struct_FILE.h \
/usr/include/x86_64-linux-gnu/bits/stdio_lim.h \
/usr/include/x86_64-linux-gnu/bits/sys_errlist.h /usr/include/stdlib.h \
/usr/include/x86_64-linux-gnu/bits/stdlib-float.h
/usr/include/x86_64-linux-gnu/bits/stdlib-float.h \
solution/include/file.h
solution/include/bmp.h:
......@@ -123,3 +124,5 @@ solution/include/transformation.h:
/usr/include/stdlib.h:
/usr/include/x86_64-linux-gnu/bits/stdlib-float.h:
solution/include/file.h:
No preview for this file type
......@@ -13,19 +13,16 @@ enum read_status {
READ_INVALID_SIGNATURE,
READ_INVALID_BITS,
READ_INVALID_HEADER
/* коды других ошибок */
};
};
enum read_status from_bmp( const char *fileName, struct image* img );
enum read_status from_bmp( FILE* file, struct image* img );
/* serializer */
enum write_status {
WRITE_OK = 0,
WRITE_ERROR
/* коды других ошибок */
};
enum write_status to_bmp( const char *fileName, struct image * img );
enum write_status to_bmp( FILE* file, struct image * img );
#endif
#ifndef FILE_H
#define FILE_H
#include <stdio.h>
enum open_status{
OPEN_OK = 0,
OPEN_ERROR
};
enum close_status{
CLOSE_OK = 0,
CLOSE_ERROR
};
enum open_status open_file(char* filename, FILE** file, char* actionTYPE);
enum close_status close_file(FILE* file);
#endif
......@@ -29,7 +29,4 @@ void set_image_row(struct image* image, uint64_t x, struct pixel_row* new_pixel_
void free_image_memory(struct image* image);
void image_2GRAYSCALE(struct image* image);
#endif
......@@ -5,4 +5,6 @@
struct image image_rotate(struct image* img);
struct image image_2GRAYSCALE(struct image* image);
#endif
#include "bmp.h"
#define DATA_OFFSET_OFFSET 0x000A
#define WIDTH_OFFSET 0x0012
#define HEIGHT_OFFSET 0x0016
#define BITS_PER_PIXEL_OFFSET 0x001C
#define HEADER_SIZE 14
#define INFO_HEADER_SIZE 40
#define NO_COMPRESION 0
#define MAX_NUMBER_OF_COLORS 0
#define ALL_COLORS_REQUIRED 0
#pragma pack(push, 1)
struct bmp_header
{
......@@ -42,14 +31,13 @@ uint32_t padding_count(uint32_t width, uint32_t bytes_per_pixel) {
}
enum read_status from_bmp(const char *fileName, struct image* img)
enum read_status from_bmp(FILE *file, struct image* img)
{
FILE *input_image = fopen(fileName, "rb");
struct bmp_header bmp = {0};
//read file
fread(&bmp, sizeof(struct bmp_header), 1, input_image);
fread(&bmp, sizeof(struct bmp_header), 1, file);
HEADER_TEMPLATE = bmp;
// creating new image type of struct image
......@@ -66,26 +54,23 @@ enum read_status from_bmp(const char *fileName, struct image* img)
struct pixel currentPixelPointer = {0};
int64_t bmp_size = (int64_t)sizeof(struct bmp_header);
// printf("%d", (int)bmp_size);
for (uint32_t i = 0; i < bmp.biHeight; i++)
{
fseek(input_image, bmp_size+(i*paddedRowSize), SEEK_SET);
fseek(file, bmp_size+(i*paddedRowSize), SEEK_SET);
for (uint32_t j = 0; j < bmp.biWidth; j++) {
fread(&currentPixelPointer, sizeof(struct pixel), 1, input_image);
fread(&currentPixelPointer, sizeof(struct pixel), 1, file);
set_image_pixel(&image, i, j, currentPixelPointer);
}
}
*img = image;
fclose(input_image);
return READ_OK;
}
enum write_status to_bmp(const char *fileName, struct image * img ) {
FILE *output_image = fopen(fileName, "wb");
enum write_status to_bmp(FILE *file, struct image * img ) {
HEADER_TEMPLATE.biHeight = img->height;
HEADER_TEMPLATE.biWidth = img->width;
......@@ -96,16 +81,16 @@ enum write_status to_bmp(const char *fileName, struct image * img ) {
uint32_t padding = padding_count(HEADER_TEMPLATE.biWidth, bytes_per_pixel); //count of bytes in padding
fwrite(&HEADER_TEMPLATE, sizeof(struct bmp_header), 1, output_image);
fwrite(&HEADER_TEMPLATE, sizeof(struct bmp_header), 1, file);
for (uint32_t i = 0; i < img->height; i++) {
for (uint32_t j = 0; j < img->width; j++) {
struct pixel currentPixel = get_image_pixel(img, i, j);
fwrite(&currentPixel, sizeof(struct pixel), 1, output_image);
fwrite(&currentPixel, sizeof(struct pixel), 1, file);
}
fseek(output_image, padding, SEEK_CUR);
fseek(file, padding, SEEK_CUR);
}
fclose(output_image);
return WRITE_OK;
}
#include "file.h"
enum open_status open_file(char* filename, FILE** file, char* actionTYPE) {
FILE *output_image = fopen(filename, actionTYPE);
*file = output_image;
if (output_image == NULL)
return OPEN_ERROR;
// printf("%p", (void *) output_image);
return OPEN_OK;
}
enum close_status close_file(FILE* file) {
if (fclose(file) == -1)
return CLOSE_ERROR;
return CLOSE_OK;
}
......@@ -4,10 +4,7 @@
#include <stdint.h>
void create_image(struct image* image, uint64_t width, uint64_t height) {
// what do if we have no memory?
struct pixel* image_data = (struct pixel*) malloc(width*height*sizeof(struct pixel));
for (int i = 0; i < width*height; i++) {
struct pixel pixel = {0};
......@@ -19,27 +16,21 @@ void create_image(struct image* image, uint64_t width, uint64_t height) {
// input: image type of struct image, x - number of row (from 0 to height - 1), y - number of column (from 0 to weight - 1)
struct pixel get_image_pixel(const struct image* image, uint64_t x, uint64_t y) {
// what should we do if there is no image?
// if (x < 0 || x >= (image->height) || y < 0 || y >= (image->width))
// // return Error
// return (struct pixel) {0, 0, 0};
if (x < 0 || x >= (image->height) || y < 0 || y >= (image->width))
return (struct pixel) {0, 0, 0};
return image->data[x * (image->width) + y];
}
// input: image type of struct image, x - number of row (from 0 to height - 1), y - number of column (from 0 to weight - 1), new pixel - struct pixel
void set_image_pixel(struct image* image, uint64_t x, uint64_t y, struct pixel new_pixel) {
// what should we do if there is no image or incorrect pixel?
// if (x < 0 || x >= (image->height) || y < 0 || y >= (image->width))
// // return Error
// return;
if (x < 0 || x >= (image->height) || y < 0 || y >= (image->width))
return;
image->data[x * (image->width) + y] = new_pixel;
}
// input: image type of struct image, x - number of row (from 0 to height - 1), pixel_row - struct pixel_row - array of pixels - place where row will be save
void get_image_row(const struct image* image, uint64_t x, struct pixel_row* pixel_row) {
// what should we do if there is no image or incorrect pixel row?
if (x < 0 || x >= (image->height))
// return Error
return;
pixel_row->size = image->width;
for (uint64_t i = 0; i < pixel_row->size; i++)
......@@ -49,9 +40,7 @@ void get_image_row(const struct image* image, uint64_t x, struct pixel_row* pixe
// input: image type of struct image, x - number of row (from 0 to height - 1), new_pixel_row - struct pixel_row - array of pixels
void set_image_row(struct image* image, uint64_t x, struct pixel_row* new_pixel_row) {
// what should we do if there is no image or incorrect pixel row?
if (x < 0 || x >= (image->height) || new_pixel_row->size != image->width)
// return Error
return;
for (uint64_t i = 0; i < new_pixel_row->size; i++)
image->data[x * (image->width) + i] = new_pixel_row->data[i];
......@@ -63,13 +52,3 @@ void free_image_memory(struct image* image) {
image->height = 0;
free(image->data);
}
// input: image type of struct image
// convert image to gray color
void image_2GRAYSCALE(struct image* image) {
for (uint64_t i = 0; i < image->height*image->width; i++) {
struct pixel pixel = image->data[i];
uint8_t gray_color = (uint8_t)(pixel.r + pixel.g + pixel.b) / 3;
image->data[i] = (struct pixel) {gray_color, gray_color, gray_color};
}
}
solution/src/img.bmp

29.3 KB

#include "bmp.h"
#include "file.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
......@@ -11,14 +12,34 @@ int main(int argc, char** argv)
if (argc != 3) {
return 1;
}
char* input_file = argv[1];
char* output_file = argv[2];
char* input_filename = argv[1];
char* output_filename = argv[2];
struct image img = {0};
from_bmp(input_file, &img);
struct image new_img = {0};
new_img = image_rotate(&img);
free_image_memory(&img);
to_bmp(output_file, &new_img);
free_image_memory(&new_img);
FILE input_file = {0};
FILE* input_file_ptr = &input_file;
FILE output_file = {0};
FILE* output_file_ptr = &output_file;
if (open_file(input_filename, &input_file_ptr, "rb") == OPEN_OK) {
from_bmp(input_file_ptr, &img);
if (close_file(input_file_ptr) != CLOSE_OK)
return 1;
printf("HELLO");
struct image new_img = {0};
new_img = image_rotate(&img);
free_image_memory(&img);
if (open_file(output_filename, &output_file_ptr, "wb") != OPEN_OK)
return 1;
to_bmp(output_file_ptr, &new_img);
if (close_file(output_file_ptr) != CLOSE_OK)
return 1;
free_image_memory(&new_img);
} else {
return 1;
}
return 0;
}
......@@ -8,11 +8,22 @@ struct image image_rotate(struct image* img) {
for (uint32_t i = 0; i < img->height; i++) {
for (uint32_t j = 0; j < img->width; j++) {
struct pixel currentPixel = get_image_pixel(img, i, j);
set_image_pixel(&new_img, j, new_img.width - i - 1, currentPixel);
set_image_pixel(&new_img, j, new_img.width - i - 1, currentPixel);
}
}
return new_img;
}
// input: image type of struct image
// convert image to gray color
struct image image_2GRAYSCALE(struct image* image) {
struct image new_img = {0};
create_image(&new_img, image->width, image->height);
for (uint64_t i = 0; i < image->height*image->width; i++) {
struct pixel pixel = image->data[i];
uint8_t gray_color = (uint8_t)(pixel.r + pixel.g + pixel.b) / 3;
image->data[i] = (struct pixel) {gray_color, gray_color, gray_color};
}
return new_img;
}
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