RosettaCodeData/Task/Bitmap/Prolog/bitmap.pro

52 lines
1.5 KiB
Prolog
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
:- module(bitmap, [
2026-04-30 12:34:36 -04:00
new_bitmap/3,
fill_bitmap/3,
get_pixel0/3,
set_pixel0/4 ]).
2023-07-01 11:58:00 -04:00
:- use_module(library(lists)).
%-----------------------------------------------------------------------------%
% Convenience Predicates
replicate(Term,Times,L):-
2026-04-30 12:34:36 -04:00
length(L,Times),
maplist(=(Term),L).
2023-07-01 11:58:00 -04:00
replace0(N,OL,E,NL):-
2026-04-30 12:34:36 -04:00
nth0(N,OL,_,TL),
nth0(N,NL,E,TL).
2023-07-01 11:58:00 -04:00
%-----------------------------------------------------------------------------%
% Bitmap Utilities
%
% The Bitmap structure is a list with pixels kept in row major order:
% [dimensions-[X,Y],pixels-[[n11,n12...],[n21,n22...]]]
% In this code what exactly an RGB value is doesn't matter however
% in other bitmap tasks it is assumed to be a list [R,G,B] where
% each is an int between 0 and 255, in code:
rgb_pixel(RGB):-
2026-04-30 12:34:36 -04:00
length(RGB,3),
maplist(integer,RGB),
maplist(between(0,255),RGB).
2023-07-01 11:58:00 -04:00
%new_bitmap(Bitmap,Dimensions,RGB)
new_bitmap([[X,Y],Pixels],[X,Y],RGB) :-
2026-04-30 12:34:36 -04:00
replicate(RGB,X,Row),
replicate(Row,Y,Pixels).
2023-07-01 11:58:00 -04:00
%fill_bitmap(New_Bitmap,Bitmap,RGB)
fill_bitmap(New_Bitmap,[[X,Y],_],RGB) :-
2026-04-30 12:34:36 -04:00
new_bitmap(New_Bitmap,[X,Y],RGB).
2023-07-01 11:58:00 -04:00
%here get and set use 0 based indexing
%get_pixel0(Bitmap,Coordinates,RGB)
get_pixel0([[_DimX,_DimY],Pixels],[X,Y],RGB) :-
2026-04-30 12:34:36 -04:00
nth0(Y,Pixels,Row),
nth0(X,Row,RGB).
2023-07-01 11:58:00 -04:00
%set_pixel0(New Bitmap, Bitmap, Coordinates, RGB)
set_pixel0([[DimX,DimY],New_Pixels],[[DimX,DimY],Pixels],[X,Y],RGB) :-
2026-04-30 12:34:36 -04:00
nth0(Y,Pixels,Row),
replace0(X,Row,RGB,New_Row),
replace0(Y,Pixels,New_Row,New_Pixels).