This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,51 @@
# Dirty implementation
# Only P3 format, an image is a list of 3 matrices (r, g, b)
# Max color is always 255
WriteImage := function(name, img)
local f, r, g, b, i, j, maxcolor, nrow, ncol, dim;
f := OutputTextFile(name, false);
r := img[1];
g := img[2];
b := img[3];
dim := DimensionsMat(r);
nrow := dim[1];
ncol := dim[2];
maxcolor := 255;
WriteLine(f, "P3");
WriteLine(f, Concatenation(String(ncol), " ", String(nrow), " ", String(maxcolor)));
for i in [1 .. nrow] do
for j in [1 .. ncol] do
WriteLine(f, Concatenation(String(r[i][j]), " ", String(g[i][j]), " ", String(b[i][j])));
od;
od;
CloseStream(f);
end;
PutPixel := function(img, i, j, color)
img[1][i][j] := color[1];
img[2][i][j] := color[2];
img[3][i][j] := color[3];
end;
GetPixel := function(img, i, j)
return [img[1][i][j], img[2][i][j], img[3][i][j]];
end;
NewImage := function(nrow, ncol, color)
local r, g, b;
r := color[1] + NullMat(nrow, ncol);
g := color[2] + NullMat(nrow, ncol);
b := color[3] + NullMat(nrow, ncol);
return [r, g, b];
end;
# Reproducing the example from Wikipedia
black := [ 0, 0, 0 ];
g := NewImage(2, 3, black);
PutPixel(g, 1, 1, [255, 0, 0]);
PutPixel(g, 1, 2, [0, 255, 0]);
PutPixel(g, 1, 3, [0, 0, 255]);
PutPixel(g, 2, 1, [255, 255, 0]);
PutPixel(g, 2, 2, [255, 255, 255]);
PutPixel(g, 2, 3, [0, 0, 0]);
WriteImage("example.ppm", g);

View file

@ -0,0 +1,7 @@
require 'files'
NB. ($x) is height, width, colors per pixel
writeppm=:dyad define
header=. 'P6',LF,(":1 0{$x),LF,'255',LF
(header,,x{a.) fwrite y
)

View file

@ -0,0 +1,4 @@
NB. create 10 by 10 block of magenta pixels in top right quadrant of a 300 wide by 600 high green image
myimg=: ((145 + pixellist) ; 255 0 255) setPixels 0 255 0 makeRGB 600 200
myimg writeppm jpath '~temp/myimg.ppm'
360015

View file

@ -0,0 +1 @@
Export["file.ppm",image,"PPM"]

View file

@ -0,0 +1,7 @@
INTERFACE PPM;
IMPORT Bitmap, Pathname;
PROCEDURE Create(imgfile: Pathname.T; img: Bitmap.T);
END PPM.

View file

@ -0,0 +1,30 @@
MODULE PPM;
IMPORT Bitmap, Wr, FileWr, Pathname;
FROM Fmt IMPORT F, Int;
<*FATAL ANY*>
VAR imgfilewr: FileWr.T;
PROCEDURE Create(imgfile: Pathname.T; img: Bitmap.T) =
VAR height := LAST(img^);
width := LAST(img[0]);
color: Bitmap.Pixel;
BEGIN
imgfilewr := FileWr.Open(imgfile);
Wr.PutText(imgfilewr, F("P6\n%s %s\n255\n", Int(height + 1), Int(width + 1)));
FOR i := 0 TO height DO
FOR j := 0 TO width DO
color := img[i,j];
Wr.PutChar(imgfilewr, VAL(color.R, CHAR));
Wr.PutChar(imgfilewr, VAL(color.G, CHAR));
Wr.PutChar(imgfilewr, VAL(color.B, CHAR));
END;
END;
Wr.PutChar(imgfilewr, '\n');
Wr.Flush(imgfilewr);
END Create;
BEGIN
END PPM.

View file

@ -0,0 +1,14 @@
let output_ppm ~oc ~img:(_, r_channel, g_channel, b_channel) =
let width = Bigarray.Array2.dim1 r_channel
and height = Bigarray.Array2.dim2 r_channel in
Printf.fprintf oc "P6\n%d %d\n255\n" width height;
for y = 0 to pred height do
for x = 0 to pred width do
output_char oc (char_of_int r_channel.{x,y});
output_char oc (char_of_int g_channel.{x,y});
output_char oc (char_of_int b_channel.{x,y});
done;
done;
output_char oc '\n';
flush oc;
;;