Skip to content

Commit

Permalink
fix crash in case of missing PNG support in Leptonica see #2333
Browse files Browse the repository at this point in the history
  • Loading branch information
zdenop authored and stweil committed May 1, 2019
1 parent b2fc3eb commit ef33a06
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/ccstruct/imagedata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}
Expand Down Expand Up @@ -323,10 +325,16 @@ void ImageData::AddBoxes(const GenericVector<TBOX>& 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<char>* 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);
Expand Down
8 changes: 6 additions & 2 deletions src/ccstruct/imagedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<char>* image_data);
// Returns the Pix image for the image_data. Must be pixDestroyed after use.
static Pix* GetPixInternal(const GenericVector<char>& image_data);
Expand All @@ -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<char> image_data_; // PNG file data.
int32_t page_number_; // Page number if multi-page tif or -1.
GenericVector<char> image_data_; // PNG/PNM file data.
STRING language_; // Language code for image.
STRING transcription_; // UTF-8 ground truth of image.
GenericVector<TBOX> boxes_; // If non-empty boxes of the image.
Expand Down

0 comments on commit ef33a06

Please sign in to comment.