It is currently Tue May 23, 2017 9:54 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Converting and displaying a bitmap
PostPosted: Sat Jan 14, 2017 11:09 am 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 21, 2011 10:39 am
Posts: 326
I am working with a C++ library which user "jack" helped me to import into PB here: viewtopic.php?p=500419#p500419

A function there returns a bitmap and I need some help to use that bitmap. The function renders some graphics into a bitmap and I think it must be converetd to CGImage or NSImage before I can display it.
Code:
bitmap = FPDFBitmap_Create(800, 600, 0)   ;returns a non-zero handle
RenderPageBitmap(bitmap, pdf_pages(0), 0, 0, 800, 600, 0, $100)   ;no error

; StartDrawing(CanvasOutput(0))
;   DrawImage(bitmap)   ;IMA!
; StopDrawing()

StartVectorDrawing(CanvasVectorOutput(0))
  DrawVectorImage(bitmap)   :IMA!
StopDrawing()

In case it will help here is a C++ example of the functions being used (lines 598 to 607):
https://github.com/hfiguiere/pdfium/blo ... um_test.cc
Code:
  int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
  int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
  int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
  FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
  if (bitmap) {
    FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
    FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
    FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT);

    FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT);

Any help is appreciated. Thank you. :D


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting and displaying a bitmap
PostPosted: Wed Jan 18, 2017 3:54 pm 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 21, 2011 10:39 am
Posts: 326
I managed to get a picture! :D

But the color and size is a little off and I don't know where I'm going wrong:
Code:
; *bitmap, bitmapWidth, bitmapHeight returned from DLL

color = CGColorSpaceCreateDeviceRGB()
provider = CGDataProviderCreateWithData(#Null, *bitmap, bmpWidth * bmpHeight * 4, #Null)   ;why 4?
image = CGImageCreate(bmpWidth, bmpHeight, 8, 32, bmpWidth * 4, color,                     ;why 8, 32, *4?
                      #kCGBitmapByteOrderDefault|#kCGImageAlphaNone,                       ;both 0 values?
                      provider, #Null, #False, #kCGRenderingIntentDefault) 

size.NSSize\width = bmpWidth
size\height = bmpHeight
nsimage = CocoaMessage(0, CocoaMessage(0, 0, "NSImage alloc"), "initWithCGImage:", image, "size:@", @size)
StartVectorDrawing(CanvasVectorOutput(canvas))
  DrawVectorImage(nsimage)
  CocoaMessage(0, nsimage, "release")
StopVectorDrawing()

Can someone please tell me what I did wrong? :(


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting and displaying a bitmap
PostPosted: Wed Jan 18, 2017 3:59 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1673
if its just the color being off my first guess would be BGR<>RGB byte order, so maybe try the other version of #kCGBitmapByteOrderDefault
Also you're specifying "#kCGImageAlphaNone" yet with depth 32 that does have alpha channel, so im guessing you dont want that flag.
";why 4?" - 4 bytes for RGBA (so, if ImageDepth(img) = 32 ... use 3 for depth 24)
";why 8, 32, *4?" - 8 bits per R/G/B component, 32 bits per total pixel (32 for RGBA, 24 for RGB), and *4 as there's 4 components (R G B A)

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting and displaying a bitmap
PostPosted: Fri Jan 20, 2017 1:49 am 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 21, 2011 10:39 am
Posts: 326
Keya wrote:
if its just the color being off my first guess would be BGR<>RGB byte order, so maybe try the other version of #kCGBitmapByteOrderDefault
Also you're specifying "#kCGImageAlphaNone" yet with depth 32 that does have alpha channel, so im guessing you dont want that flag.
";why 4?" - 4 bytes for RGBA (so, if ImageDepth(img) = 32 ... use 3 for depth 24)
";why 8, 32, *4?" - 8 bits per R/G/B component, 32 bits per total pixel (32 for RGBA, 24 for RGB), and *4 as there's 4 components (R G B A)
Thanks for the explanation Keya. But after trying many combinations the image either does not display or is displayed with wrong colors or wrong sizes (always in height). :evil:


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye