[cairo] How to load a BMP/JPG/TGA to a Surface?

Mj Mendoza IV mjmendoza at konsolscript.org
Mon May 31 21:35:43 PDT 2010


Hello,

PNG is directly supported via cairo_image_surface_create_from_png. Is there any way that I could load BMP/JPG/TGA without being tied to a specific library to avoid bloat? Is there a minimalist reusable 'standalone' function that directly loads a BMP/JPG/TGA into the memory which cairo could use?

BTW, I am using Cairo with a FreeBASIC (http://www.freebasic.net -- a free and open dialect of BASIC; think of FreePascal). Using C-libraries is as simple as translating only the header files then FreeBASIC can easily use the pre-compiled lib files (.a, .lib)
For sample codes and test renders of Cairo on FreeBASIC, see: http://konsolscript.org/~mjmendoza/freebasic/cairo/

FreeBASIC already have 'standalone' reusable functions that loads JPG/TGA (BMP is internally supported via BLoad). However, when I pass the loaded image to cairo, it has an offset of 8-pixels. See this image to see what I mean.
http://konsolscript.org/~mjmendoza/freebasic/cairo/bmp-problem.png


The original file is on the left-side while the right-side shows how cairo renders the loaded image. Below is the code that loaded and passed the BMP file to Cairo.

Dim imgCairo As Cairo_Surface_T
Dim imgBMP As Integer Ptr

'1. read the width and height of the BMP file from the BMP file header
Dim As Integer f = FreeFile
Dim As Integer  wid, hgt
Open "file.bmp" For Binary Access Read As #f
  Get #f, 19, wid
  Get #f, 23, hgt
Close #f

'2. allocate a memory of that size
imgBMP = ImageCreate(wid, hgt, RGBA(0, 0, 0, 0), 32)

'3. load the BMP file to the allocated memory
BLoad "file.bmp", imgBMP

'4. pass the memory to a Cairo Surface
imgCairo = Cairo_Image_Surface_Create_For_Data(imgBMP, CAIRO_FORMAT_ARGB32, wid, hgt, wid * Len(Integer))

I know that I should also inform the FreeBASIC developers and/or community, I am just hoping that some of cairo users could have already written a non-official patch to directly load BMP/JPG/TGA on a Cairo Surface (ex. cairo_image_surface_create_from_bmp, cairo_image_surface_create_from_jpg, or cairo_image_surface_create_from_tga)


Regards.

Mj Mendoza IV,
Developer, KonsolScript
http://www.konsolscript.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20100601/8687a5fc/attachment.html>


More information about the cairo mailing list