yanuart
08-12-2005, 08:56 AM
Hi, my program needs to resize (for thumbnails) the bmp produced from screen capture.
I wonder if there's a good BMP/graphic library for this ?
I've been googling for awhile and only found easyBmp.
Nikster
08-12-2005, 09:07 AM
For the same sort of thing I use GDI's StretchBlt, You could also use FreeImage (http://freeimage.sourceforge.net/) and it's Rescale function, but I guess it depends what enviroment you're under.
milieu
08-12-2005, 10:24 AM
If you're using SDL, the sdl_gfx library offers some very nice scaling and rotating functions.
Bad Sector
08-12-2005, 10:42 PM
Here is a piece of code i just wrote:
typedef struct _Color
{
unsigned char r;
unsigned char g;
unsigned char b;
} Color;
typedef void (*PixelFunc)(int x, int y, Color *c);
// w: original width
// h: original height
// nw: new width
// nh: new height
// get: a PixelFunc that gets a pixel from the original image
// set: a PixelFunc that sets a pixel to the new image
void resizeImage(int w, int h, int nw, int nh, PixelFunc get, PixelFunc set)
{
int x, y;
float dw, dh;
float sx = 0, sy = 0;
if (nw < 1 || nh < 1)
return;
dw = (float)w/(float)nw;
dh = (float)h/(float)nh;
for (y=0;y<nh;y++)
{
sx = 0;
for (x=0;x<nw;x++)
{
Color sc1;
int subx, suby;
int r = 0, g = 0, b = 0, cnt = 0;
for (suby=0;suby<(int)dh;suby++)
for (subx=0;subx<(int)dw;subx++)
{
Color s;
get(sx+subx, sy+suby, &s);
r += s.r;
g += s.g;
b += s.b;
cnt++;
}
r /= cnt;
g /= cnt;
b /= cnt;
sc1.r = r;
sc1.g = g;
sc1.b = b;
set(x, y, &sc1);
sx += dw;
}
sy += dh;
}
}
The above function shrinks a given image to a new one with bilinear interpolation (to avoid the artifacts of plain linear interpolation). This function works only if both the new width and the new height are smaller than the originals. A function that works with each case will be a little more complicated.
The "get" and "set" functions are functions that gets and sets a pixel from the source and to the target image.
For the full source code under SDL click here (http://www.slashstone.com/gimme/sdlresize.tar.bz2). I suppose that even if you don't use SDL, you'll be able to understand how this works.
If you use Linux or Windows with MinGW+MSYS and have SDL installed, just place the files in this archive in an empty directory, open console, type make and you'll have the program ready to test :-).
Note that it's the first time i write code for bilinear interpolation so i may do something terribly wrong... :D but it produces the desired result, so i think it's ok ;-)
vBulletin v3.6.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.