From ef33a06e65d3ec33108141c97f25e66afe909a6e Mon Sep 17 00:00:00 2001 From: zdenop Date: Wed, 1 May 2019 19:51:54 +0200 Subject: [PATCH] fix crash in case of missing PNG support in Leptonica see #2333 --- src/ccstruct/imagedata.cpp | 10 +++++++++- src/ccstruct/imagedata.h | 8 ++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/ccstruct/imagedata.cpp b/src/ccstruct/imagedata.cpp index 3b2b74a57..1282cd0af 100644 --- a/src/ccstruct/imagedata.cpp +++ b/src/ccstruct/imagedata.cpp @@ -208,6 +208,8 @@ bool ImageData::SkipDeSerialize(TFile* fp) { } // Saves the given Pix as a PNG-encoded string and destroys it. +// In case of missing PNG support in Leptonica use PNM format, +// which requires more memory. void ImageData::SetPix(Pix* pix) { SetPixInternal(pix, &image_data_); } @@ -323,10 +325,16 @@ void ImageData::AddBoxes(const GenericVector& boxes, } // Saves the given Pix as a PNG-encoded string and destroys it. +// In case of missing PNG support in Leptonica use PNM format, +// which requires more memory. void ImageData::SetPixInternal(Pix* pix, GenericVector* image_data) { l_uint8* data; size_t size; - pixWriteMem(&data, &size, pix, IFF_PNG); + l_int32 ret; + ret = pixWriteMem(&data, &size, pix, IFF_PNG); + if (ret) { + ret = pixWriteMem(&data, &size, pix, IFF_PNM); + } pixDestroy(&pix); image_data->resize_no_init(size); memcpy(&(*image_data)[0], data, size); diff --git a/src/ccstruct/imagedata.h b/src/ccstruct/imagedata.h index 6679db4cb..c559a9a47 100644 --- a/src/ccstruct/imagedata.h +++ b/src/ccstruct/imagedata.h @@ -157,6 +157,8 @@ class ImageData { return box_texts_[index]; } // Saves the given Pix as a PNG-encoded string and destroys it. + // In case of missing PNG support in Leptonica use PNM format, + // which requires more memory. void SetPix(Pix* pix); // Returns the Pix image for *this. Must be pixDestroyed after use. Pix* GetPix() const; @@ -183,6 +185,8 @@ class ImageData { private: // Saves the given Pix as a PNG-encoded string and destroys it. + // In case of missing PNG support in Leptonica use PNM format, + // which requires more memory. static void SetPixInternal(Pix* pix, GenericVector* image_data); // Returns the Pix image for the image_data. Must be pixDestroyed after use. static Pix* GetPixInternal(const GenericVector& image_data); @@ -192,8 +196,8 @@ class ImageData { private: STRING imagefilename_; // File to read image from. - int32_t page_number_; // Page number if multi-page tif or -1. - GenericVector image_data_; // PNG file data. + int32_t page_number_; // Page number if multi-page tif or -1. + GenericVector image_data_; // PNG/PNM file data. STRING language_; // Language code for image. STRING transcription_; // UTF-8 ground truth of image. GenericVector boxes_; // If non-empty boxes of the image.