caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Different function on Image format  (uses camlImages)
@ 2003-11-27 23:27 chris.danx
  2003-11-27 23:55 ` chris.danx
  0 siblings, 1 reply; 3+ messages in thread
From: chris.danx @ 2003-11-27 23:27 UTC (permalink / raw)
  To: Caml Mailing List

Hi,

I'm writing a simple texture loader class so that I can load up images 
using camlimages, convert them to a format suitable for opengl, then 
store them under some name in a hash table.  So far I've got it working 
for rgb24 format images, but ideally I want to use both 24bit rgb pixels 
and 32bit rgba pixels.  The problem is deciding which function to call 
depending on the image type.

Before I go on, is this an appropriate place to ask about CamlImages? 
Also I am new to OCaml, and this is one of the few things I've attempted 
of any complexity.

This is the class as it stands.


class texture_loader
   = object(self)

     val mutable texts = Hashtbl.create 5;

     method load (filename : string) (tex_id : string) =
	try
	  let x = Hashtbl.find texts tex_id in
	    raise (Id_exists tex_id)
	with
	    Not_found ->         	
	      let img = OImage.load filename [] in
		let img = OImage.rgb24 img in
		  Hashtbl.add texts tex_id (rgb_to_gl img);
		  ()
end;;


Assuming I have functions "rgb_to_gl" and "rgba_to_gl", how can I tell 
which format the image is so the correct function can be called. 
Something like this is what sprang to mind

let img = match <image_format> with
      <rgb>  -> OImage.rgb24  img
    | <rgba> -> OImage.rgba24 img
in ...

The problem is what should the things between the angled brackets be?


Thanks,
Chris

-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Different function on Image format  (uses camlImages)
  2003-11-27 23:27 [Caml-list] Different function on Image format (uses camlImages) chris.danx
@ 2003-11-27 23:55 ` chris.danx
  2003-11-29  0:36   ` chris.danx
  0 siblings, 1 reply; 3+ messages in thread
From: chris.danx @ 2003-11-27 23:55 UTC (permalink / raw)
  To: Caml Mailing List

chris.danx wrote:

> let img = match <image_format> with
>      <rgb>  -> OImage.rgb24  img
>    | <rgba> -> OImage.rgba24 img
> in ...
> 
> The problem is what should the things between the angled brackets be?

Zaps himself!!! I went off on a tangent there.  I can tell which format 
the image is but can't apply rgba_to_gl.  The error is


File "texload.ml", line 35, characters 45-48:
This expression has type Image.rgb = Color.Rgb.t but is here used with type
   Image.rgba = Color.Rgba.t


Line 35 is the [| 4 component array |] in rgba_to_gl.  I think I 
understand why i get this error, I'm just not sure I know how to resolve 
it (i got it previously, but ripped out the pattern matching code for 
the image format).  It seems the image needs to be treated as 
OImage.rgba32 but I'm a bit lost...

This is the complete script so far...


let rgb_to_gl img =
   let     w = img#width
       and h = img#height in
       let targ_img = GlPix.create `ubyte ~format:`rgb ~width:w ~height:h in
	for i = 0 to w - 1 do
	  for j = 0 to h - 1 do
	    let pix = img#get i j in
	      Raw.sets (GlPix.to_raw targ_img)
	                ~pos:(3*i*h+j)
     	                [| pix.r; pix.g; pix.b |];
	  done
	done;
	targ_img;;


let rgba_to_gl img =
   let     w = img#width
       and h = img#height in
       let targ_img = GlPix.create `ubyte ~format:`rgba ~width:w 
~height:h in
	for i = 0 to w - 1 do
	  for j = 0 to h - 1 do
	    let pix = img#get i j in
	      Raw.sets (GlPix.to_raw targ_img)
	                ~pos:(4*i*h+j)
     	                [| pix.r; pix.g; pix.b; pix.alpha|];
	  done
	done;
	targ_img;;


class texture_loader
   = object(self)

     val mutable texts = Hashtbl.create 5;

     method load (filename : string) (tex_id : string) =
	try
	  let x = Hashtbl.find texts tex_id in
	    raise (Id_exists tex_id)
	with
	    Not_found ->         	
	      let img = OImage.load filename [] in
    	        let conv = match img#tag with
		    ClassRgb24  -> rgb_to_gl
		  | ClassRgba32 -> rgba_to_gl
		in
		  Hashtbl.add texts tex_id (conv img);
		  ()
end;;

let _ =
   let tex = new texture_loader in
     tex#load "NeHe.bmp" "bob";;




Chris

-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Different function on Image format  (uses camlImages)
  2003-11-27 23:55 ` chris.danx
@ 2003-11-29  0:36   ` chris.danx
  0 siblings, 0 replies; 3+ messages in thread
From: chris.danx @ 2003-11-29  0:36 UTC (permalink / raw)
  To: Caml Mailing List

Hi

I've sortof resolved the original problem by a) being awake, b) 
realising what the compiler was actually saying and c) simplifying the 
code to always convert to rgba32 and hence all textures will be in that 
format.

I am still unsure how to handle the general problem of determining the 
type of class and passing to specific functions but it'll keep!


Regards,
Chris

-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-11-29 17:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-27 23:27 [Caml-list] Different function on Image format (uses camlImages) chris.danx
2003-11-27 23:55 ` chris.danx
2003-11-29  0:36   ` chris.danx

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).