Encode and decode data as PNG image.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.6KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <png.h>
  6. static unsigned int * image_buffer = NULL;
  7. static unsigned int image_width = 0;
  8. static unsigned int image_height = 0;
  9. static void import_image (char * file_path) {
  10. png_image temporary = { 0 };
  11. temporary.version = PNG_IMAGE_VERSION;
  12. if (png_image_begin_read_from_file (& temporary, file_path) == 0) {
  13. printf ("PNG: Image init fuck up!\n");
  14. exit (EXIT_FAILURE);
  15. }
  16. temporary.format = PNG_FORMAT_RGB;
  17. image_width = temporary.width;
  18. image_height = temporary.height;
  19. image_buffer = calloc ((size_t) (image_width * image_height), sizeof (* image_buffer));
  20. if (png_image_finish_read (& temporary, NULL, image_buffer, 0, NULL) == 0) {
  21. printf ("PNG: Image read fuck up!\n");
  22. exit (EXIT_FAILURE);
  23. }
  24. png_image_free (& temporary);
  25. }
  26. static void export_image (char * file_path) {
  27. png_image temporary = { 0 };
  28. temporary.version = PNG_IMAGE_VERSION;
  29. temporary.format = PNG_FORMAT_RGB;
  30. temporary.width = image_width;
  31. temporary.height = image_height;
  32. if (png_image_write_to_file (& temporary, file_path, 0, image_buffer, 0, NULL) == 0) {
  33. printf ("PNG: Image write fuck up!\n");
  34. }
  35. png_image_free (& temporary);
  36. }
  37. int main (int argc, char * * argv) {
  38. FILE * file = NULL;
  39. size_t file_size = 0;
  40. if (argc != 4) {
  41. printf ("Encode: xiew -e input output\n");
  42. printf ("Decode: xiew -d input output\n");
  43. return (EXIT_FAILURE);
  44. }
  45. if (strcmp (argv [1], "-e") == 0) {
  46. printf ("Encoding...\n");
  47. file = fopen (argv [2], "r");
  48. if (file == NULL) printf ("File is null.\n");
  49. fseek (file, 0, SEEK_END);
  50. file_size = (size_t) ftell (file);
  51. printf ("File size: %lu\n", file_size);
  52. fseek (file, 0, SEEK_SET);
  53. image_width = image_height = (unsigned int) (sqrt (((double) file_size) / 3.0) + 1);
  54. printf ("Image size: %u x %u\n", image_width, image_height);
  55. image_buffer = calloc ((size_t) (image_width * image_height), sizeof (* image_buffer));
  56. image_buffer [0] = (unsigned int) file_size;
  57. fread (& image_buffer [1], sizeof (char), file_size, file);
  58. export_image (argv [3]);
  59. } else if (strcmp (argv [1], "-d") == 0) {
  60. printf ("Decoding...\n");
  61. import_image (argv [2]);
  62. file = fopen (argv [3], "w");
  63. if (file == NULL) printf ("File is null.\n");
  64. file_size = image_buffer [0];
  65. printf ("File size: %lu\n", file_size);
  66. fwrite (& image_buffer [1], sizeof (char), file_size, file);
  67. } else {
  68. printf ("Encode: xiew -e input output\n");
  69. printf ("Decode: xiew -d input output\n");
  70. return (EXIT_FAILURE);
  71. }
  72. if (image_buffer != NULL) {
  73. free (image_buffer);
  74. }
  75. fclose (file);
  76. return (EXIT_SUCCESS);
  77. }