5161|11

56

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

图像旋转后显示乱码 [复制链接]

旋转前图像的宽高是4的倍数的矩形就没有问题;如果不是4的倍数,旋转就会显示出来乱码,我查了,说是扫描的行数要是4的倍数,要更改width的值,但更改了好几回,图片还是乱的。。。有人做过的,麻烦指点一下。多谢了



BITMAPINFO RGB24BitsBITMAPINFO;
                ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
                RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
                RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
                RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;//
                RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
                RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;
               
                //use the function CreateDIBSection and SelectObject
                //in order to get the bimap pointer : lpBitmapBits
                HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO,
                        DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);

                     HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);
               
                // copy the screen dc to the memory dc
                BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);

                BITMAPFILEHEADER bmBITMAPFILEHEADER;
                ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
                bmBITMAPFILEHEADER.bfType = 0x4d42;  //bmp  
                bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
                bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*3); ///3=(24 / 8)
               
                typedef struct _MYRGB
                {
                        char r;
                        char g;
                        char b;
                }MyRGB;
               
                ASSERT(sizeof(MyRGB) == 3);

                MyRGB* pDes = (MyRGB*)malloc(width * height * 3 + 1);
                if(pDes == NULL)
                {
                        return;
                }
                memset(pDes,0,width * height * 3 + 1);
                MyRGB* pSrc = (MyRGB* )lpBitmapBits;
       
                if(nRotateType==1)//顺时针
                {
                        for(int l = 0;l < width * height;l++)
                        {
                                int i = l/width;
                                int j = l%width;
                                pDes[(width - 1 - j) * height + i] = pSrc[i * width + j];       
                        }
                }

                else  //逆时针
                {
                        for(int l = 0;l < width * height;l++)
                        {
                                int i = l/width;
                                int j = l%width;
                                pDes[j * height + height - 1 - i] = pSrc[i * width + j];
                        }
                }

                RGB24BitsBITMAPINFO.bmiHeader.biWidth = height;
                RGB24BitsBITMAPINFO.bmiHeader.biHeight = width;

最新回复

sWbytes = (width + 31)/8 * 3;  //source width in bytes dWbytes = (height + 31)/8 * 3; //dest width in bytes BYTE* pDes = (BYTE*)malloc(width * dWbytes); if(pDes == NULL) { return; } memset(pDes,0,width * dWbytes); BYTE* pSrc = (BYTE* )lpBitmapBits; if(nRotateType==1)//顺时针 { for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { pDest[i*dWbytes + j*3] = pSrc[(height-j-1)*sWbytes + i*3]; pDest[i*dWbytes + j*3 + 1] = pSrc[(height-j-1)*sWbytes + i*3 + 1]; pDest[i*dWbytes + j*3 + 2] = pSrc[(height-j-1)*sWbytes + i*3 + 2]; } } } else //逆时针 { for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { pDest[i*dWbytes + j*3] = pSrc[j*width*3 - i*3]; pDest[i*dWbytes + j*3 + 1] = pSrc[j*width*3 - i*3 + 1]; pDest[i*dWbytes + j*3 + 2] = pSrc[j*width*3 - i*3 + 2]; } } } 这个感觉更好些。。。。不知你有没有这个的工程。。  详情 回复 发表于 2007-8-28 14:04
点赞 关注

回复
举报

78

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
一个扫描行所占的字节数计算方法:
DataSizePerLine= (biWidth* biBitCount+31)/8;
// 一个扫描行所占的字节数
DataSizePerLine= DataSizePerLine/4*4; // 字节数必须是4的倍数

我在网上看到是这么说的,但不知在哪加上
 
 

回复

78

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
sWbytes = (width + 31)/8 * 3;  //source width in bytes
dWbytes = (height + 31)/8 * 3; //dest width in bytes

BYTE* pDes = (BYTE*)malloc(width * dWbytes);
if(pDes == NULL)
{
return;
}
memset(pDes,0,width * dWbytes);
BYTE* pSrc = (BYTE* )lpBitmapBits;

if(nRotateType==1)//顺时针
{
        for (i = 0; i < width; i++)
        {
                for (j = 0; j < height; j++)
                {
                        pDest[i*dWbytes + j*3] = pSrc[(height-j-1)*sWbytes + i*3];
                        pDest[i*dWbytes + j*3 + 1] = pSrc[(height-j-1)*sWbytes + i*3 + 1];
                        pDest[i*dWbytes + j*3 + 2] = pSrc[(height-j-1)*sWbytes + i*3 + 2];
                }
        }
}

