Implemented main

This commit is contained in:
Pünkösd Marcell 2020-11-06 03:42:57 +01:00
parent 45f1705168
commit f27830e07a

View File

@ -1,5 +1,4 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdint.h> #include <stdint.h>
#include "libtarga.h" #include "libtarga.h"
@ -9,56 +8,90 @@
#include "caff_tools.h" #include "caff_tools.h"
#include "utils.h" #include "utils.h"
int main() { #define LIBTARGA_ERROR_PREFIX 0x40
magic_memory_context_t *context = magic_memory_begin(); #define USAGE_ERROR_PREFIX 0x50
#define USAGE_ERROR_WRONG_PARAMETERS (USAGE_ERROR_PREFIX + 0x01)
uint8_t *caff_file; #define OTHER_ERROR_PREFIX 0x60
uint64_t caff_size; #define OTHER_FLIP_COULD_NOT_ALLOCATE (OTHER_ERROR_PREFIX + 0x01)
uint8_t result = read_file_to_mem(context, "/home/marcsello/Documents/etyetem/szamitobiztonsag/caff/2.caff",
67108864, &caff_file, &caff_size); // Mivel az egyszerűség kedvéért az egész fájlt memóriába olvassuk, jobb ha limitáljuk a méretét
#define CAFF_MAX_SIZE 536870912 // 512MB
uint8_t perform_extraction(mm_ctx context, const char *infile, const char *outfile) {
// Read the entire CAFF data to memory
uint8_t *caff_data;
uint64_t caff_data_len;
uint8_t result = read_file_to_mem(context, infile, CAFF_MAX_SIZE, &caff_data, &caff_data_len);
if (result != FILE_READ_SUCCESS) { if (result != FILE_READ_SUCCESS) {
printf("File read failure: %d", result); return result;
return 1;
} }
uint8_t* ciff_ptr; // Seek for the first CIFF
uint64_t ciff_size; uint8_t *ciff_data;
uint64_t ciff_data_len;
result = parse_caff_get_first_ciff(caff_file,caff_size,&ciff_ptr, &ciff_size); result = parse_caff_get_first_ciff(caff_data, caff_data_len, &ciff_data, &ciff_data_len);
if (result != CAFF_PARSE_SUCCESS) { if (result != CAFF_PARSE_SUCCESS) {
printf("CAFF Parse failure: %d", result); return result;
return 1;
} }
// Extract pixel data from the CIFF
uint64_t width; uint64_t width;
uint64_t height; uint64_t height;
uint64_t pixel_data_starts; uint8_t *pixel_data;
uint64_t pixel_data_len;
result = get_pixel_data_from_ciff(ciff_ptr, ciff_size, &width, &height, &pixel_data_starts); result = parse_ciff_get_pixel_data(ciff_data, ciff_data_len, &pixel_data, &pixel_data_len, &width, &height);
uint64_t pixel_data_size = width * height * 3; if (result != CIFF_PARSE_SUCCESS) {
return result;
if (result == CIFF_PARSE_SUCCESS) {
uint8_t *flipped_image = (uint8_t *) magic_malloc(context, caff_size - pixel_data_starts);
if (flip_image(ciff_ptr + pixel_data_starts, flipped_image, pixel_data_size, width, height) !=
IMAGE_FLIP_SUCCESS) {
printf("Literally failed to flip the image");
} else {
if (!tga_write_raw("test.tga", width, height, flipped_image, TGA_TRUECOLOR_24)) {
printf("%s", tga_error_string(tga_get_last_error()));
}
} }
// Flip the image as required by libtarga
} else { uint8_t *flipped_pixel_data = (uint8_t *) magic_malloc(context, pixel_data_len);
printf("Failed to get pixel data: %d", result);
if (flipped_pixel_data == NULL) {
return OTHER_FLIP_COULD_NOT_ALLOCATE;
} }
result = flip_image(pixel_data, flipped_pixel_data, pixel_data_len, width, height);
magic_cleanup(context); // Free all memory used automagically if (result != IMAGE_FLIP_SUCCESS) {
return result;
}
// Write out the image to a TGA file
if (!tga_write_raw(outfile, width, height, flipped_pixel_data, TGA_TRUECOLOR_24)) {
return LIBTARGA_ERROR_PREFIX + tga_get_last_error(); // TGA errors range from 1 to 11
}
// And we are done! MagicMemory will take care of cleaning up
return 0; return 0;
} }
int main(int argc, char *argv[]) {
// Check arguments
if (argc != 3) {
printf("Usage: caff_previewer [SOURCE FILE] [DESTINATION FILE]\n");
return USAGE_ERROR_WRONG_PARAMETERS;
}
// Open a magic memory context
mm_ctx context = magic_memory_begin();
// Run the program
uint8_t result = perform_extraction(context, argv[1], argv[2]);
// Free all memory used automagically
magic_cleanup(context); // In C11 we could have used atexit()
return result;
}