图片旋转
如何将一张图片按任意角度旋转,用C语言求解!~这个是图片旋转函数,我没看懂,谁能帮我注释下。
static void so_graphics_drawImage_rotate_ext(so_graphics_t * g, so_image_t * image, S32 x, S32 y, S32 angle)
{
S32 left, top, right, bottom, src_width;
_FIXED_22_10 size, cx, cy, fixed_x, fixed_y, sizex, sizey, sin_value, cos_value;
_FIXED_22_10 width_border, height_border;
S32 i, j, dst_offset, src_offset, flr_x, flr_y;
U16 * src_rgb_buf, *rgb_buf, * dst_buf, dst_color;
U16 color1, color2, color3, color4, rob1, ogo1, rob3, ogo3;
U16 color5;
U16 transparentColor = so_graphics_getTransparentColor();
cx = _TO_FIXED_22_10(x);
cy = _TO_FIXED_22_10(y);
size = _TO_FIXED_22_10(SO_MATH_MAX(image->width, image->height)) / 2 + _TO_FIXED_22_10(2);
left = SO_MATH_MAX(_FIXED_22_10_FLOOR(cx - size), g->clipX);
top = SO_MATH_MAX(_FIXED_22_10_FLOOR(cy - size), g->clipY);
right = SO_MATH_MIN(_FIXED_22_10_CEIL(cx + size), g->clipX + g->clipWidth);
bottom = SO_MATH_MIN(_FIXED_22_10_CEIL(cy + size), g->clipY + g->clipHeight);
if (left >= right || top >= bottom)
return;
src_width = image->width;
width_border = _TO_FIXED_22_10(image->width - 1);
sizex = _TO_FIXED_22_10(image->width) / 2;
height_border = _TO_FIXED_22_10(image->height - 1);
sizey = _TO_FIXED_22_10(image->height) / 2;
so_math_fixed_sincos((270 - angle) * 10, &sin_value, &cos_value);
src_rgb_buf = image->buffer;
dst_buf = (U16 *)g->buffer + top * g->width + left;
dst_offset = g->width - (right - left);
for (i = top; i < bottom; i ++)
{
fixed_x = (((_TO_FIXED_22_10(left) - cx) * cos_value -
(_TO_FIXED_22_10(i) - cy) * sin_value) >> 10) + sizex;
fixed_y = (((_TO_FIXED_22_10(left) - cx) * sin_value +
(_TO_FIXED_22_10(i) - cy) * cos_value) >> 10) + sizey;
for (j = left; j < right; j ++,
dst_buf ++, fixed_x += cos_value, fixed_y += sin_value)
{
if (!(fixed_x < 0 || fixed_y < 0 || fixed_x >= width_border ||
fixed_y >= height_border))
{
src_offset = _FIXED_22_10_FLOOR(fixed_y) * src_width + _FIXED_22_10_FLOOR(fixed_x);
flr_x = (fixed_x & 0x3FF) >> 6;
flr_y = (fixed_y & 0x3FF) >> 6;
// 计算平均混合色
// 4次取址
rgb_buf = src_rgb_buf + src_offset;
color1 = *rgb_buf;
if (color1 == transparentColor)
continue;
rgb_buf ++;
color2 = *rgb_buf;
if (color2 == transparentColor)
continue;
rgb_buf += src_width;
color4 = *rgb_buf;
if (color4 == transparentColor)
continue;
rgb_buf --;
color3 = *rgb_buf;
if (color3 == transparentColor)
continue;
#if 0
if(color1 == transparentColor){
if(color2 != transparentColor){
color1 = color2;
}else if(color3 != transparentColor){
color1 = color3;
}else if(color4 != transparentColor){
color1 = color4;
}
}
if(color2 == transparentColor){
if(color1 != transparentColor){
color2 = color1;
}else if(color3 != transparentColor){
color2 = color3;
}else if(color4 != transparentColor){
color2 = color4;
}
}
if(color3 == transparentColor){
if(color2 != transparentColor){
color3 = color2;
}else if(color1 != transparentColor){
color3 = color1;
}else if(color4 != transparentColor){
color3 = color4;
}
}
if(color4 == transparentColor){
if(color2 != transparentColor){
color4 = color2;
}else if(color3 != transparentColor){
color4 = color3;
}else if(color1 != transparentColor){
color4 = color1;
}
}
#endif
// 6次混合乘法
rob1 = ((((color1 & 0xF81F) << 4) + ((color2 & 0xF81F) - (color1 & 0xF81F)) * flr_x) >> 4) & 0xF81F;
ogo1 = ((((color1 & 0x07E0) << 4) + ((color2 & 0x07E0) - (color1 & 0x07E0)) * flr_x) >> 4) & 0x07E0;
rob3 = ((((color3 & 0xF81F) << 4) + ((color4 & 0xF81F) - (color3 & 0xF81F)) * flr_x) >> 4) & 0xF81F;
ogo3 = ((((color3 & 0x07E0) << 4) + ((color4 & 0x07E0) - (color3 & 0x07E0)) * flr_x) >> 4) & 0x07E0;
rob1 = (((rob1 << 4) + (rob3 - rob1) * flr_y) >> 4) & 0xF81F;
ogo1 = (((ogo1 << 4) + (ogo3 - ogo1) * flr_y) >> 4) & 0x07E0;
color5 = rob1 | ogo1;
dst_color = *dst_buf;
//alpha
if (color5 != transparentColor) {
*dst_buf = color5 ;
}
}
}
dst_buf += dst_offset;
}
}
[ 本帖最后由 狼性执行 于 2012-3-2 10:16 编辑 ]