Finished...
This commit is contained in:
parent
07226ba96f
commit
985756685e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
xiew
|
@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
set -xe
|
set -xe
|
||||||
|
|
||||||
gcc -g -ansi -Wall -Wextra -Wpedantic -Werror -o xiew xiew.c
|
gcc -g -ansi -Wall -Wextra -Wpedantic -Werror -o xiew xiew.c -lm -lpng
|
||||||
|
|
||||||
exit
|
exit
|
||||||
|
97
xiew.c
97
xiew.c
@ -1,8 +1,97 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
#include <png.h>
|
#include <png.h>
|
||||||
|
|
||||||
int main (int argc, char * * argv) {
|
static unsigned int * image_buffer = NULL;
|
||||||
(void) argc;
|
static unsigned int image_width = 0;
|
||||||
(void) argv;
|
static unsigned int image_height = 0;
|
||||||
return (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);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (int argc, char * * argv) {
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user