else //逆时针
{
        for (i = 0; i < width; i++)
        {
                for (j = 0; j < height; j++)
                {
                        pDest[i*dWbytes + j*3] = pSrc[j*width*3 - i*3];
                        pDest[i*dWbytes + j*3 + 1] = pSrc[j*width*3 - i*3 + 1];
                        pDest[i*dWbytes + j*3 + 2] = pSrc[j*width*3 - i*3 + 2];
                }
        }
}
 
 
 

回复

71

帖子

0

TA的资源

一粒金砂(初级)

4
 
多谢,但好像并没有起作用,现在点了旋转按纽都没反应了
估计是不是数组溢出了
 
 
 

回复

68

帖子

0

TA的资源

一粒金砂(初级)

5
 
哎,还是没搞定。。。
sterrys(sterrys) 还在么
 
 
 

回复

64

帖子

0

TA的资源

一粒金砂(初级)

6
 
// BMP.cpp: implementation of the BMP class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Image Processing.h"
#include "BMP.h"
#include "stdio.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

BMP::BMP()
{
}

BMP::~BMP()
{

}

BOOL BMP::Test()
{
        lpSrcFile = "c:\\temp\\1.bmp";
        lpDstFile = "c:\\temp\\2.bmp";
        bmDegree = 180;
        Read();
        bmDstWidth = bmWidth;
        bmDstHeight = bmHeight;
        hDst = GlobalAlloc(GPTR, (INT)((bmDstWidth + 15) / 16) * 2 * bmDstHeight);
        bmDstBits = (LPBYTE)GlobalLock(hDst);
        Rotate();
        Write();
        if (hDst)
                GlobalUnlock(hDst);
        if (bmDstBits)
                GlobalFree(bmDstBits);
        return TRUE;
}

BOOL BMP::Read()
{
        BITMAPFILEHEADER fileHead;
        BITMAPINFOHEADER infoHead;
        DWORD size;

        HANDLE hRead;

        if (!(hRead = CreateFile(lpSrcFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)))
        {
                MessageBox(NULL, "Open file failed", "Error", MB_OK);
                return FALSE;
        }

        ReadFile(hRead, &fileHead, sizeof(BITMAPFILEHEADER), &size, NULL);
        if (fileHead.bfType != 0x4D42)
        {
                MessageBox(NULL, "Not BMP file", "Error", MB_OK);
                CloseHandle(hRead);
                return FALSE;
        }
        hSrc = GlobalAlloc(GPTR, (fileHead.bfSize - fileHead.bfOffBits));
        bmSrcBits = (LPBYTE)GlobalLock(hSrc);
        ReadFile(hRead, &infoHead, sizeof(BITMAPINFOHEADER), &size, NULL);

        bmWidth = infoHead.biWidth;
        bmWidthBytes = (INT)((bmWidth + 15) / 16) * 2;
        bmHeight = infoHead.biHeight;
        bmPlanes = infoHead.biPlanes;
        bmBitsPixel = infoHead.biBitCount;
        bmXReso = infoHead.biXPelsPerMeter;
        bmYReso = infoHead.biYPelsPerMeter;

        switch(infoHead.biBitCount)
        {
                case 1:
                        size = 2;
                        break;
                case 4:
                        size = 16;
                        break;
                case 8:
                        size = 256;
                        break;
                default:
                        size = 0;
                        break;
        }
        if (size > 0)
        {
                hPal = GlobalAlloc(GPTR, size * sizeof(DWORD));
                bmPallete = (LPDWORD)GlobalLock(hPal);
                ReadFile(hRead, bmPallete, size * sizeof(DWORD), &size, NULL);
        }

        SetFilePointer(hRead, fileHead.bfOffBits, NULL, FILE_BEGIN);
        ReadFile(hRead, bmSrcBits, (fileHead.bfSize - fileHead.bfOffBits), &size, NULL);

        CloseHandle(hRead);
        return TRUE;
}

