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
 Resizing images
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

hrc

Denmark
6 Posts

Posted - Aug 14 2012 :  05:03:44  Show Profile  Reply
Been here before but never really succeeded in accomplishing the task. I'm a total NOB at image handling, not understanding alpha-levels, layers or the sparse collection of ununderstandable words in the help file. This is not my expertise! Bear with me.

My customers scan in Multipage TIFF and are allowed to tinkle with bit depth, and DPI themselves. Sometimes they end up with a scanning above 20 MB that they want to email. I need to catch that situation and offer them to optimize data.

First I find the size of data:

Size := 0;
for i := 0 to ImageEnMView.ImageCount - 1 do
  with ImageEnMView.MIO.Params[i] do
     Inc(Size,Height*Width*SamplesPerPixel);
AvgSize := Size div ImageEnMView.ImageCount;

if (Size > (OneMB shl 2)) or // larger than 4 MB
   (AvgSize > (OneMB shl 1)) or // or averging 2 MB per page
   (ImageEnMView.MIO.Params[0].DPIY > 150) then // or just too high DPI
  if MessageDlg(format('Data is %d MB. Optimize?',[AvgSize div OneMB]), mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
  // Tried several things here
end;


Things I have tried:

ImageEnMView.Proc.ConvertTo(CalcColorsUsed);
ImageEnMView.Proc.ConvertToGray;
ImageEnMView.Proc.ConvertToBWOrdered;


Also tried resampling using this altered copy of ImageEnView.ChangeResolution

var
  oldDPI: integer;
  newheight: integer;
begin
  oldDPI := aOldDPI_Y;
  newheight := round(aIEBitmap.Height / oldDPI * aNewDPI_Y);
  ImageEnMView.Proc.Resample(-1, newheight, aResampleFilter);
  ImageEnMView.Update;


.. nothing works. The files are apparently never changed (don't think IrfanView is showing wrong information). Apart from the obvious reason: I'm a fool at this - can anyone please help me, in terms of:

- Best practice on shrinking data
- Examples

I know there's a lot of demos but when not knowing that resizing actually is called resample and such, I'm pretty lost and need someone pointing in the right direction.

rswyman@docuxplorer.com

USA
156 Posts

Posted - Aug 14 2012 :  12:43:31  Show Profile  Reply
I use the follow to enable the use to pick the compression method base on the file type (see below). It compress the selected thumnail items.

You can slo adjust the resolution aka height and width with these functions

ImageEnView.ChangeResolution(height, TResampleFilter value here)
or
ImageEnView.Proc.Resample(width, height, TResampleFilter value here);


Hope this helps.

procedure TImagEnFrm.acImageCompressionExecute(Sender: TObject);
var
i: Integer;
Compression: TioTiffCompression;
vCursor: Integer;
ModelResult: boolean;

procedure DuplicateCompressionInfo;
var
i: integer;
tmp: TIOParams;
begin
tmp := TIOParams.Create(nil);
tmp.AssignCompressionInfo(ImageEnMView.MIO.Params[_CurrentFrame]);
for i := 0 to Pred(ImageEnMView.MultiSelectedImagesCount) do begin
ImageEnMView.MIO.Params[ImageEnMView.MultiSelectedImages[i]].AssignCompressionInfo(tmp);
if ImageEnMView.MIO.IsTIFFIntelFile(_Currentfile) then begin
ImageEnMView.MIO.ReplaceToFileTIFF(_CurrentFile, ImageEnMView.MultiSelectedImages[i])
end else begin
SaveMIO(_CurrentFile);
end;
end;
FreeAndNil(tmp);
end;

begin
vCursor := Screen.Cursor;
ModelResult := false;
try
Screen.Cursor := crHourGlass;
if ImageEnMView.Mio.Params[_CurrentFrame].FileType = ioTiff then begin
ModelResult := ImageEnMView.MIO.DoPreviews(_CurrentFrame, [ppTiff]);
end else if ImageEnMView.Mio.Params[_CurrentFrame].FileType = ioJPEG then begin
ModelResult := ImageEnMView.MIO.DoPreviews(_CurrentFrame, [ppJpeg]);
end else if ImageEnMView.Mio.Params[_CurrentFrame].FileType = ioBMP then begin
ModelResult := ImageEnMView.MIO.DoPreviews(_CurrentFrame, [ppBMP]);
end else if ImageEnMView.Mio.Params[_CurrentFrame].FileType = ioPNG then begin
ModelResult := ImageEnMView.MIO.DoPreviews(_CurrentFrame, [ppPNG]);
end else if ImageEnMView.Mio.Params[_CurrentFrame].FileType = ioGIF then begin
ModelResult := ImageEnMView.MIO.DoPreviews(_CurrentFrame, [ppGIF]);
end else if ImageEnMView.Mio.Params[_CurrentFrame].FileType = ioJP2 then begin
ModelResult := ImageEnMView.MIO.DoPreviews(_CurrentFrame, [ppJpeg]);
end else if ImageEnMView.Mio.Params[_CurrentFrame].FileType = ioJ2K then begin
ModelResult := ImageEnMView.MIO.DoPreviews(_CurrentFrame, [ppJpeg]);
end else begin
ModelResult := ImageEnMView.MIO.DoPreviews(_CurrentFrame, [ppTiff,ppJpeg]);
end;

if ModelResult then begin
DuplicateCompressionInfo;
// save your changes here.....

end;
finally
Screen.Cursor := vCursor;
end;
end;
Go to Top of Page

hrc

Denmark
6 Posts

Posted - Aug 15 2012 :  02:32:46  Show Profile  Reply
Thanks for the post. I have already tried Resample and ChangeResolution (which end up calling ChangeResulution). It doesn't work and I don't know why.

.. well now Resample (and hereby also ChangeResolution) works. Working with MTiff I have to iterate each page scanned (why can't I read such things in the help file?):

for i := 0 to ImageEnMView.ImageCount - 1 do
begin
  ImageEnMView.SelectedImage := i;
  ImageEnMView.Proc.Resample(2000,-1); // hardcoded, testing
  ImageEnMView.Update;
  ImageEnMView.Proc.ConvertTo(8);
  ImageEnMView.Update;
end;

The result is a changed file! Next problem is that the colours seems to be reduced to 8 bit, but Irfan still claims it is a 24 bit image. Reducing the colours improve compression but I need to be able to change that.

The same goes for my ChangeResulution which changes the dimension but Irfan again reports 300 DPI instead of the 75 it apparently is changed to. Don't know if DPI is informative only.

Progress, but not there yet.
Go to Top of Page

w2m

USA
1990 Posts

Posted - Aug 15 2012 :  06:17:15  Show Profile  Reply
i could be wrong, but changeresolution may require saving to a file for the image size to be set. In your case you may be able to changeresolution, save the file to a memory stream, then replace the image with loadfromstream. I am not sure about this: untested.
// load input.jpg which has 100 dpi
ImageEnView1.IO.LoadFromFile('input.jpg');
// we want 75 dpi
ImageEnView1.ChangeResolution(75,rfTriangle);
// save back as 75 dpi
ImageEnView1.IO.SaveToFile('output.jpg');

Dpi, image dimensions, bitdepth and filetype are the things that optimize.
I think that
ImageEnMView.Proc.ConvertTo(CalcColorsUsed);
ImageEnMView.Proc.ConvertToGray;
ImageEnMView.Proc.ConvertToBWOrdered;
all result in 24-bit images so that is why there is no optimazation.

Email: w2m@frontiernet.net
EBook: http://www.imageen.com/ebook/
Apprehend: http://www.frontiernet.net/~w2m/index.html

William Miller
Go to Top of Page

rswyman@docuxplorer.com

USA
156 Posts

Posted - Aug 15 2012 :  07:35:53  Show Profile  Reply
Just a comment: The tag Dpi, DpiX, DpiY are tags and are not update via ImageEnMView.Proc.Resample as there is no referance to the display size or print size where ChangeResolution updates these tag but does not update the Width and Height tags untill the image is reloaded.
Go to Top of Page

hrc

Denmark
6 Posts

Posted - Aug 15 2012 :  08:21:32  Show Profile  Reply
If uncompressed there will be no gain but if colours are reduced from 100,000 to 8 the compression will improve greatly.

I can now resample the files the way I wan't it. The images are downscaled so they fit the A4-page. Because the DPI's are tags they are set too for informational purposes.

The only hurdle I now have is to change the bit depth (although this is less important as all images are compressed)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: