gimp源程序:
static void
remove_redeye (gimpdrawable *drawable)
{
gimppixelrgn src_rgn;
gimppixelrgn dest_rgn;
gint progress, max_progress;
gboolean has_alpha;
gint x, y;
gint width, height;
gint i;
gpointer pr;
if (! gimp_drawable_mask_intersect (drawable->drawable_id,
&x, &y, &width, &height))
return;
gimp_progress_init (_("removing red eye"));
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
progress = 0;
max_progress = width * height;
gimp_pixel_rgn_init (&src_rgn, drawable,
x, y, width, height, false, false);
gimp_pixel_rgn_init (&dest_rgn, drawable,
x, y, width, height, true, true);
for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn), i = 0;
pr != null;
pr = gimp_pixel_rgns_process (pr), i )
{
redeye_inner_loop (src_rgn.data, dest_rgn.data, src_rgn.w, src_rgn.h,
src_rgn.bpp, has_alpha, src_rgn.rowstride);
progress = src_rgn.w * src_rgn.h;
if (i % 16 == 0)
gimp_progress_update ((gdouble) progress / (gdouble) max_progress);
}
gimp_progress_update (1.0);
gimp_drawable_flush (drawable);
gimp_drawable_merge_shadow (drawable->drawable_id, true);
gimp_drawable_update (drawable->drawable_id, x, y, width, height);
}
主要功能在redeye_inner_loop这个函数里,这个函数里面要改两处:
1 把gint之类改成int
2 原程序是按r,g,b,a来处理的,实际要反过来,也就是说,
const gint red = 0;
const gint green = 1;
const gint blue = 2;
const gint alpha = 3;
倒序就可以了。程序里实际操作的图都是24位真彩,没有用到a,所以改成了:
const int red = 2;
const int green = 1;
const int blue = 0;
const int alpha = 3;//没有用到
主要改造remove_redeye 这个函数。说改造,其实就是按范围取值计算而已。
简单改写之写:
#define widthbytes(bits) ((dword)(((bits) 31) & (~31)) / 8)
void remove_redeye()
{
if (!m_image.isvalid())
return;
//准备数据
int bytes = m_image.getbitsperpixel()/8;
bool has_alpha = m_image.istransparent();
fipwinimage image2 = m_image;
int x = 0;
int y = 0;
int width = m_image.getwidth();
int height = m_image.getheight();
int size = width*height*3;
size = widthbytes(m_image.getbitsperpixel()*width)*height;
bitmapinfo* info = m_image.getinfo();
int rowstride = widthbytes(m_image.getbitsperpixel()*width);
byte *src_ptr = m_image.accesspixels();
byte *dest_ptr = new byte[size];
byte* dest_line = dest_ptr;
redeye_inner_loop(src_ptr, dest_ptr, width, height, bytes, has_alpha, rowstride);
char* szfile = "out.bmp";
g_fsavedatatobitmap(&(info->bmiheader), szfile, dest_ptr, m_image.getimagesize());
m_imagedest.load(szfile);
if (!m_imagedest.isvalid())
{
afxmessagebox("处理失败!");
}
delete[] dest_ptr;
}
程序使用freeimage读出源图,处理完保存成bmp。注意,一定要用widthbytes保证字节对齐。
阅读(4230) | 评论(0) | 转发(0) |