Current section
Files
Jump to
Current section
Files
src/netpbm.erl
-module(netpbm).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/netpbm.gleam").
-export([render_pgm/4, simple_render_pgm/3, render_pbm/4, simple_render_pbm/3]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/netpbm.gleam", 102).
-spec pgm_loop(
bitstring(),
integer(),
integer(),
integer(),
integer(),
DLE,
fun((DLE, integer(), integer()) -> {DLE, integer()})
) -> {DLE, bitstring()}.
pgm_loop(Pbm, Width, Height, X, Y, State, Render) ->
{State@1, Pixel} = Render(State, X, Y),
Pgm = <<Pbm/bitstring, Pixel:8>>,
X@1 = case Width of
0 -> 0;
Gleam@denominator -> (X + 1) rem Gleam@denominator
end,
case X@1 of
0 when Y =:= (Height - 1) ->
{State@1, Pgm};
0 ->
pgm_loop(Pgm, Width, Height, 0, Y + 1, State@1, Render);
_ ->
pgm_loop(Pgm, Width, Height, X@1, Y, State@1, Render)
end.
-file("src/netpbm.gleam", 92).
?DOC(
" Build a PGM image by iterating over each pixel with some state.\n"
"\n"
" 0 is black, 255 is white. Values outside this range are truncated.\n"
"\n"
" The callback gets the X and Y position of the pixel, where 0,0 is\n"
" top-left.\n"
"\n"
" Rendering starts at the top and goes over each row, left to right, top to\n"
" bottom.\n"
).
-spec render_pgm(
integer(),
integer(),
DLD,
fun((DLD, integer(), integer()) -> {DLD, integer()})
) -> {DLD, bitstring()}.
render_pgm(Width, Height, State, Plot) ->
_pipe = <<"P5 "/utf8,
(erlang:integer_to_binary(Width))/binary,
" "/utf8,
(erlang:integer_to_binary(Height))/binary,
" 255 "/utf8>>,
pgm_loop(_pipe, Width, Height, 0, 0, State, Plot).
-file("src/netpbm.gleam", 74).
?DOC(
" Build a PGM image by iterating over each pixel.\n"
"\n"
" 0 is black, 255 is white. Values outside this range are truncated.\n"
"\n"
" The callback gets the X and Y position of the pixel, where 0,0 is\n"
" top-left.\n"
"\n"
" Rendering starts at the top and goes over each row, left to right, top to\n"
" bottom.\n"
).
-spec simple_render_pgm(
integer(),
integer(),
fun((integer(), integer()) -> integer())
) -> bitstring().
simple_render_pgm(Width, Height, Plot) ->
erlang:element(
2,
render_pgm(Width, Height, nil, fun(Nil, X, Y) -> {Nil, Plot(X, Y)} end)
).
-file("src/netpbm.gleam", 121).
-spec pad_to_bytes(bitstring()) -> bitstring().
pad_to_bytes(Pbm) ->
Remaining = (8 - (erlang:bit_size(Pbm) rem 8)) rem 8,
<<Pbm/bitstring, 0:(lists:max([(Remaining), 0]))>>.
-file("src/netpbm.gleam", 42).
-spec pbm_loop(
bitstring(),
integer(),
integer(),
integer(),
integer(),
DLC,
fun((DLC, integer(), integer()) -> {DLC, boolean()})
) -> {DLC, bitstring()}.
pbm_loop(Pbm, Width, Height, X, Y, State, Render) ->
{State@1, Pixel} = Render(State, X, Y),
Pbm@1 = case Pixel of
true ->
<<Pbm/bitstring, 1:1>>;
false ->
<<Pbm/bitstring, 0:1>>
end,
X@1 = case Width of
0 -> 0;
Gleam@denominator -> (X + 1) rem Gleam@denominator
end,
case X@1 of
0 when Y =:= (Height - 1) ->
{State@1, pad_to_bytes(Pbm@1)};
0 ->
_pipe = pad_to_bytes(Pbm@1),
pbm_loop(_pipe, Width, Height, 0, Y + 1, State@1, Render);
_ ->
pbm_loop(Pbm@1, Width, Height, X@1, Y, State@1, Render)
end.
-file("src/netpbm.gleam", 32).
?DOC(
" Build a PBM image by iterating over each pixel with some state.\n"
"\n"
" True is white, false is black.\n"
"\n"
" The callback gets the X and Y position of the pixel, where 0,0 is\n"
" top-left.\n"
"\n"
" Rendering starts at the top and goes over each row, left to right, top to\n"
" bottom.\n"
).
-spec render_pbm(
integer(),
integer(),
DLB,
fun((DLB, integer(), integer()) -> {DLB, boolean()})
) -> {DLB, bitstring()}.
render_pbm(Width, Height, State, Plot) ->
_pipe = <<"P4 "/utf8,
(erlang:integer_to_binary(Width))/binary,
" "/utf8,
(erlang:integer_to_binary(Height))/binary,
" "/utf8>>,
pbm_loop(_pipe, Width, Height, 0, 0, State, Plot).
-file("src/netpbm.gleam", 14).
?DOC(
" Build a PBM image by iterating over each pixel.\n"
"\n"
" True is white, false is black.\n"
"\n"
" The callback gets the X and Y position of the pixel, where 0,0 is\n"
" top-left.\n"
"\n"
" Rendering starts at the top and goes over each row, left to right, top to\n"
" bottom.\n"
).
-spec simple_render_pbm(
integer(),
integer(),
fun((integer(), integer()) -> boolean())
) -> bitstring().
simple_render_pbm(Width, Height, Plot) ->
erlang:element(
2,
render_pbm(Width, Height, nil, fun(Nil, X, Y) -> {Nil, Plot(X, Y)} end)
).