Fix BufferedImage subimage handling in RapidOCRLite
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
- Domain
- computer-vision
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:
- The [ComponentColorModel documentation](https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/java/awt/image/ComponentColorModel.html) specifies that RGB components in primitive array representations are ordered as red, green and blue.
- The [OpenJDK BufferedImage implementation](https://github.com/openjdk/jdk21u/blob/master/src/java.desktop/share/classes/java/awt/image/BufferedImage.java) constructs
TYPE_3BYTE_BGRwith band offsets{2, 1, 0}, mapping the physical BGR storage to the logical RGB components. - The [OpenJDK ByteInterleavedRaster implementation](https://github.com/openjdk/jdk21u/blob/master/src/java.desktop/share/classes/sun/awt/image/ByteInterleavedRaster.java) uses these band offsets, together with the pixel stride and scanline stride, when extracting pixel data through
getDataElements().
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
BufferedImageallocations. - 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
jaff-chemistry/jaff#231 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
ColumnDecimal::Append(Int128) stores a value outside the Decimal(P, S) range with no error: cut to 32/64 bits, or with too many digitsPossibly taken @RUSHIT305 claimed this today. Openbug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cpp#645 · 1 comment ·
Maintainers usually reply within 1 day
-
bug compatibility difficulty:easy prio:normal
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
NatronGitHub/openfx-misc#524 ·
Maintainers usually reply within 1 day
-
documentation enhancement help wanted P0
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Awesome-Embedded-Learning-Studio/Tutorial_AwesomeModernCPP#359 ·
Maintainers usually reply within 1 day