MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Importing images

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13004] RE: [mg12951] Importing images
  • From: "Barthelet, Luc" <lucb at ea.com>
  • Date: Tue, 30 Jun 1998 00:26:03 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

This should do it.

ReadBMPfile[filename_]:=Module[
	
{afile,data,validFile,width,height,planes,bitPerPixel,compression,
      sizeOfBitmap,horzRes,vertRes,numOfColors,colorsImportant,bitmap,
      firstImageByte,palette},
 
toDWORD[l_]:= Apply[Plus,l {1,256, 65536, 16777216}]; toWORD[l_]:=
Apply[Plus,l {1,256}];
		
afile = OpenRead[filename,DOSTextFormat->False]; data =
ReadList[afile,Byte];
Close[afile];
		
bitPerPixel = toWORD[data[[{29,30}]] ]; firstImageByte = toDWORD[ data[[
{11,12,13,14} ]] ]; numOfColors =  toDWORD[ data[[ {47,48,49,50} ]] ];
compression = toDWORD[ data[[ {31,32,33,34} ]] ];

validFile = (compression==0)&&
(data[[{1,2}]] == {66,77}) &&
		(data[[ {7,8,9,10}]] == {0,0,0,0})&&
		( toDWORD[ data[[ {3,4,5,6}]]] == Length[data] ) && (firstImageByte 
== 54 + 4 numOfColors)&&
		((bitPerPixel==24)|| (bitPerPixel==8));

				If[validFile ,
width = toDWORD[ data[[ {19,20,21,22} ]] ]; height = toDWORD[ data[[
{23,24,25,26} ]] ];
			
(* don't need any of those values 
planes = toWORD[data[[{27,28}]] ];
sizeOfBitmap =  toDWORD[ data[[ {35,36,37,38} ]] ]; horzRes =  toDWORD[
data[[ {39,40,41,42} ]] ]; vertRes =  toDWORD[ data[[ {43,44,45,46} ]]
]; numOfColors =  toDWORD[ data[[ {47,48,49,50} ]] ]; colorsImportant =
toDWORD[ data[[ {51,52,53,54} ]] ]; *)
			
If[(bitPerPixel==24),
				(* 24 bit stuff *)
bitmap = Take[data,{firstImageByte+1,firstImageByte+width height 3}];
bitmap = Map[Reverse,Partition[Partition[bitmap,3],width],{2}];
			{bitmap,{}},
		(* 8 bit stuff *)
		palette =
          Rest /@ (Reverse /@ Partition[Take[data,{55,55+4
numOfColors}],4]);
		bitmap  = 
          Partition[
            Take[data,{firstImageByte+1,firstImageByte+1+width height
}],
            width];
				(* return the bitmap and the palette *)
				{bitmap,palette}
		],
			
			Print["wrong format for BMP file, write more code..."];
			If[  toDWORD[ data[[ {3,4,5,6}]]] == Length[data],,
				Print[ "Data Length: ",Length[data]," versus:" ,
          toDWORD[ data[[ {3,4,5,6}]]]," reported"]];
			]
](*Module *);

ShowBMP[{bitmap_,palette_}]:=Module[
		{width,height,copyPalette},
		If[Length[bitmap]>0,
	{height,width} = Dimensions[bitmap][[{1,2}]]; Show[Graphics[
					If[palette=={},
						(* for 24 bit images *)
						RasterArray[
Map[Apply[RGBColor,# ]&, bitmap/255.,{2}]],
						(* for 8 bit images *)
	
copyPalette=Map[Apply[RGBColor,# ]&, palette/255.,{1}];
	
RasterArray[Map[copyPalette[[#+1]]&,bitmap,{2}] ] ]
	
],ImageSize->{width+9,height+9},AspectRatio->(height+9)/(width+9)];
			];
		](*Module *);

> -----Original Message-----
> From:	Carl Storrs [SMTP:carl at mdru.uc.edu]
To: mathgroup at smc.vnet.net
> Sent:	Saturday, June 27, 1998 11:52 PM
> To:	mathgroup at smc.vnet.net
> Subject:	[mg12951] Importing images
> 
> I'm doing some medical image measurement, and it would be great to do
> it
> in Mathematica.
> 
> My image files are in BMP format. Does anybody know how to get the
> images into a Raster or RasterArray?
> 
> Thanks in advance.
> 
> ___________________
> Carl Storrs
> carl-storrs at usa.net
> 861-3100 x 5517
> 
> 


  • Prev by Date: Importing external grid file to do contour plot
  • Next by Date: Re: (a-b)/(c-d)=!=(b-a)/(d-c) ???
  • Previous by thread: Re: Importing images
  • Next by thread: Re: Importing images