Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. module Image = {
  2. type channel_t =
  3. Bigarray.Array2.t(char, Bigarray.int8_unsigned_elt, Bigarray.c_layout);
  4. type t = {
  5. width: int,
  6. height: int,
  7. r_channel: channel_t,
  8. g_channel: channel_t,
  9. b_channel: channel_t,
  10. };
  11. let make = (width, height) => {
  12. let make_channel = () =>
  13. Bigarray.(Array2.create(char, c_layout, width, height));
  14. {
  15. width,
  16. height,
  17. r_channel: make_channel(),
  18. g_channel: make_channel(),
  19. b_channel: make_channel(),
  20. };
  21. };
  22. let set_rgb = (img, ~x, ~y, ~rgb as (r, g, b)) => {
  23. (img.r_channel).{x, y} = char_of_int(r);
  24. (img.g_channel).{x, y} = char_of_int(g);
  25. (img.b_channel).{x, y} = char_of_int(b);
  26. };
  27. let fill = (img, ~rgb) => {
  28. for (y in 0 to pred(img.height)) {
  29. for (x in 0 to pred(img.width)) {
  30. set_rgb(img, ~x, ~y, ~rgb);
  31. };
  32. };
  33. };
  34. let get_rgb = (img, ~x, ~y) => {
  35. (
  36. (img.r_channel).{x, y} |> int_of_char,
  37. (img.g_channel).{x, y} |> int_of_char,
  38. (img.b_channel).{x, y} |> int_of_char,
  39. );
  40. };
  41. let output_ppm = (img, oc) => {
  42. Printf.fprintf(oc, "P6\n%d %d\n255\n", img.width, img.height);
  43. for (y in 0 to pred(img.height)) {
  44. for (x in 0 to pred(img.width)) {
  45. Out_channel.output_char(oc, (img.r_channel).{x, y});
  46. Out_channel.output_char(oc, (img.g_channel).{x, y});
  47. Out_channel.output_char(oc, (img.b_channel).{x, y});
  48. };
  49. };
  50. Out_channel.output_char(oc, '\n');
  51. Out_channel.flush(oc);
  52. };
  53. };
  54.  
  55. module Thick_line_image = {
  56. include Image;
  57. let height = 30;
  58. let make = width => Image.make(width, height);
  59. let set_rgb = (img, ~x, ~rgb) => {
  60. for (y in 0 to pred(height)) {
  61. Image.set_rgb(img, ~x, ~y, ~rgb);
  62. };
  63. };
  64. let set_rgb_range = (img, ~x_range as (x_start, x_end), ~rgb) => {
  65. for (x in x_start to x_end) {
  66. set_rgb(img, ~x, ~rgb);
  67. };
  68. };
  69. let get_rgb = (img, ~x, ~y) => {
  70. Image.get_rgb(img, ~x, ~y=0);
  71. };
  72. };
  73.  
  74. module Colors = {
  75. let aqua = (0, 255, 255);
  76. let black = (0, 0, 0);
  77. let blue = (0, 0, 255);
  78. let fuchsia = (255, 0, 255);
  79. let gray = (128, 128, 128);
  80. let green = (0, 128, 0);
  81. let lime = (0, 255, 0);
  82. let maroon = (128, 0, 0);
  83. let navy = (0, 0, 128);
  84. let olive = (128, 128, 0);
  85. let purple = (128, 0, 128);
  86. let red = (255, 0, 0);
  87. let silver = (192, 192, 192);
  88. let teal = (0, 128, 128);
  89. let white = (255, 255, 255);
  90. let yellow = (255, 255, 0);
  91. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement