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
 Change the palette to 256 color
 New Topic  Reply to Topic
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

brm121966

USA
22 Posts

Posted - Nov 29 2021 :  20:39:58  Show Profile  Reply
Hello.
Help me please.
How can I load 800x800 jpg 16M color file to resize 200x200px and change palette 256color
and write png using only one obj TImageEnView.
I got the code, but there is a problem.
The code does not change the palette to 256color.


var
  i, w, h, nKey: integer;;
  NewExtension, OutputFilename: string;
  DestImageEnView, InImageEnView  : TImageEnView;
begin 
    InputFilename := 'cover.jpg'; 
    InImageEnView := TImageEnView.Create(Owner);
    with InImageEnView do begin
      Clear;
      IO.LoadFromFile( InputFilename );
    end;

    DestImageEnView := TImageEnView.Create(Owner);
    with DestImageEnView do begin
      Clear;
      Assign(InImageEnView.IEBitmap);
      LockUpdate;

      Fit;
      Stretch;
      w := 200;
      h := 200;
      Proc.Resample(w, h, rfLanczos3, True);
      Invalidate;
      Proc.Update;

      Proc.ConvertTo(256, ieThreshold);
      IO.Params.BitsPerSample := 8;
      IO.Params.SamplesPerPixel := 1;
      IO.Update;
      Invalidate;

      IO.Params.PNG_Compression := 8;
      IO.Params.Width := IEBitmap.Width;
      IO.Params.Height := IEBitmap.Height;
      IO.Update;

      Update;
      UnLockUpdate;

      OutputFilename := ReplaceStr(InputFilename,'Cover','Folder');
      OutputFilename := ReplaceStr(OutputFilename,'.jpg','.png');
      Log('ConvertImage: save: ' + OutputFilename);
      IO.SaveToFile(OutputFilename, ioPNG);
   end;
end;


Thanks.

xequte

7028 Posts

Posted - Nov 30 2021 :  15:25:49  Show Profile  Reply
Hi

If you are not using IEGlobalSettings().AutoSetBitDepth then you should also manually specify the PixelFormat.

// Set PixelFormat to 8bit
ImageEnView1.IEBitmap.PixelFormat := ie8p;

// Use PixelFormat as authoritative source of bit-depth
IEGlobalSettings().AutoSetBitDepth := True;

https://www.imageen.com/help/TIEImageEnGlobalSettings.AutoSetBitDepth.html


Nigel
Xequte Software
www.imageen.com
Go to Top of Page

brm121966

USA
22 Posts

Posted - Nov 30 2021 :  18:23:30  Show Profile  Reply
Hi.
Sorry. I try different options, but it doesn't work.
Where would you advise to put ImageEnView1.IEBitmap.PixelFormat := ie8p and IEGlobalSettings().AutoSetBitDepth := True. Which object to control DestImageEnView or InImageEnView.
Thanks.

Rudolf
Go to Top of Page

xequte

7028 Posts

Posted - Nov 30 2021 :  22:49:42  Show Profile  Reply
Hi Rudolf

Here's an example using a TImageEnView:

var
  w, h: integer;
  InputFilename, OutputFilename: string;
  ImageEnView1: TImageEnView;
begin
  InputFilename := 'D:\im.jpg';

  ImageEnView1 := TImageEnView.Create(Owner);
  ImageEnView1.IO.LoadFromFile( InputFilename );
  ImageEnView1.LockUpdate();

  // Resize to 200x200
  w := 200;
  h := 200;
  ImageEnView1.Proc.Resample(w, h, rfLanczos3, True);

  // Reduce colors
  ImageEnView1.Proc.ConvertTo(256, ieThreshold);

  // Set PixelFormat to 8bit
  ImageEnView1.IEBitmap.PixelFormat := ie8p;

  // Use PixelFormat as authoritative source of bit-depth
  IEGlobalSettings().AutoSetBitDepth := True;

  ImageEnView1.UnLockUpdate();

  // Save it
  OutputFilename := ReplaceStr(InputFilename,'.jpg','.png');
  ImageEnView1.IO.Params.PNG_Compression := 8;
  ImageEnView1.IO.SaveToFile(OutputFilename, ioPNG);

  ImageEnView1.Free;
end;


Nigel
Xequte Software
www.imageen.com
Go to Top of Page

xequte

7028 Posts

