pngtopng.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*- pngtopng
  2. *
  3. * COPYRIGHT: Written by John Cunningham Bowler, 2011, 2017.
  4. * To the extent possible under law, the author has waived all copyright and
  5. * related or neighboring rights to this work. This work is published from:
  6. * United States.
  7. *
  8. * Last changed in libpng 1.6.29 [March 16, 2017]
  9. *
  10. * Read a PNG and write it out in a fixed format, using the 'simplified API'
  11. * that was introduced in libpng-1.6.0.
  12. *
  13. * This sample code is just the code from the top of 'example.c' with some error
  14. * handling added. See example.c for more comments.
  15. */
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. /* Normally use <png.h> here to get the installed libpng, but this is done to
  21. * ensure the code picks up the local libpng implementation:
  22. */
  23. #include "../../png.h"
  24. #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && \
  25. defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
  26. int main(int argc, const char **argv)
  27. {
  28. int result = 1;
  29. if (argc == 3)
  30. {
  31. png_image image;
  32. /* Only the image structure version number needs to be set. */
  33. memset(&image, 0, sizeof image);
  34. image.version = PNG_IMAGE_VERSION;
  35. if (png_image_begin_read_from_file(&image, argv[1]))
  36. {
  37. png_bytep buffer;
  38. /* Change this to try different formats! If you set a colormap format
  39. * then you must also supply a colormap below.
  40. */
  41. image.format = PNG_FORMAT_RGBA;
  42. buffer = malloc(PNG_IMAGE_SIZE(image));
  43. if (buffer != NULL)
  44. {
  45. if (png_image_finish_read(&image, NULL/*background*/, buffer,
  46. 0/*row_stride*/, NULL/*colormap for PNG_FORMAT_FLAG_COLORMAP */))
  47. {
  48. if (png_image_write_to_file(&image, argv[2],
  49. 0/*convert_to_8bit*/, buffer, 0/*row_stride*/,
  50. NULL/*colormap*/))
  51. result = 0;
  52. else
  53. fprintf(stderr, "pngtopng: write %s: %s\n", argv[2],
  54. image.message);
  55. }
  56. else
  57. fprintf(stderr, "pngtopng: read %s: %s\n", argv[1],
  58. image.message);
  59. free(buffer);
  60. }
  61. else
  62. {
  63. fprintf(stderr, "pngtopng: out of memory: %lu bytes\n",
  64. (unsigned long)PNG_IMAGE_SIZE(image));
  65. /* This is the only place where a 'free' is required; libpng does
  66. * the cleanup on error and success, but in this case we couldn't
  67. * complete the read because of running out of memory and so libpng
  68. * has not got to the point where it can do cleanup.
  69. */
  70. png_image_free(&image);
  71. }
  72. }
  73. else
  74. /* Failed to read the first argument: */
  75. fprintf(stderr, "pngtopng: %s: %s\n", argv[1], image.message);
  76. }
  77. else
  78. /* Wrong number of arguments */
  79. fprintf(stderr, "pngtopng: usage: pngtopng input-file output-file\n");
  80. return result;
  81. }
  82. #endif /* READ && WRITE */