I have binary images with an fixed sized header. These can be converted into PGM images, provided cutting the header, and putting the appropriate information using linux/OSX tools as "head -c", "tail -c", and "cat". I can later import these images in Mathematica for further treatment.
I would like to create an importer in Mathematica as treated in the documentation which could do all this so the import can be done on a Windows station without using the linux/OSX tools.
I have not been able to manage this problem.
The only Idea I had is to use the functions BinaryReadList
or BinaryRead
. But since the information I get with them is not the same as what I get with ImageData[Import[file, "PGM"]]
. I am stuck.
The difference can be directly verified with binread-module
based an answer of Sjoerd C. de Vries. And also with a little code I wrote to show the difference on readable files
(* PGM : filename for output *)
PGM= "/Users/thomas/Documents/Mathematica/LM.pgm";
Dim = {25,25};
f[x_,y_]:=2^16*E^(-(2/3) (x^2-0.5` x y+0.25` y^2));
(* Creating now an small Image *)
LittleMat=IntegerPart[Array[f,Dim,{{-6,6},{-6,6}}]+1];
(*LittleMat= RandomInteger[{1,2^16},Dim];*)
ArrayPlot[LittleMat, ImageSize->{300,300},AspectRatio->Full]
(* Exporting and re-importing the small Image *)
Export[PGM,Image[LittleMat,"UnsignedInteger16"],"PGM"];
ReImp = Import[PGM,"PGM"];
RI = IntegerPart[2^16*ImageData[ReImp]];
ArrayPlot[RI, ImageSize->{300,300},AspectRatio->Full]
(* Information is luckily still the same *)
(*LittleMat-RI*)
(* Now reading the file binary, bitwise *)
Bytestest = FileByteCount[PGM]
N[Bytestest /(25*25)]
(* Why is the ImageSize in bits twice the pixel-number? *)
HeaderL = Bytestest -2*25*25
stream=OpenRead[PGM,BinaryFormat -> True];
SetStreamPosition[stream,HeaderL];
MyPGM = BinaryReadList[stream,"UnsignedInteger16",ByteOrdering->-1];
SetStreamPosition[stream,0];
Close[stream];
(* MyPGM list of the same size as pixel-number *)
PGMmat = Partition[MyPGM,25];
ArrayPlot[PGMmat, ImageSize->{300,300},AspectRatio->Full]
(* Information not the same aby more *)
ArrayPlot[PGMmat-RI, ImageSize->{300,300},AspectRatio->Full]