ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Raw Grayscale 256-color
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

AndyColmes

USA
351 Posts

Posted - Apr 27 2012 :  07:41:17  Show Profile  Reply
What is the best way to convert an image to a raw 256-color grayscale?

I need to convert files to match the same format. I tried using the following to create the grayscale image but the other program complains that it not:

ImageEnView1.IO.Params.BitsPerSample:=8;
ImageEnView1.IO.Params.SamplesPerPixel:=1;
ImageEnView1.IO.Params.SaveToFile('alfa.bmp');

The other program reads the file as follows:

var
Y: Longint;
SizeX: Longint;
SizeY: Longint;
RetCode: Longint;
Buffer: PAnsiChar;
fBmp: Integer;
begin

Buffer := nil;
try
fBmp := FileOpen(FileName, fmOpenRead);
if (fBmp = -1) then
begin
WriteLn('Unable to read from ', FileName);
Exit;
end;

try
FileSeek(fBmp, 18, 0);
FileRead(fBmp, SizeX, SizeOf(SizeX));
FileRead(fBmp, SizeY, SizeOf(SizeY));

while ((SizeX and 3) <> 0) do
begin
Inc(SizeX);
end;

if (FileSeek(fBmp, 0, 2) <> (54 + 1024 + (SizeX * SizeY))) then
begin
WriteLn(FileName, ' is not a 256-color grayscale bitmap');
Exit;
end;

finally
FileClose(fBmp);
end;
finally
FreeMem(Buffer);
end;


22.4 KBas the one attached.

Thanks.

w2m

USA
1990 Posts

Posted - Apr 27 2012 :  10:08:41  Show Profile  Reply
Try this:

ImageENView.IO.Params.BitsPerSample := 8;
ImageENView.IO.Params.SamplesPerPixel := 1;
ImageENView.Proc.ConvertToGray;
ImageENView.Proc.ConvertTo ( 256 );
ImageENView.Update;
ImageENView.IO.SaveToFile ( FFilePath );


I am not sure if all of this is needed (you may not need ConvertTo Gray unless you are using color images) but I get a 22.0 KB size with size on disk of 24 KB.

William Miller
Go to Top of Page

AndyColmes

USA
351 Posts

Posted - Apr 27 2012 :  12:24:37  Show Profile  Reply
Unfortunately, it didn't work but thanks for jumping in. I think I had tried that previously but to no avail. You can try to copy the code from above that uses the FileSeek and see if you could convert an image that is acceptable to it.
Go to Top of Page

fab

1310 Posts

Posted - Apr 29 2012 :  01:04:03  Show Profile  Reply
I tested your code with the output of following:

imageenview1.Proc.ConvertToGray();
imageenview1.IO.Params.BitsPerSample := 8;
imageenview1.IO.Params.SamplesPerPixel := 1;
imageenview1.IO.SaveToFile('test.bmp');

It just works.
Go to Top of Page

w2m

USA
1990 Posts

Posted - Apr 29 2012 :  09:08:37  Show Profile  Reply
I get the correct results here as well...
Maybe the FileSeek formula is the problem and not ImageEN at all:

if (FileSeek(fBmp, 0, 2) <> (54 + 1024 + (SizeX * SizeY))) then
 begin
 WriteLn(FileName, ' is not a 256-color grayscale bitmap');
 Exit;
 end;

What is the bitdepth of the images that works with your program that uses the FileSeek formula? Unfortunately, I am not able to help with the formula.



William Miller
Go to Top of Page

AndyColmes

USA
351 Posts

Posted - Apr 29 2012 :  17:33:18  Show Profile  Reply
The code works incredibly well. Thanks to you both, Fabrizio and William.
Go to Top of Page

AndyColmes

USA
351 Posts

Posted - Apr 29 2012 :  17:46:09  Show Profile  Reply
Using the other parts of the code shown here, how would one determine what the 'Buffer' is if the 'Buffer' is a binary type? It is supposed to be an image and I would like to 'extract' that and display the image using ImageEn. According to the author, I am supposed to be able to detect that if the first 2 bytes of the 'Buffer' are hex FF D8, then it is a JPEG. How would I do this in Delphi and maybe save that 'Buffer' to disk as an image file for display?

Thanks in advance.

Andy


try
GetMem(Buffer, SizeX * SizeY);
except
WriteLn('Unable to allocate ', SizeX * SizeY, ' bytes of memory');
end;

FileSeek(fBmp, 54 + 1024, 0);
for Y := 0 to SizeY-1 do
begin
FileRead(fBmp, Buffer[(SizeY - 1 - Y) * SizeX], SizeX);
end;
finally
FileClose(fBmp);
end;

//function from an external DLL
RetCode := ReadMyInfo(PByte(Buffer), SizeX, SizeY);
if (RetCode > 0) then
begin
WriteLn(' Only String data was ', StrLen(Buffer), ' characters: ', Buffer);
if (RetCode > (StrLen(Buffer) + 1)) then
begin
WriteLn(' Binary data was ', RetCode - (StrLen(Buffer) + 1), ' bytes');
//Need to be able to detect if the Binary data is JPEG - if first 2 bytes
//is hex FF D8 and if yes, display that with ImageEn
end;
end;
finally
FreeMem(Buffer);
end;
Go to Top of Page

fab

1310 Posts

Posted - Apr 30 2012 :  03:39:23  Show Profile  Reply
I can tell you how load an image from buffer with ImageEn:

ImageEnView.IO.LoadFromBuffer(Buffer, BufferSizeInBytes);

It auto-detects the image type (jpeg, bmp, etc..) and then loads it.
There is also ParamsFromBuffer, that could help you to know the file type without loading the image.

Also, inside hyieutils unit there is the undocumented class TIEMemStream. It gets a buffer and its length, and allows you to save that buffer to file. For example:

var memstream:TIEMemStream;
...
memstream := TIEMemStream.Create(Buffer, BufferLen);
memstream.SaveToFile('output.dat');
memstream.Free();
Go to Top of Page

AndyColmes

USA
351 Posts

Posted - Apr 30 2012 :  10:16:33  Show Profile  Reply
ImageEn is probably the best single component I have ever seen and work with. Thanks a million Fabrizio. ImageEn never ceases to amaze me.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: