?
Solved

Reading values from file on disc into array

Posted on 2004-10-08
4
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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 150 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

752 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