iccfrompng.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*- iccfrompng
  2. *
  3. * COPYRIGHT: Written by John Cunningham Bowler, 2011.
  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. * Extract any icc profiles found in the given PNG files. This is a simple
  9. * example of a program that extracts information from the header of a PNG file
  10. * without processing the image. Notice that some header information may occur
  11. * after the image data. Textual data and comments are an example; the approach
  12. * in this file won't work reliably for such data because it only looks for the
  13. * information in the section of the file that preceeds the image data.
  14. *
  15. * Compile and link against libpng and zlib, plus anything else required on the
  16. * system you use.
  17. *
  18. * To use supply a list of PNG files containing iCCP chunks, the chunks will be
  19. * extracted to a similarly named file with the extension replaced by 'icc',
  20. * which will be overwritten without warning.
  21. */
  22. #include <stdlib.h>
  23. #include <setjmp.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <png.h>
  27. static int verbose = 1;
  28. static png_byte no_profile[] = "no profile";
  29. static png_bytep
  30. extract(FILE *fp, png_uint_32 *proflen)
  31. {
  32. png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
  33. png_infop info_ptr = NULL;
  34. png_bytep result = NULL;
  35. /* Initialize for error or no profile: */
  36. *proflen = 0;
  37. if (png_ptr == NULL)
  38. {
  39. fprintf(stderr, "iccfrompng: version library mismatch?\n");
  40. return 0;
  41. }
  42. if (setjmp(png_jmpbuf(png_ptr)))
  43. {
  44. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  45. return 0;
  46. }
  47. png_init_io(png_ptr, fp);
  48. info_ptr = png_create_info_struct(png_ptr);
  49. if (info_ptr == NULL)
  50. png_error(png_ptr, "OOM allocating info structure");
  51. png_read_info(png_ptr, info_ptr);
  52. {
  53. png_charp name;
  54. int compression_type;
  55. png_bytep profile;
  56. if (png_get_iCCP(png_ptr, info_ptr, &name, &compression_type, &profile,
  57. proflen) & PNG_INFO_iCCP)
  58. {
  59. result = malloc(*proflen);
  60. if (result != NULL)
  61. memcpy(result, profile, *proflen);
  62. else
  63. png_error(png_ptr, "OOM allocating profile buffer");
  64. }
  65. else
  66. result = no_profile;
  67. }
  68. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  69. return result;
  70. }
  71. static int
  72. extract_one_file(const char *filename)
  73. {
  74. int result = 0;
  75. FILE *fp = fopen(filename, "rb");
  76. if (fp != NULL)
  77. {
  78. png_uint_32 proflen = 0;
  79. png_bytep profile = extract(fp, &proflen);
  80. if (profile != NULL && profile != no_profile)
  81. {
  82. size_t len;
  83. char *output;
  84. {
  85. const char *ep = strrchr(filename, '.');
  86. if (ep != NULL)
  87. len = ep-filename;
  88. else
  89. len = strlen(filename);
  90. }
  91. output = malloc(len + 5);
  92. if (output != NULL)
  93. {
  94. FILE *of;
  95. memcpy(output, filename, len);
  96. strcpy(output+len, ".icc");
  97. of = fopen(output, "wb");
  98. if (of != NULL)
  99. {
  100. if (fwrite(profile, proflen, 1, of) == 1 &&
  101. fflush(of) == 0 &&
  102. fclose(of) == 0)
  103. {
  104. if (verbose)
  105. printf("%s -> %s\n", filename, output);
  106. /* Success return */
  107. result = 1;
  108. }
  109. else
  110. {
  111. fprintf(stderr, "%s: error writing profile\n", output);
  112. if (remove(output))
  113. fprintf(stderr, "%s: could not remove file\n", output);
  114. }
  115. }
  116. else
  117. fprintf(stderr, "%s: failed to open output file\n", output);
  118. free(output);
  119. }
  120. else
  121. fprintf(stderr, "%s: OOM allocating string!\n", filename);
  122. free(profile);
  123. }
  124. else if (verbose && profile == no_profile)
  125. printf("%s has no profile\n", filename);
  126. }
  127. else
  128. fprintf(stderr, "%s: could not open file\n", filename);
  129. return result;
  130. }
  131. int
  132. main(int argc, char **argv)
  133. {
  134. int i;
  135. int extracted = 0;
  136. for (i=1; i<argc; ++i)
  137. {
  138. if (strcmp(argv[i], "-q") == 0)
  139. verbose = 0;
  140. else if (extract_one_file(argv[i]))
  141. extracted = 1;
  142. }
  143. /* Exit code is true if any extract succeeds */
  144. return extracted == 0;
  145. }