求BMP图像放大算法源码!分不够可以加。

IvanQi 2004-04-08 07:18:28
求BMP图像放大算法代码,要求是自己设计的放大算法。谢谢了。
...全文
238 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
酒红绿叶 2004-04-09
  • 打赏
  • 举报
回复
#include "stdafx.h"


void Zoom( BYTE *lpDIBBits,BYTE *lpNewDIBBits,long lWidth,long lHeight, float x_Zoom, float y_Zoom )
{

//x_Zoom x方向放大的倍数,
//y_Zoom y方向放大的倍数
long lNewWidth;
long lNewHeight;// 缩放后图像的宽度和高度
long i,j;
long lNewLineBytes; // 缩放后图像的宽度(lNewWidth',必须是4的倍数)

BYTE * lpSrc;// 指向源象素的指针
BYTE * lpDst;// 指向缩放图像对应象素的指针

long i0; // 象素在源DIB中的坐标
long j0;

long lLineBytes;// 图像每行的字节数

lLineBytes = WIDTHBYTES(8*lWidth);

lNewWidth = (long) (lWidth * x_Zoom + 0.5);
lNewLineBytes = WIDTHBYTES(lNewWidth * 8);
lNewHeight = (long) (lHeight *y_Zoom + 0.5);

// 针对图像每行进行操作
for(i = 0; i < lNewHeight; i++)
{
// 针对图像每列进行操作
for(j = 0; j < lNewWidth; j++)
{

lpDst = (BYTE *)lpNewDIBBits + lNewLineBytes * (lNewHeight - 1 - i) + j;

// 计算该象素在源DIB中的坐标
i0 = (long) (i / y_Zoom + 0.5);
j0 = (long) (j / x_Zoom + 0.5);

// 判断是否在源图范围内
if( (j0 >= 0) && (j0 < lWidth) && (i0 >= 0) && (i0 < lHeight))
{

// 指向源DIB第i0行,第j0个象素的指针
lpSrc = (BYTE *)lpDIBBits + lLineBytes * (lHeight - 1 - i0) + j0;

// 复制象素
*lpDst = *lpSrc;
}
else
{
// 对于源图中没有的象素,直接赋值为255
* ((BYTE*)lpDst) = 255;
}
}
}

//这只是简单的缩放,应该加插值,
}

BYTE Interpolation (BYTE * lpDIBBits, long lWidth, long lHeight, FLOAT x, FLOAT y)
{


// 四个最临近象素的坐标(i1, j1), (i2, j1), (i1, j2), (i2, j2)
long i1, i2;
long j1, j2;

// 四个最临近象素值
unsigned char f1, f2, f3, f4;

// 二个插值中间值
unsigned char f12, f34;

// 定义一个值,当象素坐标相差小于改值时认为坐标相同
FLOAT EXP;

// 图像每行的字节数
long lLineBytes;

// 计算图像每行的字节数
lLineBytes = WIDTHBYTES(lWidth * 8);

// 赋值
EXP = (FLOAT) 0.0001;

// 计算四个最临近象素的坐标
i1 = (long) x;
i2 = i1 + 1;
j1 = (long) y;
j2 = j1 + 1;

// 根据不同情况分别处理
if( (x < 0) || (x > lWidth - 1) || (y < 0) || (y > lHeight - 1))
{
// 要计算的点不在源图范围内,直接返回255。
return 255;
}
else
{
if (fabs(x - lWidth + 1) <= EXP)
{
// 要计算的点在图像右边缘上
if (fabs(y - lHeight + 1) <= EXP)
{
// 要计算的点正好是图像最右下角那一个象素,直接返回该点象素值
f1 = *((unsigned char *)lpDIBBits + lLineBytes * (lHeight - 1 - j1) + i1);
return f1;
}
else
{
// 在图像右边缘上且不是最后一点,直接一次插值即可
f1 = *((unsigned char *)lpDIBBits + lLineBytes * (lHeight - 1 - j1) + i1);
f3 = *((unsigned char *)lpDIBBits + lLineBytes * (lHeight - 1 - j1) + i2);

// 返回插值结果
return ((unsigned char) (f1 + (y -j1) * (f3 - f1)));
}
}
else if (fabs(y - lHeight + 1) <= EXP)
{
// 要计算的点在图像下边缘上且不是最后一点,直接一次插值即可
f1 = *((unsigned char *)lpDIBBits + lLineBytes * (lHeight - 1 - j1) + i1);
f2 = *((unsigned char *)lpDIBBits + lLineBytes * (lHeight - 1 - j2) + i1);

// 返回插值结果
return ((unsigned char) (f1 + (x -i1) * (f2 - f1)));
}
else
{
// 计算四个最临近象素值
f1 = *((unsigned char *)lpDIBBits + lLineBytes * (lHeight - 1 - j1) + i1);
f2 = *((unsigned char *)lpDIBBits + lLineBytes * (lHeight - 1 - j2) + i1);
f3 = *((unsigned char *)lpDIBBits + lLineBytes * (lHeight - 1 - j1) + i2);
f4 = *((unsigned char *)lpDIBBits + lLineBytes * (lHeight - 1 - j2) + i2);

// 插值1
f12 = (unsigned char) (f1 + (x - i1) * (f2 - f1));

// 插值2
f34 = (unsigned char) (f3 + (x - i1) * (f4 - f3));

// 插值3
return ((unsigned char) (f12 + (y -j1) * (f34 - f12)));
}
}
}
zhangcrony 2004-04-09
  • 打赏
  • 举报