BOOL BMP::Write()
{
        BITMAPFILEHEADER fileHead;
        BITMAPINFOHEADER infoHead;
        DWORD size, palSize;

        HANDLE hWrite;

        memset(&fileHead, 0, sizeof(BITMAPFILEHEADER));
        memset(&infoHead, 0, sizeof(BITMAPINFOHEADER));
        if (!(hWrite = CreateFile(lpDstFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)))
        {
                MessageBox(NULL, "Open file failed", "Error", MB_OK);
                return FALSE;
        }

        infoHead.biSize = sizeof(BITMAPINFOHEADER);
        infoHead.biWidth = bmDstWidth;
        infoHead.biHeight = bmDstHeight;
        infoHead.biPlanes = bmPlanes;
        infoHead.biBitCount = bmBitsPixel;
        infoHead.biClrImportant = 2;
        infoHead.biXPelsPerMeter = bmXReso;
        infoHead.biYPelsPerMeter = bmYReso;
        infoHead.biSizeImage = ((INT)((infoHead.biWidth * infoHead.biBitCount + 15) / 16) * 2 * infoHead.biHeight);

        fileHead.bfType = 0x4D42; //'BM'
        switch (infoHead.biBitCount)
        {
                case 1:
                        palSize = 2;
                        break;
                case 4:
                        palSize = 16;
                        break;
                case 8:
                        palSize = 256;
                        break;
                default:
                        palSize = 0;
                        break;
        }
        fileHead.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + palSize * sizeof(DWORD);
        fileHead.bfSize = fileHead.bfOffBits; //head size
        fileHead.bfSize += infoHead.biSizeImage; //data size

        WriteFile(hWrite, &fileHead, sizeof(BITMAPFILEHEADER), &size, NULL);
        WriteFile(hWrite, &infoHead, sizeof(BITMAPINFOHEADER), &size, NULL);
        WriteFile(hWrite, bmPallete, palSize * sizeof(DWORD), &size, NULL);

        WriteFile(hWrite, bmDstBits, (fileHead.bfSize - fileHead.bfOffBits), &size, NULL);

        CloseHandle(hWrite);

        if (hPal)
                GlobalUnlock(hPal);
        if (bmPallete)
                GlobalFree(bmPallete);
        if (hSrc)
                GlobalUnlock(hSrc);
        if (bmSrcBits)
                GlobalFree(bmSrcBits);

        return TRUE;
}

bool BMP::Rotate()
{
        int i, j, k;
        int pos1, pos2;

        switch (bmDegree)
        {
                case 180:
                        switch (bmBitsPixel)
                        {
                                case 1:
                                        for (j = 0; j < bmHeight; j++)
                                        {
                                                if (bmWidthBytes == (bmWidth / 8))
                                                {
                                                        for (i = 0; i < bmWidthBytes; i++)
                                                        {
                                                                pos1 = (j * bmWidthBytes + i);
                                                                pos2 = (bmHeight - j - 1) * bmWidthBytes + bmWidth / 8 - 1 - i;
                                                                *(bmDstBits + pos1) = shift[*(bmSrcBits + pos2)];
                                                        }
                                                }
                                                else
                                                {
                                                        for (i = 0; i < bmWidth; i++)
                                                        {
                                                                pos1 = (j * bmWidthBytes + i / 8);
                                                                pos2 = (j * bmWidthBytes + (bmWidth - 1 - i) / 8);
                                                        }
                                                }
                                        }
                                        break;
                                case 4:
                                        break;
                                case 8:
                                case 16:
                                case 24:
                                case 32:
                                        for (j = 0; j < bmHeight; j++)
                                        {
                                                for (i = 0; i < bmWidth; i++)
                                                {
                                                        pos1 = j * bmWidthBytes + i * bmBitsPixel / 8;
                                                        pos2 = (bmHeight - j - 1) * bmWidthBytes + (bmWidth - i - 1) * bmBitsPixel / 8;
                                                        for (k = 0; k < bmBitsPixel / 8; k++)
                                                        {
                                                                *(bmDstBits + pos1 + k) = *(bmSrcBits + pos2 + k);
                                                        }
                                                }
                                        }
                                        break;
                                default:
                                        memcpy(bmDstBits, bmSrcBits, bmHeight * bmWidthBytes);
                                        break;
                        }
                        break;
                case 90:
                        switch (bmBitsPixel)
                        {
                                case 1:
                                        break;
                                case 4:
                                        break;
                                case 8:
                                case 16:
                                case 24:
                                case 32:
                                        LONG bmDstWidth, bmDstWidthBytes, bmDstHeight;
                                        int pos1, pos2;

                                        bmDstHeight = bmWidth;
                                        bmDstWidth = bmHeight;
                                        bmDstWidthBytes = (int)((bmDstWidth * bmBitsPixel + 15) / 16) * 2;
                                        for (j = 0; j < bmDstHeight; j++)
                                        {
                                                for (i = 0; i < bmDstWidth; i++)
                                                {
                                                        pos1 = j * bmDstWidthBytes + i * bmBitsPixel / 8;
                                                        pos2 = i * bmWidthBytes + (bmWidth - j - 1) * bmBitsPixel / 8;
                                                        for (k = 0; k < bmBitsPixel / 8; k++)
                                                        {
                                                                *(bmDstBits + pos1 + k) = *(bmSrcBits + pos2 + k);
                                                        }
                                                }
                                        }
                                        break;
                                default:
                                        memcpy(bmDstBits, bmSrcBits, bmHeight * bmWidthBytes);
                                        break;
                        }
                        break;
                case 270:
                        switch (bmBitsPixel)
                        {
                                case 1:
                                        break;
                                case 4:
                                        break;
                                case 8:
                                case 16:
                                case 24:
                                case 32:
                                        LONG bmDstWidth, bmDstWidthBytes, bmDstHeight;
                                        int pos1, pos2;

                                        bmDstHeight = bmWidth;
                                        bmDstWidth = bmHeight;
                                        bmDstWidthBytes = (int)((bmDstWidth * bmBitsPixel + 15) / 16) * 2;
                                        for (j = 0; j < bmDstHeight; j++)
                                        {
                                                for (i = 0; i < bmDstWidth; i++)
                                                {
                                                        pos1 = j * bmDstWidthBytes + i * bmBitsPixel / 8;
                                                        pos2 = (bmHeight - i - 1) * bmWidthBytes + j * bmBitsPixel / 8;
                                                        for (k = 0; k < bmBitsPixel / 8; k++)
                                                        {
                                                                *(bmDstBits + pos1 + k) = *(bmSrcBits + pos2 + k);
                                                        }
                                                }
                                        }
                                        break;
                                default:
                                        break;
                        }
                        break;
                case 0:
                        memcpy(bmDstBits, bmSrcBits, bmHeight * bmWidthBytes);
                        break;
                default:
                        memcpy(bmDstBits, bmSrcBits, bmHeight * bmWidthBytes);
                        break;
        }
        return TRUE;
}

 
 
 

