Implemented file reader function

This commit is contained in:
Pünkösd Marcell 2020-11-05 19:56:16 +01:00
parent 8c64bbfc26
commit e4cb965234
3 changed files with 62 additions and 12 deletions

View File

@ -6,32 +6,32 @@
#include "magic_memory.h"
#include "pixeldata_utils.h"
#include "ciff_tools.h"
#include "utils.h"
int main() {
magic_memory_context_t *context = magic_memory_begin();
uint8_t *ciff_file;
uint64_t ciff_size;
uint8_t result = read_file_to_mem(context, "/home/marcsello/Documents/etyetem/szamitobiztonsag/caff/1.ciff",
67108864, &ciff_file, &ciff_size);
FILE *fp = fopen("/home/marcsello/Documents/etyetem/szamitobiztonsag/caff/1.ciff", "rb");
fseek(fp, 0L, SEEK_END);
uint64_t fsize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
uint8_t *ciff_file = (uint8_t *) magic_malloc(context, fsize);
fread(ciff_file, 1, fsize, fp);
fclose(fp);
if (result != FILE_READ_SUCCESS) {
printf("File read failure: %d", result);
return 1;
}
uint64_t width;
uint64_t height;
uint64_t pixel_data_starts;
uint8_t result = parse_ciff_from_mem(ciff_file, fsize, &width, &height, &pixel_data_starts);
result = get_pixel_data_from_ciff(ciff_file, ciff_size, &width, &height, &pixel_data_starts);
uint64_t pixel_data_size = width * height * 3;
if (result == CIFF_PARSE_SUCCESS) {
uint8_t *flipped_image = (uint8_t *) magic_malloc(context, fsize - pixel_data_starts);
uint8_t *flipped_image = (uint8_t *) magic_malloc(context, ciff_size - pixel_data_starts);
if (flip_image(ciff_file + pixel_data_starts, flipped_image, pixel_data_size, width, height) !=
IMAGE_FLIP_SUCCESS) {
printf("Literally failed to flip the image");

View File

@ -2,9 +2,12 @@
// Created by marcsello on 05/11/2020.
//
#include <stdio.h>
#include "utils.h"
bool contains(const uint8_t* data, uint64_t data_len, uint8_t what) {
bool contains(const uint8_t *data, uint64_t data_len, uint8_t what) {
for (uint64_t i = 0; i < data_len; i++) {
if (data[i] == what) {
return true;
@ -12,3 +15,41 @@ bool contains(const uint8_t* data, uint64_t data_len, uint8_t what) {
}
return false;
}
uint8_t read_file_to_mem(mm_ctx context, const char *filename, uint64_t max_len, uint8_t **data, uint64_t *data_len) {
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
return FILE_READ_COULD_NOT_OPEN;
}
fseek(fp, 0L, SEEK_END);
uint64_t fsize = ftell(fp);
if (fsize > max_len) {
fclose(fp);
return FILE_READ_TOO_BIG;
}
fseek(fp, 0L, SEEK_SET);
uint8_t *contents = (uint8_t *) magic_malloc(context, fsize);
if (contents == NULL) {
fclose(fp);
return FILE_READ_COULD_NOT_ALLOCATE;
}
uint64_t bytes_read = fread(contents, 1, fsize, fp);
if (bytes_read != fsize) {
fclose(fp);
return FILE_READ_ERROR;
}
*data = contents;
*data_len = fsize;
fclose(fp);
return FILE_READ_SUCCESS;
}

View File

@ -8,7 +8,16 @@
#include <stdint.h>
#include <stdbool.h>
#include "magic_memory.h"
#define FILE_READ_COULD_NOT_OPEN 1
#define FILE_READ_TOO_BIG 2
#define FILE_READ_COULD_NOT_ALLOCATE 3
#define FILE_READ_ERROR 4
#define FILE_READ_SUCCESS 0
bool contains(const uint8_t *data, uint64_t data_len, uint8_t what);
uint8_t read_file_to_mem(mm_ctx context, const char *filename, uint64_t max_len, uint8_t **data, uint64_t *data_len);
#endif //CAFF_PREVIEWER_UTILS_H