回复
StretchBlt
The StretchBlt function copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the dimensions of the destination rectangle, if necessary. The system stretches or compresses the bitmap according to the stretching mode currently set in the destination device context.

BOOL StretchBlt(
HDC hdcDest, // handle to destination device context
int nXOriginDest, // x-coordinate of upper-left corner of dest. rectangle
int nYOriginDest, // y-coordinate of upper-left corner of dest. rectangle
int nWidthDest, // width of destination rectangle
int nHeightDest, // height of destination rectangle
HDC hdcSrc, // handle to source device context
int nXOriginSrc, // x-coordinate of upper-left corner of source rectangle
int nYOriginSrc, // y-coordinate of upper-left corner of source rectangle
int nWidthSrc, // width of source rectangle
int nHeightSrc, // height of source rectangle
DWORD dwRop // raster operation code
);

Parameters
hdcDest
Handle to the destination device context.
nXOriginDest
Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nYOriginDest
Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nWidthDest
Specifies the width, in logical units, of the destination rectangle.
nHeightDest
Specifies the height, in logical units, of the destination rectangle.
hdcSrc
Handle to the source device context.
nXOriginSrc
Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle.
nYOriginSrc
Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle.
nWidthSrc
Specifies the width, in logical units, of the source rectangle.
nHeightSrc
Specifies the height, in logical units, of the source rectangle.
dwRop
Specifies the raster operation to be performed. Raster operation codes define how the system combines colors in output operations that involve a brush, a source bitmap, and a destination bitmap.
See BitBlt for a list of common raster operation codes.

Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero.

Windows NT: To get extended error information, callGetLastError.

Remarks
StretchBlt stretches or compresses the source bitmap in memory and then copies the result to the destination rectangle. The color data for pattern or destination pixels is merged after the stretching or compression occurs.

When an enhanced metafile is being recorded, an error occurs (and the function returns FALSE) if the source device context identifies an enhanced-metafile device context.

If the specified raster operation requires a brush, the system uses the brush currently selected into the destination device context.

The destination coordinates are transformed by using the transformation currently specified for the destination device context; the source coordinates are transformed by using the transformation currently specified for the source device context.

If the source transformation has a rotation or shear, an error occurs.

If destination, source, and pattern bitmaps do not have the same color format, StretchBlt converts the source and pattern bitmaps to match the destination bitmap.

If StretchBlt must convert a monochrome bitmap to a color bitmap, it sets white bits (1) to the background color and black bits (0) to the foreground color. To convert a color bitmap to a monochrome bitmap, it sets pixels that match the background color to white (1) and sets all other pixels to black (0). The foreground and background colors of the device context with color are used.

StretchBlt creates a mirror image of a bitmap if the signs of the nWidthSrc and nWidthDest parameters or of the nHeightSrc and nHeightDest parameters differ. If nWidthSrc and nWidthDest have different signs, the function creates a mirror image of the bitmap along the x-axis. If nHeightSrc and nHeightDest have different signs, the function creates a mirror image of the bitmap along the y-axis.

Not all devices support the StretchBlt function. For more information, see the GetDeviceCaps.

ICM: No color management is performed when a blit operation occurs.

Windows CE: Windows CE version 1.0 supports only the SRCCOPY and SRCINVERT raster operations. Windows CE version 1.0 does not support mirroring.

This function is the same in Windows CE version 2.0 as it is in Windows desktop platforms.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in wingdi.h.
Import Library: Use gdi32.lib.
自由的风 2004-04-09
  • 打赏
  • 举报
回复
www.gameres.com上面有例子,和你说得要求一样
loseme915 2004-04-09
  • 打赏
  • 举报
回复
你的放大要求是什么??我需要一个放大同时象素自动调整的算法,目的是要保持图片的清晰!!
酒红绿叶 2004-04-09
  • 打赏
  • 举报
回复
给我留言,给你算法,
自己设计,要求是什么!!?

无非就是坐标变换,
以及双线性插值,
我做的支持256 灰度,和彩色,
24位的不支持!!


需要的话,告诉我!!


puhuofeie@hotmail.com
enoloo 2004-04-08
  • 打赏
  • 举报
回复
关注。
希望斑竹能置顶。

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