Posted - Nov 30 2021 :  22:54:41  Show Profile  Reply
And one just using a TIEBitmap:
var
  w, h: integer;
  InputFilename, OutputFilename: string;
  bmp: TIEBitmap;
begin
  InputFilename := 'D:\im2.jpg';

  bmp := TIEBitmap.Create();

  bmp.ParamsEnabled := True;
  bmp.Read( InputFilename );

  // Resize to 200x200
  w := 200;
  h := 200;
  bmp.Resample(w, h, rfLanczos3, True);

  // Set PixelFormat to 8bit
  bmp.PixelFormat := ie8p;

  // Use PixelFormat as authoritative source of bit-depth
  IEGlobalSettings().AutoSetBitDepth := True;

  // Save it
  OutputFilename := ReplaceStr(InputFilename,'.jpg','.png');
  bmp.Params.PNG_Compression := 8;
  bmp.Write(OutputFilename, ioPNG);

  bmp.Free
end;


Nigel
Xequte Software
www.imageen.com
Go to Top of Page

brm121966

USA
22 Posts

Posted - Dec 01 2021 :  10:16:36  Show Profile  Reply
Hi.
I'm sorry.
But I get Exception: hyiedefs [hyiedefs] [EIEException] [] [WIC Failure] after SaveToFile or Write and png file 0 size.
I am using EmbarcaderoŽ DelphiŽ XE5 version 19.0.14356.6604 (32). Win10 (64). My app (64) converting ipg to png without converting palette and png to png resample without converting palette works.
Thanks.

Rudolf
Go to Top of Page

xequte

7028 Posts

Posted - Dec 01 2021 :  14:37:12  Show Profile  Reply
Hi Rudolf

The ie8p pixel format is not supported with WIC. For 64bit applications, please add ielib64.dll to your EXE folder:

https://www.imageen.com/help/ImageEn_DLLs.html

Nigel
Xequte Software
www.imageen.com
Go to Top of Page

brm121966

USA
22 Posts

Posted - Dec 01 2021 :  16:53:17  Show Profile  Reply
Thanks. It works.

Rudolf
Go to Top of Page

brm121966

USA
22 Posts

Posted - Dec 01 2021 :  18:04:05  Show Profile  Reply
Sorry.
Another problem is converting the palette JPG, PNG (24 Mb) 800x800 and resizing to PNG (8 bit) 200x200 using the algorithm with TIEBitmap, returns a file without an image or a file with vertical B/W lines, the correct image size. Some files will convert normally with the same characteristics. The procedure runs without error messages.
What settings do you need to make for this code to work?

P.S.
Some files have rounded corners with transparent backgrounds. In the modified files, the corners are filled with white.

Thanks.

Rudolf
Go to Top of Page

brm121966

USA
22 Posts

Posted - Dec 02 2021 :  09:27:34  Show Profile  Reply
I've Added AlphaChannel. The conversion was correct.

One problem remains. Some png files have areas with transparent backgrounds. In modified files, the background is filled with a solid color (white or black).

Rudolf
Go to Top of Page

xequte

7028 Posts

Posted - Dec 04 2021 :  15:11:27  Show Profile  Reply
Hi Rudolf

You can remove the Alpha channel and merge it with a preferred fill color:

https://www.imageen.com/help/TIEBitmap.RemoveAlphaChannel.html

Nigel
Xequte Software
www.imageen.com
Go to Top of Page

brm121966

USA
22 Posts

Posted - Dec 04 2021 :  19:43:12  Show Profile  Reply
Hi Nigel.
This is my last code.
w := 400;
  h := 400;
      ImageEnView1 := TImageEnView.Create(nil);
      with ImageEnView1 do begin
        IO.LoadFromFile( InputFilename );
        AlphaChannel;                                                           //Add AlphaChannel

        Proc.Resample(w, h, rfLanczos3, True);                                  //Resize to 
        Proc.ConvertTo(256, ieThreshold);                                       //Reduce colors
        Proc.Sharpen(10,4);

        IEBitmap.PixelFormat := ie8p;                                           // Set PixelFormat to 8bit
        IEGlobalSettings().AutoSetBitDepth := True;                             // Use PixelFormat as authoritative source of bit-depth
        OutputFilename := ReplaceStr(InputFilename,'Cover','Folder');           // Save it
        OutputFilename := ReplaceStr(OutputFilename,'.jpg','.png');

        IO.Params.PNG_Compression := 8;
        IO.SaveToFile(OutputFilename, ioPNG);                                   // Save it
        Free;
      end;


