#1 2016-02-24 23:06:53

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

Bitmaps are randomly not painted on the VCL canvas

Hello,

In a PDF document, I paint about 100 bitmap images. These images are spread over several pages, and I use the Draw() function of the VCL canvas provided by the document to draw these images. However, I noticed that randomly, some images are not painted (the location where the image should appear is simply white). The concerned images contain nothing special regarding to other images that are drawn correctly on the document. Sometimes, the images appear entirely black, or the image object can be selected on the document when opening in Adobe Reader, but the content is entirely white, sometimes nothing appear but a blank area. If I try to recreate the document, the previously missing images appear again, but others are missing, randomly.

Here is the code I use to draw the images:
pDoc->VCLCanvas->Draw(pos.X, pos.Y, pSource);

Where pSource is a 24 bit TBitmap containing the source image. I verified the content of pSource by saving all content on the disk, using pSource->SaveToFile() function. All of the saved files contained the correct image to paint.

Someone already encountered this problem? Is there a problem with the image conversion in the PDF metafile?

Regards

Last edited by jeanmilost (2016-02-24 23:10:08)

Offline

#2 2016-02-25 13:58:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,232
Website

Re: Bitmaps are randomly not painted on the VCL canvas

Which version of SynPdf are you using?
Latest is 1.18.2429.

Try to disable bitmap hashing - by setting TPdfDocument.ForceNoBitmapReuse property to TRUE ?

From which source are you creating the bitmaps? jpeg? png?
Try to create a new void bitmap from the source before drawing it to the Canvas.

Also note that the library does not support bitmap transparency yet.

Offline

#3 2016-02-26 15:04:02

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

Re: Bitmaps are randomly not painted on the VCL canvas

Hi ab,

Thanks for your reply.

Which version of SynPdf are you using?
=> I downloaded and tried the last version, same issue, unfortunately

Try to disable bitmap hashing - by setting TPdfDocument.ForceNoBitmapReuse property to TRUE ?
=> Yes I tried, unfortunately this changes nothing

From which source are you creating the bitmaps? jpeg? png?
=> All of these, but in the PDF containing the issue, all sources were created from JPEG. However I could reproduce the exactly same issue with PNG files

Also note that the library does not support bitmap transparency yet.
=> I know smile for this reason I "flattened" the image before draw it, using the following code

    // get source bitmap width and height
    const int width  = pSource->Width;
    const int height = pSource->Height;

    // create and configure overlay
    std::auto_ptr<TBitmap> pOverlay(new TBitmap());
    pOverlay->PixelFormat = pSource->PixelFormat;
    pOverlay->SetSize(width, height);

    // fill overlay background
    pOverlay->Canvas->Brush->Color = bgColor.GetColor();
    pOverlay->Canvas->Brush->Style = bsSolid;
    pOverlay->Canvas->FillRect(TRect(0, 0, width, height));

    // configure alpha blending function
    ::BLENDFUNCTION bf;
    bf.BlendOp             = AC_SRC_OVER;
    bf.BlendFlags          = 0;
    bf.SourceConstantAlpha = opacity;
    bf.AlphaFormat         = AC_SRC_ALPHA;

    // alpha blend source bitmap above overlay
    ::AlphaBlend(pOverlay->Canvas->Handle, 0, 0, width, height, pSource->Canvas->Handle, 0, 0, width,
            height, bf);

    // copy back overlay to destination canvas
    pDestination->Draw(pos.X, pos.Y, pOverlay.get());

Where pSource is a TBitmap and pDestination the VCL canvas provided from the PDF document. Of course, the fill overlay background part should be replaced by a BitBlt() from a source background image in case the background is an image and not just a solid color.

An important info is that my PDF document is created inside a thread. However, the PDF document object is entirely created and executed on the thread side, and it has no direct interaction with another thread. But is it possible that some global static variable, existing inside the PdfSyn library, may enter in conflict with my threaded job, and thus cause a such issue?

NOTE I can provide some sample PDF if needed

Regards

Last edited by jeanmilost (2016-02-26 15:37:19)

Offline

#4 2016-02-26 19:26:18

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,232
Website

Re: Bitmaps are randomly not painted on the VCL canvas

AFAIR there is no global variable in SynPDF, which should be thread-safe, as soon as each thread has its own TPdfDocument instance.

Offline

#5 2016-03-01 13:00:10

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

Re: Bitmaps are randomly not painted on the VCL canvas

Hi ab,

Thanks for your reply

In fact, it seems that the issue comes because the TBitmap canvas needs to be locked before be painted when used inside a threaded context. Here is a post about that: http://qc.embarcadero.com/wc/qcmain.aspx?d=43018

By locking my bitmap canvas the bug no more appeared

Regards

Offline

#6 2016-03-01 13:06:48

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,232
Website

Re: Bitmaps are randomly not painted on the VCL canvas

Yes, it is a well-known fact that the VCL is not thread-safe, by design.

Offline

Board footer

Powered by FluxBB