Recent

Author Topic: Component to resize and change color depth on images?  (Read 2645 times)

bulrush

  • Jr. Member
  • **
  • Posts: 86
Component to resize and change color depth on images?
« on: July 06, 2016, 01:24:14 pm »
Looking for component to resize an image (and keep width to height ratio) and change color depth, to say, 8-bit. I want my mkepub program to have the option to make images a big smaller in color depth, and reduce them to some max dimension I will determine later.

This will be for these image types: jpg, png.

Free is preferred.

  • I assume PascalMagic shells out to use the ImageMagick executable, which I'd rather avoid as it's slow. I could be processing 50 images in one epub.


Thanks!
« Last Edit: July 06, 2016, 01:36:38 pm by bulrush »
Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Component to resize and change color depth on images?
« Reply #1 on: July 06, 2016, 03:01:24 pm »
That's doable with BGRABitmap.

There is a Resample function that resizes to any width/height. So you would need to multiply/divide both by the same number to preserve the aspect ratio.

Then with the TBGRAColorQuantizer class of the unit BGRAColorQuantization, you can specify the number of colors and save it. This makes sense for PNG 256 colors and BMP 16 colors or 256 colors. Note that sometimes, a 24bit PNG file is smaller than a 256 colors PNG file.

For JPG, the way is not quantization but to specify the quality. You can specify by creating a writer (class is defined in FPWriteJPEG), set its CompressionQuality property, and then pass it as a parameter when calling SaveToFile.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018