-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]). -export_type([black_or_white/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type black_or_white() :: black | white. -file("src/netpbm.gleam", 103). -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 = <>, 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", 93). ?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", 75). ?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", 122). -spec pad_to_bytes(bitstring()) -> bitstring(). pad_to_bytes(Pbm) -> Remaining = (8 - (erlang:bit_size(Pbm) rem 8)) rem 8, <>. -file("src/netpbm.gleam", 43). -spec pbm_loop( bitstring(), integer(), integer(), integer(), integer(), DLC, fun((DLC, integer(), integer()) -> {DLC, black_or_white()}) ) -> {DLC, bitstring()}. pbm_loop(Pbm, Width, Height, X, Y, State, Render) -> {State@1, Pixel} = Render(State, X, Y), Pbm@1 = case Pixel of white -> <>; black -> <> 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", 33). ?DOC( " Build a PBM image by iterating over each pixel with some state.\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, black_or_white()}) ) -> {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", 12). ?DOC( " Build a PBM image by iterating over each pixel.\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()) -> black_or_white()) ) -> bitstring(). simple_render_pbm(Width, Height, Plot) -> erlang:element( 2, render_pbm(Width, Height, nil, fun(Nil, X, Y) -> {Nil, Plot(X, Y)} end) ).