summaryrefslogtreecommitdiff
path: root/src/feh_png.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/feh_png.c')
-rw-r--r--src/feh_png.c40
1 files changed, 23 insertions, 17 deletions
diff --git a/src/feh_png.c b/src/feh_png.c
index e2842f8..8f5b94d 100644
--- a/src/feh_png.c
+++ b/src/feh_png.c
@@ -23,20 +23,18 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
-#include "feh_png.h"
-
#include <png.h>
#include <stdio.h>
#include <stdarg.h>
+#include "feh_png.h"
+
#define FEH_PNG_COMPRESSION 3
-#define FEH_PNG_NUM_COMMENTS 2 /* only Thumb::URI and Thumb::MTime for now */
+#define FEH_PNG_NUM_COMMENTS 4
gib_hash *feh_png_read_comments(char *file)
{
- gib_hash *hash = NULL;
-
FILE *fp;
int i, sig_bytes, comments = 0;
@@ -45,31 +43,31 @@ gib_hash *feh_png_read_comments(char *file)
png_textp text_ptr;
if (!(fp = fopen(file, "rb")))
- return hash;
+ return NULL;
if (!(sig_bytes = feh_png_file_is_png(fp))) {
fclose(fp);
- return hash;
+ return NULL;
}
/* initialize data structures */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fclose(fp);
- return hash;
+ return NULL;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
fclose(fp);
- return hash;
+ return NULL;
}
- if (setjmp(png_ptr->jmpbuf)) {
+ if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
- return hash;
+ return NULL;
}
/* initialize reading */
@@ -78,6 +76,8 @@ gib_hash *feh_png_read_comments(char *file)
png_read_info(png_ptr, info_ptr);
+ gib_hash *hash = NULL;
+
#ifdef PNG_TEXT_SUPPORTED
png_get_text(png_ptr, info_ptr, &text_ptr, &comments);
if (comments > 0) {
@@ -94,7 +94,7 @@ gib_hash *feh_png_read_comments(char *file)
}
/* grab image data from image and write info file with comments ... */
-int feh_png_write_png(Imlib_Image image, char *file, ...)
+int feh_png_write_png_fd(Imlib_Image image, int fd, ...)
{
FILE *fp;
int i, w, h;
@@ -111,20 +111,23 @@ int feh_png_write_png(Imlib_Image image, char *file, ...)
char *pair_key, *pair_text;
#endif /* PNG_TEXT_SUPPORTED */
- if (!(fp = fopen(file, "wb")))
+ if (!(fp = fdopen(fd, "wb")))
return 0;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!png_ptr)
+ if (!png_ptr) {
+ fclose(fp);
return 0;
+ }
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
+ fclose(fp);
return 0;
}
- if (setjmp(png_ptr->jmpbuf)) {
+ if (setjmp(png_jmpbuf(png_ptr))) {
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
png_destroy_info_struct(png_ptr, &info_ptr);
@@ -152,7 +155,7 @@ int feh_png_write_png(Imlib_Image image, char *file, ...)
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
#ifdef PNG_TEXT_SUPPORTED
- va_start(args, file);
+ va_start(args, fd);
for (i = 0; i < FEH_PNG_NUM_COMMENTS; i++) {
if ((pair_key = va_arg(args, char *))
&& (pair_text = va_arg(args, char *))) {
@@ -194,7 +197,10 @@ int feh_png_file_is_png(FILE * fp)
{
unsigned char buf[8];
- fread(buf, 1, 8, fp);
+ if (fread(buf, 1, 8, fp) != 8) {
+ return 0;
+ }
+
if (png_sig_cmp(buf, 0, 8)) {
return 0;
}