Solved

Reading values from file on disc into array

Posted on 2004-10-08
4
186 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tidtcpserver connection lost handle 2 123
Dynamically Created Query 3 78
Sending files from  idTcpServer Socket to idTcpClient 2 63
Twebbrowser add css to the header 3 53
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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