Skip to content

Commit

Permalink
Do not crash on broken images. Related: EasyRPG#858,EasyRPG#432
Browse files Browse the repository at this point in the history
This also degrades most image reader errors to warnings, because Player
does not need to close if one image is unreadable.
  • Loading branch information
carstene1ns committed May 26, 2016
1 parent 1754b11 commit db1c558
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
10 changes: 6 additions & 4 deletions src/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,13 @@ Bitmap::Bitmap(const std::string& filename, bool transparent, uint32_t flags) {
ImagePNG::ReadPNG(stream, (void*)NULL, transparent, w, h, pixels);
#endif
else
Output::Error("Unsupported image file %s", filename.c_str());
Output::Warning("Unsupported image file %s", filename.c_str());

fclose(stream);

Init(w, h, (void *) NULL);
ConvertImage(w, h, pixels, transparent);
if (pixels)
ConvertImage(w, h, pixels, transparent);

CheckPixels(flags);
}
Expand All @@ -574,10 +575,11 @@ Bitmap::Bitmap(const uint8_t* data, unsigned bytes, bool transparent, uint32_t f
ImagePNG::ReadPNG((FILE*) NULL, (const void*) data, transparent, w, h, pixels);
#endif
else
Output::Error("Unsupported image");
Output::Warning("Unsupported image");

Init(w, h, (void *) NULL);
ConvertImage(w, h, pixels, transparent);
if (pixels)
ConvertImage(w, h, pixels, transparent);

CheckPixels(flags);
}
Expand Down
10 changes: 5 additions & 5 deletions src/image_bmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void ImageBMP::ReadBMP(const uint8_t* data, unsigned len, bool transparent,
static const unsigned BITMAPFILEHEADER_SIZE = 14;

if (len < 64) {
Output::Error("Not a valid BMP file.");
Output::Warning("Not a valid BMP file.");
return;
}

Expand Down Expand Up @@ -85,19 +85,19 @@ void ImageBMP::ReadBMP(const uint8_t* data, unsigned len, bool transparent,

const int planes = (int) get_2(&data[BITMAPFILEHEADER_SIZE + 12]);
if (planes != 1) {
Output::Error("BMP planes is not 1.");
Output::Warning("BMP planes is not 1.");
return;
}

const int depth = (int) get_2(&data[BITMAPFILEHEADER_SIZE + 14]);
if (depth != 8 && depth != 4) {
Output::Error("BMP image depth unsupported: %i bit.", depth);
Output::Warning("BMP image depth unsupported: %i bit.", depth);
return;
}

const int compression = get_4(&data[BITMAPFILEHEADER_SIZE + 16]);
if (compression) {
Output::Error("BMP image is compressed.");
Output::Warning("BMP image is compressed.");
return;
}

Expand Down Expand Up @@ -167,7 +167,7 @@ void ImageBMP::ReadBMP(FILE* stream, bool transparent,
std::vector<uint8_t> buffer(size);
long size_read = fread((void*) &buffer.front(), 1, size, stream);
if (size_read != size) {
Output::Error("Error reading BMP file.");
Output::Warning("Error reading BMP file.");
return;
}
ReadBMP(&buffer.front(), (unsigned) size, transparent, width, height, pixels);
Expand Down
7 changes: 4 additions & 3 deletions src/image_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ void ImagePNG::ReadPNG(FILE* stream, const void* buffer, bool transparent,

png_struct *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, on_png_error, on_png_warning);
if (png_ptr == NULL) {
Output::Error("Couldn't allocate PNG structure");
Output::Warning("Couldn't allocate PNG structure");
return;
}

png_info *info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
Output::Error("Couldn't allocate PNG info structure");
Output::Warning("Couldn't allocate PNG info structure");
return;
}

Expand Down Expand Up @@ -117,7 +117,8 @@ static void ReadPalettedData(
png_read_update_info(png_ptr, info_ptr);

if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)) {
Output::Error("Palette PNG without PLTE block");
Output::Warning("Palette PNG without PLTE block");
return;
}

png_colorp palette;
Expand Down
6 changes: 3 additions & 3 deletions src/image_xyz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void ImageXYZ::ReadXYZ(const uint8_t* data, unsigned len, bool transparent,
pixels = NULL;

if (len < 8) {
Output::Error("Not a valid XYZ file.");
Output::Warning("Not a valid XYZ file.");
return;
}

Expand All @@ -45,7 +45,7 @@ void ImageXYZ::ReadXYZ(const uint8_t* data, unsigned len, bool transparent,

int status = uncompress(&dst_buffer.front(), &dst_size, src_buffer, src_size);
if (status != Z_OK) {
Output::Error("Error decompressing XYZ file.");
Output::Warning("Error decompressing XYZ file.");
return;
}
const uint8_t (*palette)[3] = (const uint8_t(*)[3]) &dst_buffer.front();
Expand Down Expand Up @@ -76,7 +76,7 @@ void ImageXYZ::ReadXYZ(FILE* stream, bool transparent,
std::vector<uint8_t> buffer(size);
long size_read = fread((void*) &buffer.front(), 1, size, stream);
if (size_read != size) {
Output::Error("Error reading XYZ file.");
Output::Warning("Error reading XYZ file.");
return;
}
ReadXYZ(&buffer.front(), (unsigned) size, transparent, width, height, pixels);
Expand Down

0 comments on commit db1c558

Please sign in to comment.