All pastes #924546 Raw Edit

Someone

public text v1 · immutable
·published 2008-03-01 13:32 UTC
rendered paste body
-------------------exC.c------------------------------

#include <caml/bigarray.h> 

extern void my_c_function(double * data, int dimx, int dimy);

void my_c_function (double * data, int dimx, int dimy)
{
  int i,j;
  printf("\n\n");
  for(i=0; i<dimx; i++)
    {
     for(j=0; j<dimy; j++)
	{
 	printf("%d ", data[j]);
	} 
    }
    printf("\n\n");
}

value camlStub (value bigarray)
{
  int dimx = Bigarray_val(bigarray)->dim[0];
  int dimy = Bigarray_val(bigarray)->dim[1];
  my_c_function(Data_bigarray_val(bigarray), dimx, dimy);
  return Val_unit;
}

value sommeC (value x, value y)
{
  int cx = Int_val(x);
  int cy = Int_val(y);
  int res = cx + cy;
  printf("=> %d + %d = %d\n",cx,cy,res);
  return(res);
}


-------------------exOcaml.ml------------------------------

external somme : int -> int -> int = "sommeC";;
print_int (somme 2 23);;
print_newline();;


external afftab : (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array2.t -> unit = "camlStub";;

exception Out_of_range

class big_img w h =
object (this)
  val mutable width = w
  val mutable height = h
  val pixels = Bigarray.Array2.create
                        Bigarray.int8_unsigned
                        Bigarray.c_layout
                        w h

  method get_width = width

  method get_height = height

  method get_pixels = pixels

  method get_pixel x y = pixels.{x, y}

  method set_pixels array = Bigarray.Array2.blit array pixels

  method set_pixel x y v =
    if (x >= 0 && y >= 0 && x < width && y < height) then
      pixels.{x, y} <- v
    else
      raise Out_of_range

(*      Fills this object with a function (int -> int -> int)   *)
  method fill f =
    for i = 0 to width - 1 do
      for j = 0 to height - 1 do
        pixels.{i, j} <- f i j
      done
    done
    
(*      Returns the surrounding coordinates             *)
(*      from a point in a list in an array              *)
  method get_surround_coord xy size =
    let surround = ref []
    in
      (
        match xy with
            (x, y) ->
              (
                for i = 0 - (size / 2) to 0 + (size / 2) do
                  for j = 0 - (size / 2) to 0 + (size / 2) do
                    surround := (x - j, y - i)::!surround
                  done
              done
              )
      );
      !surround

(*      Gets the values from the list                   *)
(*      that you provide                                *)
  method get_values_from_list list = match list with
      [] -> []
    | (x,y)::t ->
        begin
         try
           (pixels.{x, y})::(this#get_values_from_list t)
         with
             _ -> this#get_values_from_list t
        end

  method pretty_print_or_not =
    for i = 0 to height - 1 do
      print_string "@ ";
      for j = 0 to width - 1 do
        if pixels.{j, i} > 255/2 then
          print_string "X"
        else
          print_string " "
      done;
        print_string " @\n"
    done;
    print_string "\n\n\n"
    
end


let _ =
let bla = new big_img 12 12 in
bla#fill (fun x y -> 0);
bla#pretty_print_or_not;
bla#set_pixel 2 1 255;
bla#set_pixel 3 1 255;
bla#set_pixel 4 1 255;
bla#set_pixel 5 1 255;
bla#set_pixel 1 2 255;
bla#set_pixel 2 2 255;
bla#set_pixel 3 2 255;
bla#set_pixel 4 2 255;
bla#set_pixel 5 2 255;
bla#set_pixel 1 3 255;
bla#set_pixel 2 3 255;
bla#set_pixel 3 3 255;
bla#set_pixel 4 3 255;
bla#set_pixel 5 3 255;
bla#set_pixel 1 4 255;
bla#set_pixel 2 4 255;
bla#set_pixel 3 4 255;
bla#set_pixel 4 4 255;
bla#set_pixel 1 5 255;
bla#set_pixel 2 5 255;
bla#set_pixel 3 5 255;
bla#set_pixel 4 5 255;
bla#set_pixel 8 5 255;
bla#set_pixel 10 5 255;
for i = 1 to 10 do
bla#set_pixel i 6 255
done;
for i = 4 to 10 do
bla#set_pixel i 7 255
done;
for i = 5 to 9 do
bla#set_pixel i 8 255
done;
for i = 6 to 8 do
bla#set_pixel i 9 255
done;
for i = 6 to 7 do
bla#set_pixel i 10 255
done;
bla#pretty_print_or_not;

afftab bla#get_pixels;
print_newline();;

-------------------makefile------------------------------

test:
	cc -c -I /usr/local/lib/ocaml  exC.c 
	ocamlopt bigarray.cmxa exC.o exOcaml.ml -o ex