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:
8 * Extract any icc profiles found in the given PNG files. This is a simple
9 * example of a program which 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.
15 * Compile and link against libpng and zlib, plus anything else required on the
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.
29 static int verbose
= 1;
30 static png_byte no_profile
[] = "no profile";
33 extract(FILE *fp
, png_uint_32
*proflen
)
35 png_structp png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
,0,0,0);
36 png_infop info_ptr
= NULL
;
37 png_bytep result
= NULL
;
39 /* Initialize for error or no profile: */
44 fprintf(stderr
, "iccfrompng: version library mismatch?\n");
48 if (setjmp(png_jmpbuf(png_ptr
)))
50 png_destroy_read_struct(&png_ptr
, &info_ptr
, NULL
);
54 png_init_io(png_ptr
, fp
);
56 info_ptr
= png_create_info_struct(png_ptr
);
58 png_error(png_ptr
, "OOM allocating info structure");
60 png_read_info(png_ptr
, info_ptr
);
67 if (png_get_iCCP(png_ptr
, info_ptr
, &name
, &compression_type
, &profile
,
68 proflen
) & PNG_INFO_iCCP
)
70 result
= malloc(*proflen
);
72 memcpy(result
, profile
, *proflen
);
75 png_error(png_ptr
, "OOM allocating profile buffer");
82 png_destroy_read_struct(&png_ptr
, &info_ptr
, NULL
);
87 extract_one_file(const char *filename
)
90 FILE *fp
= fopen(filename
, "rb");
94 png_uint_32 proflen
= 0;
95 png_bytep profile
= extract(fp
, &proflen
);
97 if (profile
!= NULL
&& profile
!= no_profile
)
103 const char *ep
= strrchr(filename
, '.');
109 len
= strlen(filename
);
112 output
= malloc(len
+ 5);
117 memcpy(output
, filename
, len
);
118 strcpy(output
+len
, ".icc");
120 of
= fopen(output
, "wb");
123 if (fwrite(profile
, proflen
, 1, of
) == 1 &&
128 printf("%s -> %s\n", filename
, output
);
135 fprintf(stderr
, "%s: error writing profile\n", output
);
137 fprintf(stderr
, "%s: could not remove file\n", output
);
142 fprintf(stderr
, "%s: failed to open output file\n", output
);
148 fprintf(stderr
, "%s: OOM allocating string!\n", filename
);
153 else if (verbose
&& profile
== no_profile
)
154 printf("%s has no profile\n", filename
);
158 fprintf(stderr
, "%s: could not open file\n", filename
);
164 main(int argc
, char **argv
)
169 for (i
=1; i
<argc
; ++i
)
171 if (strcmp(argv
[i
], "-q") == 0)
174 else if (extract_one_file(argv
[i
]))
178 /* Exit code is true if any extract succeeds */
179 return extracted
== 0;