回复

69

帖子

0

TA的资源

一粒金砂(初级)

7
 
代码大意是看懂了。。。不过太多了。
像我发的那段代码怎么改呢。。
多谢
或者能把这个测试的整个工程文件发我一下么

happytrm@gmail.com
 
 
 

回复

76

帖子

0

TA的资源

一粒金砂(初级)

8
 
图片分辨率都是4的倍数哦。
 
 
 

回复

82

帖子

0

TA的资源

一粒金砂(初级)

9
 
是哈,知道是4的倍数,可就不知道怎么改了呵.....
sterrys(sterrys)给了不少帮助,可就是不起作用呵..
 
 
 

回复

74

帖子

0

TA的资源

一粒金砂(初级)

10
 
还不行?
如果是VC做的,把全套的代码发来给你调试一下
 
 
 

回复

68

帖子

0

TA的资源

一粒金砂(初级)

11
 
是eVC做的,我知道应该就在
for(int l = 0;l < width * height;l++)
{
int i = l/width;
int j = l%width;
pDes[(width - 1 - j) * height + i] = pSrc[i * width + j];
}
这个地方进行扫描的控制,但不知道如何处理,手头也没有可参考的资料
 
 
 

回复

60

帖子

0

TA的资源

一粒金砂(初级)

12
 
sWbytes = (width + 31)/8 * 3;  //source width in bytes
dWbytes = (height + 31)/8 * 3; //dest width in bytes

BYTE* pDes = (BYTE*)malloc(width * dWbytes);
if(pDes == NULL)
{
return;
}
memset(pDes,0,width * dWbytes);
BYTE* pSrc = (BYTE* )lpBitmapBits;

if(nRotateType==1)//顺时针
{
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
pDest[i*dWbytes + j*3] = pSrc[(height-j-1)*sWbytes + i*3];
pDest[i*dWbytes + j*3 + 1] = pSrc[(height-j-1)*sWbytes + i*3 + 1];
pDest[i*dWbytes + j*3 + 2] = pSrc[(height-j-1)*sWbytes + i*3 + 2];
}
}
}

else //逆时针
{
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
pDest[i*dWbytes + j*3] = pSrc[j*width*3 - i*3];
pDest[i*dWbytes + j*3 + 1] = pSrc[j*width*3 - i*3 + 1];
pDest[i*dWbytes + j*3 + 2] = pSrc[j*width*3 - i*3 + 2];
}
}
}


这个感觉更好些。。。。不知你有没有这个的工程。。
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表