Loading ...
Sorry, an error occurred while loading the content.
 

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

Expand Messages
  • Vincent Aravantinos
    ... (...) ... 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
    Message 1 of 5 , Sep 9, 2007
      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
    • William Neumann
      ... 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
      Message 2 of 5 , Sep 9, 2007
        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
      • Andre Kuehne
        ... Thanks, William! This works, but i have to read some more about the Ocaml type system to really understand why this is necessary.
        Message 3 of 5 , Sep 9, 2007
          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
          >
          >
          >
          >
        • Andre Kuehne
          ... Thanks for the helpful explanation, Vincent. Andre
          Message 4 of 5 , Sep 10, 2007
            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
          Your message has been successfully submitted and would be delivered to recipients shortly.