Without an alpha channel, images are spoiled.

example image: https://www.dropbox.com/s/54gcnobsh1r3ugh/image.zip?dl=0

Compilation of Folder-4.png and Folder-5.png without alpha channel
I want to get a thumbnail of the original image from the Cover*.* files.


Rudolf
Go to Top of Page

brm121966

USA
22 Posts

Posted - Dec 08 2021 :  19:39:39  Show Profile  Reply
Hi Nigel.
Some files have a transparent background. In the modified files, the background is filled with white or black. Any ideas on how to fix this.
Thanks.

Rudolf
Go to Top of Page

xequte

7028 Posts

Posted - Dec 09 2021 :  23:07:52  Show Profile  Reply
Hi Rudolf

Your code works for me as long as there is an AlphaChannel (i.e. your call to ImageEnView1.AlphaChanel). Otherwise I get an output image with vertical banding. It is an issue with the PNG output filter as it only affects PNG.

We have a fix for this in the upcoming 10.3.0



Nigel
Xequte Software
www.imageen.com
Go to Top of Page

brm121966

USA
22 Posts

Posted - Dec 20 2021 :  22:06:36  Show Profile  Reply
Hi Nigel.

I now have 10.3.0, but there is a problem. The file image is not drawn correctly.

example image: https://www.dropbox.com/s/ph4lij5f73ce3zu/Blues.zip?dl=0

Thinks.

Rudolf

P.S.
my code:

  ImageEnView1 := TImageEnView.Create(nil);
  with ImageEnView1 do begin
    IO.LoadFromFile( InputFilename );
    Proc.Resample(400, 400, rfLanczos3, True);
    Proc.ConvertTo(256, ieThreshold);                //Reduce colors
    Proc.Sharpen(10,4);
    IEBitmap.PixelFormat := ie8p;                    // Set PixelFormat to 8bit
    IEGlobalSettings().AutoSetBitDepth := True;
    OutputFilename := ReplaceStr(InputFilename,'Cover','Folder'); 
    OutputFilename := ReplaceStr(OutputFilename,'.jpg','.png');
    IO.Params.PNG_Compression := 8;
    IO.SaveToFile(OutputFilename, ioPNG);                          // Save it
    Free;
  end;
Go to Top of Page

xequte

7028 Posts

Posted - Dec 21 2021 :  23:18:57  Show Profile  Reply
8bit PNG format only supports index transparency, which accounts for the artifacts you see. You might want to merge/remove the alpha channel before saving.

I'll see if we can improve the output.

Nigel
Xequte Software
www.imageen.com
Go to Top of Page

brm121966

USA
22 Posts

Posted - Dec 22 2021 :  20:42:09  Show Profile  Reply
Sorry, but FastStone 7.5 converted this file (Cover.png) without artifacts.
Inserted IEBitmap.RemoveAlphaChannel(True), but the transparent areas are replaced with areas with a white background.
It spoils the picture.
What function can I use to merge / remove the alpha channel?

Thinks.

Rudolf
Go to Top of Page

xequte

7028 Posts

Posted - Dec 23 2021 :  17:21:19  Show Profile  Reply
Hi Rudolf

In the new year, we'll see if there is a better method to calculate the index color.

Nigel
Xequte Software
www.imageen.com
Go to Top of Page

xequte

7028 Posts

Posted - Jan 30 2022 :  20:24:10  Show Profile  Reply
Hi

You can email me for a fix for this.

If you need to tweak the result, add pngfiltw to your uses and specify a different alpha threshold value, e.g.

gPNGAlphaThreshold := 125;

Nigel
Xequte Software
www.imageen.com
Go to Top of Page

brm121966

USA
22 Posts

Posted - Feb 08 2022 :  09:25:08  Show Profile  Reply
Hi Nigel. Sorry, but how do I use this (gPNGAlphaThreshold := 125;) in the code I have. Thank you.

Rudolf
Go to Top of Page

xequte

7028 Posts

Posted - Feb 08 2022 :  15:27:20  Show Profile  Reply
Hi Rudolf

8bit PNG files only support 1bit alpha (transparent or not-transparent pixels).

gPNGAlphaThreshold just specifies which pixels are converted to transparent or not.

So for the default of gPNGAlphaThreshold := 125; pixels with alpha values < 125 become fully transparent. Pixels with alpha values > 125 become opaque.



Nigel
Xequte Software
www.imageen.com
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Reply to Topic
Jump To: