[Gambas-devel] [Gambas-devel-svn] Image help.

Benoit Minisini gambas at ...1...
Fri Nov 21 13:32:51 CET 2008


On mardi 18 novembre 2008, Wellington de Souza Pinto wrote:
> Hi!
>
> My english is not very well, but i'm need help.
>
>
> I have this functions and yours returns
>
>         unsigned char *wimg;
>         wimg = fp_img_get_data(img);
>
>         // This function Return my image in map of bits
>         // For understand! if i write an file in this mode work fine (This
> is write for .pgm format)
>
>         ...
>         width = fp_img_get_width(img);
>         height = fp_img_get_height(img);
>
>         r = fprintf(fd, "P5 %d %d 255\n", width, height);
>         if (r < 0) {
>                 printf("pgm header write failed, error %d", r);
>                 return r;
>         }
>
>         size_t write_size = width * height;
>
>         r = fwrite(wimg, 1, write_size, fd);
>         if (r < write_size) {
>                 printf("short write (%d)", r);
>                 return -1;
>         }
>
>         fclose(fd);
>         ...
>
>        How to i convert the type (wimg) for use with GB.ReturnObject()
> function? I'm need return an image.
>
>        My C hability is not very well yet.
>
> Reguars,
>
> any ideia (wspinto at ...613...)
>

First: do not send e-mails to the developer svn mailing-list, this 
mailing-list is just for getting svn commit reports.

Second: you should enhance your C habilities, otherwise you may not understand 
the following.

Let's start the explanation anyway...

You can't return a C pointer directly, Gambas can't use it. GB.ReturnObject() 
returns a Gambas object.

If you want to use a C (or a C++) pointer that points at something useful 
through a Gambas object, you must reference the pointer inside the Gambas 
object structure.

For example, the Control Gambas object has a pointer to the internal Qt widget 
C++ object.

Then, you must take care about the life of your object: 

- When the Gambas object is destroyed, then the underlying C or C++ object 
must be freed.

- When the underlying C/C++ object is freed by the library (not by the Gambas 
interpreter), your code must catch it, and makes the object invalid. You do 
that by: storing NULL in the C/C++ pointer located in the Gambas object 
structure, and use the GB_HOOK_CHECK() macro to implement a check function 
that returns TRUE when the Gambas object is invalid, i.e. then the previous 
pointer is NULL.

I suggest you read the source code of a simple component, like gb.eval, to see 
how the basic things work. An example of GB_HOOK_CHECK() is located in the 
CWidget.cpp file on the gb.qt source code.

As for the GB.Image() lack of documentation: the documentation is old, it is 
for Gambas 1. If I have time, I will try to complete the documentation. But 
if you had explain what you did exactly, I would have talked about these API 
functions before you find them by yourself.

Regards,

-- 
Benoit Minisini




More information about the Devel mailing list