Board index » cppbuilder » JPEG Image resize with antialias?

JPEG Image resize with antialias?


2005-07-27 06:13:36 AM
cppbuilder103
Hi All,
I made a little program which resizes a JPEG image, but the thumbnails I
create have rough edges. In graphics software, there is an option to resize
the image with antialias, I wonder if there is a way to do this in my
program. Ideally I would need a free solution and easy to implement. I am
only interested in JPEG images.
Regards,
Nicolae
 
 

Re:JPEG Image resize with antialias?

Hi Nicolae,
If you don't need to deploy your application on Windows 95, you could
use GDI+, which has built-in support for antialiasing. Otherwise, a
quick solution (when reducing the image's size) is to use the
StretchBlt() GDI function with the HALFTONE stretching mode. For
example, if Image1 contains the source image, and Image2 is where you
want to place the (e.g., 128x128) thumbnail, you'd do...
// make room for the thumbnail
Image2->Picture->Bitmap->PixelFormat = pf24bit;
Image2->Picture->Bitmap->Height = 0;
Image2->Picture->Bitmap->Width = 128;
Image2->Picture->Bitmap->Height = 128;
// grab a handle to the thumbnail's memory DC
// and specify the HALFTONE stretching mode
HDC const hDstDC = Image2->Picture->Bitmap->Canvas->Handle;
POINT brush_org;
GetBrushOrgEx(hDstDC, &brush_org);
SetStretchBltMode(hDstDC, HALFTONE);
SetBrushOrgEx(hDstDC, brush_org.x, brush_org.y, NULL);
// draw Image1 stretched-to-fit onto Image2
StretchBlt(
hDstDC, 0, 0, 128, 128,
Image1->Picture->Bitmap->Canvas->Handle, 0, 0,
Image1->Picture->Bitmap->Width,
Image1->Picture->Bitmap->Height,
SRCCOPY
);
// update the display
Image2->Refresh();
If you're still seeing aliasing, your best option would be to use an
antialiasing filter (say, a Gaussian filter with the proper cutoff
frequency) followed by downsampling (e.g., tinyurl.com/2spwx).
Let me know if you need assistance designing the filter; or if you have
access to Matlab, check out the gausswin() function--the cutoff
frequency depends only on the scaling factor.
Good luck,
C++Builder Developer's Journal
bcbjournal.com
BCB Commonly Asked Questions
bcbjournal.com/bcbcaq
Nicolae Fieraru wrote:
Quote
I made a little program which resizes a JPEG image, but the thumbnails I
create have rough edges. In graphics software, there is an option to resize
the image with antialias, I wonder if there is a way to do this in my
program. Ideally I would need a free solution and easy to implement. I am
only interested in JPEG images.
 

Re:JPEG Image resize with antialias?

Hi Damon,
Thank you very much for your support. My application is intended to work
only on my XP computer and I actually need to read a jpg file, resize the
image and then save it again as a jpg image file. If you have other ideas,
please share with me.
I will have a look at the GDI+ to see if I am able to use it.
Regards,
Nicolae
 

{smallsort}

Re:JPEG Image resize with antialias?

Nicolae,
If you're targeting only XP, then I'd definitely suggest using GDI+.
In particular, the GDI+ Image class has a GetThumbnailImage() method,
which will do all of the work for you; see tinyurl.com/cf59p
Keep in mind though, that JPEG is a compressed format, which requires
decompression to get to the actual pixel data. No matter what technique
you use (GDI, GDI+, or VCL), you will have to use an intermediate array
of pixels (e.g., a bitmap) to create a thumbnail. If you want to stick
with the GDI/VCL, you can use a TJPEGImage object to load the JPG (i.e.,
decompress it into a bitmap), use the code in the previous post to
resize the bitmap, and then use the TJPEGImage class to save the
thumbnail to a JPG file.
Good luck,
Damon
Nicolae Fieraru wrote:
Quote
Thank you very much for your support. My application is intended to work
only on my XP computer and I actually need to read a jpg file, resize the
image and then save it again as a jpg image file. If you have other ideas,
please share with me.
I will have a look at the GDI+ to see if I am able to use it.