Fix BufferedImage subimage handling in RapidOCRLite

Open Beginner friendly
#3 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
73/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java

Research direction

Find toPackedRgb() in the Java implementation and read how it currently handles BufferedImage.TYPE_3BYTE_BGR. Check the image conversion tests, if present, and compare a subimage’s output with the equivalent packed image. Done when subimages and nested subimages produce correct OCR input without the workaround copy.

Written by the indexing model from the issue text.

Description

Fix BufferedImage subimage handling in RapidOCRLite

Problem description

The current implementation of toPackedRgb() in RapidOCRLite directly accesses the underlying DataBufferByte for images of type BufferedImage.TYPE_3BYTE_BGR.

This implementation assumes that the pixel data starts at index zero and is stored contiguously. However, when using BufferedImage.getSubimage(), the resulting image shares the underlying data buffer with its parent image.

The current implementation does not account for raster offsets, raster translations or scanline strides, resulting in incorrect pixel data being passed to the OCR engine.

As a workaround, we currently create a new BufferedImage containing a copy of the subimage. This introduces unnecessary memory allocations and pixel copying.

Proposed solution

Replace the direct DataBufferByte access and manual BGR-to-RGB conversion in toPackedRgb() with the raster-aware getDataElements() method:

if (img.getType() == BufferedImage.TYPE_3BYTE_BGR) {
    return (byte[]) img.getRaster()
            .getDataElements(0, 0, w, h, null);
}

This approach automatically accounts for raster offsets, band offsets and scanline strides, while allocating only the required packed RGB output array.

Technical justification: RGB component ordering

Although TYPE_3BYTE_BGR physically stores pixel data in BGR order, Raster.getDataElements() retrieves the pixel components in logical RGB order.

This is supported by the following:

Consequently, the resulting byte array contains contiguous RGB pixels in row-major order, as required by RapidOCRLite. No additional channel swapping is necessary.

This behaviour depends on the associated color model and sample model. Therefore, the optimization should remain restricted to TYPE_3BYTE_BGR.

Expected benefits
  • Correct support for subimages and nested subimages.
  • Elimination of intermediate BufferedImage allocations.
  • Elimination of explicit BGR-to-RGB conversion in RapidOCRLite.
  • Reduced memory consumption and garbage collection pressure.
  • Reduced pixel copying overhead.
  • No changes required to the public API, JNI bindings or native OCR implementation.
Dominant language
C++
Stars
35
Forks
8
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.