Re: Create Pix from HBITMAP

1,031 views
Skip to first unread message

TP

unread,
Jan 2, 2013, 4:37:11 AM1/2/13
to tesser...@googlegroups.com
On Wed, Jan 2, 2013 at 12:15 AM, newbie <duon...@gmail.com> wrote:
> Hello experts,
>
> I would like to create a Pix of Leptonica from my HBITMAP which was captured
> from the screen shot.Could you please advise me how to create it?

See pixGetWindowsHBITMAP() in leptonica/src/leptwin.c [1]

[1] http://tpgit.github.com/Leptonica/leptwin_8c_source.html#l00276

TP

unread,
Jan 2, 2013, 5:11:53 AM1/2/13
to tesser...@googlegroups.com
Oops. I just re-read your message and you want to go from HBITMAP to
Pix, not the other way around. I never got around to writing that
routine, but using the various static "helper" functions in leptwin.c
and pixCreate() [1], pixSetData() [2], and possibly pixSetColorMap()
[3] and pixEndianByteSwap() [4] this should be reasonably "simple" ---
assuming you are familiar with the internal structure of hbitmaps.
Reading dibsectn.c [5], which the guts of leptwin.c was based on, may
or may not help.

Otherwise your easiest course is to just write the HBITMAP to disk as
TIFF or PNG and then read it back in.

[1] http://tpgit.github.com/Leptonica/pix1_8c_source.html#l00269

[2] http://tpgit.github.com/Leptonica/pix1_8c_source.html#l01376

[3] http://tpgit.github.com/Leptonica/pix1_8c_source.html#l01312

[4] http://tpgit.github.com/Leptonica/pix2_8c_source.html#l02283

[5] http://shito.servebeer.com/pub/study/win32app/_tips/_DIBSectn.c

TP

unread,
Jan 8, 2013, 12:34:09 PM1/8/13
to tesser...@googlegroups.com
On Mon, Jan 7, 2013 at 8:47 PM, newbie <duon...@gmail.com> wrote:
> I have created the Pix using pixCreate,pixSetData, pixEndianByteSwap.
> However, when I call pixDestroy, it crashed. Do you know what I am doing
> wrong? Is there any restriction for pixCreate? Noted that Only the pix was
> created by pixCreate crashed.

Sorry, no. You should take a look at how leptonica creates pix from
tiff [1] or png [2], and do something similar.

Did you save the PIX out to disk (as tiff or png) just to make sure
you really were creating it correctly?

Also, in the past when trying to figure out such problems, it's VERY
helpful to step through the failing code with the Visual Studio
debugger. Leptonica heavily uses "reference counting" to figure out
when to free memory. If you do something wrong, you end up freeing
unallocated memory and crashing.

IIRC if you create a Windows GDI object [3] with say CreateBitmap(),
then you better destroy it via DeleteObject and NOT free() it. So...
for leptonica's purposes you might have to make a copy of the data
that leptonica can free, or somehow mess with leptonica's ref counter
so it doesn't try to free the data when the pix is destroyed.

[1] http://tpgit.github.com/Leptonica/tiffio_8c_source.html

[2] http://tpgit.github.com/Leptonica/pngio_8c_source.html

[3] http://msdn.microsoft.com/en-us/library/ms724291(v=VS.85).aspx

TP

unread,
Jan 9, 2013, 7:39:36 PM1/9/13
to tesser...@googlegroups.com
That might work but sounds dangerous. I just took a more careful look
at what pixCreate() does, and by default it allocates memory for you.
The implication being that you are supposed to copy your image data
into that location.

In any case, you should definitely check your routine for memory
leaks. Loop it say 100 times and watch your memory usage (and GDI
handle usage). Something like Sysinternals Process Explorer can be
helpful here. Or possibly look into Visual Leak Detector [2].

[1] http://technet.microsoft.com/en-us/sysinternals/bb896653

[2] http://vld.codeplex.com/documentation

TP

unread,
Jan 9, 2013, 7:57:24 PM1/9/13
to tesser...@googlegroups.com
On Tue, Jan 8, 2013 at 7:58 PM, newbie <duon...@gmail.com> wrote:
> BTW, I just have one more question regarding the pixCreate. Is it default
> set the bitmap resolution to 300 DPI even though I create the pix* from the
> bmp with 96 DPI?
> I saved the tiff file to disk and see it has 300 DPI. But if I manually set
> the xRes & yRes to 96, then it has 96 DPI of resolution. I don't if it is
> expectation for DPI.

Take a look at tiffio.c. You'll see lines like:

static const l_int32 DEFAULT_RESOLUTION = 300; /* ppi */

if (xres == 0) xres = DEFAULT_RESOLUTION;
if (yres == 0) yres = DEFAULT_RESOLUTION;

so the answer would seem to be yes.

> Sometime, I would like to resample the bitmap which has low resolution and
> set to 300 DPI for input bitmap of Tesseract OCR. Could you advise me?

For a discussion of leptonica's image scaling support see "Image
Scaling" [1], and in particular "Summary of scaling functions in
leptonica" [2]. See prog/scaletest1.c, prog/scaletest2.c, and
prog/scale_reg.c for example usage.

[1] http://tpgit.github.com/UnOfficialLeptDocs/leptonica/scaling.html

[2] http://tpgit.github.com/UnOfficialLeptDocs/leptonica/scaling.html#summary-of-scaling-functions-in-leptonica

John Smith

unread,
Apr 8, 2014, 5:32:55 PM4/8/14
to tesser...@googlegroups.com
Can you show us how you did it?
I have the same problem.

On Monday, January 7, 2013 11:47:49 PM UTC-5, newbie wrote:
Hi,

I have created the Pix using pixCreate,pixSetData, pixEndianByteSwap. However, when I call pixDestroy, it crashed. Do you know what I am doing wrong? Is there any restriction for pixCreate? Noted that Only the pix was created by pixCreate crashed. 

Please advise.
Thanks.

On Thursday, January 3, 2013 3:22:07 PM UTC+7, newbie wrote:
Thanks for your reply. I will try those APIs. 
Saving image into disk and reading back is not preferred since it will affect the performance. I would like to process it in memory rather than read from IO.
Thanks.

Sirus20x6

unread,
Dec 10, 2014, 1:43:50 AM12/10/14
to tesser...@googlegroups.com
did you ever post your code? I'm looking to do the same thing with leptonica
Reply all
Reply to author
Forward
0 new messages