Solved

Reading values from file on disc into array

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
QRReport  TQrmemo vertical stretching 1 53
Delphi 2 69
Reconfigure Delphi Install? 2 61
how to make 2 step back? ..\..\ ExtractFilePath (paramstr(0)) 5 52
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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