Skip to content

Commit

Permalink
stdin/stdout has to be binary mode, fixes #341
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsneyers committed Jan 9, 2017
1 parent ec475b3 commit bf8f5b5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/image/image-pam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>

#include "image.hpp"
#include "image-pam.hpp"
Expand Down Expand Up @@ -103,9 +104,8 @@ bool image_save_pam(const char *filename, const Image& image)
{
if (image.numPlanes() < 4) return image_save_pnm(filename, image);
FILE *fp = NULL;
if (!strcmp(filename,"-")) fp = stdout;
if (!strcmp(filename,"-")) fp = fdopen(dup(fileno(stdout)), "wb"); // make sure it is in binary mode (needed in Windows)
else fp = fopen(filename,"wb");

if (!fp) {
return false;
}
Expand Down Expand Up @@ -173,7 +173,7 @@ bool image_save_pam(const char *filename, const Image& image)
}
}

if (fp != stdout) fclose(fp);
// if (fp != stdout) fclose(fp);

This comment has been minimized.

Copy link
@bjorn3

bjorn3 Jun 27, 2017

Why did you comment this out?

This comment has been minimized.

Copy link
@jonsneyers

jonsneyers Jun 27, 2017

Author Member

I don't remember... is it causing trouble?

This comment has been minimized.

Copy link
@bjorn3

bjorn3 Jun 27, 2017

I didn't test it. I just saw it. It could however cause fd exhaustion if I'm correct.

if (image.get_metadata("iCCP")) {
v_printf(1,"Warning: input image has color profile, which cannot be stored in output image format.\n");
}
Expand Down
9 changes: 5 additions & 4 deletions src/image/image-pnm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>

#include "image.hpp"
#include "image-pnm.hpp"
Expand All @@ -28,7 +29,7 @@ unsigned int read_pnm_int(FILE *fp, char* buf, char** t) {

bool image_load_pnm(const char *filename, Image& image) {
FILE *fp = NULL;
if (!strcmp(filename,"-")) fp = stdin;
if (!strcmp(filename,"-")) fp = fdopen(dup(fileno(stdin)), "rb"); // make sure it is in binary mode (needed in Windows)
else fp = fopen(filename,"rb");

char buf[PPMREADBUFLEN], *t;
Expand Down Expand Up @@ -107,15 +108,15 @@ bool image_load_pnm(const char *filename, Image& image) {
}
}
}
if (fp != stdin) fclose(fp);
// if (fp != stdin) fclose(fp);
return true;
}
#endif

bool image_save_pnm(const char *filename, const Image& image)
{
FILE *fp = NULL;
if (!strcmp(filename,"-")) fp = stdout;
if (!strcmp(filename,"-")) fp = fdopen(dup(fileno(stdout)), "wb"); // make sure it is in binary mode (needed in Windows)
else fp = fopen(filename,"wb");
if (!fp) {
return false;
Expand Down Expand Up @@ -166,7 +167,7 @@ bool image_save_pnm(const char *filename, const Image& image)
fclose(fp);
return false;
}
if (fp != stdout) fclose(fp);
// if (fp != stdout) fclose(fp);
if (image.get_metadata("iCCP")) {
v_printf(1,"Warning: input image has color profile, which cannot be stored in output image format.\n");
}
Expand Down

0 comments on commit bf8f5b5

Please sign in to comment.