bitmap compression with ICCompress or equivalent

OpenSourceDeveloper
OpenSourceDeveloper used Ask the Experts™
on
I am trying to compress a bitmap with ICCompress to jpeg but I just can't figure out how to do it and there is little coverage of how to do this. this is what I am trying so far, it should give you an idea of what I want to do. I have the VfW.pas headers by JEDI.

var
  Compressor: HIC;
  InBIH, OutBIH: PBITMAPINFOHEADER;
  InData, OutData: pchar;
  InFileSize, InFile, OutFile, Bytes: dword;
begin
  Infile := CreateFile('c:\file.bmp', GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  Outfile := CreateFile('d:\file.jpg', GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  InFileSize := GetFileSize(InFile, nil);
  GetMem(InData, InFileSize);
  GetMem(OutData, InFileSize);
  ReadFile(InFile, InData^, InFileSize, Bytes, nil);
  Compressor := ICOpen(ICTYPE_VIDEO, mmioFOURCC('L', 'J','4','4'), ICMODE_COMPRESS);
  ICCompress(Compressor, 0, OutBIH, OutData, InBIH, InData, 0, 0, 0, 0, 1000, InBIH, nil);
  WriteFile(OutFile, OutData^, InFileSize, Bytes, nil);
  CloseHandle(InFile);
  CloseHandle(OutFile);
  FreeMem(InData);
  FreeMem(OutData);
end.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I don't know if it is any use, but if you just include the Jpeg unit in delphi you can do the following to save a Jpeg.  


procedure SaveToJpeg(ABitmap : TBitmap);
var
  aJpg : TJpegImage
begin
 aJpg := TJpegImage.create;
 aJpg.assign(abitmap);
 aJpg.savetofile('x.jpg');
end;

Is this an equivalent to what you want ?

Author

Commented:
nope sorry, I already know how to do that. I really need to use ICCompress or some other built-in windows api. the delphi jpeg unit adds way too much file size. thanks though.
I played with the Jpeg some time ago, I can tell you that a Jpeg is not just a Compression of a Bitmap, as you have done in your code. First, the JPEG header is not compressed, and it is different than a bitmap header, the image pixel data for a jpeg is also different than the image pixel data for a bitmap (the image pixel data for a jpeg is in a compressed form, but uses it's own arrangement (structure), as I remember, there are some pages with the JFIF (jpeg) info for that format


http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/JPEG.txt

http://www.prepressure.com/techno/compressionjpeg.htm

http://bitscap.bits-pilani.ac.in/faculty-homep/sanjay/jpeg_file_interchange_format%20(not%20properly%20formatted).htm

and many others
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
well it actually doesn't matter if it is in viewable format I just need it compressed for transfering across the internet. once it is received at the other end it can be decompressed back to bmp for viewing.

all I need is for it to be compressed and then decompressed using some sort of native windows api so I don't have to add any extra libraries. guess I should have made it more clear that it being in a compressed viewable format doesn't matter as long as it can be reversed.

thanks for your time.
doing commpression without librarys is not easy, There is the zlib in delphi, which I have used for compression, and works pretty well. You can also use the windows system cabinet.dll to do commpression, and the Lempel-Ziv commpression of the system LZEXPAND.DLL. I had some code for the cabinet.dll, some where, but I can not find it, and don't have much time to search for it. You might do a search here at EE for cabinet compression. You can look at the API help for index "Cabinet Files". You can look at the API help in index "Data Decompression Library" for the use of the LZ compression. Oh, and if I'm not misteaken, the ICOpen is just for video files, maybe that's why it don't work. You will need some commpression library, if you don't want to add one to your app, the system cabinet.dll can give you good compression. If I find the code, I'll post it
I found the cabinet code but it uses a Cabinet component, with a Cabinet definitions file (from an C code .h file) it only does Compression. To do decompression it uses the SetUpApi.pas (from the C code SetUpApi.h I think). There's quite a bit of code to the SetupApi and the Cabinet Component, more than I could post here. ( but it only ass 11 Kb to the app). I might could zip them and you could download them. but you may be better off going to Torry's or other Component place and get a more recent version of a Cabinet.dll use component.

Commented:
How about using Portable Network Graphics (.PNG)?

I started to replace all my .BMP's with .PNG recently, and the size advantage is considerable, of course. For non-photo material, PNG is superiour to everything else.

There's a nice Open Source PNG component available at pngdelphi.sourceforge.net. Plug'n'go.

OpenSourceDeveloper:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial