summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLode <lvandeve@gmail.com>2015-12-09 00:20:41 +0100
committerLode <lvandeve@gmail.com>2015-12-09 00:20:41 +0100
commitc965e8a1f340675bc8a10638470f818e3f743724 (patch)
treed5834e8948339ee50eadd277230efd03dd1ceb0a
parent942c151c7b92dc727fc2d0a00211e9fa83ef6e83 (diff)
Made load_file function return error if file can't be opened
-rw-r--r--examples/example_decode.c14
-rw-r--r--examples/example_decode.cpp8
-rw-r--r--lodepng.cpp15
-rw-r--r--lodepng.h9
4 files changed, 25 insertions, 21 deletions
diff --git a/examples/example_decode.c b/examples/example_decode.c
index 1ecec3d..8f6ce39 100644
--- a/examples/example_decode.c
+++ b/examples/example_decode.c
@@ -59,11 +59,11 @@ void decodeTwoSteps(const char* filename)
unsigned error;
unsigned char* image;
unsigned width, height;
- unsigned char* png;
+ unsigned char* png = 0;
size_t pngsize;
- lodepng_load_file(&png, &pngsize, filename);
- error = lodepng_decode32(&image, &width, &height, png, pngsize);
+ error = lodepng_load_file(&png, &pngsize, filename);
+ if(!error) error = lodepng_decode32(&image, &width, &height, png, pngsize);
if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
free(png);
@@ -82,15 +82,15 @@ void decodeWithState(const char* filename)
unsigned error;
unsigned char* image;
unsigned width, height;
- unsigned char* png;
+ unsigned char* png = 0;
size_t pngsize;
LodePNGState state;
lodepng_state_init(&state);
/*optionally customize the state*/
- lodepng_load_file(&png, &pngsize, filename);
- error = lodepng_decode(&image, &width, &height, &state, png, pngsize);
+ error = lodepng_load_file(&png, &pngsize, filename);
+ if(!error) error = lodepng_decode(&image, &width, &height, &state, png, pngsize);
if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
free(png);
@@ -107,7 +107,7 @@ int main(int argc, char *argv[])
const char* filename = argc > 1 ? argv[1] : "test.png";
decodeOneStep(filename);
-
+
return 0;
}
diff --git a/examples/example_decode.cpp b/examples/example_decode.cpp
index 4eee6b8..76cda6e 100644
--- a/examples/example_decode.cpp
+++ b/examples/example_decode.cpp
@@ -58,8 +58,8 @@ void decodeTwoSteps(const char* filename)
unsigned width, height;
//load and decode
- lodepng::load_file(png, filename);
- unsigned error = lodepng::decode(image, width, height, png);
+ unsigned error = lodepng::load_file(png, filename);
+ if(!error) error = lodepng::decode(image, width, height, png);
//if there's an error, display it
if(error) std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
@@ -76,8 +76,8 @@ void decodeWithState(const char* filename)
unsigned width, height;
lodepng::State state; //optionally customize this one
- lodepng::load_file(png, filename); //load the image file with given filename
- unsigned error = lodepng::decode(image, width, height, state, png);
+ unsigned error = lodepng::load_file(png, filename); //load the image file with given filename
+ if(!error) error = lodepng::decode(image, width, height, state, png);
//if there's an error, display it
if(error) std::cout << "decoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
diff --git a/lodepng.cpp b/lodepng.cpp
index a5a5b78..b0d9845 100644
--- a/lodepng.cpp
+++ b/lodepng.cpp
@@ -1,5 +1,5 @@
/*
-LodePNG version 20151024
+LodePNG version 20151208
Copyright (c) 2005-2015 Lode Vandevenne
@@ -42,7 +42,7 @@ Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for
#pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/
#endif /*_MSC_VER */
-const char* LODEPNG_VERSION_STRING = "20151024";
+const char* LODEPNG_VERSION_STRING = "20151208";
/*
This source file is built up in the following large parts. The code sections
@@ -5873,8 +5873,7 @@ const char* lodepng_error_text(unsigned code)
case 43: return "bKGD chunk has wrong size for palette image";
case 44: return "bKGD chunk has wrong size for greyscale image";
case 45: return "bKGD chunk has wrong size for RGB image";
- /*the input data is empty, maybe a PNG file doesn't exist or is in the wrong path*/
- case 48: return "empty input or file doesn't exist";
+ case 48: return "empty input buffer given to decoder. Maybe caused by non-existing file?";
case 49: return "jumped past memory while generating dynamic huffman tree";
case 50: return "jumped past memory while generating dynamic huffman tree";
case 51: return "jumped past memory while inflating huffman block";
@@ -5940,9 +5939,10 @@ namespace lodepng
{
#ifdef LODEPNG_COMPILE_DISK
-void load_file(std::vector<unsigned char>& buffer, const std::string& filename)
+unsigned load_file(std::vector<unsigned char>& buffer, const std::string& filename)
{
std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
+ if(!file) return 78;
/*get filesize*/
std::streamsize size = 0;
@@ -5952,6 +5952,8 @@ void load_file(std::vector<unsigned char>& buffer, const std::string& filename)
/*read contents of the file into the vector*/
buffer.resize(size_t(size));
if(size > 0) file.read((char*)(&buffer[0]), size);
+
+ return 0; /* OK */
}
/*write given buffer to the file, overwriting the file, it doesn't append to it.*/
@@ -6087,7 +6089,8 @@ unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const
LodePNGColorType colortype, unsigned bitdepth)
{
std::vector<unsigned char> buffer;
- load_file(buffer, filename);
+ unsigned error = load_file(buffer, filename);
+ if(error) return error;
return decode(out, w, h, buffer, colortype, bitdepth);
}
#endif /* LODEPNG_COMPILE_DECODER */
diff --git a/lodepng.h b/lodepng.h
index 86c24f9..800cd0c 100644
--- a/lodepng.h
+++ b/lodepng.h
@@ -1,5 +1,5 @@
/*
-LodePNG version 20151024
+LodePNG version 20151208
Copyright (c) 2005-2015 Lode Vandevenne
@@ -848,10 +848,10 @@ unsigned encode(std::vector<unsigned char>& out,
#ifdef LODEPNG_COMPILE_DISK
/*
-Load a file from disk into an std::vector. If the vector is empty, then either
-the file doesn't exist or is an empty file.
+Load a file from disk into an std::vector.
+return value: error code (0 means ok)
*/
-void load_file(std::vector<unsigned char>& buffer, const std::string& filename);
+unsigned load_file(std::vector<unsigned char>& buffer, const std::string& filename);
/*
Save the binary data in an std::vector to a file on disk. The file is overwritten
@@ -1564,6 +1564,7 @@ yyyymmdd.
Some changes aren't backwards compatible. Those are indicated with a (!)
symbol.
+*) 08 dec 2015: Made load_file function return error if file can't be opened.
*) 24 okt 2015: Bugfix with decoding to palette output.
*) 18 apr 2015: Boundary PM instead of just package-merge for faster encoding.
*) 23 aug 2014: Reduced needless memory usage of decoder.