SHARE
TWEET

Write a TGA image file

ptrelford Sep 14th, 2015 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let tga f =
  2.   let width = 640 in
  3.   let height = 480 in
  4.   let header =
  5.     [|0;0;2;0;0;0;0;0;0;0;0;0;
  6.       width mod 256;width/256;
  7.       height mod 256;height/256;
  8.       24;0|] in
  9.   let asByte x =
  10.      min 255 (max 0 (int_of_float (128.0 *. (x +. 1.0))))
  11.   in
  12.   let o = open_out_bin "x.tga" in
  13.   Array.iter (output_byte o) header;
  14.   for y = 0 to height - 1 do
  15.     for x = 0 to width - 1 do
  16.        let u = float_of_int (x-width/2) /. float_of_int width in
  17.        let v = float_of_int (y-height/2) /. float_of_int height in
  18.        let r,g,b = f (u,v) in
  19.        output_byte o (asByte b);
  20.        output_byte o (asByte g);
  21.        output_byte o (asByte r)
  22.     done
  23.   done;
  24.   close_out o
  25. let () =
  26.    tga (fun (x,y) -> sin x, sin y, x *. y)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top