Skip to content

Commit 41f50b1

Browse files
committed
fix crash in case of missing PNG support in Leptonica see #2333
1 parent 90aef80 commit 41f50b1

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/ccstruct/imagedata.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ bool ImageData::SkipDeSerialize(TFile* fp) {
208208
}
209209

210210
// Saves the given Pix as a PNG-encoded string and destroys it.
211+
// In case of missing PNG support in Leptonica use PNM format,
212+
// which requires more memory.
211213
void ImageData::SetPix(Pix* pix) {
212214
SetPixInternal(pix, &image_data_);
213215
}
@@ -323,10 +325,16 @@ void ImageData::AddBoxes(const GenericVector<TBOX>& boxes,
323325
}
324326

325327
// Saves the given Pix as a PNG-encoded string and destroys it.
328+
// In case of missing PNG support in Leptonica use PNM format,
329+
// which requires more memory.
326330
void ImageData::SetPixInternal(Pix* pix, GenericVector<char>* image_data) {
327331
l_uint8* data;
328332
size_t size;
329-
pixWriteMem(&data, &size, pix, IFF_PNG);
333+
l_int32 ret;
334+
ret = pixWriteMem(&data, &size, pix, IFF_PNG);
335+
if (ret) {
336+
ret = pixWriteMem(&data, &size, pix, IFF_PNM);
337+
}
330338
pixDestroy(&pix);
331339
image_data->resize_no_init(size);
332340
memcpy(&(*image_data)[0], data, size);

src/ccstruct/imagedata.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ class ImageData {
157157
return box_texts_[index];
158158
}
159159
// Saves the given Pix as a PNG-encoded string and destroys it.
160+
// In case of missing PNG support in Leptonica use PNM format,
161+
// which requires more memory.
160162
void SetPix(Pix* pix);
161163
// Returns the Pix image for *this. Must be pixDestroyed after use.
162164
Pix* GetPix() const;
@@ -183,6 +185,8 @@ class ImageData {
183185

184186
private:
185187
// Saves the given Pix as a PNG-encoded string and destroys it.
188+
// In case of missing PNG support in Leptonica use PNM format,
189+
// which requires more memory.
186190
static void SetPixInternal(Pix* pix, GenericVector<char>* image_data);
187191
// Returns the Pix image for the image_data. Must be pixDestroyed after use.
188192
static Pix* GetPixInternal(const GenericVector<char>& image_data);
@@ -192,8 +196,8 @@ class ImageData {
192196

193197
private:
194198
STRING imagefilename_; // File to read image from.
195-
int32_t page_number_; // Page number if multi-page tif or -1.
196-
GenericVector<char> image_data_; // PNG file data.
199+
int32_t page_number_; // Page number if multi-page tif or -1.
200+
GenericVector<char> image_data_; // PNG/PNM file data.
197201
STRING language_; // Language code for image.
198202
STRING transcription_; // UTF-8 ground truth of image.
199203
GenericVector<TBOX> boxes_; // If non-empty boxes of the image.

0 commit comments

Comments
 (0)