Converting and displaying a bitmap

Mac OSX specific forum
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Converting and displaying a bitmap

Post by coder14 »

I am working with a C++ library which user "jack" helped me to import into PB here: http://www.purebasic.fr/english/viewtop ... 19#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: Select all

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 ... st.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);[/code]
Any help is appreciated. Thank you. :D
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Converting and displaying a bitmap

Post by coder14 »

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: Select all

; *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? :(
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Converting and displaying a bitmap

Post by Keya »

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)
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Converting and displaying a bitmap

Post by coder14 »

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:
Post Reply