caml-list - the Caml user's mailing list
help / Atom feed
* [Caml-list] Problem with Graph module
@ 2002-06-17  1:04 Nicolas FRANCOIS (AKA El Bofo)
2002-06-17 13:23 ` Jun P.FURUSE
From: Nicolas FRANCOIS (AKA El Bofo) @ 2002-06-17  1:04 UTC (permalink / raw)
To: Caml List

A strange thing, under Linux : this piece of code :

open Graphics;;
open Random;;

type t_fourmi = {mutable dir : int;
mutable x : int;
mutable y : int};;

let trace_fourmi_deux_etats n =
let mouvement = [|(0, 5); (-5, 0); (0, -5); (5, 0)|] in
let init = open_graph " 800x800+50-50" in
let ma_fourmi = {dir = 0; x = 349; y = 349} in
let onestep () =
if (point_color ma_fourmi.x ma_fourmi.y) = white
then
begin
set_color black;
ma_fourmi.dir <-
if ma_fourmi.dir + 1 > 3 then 0 else ma_fourmi.dir + 1
end
else
begin
set_color white;
ma_fourmi.dir <-
if ma_fourmi.dir - 1 < 0 then 3 else ma_fourmi.dir - 1
end;
fill_rect ma_fourmi.x ma_fourmi.y 5 5;
let (dx, dy) = mouvement.(ma_fourmi.dir) in
ma_fourmi.x <- ma_fourmi.x + dx;
ma_fourmi.y <- ma_fourmi.y + dy;
in
init;
for i = 0 to n do
onestep ();
for j = 1 to 10000 do begin end done
done;;

trace_fourmi_deux_etats 10000;;

works perfectly in camllight (with a few modifications to adapt the code),
but not in Ocaml. The graphic window opens well, but the test

if (point_color ma_fourmi.x ma_fourmi.y) = white

is allways false under Ocaml.

Can you see the problem ?????

Thanks.

--

Nicolas FRANCOIS
http://nicolas.francois.free.fr
A TRUE Klingon programmer does NOT comment his code
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

*
2002-06-17  1:04 [Caml-list] Problem with Graph module Nicolas FRANCOIS (AKA El Bofo)
@ 2002-06-17 13:23 ` Jun P.FURUSE
2002-06-17 17:38   ` [Caml-list] Nicolas FRANCOIS (AKA El Bofo)
From: Jun P.FURUSE @ 2002-06-17 13:23 UTC (permalink / raw)
To: nicolas.francois; +Cc: caml-list

Hello,

> if (point_color ma_fourmi.x ma_fourmi.y) = white
> is allways false under Ocaml.

If the following code says "BUG" in your environment, I am afraid that
there is a bug inside the graphics library. In such case, could you
send me the result of the command xdpyinfo in your environment ?

(* ocamlc -o testgraphics graphics.cma testgraphics.ml *)
open Graphics
let _ =
open_graph "";
if point_color 0 0 = white then
prerr_endline "OK"
else prerr_endline "BUG"
;;

--
Jun
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

* [Caml-list] Re:
2002-06-17 13:23 ` Jun P.FURUSE
@ 2002-06-17 17:38   ` Nicolas FRANCOIS (AKA El Bofo)
2002-06-18  9:48     ` [Caml-list] Re: Problem with Graph module Jun P.FURUSE
From: Nicolas FRANCOIS (AKA El Bofo) @ 2002-06-17 17:38 UTC (permalink / raw)
To: Jun P.FURUSE; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 876 bytes --]

Le Mon, 17 Jun 2002 15:23:09 +0200 (CEST) "Jun P.FURUSE"
<Jun.Furuse@inria.fr> a écrit :

> Hello,
>
> > if (point_color ma_fourmi.x ma_fourmi.y) = white
> > is allways false under Ocaml.
>
> If the following code says "BUG" in your environment, I am afraid that
> there is a bug inside the graphics library. In such case, could you
> send me the result of the command xdpyinfo in your environment ?
>
> (* ocamlc -o testgraphics graphics.cma testgraphics.ml *)
> open Graphics
> let _ =
>     open_graph "";
>     if point_color 0 0 = white then
>       prerr_endline "OK"
>     else prerr_endline "BUG"
> ;;

