List Info

Thread: "ocaml_beginners"::[] camlimages: save Graphics buffer as PNG




"ocaml_beginners"::[] camlimages: save Graphics buffer as PNG
country flaguser name
Germany
2007-09-09 07:41:32

Hello

I want to save the current Graphics buffer as a PNG-Image.

With this code:

let img = Graphic_image.get_image 0 0 width height in
Images.save "image.png" None [] img;

I get the following error:

This expression has type Rgb24.t but is here used with type Images.t

But if i look at images.mli:

type t =
| Index8 of Index8.t
| Rgb24 of Rgb24.t
| Index16 of Index16.t
| Rgba32 of Rgba32.t
| Cmyk32 of Cmyk32.t;;

Doesn't this mean that Rgb24.t is a subtype of Images.t and thus should be compatible?

Can someone show me how to do this right?

Regards
Andre

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] camlimages: save Graphics buffer as PNG
country flaguser name
United States
2007-09-09 09:50:15

On Sep 9, 2007, at 7:41 AM, Andre Kuehne wrote:

> But if i look at images.mli:
>
> type t =
> | Index8 of Index8.t
> | Rgb24 of Rgb24.t
> | Index16 of Index16.t
> | Rgba32 of Rgba32.t
> | Cmyk32 of Cmyk32.t;;
>
> Doesn't this mean that Rgb24.t is a subtype of Images.t and thus
> should be compatible?

Well, it's not a subtype, but rather an inhabitant of the type (much
like 1 is not a subtype of the integers). What this means is that
you need to construct a value of type Images.t from something of type
Rgb24.t by tagging it with the Rgb24 tag. So if img is of type
Rgb24.t, then (Rgb24 img) [parens used for clarity, they are not
necessarily required in your code] has type Images.t.

William D. Neumann

"I eat T-bone steaks, I lift barbell plates, I'm sweeter than a
German chocolate cake. I'm the reflection of perfection, the number
one selection. I'm the man of the hour, the man with the power, too
sweet to be sour. The ladies' pet, the men's regret, where what you
see is what you get, and what you don't see, is better yet."

--Superstar Billy Graham

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] camlimages: save Graphics buffer as PNG
country flaguser name
Germany
2007-09-09 10:19:51

William Neumann wrote:
> On Sep 9, 2007, at 7:41 AM, Andre Kuehne wrote:
>
>>; But if i look at images.mli:
>>;
>>; type t =
>> | Index8 of Index8.t
>> | Rgb24 of Rgb24.t
>> | Index16 of Index16.t
>> | Rgba32 of Rgba32.t
>> | Cmyk32 of Cmyk32.t;;
>>
>> Doesn't this mean that Rgb24.t is a subtype of Images.t and thus
>>; should be compatible?
>
> Well, it's not a subtype, but rather an inhabitant of the type (much
> like 1 is not a subtype of the integers). What this means is that
> you need to construct a value of type Images.t from something of type
> Rgb24.t by tagging it with the Rgb24 tag. So if img is of type
> Rgb24.t, then (Rgb24 img) [parens used for clarity, they are not
> necessarily required in your code] has type Images.t.
>
Thanks, William! This works, but i have to read some more about
the Ocaml type system to really understand why this is necessary.

> William D. Neumann
>
> "I eat T-bone steaks, I lift barbell plates, I'm sweeter than a
> German chocolate cake. I'm the reflection of perfection, the number
> one selection. I'm the man of the hour, the man with the power, too
> sweet to be sour. The ladies' pet, the men's regret, where what you
> see is what you get, and what you don't see, is better yet."
>
> --Superstar Billy Graham
>
>
>
>

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] camlimages: save Graphics buffer as PNG
country flaguser name
United Kingdom
2007-09-09 08:17:00


Le 9 sept. 07 à 14:41, Andre Kuehne a écrit :

>; let img = Graphic_image.get_image 0 0 width height in
> Images.save "image.png" None [] img;
>;
(...)
> Can someone show me how to do this right?

Do :

let img = Graphic_image.get_image 0 0 width height in
Images.save "image.png" None [] (Rgb24 img);

(You can see the fact of putting 'Rgb24' before 'img' as explicitly
casting Rgb24.t into Images.t.)

> type t =
> | Index8 of Index8.t
> | Rgb24 of Rgb24.t
> | Index16 of Index16.t
> | Rgba32 of Rgba32.t
> | Cmyk32 of Cmyk32.t;;
>
> Doesn't this mean that Rgb24.t is a subtype of Images.t and thus
> should be compatible?

- Well it's a bit more than a subtype, even though in your case, you
can see it as a subtype.

But imagine you had :

type t =
|A of int
|B of int

Here, int can also be seen as a subtype of t. But it is "twice" : one
time it is called A the other it is called B.
There is more information than simply subtyping.

- Notice type t could have been defined the following way :

type t =
| A of Index8.t
| B of Rgb24.t
| C of Index16.t
| D of Rgba32.t
| E of Cmyk32.t;;

and then you would have done

Images.save "image.png" None [] (B img);

I mean it's just a coincidence if the names were the same as the
types' ones.
Maybe this was the confusing point ?

Hope this is clarifying things.

--
Vincent Aravantinos
PhD Student - LIG - CAPP Team

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] camlimages: save Graphics buffer as PNG
country flaguser name
Germany
2007-09-10 13:02:39

Vincent Aravantinos wrote:
> Le 9 sept. 07 à 14:41, Andre Kuehne a écrit :
>
>>; let img = Graphic_image.get_image 0 0 width height in
>> Images.save "image.png" None [] img;
>;>
>; (...)
>> Can someone show me how to do this right?
>
> Do :
>
> let img = Graphic_image.get_image 0 0 width height in
> Images.save "image.png" None [] (Rgb24 img);
>
> (You can see the fact of putting 'Rgb24' before 'img' as explicitly
> casting Rgb24.t into Images.t.)
>
>>; type t =
>> | Index8 of Index8.t
>> | Rgb24 of Rgb24.t
>> | Index16 of Index16.t
>> | Rgba32 of Rgba32.t
>> | Cmyk32 of Cmyk32.t;;
>>
>> Doesn't this mean that Rgb24.t is a subtype of Images.t and thus
>>; should be compatible?
>
> - Well it's a bit more than a subtype, even though in your case, you
> can see it as a subtype.
>
> But imagine you had :
>
> type t =
> |A of int
> |B of int
>
> Here, int can also be seen as a subtype of t. But it is "twice" : one
> time it is called A the other it is called B.
> There is more information than simply subtyping.
>
> - Notice type t could have been defined the following way :
>
> type t =
> | A of Index8.t
> | B of Rgb24.t
> | C of Index16.t
> | D of Rgba32.t
> | E of Cmyk32.t;;
>
> and then you would have done
>;
> Images.save "image.png" None [] (B img);
>
> I mean it's just a coincidence if the names were the same as the
> types' ones.
> Maybe this was the confusing point ?
>
>
> Hope this is clarifying things.

Thanks for the helpful explanation, Vincent.

Andre

__._,_.___
.

__,_._,___
[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )