Implemented file reader function
This commit is contained in:
parent
8c64bbfc26
commit
e4cb965234
22
src/main.c
22
src/main.c
@ -6,32 +6,32 @@
|
|||||||
#include "magic_memory.h"
|
#include "magic_memory.h"
|
||||||
#include "pixeldata_utils.h"
|
#include "pixeldata_utils.h"
|
||||||
#include "ciff_tools.h"
|
#include "ciff_tools.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
magic_memory_context_t *context = magic_memory_begin();
|
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");
|
if (result != FILE_READ_SUCCESS) {
|
||||||
|
printf("File read failure: %d", result);
|
||||||
fseek(fp, 0L, SEEK_END);
|
return 1;
|
||||||
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);
|
|
||||||
|
|
||||||
uint64_t width;
|
uint64_t width;
|
||||||
uint64_t height;
|
uint64_t height;
|
||||||
uint64_t pixel_data_starts;
|
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;
|
uint64_t pixel_data_size = width * height * 3;
|
||||||
|
|
||||||
if (result == CIFF_PARSE_SUCCESS) {
|
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) !=
|
if (flip_image(ciff_file + pixel_data_starts, flipped_image, pixel_data_size, width, height) !=
|
||||||
IMAGE_FLIP_SUCCESS) {
|
IMAGE_FLIP_SUCCESS) {
|
||||||
printf("Literally failed to flip the image");
|
printf("Literally failed to flip the image");
|
||||||
|
41
src/utils.c
41
src/utils.c
@ -2,8 +2,11 @@
|
|||||||
// Created by marcsello on 05/11/2020.
|
// Created by marcsello on 05/11/2020.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "utils.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++) {
|
for (uint64_t i = 0; i < data_len; i++) {
|
||||||
if (data[i] == what) {
|
if (data[i] == what) {
|
||||||
@ -12,3 +15,41 @@ bool contains(const uint8_t* data, uint64_t data_len, uint8_t what) {
|
|||||||
}
|
}
|
||||||
return false;
|
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;
|
||||||
|
}
|
@ -8,7 +8,16 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.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);
|
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
|
#endif //CAFF_PREVIEWER_UTILS_H
|
||||||
|
Loading…
Reference in New Issue
Block a user