Skip to content

Memory access violation when using SetRectangle for 8Bit images with colormap #4127

@AdmiralPellaeon

Description

@AdmiralPellaeon

Current Behavior

Hi

I got a memory access violation when using SetRectangle and an 8Bit image.
I only tested the simple example from the tesseract documentation (SetRectangle_example from https://tesseract-ocr.github.io/tessdoc/Examples_C++.html).
My image is a PNG file. When saving as 8Bit image I got the crash in debug mode. When I save the image in 32bit, then it works properly.

The VS debugger recognized the violation in the method HistogramRect in the file otsuthr.cpp in line 157: int pixel = GET_DATA_BYTE(linedata, (x + left) * num_channels + channel);

I tried to look if there is an error in the formula for calculating the address. But I am not familiar with the Tesseract internals (installed it last week for the first time), so I have no idea what the error causes.

But I think the mentioned method is a little weird. The input parameter src_pix is a pointer to the small rectangle image part, not to the overall (big) image (width and height have the size of the rectangle). Based on this it uses the WPL of the small rectangle image (src_wpl) and not of the whole original picture with much bigger resolution.
But when calculating the address of the pixels, the algorithm uses the pixel coordinates of the rectangle within the whole big picture (left, top, width, height), these are "global" pixel coodinates. So it seems, that the image is only a shallow structure pointing to the memory of the actual loaded image.
But if this is true: why does the line const l_uint32 *linedata = srcdata + y * src_wpl; use the WPL of the small rectangle and not of the actual whole image? If it is the memory of the loaded image, then it should be the stride based in the width of the image and not the width of the small rectangle.
On the other side: it seems to work properly with 32 bit images. So if the address calculation is wrong, then it should also cause problems with all other channel counts.

These are my thoughts. Hope it helps a little bit.

Best regards

Expected Behavior

No response

Suggested Fix

No response

tesseract -v

Tesseract 5.3.2

Operating System

Windows 10

Other Operating System

No response

uname -a

No response

Compiler

MSVC 2022 17.7.4, x64

CPU

No response

Virtualization / Containers

No response

Other Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions