milance
asked on
Reading BMP and PCX
Can someone send me a code in Turbo PASCAL 6.0 for reading and drawing pictures in BMP and PCX format, with resolution 640X480, and 256 colour?
I've actually have loaders for 320x200x256 with sources. I will see if I can find something in your desired mode ;)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
{ This is my first answer to Experts-Exchange(I'm New)
By Raid
BMP File Loader: (256 color)
There is 3 parts to a BMP.
1st Part: Header
2nd Part: Color Declaration
3rd Part: Graphic }
Uses
Crt, graph;
Type
ColorType = Record
Blue: Byte;
Green: Byte;
Red: Byte;
Reserved: Byte;
End;
Procedure LoadBMP(FileName: String);
Var
Color: Byte;
LoopVar: Byte;
ReadVar: Byte;
BitsPerPlane: Word;
BitsPerPixel: Word;
LoopX: Word;
LoopY: Word;
BMPSizeX: Longint;
BMPSizeY: Longint;
BitCompression: Longint;
ColorsUsed: Longint;
ColorsImportant: Longint;
ImageSize: Longint;
ResX: Longint;
ResY: Longint;
Colors: array[0 .. 255] of ColorType;
FileVar: File;
Begin
assign(FileVar, FileName); reset(FileVar, 1); { Open BMP }
{ Now, lets load the header(1st Part) }
{ The first 18 bytes in the file }
{ have nothing to do with the graphic }
for LoopVar := 1 to 18 do blockread(FileVar, ReadVar, 1);
{ Read The next 4 Bytes(Longint) which is the width of the BMP }
blockread(FileVar, BMPSizeX, 4);
{ Read The next 4 Bytes(Longint) which is the length of the BMP }
blockread(FileVar, BMPSizeY, 4);
{ Read The next 2 Bytes(Word) for the number of Bits per plane }
blockread(FileVar, BitsPerPlane, 2);
{ Read The next 2 Bytes(Word) for the number of Bits per pixel }
{ Bits per pixel example }
{ Lets say the value is 8, that means 8 Bits per pixel, in other }
{ words that means 256 Colors }
{ 8-Bits is a Byte(256 Values) }
{ 16-Bits is a Word(65536 Values) }
{ 24-Bits is a Lonting(-2 Million to 2 Million, Roughly) }
blockread(FileVar, BitsPerPixel, 2);
{ Next, we read the bit compression Value, which is 4 Bytes(Longint) }
{ Don't worry about this, because most BMPS don't have bit compression }
blockread(FileVar, BitCompression, 4);
{ The next 4 Bytes(Longint) is the size of the BMP Graphic }
{ Size of Graphic should be BMPSizeX * BMPSizeY }
blockread(FileVar, ImageSize, 4);
{ The next 4 Bytes(Longint) is the resolution width }
{ Don't worry about this, because it has to do with windows }
{ I presume. }
blockread(FileVar, ResX, 4);
{ And the next 4 Bytes(Longint) is the resolution length }
{ also don't worry about it }
blockread(FileVar, ResY, 4);
{ The next 4 Bytes(Longint) is how many colors there are used }
{ in the BMP. }
blockread(FileVar, ColorsUsed, 4);
{ And the Next 4 Bytes(Longint) is how many colors are important }
{ This is the value that you are looking for of how many colors }
{ There are in the BMP }
blockread(FileVar, ColorsImportant, 4);
{ -Phew- done loading the Header. Now lets go on to loading the colors }
{ which is the 2nd Part }
{ The colors are saved like this: }
{ Color 0: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ Color 1: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ Color 2: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ etc. }
{ Load Colors into Variable }
for LoopVar := 0 to ColorsImportant - 1 do begin
blockread(FileVar, Colors[LoopVar], SizeOf(ColorType));
end;
{ To Palette the Colors Quickly, do this }
port[$3c8] := 0; { Start at color 0 }
for LoopVar := 0 to ColorsImportant - 1 do begin
port[$3c9] := Colors[LoopVar].Red;
port[$3c9] := Colors[LoopVar].Green;
port[$3c9] := Colors[LoopVar].Blue;
end;
{ or you could set the palette using SetRGBPalette }
{ eep! }
{ Then, (Finally) we load the 3rd part, which is the graphic }
{ The graphic is saved funny, I don't know why the people who }
{ designed the BMP saved from left-right, down-up. Instead of }
{ left-right, up-down. I guess they wanted to be different }
{ Ok, lets draw it }
for LoopY := BMPSizeY downto 1 do begin
for LoopX := 1 to BMPSizeX do begin
blockread(FileVar, Color, 1);
putpixel(LoopX - 1, LoopY - 1, Color);
end;
end;
close(FileVar); { Close BMP }
{ If you have any questions, contact RAID }
End;
By Raid
BMP File Loader: (256 color)
There is 3 parts to a BMP.
1st Part: Header
2nd Part: Color Declaration
3rd Part: Graphic }
Uses
Crt, graph;
Type
ColorType = Record
Blue: Byte;
Green: Byte;
Red: Byte;
Reserved: Byte;
End;
Procedure LoadBMP(FileName: String);
Var
Color: Byte;
LoopVar: Byte;
ReadVar: Byte;
BitsPerPlane: Word;
BitsPerPixel: Word;
LoopX: Word;
LoopY: Word;
BMPSizeX: Longint;
BMPSizeY: Longint;
BitCompression: Longint;
ColorsUsed: Longint;
ColorsImportant: Longint;
ImageSize: Longint;
ResX: Longint;
ResY: Longint;
Colors: array[0 .. 255] of ColorType;
FileVar: File;
Begin
assign(FileVar, FileName); reset(FileVar, 1); { Open BMP }
{ Now, lets load the header(1st Part) }
{ The first 18 bytes in the file }
{ have nothing to do with the graphic }
for LoopVar := 1 to 18 do blockread(FileVar, ReadVar, 1);
{ Read The next 4 Bytes(Longint) which is the width of the BMP }
blockread(FileVar, BMPSizeX, 4);
{ Read The next 4 Bytes(Longint) which is the length of the BMP }
blockread(FileVar, BMPSizeY, 4);
{ Read The next 2 Bytes(Word) for the number of Bits per plane }
blockread(FileVar, BitsPerPlane, 2);
{ Read The next 2 Bytes(Word) for the number of Bits per pixel }
{ Bits per pixel example }
{ Lets say the value is 8, that means 8 Bits per pixel, in other }
{ words that means 256 Colors }
{ 8-Bits is a Byte(256 Values) }
{ 16-Bits is a Word(65536 Values) }
{ 24-Bits is a Lonting(-2 Million to 2 Million, Roughly) }
blockread(FileVar, BitsPerPixel, 2);
{ Next, we read the bit compression Value, which is 4 Bytes(Longint) }
{ Don't worry about this, because most BMPS don't have bit compression }
blockread(FileVar, BitCompression, 4);
{ The next 4 Bytes(Longint) is the size of the BMP Graphic }
{ Size of Graphic should be BMPSizeX * BMPSizeY }
blockread(FileVar, ImageSize, 4);
{ The next 4 Bytes(Longint) is the resolution width }
{ Don't worry about this, because it has to do with windows }
{ I presume. }
blockread(FileVar, ResX, 4);
{ And the next 4 Bytes(Longint) is the resolution length }
{ also don't worry about it }
blockread(FileVar, ResY, 4);
{ The next 4 Bytes(Longint) is how many colors there are used }
{ in the BMP. }
blockread(FileVar, ColorsUsed, 4);
{ And the Next 4 Bytes(Longint) is how many colors are important }
{ This is the value that you are looking for of how many colors }
{ There are in the BMP }
blockread(FileVar, ColorsImportant, 4);
{ -Phew- done loading the Header. Now lets go on to loading the colors }
{ which is the 2nd Part }
{ The colors are saved like this: }
{ Color 0: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ Color 1: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ Color 2: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ etc. }
{ Load Colors into Variable }
for LoopVar := 0 to ColorsImportant - 1 do begin
blockread(FileVar, Colors[LoopVar], SizeOf(ColorType));
end;
{ To Palette the Colors Quickly, do this }
port[$3c8] := 0; { Start at color 0 }
for LoopVar := 0 to ColorsImportant - 1 do begin
port[$3c9] := Colors[LoopVar].Red;
port[$3c9] := Colors[LoopVar].Green;
port[$3c9] := Colors[LoopVar].Blue;
end;
{ or you could set the palette using SetRGBPalette }
{ eep! }
{ Then, (Finally) we load the 3rd part, which is the graphic }
{ The graphic is saved funny, I don't know why the people who }
{ designed the BMP saved from left-right, down-up. Instead of }
{ left-right, up-down. I guess they wanted to be different }
{ Ok, lets draw it }
for LoopY := BMPSizeY downto 1 do begin
for LoopX := 1 to BMPSizeX do begin
blockread(FileVar, Color, 1);
putpixel(LoopX - 1, LoopY - 1, Color);
end;
end;
close(FileVar); { Close BMP }
{ If you have any questions, contact RAID }
End;
{ Sorry Miliance, The one above was not tested
and it does not work }
{ So here is the working version }
{ You will have to set the screen mode yourself though }
{ If you want a program and a driver of 256 colors (.BGI) }
{ I can give you one. }
{BMP File Loader: (256 color)
There is 3 parts to a BMP.
1st Part: Header
2nd Part: Color Declaration
3rd Part: Graphic }
Uses
Crt, graph;
Type
ColorType = Record
Blue: Byte;
Green: Byte;
Red: Byte;
Reserved: Byte;
End;
Procedure LoadBMP(FileName: String);
Var
Color: Byte;
LoopVar: Word;
ReadVar: Byte;
BitsPerPlane: Word;
BitsPerPixel: Word;
LoopX: Word;
LoopY: Word;
BMPSizeX: Longint;
BMPSizeY: Longint;
BitCompression: Longint;
ColorsUsed: Longint;
ColorsImportant: Longint;
ImageSize: Longint;
ResX: Longint;
ResY: Longint;
Colors: array[0 .. 255] of ColorType;
FileVar: File;
Begin
assign(FileVar, FileName); reset(FileVar, 1); { Open BMP }
{ Now, lets load the header(1st Part) }
{ The first 18 bytes in the file }
{ have nothing to do with the graphic }
for LoopVar := 1 to 18 do blockread(FileVar, ReadVar, 1);
{ Read The next 4 Bytes(Longint) which is the width of the BMP }
blockread(FileVar, BMPSizeX, 4);
{ Read The next 4 Bytes(Longint) which is the length of the BMP }
blockread(FileVar, BMPSizeY, 4);
{ Read The next 2 Bytes(Word) for the number of Bits per plane }
blockread(FileVar, BitsPerPlane, 2);
{ Read The next 2 Bytes(Word) for the number of Bits per pixel }
{ Bits per pixel example }
{ Lets say the value is 8, that means 8 Bits per pixel, in other }
{ words that means 256 Colors }
{ 8-Bits is a Byte(256 Values) }
{ 16-Bits is a Word(65536 Values) }
{ 24-Bits is a Lonting(-2 Million to 2 Million, Roughly) }
blockread(FileVar, BitsPerPixel, 2);
{ Next, we read the bit compression Value, which is 4 Bytes(Longint) }
{ Don't worry about this, because most BMPS don't have bit compression }
blockread(FileVar, BitCompression, 4);
{ The next 4 Bytes(Longint) is the size of the BMP Graphic }
{ Size of Graphic should be BMPSizeX * BMPSizeY }
blockread(FileVar, ImageSize, 4);
{ The next 4 Bytes(Longint) is the resolution width }
{ Don't worry about this, because it has to do with windows }
{ I presume. }
blockread(FileVar, ResX, 4);
{ And the next 4 Bytes(Longint) is the resolution length }
{ also don't worry about it }
blockread(FileVar, ResY, 4);
{ The next 4 Bytes(Longint) is how many colors there are used }
{ in the BMP. }
blockread(FileVar, ColorsUsed, 4);
{ And the Next 4 Bytes(Longint) is how many colors are important }
{ This is the value that you are looking for of how many colors }
{ There are in the BMP }
blockread(FileVar, ColorsImportant, 4);
{ -Phew- done loading the Header. Now lets go on to loading the colors }
{ which is the 2nd Part }
{ The colors are saved like this: }
{ Color 0: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ Color 1: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ Color 2: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ etc. }
{ Load Colors into Variable }
if ColorsImportant = 0 then ColorsImportant := 256;
for LoopVar := 0 to ColorsImportant - 1 do begin
blockread(FileVar, Colors[LoopVar], SizeOf(ColorType));
end;
{ To Palette the Colors Quickly, do this }
port[$3c8] := 0; { Start at color 0 }
for LoopVar := 0 to ColorsImportant - 1 do begin
port[$3c9] := Colors[LoopVar].Red div 4;
port[$3c9] := Colors[LoopVar].Green div 4;
port[$3c9] := Colors[LoopVar].Blue div 4;
end;
{ or you could set the palette using SetRGBPalette }
{ eep! }
{ Then, (Finally) we load the 3rd part, which is the graphic }
{ The graphic is saved funny, I don't know why the people who }
{ designed the BMP saved from left-right, down-up. Instead of }
{ left-right, up-down. I guess they wanted to be different }
{ Ok, lets draw it }
for LoopY := BMPSizeY downto 1 do begin
for LoopX := 1 to BMPSizeX do begin
blockread(FileVar, Color, 1);
putpixel(LoopX - 1, LoopY - 1, Color);
end;
end;
close(FileVar); { Close BMP }
{ If you have any questions, contact RAID }
End;
Begin
{ Example }
{ LoadBMP('C:\WINDOWS\CLOUDS .BMP'); }
End.
and it does not work }
{ So here is the working version }
{ You will have to set the screen mode yourself though }
{ If you want a program and a driver of 256 colors (.BGI) }
{ I can give you one. }
{BMP File Loader: (256 color)
There is 3 parts to a BMP.
1st Part: Header
2nd Part: Color Declaration
3rd Part: Graphic }
Uses
Crt, graph;
Type
ColorType = Record
Blue: Byte;
Green: Byte;
Red: Byte;
Reserved: Byte;
End;
Procedure LoadBMP(FileName: String);
Var
Color: Byte;
LoopVar: Word;
ReadVar: Byte;
BitsPerPlane: Word;
BitsPerPixel: Word;
LoopX: Word;
LoopY: Word;
BMPSizeX: Longint;
BMPSizeY: Longint;
BitCompression: Longint;
ColorsUsed: Longint;
ColorsImportant: Longint;
ImageSize: Longint;
ResX: Longint;
ResY: Longint;
Colors: array[0 .. 255] of ColorType;
FileVar: File;
Begin
assign(FileVar, FileName); reset(FileVar, 1); { Open BMP }
{ Now, lets load the header(1st Part) }
{ The first 18 bytes in the file }
{ have nothing to do with the graphic }
for LoopVar := 1 to 18 do blockread(FileVar, ReadVar, 1);
{ Read The next 4 Bytes(Longint) which is the width of the BMP }
blockread(FileVar, BMPSizeX, 4);
{ Read The next 4 Bytes(Longint) which is the length of the BMP }
blockread(FileVar, BMPSizeY, 4);
{ Read The next 2 Bytes(Word) for the number of Bits per plane }
blockread(FileVar, BitsPerPlane, 2);
{ Read The next 2 Bytes(Word) for the number of Bits per pixel }
{ Bits per pixel example }
{ Lets say the value is 8, that means 8 Bits per pixel, in other }
{ words that means 256 Colors }
{ 8-Bits is a Byte(256 Values) }
{ 16-Bits is a Word(65536 Values) }
{ 24-Bits is a Lonting(-2 Million to 2 Million, Roughly) }
blockread(FileVar, BitsPerPixel, 2);
{ Next, we read the bit compression Value, which is 4 Bytes(Longint) }
{ Don't worry about this, because most BMPS don't have bit compression }
blockread(FileVar, BitCompression, 4);
{ The next 4 Bytes(Longint) is the size of the BMP Graphic }
{ Size of Graphic should be BMPSizeX * BMPSizeY }
blockread(FileVar, ImageSize, 4);
{ The next 4 Bytes(Longint) is the resolution width }
{ Don't worry about this, because it has to do with windows }
{ I presume. }
blockread(FileVar, ResX, 4);
{ And the next 4 Bytes(Longint) is the resolution length }
{ also don't worry about it }
blockread(FileVar, ResY, 4);
{ The next 4 Bytes(Longint) is how many colors there are used }
{ in the BMP. }
blockread(FileVar, ColorsUsed, 4);
{ And the Next 4 Bytes(Longint) is how many colors are important }
{ This is the value that you are looking for of how many colors }
{ There are in the BMP }
blockread(FileVar, ColorsImportant, 4);
{ -Phew- done loading the Header. Now lets go on to loading the colors }
{ which is the 2nd Part }
{ The colors are saved like this: }
{ Color 0: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ Color 1: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ Color 2: }
{ Blue(Byte) }
{ Green(Byte) }
{ Red(Byte) }
{ Reserved(Byte) }
{ etc. }
{ Load Colors into Variable }
if ColorsImportant = 0 then ColorsImportant := 256;
for LoopVar := 0 to ColorsImportant - 1 do begin
blockread(FileVar, Colors[LoopVar], SizeOf(ColorType));
end;
{ To Palette the Colors Quickly, do this }
port[$3c8] := 0; { Start at color 0 }
for LoopVar := 0 to ColorsImportant - 1 do begin
port[$3c9] := Colors[LoopVar].Red div 4;
port[$3c9] := Colors[LoopVar].Green div 4;
port[$3c9] := Colors[LoopVar].Blue div 4;
end;
{ or you could set the palette using SetRGBPalette }
{ eep! }
{ Then, (Finally) we load the 3rd part, which is the graphic }
{ The graphic is saved funny, I don't know why the people who }
{ designed the BMP saved from left-right, down-up. Instead of }
{ left-right, up-down. I guess they wanted to be different }
{ Ok, lets draw it }
for LoopY := BMPSizeY downto 1 do begin
for LoopX := 1 to BMPSizeX do begin
blockread(FileVar, Color, 1);
putpixel(LoopX - 1, LoopY - 1, Color);
end;
end;
close(FileVar); { Close BMP }
{ If you have any questions, contact RAID }
End;
Begin
{ Example }
{ LoadBMP('C:\WINDOWS\CLOUDS
End.