T O P I C R E V I E W |
AndyColmes |
Posted - Apr 27 2012 : 07:41:17 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. |
8 L A T E S T R E P L I E S (Newest First) |
AndyColmes |
Posted - Apr 30 2012 : 10:16:33 ImageEn is probably the best single component I have ever seen and work with. Thanks a million Fabrizio. ImageEn never ceases to amaze me. |
fab |
Posted - Apr 30 2012 : 03:39:23 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(); |
AndyColmes |
Posted - Apr 29 2012 : 17:46:09 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;
|
AndyColmes |
Posted - Apr 29 2012 : 17:33:18 The code works incredibly well. Thanks to you both, Fabrizio and William. |
w2m |
Posted - Apr 29 2012 : 09:08:37 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 |
fab |
Posted - Apr 29 2012 : 01:04:03 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.
|
AndyColmes |
Posted - Apr 27 2012 : 12:24:37 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. |
w2m |
Posted - Apr 27 2012 : 10:08:41 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 |
|
|