Recent Posts
Selamat datang di Coding Delphi Land Weblog kumpulan source code pemogram delphi
(Bukan maksud untuk menggurui tetapi marilah kita berbagi ilmu tuk perkembangan kemajuan teknologi kita
Minggu, 15 November 2009
Menyimpan Data Gambar menjadi Text (BMP To Text)
unit ScreenRGBToText;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtDlgs, ExtCtrls,jpeg;
type
TFormRGBValues = class(TForm)
ButtonReadBMP: TButton;
ButtonReadTXT: TButton;
Image: TImage;
procedure ButtonReadBMPClick(Sender: TObject);
procedure ButtonReadTXTClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormRGBValues: TFormRGBValues;
implementation
{$R *.DFM}
CONST
FileSignature = 'RGB:';
TYPE
TRGBTripleArray = ARRAY[WORD] OF TRGBTriple;
pRGBTripleArray = ^TRGBTripleArray;
PROCEDURE BitmapToRGBValues(CONST BMPFilename : STRING;
CONST DataFilename: STRING);
VAR
Bitmap : TBitmap;
DataFile: TextFile;
i : INTEGER;
j : INTEGER;
row : pRGBTripleArray;
BEGIN
Bitmap := TBitmap.Create;
TRY
Bitmap.LoadFromFile(BMPFilename);
Bitmap.PixelFormat := pf24bit; // in case not pf24bit
Assignfile(DataFile, DataFilename);
REWRITE(DataFile);
WRITELN(DataFile, FileSignature);
WRITELN(DataFile, Bitmap.Width, ' ', Bitmap.Height);
FOR j := 0 TO Bitmap.Height-1 DO
BEGIN
row := Bitmap.Scanline[j];
WRITE(DataFile, 'R ');
FOR i := 0 TO Bitmap.Width-1 DO
WRITE(DataFile, row[i].rgbtRed:4);
WRITELN(Datafile);
WRITE(DataFile, 'G ');
FOR i := 0 TO Bitmap.Width-1 DO
WRITE(DataFile, row[i].rgbtGreen:4);
WRITELN(DataFile);
WRITE(DataFile, 'B ');
FOR i := 0 TO Bitmap.Width-1 DO
WRITE(DataFile, row[i].rgbtBlue:4);
WRITELN(DataFile)
END;
CloseFile(DataFile)
FINALLY
Bitmap.Free
END
END {BitmapToRGBValues};
FUNCTION TxtToBitmap(CONST Filename: STRING): TBitmap;
VAR
DataFile: TextFile;
height : INTEGER;
i : INTEGER;
j : INTEGER;
row : pRGBTripleArray;
s : STRING;
tag : CHAR;
width : INTEGER;
BEGIN
RESULT := TBitmap.Create;
Assignfile(DataFile, Filename);
RESET(DataFile);
READLN(DataFile, s);
ASSERT(s = FileSignature, 'Wrong File Signature');
READLN(DataFile, Width, Height);
RESULT.Width := Width;
RESULT.Height := Height;
RESULT.PixelFormat := pf24bit;
FOR j := 0 TO RESULT.Height-1 DO
BEGIN
row := RESULT.Scanline[j];
READ(DataFile, Tag);
ASSERT(Tag = 'R');
FOR i := 0 TO RESULT.Width-1 DO
READ(DataFile, row[i].rgbtRed);
READLN(Datafile);
READ(DataFile, Tag);
ASSERT(Tag = 'G');
FOR i := 0 TO RESULT.Width-1 DO
READ(DataFile, row[i].rgbtGreen);
READLN(DataFile);
READ(DataFile, Tag);
ASSERT(Tag = 'B');
FOR i := 0 TO RESULT.Width-1 DO
READ(DataFile, row[i].rgbtBlue);
READLN(DataFile)
END;
CloseFile(DataFile)
END {TxtToBitmap};
// Read BMP and create Txt file
procedure TFormRGBValues.ButtonReadBMPClick(Sender: TObject);
begin
BitmapToRGBValues('usa.BMP', 'Pixels.Txt');
{ Memo1.Lines.LoadFromFile('Pixels.Txt');}
end;
procedure TFormRGBValues.ButtonReadTXTClick(Sender: TObject);
VAR
Bitmap: TBitmap;
begin
Bitmap := TxtToBitmap('Pixels.Txt');
TRY
Image.Picture.Graphic := Bitmap
FINALLY
Bitmap.Free
END
end;
end.
0 komentar:
Posting Komentar