Solved

Reading values from file on disc into array

Posted on 2004-10-08
4
181 Views
Last Modified: 2010-04-05
I have a binary file with a header block followed by a stream of 2-byte values. These need to be read into a two dimensional array of x and y values which are to be converted into a grey scale bitmap.  Currently I am using the  BlockRead procedure but this is very slow.  Can somebody point me to a better method. Please note that I am only a novice at delphi programming - but learning fast.  
0
Comment
Question by:barfbagger
4 Comments
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12257107
there is no reason for blockread to be slow.

type
header = record
....
end;

arrayItem = record
....
end;

myArray = array [0..1] of arrayItem;

var
  f:file; h:header;
  p: ^myArray;
 

assignfile(f, filename);
reset(f, 1);
blockread(f, h, sizeof(header));

// now you know size of your array , lets say is arrSize
GetMem(p, arrSize * sizeof(arrayItem));
blockread(f, p^, arrSize * sizeof(arrayItem));
CloseFile(f);
// now you have in p reference to your array for processing
0
 

Author Comment

by:barfbagger
ID: 12257162
Thanks,  I'll try this.
0
 
LVL 1

Accepted Solution

by:
Bart_Thomas earned 50 total points
ID: 12260494
Normally speaking, you read imagedata into a 1 dimensional array.
Since the width and height are stored in the header, converting
into 2 dimensions isn't that difficult.

type
  TImageHeader = record
                   Width,Height: Cardinal;
                 end;

var
  F: TFileStream;
  ImageHeader: TImageHeader;
  ImageData: array of word;
  i,x,y,DataLength: Cardinal;
begin
  F := TFileStream.Create ('FileName.Ext', fmOpenRead);
  try
    F.Position := 0;
    F.Read(ImageHeader, SizeOf (TImageHeader));
    DataLength := (F.Size - F.Position) div SizeOf (WORD);
    SetLength (ImageData, DataLength);
    try
      F.Read (ImageData[0], DataLength);

      i := 0;
      y := 0;
      while y < ImageHeader.Height do
      begin
        x := 0;
        while x < ImageHeader.Width do
        begin
          // ImageData[i] is greyvalue at x,y

          inc (i);
          inc (x);
        end;
        inc (y);
      end;
    finally
      SetLength (ImageData, 0);
    end;
  finally
    F.Free;
  end;
end;
0
 

Assisted Solution

by:joncmora
joncmora earned 50 total points
ID: 12274451
I think the reason for BlockRead being slow is that you're using a very small buffer. Instead of reading 2 bytes at a time after reading the header, why not get 4KB or 8KB or higher for each BlockRead. Then loop through the buffer for your X,Y pair.

const
  HeaderSize = 512; {your header size here}

type
  TBuffer = array[1..8192] of Byte; {buffer size, experiment other sizes to get the fastest BlockRead performance}

procedure ReadMyImage;
var
  MyBufer: TBuffer;
  BytesRead: Integer;
  I: Integer;
  X, Y: Byte;
begin
  ...
  BlockRead(F, MyBuffer, SizeOf(MyBuffer), BytesRead);
  for I := 1 to (BytesRead div 2) do
  begin
    X := Buffer[(I*2) - 1];
    Y := Buffer[(I*2)];
    DoSomethingWithyourXY(X, Y);
  end;
  ...
end;



Or you can use TFileStream instead...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now