xiew/xiew.c

98 lines
2.6 KiB
C
Raw Normal View History

2024-04-02 16:24:14 -04:00
#include <stdio.h>
2024-04-02 17:40:59 -04:00
#include <stdlib.h>
#include <string.h>
#include <math.h>
2024-04-02 16:24:14 -04:00
#include <png.h>
2024-04-02 17:40:59 -04:00
static unsigned int * image_buffer = NULL;
static unsigned int image_width = 0;
static unsigned int image_height = 0;
static void import_image (char * file_path) {
png_image temporary = { 0 };
temporary.version = PNG_IMAGE_VERSION;
if (png_image_begin_read_from_file (& temporary, file_path) == 0) {
printf ("PNG: Image init fuck up!\n");
exit (EXIT_FAILURE);
}
temporary.format = PNG_FORMAT_RGB;
image_width = temporary.width;
image_height = temporary.height;
image_buffer = calloc ((size_t) (image_width * image_height), sizeof (* image_buffer));
if (png_image_finish_read (& temporary, NULL, image_buffer, 0, NULL) == 0) {
printf ("PNG: Image read fuck up!\n");
exit (EXIT_FAILURE);
}
png_image_free (& temporary);
}
static void export_image (char * file_path) {
png_image temporary = { 0 };
temporary.version = PNG_IMAGE_VERSION;
temporary.format = PNG_FORMAT_RGB;
temporary.width = image_width;
temporary.height = image_height;
if (png_image_write_to_file (& temporary, file_path, 0, image_buffer, 0, NULL) == 0) {
printf ("PNG: Image write fuck up!\n");
}
png_image_free (& temporary);
}
2024-04-02 16:24:14 -04:00
int main (int argc, char * * argv) {
2024-04-02 17:40:59 -04:00
FILE * file = NULL;
size_t file_size = 0;
if (argc != 4) {
printf ("Encode: xiew -e input output\n");
printf ("Decode: xiew -d input output\n");
return (EXIT_FAILURE);
}
if (strcmp (argv [1], "-e") == 0) {
printf ("Encoding...\n");
file = fopen (argv [2], "r");
if (file == NULL) printf ("File is null.\n");
fseek (file, 0, SEEK_END);
file_size = (size_t) ftell (file);
printf ("File size: %lu\n", file_size);
fseek (file, 0, SEEK_SET);
image_width = image_height = (unsigned int) (sqrt (((double) file_size) / 3.0) + 1);
printf ("Image size: %u x %u\n", image_width, image_height);
image_buffer = calloc ((size_t) (image_width * image_height), sizeof (* image_buffer));
image_buffer [0] = (unsigned int) file_size;
fread (& image_buffer [1], sizeof (char), file_size, file);
export_image (argv [3]);
} else if (strcmp (argv [1], "-d") == 0) {
printf ("Decoding...\n");
import_image (argv [2]);
file = fopen (argv [3], "w");
if (file == NULL) printf ("File is null.\n");
file_size = image_buffer [0];
printf ("File size: %lu\n", file_size);
fwrite (& image_buffer [1], sizeof (char), file_size, file);
} else {
printf ("Encode: xiew -e input output\n");
printf ("Decode: xiew -d input output\n");
return (EXIT_FAILURE);
}
if (image_buffer != NULL) {
free (image_buffer);
}
fclose (file);
return (EXIT_SUCCESS);
2024-04-02 16:24:14 -04:00
}