You'll find the xdpyinfo output in attachment.

What can I do, boss ? Please !!!!!

\bye

--

Nicolas FRANCOIS
http://nicolas.francois.free.fr
A TRUE Klingon programmer does NOT comment his code

[-- Attachment #2: xdpyinfo.out --]
[-- Type: application/octet-stream, Size: 9720 bytes --]

* [Caml-list] Re: Problem with Graph module
2002-06-17 17:38   ` [Caml-list] Nicolas FRANCOIS (AKA El Bofo)
@ 2002-06-18  9:48     ` Jun P.FURUSE
2002-06-18 16:53       ` Nicolas FRANCOIS (AKA El Bofo)
From: Jun P.FURUSE @ 2002-06-18  9:48 UTC (permalink / raw)
To: nicolas.francois; +Cc: caml-list, ecc, Andreas.Eder

Hello,

I thank people who sent their xdpyinfo result.

> Le Mon, 17 Jun 2002 15:23:09 +0200 (CEST) "Jun P.FURUSE"
> <Jun.Furuse@inria.fr> a écrit :
>
> > Hello,
> >
> > > if (point_color ma_fourmi.x ma_fourmi.y) = white
> > > is allways false under Ocaml.
> >
> > If the following code says "BUG" in your environment, I am afraid that
> > there is a bug inside the graphics library. In such case, could you
> > send me the result of the command xdpyinfo in your environment ?
> >
> > (* ocamlc -o testgraphics graphics.cma testgraphics.ml *)
> > open Graphics
> > let _ =
> >     open_graph "";
> >     if point_color 0 0 = white then
> >       prerr_endline "OK"
> >     else prerr_endline "BUG"
> > ;;

I verified that this code prints out "BUG" when X display has less
than 24bits color depth. In such environment, the colour mapping from
Graphics to X is not one-to-one. This means that point_color may not
always return the exact colour which you drew on the screen.
For example, in an X display of 15bpp, the Graphics colour 0xffffff
and 0xf8f8f8 are translated to the same X white colour.
This is also true for the default white background color of Graphics.
point_color returns 0xf8f8f8, not 0xffffff, this is why the above
code prints out "BUG".

In Camllight, the translation between Caml colour and X colour
performs communication between programs and the X server. Since it is
highly expensive operation, the colour translation of O'Caml Graphics
is rewritten, so that it would never ask the X server about
colours. It improves the speed of colour query impressively.
Unfortunately, the both colour translation code do not return always
the same result. The different behavior of the Nicolas' O'Caml and
Camllight programs can be explained by this difference of colour
translation.

I think this does not mean the new colour translation of O'Caml is
buggy. Even in Camllight, the same things can happen, unless the colour
translation is one-to-one. (It happens less, but not never.)

The important things (which should be noted in graphics.mli) are that

* you may get different colour index from one you draw,

* the colour indices predefined in Graphics for the basic colours
such as white, blue, red, etc can be just canonical candidate
indices for them.

You should not compare these colour indices against the colours
got from the screen. For example, in Nicolas' program, instead of
comparing Graphics.white, we should use the colour index obtained
from the screen  at the begining of the program:

let screen_white = point_color 0 0
(* this can be different from Graphics.white *)

Regards,
--
Jun
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

* [Caml-list] Re: Problem with Graph module
2002-06-18  9:48     ` [Caml-list] Re: Problem with Graph module Jun P.FURUSE
@ 2002-06-18 16:53       ` Nicolas FRANCOIS (AKA El Bofo)
2002-06-18 23:13         ` Nicolas FRANCOIS (AKA El Bofo)
From: Nicolas FRANCOIS (AKA El Bofo) @ 2002-06-18 16:53 UTC (permalink / raw)
To: Jun P.FURUSE; +Cc: Caml List

Le Tue, 18 Jun 2002 11:48:31 +0200 (CEST) "Jun P.FURUSE"
<Jun.Furuse@inria.fr> a écrit :

> I verified that this code prints out "BUG" when X display has less
> than 24bits color depth. In such environment, the colour mapping from
> Graphics to X is not one-to-one. This means that point_color may not
> always return the exact colour which you drew on the screen.
> For example, in an X display of 15bpp, the Graphics colour 0xffffff
> and 0xf8f8f8 are translated to the same X white colour.
> This is also true for the default white background color of Graphics.
> point_color returns 0xf8f8f8, not 0xffffff, this is why the above
> code prints out "BUG".

OK. Now I have this simple problem : I can use Direct Rendering from X
with my Voodoo card in 15 bits, and correct color translation in 24 bits.
is that right ?

> In Camllight, the translation between Caml colour and X colour
> performs communication between programs and the X server. Since it is
> highly expensive operation, the colour translation of O'Caml Graphics
> is rewritten, so that it would never ask the X server about
> colours. It improves the speed of colour query impressively.
> Unfortunately, the both colour translation code do not return always
> the same result. The different behavior of the Nicolas' O'Caml and
> Camllight programs can be explained by this difference of colour
> translation.

Who said computer science is not an exact one ?

> I think this does not mean the new colour translation of O'Caml is
> buggy. Even in Camllight, the same things can happen, unless the colour
> translation is one-to-one. (It happens less, but not never.)
>
> The important things (which should be noted in graphics.mli) are that
>
>   * you may get different colour index from one you draw,
>
>   * the colour indices predefined in Graphics for the basic colours
>     such as white, blue, red, etc can be just canonical candidate
>     indices for them.
>
> You should not compare these colour indices against the colours
> got from the screen. For example, in Nicolas' program, instead of
> comparing Graphics.white, we should use the colour index obtained
> from the screen  at the begining of the program:
>
>       let screen_white = point_color 0 0
>       (* this can be different from Graphics.white *)

Great. Thanks for the tip. Euh, and what if I have to test several colors
? Oh, yes, here it is :

let screen_from_color c =
set_color x;
plot 0 0;
point_color 0 0;;

Right ? I'll try it now.

\bye, and thanks again.

--

Nicolas FRANCOIS
http://nicolas.francois.free.fr
A TRUE Klingon programmer does NOT comment his code
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

* Re: [Caml-list] Re: Problem with Graph module
2002-06-18 16:53       ` Nicolas FRANCOIS (AKA El Bofo)
@ 2002-06-18 23:13         ` Nicolas FRANCOIS (AKA El Bofo)
0 siblings, 0 replies; 6+ messages in thread
From: Nicolas FRANCOIS (AKA El Bofo) @ 2002-06-18 23:13 UTC (permalink / raw)
To: caml-list

Le Tue, 18 Jun 2002 19:01:39 +0200 Nicolas FRANCOIS (AKA El Bofo)
<nicolas.francois@free.fr> a écrit :

> Great. Thanks for the tip. Euh, and what if I have to test several
> colors? Oh, yes, here it is :
>
> let screen_from_color c =
>   set_color x;
>   plot 0 0;
>   point_color 0 0;;
>
> Right ? I'll try it now.

A better version (known to work, it ran just a few seconds ago in front of
my eyes ;-) :

let color_from_screen c =
let o = point_color 0 0 in
set_color c;
plot 0 0;
let s = point_color 0 0 in
set_color o;
plot 0 0;
s;;

Just keep the place exactly in the same state you would like to find it...

Release 1.0 for monday.

Anyway, if you don't know the Langton ant, you should try it. It's
amazing. And the generalizes ones are not bad, too.

\bye

--

Nicolas FRANCOIS
http://nicolas.francois.free.fr
A TRUE Klingon programmer does NOT comment his code
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

end of thread, back to index

2002-06-17  1:04 [Caml-list] Problem with Graph module Nicolas FRANCOIS (AKA El Bofo)
2002-06-17 13:23 ` Jun P.FURUSE
2002-06-17 17:38   ` [Caml-list] Nicolas FRANCOIS (AKA El Bofo)
2002-06-18  9:48     ` [Caml-list] Re: Problem with Graph module Jun P.FURUSE
2002-06-18 16:53       ` Nicolas FRANCOIS (AKA El Bofo)
2002-06-18 23:13         ` Nicolas FRANCOIS (AKA El Bofo)

caml-list - the Caml user's mailing list

Archives are clonable: git clone --mirror https://inbox.ocaml.org/caml-list

AGPL code for this site: git clone https://public-inbox.org/ public-inbox