Exporting BMP graphics
- To: mathgroup@smc.vnet.net
- Subject: [mg12187] Exporting BMP graphics
- From: "Barthelet, Luc" <lucb@ea.com>
- Date: Fri, 1 May 1998 03:08:39 -0400
I did not find a way to save BMP directly using Display. As I posted some code to read BMPs a while back, here is some code to write BMPs. It uses display to do most of the work and then changes the header to match what a BMP needs. Just copy what is under the line here and paste it. There is an example on how to use it on the last line. ------------------- \!\(\* RowBox[{ RowBox[{\(WriteBMPFile[filename_, g_]\), ":=", RowBox[{"Module", "[", "\n", "\t\t", RowBox[{ \({bigString, endOfText, startString, fakeFile, \ dataID, dataWidth, dataHeight, dataPixelSize, \n\t\t\theader, awriteFile, \n\t\t\t\n \t\t}\), ",", "\n", RowBox[{ \(bigString\ = \ DisplayString[g, "\<PBM\>"]\), ";", "\n", \(endOfText\ = \ \(Position[ToCharacterCode[\ StringTake[bigString, 100]], 10] \)[[3, 1]]\), ";", "\n", \(startString\ = StringTake[bigString, endOfText]\), ";", "\n", \(fakeFile\ = \ StringToStream[startString]\), ";", "\n", \({dataID, dataWidth, dataHeight, dataPixelSize}\ \ = \ Read[fakeFile, {Word, Number, Number, Number}]\), ";", "\n", \(Close[fakeFile]\), ";", "\n", \(to2Bytes[n_] := \ {Mod[n, 256], Mod[\((n - \ Mod[n, 256])\)/256, 256]}\), ";", "\n", \(to4Bytes[n_]\ := \ Join[\ to2Bytes[Mod[n, 256\ 256]], \ to2Bytes[ Mod[\((\ n - \ Mod[n, 256\ 256])\)/\((256\ 256)\), 256\ 256]]]\), ";", "\n", \(header\ = \ Join[{66, 77}, \ (*\ ID\ *) \n\t\t to4Bytes[\ dataWidth\ dataHeight\ 3\ + \ 54\ ], \ (*\ data\ Length\ *) \n\t\tto4Bytes[0], \ (*\ ?? ?\ *) \n \t\tto4Bytes[54], \ (*\ firstImageByte\ *) \n\t\t to4Bytes[40], \ (*\ ?? ?\ *) \n\t\tto4Bytes[dataWidth], \ (*\ dataWidth\ *) \n\t\tto4Bytes[dataHeight], \ (*\ dataHeight\ *) \n\t\tto2Bytes[1], \ (*\ Planes\ *) \n \t\tto2Bytes[24], \ (*\ bitsPerPixels\ *) \n\t\tto4Bytes[0], \ (*\ ?? ?\ *) \n\t\tto4Bytes[dataWidth\ dataHeight\ 3], \ (*\ sizeOfBitmap\ *) \n\t\tto4Bytes[2834], \ (*\ horzRes\ pixels\ per\ meter*) \n\t\tto4Bytes[2834], \ (*\ vertRes\ pixels\ per\ meter*) \n\t\tto4Bytes[0], \ (*\ numOfColors\ *) \n\t\t to4Bytes[0]\ (*\ colorsImportant\ *) \n\t\t]\), ";", "\n", RowBox[{"awriteFile", " ", "=", " ", RowBox[{ "OpenWrite", "[", \(filename, DOSTextFormat -> False\), StyleBox["]", FontColor->RGBColor[0, 0, 1]]}]}], ";", "\n", \(WriteString[awriteFile, \n\t StringJoin[FromCharacterCode[header], \n\t\t FromCharacterCode[ Flatten[\n\t\t Reverse[\ Map[Reverse, \n\t Partition[\t Partition[\ ToCharacterCode[\ StringDrop[bigString, endOfText]\ ]\ , 3], dataWidth]\n\t\t\t\t, {2}]\n\t\t\t]\n\t]\n\t]\n\t\t] \n]\), ";", "\n", \(Close[awriteFile]\), ";"}]}], "\n", "]"}]}], ";", "\n", \(WriteBMPFile["\<C:\\Windows\\desktop\\test.bmp\>", Plot3D[Sin[x\ y], {x, 0, \(3 Pi\)\/2}, {y, 0, \(3 Pi\)\/2}, PlotPoints -> 30, LightSources \[Rule] {{{1.3, \(-2.4\), 2. }, RGBColor[1, 0, 0]}, {{2.7, 0. , 2. }, RGBColor[0, 1, 0]}, {{\(-2.3\), \(-1.4\), 2. }, RGBColor[0, 0, 1]}}, ImageSize -> {320, 240}]]\), ";"}]\)